-
Notifications
You must be signed in to change notification settings - Fork 0
/
servers.py
executable file
·494 lines (370 loc) · 14 KB
/
servers.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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#!/usr/bin/python
# Import external modules
import time
import snack
import pyrax
import keyring
#######################################################
# Snack form to get server info from a user
#######################################################
def createServerForm(helpline, title, myservers, myimages, myflavors, mykeypairs, mynetworks):
# Set up the main screen
newsrvwin = snack.SnackScreen()
# Set the help line
newsrvwin.pushHelpLine(helpline)
# Get user entries
nametb = snack.Textbox(13,1,"Server Name: ",scroll=0,wrap=0)
namee = snack.Entry(50, text="", hidden = 0, password = 0, scroll = 1, returnExit = 0)
# Allow Image Selection
imagetb = snack.Textbox(13,1,"Image: ",scroll=0,wrap=0)
imagelb = snack.Listbox(height = 3, width = 50, returnExit = 0)
for image in myimages:
imagelb.append(image.name,image.id)
# Allow Flavor Selection
flavortb = snack.Textbox(13,1,"Flavor: ",scroll=0,wrap=0)
flavorlb = snack.Listbox(height = 3, width = 50, returnExit = 0)
for flavor in myflavors:
flavorlb.append(flavor.name,flavor.id)
# Select any public keys attache to the server's root
keypairtb = snack.Textbox(13,1,"Keypair: ",scroll=0,wrap=0)
keypairlb = snack.Listbox(height = 3, width = 50, returnExit = 0)
for keypair in mykeypairs:
keypairlb.append(keypair.name, keypair.name)
# Select networks to attach to the server
networktb = snack.Textbox(13,1,"Networks: ",scroll=0,wrap=0)
networkcbt = snack.CheckboxTree(height = 3, scroll = 0)
for network in mynetworks:
if network.name == 'private' or network.name == 'public':
networkcbt.append(network.name, network, selected = 1)
else:
networkcbt.append(network.name, network, selected = 0)
bb = snack.ButtonBar(newsrvwin, (("Add", "add"), ("Cancel", "cancel")))
g = snack.GridForm(newsrvwin, title, 2, 10)
# name entry row
g.add(nametb, 0, 0)
g.add(namee, 1, 0)
# image entry row
g.add(imagetb, 0, 1, padding=(0,1,0,1))
g.add(imagelb, 1, 1, padding=(0,1,0,1))
# flavor entry row
g.add(flavortb, 0, 2, padding=(0,1,0,1))
g.add(flavorlb, 1, 2, padding=(0,1,0,1))
# keypair entry row
g.add(keypairtb, 0, 3, padding=(0,1,0,1))
g.add(keypairlb, 1, 3, padding=(0,1,0,1))
# network entry row
g.add(networktb, 0, 4, padding=(0,1,0,1))
g.add(networkcbt, 1, 4, padding=(0,1,0,1))
g.add(bb, 0, 5, growx = 1)
result = g.runOnce()
newsrvwin.finish()
if bb.buttonPressed(result) == "add":
# Create a dict to return
retval = {
'servername': namee.value(),
'imageid': imagelb.current(),
'flavorid': flavorlb.current(),
'keypairname': keypairlb.current(),
'networklist': networkcbt.getSelection(),
}
return retval
else:
return bb.buttonPressed(result)
##########################################################
# Select then delete a server
##########################################################
def deleteServer(helpline, title, servers, srvobj):
# Set up the main screen
mainlswin = snack.SnackScreen()
# Set the help line
mainlswin.pushHelpLine(helpline)
li = snack.Listbox(height = 10, width = 80, returnExit = 0)
for server in servers:
full = "<%s>: %s" % (server.id, server.name)
li.append(full,server.id)
bb = snack.ButtonBar(mainlswin, (("(o)k", "ok", 'o'), ("(q)uit", "quit", 'q')))
g = snack.GridForm(mainlswin, title, 1, 4)
g.add(li, 0, 0)
g.add(bb, 0, 3, growx = 1)
result = g.runOnce()
mainlswin.finish()
if bb.buttonPressed(result) == "ok":
# Ask if they really want to delete this one.
deleteid = li.current()
delobj = 0
for server in servers:
if server.id == deleteid:
delobj = server
mainlswin = snack.SnackScreen()
mainlswin.pushHelpLine(helpline)
question = snack.ButtonChoiceWindow(mainlswin,
"Are you Sure?",
"%s: %s" % (delobj.name, delobj.id),
buttons = ['Delete', 'No!!! Keep it'],
width = 60, x = None, y = None, help = None)
mainlswin.finish()
# if the delete button was pushed do the needful
if 'delete' in question:
# Make a quick display to show the user we are
# updating the server list
infowin = snack.SnackScreen()
infowin.pushHelpLine(helpline)
tb = snack.Textbox(40, 10, "", scroll = 0, wrap = 0)
g = snack.GridForm(infowin, "Waiting for server to delete", 1, 10)
g.add(tb, 0, 0)
g.draw()
# Put real delete after the draw to reduce flash
delobj.delete()
while True:
text = "\nRefreshing Server Objects..."
tb.setText(text)
infowin.refresh()
myservers = srvobj.list()
found = 0
for server in myservers:
if server.id == delobj.id:
found = 1
text += "\nServer still exists..."
tb.setText(text)
infowin.refresh()
if found == 1:
text += "\nSleeping 20 seconds..."
tb.setText(text)
infowin.refresh()
time.sleep(20)
else:
break
infowin.finish()
else:
return bb.buttonPressed(result)
##########################################################
# Display all of the servers and return the selected
##########################################################
def listServers(helpline, title, servers):
# Set up the main screen
mainlswin = snack.SnackScreen()
# Set the help line
mainlswin.pushHelpLine(helpline)
li = snack.Listbox(height = 10, width = 80, returnExit = 0)
for server in servers:
full = "<%s>: %s" % (server.id, server.name)
li.append(full,server.id)
bb = snack.ButtonBar(mainlswin, (("(o)k", "ok", 'o'), ("(q)uit", "quit", 'q')))
g = snack.GridForm(mainlswin, title, 1, 4)
g.add(li, 0, 0)
g.add(bb, 0, 3, growx = 1)
result = g.runOnce()
mainlswin.finish()
if bb.buttonPressed(result) == "ok":
#return li.current()
dispsrv = 0
for server in servers:
if server.id == li.current():
dispsrv = server
# Make a quick display to show the user we are
# updating the server list
infowin = snack.SnackScreen()
infowin.pushHelpLine(helpline)
tb1_1 = snack.Textbox(10, 1, "Name: ", scroll = 0, wrap = 0)
tb1_2 = snack.Textbox(50, 1, dispsrv.name, scroll = 0, wrap = 0)
tb2_1 = snack.Textbox(10, 1, "ID: ", scroll = 0, wrap = 0)
tb2_2 = snack.Textbox(50, 1, dispsrv.id, scroll = 0, wrap = 0)
tb3_1 = snack.Textbox(10, 1, "Status: ", scroll = 0, wrap = 0)
tb3_2 = snack.Textbox(50, 1, dispsrv.status, scroll = 0, wrap = 0)
tb4_1 = snack.Textbox(10, 1, "Network: ", scroll = 0, wrap = 0)
ntext = ""
for network in dispsrv.networks:
for range in dispsrv.networks[network]:
if ntext != "":
ntext += "\n%s: %s" % (network, range)
else:
ntext += "%s: %s" % (network, range)
tb4_2 = snack.Textbox(50, 4, ntext, scroll = 0, wrap = 1)
g = snack.GridForm(infowin, "Server Info", 2, 5)
bb = snack.Button("Continue")
g.add(tb1_1, 0, 0, padding=(0,1,0,1))
g.add(tb1_2, 1, 0, padding=(0,1,0,1))
g.add(tb2_1, 0, 1, padding=(0,1,0,1))
g.add(tb2_2, 1, 1, padding=(0,1,0,1))
g.add(tb3_1, 0, 2, padding=(0,1,0,1))
g.add(tb3_2, 1, 2, padding=(0,1,0,1))
g.add(tb4_1, 0, 3, padding=(0,1,0,1))
g.add(tb4_2, 1, 3, padding=(0,1,0,1))
g.add(bb, 0, 4)
g.runOnce()
infowin.finish()
else:
return bb.buttonPressed(result)
#########################################
# Add a New Server and Display Results
#########################################
def addServer(help_text, title, srvobj, svals):
# Start the informational gui
infowin = snack.SnackScreen()
# Set the help line
infowin.pushHelpLine(help_text)
# Create an object to throw the text in
tb = snack.Textbox(80, 20, "", scroll = 0, wrap = 1)
g = snack.GridForm(infowin, title, 1, 2)
bb = snack.Button("Continue")
g.add(tb, 0, 0)
g.add(bb, 0, 1)
g.draw()
text = "Adding Server..."
tb.setText(text)
infowin.refresh()
net_list = []
for network in svals['networklist']:
net_list.append({'net-id': network.id})
server = srvobj.servers.create(svals['servername'],
svals['imageid'],
svals['flavorid'],
key_name=svals['keypairname'],
nics=net_list
)
text += "\nServer ID: %s" % server.id
text += "\nAdmin Pass: %s" % server.adminPass
text += "\nServer Status: %s" % server.status
text += "\nNetworks: %s" % server.networks
tb.setText(text)
infowin.refresh()
text += "\nWaiting on build to complete..."
text += "\nGet some coffee. This could take a while..."
tb.setText(text)
infowin.refresh()
new_srv = pyrax.utils.wait_until(server, "status", ["ACTIVE", "ERROR"])
text += "\nBuild Complete:"
text += "\n Server ID: %s" % new_srv.id
text += "\n Admin Pass: %s" % server.adminPass
text += "\n Server Status: %s" % new_srv.status
text += "\n Networks: %s" % new_srv.networks
tb.setText(text)
infowin.refresh()
# Add a continue button
g.runOnce()
infowin.finish()
#########################################
# Display the primary server screen
#########################################
def mainServersScreen(helpline):
# Set up the main screen
mainsrvwin = snack.SnackScreen()
# Set the help line
mainsrvwin.pushHelpLine(helpline)
li = snack.Listbox(height = 10, width = 40, returnExit = 0)
li.append("l - Server List & Info", "server_list")
li.append("a - Add Server", "server_add")
li.append("d - Delete Server", "server_del")
bb = snack.ButtonBar(mainsrvwin, (("(o)k", "ok", 'o'), ("(q)uit", "quit", 'q')))
g = snack.GridForm(mainsrvwin, "Cloud Servers", 1, 4)
g.add(li, 0, 0)
g.add(bb, 0, 3, growx = 1)
result = g.runOnce()
mainsrvwin.finish()
if bb.buttonPressed(result) == "ok":
return li.current()
else:
return bb.buttonPressed(result)
#######################################
# Main loop for the servers menus
#######################################
def mainServersScreenLoop(help_text, selected_creds, curregion):
###################################################
# Throw up a information window while loading data
###################################################
# Start the informational gui
infowin = snack.SnackScreen()
# Set the help line
infowin.pushHelpLine(help_text)
# Create an object to throw the text in
tb = snack.Textbox(40, 10, "", scroll = 0, wrap = 0)
g = snack.GridForm(infowin, "Loading Server Data", 1, 10)
g.add(tb, 0, 0)
g.draw()
# Set up pyrax to prepare for server actions
text = "Setting up Cloud Session..."
tb.setText(text)
infowin.refresh()
pyrax.set_setting("identity_type", "rackspace")
pyrax.set_setting("region", "ORD")
cur_session = pyrax.create_context(
tenant_id = selected_creds[1],
username = selected_creds[2],
api_key = selected_creds[3],
verify_ssl=None)
cur_session.authenticate()
srvobj = cur_session.get_client("compute", curregion)
netobj = cur_session.get_client("network", curregion)
# Start pulling some values to assist
text += "\nPulling Server Objects..."
tb.setText(text)
infowin.refresh()
myservers = srvobj.list()
text += "\nPulling Image Objects..."
tb.setText(text)
infowin.refresh()
myimages = srvobj.images.list()
text += "\nPulling Flavor Objects..."
tb.setText(text)
infowin.refresh()
myflavors = srvobj.flavors.list()
text += "\nPulling Keypairs..."
tb.setText(text)
infowin.refresh()
mykeypairs = srvobj.keypairs.list()
text += "\nPulling Networks..."
tb.setText(text)
infowin.refresh()
mynetworks = netobj.list()
infowin.finish()
###################################
# Run the main server screen loop
###################################
while True:
# Print the main servers screen
################################
mcsrun = mainServersScreen(help_text)
# Simple list the servers for the active datacenter
if mcsrun == "server_list":
lsret = 'keepgoing'
# Keep looping so the user doesn't get kicked out on a info exit
while lsret != "quit":
lsret = listServers(help_text, "Server List for %s" % curregion, myservers)
# Delete a selected server. This will take two actions.
if mcsrun == "server_del":
retval = deleteServer(help_text, "Delete a server in %s" % curregion, myservers, srvobj)
if retval != "quit":
# Make a quick display to show the user we are
# updating the server list
infowin = snack.SnackScreen()
infowin.pushHelpLine(help_text)
tb = snack.Textbox(40, 10, "", scroll = 0, wrap = 0)
g = snack.GridForm(infowin, "Loading Server Data", 1, 10)
g.add(tb, 0, 0)
g.draw()
text = "\nRefreshing Server Objects..."
tb.setText(text)
infowin.refresh()
myservers = srvobj.list()
infowin.finish()
# Add a new server to the active datacenter
if mcsrun == "server_add":
formvals = createServerForm(help_text, "New Cloud Server for %s" % curregion, myservers, myimages, myflavors, mykeypairs, mynetworks)
if formvals != 'cancel':
addServer(help_text, "Adding New Cloud Server %s" % formvals['servername'], srvobj, formvals)
# Make a quick display to show the user we are
# updating the server list
infowin = snack.SnackScreen()
infowin.pushHelpLine(help_text)
tb = snack.Textbox(40, 10, "", scroll = 0, wrap = 0)
g = snack.GridForm(infowin, "Loading Server Data", 1, 10)
g.add(tb, 0, 0)
g.draw()
text = "\nRefreshing Server Objects..."
tb.setText(text)
infowin.refresh()
myservers = srvobj.list()
infowin.finish()
# Return to the main menu with the results of server actions
if mcsrun == 'quit':
return