This repository has been archived by the owner on Sep 23, 2020. It is now read-only.
/
de_tutorial.py
executable file
·153 lines (118 loc) · 5.58 KB
/
de_tutorial.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import os
import time
import urlparse
import boto
import boto.ec2.autoscale
#from boto.exception import BotoServerError
from boto.regioninfo import RegionInfo
from boto.ec2.autoscale import Tag
from boto.ec2.autoscale.launchconfig import LaunchConfiguration
from boto.ec2.autoscale.group import AutoScalingGroup
class MyPhantomDecisionEngine(object):
def __init__(self):
self.username = os.environ['EC2_ACCESS_KEY']
self.password = os.environ['EC2_SECRET_KEY']
self.iaas_url = os.environ['PHANTOM_URL']
self.domain_name = "my_domain"
self.launch_config_name = "my_launch_config"
self.vm_image = "hello-phantom.gz"
self.max_vms = 4
self.key_name = "phantomkey"
self.image_type = "m1.small"
self.clouds = ["hotel", "sierra"]
# Create our Phantom connection
parsed_url = urlparse.urlparse(self.iaas_url)
ssl = parsed_url.scheme == "https"
host = parsed_url.hostname
port = parsed_url.port
region = RegionInfo(name="nimbus", endpoint=host)
self.connection = boto.ec2.autoscale.AutoScaleConnection(
aws_access_key_id=self.username,
aws_secret_access_key=self.password,
is_secure=ssl, port=port, debug=2, region=region,
validate_certs=False)
self.connection.host = host
self.create_launch_configuration()
self.create_domain()
self.run_policy()
def create_launch_configuration(self):
#create_domain Get a list of existing launch configurations
existing_launch_configurations = self.connection.get_all_launch_configurations()
existing_lc_names = [lc.name for lc in existing_launch_configurations]
# Create launch configurations that don't exist
for cloud in self.clouds:
full_lc_name = "%s@%s" % (self.launch_config_name, cloud)
if not full_lc_name in existing_lc_names:
print "Creating launch config '%s'" % full_lc_name
launch_config = LaunchConfiguration(
self.connection, name=full_lc_name, image_id=self.image,
key_name=self.key_name, security_groups=['default'],
instance_type=self.image_type)
self.connection.create_launch_configuration(launch_config)
else:
print "Launch config '%s' has already been added, skipping..." % (full_lc_name,)
def create_domain(self):
# Set our policy name
policy_name_key = 'PHANTOM_DEFINITION'
policy_name = 'error_overflow_n_preserving'
# Set the order of clouds in which VMs are started
ordered_clouds_key = 'clouds'
ordered_clouds = ""
cloud_size_pairs = ["%s:%s" % (cloud, self.max_vms) for cloud in self.clouds]
ordered_clouds = ",".join(cloud_size_pairs)
# Get a Cloud and Launch Config to feed to the domain constructor
a_cloud = self.clouds[0]
a_lc_name = "%s@%s" % (self.launch_config_name, a_cloud)
a_lc_list = self.connection.get_all_launch_configurations(names=[a_lc_name, ])
if len(a_lc_list) != 1:
raise SystemExit("Couldn't get launch config %s" % self.launch_config_name)
a_lc = a_lc_list[0]
# Set how many domains we would like to start our domain with
n_preserve_key = 'minimum_vms'
n_preserve = 0
# Marshall Phantom Parameters
policy_tag = Tag(connection=self.connection, key=policy_name_key,
value=policy_name, resource_id=self.domain_name)
clouds_tag = Tag(connection=self.connection, key=ordered_clouds_key,
value=ordered_clouds, resource_id=self.domain_name)
npreserve_tag = Tag(connection=self.connection, key=n_preserve_key,
value=n_preserve, resource_id=self.domain_name)
tags = [policy_tag, clouds_tag, npreserve_tag]
# Remove any existing domain name with the same name
existing_domains = self.connection.get_all_groups(names=[self.domain_name, ])
for domain in existing_domains:
print "Removing existing instance of domain '%s'" % domain.name
domain.delete()
# Create our domain
print "Creating domain %s" % self.domain_name
domain = AutoScalingGroup(
availability_zones=["us-east-1"],
connection=self.connection, group_name=self.domain_name,
min_size=n_preserve, max_size=n_preserve, launch_config=a_lc, tags=tags)
self.connection.create_auto_scaling_group(domain)
def run_policy(self):
domains = self.connection.get_all_groups(names=[self.domain_name, ])
if len(domains) != 1:
raise SystemExit("Couldn't get domain %s" % self.domain_name)
domain = domains[0]
capacity = 1
print "set %s capacity to %s" % (self.domain_name, capacity)
domain.set_capacity(capacity)
time.sleep(10)
capacity += 1
print "set %s capacity to %s" % (self.domain_name, capacity)
domain.set_capacity(capacity)
time.sleep(10)
capacity += 1
print "set %s capacity to %s" % (self.domain_name, capacity)
domain.set_capacity(capacity)
time.sleep(10)
capacity += 1
print "set %s capacity to %s" % (self.domain_name, capacity)
domain.set_capacity(capacity)
print "let domain settle for 60s"
time.sleep(60)
capacity = 0
print "set %s capacity back to %s" % (self.domain_name, capacity)
domain.set_capacity(capacity)
MyPhantomDecisionEngine()