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.
40 from image_creator.kamaki_wrapper import Kamaki, ClientError
41 from image_creator.util import MD5, FatalError
42 from image_creator.output.cli import OutputWthProgress
43 from image_creator.dialog_util import extract_image, update_background_title
48 class WizardExit(Exception):
53 def __init__(self, session):
54 self.session = session
56 self.session['wizard'] = {}
58 def add_page(self, page):
59 self.pages.append(page)
65 idx += self.pages[idx].run(self.session, idx, len(self.pages))
69 if idx >= len(self.pages):
81 def run(self, session, index, total):
82 raise NotImplementedError
85 class WizardRadioListPage(WizardPage):
87 def __init__(self, name, message, choices, **kargs):
89 self.message = message
90 self.choices = choices
91 self.title = kargs['title'] if 'title' in kargs else ''
92 self.default = kargs['default'] if 'default' in kargs else 0
94 def run(self, session, index, total):
99 for i in range(len(self.choices)):
100 default = 1 if i == self.default else 0
101 choices.append((self.choices[i][0], self.choices[i][1], default))
105 d.radiolist(self.message, width=PAGE_WIDTH,
106 ok_label="Next", cancel="Back", choices=choices,
107 title="(%d/%d) %s" % (index + 1, total, self.title)
110 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
113 for i in range(len(choices)):
114 if self.choices[i] == answer:
122 class WizardInputPage(WizardPage):
124 def __init__(self, name, message, **kargs):
126 self.message = message
127 self.title = kargs['title'] if 'title' in kargs else ''
128 self.init_value = kargs['init'] if 'init' in kargs else ''
129 self.allow_empty = kargs['empty'] if 'empty' in kargs else False
131 def run(self, session, index, total):
132 d = session['dialog']
133 w = session['wizard']
135 init = w[self.name] if self.name in w else self.init_value
138 d.inputbox(self.message, init=init,
139 width=PAGE_WIDTH, ok_label="Next", cancel="Back",
140 title="(%d/%d) %s" % (index + 1, total, self.title))
142 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
145 value = answer.strip()
146 if len(value) == 0 and self.allow_empty is False:
147 d.msgbox("The value cannot be empty!", width=PAGE_WIDTH)
155 class WizardYesNoPage(WizardPage):
157 def __init__(self, message, **kargs):
158 self.message = message
159 self.title = kargs['title'] if 'title' in kargs else ''
161 def run(self, session, index, total):
162 d = session['dialog']
165 ret = d.yesno(self.message, width=PAGE_WIDTH, ok_label="Yes",
166 cancel="Back", extra_button=1, extra_label="Quit",
167 title="(%d/%d) %s" % (index + 1, total, self.title))
169 if ret == d.DIALOG_CANCEL:
171 elif ret == d.DIALOG_EXTRA:
173 elif ret == d.DIALOG_OK:
179 name = WizardInputPage("ImageName", "Please provide a name for the image:",
180 title="Image Name", init=session['device'].distro)
181 descr = WizardInputPage("ImageDescription",
182 "Please provide a description for the image:",
183 title="Image Description", empty=True,
184 init=session['metadata']['DESCRIPTION'] if
185 'DESCRIPTION' in session['metadata'] else '')
186 account = WizardInputPage("account",
187 "Please provide your ~okeanos account e-mail:",
188 title="~okeanos account information",
189 init=Kamaki.get_account())
190 token = WizardInputPage("token",
191 "Please provide your ~okeanos account token:",
192 title="~okeanos account token",
193 init=Kamaki.get_token())
195 msg = "All necessary information has been gathered. Confirm and Proceed."
196 proceed = WizardYesNoPage(msg, title="Confirmation")
207 create_image(session)
214 def create_image(session):
215 d = session['dialog']
216 disk = session['disk']
217 device = session['device']
218 snapshot = session['snapshot']
219 image_os = session['image_os']
220 wizard = session['wizard']
222 with_progress = OutputWthProgress(True)
224 out.add(with_progress)
230 image_os.do_sysprep()
231 metadata = image_os.meta
235 size = device.shrink()
236 session['shrinked'] = True
237 update_background_title(session)
239 metadata.update(device.meta)
240 metadata['DESCRIPTION'] = wizard['ImageDescription']
244 session['checksum'] = md5.compute(snapshot, size)
247 metastring = '\n'.join(
248 ['%s=%s' % (key, value) for (key, value) in metadata.items()])
253 out.output("Uploading image to pithos:")
254 kamaki = Kamaki(wizard['account'], wizard['token'], out)
256 name = "%s-%s.diskdump" % (wizard['ImageName'],
257 time.strftime("%Y%m%d%H%M"))
259 with open(snapshot, 'rb') as f:
260 pithos_file = kamaki.upload(f, size, name,
261 "(1/4) Calculating block hashes",
262 "(2/4) Uploading missing blocks")
264 out.output("(3/4) Uploading metadata file...", False)
265 kamaki.upload(StringIO.StringIO(metastring), size=len(metastring),
266 remote_path="%s.%s" % (name, 'meta'))
268 out.output("(4/4) Uploading md5sum file...", False)
269 md5sumstr = '%s %s\n' % (session['checksum'], name)
270 kamaki.upload(StringIO.StringIO(md5sumstr), size=len(md5sumstr),
271 remote_path="%s.%s" % (name, 'md5sum'))
275 out.output('Registering image with ~okeanos...', False)
276 kamaki.register(wizard['ImageName'], pithos_file, metadata)
280 except ClientError as e:
281 raise FatalError("Pithos client: %d %s" % (e.status, e.message))
283 out.remove(with_progress)
285 msg = "The image was successfully uploaded and registered with " \
286 "~okeanos. Would you like to keep a local copy of the image?"
287 if not d.yesno(msg, width=PAGE_WIDTH):
288 extract_image(session)
290 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :