-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenge11.py
225 lines (199 loc) · 9.03 KB
/
challenge11.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env python
#
# Create an SSL terminated load balancer (Create self-signed certificate.)
# Create a DNS record that should be pointed to the load balancer.
# Create Three servers as nodes behind the LB.
# Each server should have a CBS volume attached to it.
# (Size and type are irrelevant.)
# All three servers should have a private Cloud Network
# shared between them.
# Login information to all three servers returned in a readable format as
# the result of the script, including connection information.
import os
import pyrax
import pyrax.exceptions as e
import sys
from time import sleep
# User configurable settings
server_names = ['LBaaSWeb1', 'LBaaSWeb2', 'LBaaSWeb3']
clb_name = "TestLB"
new_network_name = 'MY_NETWORK'
new_network_cidr = '192.168.0.0/24'
lb_cert = ('-----BEGIN CERTIFICATE-----\n'
'MIIDqzCCAxSgAwIBAgIJAOzGQ/iHv6qNMA0GCSqGSIb3DQEBBQUAMIGWMQswCQYD\n'
'VQQGEwJVUzEOMAwGA1UECBMFVGV4YXMxFDASBgNVBAcTC1NhbiBBbnRvbmlvMRIw\n'
'EAYDVQQKEwlSYWNrc3BhY2UxDzANBgNVBAsTBkRld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RIVATE KEY-----')
# Non-configurable code is below
# Statically configured credentials file
credentials_file = os.path.expanduser('~/.rackspace_cloud_credentials')
# Check to make sure we can access the credentials file and authenticate.
try:
pyrax.set_credential_file(credentials_file)
except e.AuthenticationFailed:
print ('Authentication Failed: Ensure valid credentials in {}'
.format(credentials_file))
except e.FileNotFound:
print ('File Not Found: Make sure a valid credentials file is located at'
'{}'.format(credentials_file))
# Initilize pyrax for Cloud Servers
cs = pyrax.cloudservers
# Initilize pyrax for Cloud Load Balancers
clb = pyrax.cloud_loadbalancers
# Initilize pyrax for Cloud Block Storage
cbs = pyrax.cloud_blockstorage
# Initilize pyrax for Cloud Networks
cnw = pyrax.cloud_networks
# Set default image for CentOS 6.3
image = 'c195ef3b-9195-4474-b6f7-16e5bd86acd0'
# Set default flavof of 512M
flavor = '2'
# Initilize out LB nodes
nodes = []
# Create the new network
my_net = cnw.create(new_network_name, cidr=new_network_cidr)
print 'Created Cloud Network:', my_net
# For each of the server names in server_names, create a server
for server_name in server_names:
# Add our Cloud Network interface
networks = my_net.get_server_networks(public=True, private=True)
create_server = cs.servers.create(server_name, image, flavor,
nics=networks)
# Server information stored in dictionary for easy access
server_info = {'id': create_server.id,
'name': server_name,
'admin_pass': create_server.adminPass}
# Give the end user a status update
print 'Building Server {}'.format(server_name)
# Servers take a bit of time to build, the initial information only
# contains the admin password. We polll every 5 seconds for a
# complete ACTIVE status which means it is done
server = cs.servers.get(server_info['id'])
while server.status != 'ACTIVE':
sleep(5)
server = cs.servers.get(server_info['id'])
if server.status == 'ERROR' or server.status == 'UNKNOWN':
print 'Server build error! Status: {}'.format(server.status)
sys.exit()
else:
sys.stdout.write('\rServer Status: {} {}%'
.format(server.status, server.progress))
sys.stdout.flush()
# Now that server is ACTIVE present all information
print ('\nBuild Complete!\n'
'Name: {}\n'
'Admin Password: {}\n'
'Public IPv4: {}\n'
'Public IPv6: {}'
.format(server_info['name'],
server_info['admin_pass'],
server.accessIPv4,
server.accessIPv6))
# Crate and attach a CBS volume
print 'Creating a Cloud Block Storage Volume and attaching...'
create_cbs = cbs.create(name=server_info['name'] + 'CBS',
size=100, volume_type='SATA')
create_cbs.attach_to_instance(server_info['id'], '/dev/xvdb')
print 'Success!\n'
# Grab the private IP
ips = server.addresses
private = ips['private'][0]['addr']
# Add as a node for the future
nodes.append(clb.Node(private, port=80, condition='ENABLED'))
# Proceed to create a Cloud Load Balancer
vip = clb.VirtualIP(type="PUBLIC")
create_lb = clb.create(clb_name, port=80, protocol="HTTP",
nodes=nodes, virtual_ips=[vip])
# Build out the load balancer
print "Building Load Balancer {}".format(clb_name)
lb = clb.get(create_lb.id)
while lb.status != 'ACTIVE':
sleep(5)
lb = clb.get(create_lb.id)
if lb.status == 'ERROR' or lb.status == 'UNKNOWN':
print 'Load Balancer build error! Status: {}'.format(lb.status)
sys.exit()
else:
sys.stdout.write('\rLoad Balancer Status: {}'
.format(lb.status))
sys.stdout.flush()
# Once done we relay all the data to the user
print ("\nSuccess!\nLoad Balancer VIP: {}".format(lb.virtual_ips[0].address))
# Add SSL terminiation with certificate and key
print "Adding SSL termination with a self signed certificate..."
lb.add_ssl_termination(securePort=443, secureTrafficOnly=False,
certificate=lb_cert, privatekey=lb_key)
lb = clb.get(lb.id)
while lb.status != 'ACTIVE':
sleep(1)
lb = clb.get(lb.id)
if lb.status == 'ERROR' or lb.status == 'UNKNOWN':
print 'Load Balancer build error! Status: {}'.format(lb.status)
sys.exit()
else:
sys.stdout.write('\rLoad Balancer ReBuild Status: {}'
.format(lb.status))
sys.stdout.flush()
print "\nSuccess!"
# Set up DNS records pointing to our load balancer IP
fqdn = raw_input("Enter a FQDN to create DNS records that point"
" to the new LB: ")
# Initilize pyrax for clouddns
dns = pyrax.cloud_dns
# Check to see if domain exist
try:
domain = dns.find(name=fqdn)
print "Domain found: Continuing"
except e.NotFound as err:
print "Domain not found: Creating domain."
try:
domain = dns.create(name=fqdn, emailAddress="admin@" + fqdn,
ttl=900, comment="automaticly added domain")
except e.DomainCreationFailed as e:
print "Domain creation failed:", e
sys.exit()
print "Adding DNS A record."
record = [{
"type": 'A',
"name": fqdn,
"data": lb.virtual_ips[0].address,
"ttl": 300,
}]
try:
new_record = domain.add_record(record)
except e.DomainRecordAdditionFailed as err:
print "ERROR: {}".format(err)
sys.exit()
print "Record added!"