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