Esempio n. 1
0
 def test_tag_ami(self):
     conn = boto.connect_ec2()
     ami = AMI(1, 2, 3, conn=conn)
     reservation = conn.run_instances('ami-1234abcd')
     image_id = conn.create_image(reservation.instances[0].id, 'new_ami')
     ami.tag_ami({'test_key': 'test_value'}, image_id)
     target_image = conn.get_image(image_id)
     print target_image.tags
     self.assertEqual(target_image.tags['test_key'], 'test_value')
Esempio n. 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))