def provisioner_alterations(packer_template_data: dict, new_prov_data: dict) -> dict: ''' modify the provisioners section of the passed in packer data ''' section_meta('starting', getframeinfo(currentframe()).function) packer_prov_list = packer_template_data['provisioners'] bento_prov = packer_prov_list[0] # creating deepcopy of env to use later, so we can alter it and it not # mess up the original: # https://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list bento_env_vars = bento_prov['environment_vars'] bash_exec_cmd = bento_prov['execute_command'].replace(' sh ', ' bash ') bento_copy_prov = deepcopy(bento_prov) cleanup_scripts = [] # minimize.sh cleanup_scripts.append(bento_prov['scripts'].pop()) # cleanup.sh cleanup_scripts.append(bento_prov['scripts'].pop()) personal_script_dict = { 'environment_vars': bento_env_vars, 'execute_command': bash_exec_cmd, 'expect_disconnect': 'true', 'scripts': new_prov_data['scripts_custom_list'] } packerlicious_prov = packer_provisioner.Shell().from_dict( title='CustomSystemScripts', d=personal_script_dict) # adding my custom scripts packer_prov_list.insert(0, packerlicious_prov.to_dict()) # packer_prov_list.append(packerlicious_prov.to_dict()) ## SHELL: move last 2 scripts (cleanup) to bottom # clearing scripts section of all previous scripts bento_copy_prov['scripts'].clear() for script in reversed(cleanup_scripts): bento_copy_prov['scripts'].append(script) # removing for packerlicious usage del bento_copy_prov['type'] # altering the the path for the cleanup.sh, so it # doesn't try to uninstall X11 packages bento_copy_prov['scripts'][0] = '{}/cleanup.sh'.format( new_prov_data['prov_packer_dir']) packerlicious_prov = packer_provisioner.Shell().from_dict( title='CleanupBentoScripts', d=bento_copy_prov) packer_prov_list.append(packerlicious_prov.to_dict()) # print(json.dumps(packer_prov_list, indent=2)) section_meta('exiting', getframeinfo(currentframe()).function) return packer_template_data
def test_support_only(self): expected_json = """ { "provisioners": [ { "type": "shell", "inline": [ "ls" ], "except": [ "azure-arm" ] } ] } """ p = provisioner.Shell(inline=["ls"]) p.__setattr__('except', ["azure-arm"]) t = Template() t.add_provisioner(p) to_json = t.to_json() assert to_json == json.dumps(json.loads(expected_json), sort_keys=True, indent=2, separators=(',', ': '))
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_support_pause_before(self): expected_json = """ { "provisioners": [ { "type": "shell", "inline": [ "ls" ], "pause_before": "10s" } ] } """ p = provisioner.Shell(inline=["ls"], pause_before="10s") t = Template() t.add_provisioner(p) to_json = t.to_json() assert to_json == json.dumps(json.loads(expected_json), sort_keys=True, indent=2, separators=(',', ': '))
def test_required_fields_missing(self): b = provisioner.Shell() with pytest.raises(ValueError) as excinfo: b.to_dict() assert 'must be specified' in str(excinfo.value)
vboxmanage = [ [ "modifyvm", "{{.Name}}", "--nictype1", "virtio" ], [ "modifyvm", "{{.Name}}", "--memory", Ref(memory).data ], [ "modifyvm", "{{.Name}}", "--cpus", Ref(cpus).data ], ], virtualbox_version_file = ".vbox_version", vm_name = Ref(vm_name) ) ] provisioners = [ provisioner.Shell( execute_command = "echo '" + Ref(ssh_password).data + \ "' | {{.Vars}} sudo -E -S bash '{{.Path}}'", expect_disconnect = "true", scripts = [ "scripts/desktop.sh", "scripts/setup.sh", "scripts/ansible.sh" ] ), provisioner.AnsibleLocal( playbook_file = "shared/main.yml", galaxy_file = "shared/requirements.yml" ), provisioner.Shell( execute_command = "echo '" + Ref(ssh_password).data + \ "' | {{.Vars}} sudo -E -S bash '{{.Path}}'", expect_disconnect = "true", scripts = [ "scripts/clean.sh" ]