Esempio n. 1
0
    def download_image_files(self, image_id, destdir):
        """
        Downloads the manifest.xml and all AMI parts for image_id to destdir
        """
        if not os.path.isdir(destdir):
            raise exception.BaseException(
                "destination directory '%s' does not exist" % destdir)
        widgets = [
            'file: ',
            progressbar.Percentage(), ' ',
            progressbar.Bar(marker=progressbar.RotatingMarker()), ' ',
            progressbar.ETA(), ' ',
            progressbar.FileTransferSpeed()
        ]
        files = self.get_image_files(image_id)

        def _dl_progress_cb(trans, total):
            pbar.update(trans)

        log.info("Downloading image: %s" % image_id)
        for file in files:
            widgets[0] = "%s:" % file.name
            pbar = progressbar.ProgressBar(widgets=widgets,
                                           maxval=file.size).start()
            file.get_contents_to_filename(os.path.join(destdir, file.name),
                                          cb=_dl_progress_cb)
            pbar.finish()
Esempio n. 2
0
 def progress_bar(self):
     if not self._progress_bar:
         widgets = ['FileTransfer: ', ' ', progressbar.Percentage(), ' ',
                    progressbar.Bar(marker=progressbar.RotatingMarker()),
                    ' ', progressbar.ETA(), ' ',
                    progressbar.FileTransferSpeed()]
         pbar = progressbar.ProgressBar(widgets=widgets,
                                        maxval=1,
                                        force_update=True)
         self._progress_bar = pbar
     return self._progress_bar
Esempio n. 3
0
 def wait_for_snapshot(self, snapshot, refresh_interval=30):
     snap = snapshot
     log.info("Waiting for snapshot to complete: %s" % snap.id)
     widgets = ['%s: ' % snap.id, '',
                progressbar.Bar(marker=progressbar.RotatingMarker()),
                '', progressbar.Percentage(), ' ', progressbar.ETA()]
     pbar = progressbar.ProgressBar(widgets=widgets, maxval=100).start()
     while snap.status != 'completed':
         try:
             progress = int(snap.update().replace('%', ''))
             pbar.update(progress)
         except ValueError:
             time.sleep(5)
             continue
         time.sleep(refresh_interval)
     pbar.finish()
Esempio n. 4
0
 def migrate_image(self, image_id, destbucket, migrate_manifest=False,
                   kernel_id=None, ramdisk_id=None, region=None, cert=None,
                   private_key=None):
     """
     Migrate image_id files to destbucket
     """
     if migrate_manifest:
         utils.check_required(['ec2-migrate-manifest'])
         if not cert:
             raise exception.BaseException("no cert specified")
         if not private_key:
             raise exception.BaseException("no private_key specified")
         if not kernel_id:
             raise exception.BaseException("no kernel_id specified")
         if not ramdisk_id:
             raise exception.BaseException("no ramdisk_id specified")
     image = self.get_image(image_id)
     if image.root_device_type == "ebs":
         raise exception.AWSError(
             "The image you wish to migrate is EBS-based. " +
             "This method only works for instance-store images")
     ibucket = self.get_image_bucket(image)
     files = self._get_image_files(image, ibucket)
     if not files:
         log.info("No files found for image: %s" % image_id)
         return
     log.info("Migrating image: %s" % image_id)
     widgets = [files[0].name, progressbar.Percentage(), ' ',
                progressbar.Bar(marker=progressbar.RotatingMarker()), ' ',
                progressbar.ETA(), ' ', ' ']
     counter = 0
     num_files = len(files)
     pbar = progressbar.ProgressBar(widgets=widgets,
                                    maxval=num_files).start()
     for f in files:
         widgets[0] = "%s: (%s/%s)" % (f.name, counter + 1, num_files)
         # copy file to destination bucket with the same name
         f.copy(destbucket, f.name)
         pbar.update(counter)
         counter += 1
     pbar.finish()
     if migrate_manifest:
         dbucket = self.s3.get_bucket(destbucket)
         manifest_key = dbucket.get_key(self.get_image_manifest(image))
         f = tempfile.NamedTemporaryFile()
         manifest_key.get_contents_to_file(f.file)
         f.file.close()
         cmd = ('ec2-migrate-manifest -c %s -k %s -m %s --kernel %s ' +
                '--ramdisk %s --no-mapping ') % (cert, private_key,
                                                 f.name, kernel_id,
                                                 ramdisk_id)
         register_cmd = "ec2-register %s/%s" % (destbucket,
                                                manifest_key.name)
         if region:
             cmd += '--region %s' % region
             register_cmd += " --region %s" % region
         log.info("Migrating manifest file...")
         retval = os.system(cmd)
         if retval != 0:
             raise exception.BaseException(
                 "ec2-migrate-manifest failed with status %s" % retval)
         f.file = open(f.name, 'r')
         manifest_key.set_contents_from_file(f.file)
         # needed so that EC2 has permission to READ the manifest file
         manifest_key.add_email_grant('READ', '*****@*****.**')
         f.close()
         os.unlink(f.name + '.bak')
         log.info("Manifest migrated successfully. You can now run:\n" +
                  register_cmd + "\nto register your migrated image.")