def _start(self, create=True): """ Start cluster from this cluster template's settings Handles creating and configuring a cluster Does not attempt to validate before running """ log.info("Starting cluster...") if create: self.create_cluster() s = Spinner() log.log(INFO_NO_NEWLINE, "Waiting for cluster to start...") s.start() while not self.is_cluster_up(): time.sleep(30) s.stop() log.info("The master node is %s" % self.master_node.dns_name) if self.volumes: self.attach_volumes_to_master() log.info("Setting up the cluster...") default_setup = clustersetup.DefaultClusterSetup().run( self.nodes, self.master_node, self.cluster_user, self.cluster_shell, self.volumes ) self.create_receipt() self.run_plugins() log.info( user_msgs.cluster_started_msg % { "master": self.master_node.dns_name, "user": self.cluster_user, "key": self.key_location, "tag": self.cluster_tag, } )
def _request_instance(self, zone): alias = self._alias_tmpl % zone if self._instance: i = self._instance if i.state not in ['pending', 'running']: raise exception.InstanceNotRunning(i.id) if i.placement != zone: raise exception.ValidationError( "specified host instance %s is not in zone %s" % (i.id, zone)) self._instance = Node(i, self._key_location, alias) else: self._instance = self._get_existing_instance(zone) if not self._instance: self._validate_image_and_type(self._image_id, self._instance_type) log.info( "No instance in group %s for zone %s, launching one now." % \ (self.security_group.name, zone)) self._resv = self._ec2.run_instances( image_id=self._image_id, instance_type=self._instance_type, min_count=1, max_count=1, security_groups=[self.security_group.name], key_name=self._keypair, placement=zone, user_data=alias) instance = self._resv.instances[0] self._instance = Node(instance, self._key_location, alias) s = Spinner() log.log(INFO_NO_NEWLINE, "Waiting for instance %s to come up..." % self._instance.id) s.start() while not self._instance.is_up(): time.sleep(15) s.stop() return self._instance
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