def launch_amipacker(self, db, ami_name, template, sha1, committer, committer_mail, comment,
                          notify, causedby=None):
        self.logger.info("launching packer - template: %s, commit id: %s" % (template, sha1))
        user_vars = list()
        template_obj = Template(template)
        self._get_provisioner_variables(template_obj, ami_name, user_vars)
        timestamp = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d')

        # src ami
        src_ami_name = template_obj.get_source_ami_name()
        src_ami_version = self._get_ami_version(db, src_ami_name, False)
        src_ami_name_with_seq = self._get_ami_name_with_seq(src_ami_name,
                                                            None if db.is_root(src_ami_name) else src_ami_version)

        # my ami
        ami_version = self._get_ami_version(db, ami_name, True)
        ami_name_with_seq = self._get_ami_name_with_seq(ami_name, ami_version)

        # run
        output = self._run_amipacker(template, src_ami_name_with_seq, ami_name_with_seq, sha1, timestamp, user_vars)

        # notification
        if notify:
            print output
            self.send_notification(template, self._check_success(output), output, committer, committer_mail,
                                   ami_version, causedby)

        # update management DB
        if self._validate_existence:
            db.add_or_update_image(ami_name, src_ami_name, ami_version, sha1, comment,
                                   timestamp, committer, committer_mail)
            db.add_or_update_tree_node(ami_name, template_obj.get_source_ami_name())
        return self._validate_existence
示例#2
0
    def run(self):
        """
        Launching Packer
        :return: put the result to the queue
        """
        ami_obj = AMI(self.region, self.aws_access_key_id, self.aws_secret_access_key)
        # target ami name
        if self.target_name:
            self.var_list.append('ami_name=%s' % self.target_name)

        # source ami get
        template_obj = Template(self.template)
        if not self.ami:
            self.ami = template_obj.get_source_ami_name()
        image_id = ami_obj.get_ami_id(self.ami) or ami_obj.get_amzn_ami_id(self.ami)
        if not image_id:
            self.queue.put((self.region, False, "Source image not found: %s" % self.ami, False))
            raise ValueError("Source image not found: %s" % self.ami)

        #setup VPC
        vpc = VPC(self.region, self.aws_access_key_id, self.aws_secret_access_key)
        vpc_id = vpc.create_vpc('10.0.0.0/16')
        subnet_id = vpc.create_public_subnet('10.0.1.0/24')


        # do packer stuff
        retry_cnt = 0
        result = False
        while not result and retry_cnt < (OmeletConfigParser()).get_packer_retry_cnt():
            output = self.launch(self.template, self._get_packer_vars(image_id, vpc_id, subnet_id))
            (result, message) = self.check_result(self.region, output)
            retry_cnt += 1
            if not self._retry(message):
                break
            else:
                print "Retry due to: %s" % message

        # tag target ami
        if result:
            for tag in self._get_ami_tags(image_id, template_obj):
                ami_obj.tag_ami(tag, message)

        # clean up VPC
        vpc.delete_vpc()

        # validate
        valid = True if ami_obj.get_ami_id(self.target_name) else False

        # push result
        self.queue.put((self.region, result, message, valid))