Completed
Pull Request — master (#305)
by Manas
01:56
created

BaseAction.vpc_connect()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 4
rs 10
1
import re
2
import eventlet
3
import importlib
4
5
import boto.ec2
6
import boto.route53
7
import boto.vpc
8
9
from st2actions.runners.pythonrunner import Action
10
from ec2parsers import ResultSets
11
12
13
class BaseAction(Action):
14
15
    def __init__(self, config):
16
        super(BaseAction, self).__init__(config)
17
        if config['st2_user_data'] is not "":
18
            self.userdata = open(config['st2_user_data'], 'r').read()
19
        else:
20
            self.userdata = None
21
        self.setup = config['setup']
22
        self.resultsets = ResultSets()
23
24
    def ec2_connect(self):
25
        region = self.setup['region']
26
        del self.setup['region']
27
        return boto.ec2.connect_to_region(region, **self.setup)
28
29
    def vpc_connect(self):
30
        region = self.setup['region']
31
        del self.setup['region']
32
        return boto.vpc.connect_to_region(region, **self.setup)
33
34
    def r53_connect(self):
35
        del self.setup['region']
36
        return boto.route53.connection.Route53Connection(**self. setup)
37
38
    def get_r53zone(self, zone):
39
        conn = self.r53_connect()
40
        return conn.get_zone(zone)
41
42
    def st2_user_data(self):
43
        return self.userdata
44
45
    def split_tags(self, tags):
46
        tag_dict = {}
47
        split_tags = tags.split(',')
48
        for tag in split_tags:
49
            if re.search('=', tag):
50
                k, v = tag.split('=', 1)
51
                tag_dict[k] = v
52
        return tag_dict
53
54
    def wait_for_state(self, instance_id, state, timeout=10, retries=3):
55
        state_list = {}
56
        obj = self.ec2_connect()
57
        eventlet.sleep(timeout)
58
        instance_list = []
59
60
        for _ in range(retries + 1):
61
            try:
62
                instance_list = obj.get_only_instances([instance_id, ])
63
            except Exception:
64
                self.logger.info("Waiting for instance to become available")
65
                eventlet.sleep(timeout)
66
67
        for instance in instance_list:
68
            try:
69
                current_state = instance.update()
70
            except Exception, e:
71
                self.logger.info("Instance (%s) not listed. Error: %s" %
72
                                 (instance_id, e))
73
                eventlet.sleep(timeout)
74
75
            while current_state != state:
76
                current_state = instance.update()
77
            state_list[instance_id] = current_state
78
        return state_list
79
80
    def do_method(self, module_path, cls, action, **kwargs):
81
        module = importlib.import_module(module_path)
82
        # hack to connect to correct region
83
        if cls == 'EC2Connection':
84
            obj = self.ec2_connect()
85
        elif cls == 'VPCConnection':
86
            obj = self.vpc_connect()
87
        elif module_path == 'boto.route53.zone' and cls == 'Zone':
88
            zone = kwargs['zone']
89
            del kwargs['zone']
90
            obj = self.get_r53zone(zone)
91
        else:
92
        #elif cls == 'Route53Connection' or cls == 'IAMConnection' or cls == 'S3Connection':
93
            del self.setup['region']
94
            obj = getattr(module, cls)(**self.setup)
95
        resultset = getattr(obj, action)(**kwargs)
96
        return self.resultsets.formatter(resultset)
97
98
    def do_function(self, module_path, action, **kwargs):
99
        module = __import__(module_path)
100
        return getattr(module, action)(**kwargs)
101