root / image_creator / dialog_menu.py @ 0969420b
History | View | Annotate | Download (30.3 kB)
1 |
# -*- coding: utf-8 -*-
|
---|---|
2 |
#
|
3 |
# Copyright 2012 GRNET S.A. All rights reserved.
|
4 |
#
|
5 |
# Redistribution and use in source and binary forms, with or
|
6 |
# without modification, are permitted provided that the following
|
7 |
# conditions are met:
|
8 |
#
|
9 |
# 1. Redistributions of source code must retain the above
|
10 |
# copyright notice, this list of conditions and the following
|
11 |
# disclaimer.
|
12 |
#
|
13 |
# 2. Redistributions in binary form must reproduce the above
|
14 |
# copyright notice, this list of conditions and the following
|
15 |
# disclaimer in the documentation and/or other materials
|
16 |
# provided with the distribution.
|
17 |
#
|
18 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
19 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
20 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
21 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
22 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
23 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
24 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
25 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
26 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
27 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
28 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
29 |
# POSSIBILITY OF SUCH DAMAGE.
|
30 |
#
|
31 |
# The views and conclusions contained in the software and
|
32 |
# documentation are those of the authors and should not be
|
33 |
# interpreted as representing official policies, either expressed
|
34 |
# or implied, of GRNET S.A.
|
35 |
|
36 |
"""This module implements the "expert" mode of the dialog-based version of
|
37 |
snf-image-creator.
|
38 |
"""
|
39 |
|
40 |
import os |
41 |
import textwrap |
42 |
import StringIO |
43 |
import json |
44 |
|
45 |
from image_creator import __version__ as version |
46 |
from image_creator.util import MD5, FatalError |
47 |
from image_creator.output.dialog import GaugeOutput, InfoBoxOutput |
48 |
from image_creator.kamaki_wrapper import Kamaki, ClientError |
49 |
from image_creator.help import get_help_file |
50 |
from image_creator.dialog_util import SMALL_WIDTH, WIDTH, \ |
51 |
update_background_title, confirm_reset, confirm_exit, Reset, \ |
52 |
extract_image, extract_metadata_string, add_cloud, edit_cloud |
53 |
|
54 |
CONFIGURATION_TASKS = [ |
55 |
("Partition table manipulation", ["FixPartitionTable"], |
56 |
["linux", "windows"]), |
57 |
("File system resize",
|
58 |
["FilesystemResizeUnmounted", "FilesystemResizeMounted"], |
59 |
["linux", "windows"]), |
60 |
("Swap partition configuration", ["AddSwap"], ["linux"]), |
61 |
("SSH keys removal", ["DeleteSSHKeys"], ["linux"]), |
62 |
("Temporal RDP disabling", ["DisableRemoteDesktopConnections"], |
63 |
["windows"]),
|
64 |
("SELinux relabeling at next boot", ["SELinuxAutorelabel"], ["linux"]), |
65 |
("Hostname/Computer Name assignment", ["AssignHostname"], |
66 |
["windows", "linux"]), |
67 |
("Password change", ["ChangePassword"], ["windows", "linux"]), |
68 |
("File injection", ["EnforcePersonality"], ["windows", "linux"]) |
69 |
] |
70 |
|
71 |
SYSPREP_PARAM_MAXLEN = 20
|
72 |
|
73 |
class MetadataMonitor(object): |
74 |
"""Monitors image metadata chages"""
|
75 |
def __init__(self, session, meta): |
76 |
self.session = session
|
77 |
self.meta = meta
|
78 |
|
79 |
def __enter__(self): |
80 |
self.old = {}
|
81 |
for (k, v) in self.meta.items(): |
82 |
self.old[k] = v
|
83 |
|
84 |
def __exit__(self, type, value, traceback): |
85 |
d = self.session['dialog'] |
86 |
|
87 |
altered = {} |
88 |
added = {} |
89 |
|
90 |
for (k, v) in self.meta.items(): |
91 |
if k not in self.old: |
92 |
added[k] = v |
93 |
elif self.old[k] != v: |
94 |
altered[k] = v |
95 |
|
96 |
if not (len(added) or len(altered)): |
97 |
return
|
98 |
|
99 |
msg = "The last action has changed some image properties:\n\n"
|
100 |
if len(added): |
101 |
msg += "New image properties:\n"
|
102 |
for (k, v) in added.items(): |
103 |
msg += ' %s: "%s"\n' % (k, v)
|
104 |
msg += "\n"
|
105 |
if len(altered): |
106 |
msg += "Updated image properties:\n"
|
107 |
for (k, v) in altered.items(): |
108 |
msg += ' %s: "%s" -> "%s"\n' % (k, self.old[k], v) |
109 |
msg += "\n"
|
110 |
|
111 |
self.session['metadata'].update(added) |
112 |
self.session['metadata'].update(altered) |
113 |
d.msgbox(msg, title="Image Property Changes", width=SMALL_WIDTH)
|
114 |
|
115 |
|
116 |
def upload_image(session): |
117 |
"""Upload the image to the storage service"""
|
118 |
d = session["dialog"]
|
119 |
image = session['image']
|
120 |
meta = session['metadata']
|
121 |
size = image.size |
122 |
|
123 |
if "account" not in session: |
124 |
d.msgbox("You need to select a valid cloud before you can upload "
|
125 |
"images to it", width=SMALL_WIDTH)
|
126 |
return False |
127 |
|
128 |
while 1: |
129 |
if 'upload' in session: |
130 |
init = session['upload']
|
131 |
elif 'OS' in meta: |
132 |
init = "%s.diskdump" % meta['OS'] |
133 |
else:
|
134 |
init = ""
|
135 |
(code, answer) = d.inputbox("Please provide a filename:", init=init,
|
136 |
width=WIDTH) |
137 |
|
138 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
139 |
return False |
140 |
|
141 |
filename = answer.strip() |
142 |
if len(filename) == 0: |
143 |
d.msgbox("Filename cannot be empty", width=SMALL_WIDTH)
|
144 |
continue
|
145 |
|
146 |
kamaki = Kamaki(session['account'], None) |
147 |
overwrite = [] |
148 |
for f in (filename, "%s.md5sum" % filename, "%s.meta" % filename): |
149 |
if kamaki.object_exists(f):
|
150 |
overwrite.append(f) |
151 |
|
152 |
if len(overwrite) > 0: |
153 |
if d.yesno("The following storage service object(s) already " |
154 |
"exist(s):\n%s\nDo you want to overwrite them?" %
|
155 |
"\n".join(overwrite), width=WIDTH, defaultno=1): |
156 |
continue
|
157 |
|
158 |
session['upload'] = filename
|
159 |
break
|
160 |
|
161 |
gauge = GaugeOutput(d, "Image Upload", "Uploading...") |
162 |
try:
|
163 |
out = image.out |
164 |
out.add(gauge) |
165 |
kamaki.out = out |
166 |
try:
|
167 |
if 'checksum' not in session: |
168 |
md5 = MD5(out) |
169 |
session['checksum'] = md5.compute(image.device, size)
|
170 |
|
171 |
try:
|
172 |
# Upload image file
|
173 |
with open(image.device, 'rb') as f: |
174 |
session["pithos_uri"] = \
|
175 |
kamaki.upload(f, size, filename, |
176 |
"Calculating block hashes",
|
177 |
"Uploading missing blocks")
|
178 |
# Upload md5sum file
|
179 |
out.output("Uploading md5sum file...")
|
180 |
md5str = "%s %s\n" % (session['checksum'], filename) |
181 |
kamaki.upload(StringIO.StringIO(md5str), size=len(md5str),
|
182 |
remote_path="%s.md5sum" % filename)
|
183 |
out.success("done")
|
184 |
|
185 |
except ClientError as e: |
186 |
d.msgbox( |
187 |
"Error in storage service client: %s" % e.message,
|
188 |
title="Storage Service Client Error", width=SMALL_WIDTH)
|
189 |
if 'pithos_uri' in session: |
190 |
del session['pithos_uri'] |
191 |
return False |
192 |
finally:
|
193 |
out.remove(gauge) |
194 |
finally:
|
195 |
gauge.cleanup() |
196 |
|
197 |
d.msgbox("Image file `%s' was successfully uploaded" % filename,
|
198 |
width=SMALL_WIDTH) |
199 |
|
200 |
return True |
201 |
|
202 |
|
203 |
def register_image(session): |
204 |
"""Register image with the compute service"""
|
205 |
d = session["dialog"]
|
206 |
|
207 |
is_public = False
|
208 |
|
209 |
if "account" not in session: |
210 |
d.msgbox("You need to select a valid cloud before you "
|
211 |
"can register an images with it", width=SMALL_WIDTH)
|
212 |
return False |
213 |
|
214 |
if "pithos_uri" not in session: |
215 |
d.msgbox("You need to upload the image to the cloud before you can "
|
216 |
"register it", width=SMALL_WIDTH)
|
217 |
return False |
218 |
|
219 |
while 1: |
220 |
(code, answer) = d.inputbox("Please provide a registration name:",
|
221 |
width=WIDTH) |
222 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
223 |
return False |
224 |
|
225 |
name = answer.strip() |
226 |
if len(name) == 0: |
227 |
d.msgbox("Registration name cannot be empty", width=SMALL_WIDTH)
|
228 |
continue
|
229 |
|
230 |
ret = d.yesno("Make the image public?\\nA public image is accessible "
|
231 |
"by every user of the service.", defaultno=1, |
232 |
width=WIDTH) |
233 |
if ret not in (0, 1): |
234 |
continue
|
235 |
|
236 |
is_public = True if ret == 0 else False |
237 |
|
238 |
break
|
239 |
|
240 |
metadata = {} |
241 |
metadata.update(session['metadata'])
|
242 |
if 'task_metadata' in session: |
243 |
for key in session['task_metadata']: |
244 |
metadata[key] = 'yes'
|
245 |
|
246 |
img_type = "public" if is_public else "private" |
247 |
gauge = GaugeOutput(d, "Image Registration", "Registering image...") |
248 |
try:
|
249 |
out = session['image'].out
|
250 |
out.add(gauge) |
251 |
try:
|
252 |
try:
|
253 |
out.output("Registering %s image with the cloud..." % img_type)
|
254 |
kamaki = Kamaki(session['account'], out)
|
255 |
result = kamaki.register(name, session['pithos_uri'], metadata,
|
256 |
is_public) |
257 |
out.success('done')
|
258 |
# Upload metadata file
|
259 |
out.output("Uploading metadata file...")
|
260 |
metastring = unicode(json.dumps(result, ensure_ascii=False)) |
261 |
kamaki.upload(StringIO.StringIO(metastring), |
262 |
size=len(metastring),
|
263 |
remote_path="%s.meta" % session['upload']) |
264 |
out.success("done")
|
265 |
if is_public:
|
266 |
out.output("Sharing metadata and md5sum files...")
|
267 |
kamaki.share("%s.meta" % session['upload']) |
268 |
kamaki.share("%s.md5sum" % session['upload']) |
269 |
out.success('done')
|
270 |
except ClientError as e: |
271 |
d.msgbox("Error in storage service client: %s" % e.message)
|
272 |
return False |
273 |
finally:
|
274 |
out.remove(gauge) |
275 |
finally:
|
276 |
gauge.cleanup() |
277 |
|
278 |
d.msgbox("%s image `%s' was successfully registered with the cloud as `%s'"
|
279 |
% (img_type.title(), session['upload'], name), width=SMALL_WIDTH)
|
280 |
return True |
281 |
|
282 |
|
283 |
def modify_clouds(session): |
284 |
"""Modify existing cloud accounts"""
|
285 |
d = session['dialog']
|
286 |
|
287 |
while 1: |
288 |
clouds = Kamaki.get_clouds() |
289 |
if not len(clouds): |
290 |
if not add_cloud(session): |
291 |
break
|
292 |
continue
|
293 |
|
294 |
choices = [] |
295 |
for (name, cloud) in clouds.items(): |
296 |
descr = cloud['description'] if 'description' in cloud else '' |
297 |
choices.append((name, descr)) |
298 |
|
299 |
(code, choice) = d.menu( |
300 |
"In this menu you can edit existing cloud accounts or add new "
|
301 |
" ones. Press <Edit> to edit an existing account or <Add> to add "
|
302 |
" a new one. Press <Back> or hit <ESC> when done.", height=18, |
303 |
width=WIDTH, choices=choices, menu_height=10, ok_label="Edit", |
304 |
extra_button=1, extra_label="Add", cancel="Back", help_button=1, |
305 |
title="Clouds")
|
306 |
|
307 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
308 |
return True |
309 |
elif code == d.DIALOG_OK: # Edit button |
310 |
edit_cloud(session, choice) |
311 |
elif code == d.DIALOG_EXTRA: # Add button |
312 |
add_cloud(session) |
313 |
|
314 |
|
315 |
def delete_clouds(session): |
316 |
"""Delete existing cloud accounts"""
|
317 |
d = session['dialog']
|
318 |
|
319 |
choices = [] |
320 |
for (name, cloud) in Kamaki.get_clouds().items(): |
321 |
descr = cloud['description'] if 'description' in cloud else '' |
322 |
choices.append((name, descr, 0))
|
323 |
|
324 |
if len(choices) == 0: |
325 |
d.msgbox("No available clouds to delete!", width=SMALL_WIDTH)
|
326 |
return True |
327 |
|
328 |
(code, to_delete) = d.checklist("Choose which cloud accounts to delete:",
|
329 |
choices=choices, width=WIDTH) |
330 |
|
331 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
332 |
return False |
333 |
|
334 |
if not len(to_delete): |
335 |
d.msgbox("Nothing selected!", width=SMALL_WIDTH)
|
336 |
return False |
337 |
|
338 |
if not d.yesno("Are you sure you want to remove the selected cloud " |
339 |
"accounts?", width=WIDTH, defaultno=1): |
340 |
for i in to_delete: |
341 |
Kamaki.remove_cloud(i) |
342 |
if 'cloud' in session and session['cloud'] == i: |
343 |
del session['cloud'] |
344 |
if 'account' in session: |
345 |
del session['account'] |
346 |
else:
|
347 |
return False |
348 |
|
349 |
d.msgbox("%d cloud accounts were deleted." % len(to_delete), |
350 |
width=SMALL_WIDTH) |
351 |
return True |
352 |
|
353 |
|
354 |
def kamaki_menu(session): |
355 |
"""Show kamaki related actions"""
|
356 |
d = session['dialog']
|
357 |
default_item = "Cloud"
|
358 |
|
359 |
if 'cloud' not in session: |
360 |
cloud = Kamaki.get_default_cloud_name() |
361 |
if cloud:
|
362 |
session['cloud'] = cloud
|
363 |
session['account'] = Kamaki.get_account(cloud)
|
364 |
if not session['account']: |
365 |
del session['account'] |
366 |
else:
|
367 |
default_item = "Add/Edit"
|
368 |
|
369 |
while 1: |
370 |
cloud = session["cloud"] if "cloud" in session else "<none>" |
371 |
if 'account' not in session and 'cloud' in session: |
372 |
cloud += " <invalid>"
|
373 |
|
374 |
upload = session["upload"] if "upload" in session else "<none>" |
375 |
|
376 |
choices = [("Add/Edit", "Add/Edit cloud accounts"), |
377 |
("Delete", "Delete existing cloud accounts"), |
378 |
("Cloud", "Select cloud account to use: %s" % cloud), |
379 |
("Upload", "Upload image to the cloud"), |
380 |
("Register", "Register image with the cloud: %s" % upload)] |
381 |
|
382 |
(code, choice) = d.menu( |
383 |
text="Choose one of the following or press <Back> to go back.",
|
384 |
width=WIDTH, choices=choices, cancel="Back", height=13, |
385 |
menu_height=5, default_item=default_item,
|
386 |
title="Image Registration Menu")
|
387 |
|
388 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
389 |
return False |
390 |
|
391 |
if choice == "Add/Edit": |
392 |
if modify_clouds(session):
|
393 |
default_item = "Cloud"
|
394 |
elif choice == "Delete": |
395 |
if delete_clouds(session):
|
396 |
if len(Kamaki.get_clouds()): |
397 |
default_item = "Cloud"
|
398 |
else:
|
399 |
default_item = "Add/Edit"
|
400 |
else:
|
401 |
default_item = "Delete"
|
402 |
elif choice == "Cloud": |
403 |
default_item = "Cloud"
|
404 |
clouds = Kamaki.get_clouds() |
405 |
if not len(clouds): |
406 |
d.msgbox("No clouds available. Please add a new cloud!",
|
407 |
width=SMALL_WIDTH) |
408 |
default_item = "Add/Edit"
|
409 |
continue
|
410 |
|
411 |
if 'cloud' not in session: |
412 |
session['cloud'] = clouds.keys()[0] |
413 |
|
414 |
choices = [] |
415 |
for name, info in clouds.items(): |
416 |
default = 1 if session['cloud'] == name else 0 |
417 |
descr = info['description'] if 'description' in info else "" |
418 |
choices.append((name, descr, default)) |
419 |
|
420 |
(code, answer) = d.radiolist("Please select a cloud:",
|
421 |
width=WIDTH, choices=choices) |
422 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
423 |
continue
|
424 |
else:
|
425 |
session['account'] = Kamaki.get_account(answer)
|
426 |
|
427 |
if session['account'] is None: # invalid account |
428 |
if not d.yesno("The cloud %s' is not valid! Would you " |
429 |
"like to edit it?" % answer, width=WIDTH):
|
430 |
if edit_cloud(session, answer):
|
431 |
session['account'] = Kamaki.get_account(answer)
|
432 |
Kamaki.set_default_cloud(answer) |
433 |
|
434 |
if session['account'] is not None: |
435 |
session['cloud'] = answer
|
436 |
Kamaki.set_default_cloud(answer) |
437 |
default_item = "Upload"
|
438 |
else:
|
439 |
del session['account'] |
440 |
del session['cloud'] |
441 |
elif choice == "Upload": |
442 |
if upload_image(session):
|
443 |
default_item = "Register"
|
444 |
else:
|
445 |
default_item = "Upload"
|
446 |
elif choice == "Register": |
447 |
if register_image(session):
|
448 |
return True |
449 |
else:
|
450 |
default_item = "Register"
|
451 |
|
452 |
|
453 |
def add_property(session): |
454 |
"""Add a new property to the image"""
|
455 |
d = session['dialog']
|
456 |
|
457 |
while 1: |
458 |
(code, answer) = d.inputbox("Please provide a name for a new image"
|
459 |
" property:", width=WIDTH)
|
460 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
461 |
return False |
462 |
|
463 |
name = answer.strip() |
464 |
if len(name) == 0: |
465 |
d.msgbox("A property name cannot be empty", width=SMALL_WIDTH)
|
466 |
continue
|
467 |
|
468 |
break
|
469 |
|
470 |
while 1: |
471 |
(code, answer) = d.inputbox("Please provide a value for image "
|
472 |
"property %s" % name, width=WIDTH)
|
473 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
474 |
return False |
475 |
|
476 |
value = answer.strip() |
477 |
if len(value) == 0: |
478 |
d.msgbox("Value cannot be empty", width=SMALL_WIDTH)
|
479 |
continue
|
480 |
|
481 |
break
|
482 |
|
483 |
session['metadata'][name] = value
|
484 |
|
485 |
return True |
486 |
|
487 |
|
488 |
def modify_properties(session): |
489 |
"""Modify an existing image property"""
|
490 |
d = session['dialog']
|
491 |
|
492 |
while 1: |
493 |
choices = [] |
494 |
for (key, val) in session['metadata'].items(): |
495 |
choices.append((str(key), str(val))) |
496 |
|
497 |
(code, choice) = d.menu( |
498 |
"In this menu you can edit existing image properties or add new "
|
499 |
"ones. Be careful! Most properties have special meaning and "
|
500 |
"alter the image deployment behaviour. Press <HELP> to see more "
|
501 |
"information about image properties. Press <BACK> when done.",
|
502 |
height=18, width=WIDTH, choices=choices, menu_height=10, |
503 |
ok_label="Edit", extra_button=1, extra_label="Add", cancel="Back", |
504 |
help_button=1, title="Image Properties") |
505 |
|
506 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
507 |
return True |
508 |
# Edit button
|
509 |
elif code == d.DIALOG_OK:
|
510 |
(code, answer) = d.inputbox("Please provide a new value for the "
|
511 |
"image property with name `%s':" %
|
512 |
choice, |
513 |
init=session['metadata'][choice],
|
514 |
width=WIDTH) |
515 |
if code not in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
516 |
value = answer.strip() |
517 |
if len(value) == 0: |
518 |
d.msgbox("Value cannot be empty!")
|
519 |
continue
|
520 |
else:
|
521 |
session['metadata'][choice] = value
|
522 |
# ADD button
|
523 |
elif code == d.DIALOG_EXTRA:
|
524 |
add_property(session) |
525 |
elif code == 'help': |
526 |
help_file = get_help_file("image_properties")
|
527 |
assert os.path.exists(help_file)
|
528 |
d.textbox(help_file, title="Image Properties", width=70, height=40) |
529 |
|
530 |
|
531 |
def delete_properties(session): |
532 |
"""Delete an image property"""
|
533 |
d = session['dialog']
|
534 |
|
535 |
choices = [] |
536 |
for (key, val) in session['metadata'].items(): |
537 |
choices.append((key, "%s" % val, 0)) |
538 |
|
539 |
(code, to_delete) = d.checklist("Choose which properties to delete:",
|
540 |
choices=choices, width=WIDTH) |
541 |
|
542 |
# If the user exits with ESC or CANCEL, the returned tag list is empty.
|
543 |
for i in to_delete: |
544 |
del session['metadata'][i] |
545 |
|
546 |
cnt = len(to_delete)
|
547 |
if cnt > 0: |
548 |
d.msgbox("%d image properties were deleted." % cnt, width=SMALL_WIDTH)
|
549 |
return True |
550 |
else:
|
551 |
return False |
552 |
|
553 |
|
554 |
def exclude_tasks(session): |
555 |
"""Exclude specific tasks from running during image deployment"""
|
556 |
d = session['dialog']
|
557 |
|
558 |
index = 0
|
559 |
displayed_index = 1
|
560 |
choices = [] |
561 |
mapping = {} |
562 |
if 'excluded_tasks' not in session: |
563 |
session['excluded_tasks'] = []
|
564 |
|
565 |
if -1 in session['excluded_tasks']: |
566 |
if not d.yesno("Image deployment configuration is disabled. " |
567 |
"Do you wish to enable it?", width=SMALL_WIDTH):
|
568 |
session['excluded_tasks'].remove(-1) |
569 |
else:
|
570 |
return False |
571 |
|
572 |
for (msg, task, osfamily) in CONFIGURATION_TASKS: |
573 |
if session['metadata']['OSFAMILY'] in osfamily: |
574 |
checked = 1 if index in session['excluded_tasks'] else 0 |
575 |
choices.append((str(displayed_index), msg, checked))
|
576 |
mapping[displayed_index] = index |
577 |
displayed_index += 1
|
578 |
index += 1
|
579 |
|
580 |
while 1: |
581 |
(code, tags) = d.checklist( |
582 |
text="Please choose which configuration tasks you would like to "
|
583 |
"prevent from running during image deployment. "
|
584 |
"Press <No Config> to supress any configuration. "
|
585 |
"Press <Help> for more help on the image deployment "
|
586 |
"configuration tasks.",
|
587 |
choices=choices, height=19, list_height=8, width=WIDTH, |
588 |
help_button=1, extra_button=1, extra_label="No Config", |
589 |
title="Exclude Configuration Tasks")
|
590 |
|
591 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
592 |
return False |
593 |
elif code == d.DIALOG_HELP:
|
594 |
help_file = get_help_file("configuration_tasks")
|
595 |
assert os.path.exists(help_file)
|
596 |
d.textbox(help_file, title="Configuration Tasks",
|
597 |
width=70, height=40) |
598 |
# No Config button
|
599 |
elif code == d.DIALOG_EXTRA:
|
600 |
session['excluded_tasks'] = [-1] |
601 |
session['task_metadata'] = ["EXCLUDE_ALL_TASKS"] |
602 |
break
|
603 |
elif code == d.DIALOG_OK:
|
604 |
session['excluded_tasks'] = []
|
605 |
for tag in tags: |
606 |
session['excluded_tasks'].append(mapping[int(tag)]) |
607 |
|
608 |
exclude_metadata = [] |
609 |
for task in session['excluded_tasks']: |
610 |
exclude_metadata.extend(CONFIGURATION_TASKS[task][1])
|
611 |
|
612 |
session['task_metadata'] = map(lambda x: "EXCLUDE_TASK_%s" % x, |
613 |
exclude_metadata) |
614 |
break
|
615 |
|
616 |
return True |
617 |
|
618 |
|
619 |
def sysprep_params(session): |
620 |
"""Collect the needed sysprep parameters"""
|
621 |
d = session['dialog']
|
622 |
image = session['image']
|
623 |
|
624 |
available = image.os.sysprep_params |
625 |
needed = image.os.needed_sysprep_params |
626 |
names = needed.keys() |
627 |
|
628 |
if len(needed) == 0: |
629 |
return True |
630 |
|
631 |
fields = [] |
632 |
for name in names: |
633 |
param = needed[name] |
634 |
default = available[name] if name in available else "" |
635 |
fields.append(("%s: " % param.description, default,
|
636 |
SYSPREP_PARAM_MAXLEN)) |
637 |
|
638 |
txt = "Please provide the following system preparation parameters:"
|
639 |
code, output = d.form(txt, height=13, width=WIDTH, form_height=len(fields), |
640 |
fields=fields) |
641 |
|
642 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
643 |
return False |
644 |
|
645 |
for i in range(len(fields)): |
646 |
param = needed[names[i]] |
647 |
if param.validator(output[i]):
|
648 |
image.os.sysprep_params[names[i]] = output[i] |
649 |
else:
|
650 |
d.msgbox("The value you provided for parameter: %s is not valid" %
|
651 |
names[i], width=SMALL_WIDTH) |
652 |
return False |
653 |
|
654 |
return True |
655 |
|
656 |
|
657 |
def sysprep(session): |
658 |
"""Perform various system preperation tasks on the image"""
|
659 |
d = session['dialog']
|
660 |
image = session['image']
|
661 |
|
662 |
# Is the image already shrinked?
|
663 |
if 'shrinked' in session and session['shrinked']: |
664 |
msg = "It seems you have shrinked the image. Running system " \
|
665 |
"preparation tasks on a shrinked image is dangerous."
|
666 |
|
667 |
if d.yesno("%s\n\nDo you really want to continue?" % msg, |
668 |
width=SMALL_WIDTH, defaultno=1):
|
669 |
return
|
670 |
|
671 |
wrapper = textwrap.TextWrapper(width=WIDTH - 5)
|
672 |
|
673 |
help_title = "System Preperation Tasks"
|
674 |
sysprep_help = "%s\n%s\n\n" % (help_title, '=' * len(help_title)) |
675 |
|
676 |
syspreps = image.os.list_syspreps() |
677 |
|
678 |
if len(syspreps) == 0: |
679 |
d.msgbox("No system preparation task available to run!",
|
680 |
title="System Preperation", width=SMALL_WIDTH)
|
681 |
return
|
682 |
|
683 |
while 1: |
684 |
choices = [] |
685 |
index = 0
|
686 |
for sysprep in syspreps: |
687 |
name, descr = image.os.sysprep_info(sysprep) |
688 |
display_name = name.replace('-', ' ').capitalize() |
689 |
sysprep_help += "%s\n" % display_name
|
690 |
sysprep_help += "%s\n" % ('-' * len(display_name)) |
691 |
sysprep_help += "%s\n\n" % wrapper.fill(" ".join(descr.split())) |
692 |
enabled = 1 if sysprep.enabled else 0 |
693 |
choices.append((str(index + 1), display_name, enabled)) |
694 |
index += 1
|
695 |
|
696 |
(code, tags) = d.checklist( |
697 |
"Please choose which system preparation tasks you would like to "
|
698 |
"run on the image. Press <Help> to see details about the system "
|
699 |
"preparation tasks.", title="Run system preparation tasks", |
700 |
choices=choices, width=70, ok_label="Run", help_button=1) |
701 |
|
702 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
703 |
return False |
704 |
elif code == d.DIALOG_HELP:
|
705 |
d.scrollbox(sysprep_help, width=WIDTH) |
706 |
elif code == d.DIALOG_OK:
|
707 |
# Enable selected syspreps and disable the rest
|
708 |
for i in range(len(syspreps)): |
709 |
if str(i + 1) in tags: |
710 |
image.os.enable_sysprep(syspreps[i]) |
711 |
else:
|
712 |
image.os.disable_sysprep(syspreps[i]) |
713 |
|
714 |
if len([s for s in image.os.list_syspreps() if s.enabled]) == 0: |
715 |
d.msgbox("No system preperation task is selected!",
|
716 |
title="System Preperation", width=SMALL_WIDTH)
|
717 |
continue
|
718 |
|
719 |
if not sysprep_params(session): |
720 |
continue
|
721 |
|
722 |
infobox = InfoBoxOutput(d, "Image Configuration")
|
723 |
try:
|
724 |
image.out.add(infobox) |
725 |
try:
|
726 |
# The checksum is invalid. We have mounted the image rw
|
727 |
if 'checksum' in session: |
728 |
del session['checksum'] |
729 |
|
730 |
# Monitor the metadata changes during syspreps
|
731 |
with MetadataMonitor(session, image.os.meta):
|
732 |
try:
|
733 |
image.os.do_sysprep() |
734 |
infobox.finalize() |
735 |
except FatalError as e: |
736 |
title = "System Preparation"
|
737 |
d.msgbox("System Preparation failed: %s" % e,
|
738 |
title=title, width=SMALL_WIDTH) |
739 |
finally:
|
740 |
image.out.remove(infobox) |
741 |
finally:
|
742 |
infobox.cleanup() |
743 |
break
|
744 |
return True |
745 |
|
746 |
|
747 |
def shrink(session): |
748 |
"""Shrink the image"""
|
749 |
d = session['dialog']
|
750 |
image = session['image']
|
751 |
|
752 |
shrinked = 'shrinked' in session and session['shrinked'] |
753 |
|
754 |
if shrinked:
|
755 |
d.msgbox("The image is already shrinked!", title="Image Shrinking", |
756 |
width=SMALL_WIDTH) |
757 |
return True |
758 |
|
759 |
msg = "This operation will shrink the last partition of the image to " \
|
760 |
"reduce the total image size. If the last partition is a swap " \
|
761 |
"partition, then this partition is removed and the partition " \
|
762 |
"before that is shrinked. The removed swap partition will be " \
|
763 |
"recreated during image deployment."
|
764 |
|
765 |
if not d.yesno("%s\n\nDo you want to continue?" % msg, width=WIDTH, |
766 |
height=12, title="Image Shrinking"): |
767 |
with MetadataMonitor(session, image.meta):
|
768 |
infobox = InfoBoxOutput(d, "Image Shrinking", height=4) |
769 |
image.out.add(infobox) |
770 |
try:
|
771 |
image.shrink() |
772 |
infobox.finalize() |
773 |
finally:
|
774 |
image.out.remove(infobox) |
775 |
|
776 |
session['shrinked'] = True |
777 |
update_background_title(session) |
778 |
else:
|
779 |
return False |
780 |
|
781 |
return True |
782 |
|
783 |
|
784 |
def customization_menu(session): |
785 |
"""Show image customization menu"""
|
786 |
d = session['dialog']
|
787 |
|
788 |
choices = [("Sysprep", "Run various image preparation tasks"), |
789 |
("Shrink", "Shrink image"), |
790 |
("View/Modify", "View/Modify image properties"), |
791 |
("Delete", "Delete image properties"), |
792 |
("Exclude", "Exclude various deployment tasks from running")] |
793 |
|
794 |
default_item = 0
|
795 |
|
796 |
actions = {"Sysprep": sysprep,
|
797 |
"Shrink": shrink,
|
798 |
"View/Modify": modify_properties,
|
799 |
"Delete": delete_properties,
|
800 |
"Exclude": exclude_tasks}
|
801 |
while 1: |
802 |
(code, choice) = d.menu( |
803 |
text="Choose one of the following or press <Back> to exit.",
|
804 |
width=WIDTH, choices=choices, cancel="Back", height=13, |
805 |
menu_height=len(choices), default_item=choices[default_item][0], |
806 |
title="Image Customization Menu")
|
807 |
|
808 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
809 |
break
|
810 |
elif choice in actions: |
811 |
default_item = [entry[0] for entry in choices].index(choice) |
812 |
if actions[choice](session):
|
813 |
default_item = (default_item + 1) % len(choices) |
814 |
|
815 |
|
816 |
def main_menu(session): |
817 |
"""Show the main menu of the program"""
|
818 |
d = session['dialog']
|
819 |
|
820 |
update_background_title(session) |
821 |
|
822 |
choices = [("Customize", "Customize image & cloud deployment options"), |
823 |
("Register", "Register image to a cloud"), |
824 |
("Extract", "Dump image to local file system"), |
825 |
("Reset", "Reset everything and start over again"), |
826 |
("Help", "Get help for using snf-image-creator")] |
827 |
|
828 |
default_item = "Customize"
|
829 |
|
830 |
actions = {"Customize": customization_menu, "Register": kamaki_menu, |
831 |
"Extract": extract_image}
|
832 |
while 1: |
833 |
(code, choice) = d.menu( |
834 |
text="Choose one of the following or press <Exit> to exit.",
|
835 |
width=WIDTH, choices=choices, cancel="Exit", height=13, |
836 |
default_item=default_item, menu_height=len(choices),
|
837 |
title="Image Creator for synnefo (snf-image-creator version %s)" %
|
838 |
version) |
839 |
|
840 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
841 |
if confirm_exit(d):
|
842 |
break
|
843 |
elif choice == "Reset": |
844 |
if confirm_reset(d):
|
845 |
d.infobox("Resetting snf-image-creator. Please wait...",
|
846 |
width=SMALL_WIDTH) |
847 |
raise Reset
|
848 |
elif choice == "Help": |
849 |
d.msgbox("For help, check the online documentation:\n\nhttp://www"
|
850 |
".synnefo.org/docs/snf-image-creator/latest/",
|
851 |
width=WIDTH, title="Help")
|
852 |
elif choice in actions: |
853 |
actions[choice](session) |
854 |
|
855 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
|