1 # -*- coding: utf-8 -*-
3 # Copyright 2012 GRNET S.A. All rights reserved.
5 # Redistribution and use in source and binary forms, with or
6 # without modification, are permitted provided that the following
9 # 1. Redistributions of source code must retain the above
10 # copyright notice, this list of conditions and the following
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.
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.
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.
36 """This module implements the "wizard" mode of the dialog-based version of
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, \
54 class WizardExit(Exception):
55 """Exception used to exit the wizard"""
59 class WizardReloadPage(Exception):
60 """Exception that reloads the last WizardPage"""
65 """Represents a dialog-based wizard
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.
71 def __init__(self, session):
72 self.session = session
74 self.session['wizard'] = {}
75 self.d = session['dialog']
77 def add_page(self, page):
78 """Add a new page to the wizard"""
79 self.pages.append(page)
86 idx += self.pages[idx].run(self.session, idx, len(self.pages))
89 except WizardReloadPage:
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?"
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")
103 if ret == self.d.DIALOG_CANCEL:
105 elif ret == self.d.DIALOG_EXTRA:
107 elif ret == self.d.DIALOG_OK:
114 class WizardPage(object):
115 """Represents a page in a wizard"""
119 def __init__(self, **kargs):
120 validate = kargs['validate'] if 'validate' in kargs else lambda x: x
121 setattr(self, "validate", validate)
123 display = kargs['display'] if 'display' in kargs else lambda x: x
124 setattr(self, "display", display)
126 def run(self, session, index, total):
127 """Display this wizard page
129 This function is used by the wizard program when accessing a page.
131 raise NotImplementedError
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)
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 ""
145 def run(self, session, index, total):
146 d = session['dialog']
147 w = session['wizard']
150 for choice in self.choices():
151 default = 1 if choice[0] == self.default else 0
152 choices.append((choice[0], choice[1], default))
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))
159 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
162 w[self.name] = self.validate(answer)
163 self.default = answer
164 self.info = "%s: %s" % (self.printable, self.display(w[self.name]))
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)
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 ''
180 def run(self, session, index, total):
181 d = session['dialog']
182 w = session['wizard']
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))
189 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
192 value = answer.strip()
194 w[self.name] = self.validate(value)
195 self.info = "%s: %s" % (self.printable, self.display(w[self.name]))
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)
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
213 kargs['extra_label'] if 'extra_label' in kargs else 'Extra'
214 self.fallback = kargs['fallback'] if 'fallback' in kargs else None
216 def run(self, session, index, total):
217 d = session['dialog']
218 w = session['wizard']
220 extra_button = 1 if self.extra else 0
222 choices = self.choices()
224 if len(choices) == 0:
225 assert self.fallback, "Zero choices and no fallback"
227 raise WizardReloadPage
231 default_item = self.default if self.default else choices[0][0]
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)
239 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
241 elif code == d.DIALOG_EXTRA:
243 raise WizardReloadPage
245 self.default = choice
246 w[self.name] = self.validate(choice)
247 self.info = "%s: %s" % (self.printable, self.display(w[self.name]))
252 def start_wizard(session):
253 """Run the image creation wizard"""
255 distro = session['image'].distro
256 ostype = session['image'].ostype
260 for (name, cloud) in Kamaki.get_clouds().items():
261 descr = cloud['description'] if 'description' in cloud else ''
262 choices.append((name, descr))
267 return add_cloud(session)
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)
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):
284 raise WizardInvalidData
288 cloud = WizardMenuPage(
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)
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)
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 '')
304 def registration_choices():
305 return [("Private", "Image is accessible only by this user"),
306 ("Public", "Everyone can create VMs from this image")]
308 registration = WizardRadioListPage(
309 "ImageRegistration", "Registration Type",
310 "Please provide a registration type:", registration_choices,
311 title="Registration Type", default="Private")
318 w.add_page(registration)
321 create_image(session)
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']
334 with_progress = OutputWthProgress(True)
336 out.add(with_progress)
341 image.os.do_sysprep()
342 metadata = image.os.meta
345 size = image.shrink()
346 session['shrinked'] = True
347 update_background_title(session)
349 metadata.update(image.meta)
350 metadata['DESCRIPTION'] = wizard['ImageDescription']
354 session['checksum'] = md5.compute(image.device, size)
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)
363 name = "%s-%s.diskdump" % (wizard['ImageName'],
364 time.strftime("%Y%m%d%H%M"))
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")
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'))
378 is_public = True if wizard['ImageRegistration'] == "Public" else \
380 out.output('Registering %s image with cyclades ...' %
381 wizard['ImageRegistration'].lower(), False)
382 result = kamaki.register(wizard['ImageName'], pithos_file,
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'))
392 out.output("Sharing md5sum file ...", False)
393 kamaki.share("%s.md5sum" % name)
395 out.output("Sharing metadata file ...", False)
396 kamaki.share("%s.meta" % name)
401 except ClientError as e:
402 raise FatalError("Pithos client: %d %s" % (e.status, e.message))
404 out.remove(with_progress)
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)
412 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :