def _attach_volumes_task(self): attach_tasks = (volume.VolumeAttachTask(self.stack, self.resource_id, volume_id, device) for volume_id, device in self.volumes()) return scheduler.PollingTaskGroup(attach_tasks)
def handle_create(self): security_groups = self._get_security_groups() userdata = self.properties['UserData'] or '' flavor = self.properties['InstanceType'] key_name = self.properties['KeyName'] availability_zone = self.properties['AvailabilityZone'] keypairs = [k.name for k in self.nova().keypairs.list()] if key_name not in keypairs and key_name is not None: raise exception.UserKeyPairMissing(key_name=key_name) image_name = self.properties['ImageId'] image_id = None image_list = self.nova().images.list() for o in image_list: if o.name == image_name: image_id = o.id break if image_id is None: logger.info("Image %s was not found in glance" % image_name) raise exception.ImageNotFound(image_name=image_name) flavor_id = None flavor_list = self.nova().flavors.list() for o in flavor_list: if o.name == flavor: flavor_id = o.id break if flavor_id is None: raise exception.FlavorMissing(flavor_id=flavor) tags = {} if self.properties['Tags']: for tm in self.properties['Tags']: tags[tm['Key']] = tm['Value'] else: tags = None scheduler_hints = {} if self.properties['NovaSchedulerHints']: for tm in self.properties['NovaSchedulerHints']: scheduler_hints[tm['Key']] = tm['Value'] else: scheduler_hints = None nics = self._build_nics(self.properties['NetworkInterfaces'], subnet_id=self.properties['SubnetId']) server_userdata = self._build_userdata(userdata) server = None try: server = self.nova().servers.create( name=self.physical_resource_name(), image=image_id, flavor=flavor_id, key_name=key_name, security_groups=security_groups, userdata=server_userdata, meta=tags, scheduler_hints=scheduler_hints, nics=nics, availability_zone=availability_zone) finally: # Avoid a race condition where the thread could be cancelled # before the ID is stored if server is not None: self.resource_id_set(server.id) attach_tasks = (volume.VolumeAttachTask(self.stack, self.resource_id, volume_id, device) for volume_id, device in self.volumes()) attach_volumes_task = scheduler.PollingTaskGroup(attach_tasks) return server, scheduler.TaskRunner(attach_volumes_task)