def main(): """ main method. """ args = get_args() ami_config_file = args.var_file ami_config = get_config(ami_config_file) target_ami_name = generate_ami_name(ami_config['ami_name_prefix']) print(target_ami_name) template = Template() template.add_builder( builder.AmazonEbs( region=ami_config['region'], ami_name=target_ami_name, instance_type=ami_config['instance_type'], source_ami=ami_config['source_ami'], ssh_username=ami_config['ssh_username'], ami_description=ami_config['ami_description'], ami_virtualization_type=ami_config['ami_virtualization_type'], force_deregister=ami_config['force_deregister'], shutdown_behavior=ami_config['shutdown_behavior'], vpc_id=ami_config['vpc_id'], subnet_id=ami_config['subnet_id'], ssh_private_key_file=ami_config['ssh_private_key_file'], ssh_keypair_name=ami_config['ssh_keypair_name'], security_group_id=ami_config['security_group_id'], ) ) template.add_provisioner( provisioner.Ansible( playbook_file=ami_config['playbook_name'], command=ami_config['cmd_ansible'], user=ami_config['ssh_username'], ) ) PackerExecutable(machine_readable=False) p = PackerExecutable(executable_path=ami_config['cmd_packer']) (ret, out, err) = p.build( template.to_json(), ) print(out) print(err)
def test_exactly_one_source_ami(self): b = builder.AmazonEbs( access_key="dummy-access-key", secret_key="dummy-secret-key", instance_type="t2.micro", ami_name="ami-result", region="us-east-1", ) with pytest.raises(ValueError) as excinfo: b.to_dict() assert 'AmazonEbs: one of the following must be specified: source_ami, source_ami_filter' == str( excinfo.value)
def test_mutually_exclusive_security_group_ami(self): b = builder.AmazonEbs( access_key="dummy-access-key", secret_key="dummy-secret-key", instance_type="t2.micro", ami_name="ami-result", region="us-east-1", source_ami="dummy-source-ami", security_group_id="sg-123", security_group_ids=["sg-123", "sg-456"], ) with pytest.raises(ValueError) as excinfo: b.to_dict() assert 'AmazonEbs: only one of the following can be specified: security_group_id, security_group_ids' == str( excinfo.value)
def get_builder_aws_ebs() -> packer_builder: ''' build the aws builder section ''' section_meta('starting', getframeinfo(currentframe()).function) variable_dictionary = { 'source_ami': "{{ user `kali_aws_ami` }}", 'region': "{{ user `aws_region` }}", 'ssh_username': "******", 'instance_type': "t2.medium", 'ami_name': "Kali Linux (Standard)", "ami_users": [""], "force_deregister": "true", "force_delete_snapshot": "true" } auth_prompt = Bullet(prompt='Choose from the items below: ', choices=['AWS Profile', 'AWS Access Key']) auth_type = auth_prompt.launch() if auth_type == 'AWS Profile': profile_prompt = Input( prompt='Please input the profile you would like to use: ') current_profile = profile_prompt.launch() variable_dictionary.update({'profile': "{}".format(current_profile)}) elif auth_type == 'AWS Access Key': variable_dictionary.update({ 'access_key': "{{ user `aws_access_key` }}", 'secret_key': "{{ user `aws_secret_key` }}" }) else: print('unknown auth type: {}'.format(auth_type)) aws_ebs_builder = packer_builder.AmazonEbs().from_dict( 'AmazonEBS', d=variable_dictionary) # TODO: fixin base package to accept string aws_ebs_builder_dict = aws_ebs_builder.to_dict() aws_ebs_builder_dict['ami_users'] = "{{user `ami_users`}}" section_meta('exiting', getframeinfo(currentframe()).function) return aws_ebs_builder_dict
def test_mutually_exclusive_subnet_id(self): b = builder.AmazonEbs( access_key="dummy-access-key", secret_key="dummy-secret-key", instance_type="t2.micro", ami_name="ami-result", region="us-east-1", source_ami="dummy-source-ami", subnet_id="subnet-12345def", subnet_filter=builder.AmazonSubnetFilter( filters={"tag:Class": "build"}, most_free=True, random=True), ) with pytest.raises(ValueError) as excinfo: b.to_dict() assert 'AmazonEbs: only one of the following can be specified: subnet_id, subnet_filter' == str( excinfo.value)
def create_template_from_config(self, tag_configs): """ Generate a packer template using configurations. :param tag_configs: dict containing the tag details :return: template of type packerlicious.Template """ template = Template() template.add_builder( builder.AmazonEbs( access_key="{{user `aws_access_key`}}", secret_key="{{user `aws_secret_key`}}", region=builder_config.get("region", "ap-southeast-2"), instance_type=builder_config.get("type", "t2.micro"), ssh_username="******", ami_name=self.ami_name, source_ami="ami-02769748522663066", tags=tag_configs)) template.add_provisioner(provisioner.Shell(script=script_path)) return template
def test_mutually_exclusive_vpc_id(self): b = builder.AmazonEbs( access_key="dummy-access-key", secret_key="dummy-secret-key", instance_type="t2.micro", ami_name="ami-result", region="us-east-1", source_ami="dummy-source-ami", vpc_id="subnet-12345def", vpc_filter=builder.AmazonVpcFilter(filters={ "tag:Class": "build", "isDefault": "false", "cidr": "/24", }), ) with pytest.raises(ValueError) as excinfo: b.to_dict() assert 'AmazonEbs: only one of the following can be specified: vpc_id, vpc_filter' == str( excinfo.value)
class TestPacker(unittest.TestCase): aws_access_key = UserVar("aws_access_key", "") aws_secret_key = UserVar("aws_secret_key", "") user_variables = [aws_access_key, aws_secret_key] builders = [ builder.AmazonEbs(access_key=Ref(aws_access_key), secret_key=Ref(aws_secret_key), region="us-east-1", source_ami_filter=builder.AmazonSourceAmiFilter( filters={ "virtualization-type": "hvm", "name": "*ubuntu-xenial-16.04-amd64-server-*", "root-device-type": "ebs" }, owners=["099720109477"], most_recent=True), instance_type="t2.micro", ami_name="packer-example {{timestamp}}", ssh_username="******") ] template = Template() template.add_variable(user_variables) template.add_builder(builders) def test_validate(self): output = packer.validate(TestPacker.template) assert output.return_code == 0 assert "successfully" in output.output def test_build(self): output = packer.build(TestPacker.template) assert output.return_code == 0 assert "successful" in output.output def test_inspect(self): output = packer.inspect(TestPacker.template) assert output.return_code == 0
def test_required_fields_missing(self): b = builder.AmazonEbs() with pytest.raises(ValueError) as excinfo: b.to_dict() assert 'required' in str(excinfo.value)
# https://www.packer.io/intro/getting-started/build-image.html#the-template from packerlicious import builder, Ref, Template, UserVar aws_access_key = UserVar("aws_access_key", "") aws_secret_key = UserVar("aws_secret_key", "") user_variables = [aws_access_key, aws_secret_key] builders = [ builder.AmazonEbs(access_key=Ref(aws_access_key), secret_key=Ref(aws_secret_key), region="us-east-1", source_ami_filter=builder.AmazonSourceAmiFilter( filters={ "virtualization-type": "hvm", "name": "*ubuntu-xenial-16.04-amd64-server-*", "root-device-type": "ebs" }, owners=["099720109477"], most_recent=True), instance_type="t2.micro", ami_name="packer-example {{timestamp}}", ssh_username="******") ] t = Template() t.add_variable(user_variables) t.add_builder(builders) t.to_json()