-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge13
executable file
·213 lines (145 loc) · 5.08 KB
/
challenge13
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
#!/usr/bin/env python
import os
import time
import pyrax
def delete_all_load_balancers(clbobj):
print "Finding and deleting load balancers"
load_balancers = clbobj.list()
for load_balancer in load_balancers:
print "Deleting Load Balancer \"" + load_balancer.name + "\""
load_balancer.delete()
print "Waiting for all load balancers to be deleted"
while 1:
load_balancers = clbobj.list()
if load_balancers:
print "Load balancers still exist. Sleeping 20\n"
time.sleep(20)
else:
print "No more load balancers. Returning"
return 1
def delete_all_cloud_files(cfobj):
print "Deleting all cloud files objects"
containers = cfobj.get_all_containers()
for container in containers:
for object in container.get_objects():
print "\tDeleting %s: %s" % ( container.name, object.name )
container.delete_object(object.name)
print "\tDeleting %s" % container.name
cfobj.delete_container(container.name)
def delete_all_cloud_databases(cdbobj):
print "Deleting all cloud database instances"
instances = cdbobj.list()
for instance in instances:
print "\tDeleting instance %s\n" % (instance.name)
instance.delete()
def delete_all_cloud_block_storage(cbsobj):
print "Deleting all cloud block storage"
volumes = cbsobj.list()
# detach all volumes first
for volume in volumes:
print "\tDetaching volume %s\n" % (volume.name)
volume.detach()
# Loop through and wait for the storage to finish detaching
completed = 0
while 1:
# reset completed for the next round of checks
completed = 1
# Loop the volumes and check the status
for volume in volumes:
if volume.status == "available" or volume.status == "error":
print "\t Volume %s detached. Deleting now." % (volume.name)
volume.delete()
else:
print "\t Volume %s is still in status %s." % (volume.name, volume.status)
completed = 0
# return if things are finished, else sleep until ready
if completed == 1:
print "All volumes detached and removed. Returning"
return
else:
print "Waiting for volumes to finish detaching. Sleeping 60 seconds."
time.sleep(30)
def delete_all_cloud_networks(cnobj):
print "Deleting cloud networks"
networks = cnobj.list()
for network in networks:
if network.name != "public" and network.name != "private":
print "\tDeleting network %s\n" % (network.name)
network.delete()
def delete_all_cloud_server_images(csrvobj):
print "Deleting cloud server images"
images = csrvobj.images.list()
for image in images:
if image.metadata["image_type"] != "base":
print "Deleting image " + image.name
image.delete()
# wait for images to be deleted
while 1:
complete = 1
images = csrvobj.images.list()
for image in images:
if image.metadata["image_type"] != "base":
complete = 0
if complete != 1:
print "Images still exist. Sleeping 20 seconds"
time.sleep(20)
else:
return
def delete_all_cloud_servers(csrvobj):
print "Deleting cloud servers"
servers = csrvobj.servers.list()
for server in servers:
if server.name != "wiki.linuxrackers.com":
print "Deleting cloud server: " + server.name
server.delete()
# wait for servers to be deleted
while 1:
complete = 1
servers = csrvobj.servers.list()
for server in servers:
if server.name != "wiki.linuxrackers.com":
complete = 0
if complete != 1:
print "Servers still exist. Sleeping 20 seconds"
time.sleep(20)
else:
return
def main():
# Authenticate using a credentials file: "~/.rackspace_cloud_credentials"
cred_file = "%s/.rackspace_cloud_credentials" % (os.environ['HOME'])
print "Setting authentication file to %s" % (cred_file)
pyrax.set_credential_file(cred_file)
# Instantiate a cloudload balancer object
print "Instantiating cloud_loadbalancers object"
clbobj = pyrax.cloud_loadbalancers
# Delete all of the load balancers
delete_all_load_balancers(clbobj)
# Instantiate a cloudfiles object
print "Instantiating cloudfiles object"
cfobj = pyrax.cloudfiles
# Empty out cloud files
delete_all_cloud_files(cfobj)
# Instantiate a cloud database object
print "Instantiating cloud database object"
cdbobj = pyrax.cloud_databases
# Delete all databases
delete_all_cloud_databases(cdbobj)
# Instantiate a cloud block storage object
print "Instantiating cloud block storage object"
cbsobj = pyrax.cloud_blockstorage
# Delete all cloud block storage
delete_all_cloud_block_storage(cbsobj)
# Instantiate a cloud servers object
print "Instantiating cloud servers object"
csrvobj = pyrax.cloudservers
# Delete all server images
delete_all_cloud_server_images(csrvobj)
# Delete all servers
delete_all_cloud_servers(csrvobj)
# Instantiate a cloud network object
print "Instantiating cloud network object"
cnobj = pyrax.cloud_networks
# Delete all custom cloud networks(run after server deletions)
delete_all_cloud_networks(cnobj)
if __name__ == "__main__":
main()