forked from markcaudill/haproxy-autoscale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
haproxy_autoscale.py
88 lines (75 loc) · 3.04 KB
/
haproxy_autoscale.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from boto.ec2 import EC2Connection
from boto.ec2.securitygroup import SecurityGroup
import logging
from mako.template import Template
import urllib2
def get_self_instance_id():
'''
Get this instance's id.
'''
logging.debug('get_self_instance_id()')
response = urllib2.urlopen('http://169.254.169.254/1.0/meta-data/instance-id')
instance_id = response.read()
return instance_id
def steal_elastic_ip(access_key=None, secret_key=None, ip=None):
'''
Assign an elastic IP to this instance.
'''
logging.debug('steal_elastic_ip()')
instance_id = get_self_instance_id()
conn = EC2Connection(aws_access_key_id=access_key,
aws_secret_access_key=secret_key)
conn.associate_address(instance_id=instance_id, public_ip=ip)
def get_running_instances(access_key=None, secret_key=None, security_group=None):
'''
Get all running instances. Only within a security group if specified.
'''
logging.debug('get_running_instances()')
instances_all_regions_list = []
conn = EC2Connection(aws_access_key_id=access_key,
aws_secret_access_key=secret_key)
ec2_region_list = conn.get_all_regions()
if security_group:
for index, region in enumerate(ec2_region_list):
conn = EC2Connection(aws_access_key_id=access_key,
aws_secret_access_key=secret_key,
region=ec2_region_list[index])
sg = SecurityGroup(connection=conn, name=security_group)
running_instances = [i for i in sg.instances() if i.state == 'running']
if running_instances:
for instance in running_instances:
instances_all_regions_list.append(instance)
else:
for index, region in enumerate(ec2_region_list):
conn = EC2Connection(aws_access_key_id=access_key,
aws_secret_access_key=secret_key,
region=ec2_region_list[index])
reserved_instances = conn.get_all_instances()
if reserved_instances:
for reservation in reserved_instances:
for instance in reservation.instances:
if instance.stat == 'running':
instances_all_regions_list.append(instance)
return instances_all_regions_list
def file_contents(filename=None, content=None):
'''
Just return the contents of a file as a string or write if content
is specified. Returns the contents of the filename either way.
'''
logging.debug('file_contents()')
if content:
f = open(filename, 'w')
f.write(content)
f.close()
try:
f = open(filename, 'r')
text = f.read()
f.close()
except:
text = None
return text
def generate_haproxy_config(template=None, instances=None):
'''
Generate an haproxy configuration based on the template and instances list.
'''
return Template(filename=template).render(instances=instances)