forked from tclark/virt-openstack-assignment
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment.py
More file actions
265 lines (226 loc) · 10.7 KB
/
assignment.py
File metadata and controls
265 lines (226 loc) · 10.7 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import argparse
import openstack
conn = openstack.connect(cloud_name='openstack')
prefix = 'hookke2'
IMAGE = 'ubuntu-minimal-22.04-x86_64'
FLAVOUR = 'c1.c1r1'
NETWORK = prefix + '-net'
SUBNET = prefix + '-subnet'
ROUTER = prefix + '-rtr'
KEYPAIR = prefix + '-key'
SECURITY_GROUP = 'assignment2'
CIDR = '192.168.50.0/24'
SERVERS = [prefix + '-web',prefix + '-app',prefix + '-db']
WEBSERVER = prefix + '-web'
image = conn.compute.find_image(IMAGE)
flavour = conn.compute.find_flavor(FLAVOUR)
keypair = conn.compute.find_keypair(KEYPAIR)
public_net = conn.network.find_network ('public-net')
securityGroup = conn.network.find_security_group(SECURITY_GROUP)
netwk = conn.network.find_network(NETWORK)
subnt = conn.network.find_subnet(SUBNET)
routr = conn.network.find_router(ROUTER)
def create():
''' Create a set of Openstack resources '''
# Look for a network. If no network exists, create one
print("Checking network...")
if netwk is None:
network = conn.network.create_network(name=NETWORK)
print("Network ", NETWORK, "has been created")
else:
print("Network ", NETWORK, "already exists")
# Look for subnet. If no subnet exists, create one
print("Checking subnet...")
if subnt is None:
subnet = conn.network.create_subnet(name=SUBNET, network_id=network.id, cidr=CIDR, ip_version='4')
print("Subnet", SUBNET, "has been created")
else:
print("Subnet", SUBNET, "already exists")
# Look for a router. If no router exists, create one and assign the external gateway. Connect private network to router
print("Checking for router...")
if routr is None:
router = conn.network.create_router(name=ROUTER, external_gateway_info={"network_id": public_net.id})
print("Router", ROUTER, "has been created")
conn.network.add_interface_to_router(router,subnet.id)
else:
print("Router", ROUTER, "already exists")
# Iterate through SERVERS. If server doesn't exist, create it. If the server is the WEBSERVER, associate a floating IP for external network access.
for server in SERVERS:
serverName = server
serverFind = conn.compute.find_server(serverName)
print("Checking for servers...")
if serverFind is None:
print("Creating", serverName)
server = conn.compute.create_server(name=serverName, image_id=image.id, flavor_id=flavour.id, networks=[{"uuid": network.id}], key_name=keypair.name, security_groups=[{"name": securityGroup.id}])
server = conn.compute.wait_for_server(server)
print("Server",server.name, server.id, "created")
if serverName == WEBSERVER:
floating_ip = conn.network.create_ip(floating_network_id=public_net.id)
conn.compute.add_floating_ip_to_server(server, floating_ip.floating_ip_address)
print("Associating floating ip address:", floating_ip.floating_ip_address,"to webserver")
else:
print("Server",serverName,"No floating IP associated")
else:
print("Server",serverName, serverFind.id,"already exists")
# pass
def run():
''' Start a set of Openstack virtual machines
if they are not already running.
'''
print("Running...")
print("Checking for stopped servers...")
# Iterate through SERVERS. If server exists, and has status 'shutoff', start it.
# If server is "WEBSERVER" optional choice to reassociate floating IP address
for server in SERVERS:
serverName = server
serverFind = conn.compute.find_server(serverName)
if serverFind is not None:
server = conn.compute.get_server(serverFind)
# Un-comment below section to reassociate a floating IP address on start-up
# if serverName == WEBSERVER:
# avail_floatingIP = conn.network.find_available_ip()
# print("Checking for available ip address")
# try:
# if avail_floatingIP:
# conn.compute.add_floating_ip_to_server(server, avail_floatingIP.floating_ip_address)
# print("Associating floating ip address:",avail_floatingIP.floating_ip_address,"to webserver")
# else:
# new_floatingIP = conn.network.create_ip(floating_network_id=public_net.id)
# conn.compute.add_floating_ip_to_server(server, new_floatingIP.floating_ip_address)
# print("Associating floating ip address:", new_floatingIP.floating_ip_address,"to webserver")
# except:
# print("Error: Check status of floating ip address")
if server.status == "ACTIVE" or server.status == "BUILD":
print("The server",serverName,"is already running or currently building")
elif server.status == "SHUTOFF":
serverStart = conn.compute.start_server(server)
server = conn.compute.wait_for_server(server)
print(serverName,"is now running")
else:
print("Status of",serverName,"is",server.status,". No action taken")
elif serverFind is None:
print("The server",serverName,"does not exist")
else:
print("There seems to be an issue, check your servers")
# pass
def stop():
''' Stop a set of Openstack virtual machines
if they are running.
'''
print("Stopping...")
# Iterate through SERVERS. If server exists, and has status 'active' or 'build', stop it.
# If server is "WEBSERVER" optional choice to disassociate floating IP address
for server in SERVERS:
serverName = server
serverFind = conn.compute.find_server(serverName)
if serverFind is not None:
server = conn.compute.get_server(serverFind)
if server.status == "SHUTOFF":
print("The server",serverName,"is already stopped")
elif server.status == "ACTIVE" or server.status == "BUILD":
print("Stopping server",serverName)
serverStop = conn.compute.stop_server(server)
server = conn.compute.wait_for_server(server)
# Un-comment below section to disassociate a floating IP address on shutdown
# if serverName == WEBSERVER:
# try:
# webFloatIP = server["addresses"][NETWORK][1]["addr"]
# floatingIP = conn.network.find_ip(webFloatIP)
# except:
# print("No floating ip found")
# if floatingIP:
# print("Disassociating floating ip address:",floatingIP.floating_ip_address,"from webserver")
# conn.compute.remove_floating_ip_from_server(server,webFloatIP)
# else:
# print("No floating ip currently associated")
else:
print("Status of",serverName,"is",server.status,". No action taken")
elif serverFind is None:
print("The server",serverName,"does not exist")
else:
print("There seems to be an issue, check your servers")
# pass
def destroy():
''' Tear down the set of Openstack resources
produced by the create action
'''
# Iterate through SERVERS. If server exists, destroy it.
# If server is "WEBSERVER" diassociate floating IP address
for server in SERVERS:
serverName = server
serverFind = conn.compute.find_server(serverName)
if serverFind is not None:
server = conn.compute.get_server(serverFind)
if serverName == WEBSERVER:
try:
webFloatIP = server["addresses"][NETWORK][1]["addr"]
floatingIP = conn.network.find_ip(webFloatIP)
if floatingIP:
print("Dissassociating floating ip address:",floatingIP.floating_ip_address,"from webserver")
print("Deleting floating ip")
conn.network.delete_ip(floatingIP)
except:
print("No floating ip found")
print("Destroying server:",serverName)
conn.compute.delete_server(server)
server = conn.compute.wait_for_server(server)
else:
print("The server",serverName,"doesn't exist so cannot be destroyed")
# If router exists, remove the interface and destroy it
if routr:
conn.network.remove_interface_from_router(routr, subnt.id)
conn.network.delete_router(routr)
print("Router",routr.name,"has been deleted")
else:
print("Router does not exist")
# If network exists, delete it. This includes subnets within the network
if netwk:
conn.network.delete_network(netwk)
print("Network", netwk.name,"and subnet",subnt.name,"have been deleted")
else:
print("Network does not exist")
# pass
def status():
''' Print a status report on the OpenStack
virtual machines created by the create action.
'''
# Iterate through SERVERS. For each server found, provide it's name, status, and any associated IP addresses
for server in SERVERS:
serverName = server
serverFind = conn.compute.find_server(serverName)
if serverFind:
server = conn.compute.get_server(serverFind)
print("-------------------------------")
print("Server name:",serverName)
print("Server status:",server.status)
try:
webFixedIP = server["addresses"][NETWORK][0]["addr"]
print("Fixed ip address:",webFixedIP)
except:
print("Fixed IP address not found")
try:
webFloatIP = server["addresses"][NETWORK][1]["addr"]
print("Floating IP address:",webFloatIP)
except:
print("Floating IP address not found")
print("")
print("-------------------------------")
else:
print("Server",serverName,"not found")
# pass
### You should not modify anything below this line ###
#if __name__ == '__main__':
# parser = argparse.ArgumentParser()
# parser.add_argument('operation',
# help='One of "create", "run", "stop", "destroy", or "status"')
# args = parser.parse_args()
# operation = args.operation
# operations = {
# 'create' : create,
# 'run' : run,
# 'stop' : stop,
# 'destroy' : destroy,
# 'status' : status
# }
# action = operations.get(operation, lambda: print('{}: no such operation'.format(operation)))
# action()