-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenge7.py
157 lines (136 loc) · 5.67 KB
/
challenge7.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
153
154
155
156
157
#!/usr/bin/env python
# Copyright 2013 John Schutz
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Challenge 7
# Write a script that will create 2 Cloud Servers and add them as
# nodes to a new Cloud Load Balancer.
# Worth 3 Points
import pyrax
import os
from sys import exit
from time import sleep
from re import match
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument("-l", dest="lbname", default="testlb",
help="name the load balancer LBNAME", metavar="LBNAME")
parser.add_argument("-v", dest="port", type=int, default=80,
help="VIP port VIPPORT", metavar="VIPPORT")
parser.add_argument("-p", dest="srvport", type=int, default=80,
help="load balancer connects to TCP SRVPORT on nodes",
metavar="SRVPORT")
parser.add_argument("-o", dest="protocol", default="HTTP",
help="load balance protocol PROTOCOL", metavar="PROTOCOL")
parser.add_argument("-i", dest="image",
default="e4dbdba7-b2a4-4ee5-8e8f-4595b6d694ce",
help="use cloud image id IMAGE", metavar="IMAGE")
parser.add_argument("-s", dest="flavor", default="2",
help="use cloud flavor id FLAVOR", metavar="FLAVOR")
parser.add_argument("-n", dest="name", default="web",
help="use base server name NAME followed by instance\
number (ie. web1, web2, etc)", metavar="NAME")
parser.add_argument("-c", dest="count", type=int, default=2,
help="create COUNT cloud servers", metavar="COUNT")
parser.add_argument("-t", dest="interval", type=int, default=5,
help="wait INTERVAL seconds between status checks",
metavar="INTERVAL")
parser.add_argument("-w", dest="private", default=False, action="store_true",
help="create private VIP on servicenet")
parser.add_argument("-d", dest="dc", default="DFW",
help="use data center DC (DFW or ORD)", metavar="DC")
args = parser.parse_args()
pyrax.set_credential_file(os.path.expanduser("~/.rackspace_cloud_credentials"))
cs = pyrax.connect_to_cloudservers(region=args.dc)
clb = pyrax.connect_to_cloud_loadbalancers(region=args.dc)
# check if image is valid
images = cs.images.list()
image = None
for i in images:
if i.id == args.image:
image = i
if image is None:
print "Image %s not found. Please specify an existing image ID:" % (args.image)
for i in images:
print "ID: %s Name: %s" % (i.id, i.name)
exit(1)
# insure flavor is valid
flavors = cs.flavors.list()
flavor = None
for f in flavors:
if f.id == args.flavor:
flavor = f
if flavor is None:
print "Flavor %s not found. Please specify an existing flavor ID:" % (args.flavor)
for f in flavors:
print "ID: %s Name: %s" % (f.id, f.name)
exit(1)
print "Creating %d servers" % (args.count)
print "Image: %s" % (cs.images.get(args.image).name)
print "Flavor: %s" % (cs.flavors.get(args.flavor).name)
print "Name base: %s" % (args.name)
# build servers
servers = []
for i in xrange(0, args.count):
name = '%s%d' % (args.name, i+1)
print "Creating server %s..." % (name)
servers.append(cs.servers.create(name, args.image, args.flavor))
# monitor for server build completion
completed = []
while len(completed) < args.count:
sleep(args.interval)
print "\nChecking status"
for server in servers:
if server not in completed:
server.get()
if server.status == "BUILD":
print "%s is still building" % (server.name)
elif server.status == "ACTIVE":
print "%s is finished building" % (server.name)
completed.append(server)
else:
sname = server.name
print "UNKNOWN SERVER STATUS FOR %s: %s...exiting..." \
% (sname, server.status)
exit(1)
# print server login information
print
for server in completed:
ipv4 = 0
ipv6 = 1
if match('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', server.networks['public'][1]):
ipv4 = 1
ipv6 = 0
print "Server : %s\nIP : %s\nIPv6 : %s\nUsername: %s\nPassword: %s\n" \
% (server.name, server.networks['public'][ipv4],
server.networks['public'][ipv6], "root", server.adminPass)
# configure LB with built servers
print
print "Building load balancer %s" % (args.lbname)
nodes = []
for i in completed:
i.get()
nodes.append(clb.Node(address=i.networks["private"][0], port=args.srvport,
condition="ENABLED"))
# set if VIP will be private or not
if not args.private:
vip = clb.VirtualIP(type="PUBLIC")
else:
vip = clb.VirtualIP(type="SERVICENET")
# create load balancer
lb = clb.create(args.lbname, port=args.port, protocol=args.protocol, nodes=nodes,
virtual_ips=[vip])
# monitor for load balancer build completion
while lb.status != "ACTIVE":
sleep(args.interval)
print "Checking status"
lb.get()
# output load balancer VIP
print "Load balancer created. Name: %s Virtual IP: %s" % (lb.name, lb.virtual_ips[0].address)