Completed
Pull Request — master (#2580)
by Lakshmi
07:17
created

main()   A

Complexity

Conditions 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 7
rs 9.4285
cc 2
1
#!/usr/bin/env python
2
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
3
# contributor license agreements.  See the NOTICE file distributed with
4
# this work for additional information regarding copyright ownership.
5
# The ASF licenses this file to You under the Apache License, Version 2.0
6
# (the "License"); you may not use this file except in compliance with
7
# the License.  You may obtain a copy of the License at
8
#
9
#     http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS,
13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
# See the License for the specific language governing permissions and
15
# limitations under the License.
16
17
"""
18
A migration script that cleans up old queues.
19
"""
20
21
import traceback
22
23
from kombu import Connection
24
from st2common import config
25
from st2common.transport import reactor
26
from st2common.transport import utils as transport_utils
27
28
29
class Migrate_0_13_x_to_1_1_0(object):
30
    """
31
    Handles migration of messaging setup from 0.13.x to 1.1.
32
    """
33
34
    # All these queues are either not required due to name
35
    # changes or changes in durability proeprties.
36
    OLD_QS = [
37
        # Name changed in 1.1
38
        reactor.get_trigger_cud_queue('st2.trigger.watch.timers', routing_key='#'),
39
        # Split to multiple queues in 1.1
40
        reactor.get_trigger_cud_queue('st2.trigger.watch.sensorwrapper', routing_key='#'),
41
        # Name changed in 1.1
42
        reactor.get_trigger_cud_queue('st2.trigger.watch.webhooks', routing_key='#')
43
    ]
44
45
    def migrate(self):
46
        self._cleanup_old_queues()
47
48
    def _cleanup_old_queues(self):
49
        with Connection(transport_utils.get_messaging_urls()) as connection:
50
            for q in self.OLD_QS:
51
                bound_q = q(connection.default_channel)
52
                try:
53
                    bound_q.delete()
54
                except:
55
                    print('Failed to delete %s.' % q.name)
56
                    traceback.print_exc()
57
58
59
def main():
60
    try:
61
        migrator = Migrate_0_13_x_to_1_1_0()
62
        migrator.migrate()
63
    except:
64
        print('Messaging setup migration failed.')
65
        traceback.print_exc()
66
67
68
if __name__ == '__main__':
69
    config.parse_args(args={})
70
    main()
71