root / image_creator / dialog_main.py @ cf4f52b6
History | View | Annotate | Download (31 kB)
1 |
#!/usr/bin/env python
|
---|---|
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 |
import dialog |
37 |
import sys |
38 |
import os |
39 |
import textwrap |
40 |
import signal |
41 |
import StringIO |
42 |
import optparse |
43 |
|
44 |
from image_creator import __version__ as version |
45 |
from image_creator.util import FatalError, MD5 |
46 |
from image_creator.output.dialog import GaugeOutput, InfoBoxOutput |
47 |
from image_creator.disk import Disk |
48 |
from image_creator.os_type import os_cls |
49 |
from image_creator.kamaki_wrapper import Kamaki, ClientError |
50 |
from image_creator.help import get_help_file |
51 |
|
52 |
MSGBOX_WIDTH = 60
|
53 |
YESNO_WIDTH = 50
|
54 |
MENU_WIDTH = 70
|
55 |
INPUTBOX_WIDTH = 70
|
56 |
CHECKBOX_WIDTH = 70
|
57 |
HELP_WIDTH = 70
|
58 |
INFOBOX_WIDTH = 70
|
59 |
|
60 |
CONFIGURATION_TASKS = [ |
61 |
("Partition table manipulation", ["FixPartitionTable"], |
62 |
["linux", "windows"]), |
63 |
("File system resize",
|
64 |
["FilesystemResizeUnmounted", "FilesystemResizeMounted"], |
65 |
["linux", "windows"]), |
66 |
("Swap partition configuration", ["AddSwap"], ["linux"]), |
67 |
("SSH keys removal", ["DeleteSSHKeys"], ["linux"]), |
68 |
("Temporal RDP disabling", ["DisableRemoteDesktopConnections"], |
69 |
["windows"]),
|
70 |
("SELinux relabeling at next boot", ["SELinuxAutorelabel"], ["linux"]), |
71 |
("Hostname/Computer Name assignment", ["AssignHostname"], |
72 |
["windows", "linux"]), |
73 |
("Password change", ["ChangePassword"], ["windows", "linux"]), |
74 |
("File injection", ["EnforcePersonality"], ["windows", "linux"]) |
75 |
] |
76 |
|
77 |
|
78 |
class Reset(Exception): |
79 |
pass
|
80 |
|
81 |
|
82 |
class metadata_monitor(object): |
83 |
def __init__(self, session, meta): |
84 |
self.session = session
|
85 |
self.meta = meta
|
86 |
|
87 |
def __enter__(self): |
88 |
self.old = {}
|
89 |
for (k, v) in self.meta.items(): |
90 |
self.old[k] = v
|
91 |
|
92 |
def __exit__(self, type, value, traceback): |
93 |
d = self.session['dialog'] |
94 |
|
95 |
altered = {} |
96 |
added = {} |
97 |
|
98 |
for (k, v) in self.meta.items(): |
99 |
if k not in self.old: |
100 |
added[k] = v |
101 |
elif self.old[k] != v: |
102 |
altered[k] = v |
103 |
|
104 |
if not (len(added) or len(altered)): |
105 |
return
|
106 |
|
107 |
msg = "The last action has changed some image properties:\n\n"
|
108 |
if len(added): |
109 |
msg += "New image properties:\n"
|
110 |
for (k, v) in added.items(): |
111 |
msg += ' %s: "%s"\n' % (k, v)
|
112 |
msg += "\n"
|
113 |
if len(altered): |
114 |
msg += "Updated image properties:\n"
|
115 |
for (k, v) in altered.items(): |
116 |
msg += ' %s: "%s" -> "%s"\n' % (k, self.old[k], v) |
117 |
msg += "\n"
|
118 |
|
119 |
self.session['metadata'].update(added) |
120 |
self.session['metadata'].update(altered) |
121 |
d.msgbox(msg, title="Image Property Changes", width=MSGBOX_WIDTH)
|
122 |
|
123 |
|
124 |
def extract_metadata_string(session): |
125 |
metadata = ['%s=%s' % (k, v) for (k, v) in session['metadata'].items()] |
126 |
|
127 |
if 'task_metadata' in session: |
128 |
metadata.extend("%s=yes" % m for m in session['task_metadata']) |
129 |
|
130 |
return '\n'.join(metadata) + '\n' |
131 |
|
132 |
|
133 |
def confirm_exit(d, msg=''): |
134 |
return not d.yesno("%s Do you want to exit?" % msg, width=YESNO_WIDTH) |
135 |
|
136 |
|
137 |
def confirm_reset(d): |
138 |
return not d.yesno("Are you sure you want to reset everything?", |
139 |
width=YESNO_WIDTH, defaultno=1)
|
140 |
|
141 |
|
142 |
def update_background_title(session): |
143 |
d = session['dialog']
|
144 |
dev = session['device']
|
145 |
|
146 |
MB = 2 ** 20 |
147 |
|
148 |
size = (dev.meta['SIZE'] + MB - 1) // MB |
149 |
shrinked = 'shrinked' in session and session['shrinked'] |
150 |
postfix = " (shrinked)" if shrinked else '' |
151 |
|
152 |
title = "OS: %s, Distro: %s, Size: %dMB%s" % \
|
153 |
(dev.ostype, dev.distro, size, postfix) |
154 |
|
155 |
d.setBackgroundTitle(title) |
156 |
|
157 |
|
158 |
def extract_image(session): |
159 |
d = session['dialog']
|
160 |
dir = os.getcwd() |
161 |
while 1: |
162 |
if dir and dir[-1] != os.sep: |
163 |
dir = dir + os.sep
|
164 |
|
165 |
(code, path) = d.fselect(dir, 10, 50, title="Save image as...") |
166 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
167 |
return False |
168 |
|
169 |
if os.path.isdir(path):
|
170 |
dir = path |
171 |
continue
|
172 |
|
173 |
if os.path.isdir("%s.meta" % path): |
174 |
d.msgbox("Can't overwrite directory `%s.meta'" % path,
|
175 |
width=MSGBOX_WIDTH) |
176 |
continue
|
177 |
|
178 |
if os.path.isdir("%s.md5sum" % path): |
179 |
d.msgbox("Can't overwrite directory `%s.md5sum'" % path,
|
180 |
width=MSGBOX_WIDTH) |
181 |
continue
|
182 |
|
183 |
basedir = os.path.dirname(path) |
184 |
name = os.path.basename(path) |
185 |
if not os.path.exists(basedir): |
186 |
d.msgbox("Directory `%s' does not exist" % basedir,
|
187 |
width=MSGBOX_WIDTH) |
188 |
continue
|
189 |
|
190 |
dir = basedir |
191 |
if len(name) == 0: |
192 |
continue
|
193 |
|
194 |
files = ["%s%s" % (path, ext) for ext in ('', '.meta', '.md5sum')] |
195 |
overwrite = filter(os.path.exists, files)
|
196 |
|
197 |
if len(overwrite) > 0: |
198 |
if d.yesno("The following file(s) exist:\n" |
199 |
"%s\nDo you want to overwrite them?" %
|
200 |
"\n".join(overwrite), width=YESNO_WIDTH):
|
201 |
continue
|
202 |
|
203 |
out = GaugeOutput(d, "Image Extraction", "Extracting image...") |
204 |
try:
|
205 |
dev = session['device']
|
206 |
if "checksum" not in session: |
207 |
size = dev.meta['SIZE']
|
208 |
md5 = MD5(out) |
209 |
session['checksum'] = md5.compute(session['snapshot'], size) |
210 |
|
211 |
# Extract image file
|
212 |
dev.out = out |
213 |
dev.dump(path) |
214 |
|
215 |
# Extract metadata file
|
216 |
out.output("Extracting metadata file...")
|
217 |
with open('%s.meta' % path, 'w') as f: |
218 |
f.write(extract_metadata_string(session)) |
219 |
out.success('done')
|
220 |
|
221 |
# Extract md5sum file
|
222 |
out.output("Extracting md5sum file...")
|
223 |
md5str = "%s %s\n" % (session['checksum'], name) |
224 |
with open('%s.md5sum' % path, 'w') as f: |
225 |
f.write(md5str) |
226 |
out.success("done")
|
227 |
|
228 |
finally:
|
229 |
out.cleanup() |
230 |
d.msgbox("Image file `%s' was successfully extracted!" % path,
|
231 |
width=MSGBOX_WIDTH) |
232 |
break
|
233 |
|
234 |
return True |
235 |
|
236 |
|
237 |
def upload_image(session): |
238 |
d = session["dialog"]
|
239 |
size = session['device'].meta['SIZE'] |
240 |
|
241 |
if "account" not in session: |
242 |
d.msgbox("You need to provide your ~okeanos login username before you "
|
243 |
"can upload images to pithos+", width=MSGBOX_WIDTH)
|
244 |
return False |
245 |
|
246 |
if "token" not in session: |
247 |
d.msgbox("You need to provide your ~okeanos account authentication "
|
248 |
"token before you can upload images to pithos+",
|
249 |
width=MSGBOX_WIDTH) |
250 |
return False |
251 |
|
252 |
while 1: |
253 |
init = session["upload"] if "upload" in session else '' |
254 |
(code, answer) = d.inputbox("Please provide a filename:", init=init,
|
255 |
width=INPUTBOX_WIDTH) |
256 |
|
257 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
258 |
return False |
259 |
|
260 |
filename = answer.strip() |
261 |
if len(filename) == 0: |
262 |
d.msgbox("Filename cannot be empty", width=MSGBOX_WIDTH)
|
263 |
continue
|
264 |
|
265 |
break
|
266 |
|
267 |
out = GaugeOutput(d, "Image Upload", "Uploading...") |
268 |
try:
|
269 |
if 'checksum' not in session: |
270 |
md5 = MD5(out) |
271 |
session['checksum'] = md5.compute(session['snapshot'], size) |
272 |
kamaki = Kamaki(session['account'], session['token'], out) |
273 |
try:
|
274 |
# Upload image file
|
275 |
with open(session['snapshot'], 'rb') as f: |
276 |
session["upload"] = kamaki.upload(f, size, filename,
|
277 |
"Calculating block hashes",
|
278 |
"Uploading missing blocks")
|
279 |
# Upload metadata file
|
280 |
out.output("Uploading metadata file...")
|
281 |
metastring = extract_metadata_string(session) |
282 |
kamaki.upload(StringIO.StringIO(metastring), size=len(metastring),
|
283 |
remote_path="%s.meta" % filename)
|
284 |
out.success("done")
|
285 |
|
286 |
# Upload md5sum file
|
287 |
out.output("Uploading md5sum file...")
|
288 |
md5str = "%s %s\n" % (session['checksum'], filename) |
289 |
kamaki.upload(StringIO.StringIO(md5str), size=len(md5str),
|
290 |
remote_path="%s.md5sum" % filename)
|
291 |
out.success("done")
|
292 |
|
293 |
except ClientError as e: |
294 |
d.msgbox("Error in pithos+ client: %s" % e.message,
|
295 |
title="Pithos+ Client Error", width=MSGBOX_WIDTH)
|
296 |
if 'upload' in session: |
297 |
del session['upload'] |
298 |
return False |
299 |
finally:
|
300 |
out.cleanup() |
301 |
|
302 |
d.msgbox("Image file `%s' was successfully uploaded to pithos+" % filename,
|
303 |
width=MSGBOX_WIDTH) |
304 |
|
305 |
return True |
306 |
|
307 |
|
308 |
def register_image(session): |
309 |
d = session["dialog"]
|
310 |
|
311 |
if "account" not in session: |
312 |
d.msgbox("You need to provide your ~okeanos login username before you "
|
313 |
"can register an images to cyclades",
|
314 |
width=MSGBOX_WIDTH) |
315 |
return False |
316 |
|
317 |
if "token" not in session: |
318 |
d.msgbox("You need to provide your ~okeanos account authentication "
|
319 |
"token before you can register an images to cyclades",
|
320 |
width=MSGBOX_WIDTH) |
321 |
return False |
322 |
|
323 |
if "upload" not in session: |
324 |
d.msgbox("You need to upload the image to pithos+ before you can "
|
325 |
"register it to cyclades", width=MSGBOX_WIDTH)
|
326 |
return False |
327 |
|
328 |
while 1: |
329 |
(code, answer) = d.inputbox("Please provide a registration name:",
|
330 |
width=INPUTBOX_WIDTH) |
331 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
332 |
return False |
333 |
|
334 |
name = answer.strip() |
335 |
if len(name) == 0: |
336 |
d.msgbox("Registration name cannot be empty", width=MSGBOX_WIDTH)
|
337 |
continue
|
338 |
break
|
339 |
|
340 |
metadata = {} |
341 |
metadata.update(session['metadata'])
|
342 |
if 'task_metadata' in session: |
343 |
for key in session['task_metadata']: |
344 |
metadata[key] = 'yes'
|
345 |
|
346 |
out = GaugeOutput(d, "Image Registration", "Registrating image...") |
347 |
try:
|
348 |
out.output("Registring image to cyclades...")
|
349 |
try:
|
350 |
kamaki = Kamaki(session['account'], session['token'], out) |
351 |
kamaki.register(name, session['upload'], metadata)
|
352 |
out.success('done')
|
353 |
except ClientError as e: |
354 |
d.msgbox("Error in pithos+ client: %s" % e.message)
|
355 |
return False |
356 |
finally:
|
357 |
out.cleanup() |
358 |
|
359 |
d.msgbox("Image `%s' was successfully registered to cyclades as `%s'" %
|
360 |
(session['upload'], name), width=MSGBOX_WIDTH)
|
361 |
return True |
362 |
|
363 |
|
364 |
def kamaki_menu(session): |
365 |
d = session['dialog']
|
366 |
default_item = "Account"
|
367 |
|
368 |
(session['account'], session['token']) = Kamaki.saved_credentials() |
369 |
while 1: |
370 |
account = session["account"] if "account" in session else "<none>" |
371 |
token = session["token"] if "token" in session else "<none>" |
372 |
upload = session["upload"] if "upload" in session else "<none>" |
373 |
|
374 |
choices = [("Account", "Change your ~okeanos username: %s" % account), |
375 |
("Token", "Change your ~okeanos token: %s" % token), |
376 |
("Upload", "Upload image to pithos+"), |
377 |
("Register", "Register the image to cyclades: %s" % upload)] |
378 |
|
379 |
(code, choice) = d.menu( |
380 |
text="Choose one of the following or press <Back> to go back.",
|
381 |
width=MENU_WIDTH, choices=choices, cancel="Back", height=13, |
382 |
menu_height=5, default_item=default_item,
|
383 |
title="Image Registration Menu")
|
384 |
|
385 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
386 |
return False |
387 |
|
388 |
if choice == "Account": |
389 |
default_item = "Account"
|
390 |
(code, answer) = d.inputbox( |
391 |
"Please provide your ~okeanos account e-mail address:",
|
392 |
init=session["account"] if "account" in session else '', |
393 |
width=70)
|
394 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
395 |
continue
|
396 |
if len(answer) == 0 and "account" in session: |
397 |
del session["account"] |
398 |
else:
|
399 |
session["account"] = answer.strip()
|
400 |
Kamaki.save_account(session['account'])
|
401 |
default_item = "Token"
|
402 |
elif choice == "Token": |
403 |
default_item = "Token"
|
404 |
(code, answer) = d.inputbox( |
405 |
"Please provide your ~okeanos account authetication token:",
|
406 |
init=session["token"] if "token" in session else '', |
407 |
width=70)
|
408 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
409 |
continue
|
410 |
if len(answer) == 0 and "token" in session: |
411 |
del session["token"] |
412 |
else:
|
413 |
session["token"] = answer.strip()
|
414 |
Kamaki.save_token(session['account'])
|
415 |
default_item = "Upload"
|
416 |
elif choice == "Upload": |
417 |
if upload_image(session):
|
418 |
default_item = "Register"
|
419 |
else:
|
420 |
default_item = "Upload"
|
421 |
elif choice == "Register": |
422 |
if register_image(session):
|
423 |
return True |
424 |
else:
|
425 |
default_item = "Register"
|
426 |
|
427 |
|
428 |
def add_property(session): |
429 |
d = session['dialog']
|
430 |
|
431 |
while 1: |
432 |
(code, answer) = d.inputbox("Please provide a name for a new image"
|
433 |
" property:", width=INPUTBOX_WIDTH)
|
434 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
435 |
return False |
436 |
|
437 |
name = answer.strip() |
438 |
if len(name) == 0: |
439 |
d.msgbox("A property name cannot be empty", width=MSGBOX_WIDTH)
|
440 |
continue
|
441 |
|
442 |
break
|
443 |
|
444 |
while 1: |
445 |
(code, answer) = d.inputbox("Please provide a value for image "
|
446 |
"property %s" % name, width=INPUTBOX_WIDTH)
|
447 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
448 |
return False |
449 |
|
450 |
value = answer.strip() |
451 |
if len(value) == 0: |
452 |
d.msgbox("Value cannot be empty", width=MSGBOX_WIDTH)
|
453 |
continue
|
454 |
|
455 |
break
|
456 |
|
457 |
session['metadata'][name] = value
|
458 |
|
459 |
return True |
460 |
|
461 |
|
462 |
def modify_properties(session): |
463 |
d = session['dialog']
|
464 |
|
465 |
while 1: |
466 |
choices = [] |
467 |
for (key, val) in session['metadata'].items(): |
468 |
choices.append((str(key), str(val))) |
469 |
|
470 |
(code, choice) = d.menu( |
471 |
"In this menu you can edit existing image properties or add new "
|
472 |
"ones. Be careful! Most properties have special meaning and "
|
473 |
"alter the image deployment behaviour. Press <HELP> to see more "
|
474 |
"information about image properties. Press <BACK> when done.",
|
475 |
height=18, width=MENU_WIDTH, choices=choices, menu_height=10, |
476 |
ok_label="Edit", extra_button=1, extra_label="Add", cancel="Back", |
477 |
help_button=1, title="Image Properties") |
478 |
|
479 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
480 |
return True |
481 |
# Edit button
|
482 |
elif code == d.DIALOG_OK:
|
483 |
(code, answer) = d.inputbox("Please provide a new value for the "
|
484 |
"image property with name `%s':" %
|
485 |
choice, |
486 |
init=session['metadata'][choice],
|
487 |
width=INPUTBOX_WIDTH) |
488 |
if code not in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
489 |
value = answer.strip() |
490 |
if len(value) == 0: |
491 |
d.msgbox("Value cannot be empty!")
|
492 |
continue
|
493 |
else:
|
494 |
session['metadata'][choice] = value
|
495 |
# ADD button
|
496 |
elif code == d.DIALOG_EXTRA:
|
497 |
add_property(session) |
498 |
elif code == 'help': |
499 |
help_file = get_help_file("image_properties")
|
500 |
assert os.path.exists(help_file)
|
501 |
d.textbox(help_file, title="Image Properties", width=70, height=40) |
502 |
|
503 |
|
504 |
def delete_properties(session): |
505 |
d = session['dialog']
|
506 |
|
507 |
choices = [] |
508 |
for (key, val) in session['metadata'].items(): |
509 |
choices.append((key, "%s" % val, 0)) |
510 |
|
511 |
(code, to_delete) = d.checklist("Choose which properties to delete:",
|
512 |
choices=choices, width=CHECKBOX_WIDTH) |
513 |
|
514 |
# If the user exits with ESC or CANCEL, the returned tag list is empty.
|
515 |
for i in to_delete: |
516 |
del session['metadata'][i] |
517 |
|
518 |
cnt = len(to_delete)
|
519 |
if cnt > 0: |
520 |
d.msgbox("%d image properties were deleted." % cnt, width=MSGBOX_WIDTH)
|
521 |
return True |
522 |
else:
|
523 |
return False |
524 |
|
525 |
|
526 |
def exclude_tasks(session): |
527 |
d = session['dialog']
|
528 |
|
529 |
index = 0
|
530 |
displayed_index = 1
|
531 |
choices = [] |
532 |
mapping = {} |
533 |
if 'excluded_tasks' not in session: |
534 |
session['excluded_tasks'] = []
|
535 |
|
536 |
if -1 in session['excluded_tasks']: |
537 |
if not d.yesno("Image deployment configuration is disabled. " |
538 |
"Do you wish to enable it?", width=YESNO_WIDTH):
|
539 |
session['excluded_tasks'].remove(-1) |
540 |
else:
|
541 |
return False |
542 |
|
543 |
for (msg, task, osfamily) in CONFIGURATION_TASKS: |
544 |
if session['metadata']['OSFAMILY'] in osfamily: |
545 |
checked = 1 if index in session['excluded_tasks'] else 0 |
546 |
choices.append((str(displayed_index), msg, checked))
|
547 |
mapping[displayed_index] = index |
548 |
displayed_index += 1
|
549 |
index += 1
|
550 |
|
551 |
while 1: |
552 |
(code, tags) = d.checklist( |
553 |
text="Please choose which configuration tasks you would like to "
|
554 |
"prevent from running during image deployment. "
|
555 |
"Press <No Config> to supress any configuration. "
|
556 |
"Press <Help> for more help on the image deployment "
|
557 |
"configuration tasks.",
|
558 |
choices=choices, height=19, list_height=8, width=CHECKBOX_WIDTH, |
559 |
help_button=1, extra_button=1, extra_label="No Config", |
560 |
title="Exclude Configuration Tasks")
|
561 |
|
562 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
563 |
return False |
564 |
elif code == d.DIALOG_HELP:
|
565 |
help_file = get_help_file("configuration_tasks")
|
566 |
assert os.path.exists(help_file)
|
567 |
d.textbox(help_file, title="Configuration Tasks",
|
568 |
width=70, height=40) |
569 |
# No Config button
|
570 |
elif code == d.DIALOG_EXTRA:
|
571 |
session['excluded_tasks'] = [-1] |
572 |
session['task_metadata'] = ["EXCLUDE_ALL_TASKS"] |
573 |
break
|
574 |
elif code == d.DIALOG_OK:
|
575 |
session['excluded_tasks'] = []
|
576 |
for tag in tags: |
577 |
session['excluded_tasks'].append(mapping[int(tag)]) |
578 |
|
579 |
exclude_metadata = [] |
580 |
for task in session['excluded_tasks']: |
581 |
exclude_metadata.extend(CONFIGURATION_TASKS[task][1])
|
582 |
|
583 |
session['task_metadata'] = map(lambda x: "EXCLUDE_TASK_%s" % x, |
584 |
exclude_metadata) |
585 |
break
|
586 |
|
587 |
return True |
588 |
|
589 |
|
590 |
def sysprep(session): |
591 |
d = session['dialog']
|
592 |
image_os = session['image_os']
|
593 |
|
594 |
# Is the image already shrinked?
|
595 |
if 'shrinked' in session and session['shrinked']: |
596 |
msg = "It seems you have shrinked the image. Running system " \
|
597 |
"preparation tasks on a shrinked image is dangerous."
|
598 |
|
599 |
if d.yesno("%s\n\nDo you really want to continue?" % msg, |
600 |
width=YESNO_WIDTH, defaultno=1):
|
601 |
return
|
602 |
|
603 |
wrapper = textwrap.TextWrapper(width=65)
|
604 |
|
605 |
help_title = "System Preperation Tasks"
|
606 |
sysprep_help = "%s\n%s\n\n" % (help_title, '=' * len(help_title)) |
607 |
|
608 |
if 'exec_syspreps' not in session: |
609 |
session['exec_syspreps'] = []
|
610 |
|
611 |
all_syspreps = image_os.list_syspreps() |
612 |
# Only give the user the choice between syspreps that have not ran yet
|
613 |
syspreps = [s for s in all_syspreps if s not in session['exec_syspreps']] |
614 |
|
615 |
if len(syspreps) == 0: |
616 |
d.msgbox("No system preparation task available to run!",
|
617 |
title="System Preperation", width=MSGBOX_WIDTH)
|
618 |
return
|
619 |
|
620 |
while 1: |
621 |
choices = [] |
622 |
index = 0
|
623 |
for sysprep in syspreps: |
624 |
name, descr = image_os.sysprep_info(sysprep) |
625 |
display_name = name.replace('-', ' ').capitalize() |
626 |
sysprep_help += "%s\n" % display_name
|
627 |
sysprep_help += "%s\n" % ('-' * len(display_name)) |
628 |
sysprep_help += "%s\n\n" % wrapper.fill(" ".join(descr.split())) |
629 |
enabled = 1 if sysprep.enabled else 0 |
630 |
choices.append((str(index + 1), display_name, enabled)) |
631 |
index += 1
|
632 |
|
633 |
(code, tags) = d.checklist( |
634 |
"Please choose which system preperation tasks you would like to "
|
635 |
"run on the image. Press <Help> to see details about the system "
|
636 |
"preperation tasks.", title="Run system preperation tasks", |
637 |
choices=choices, width=70, ok_label="Run", help_button=1) |
638 |
|
639 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
640 |
return False |
641 |
elif code == d.DIALOG_HELP:
|
642 |
d.scrollbox(sysprep_help, width=HELP_WIDTH) |
643 |
elif code == d.DIALOG_OK:
|
644 |
# Enable selected syspreps and disable the rest
|
645 |
for i in range(len(syspreps)): |
646 |
if str(i + 1) in tags: |
647 |
image_os.enable_sysprep(syspreps[i]) |
648 |
session['exec_syspreps'].append(syspreps[i])
|
649 |
else:
|
650 |
image_os.disable_sysprep(syspreps[i]) |
651 |
|
652 |
out = InfoBoxOutput(d, "Image Configuration")
|
653 |
try:
|
654 |
dev = session['device']
|
655 |
dev.out = out |
656 |
dev.mount(readonly=False)
|
657 |
try:
|
658 |
# The checksum is invalid. We have mounted the image rw
|
659 |
if 'checksum' in session: |
660 |
del session['checksum'] |
661 |
|
662 |
# Monitor the metadata changes during syspreps
|
663 |
with metadata_monitor(session, image_os.meta):
|
664 |
image_os.out = out |
665 |
image_os.do_sysprep() |
666 |
image_os.out.finalize() |
667 |
|
668 |
# Disable syspreps that have ran
|
669 |
for sysprep in session['exec_syspreps']: |
670 |
image_os.disable_sysprep(sysprep) |
671 |
|
672 |
finally:
|
673 |
dev.umount() |
674 |
finally:
|
675 |
out.cleanup() |
676 |
break
|
677 |
return True |
678 |
|
679 |
|
680 |
def shrink(session): |
681 |
d = session['dialog']
|
682 |
dev = session['device']
|
683 |
|
684 |
shrinked = 'shrinked' in session and session['shrinked'] |
685 |
|
686 |
if shrinked:
|
687 |
d.msgbox("The image is already shrinked!", title="Image Shrinking", |
688 |
width=MSGBOX_WIDTH) |
689 |
return True |
690 |
|
691 |
msg = "This operation will shrink the last partition of the image to " \
|
692 |
"reduce the total image size. If the last partition is a swap " \
|
693 |
"partition, then this partition is removed and the partition " \
|
694 |
"before that is shrinked. The removed swap partition will be " \
|
695 |
"recreated during image deployment."
|
696 |
|
697 |
if not d.yesno("%s\n\nDo you want to continue?" % msg, width=70, |
698 |
height=12, title="Image Shrinking"): |
699 |
with metadata_monitor(session, dev.meta):
|
700 |
dev.out = InfoBoxOutput(d, "Image Shrinking", height=4) |
701 |
dev.shrink() |
702 |
dev.out.finalize() |
703 |
|
704 |
session['shrinked'] = True |
705 |
update_background_title(session) |
706 |
else:
|
707 |
return False |
708 |
|
709 |
return True |
710 |
|
711 |
|
712 |
def customization_menu(session): |
713 |
d = session['dialog']
|
714 |
|
715 |
choices = [("Sysprep", "Run various image preperation tasks"), |
716 |
("Shrink", "Shrink image"), |
717 |
("View/Modify", "View/Modify image properties"), |
718 |
("Delete", "Delete image properties"), |
719 |
("Exclude", "Exclude various deployment tasks from running")] |
720 |
|
721 |
default_item = 0
|
722 |
|
723 |
actions = {"Sysprep": sysprep,
|
724 |
"Shrink": shrink,
|
725 |
"View/Modify": modify_properties,
|
726 |
"Delete": delete_properties,
|
727 |
"Exclude": exclude_tasks}
|
728 |
while 1: |
729 |
(code, choice) = d.menu( |
730 |
text="Choose one of the following or press <Back> to exit.",
|
731 |
width=MENU_WIDTH, choices=choices, cancel="Back", height=13, |
732 |
menu_height=len(choices), default_item=choices[default_item][0], |
733 |
title="Image Customization Menu")
|
734 |
|
735 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
736 |
break
|
737 |
elif choice in actions: |
738 |
default_item = [entry[0] for entry in choices].index(choice) |
739 |
if actions[choice](session):
|
740 |
default_item = (default_item + 1) % len(choices) |
741 |
|
742 |
|
743 |
def main_menu(session): |
744 |
d = session['dialog']
|
745 |
dev = session['device']
|
746 |
|
747 |
update_background_title(session) |
748 |
|
749 |
choices = [("Customize", "Customize image & ~okeanos deployment options"), |
750 |
("Register", "Register image to ~okeanos"), |
751 |
("Extract", "Dump image to local file system"), |
752 |
("Reset", "Reset everything and start over again"), |
753 |
("Help", "Get help for using snf-image-creator")] |
754 |
|
755 |
default_item = "Customize"
|
756 |
|
757 |
actions = {"Customize": customization_menu, "Register": kamaki_menu, |
758 |
"Extract": extract_image}
|
759 |
while 1: |
760 |
(code, choice) = d.menu( |
761 |
text="Choose one of the following or press <Exit> to exit.",
|
762 |
width=MENU_WIDTH, choices=choices, cancel="Exit", height=13, |
763 |
default_item=default_item, menu_height=len(choices),
|
764 |
title="Image Creator for ~okeanos (snf-image-creator version %s)" %
|
765 |
version) |
766 |
|
767 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
768 |
if confirm_exit(d):
|
769 |
break
|
770 |
elif choice == "Reset": |
771 |
if confirm_reset(d):
|
772 |
d.infobox("Resetting snf-image-creator. Please wait...",
|
773 |
width=INFOBOX_WIDTH) |
774 |
raise Reset
|
775 |
elif choice in actions: |
776 |
actions[choice](session) |
777 |
|
778 |
|
779 |
def select_file(d, media): |
780 |
root = os.sep |
781 |
while 1: |
782 |
if media is not None: |
783 |
if not os.path.exists(media): |
784 |
d.msgbox("The file you choose does not exist",
|
785 |
width=MSGBOX_WIDTH) |
786 |
else:
|
787 |
break
|
788 |
|
789 |
(code, media) = d.fselect(root, 10, 50, |
790 |
title="Please select input media")
|
791 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
792 |
if confirm_exit(d, "You canceled the media selection dialog box."): |
793 |
sys.exit(0)
|
794 |
else:
|
795 |
media = None
|
796 |
continue
|
797 |
|
798 |
return media
|
799 |
|
800 |
|
801 |
def wizard(session): |
802 |
pass
|
803 |
|
804 |
|
805 |
def image_creator(d): |
806 |
|
807 |
usage = "Usage: %prog [options] [<input_media>]"
|
808 |
parser = optparse.OptionParser(version=version, usage=usage) |
809 |
|
810 |
options, args = parser.parse_args(sys.argv[1:])
|
811 |
|
812 |
if len(args) > 1: |
813 |
parser.error("Wrong numver of arguments")
|
814 |
|
815 |
d.setBackgroundTitle('snf-image-creator')
|
816 |
|
817 |
if os.geteuid() != 0: |
818 |
raise FatalError("You must run %s as root" % basename) |
819 |
|
820 |
media = select_file(d, args[0] if len(args) == 1 else None) |
821 |
|
822 |
out = GaugeOutput(d, "Initialization", "Initializing...") |
823 |
disk = Disk(media, out) |
824 |
|
825 |
def signal_handler(signum, frame): |
826 |
out.cleanup() |
827 |
disk.cleanup() |
828 |
|
829 |
signal.signal(signal.SIGINT, signal_handler) |
830 |
try:
|
831 |
snapshot = disk.snapshot() |
832 |
dev = disk.get_device(snapshot) |
833 |
|
834 |
metadata = {} |
835 |
for (key, value) in dev.meta.items(): |
836 |
metadata[str(key)] = str(value) |
837 |
|
838 |
dev.mount(readonly=True)
|
839 |
out.output("Collecting image metadata...")
|
840 |
cls = os_cls(dev.distro, dev.ostype) |
841 |
image_os = cls(dev.root, dev.g, out) |
842 |
dev.umount() |
843 |
|
844 |
for (key, value) in image_os.meta.items(): |
845 |
metadata[str(key)] = str(value) |
846 |
|
847 |
out.success("done")
|
848 |
out.cleanup() |
849 |
|
850 |
# Make sure the signal handler does not call out.cleanup again
|
851 |
def dummy(self): |
852 |
pass
|
853 |
out.cleanup = type(GaugeOutput.cleanup)(dummy, out, GaugeOutput)
|
854 |
|
855 |
session = {"dialog": d,
|
856 |
"disk": disk,
|
857 |
"snapshot": snapshot,
|
858 |
"device": dev,
|
859 |
"image_os": image_os,
|
860 |
"metadata": metadata}
|
861 |
|
862 |
msg = "snf-image-creator detected a %s system on the input media. " \
|
863 |
"Would you like to run a wizards to assists you through the " \
|
864 |
"image creation process?\n\nChoose <Yes> to run the wizard, " \
|
865 |
"<No> to run the snf-image-creator in expert mode or press " \
|
866 |
"ESC to quit the program." \
|
867 |
% (dev.ostype if dev.ostype == dev.distro else "%s/%s" % |
868 |
(dev.distro, dev.ostype)) |
869 |
|
870 |
while True: |
871 |
code = d.yesno(msg, width=YESNO_WIDTH, height=12)
|
872 |
if code == d.DIALOG_OK:
|
873 |
if wizard(session):
|
874 |
break
|
875 |
elif code == d.DIALOG_CANCEL:
|
876 |
main_menu(session) |
877 |
break
|
878 |
|
879 |
exit_msg = "You have not selected if you want to run " \
|
880 |
"snf-image-creator in wizard or expert mode."
|
881 |
if confirm_exit(d, exit_msg):
|
882 |
break
|
883 |
|
884 |
d.infobox("Thank you for using snf-image-creator. Bye", width=53) |
885 |
finally:
|
886 |
disk.cleanup() |
887 |
|
888 |
return 0 |
889 |
|
890 |
|
891 |
def main(): |
892 |
|
893 |
d = dialog.Dialog(dialog="dialog")
|
894 |
|
895 |
# Add extra button in dialog library
|
896 |
dialog._common_args_syntax["extra_button"] = \
|
897 |
lambda enable: dialog._simple_option("--extra-button", enable) |
898 |
|
899 |
dialog._common_args_syntax["extra_label"] = \
|
900 |
lambda string: ("--extra-label", string) |
901 |
|
902 |
while 1: |
903 |
try:
|
904 |
try:
|
905 |
ret = image_creator(d) |
906 |
sys.exit(ret) |
907 |
except FatalError as e: |
908 |
msg = textwrap.fill(str(e), width=70) |
909 |
d.infobox(msg, width=INFOBOX_WIDTH, title="Fatal Error")
|
910 |
sys.exit(1)
|
911 |
except Reset:
|
912 |
continue
|
913 |
|
914 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
|