Example #1
0
    def create(self):
        if self.state != None:
            return
        self.state_set(self.CREATE_IN_PROGRESS)
        Resource.create(self)

        self.instance_id = "%s/stacks/%s/resources/%s" % (self.stack.metadata_server, self.stack.name, self.name)

        self.state_set(self.CREATE_COMPLETE)
Example #2
0
    def create(self):
        if self.state != None:
            return
        self.state_set(self.CREATE_IN_PROGRESS)
        Resource.create(self)

        self._get_handle_resource_id()

        # keep polling our Metadata to see if the cfn-signal has written
        # it yet. The execution here is limited by timeout.
        print "timeout %d" % self.timeout
        tmo = eventlet.Timeout(self.timeout)
        status = "WAITING"
        reason = ""
        try:
            while status == "WAITING":
                pt = None
                if self.stack.parsed_template_id:
                    try:
                        pt = db_api.parsed_template_get(None, self.stack.parsed_template_id)
                    except Exception as ex:
                        if "not found" in ex:
                            # entry deleted
                            return
                        else:
                            pass

                if pt:
                    res = pt.template["Resources"][self.resource_id]
                    metadata = res.get("Metadata", {})
                    status = metadata.get("Status", "WAITING")
                    reason = metadata.get("Reason", "Reason not provided")
                    logger.debug("got %s" % json.dumps(metadata))
                if status == "WAITING":
                    logger.debug("Waiting some more for the Metadata[Status]")
                    eventlet.sleep(30)
        except eventlet.Timeout, t:
            if t is not tmo:
                # not my timeout
                raise
            else:
                status = "TIMEDOUT"
                reason = "Timed out waiting for instance"
Example #3
0
    def create(self):
        if self.state != None:
            return
        self.state_set(self.CREATE_IN_PROGRESS)
        Resource.create(self)
        sec = None

        groups = self.nova().security_groups.list()
        for group in groups:
            if group.name == self.name:
                sec = group
                break

        if not sec:
            sec = self.nova().security_groups.create(self.name,
                                                     self.description)

        self.instance_id_set(sec.id)

        if 'SecurityGroupIngress' in self.t['Properties']:
            rules_client = self.nova().security_group_rules
            for i in self.t['Properties']['SecurityGroupIngress']:
                try:
                    rule = rules_client.create(sec.id,
                                               i['IpProtocol'],
                                               i['FromPort'],
                                               i['ToPort'],
                                               i['CidrIp'])
                except BadRequest as ex:
                    if ex.message.find('already exists') >= 0:
                        # no worries, the rule is already there
                        pass
                    else:
                        # unexpected error
                        raise

        self.state_set(self.CREATE_COMPLETE)
Example #4
0
    def create(self):
        def _null_callback(p, n, out):
            """
            Method to silence the default M2Crypto.RSA.gen_key output.
            """
            pass

        if self.state != None:
            return
        self.state_set(self.CREATE_IN_PROGRESS)
        Resource.create(self)
        props = self.t["Properties"]
        if not "KeyName" in props:
            raise exception.UserParameterMissing(key="KeyName")
        if not "InstanceType" in props:
            raise exception.UserParameterMissing(key="InstanceType")
        if not "ImageId" in props:
            raise exception.UserParameterMissing(key="ImageId")

        security_groups = props.get("SecurityGroups")

        userdata = self.t["Properties"]["UserData"]

        flavor = self.itype_oflavor[self.t["Properties"]["InstanceType"]]
        key_name = self.t["Properties"]["KeyName"]

        keypairs = self.nova().keypairs.list()
        key_exists = False
        for k in keypairs:
            if k.name == key_name:
                # cool it exists
                key_exists = True
                break
        if not key_exists:
            raise exception.UserKeyPairMissing(key_name=key_name)

        image_name = self.t["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

        if image_id is None:
            logger.info("Image %s was not found in glance" % image_name)
            raise exception.ImageNotFound(image_name=image_name)

        flavor_list = self.nova().flavors.list()
        for o in flavor_list:
            if o.name == flavor:
                flavor_id = o.id

        server_userdata = self._build_userdata(userdata)
        server = self.nova().servers.create(
            name=self.name,
            image=image_id,
            flavor=flavor_id,
            key_name=key_name,
            security_groups=security_groups,
            userdata=server_userdata,
        )
        while server.status == "BUILD":
            server.get()
            eventlet.sleep(1)
        if server.status == "ACTIVE":
            self.instance_id_set(server.id)
            self.state_set(self.CREATE_COMPLETE)
            # just record the first ipaddress
            for n in server.networks:
                self.ipaddress = server.networks[n][0]
                break
        else:
            raise exception.Error(server.status)