def show(self, dialog, title): """Display this wizard page""" field_lenght = len(self.fields()) form_height = field_lenght if field_lenght < PAGE_HEIGHT - 4 \ else PAGE_HEIGHT - 4 (code, output) = dialog.form(self.text(), create_form_elements(self.fields(), self.dargs['width']), form_height=form_height, title=title, extra_label=self.extra_label(), default_item=self.default, **self.dargs) if code in (dialog.CANCEL, dialog.ESC): return self.PREV self.answer = self.validate(output) self.default = output return self.NEXT
def register_image(session): """Register image with the compute service""" d = session["dialog"] image = session['image'] assert 'clouds' in session try: cloud_name = session['current_cloud'] cloud = session['clouds'][cloud_name] account = cloud['account'] except KeyError: cloud = None if cloud is None or account is None: d.msgbox("You need to select a valid cloud before you can register an " "images with it", width=SMALL_WIDTH) return False if "uploaded" not in cloud: d.msgbox("You need to upload the image to a cloud before you can " "register it", width=SMALL_WIDTH) return False is_public = False _, _, _, container, remote = cloud['uploaded'].split('/') name = "" if 'registered' not in cloud else cloud['registered'] descr = image.meta['DESCRIPTION'] if 'DESCRIPTION' in image.meta else "" while 1: fields = [("Registration name:", name, 60), ("Description (optional):", descr, 80)] (code, output) = d.form( "Please provide the following registration info:", create_form_elements(fields), height=11, width=WIDTH, form_height=2) if code in (d.CANCEL, d.ESC): return False name, descr = output name = name.strip() descr = descr.strip() if len(name) == 0: d.msgbox("Registration name cannot be empty", width=SMALL_WIDTH) continue answer = d.yesno("Make the image public?\\nA public image is " "accessible by every user of the service.", defaultno=1, width=WIDTH) if answer == d.ESC: continue is_public = (answer == d.OK) break image.meta['DESCRIPTION'] = descr metadata = {} metadata.update(image.meta) if 'task_metadata' in session: for key in session['task_metadata']: metadata[key] = 'yes' img_type = "public" if is_public else "private" gauge = GaugeOutput(d, "Image Registration", "Registering image ...") try: out = session['image'].out out.append(gauge) try: try: out.info("Registering %s image with the cloud ..." % img_type, False) kamaki = Kamaki(cloud['account'], out) cloud['registered'] = kamaki.register( name, cloud['uploaded'], metadata, is_public) out.success('done') # Upload metadata file out.info("Uploading metadata file ...", False) metastring = json.dumps(cloud['registered'], indent=4, ensure_ascii=False) kamaki.upload(StringIO.StringIO(metastring), size=len(metastring), remote_path="%s.meta" % remote, container=container, content_type="application/json") out.success("done") if is_public: out.info("Sharing metadata and md5sum files ...", False) kamaki.share("%s.meta" % remote) kamaki.share("%s.md5sum" % remote) out.success('done') except ClientError as error: d.msgbox("Error in storage service client: %s" % error.message) return False finally: out.remove(gauge) finally: gauge.cleanup() d.msgbox("%s image `%s' was successfully registered with the cloud as `%s'" % (img_type.title(), remote, name), width=SMALL_WIDTH) return True
def upload_image(session): """Upload the image to the storage service""" d = session["dialog"] image = session['image'] assert 'clouds' in session try: cloud_name = session['current_cloud'] cloud = session['clouds'][cloud_name] account = cloud['account'] except KeyError: cloud = None if cloud is None or not account: d.msgbox("You need to select a valid cloud before you can upload " "images to it", width=SMALL_WIDTH) return False while 1: if 'uploaded' in cloud: _, _, _, container, name = cloud['uploaded'].split('/') elif 'OS' in session['image'].meta: name = "%s.diskdump" % session['image'].meta['OS'] container = CONTAINER else: name = "" container = CONTAINER fields = [("Remote Name:", name, 60), ("Container:", container, 60)] (code, output) = d.form("Please provide the following upload info:", create_form_elements(fields), height=11, width=WIDTH, form_height=2) if code in (d.CANCEL, d.ESC): return False name, container = output name = name.strip() container = container.strip() if len(name) == 0: d.msgbox("Remote Name cannot be empty", width=SMALL_WIDTH) continue if len(container) == 0: d.msgbox("Container cannot be empty", width=SMALL_WIDTH) continue kamaki = Kamaki(cloud['account'], None) overwrite = [] for f in (name, "%s.md5sum" % name, "%s.meta" % name): if kamaki.object_exists(container, f): overwrite.append(f) if len(overwrite) > 0: if d.yesno("The following storage service object(s) already " "exist(s):\n%s\nDo you want to overwrite them?" % "\n".join(overwrite), width=WIDTH, defaultno=1 ) != d.OK: continue break gauge = GaugeOutput(d, "Image Upload", "Uploading ...") try: out = image.out out.append(gauge) kamaki.out = out try: if 'checksum' not in session: session['checksum'] = image.md5() try: # Upload image file with image.raw_device() as raw: with open(raw, 'rb') as f: cloud["uploaded"] = \ kamaki.upload(f, image.size, name, container, None, "Calculating block hashes", "Uploading missing blocks") # Upload md5sum file out.info("Uploading md5sum file ...") md5str = "%s %s\n" % (session['checksum'], name) kamaki.upload(StringIO.StringIO(md5str), size=len(md5str), remote_path="%s.md5sum" % name, container=container, content_type="text/plain") out.success("done") except ClientError as e: d.msgbox( "Error in storage service client: %s" % e.message, title="Storage Service Client Error", width=SMALL_WIDTH) if 'uploaded' in cloud: del cloud['uploaded'] return False finally: out.remove(gauge) finally: gauge.cleanup() d.msgbox("Image file `%s' was successfully uploaded" % name, width=SMALL_WIDTH) return True
def upload_image(session): """Upload the image to the storage service""" d = session["dialog"] image = session['image'] assert 'clouds' in session try: cloud_name = session['current_cloud'] cloud = session['clouds'][cloud_name] account = cloud['account'] except KeyError: cloud = None if cloud is None or not account: d.msgbox( "You need to select a valid cloud before you can upload " "images to it", width=SMALL_WIDTH) return False while 1: if 'uploaded' in cloud: _, _, _, container, name = cloud['uploaded'].split('/') elif 'OS' in session['image'].meta: name = "%s.diskdump" % session['image'].meta['OS'] container = CONTAINER else: name = "" container = CONTAINER fields = [("Remote Name:", name, 60), ("Container:", container, 60)] (code, output) = d.form("Please provide the following upload info:", create_form_elements(fields), height=11, width=WIDTH, form_height=2) if code in (d.CANCEL, d.ESC): return False name, container = output name = name.strip() container = container.strip() if len(name) == 0: d.msgbox("Remote Name cannot be empty", width=SMALL_WIDTH) continue if len(container) == 0: d.msgbox("Container cannot be empty", width=SMALL_WIDTH) continue kamaki = Kamaki(cloud['account'], None) overwrite = [] for f in (name, "%s.md5sum" % name, "%s.meta" % name): if kamaki.object_exists(container, f): overwrite.append(f) if len(overwrite) > 0: if d.yesno("The following storage service object(s) already " "exist(s):\n%s\nDo you want to overwrite them?" % "\n".join(overwrite), width=WIDTH, defaultno=1) != d.OK: continue break gauge = GaugeOutput(d, "Image Upload", "Uploading ...") try: out = image.out out.append(gauge) kamaki.out = out try: if 'checksum' not in session: session['checksum'] = image.md5() try: # Upload image file with image.raw_device() as raw: with open(raw, 'rb') as f: cloud["uploaded"] = \ kamaki.upload(f, image.size, name, container, None, "Calculating block hashes", "Uploading missing blocks") # Upload md5sum file out.info("Uploading md5sum file ...") md5str = "%s %s\n" % (session['checksum'], name) kamaki.upload(StringIO.StringIO(md5str), size=len(md5str), remote_path="%s.md5sum" % name, container=container, content_type="text/plain") out.success("done") except ClientError as e: d.msgbox("Error in storage service client: %s" % e.message, title="Storage Service Client Error", width=SMALL_WIDTH) if 'uploaded' in cloud: del cloud['uploaded'] return False finally: out.remove(gauge) finally: gauge.cleanup() d.msgbox("Image file `%s' was successfully uploaded" % name, width=SMALL_WIDTH) return True
def register_image(session): """Register image with the compute service""" d = session["dialog"] image = session['image'] assert 'clouds' in session try: cloud_name = session['current_cloud'] cloud = session['clouds'][cloud_name] account = cloud['account'] except KeyError: cloud = None if cloud is None or account is None: d.msgbox( "You need to select a valid cloud before you can register an " "images with it", width=SMALL_WIDTH) return False if "uploaded" not in cloud: d.msgbox( "You need to upload the image to a cloud before you can " "register it", width=SMALL_WIDTH) return False is_public = False _, _, _, container, remote = cloud['uploaded'].split('/') name = "" if 'registered' not in cloud else cloud['registered'] descr = image.meta['DESCRIPTION'] if 'DESCRIPTION' in image.meta else "" while 1: fields = [("Registration name:", name, 60), ("Description (optional):", descr, 80)] (code, output) = d.form("Please provide the following registration info:", create_form_elements(fields), height=11, width=WIDTH, form_height=2) if code in (d.CANCEL, d.ESC): return False name, descr = output name = name.strip() descr = descr.strip() if len(name) == 0: d.msgbox("Registration name cannot be empty", width=SMALL_WIDTH) continue answer = d.yesno( "Make the image public?\\nA public image is " "accessible by every user of the service.", defaultno=1, width=WIDTH) if answer == d.ESC: continue is_public = (answer == d.OK) break image.meta['DESCRIPTION'] = descr metadata = {} metadata.update(image.meta) if 'task_metadata' in session: for key in session['task_metadata']: metadata[key] = 'yes' img_type = "public" if is_public else "private" gauge = GaugeOutput(d, "Image Registration", "Registering image ...") try: out = session['image'].out out.append(gauge) try: try: out.info("Registering %s image with the cloud ..." % img_type, False) kamaki = Kamaki(cloud['account'], out) cloud['registered'] = kamaki.register(name, cloud['uploaded'], metadata, is_public) out.success('done') # Upload metadata file out.info("Uploading metadata file ...", False) metastring = json.dumps(cloud['registered'], indent=4, ensure_ascii=False) kamaki.upload(StringIO.StringIO(metastring), size=len(metastring), remote_path="%s.meta" % remote, container=container, content_type="application/json") out.success("done") if is_public: out.info("Sharing metadata and md5sum files ...", False) kamaki.share("%s.meta" % remote) kamaki.share("%s.md5sum" % remote) out.success('done') except ClientError as error: d.msgbox("Error in storage service client: %s" % error.message) return False finally: out.remove(gauge) finally: gauge.cleanup() d.msgbox( "%s image `%s' was successfully registered with the cloud as `%s'" % (img_type.title(), remote, name), width=SMALL_WIDTH) return True