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