cfce374d169037194b0f85dd0edb94772ca2f978
[snf-image-creator] / image_creator / dialog_wizard.py
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 "wizard" mode of the dialog-based version of
37 snf-image-creator.
38 """
39
40 import time
41 import StringIO
42 import json
43
44 from image_creator.kamaki_wrapper import Kamaki, ClientError
45 from image_creator.util import MD5, FatalError
46 from image_creator.output.cli import OutputWthProgress
47 from image_creator.dialog_util import extract_image, update_background_title, \
48     add_cloud, edit_cloud
49
50 PAGE_WIDTH = 70
51 PAGE_HEIGHT = 10
52 SYSPREP_PARAM_MAXLEN = 20
53
54
55 class WizardExit(Exception):
56     """Exception used to exit the wizard"""
57     pass
58
59
60 class WizardReloadPage(Exception):
61     """Exception that reloads the last WizardPage"""
62     pass
63
64
65 class Wizard:
66     """Represents a dialog-based wizard
67
68     The wizard is a collection of pages that have a "Next" and a "Back" button
69     on them. The pages are used to collect user data.
70     """
71
72     def __init__(self, session):
73         self.session = session
74         self.pages = []
75         self.session['wizard'] = {}
76         self.d = session['dialog']
77
78     def add_page(self, page):
79         """Add a new page to the wizard"""
80         self.pages.append(page)
81
82     def run(self):
83         """Run the wizard"""
84         idx = 0
85         while True:
86             try:
87                 total = len(self.pages)
88                 title = "(%d/%d) %s" % (idx + 1, total, self.pages[idx].title)
89                 idx += self.pages[idx].run(self.session, title)
90             except WizardExit:
91                 return False
92             except WizardReloadPage:
93                 continue
94
95             if idx >= len(self.pages):
96                 text = "All necessary information has been gathered:\n\n"
97                 for page in self.pages:
98                     text += " * %s\n" % page.info
99                 text += "\nContinue with the image creation process?"
100
101                 ret = self.d.yesno(
102                     text, width=PAGE_WIDTH, height=8 + len(self.pages),
103                     ok_label="Yes", cancel="Back", extra_button=1,
104                     extra_label="Quit", title="Confirmation")
105
106                 if ret == self.d.DIALOG_CANCEL:
107                     idx -= 1
108                 elif ret == self.d.DIALOG_EXTRA:
109                     return False
110                 elif ret == self.d.DIALOG_OK:
111                     return True
112
113             if idx < 0:
114                 return False
115
116
117 class WizardPage(object):
118     """Represents a page in a wizard"""
119     NEXT = 1
120     PREV = -1
121
122     def __init__(self, name, display_name, text, **kargs):
123         self.name = name
124         self.display_name = display_name
125         self.text = text
126
127         self.title = kargs['title'] if 'title' in kargs else ""
128         self.default = kargs['default'] if 'default' in kargs else ""
129         self.extra = kargs['extra'] if 'extra' in kargs else None
130         self.extra_label = \
131             kargs['extra_label'] if 'extra_label' in kargs else 'Extra'
132
133         self.info = "%s: <none>" % self.display_name
134
135         validate = kargs['validate'] if 'validate' in kargs else lambda x: x
136         setattr(self, "validate", validate)
137
138         display = kargs['display'] if 'display' in kargs else lambda x: x
139         setattr(self, "display", display)
140
141     def run(self, session, title):
142         """Display this wizard page
143
144         This function is used by the wizard program when accessing a page.
145         """
146         raise NotImplementedError
147
148
149 class WizardPageWthChoices(WizardPage):
150     """Represents a Wizard Page that allows the user to select something from
151     a list of choices.
152
153     The available choices are created by a function passed to the class through
154     the choices variable. If the choices function returns an empty list, a
155     fallback funtion is executed if available.
156     """
157     def __init__(self, name, display_name, text, choices, **kargs):
158         super(WizardPageWthChoices, self).__init__(name, display_name, text,
159                                                    **kargs)
160         self.choices = choices
161         self.fallback = kargs['fallback'] if 'fallback' in kargs else None
162
163
164 class WizardRadioListPage(WizardPageWthChoices):
165     """Represent a Radio List in a wizard"""
166
167     def run(self, session, title):
168         d = session['dialog']
169         w = session['wizard']
170
171         choices = []
172         for choice in self.choices():
173             default = 1 if choice[0] == self.default else 0
174             choices.append((choice[0], choice[1], default))
175
176         (code, answer) = d.radiolist(
177             self.text, width=PAGE_WIDTH, ok_label="Next", cancel="Back",
178             choices=choices, height=PAGE_HEIGHT, title=title)
179
180         if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
181             return self.PREV
182
183         w[self.name] = self.validate(answer)
184         self.default = answer
185         self.info = "%s: %s" % (self.display_name, self.display(w[self.name]))
186
187         return self.NEXT
188
189
190 class WizardInputPage(WizardPage):
191     """Represents an input field in a wizard"""
192
193     def run(self, session, title):
194         d = session['dialog']
195         w = session['wizard']
196
197         (code, answer) = d.inputbox(
198             self.text, init=self.default, width=PAGE_WIDTH, ok_label="Next",
199             cancel="Back", height=PAGE_HEIGHT, title=title)
200
201         if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
202             return self.PREV
203
204         value = answer.strip()
205         self.default = value
206         w[self.name] = self.validate(value)
207         self.info = "%s: %s" % (self.display_name, self.display(w[self.name]))
208
209         return self.NEXT
210
211
212 class WizardFormPage(WizardPage):
213     """Represents a Form in a wizard"""
214
215     def __init__(self, name, display_name, text, fields, **kargs):
216         super(WizardFormPage, self).__init__(name, display_name, text, **kargs)
217         self.fields = fields
218
219     def run(self, session, title):
220         d = session['dialog']
221         w = session['wizard']
222
223         field_lenght = len(self.fields())
224         form_height = field_lenght if field_lenght < PAGE_HEIGHT - 4 \
225             else PAGE_HEIGHT - 4
226
227         (code, output) = d.form(
228             self.text, width=PAGE_WIDTH, height=PAGE_HEIGHT,
229             form_height=form_height, ok_label="Next", cancel="Back",
230             fields=self.fields(), title=title)
231
232         if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
233             return self.PREV
234
235         w[self.name] = self.validate(output)
236         self.default = output
237         self.info = "%s: %s" % (self.display_name, self.display(w[self.name]))
238
239         return self.NEXT
240
241
242 class WizardMenuPage(WizardPageWthChoices):
243     """Represents a menu dialog with available choices in a wizard"""
244
245     def run(self, session, title):
246         d = session['dialog']
247         w = session['wizard']
248
249         extra_button = 1 if self.extra else 0
250
251         choices = self.choices()
252
253         if len(choices) == 0:
254             assert self.fallback, "Zero choices and no fallback"
255             if self.fallback():
256                 raise WizardReloadPage
257             else:
258                 return self.PREV
259
260         default_item = self.default if self.default else choices[0][0]
261
262         (code, choice) = d.menu(
263             self.text, width=PAGE_WIDTH, ok_label="Next", cancel="Back",
264             title=title, choices=choices, height=PAGE_HEIGHT,
265             default_item=default_item, extra_label=self.extra_label,
266             extra_button=extra_button)
267
268         if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
269             return self.PREV
270         elif code == d.DIALOG_EXTRA:
271             self.extra()
272             raise WizardReloadPage
273
274         self.default = choice
275         w[self.name] = self.validate(choice)
276         self.info = "%s: %s" % (self.display_name, self.display(w[self.name]))
277
278         return self.NEXT
279
280
281 def start_wizard(session):
282     """Run the image creation wizard"""
283
284     image = session['image']
285     distro = image.distro
286     ostype = image.ostype
287
288     # Create Cloud Wizard Page
289     def cloud_choices():
290         choices = []
291         for (name, cloud) in Kamaki.get_clouds().items():
292             descr = cloud['description'] if 'description' in cloud else ''
293             choices.append((name, descr))
294
295         return choices
296
297     def cloud_add():
298         return add_cloud(session)
299
300     def cloud_none_available():
301         if not session['dialog'].yesno(
302                 "No available clouds found. Would you like to add one now?",
303                 width=PAGE_WIDTH, defaultno=0):
304             return add_cloud(session)
305         return False
306
307     def cloud_validate(cloud):
308         if not Kamaki.get_account(cloud):
309             if not session['dialog'].yesno(
310                     "The cloud you have selected is not valid! Would you "
311                     "like to edit it now?", width=PAGE_WIDTH, defaultno=0):
312                 if edit_cloud(session, cloud):
313                     return cloud
314
315             raise WizardReloadPage
316
317         return cloud
318
319     cloud = WizardMenuPage(
320         "Cloud", "Cloud",
321         "Please select a cloud account or press <Add> to add a new one:",
322         choices=cloud_choices, extra_label="Add", extra=cloud_add,
323         title="Clouds", validate=cloud_validate, fallback=cloud_none_available)
324
325     # Create Image Name Wizard Page
326     name = WizardInputPage(
327         "ImageName", "Image Name", "Please provide a name for the image:",
328         title="Image Name", default=ostype if distro == "unknown" else distro)
329
330     # Create Image Description Wizard Page
331     descr = WizardInputPage(
332         "ImageDescription", "Image Description",
333         "Please provide a description for the image:",
334         title="Image Description", default=session['metadata']['DESCRIPTION']
335         if 'DESCRIPTION' in session['metadata'] else '')
336
337     # Create Sysprep Params Wizard Page
338     needed = image.os.needed_sysprep_params
339     param_names = needed.keys()
340
341     def sysprep_params_fields():
342         fields = []
343         available = image.os.sysprep_params
344         for name in param_names:
345             text = needed[name].description
346             default = available[name] if name in available else ""
347             fields.append(("%s: " % text, default, SYSPREP_PARAM_MAXLEN))
348         return fields
349
350     def sysprep_params_validate(answer):
351         params = {}
352         for i in range(len(answer)):
353             if needed[param_names[i]].validator(answer):
354                 params[param_names[i]] = answer[i]
355             else:
356                 session['dialog'].msgbox("Invalid value for parameter `%s'" %
357                                          param_names[i])
358                 raise WizardReloadPage
359         return params
360
361     def sysprep_params_display(params):
362         return ",".join(["%s=%s" % (key, val) for key, val in params.items()])
363
364     sysprep_params = WizardFormPage(
365         "SysprepParams", "Sysprep Parameters",
366         "Prease fill in the following system preparation parameters:",
367         title="System Preparation Parameters", fields=sysprep_params_fields,
368         display=sysprep_params_display, validate=sysprep_params_validate
369     ) if len(needed) != 0 else None
370
371     # Create Image Registration Wizard Page
372     def registration_choices():
373         return [("Private", "Image is accessible only by this user"),
374                 ("Public", "Everyone can create VMs from this image")]
375
376     registration = WizardRadioListPage(
377         "ImageRegistration", "Registration Type",
378         "Please provide a registration type:", registration_choices,
379         title="Registration Type", default="Private")
380
381     w = Wizard(session)
382
383     w.add_page(cloud)
384     w.add_page(name)
385     w.add_page(descr)
386     if sysprep_params is not None:
387         w.add_page(sysprep_params)
388     w.add_page(registration)
389
390     if w.run():
391         create_image(session)
392     else:
393         return False
394
395     return True
396
397
398 def create_image(session):
399     """Create an image using the information collected by the wizard"""
400     d = session['dialog']
401     image = session['image']
402     wizard = session['wizard']
403
404     with_progress = OutputWthProgress(True)
405     out = image.out
406     out.add(with_progress)
407     try:
408         out.clear()
409
410         #Sysprep
411         image.os.sysprep_params.update(wizard['SysprepParams'])
412         image.os.do_sysprep()
413         metadata = image.os.meta
414
415         #Shrink
416         size = image.shrink()
417         session['shrinked'] = True
418         update_background_title(session)
419
420         metadata.update(image.meta)
421         metadata['DESCRIPTION'] = wizard['ImageDescription']
422
423         #MD5
424         md5 = MD5(out)
425         session['checksum'] = md5.compute(image.device, size)
426
427         out.output()
428         try:
429             out.output("Uploading image to the cloud:")
430             account = Kamaki.get_account(wizard['Cloud'])
431             assert account, "Cloud: %s is not valid" % wizard['Cloud']
432             kamaki = Kamaki(account, out)
433
434             name = "%s-%s.diskdump" % (wizard['ImageName'],
435                                        time.strftime("%Y%m%d%H%M"))
436             pithos_file = ""
437             with open(image.device, 'rb') as f:
438                 pithos_file = kamaki.upload(f, size, name,
439                                             "(1/3)  Calculating block hashes",
440                                             "(2/3)  Uploading missing blocks")
441
442             out.output("(3/3)  Uploading md5sum file ...", False)
443             md5sumstr = '%s %s\n' % (session['checksum'], name)
444             kamaki.upload(StringIO.StringIO(md5sumstr), size=len(md5sumstr),
445                           remote_path="%s.%s" % (name, 'md5sum'))
446             out.success('done')
447             out.output()
448
449             is_public = True if wizard['ImageRegistration'] == "Public" else \
450                 False
451             out.output('Registering %s image with the cloud ...' %
452                        wizard['ImageRegistration'].lower(), False)
453             result = kamaki.register(wizard['ImageName'], pithos_file,
454                                      metadata, is_public)
455             out.success('done')
456             out.output("Uploading metadata file ...", False)
457             metastring = unicode(json.dumps(result, ensure_ascii=False))
458             kamaki.upload(StringIO.StringIO(metastring), size=len(metastring),
459                           remote_path="%s.%s" % (name, 'meta'))
460             out.success('done')
461
462             if is_public:
463                 out.output("Sharing md5sum file ...", False)
464                 kamaki.share("%s.md5sum" % name)
465                 out.success('done')
466                 out.output("Sharing metadata file ...", False)
467                 kamaki.share("%s.meta" % name)
468                 out.success('done')
469
470             out.output()
471
472         except ClientError as e:
473             raise FatalError("Storage service client: %d %s" %
474                              (e.status, e.message))
475     finally:
476         out.remove(with_progress)
477
478     text = "The %s image was successfully uploaded to the storage service " \
479            "and registered with the compute service of %s. Would you like " \
480            "to keep a local copy?" % \
481            (wizard['Cloud'], wizard['ImageRegistration'].lower())
482
483     if not d.yesno(text, width=PAGE_WIDTH):
484         extract_image(session)
485
486 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :