def _create_image_from_ebs(self, size=15): log.info("Creating new EBS AMI...") imgid = self.ec2.create_image(self.host.id, self.name, self.description) img = self.ec2.get_image(imgid) log.info("New EBS AMI created: %s" % imgid) root_dev = self.host.root_device_name if root_dev in self.host.block_device_mapping: log.info("Fetching block device mapping for %s" % imgid, extra=dict(__nonewline__=True)) s = Spinner() try: s.start() while root_dev not in img.block_device_mapping: img = self.ec2.get_image(imgid) time.sleep(5) finally: s.stop() snapshot_id = img.block_device_mapping[root_dev].snapshot_id snap = self.ec2.get_snapshot(snapshot_id) self.ec2.wait_for_snapshot(snap) else: log.warn("Unable to find root device - cant wait for snapshot") log.info("Waiting for %s to become available..." % imgid, extra=dict(__nonewline__=True)) s = Spinner() try: s.start() while img.state == "pending": time.sleep(15) if img.update() == "failed": raise exception.AWSError( "EBS image creation failed for %s" % imgid) finally: s.stop() return imgid
def _create_image_from_ebs(self, size=15): log.info("Creating EBS image...") imgid = self.ec2.create_image(self.host.id, self.name, self.description) log.info("Waiting for AMI %s to become available..." % imgid, extra=dict(__nonewline__=True)) img = self.ec2.get_image(imgid) s = Spinner() s.start() while img.state == "pending": time.sleep(15) if img.update() == "failed": raise exception.AWSError( "EBS image creation failed for AMI %s" % imgid) s.stop() return imgid
def create_image(self, size=15): host = self.host host_ssh = self.host_ssh self.clean_private_data() if self.host.root_device_type == "ebs": log.info("Creating EBS image...") imgid = self.ec2.create_image(host.id, self.name, self.description) s = Spinner() log.log(INFO_NO_NEWLINE, "Waiting for AMI %s to become available..." % imgid) s.start() img = self.ec2.get_image(imgid) while img.update() == "pending": time.sleep(15) s.stop() if img.update() == "failed": raise exception.AWSError( "EBS image creation failed for AMI %s" % imgid) return imgid log.info("Creating new EBS-backed image from instance-store instance") log.info("Creating new root volume...") vol = self.ec2.create_volume(size, host.placement) log.info("Created new volume: %s" % vol.id) while vol.update() != 'available': time.sleep(5) dev = None for i in string.ascii_lowercase[::-1]: dev = '/dev/sd%s' % i if not dev in host.block_device_mapping: break log.info("Attaching volume %s to instance %s on %s" % (vol.id, host.id, dev)) vol.attach(host.id, dev) while vol.update() != 'in-use': time.sleep(5) while not host_ssh.path_exists(dev): time.sleep(5) host_ssh.execute('mkfs.ext3 -F %s' % dev) mount_point = '/ebs' while host_ssh.path_exists(mount_point): mount_point += '1' host_ssh.mkdir(mount_point) log.info("Mounting %s on %s" % (dev, mount_point)) host_ssh.execute('mount %s %s' % (dev, mount_point)) log.info("Configuring /etc/fstab") host_ssh.remove_lines_from_file('/etc/fstab', '/mnt') fstab = host_ssh.remote_file('/etc/fstab', 'a') fstab.write('/dev/sdb1 /mnt auto defaults,nobootwait 0 0\n') fstab.close() log.info("Syncing root filesystem to new volume (%s)" % vol.id) host_ssh.execute( 'rsync -avx --exclude %(mpt)s --exclude /root/.ssh / %(mpt)s' % \ {'mpt': mount_point}) log.info("Unmounting %s from %s" % (dev, mount_point)) host_ssh.execute('umount %s' % mount_point) log.info("Detaching volume %s from %s" % (dev, mount_point)) vol.detach() while vol.update() != 'available': time.sleep(5) snap = self.ec2.create_snapshot(vol, description=self.snapshot_description, wait_for_snapshot=True) log.info("New snapshot created: %s" % snap.id) bmap = self.ec2.create_root_block_device_map(snap.id, add_ephemeral_drives=True) log.info("Registering new image...") img_id = self.ec2.register_image(name=self.name, description=self.description, architecture=host.architecture, kernel_id=self.kernel_id, ramdisk_id=self.ramdisk_id, root_device_name='/dev/sda1', block_device_map=bmap) return img_id