def test_run(self): # build ami AAA via ami_packer result_queue = Queue.Queue() config = ConfigParser.SafeConfigParser() config.read(os.path.join(os.getcwd(), 'config', 'config.ini')) try: packer_path = config.get('Packer_Config', 'path') packer_bin = os.path.join(packer_path, 'packer') except: packer_bin = 'packer' timestamp = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d') amipacker = AMIPacker(os.path.join(os.getcwd(), 'tests', 'template', 'test_template.json'), 'ubuntu/images/ebs/ubuntu-trusty-14.04-amd64-server-20140416.1', '123456', timestamp, self.region, result_queue, self.aws_key.get_aws_access_key(), self.aws_key.get_aws_secret_key(), packer_bin) self._clean_previous_ami('test_omelet_ami') amipacker.run() if not result_queue.empty(): (region, result, message, existence) = result_queue.get() print message self.ami = message # launching instance B for check ami = AMI(self.region, self.aws_key.get_aws_access_key(), self.aws_key.get_aws_secret_key()) assert ami.check_image(message, 'available') self.instance_id = self.helper.launch_instance(message, self.name, self.name) # check modules are in instance B std_out = self.helper.run_command(self.instance_id, 'dpkg -l | grep newrelic', self.name, 'ubuntu') print std_out assert 'newrelic-sysmond' in std_out
def tearDown(self): # drop everything if self.instance_id: self.helper.terminate_instance(self.instance_id, 180) self.helper.delete_key_pair(self.name) self.helper.delete_sg(self.name) if self.ami: ami = AMI(self.region, self.aws_key.get_aws_access_key(), self.aws_key.get_aws_secret_key()) ami.deregister_ami(self.ami)
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')
def test_get_amzn_ami_id(self): self.aws_key = Credential() #init ami obj ami = AMI('us-east-1', self.aws_key.aws_access_key_id, self.aws_key.aws_secret_access_key) #mock asserts #self.assertEqual("ami-fb8e9292", ami.get_amzn_ami_id("amzn-ami-pv-2014.03.1.x86_64-ebs")) self.assertEqual(None, ami.get_amzn_ami_id("no way to have this kind of id"))
def get_ami_id_by_image(logger, region, base_image_name): db = _get_db_accessor() logger.info("get image latest ver") latest_ver = db.get_version(base_image_name) image_name = "%s-%s" % (base_image_name, latest_ver) logger.debug("determine latest image name : %s" % image_name) #query EC2 to get ami-id logger.info("query to EC2 to get ami-id for %s" % image_name) aws_key = Credential() ami = AMI(region, aws_key.aws_access_key_id, aws_key.aws_secret_access_key) ami_id = ami.get_ami_id(image_name) logger.info("got ami-id : %s" % ami_id) if not ami_id: logger.error("couldn't get ami-id, please check DynamoDB table, give up :-(") raise return ami_id
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))
def _clean_previous_ami(self, name): ami = AMI(self.region, self.aws_key.get_aws_access_key(), self.aws_key.get_aws_secret_key()) image_id = ami.get_ami_id(name) if image_id: ami.deregister_ami(image_id)
def main(region, id): aws_key = Credential() image = AMI(region, aws_key.get_aws_access_key(), aws_key.get_aws_secret_key()) image.get_ami_obj(id)