def build(
        working_dir_path: str,
        template_file_path: str,
        var_file_paths: List[str] = None,
        vars: dict = None,
        vars_from_files: dict = None,
        debug: bool = False,
        force: bool = False) -> dict:
    packer_command_args = []
    # add any specified var file paths
    if var_file_paths:
        for var_file_path in var_file_paths:
            packer_command_args.append(f"-var-file={var_file_path}")
    # add any specified vars
    if vars:
        for var_name, var_value in vars.items():
            packer_command_args.append(f"-var={var_name}={var_value}")
    # add any vars from files
    if vars_from_files:
        for var_name, file_path in vars_from_files.items():
            var_value = \
                read_value_from_file(
                    file_path,
                    working_dir=working_dir_path)
            packer_command_args.append(f"-var={var_name}={var_value}")
    # add force if requested
    if force:
        packer_command_args.append("-force")
    # dump args on debug
    if debug:
        log('build args:')
        log_pretty(packer_command_args)
    # execute build command
    packer_command_result = _packer(
        'build',
        *packer_command_args,
        template_file_path,
        working_dir=working_dir_path)
    # get build manifest from output
    packer_build_manifest = \
        _parse_packer_parsed_output_for_build_manifest(packer_command_result)
    # return the manifest
    return packer_build_manifest
def validate(working_dir_path: str,
             template_file_path: str,
             only_builder: str = None,
             var_file_paths: List[str] = None,
             vars: dict = None,
             vars_from_files: dict = None,
             syntax_only: bool = False,
             debug: bool = False) -> None:
    packer_command_args = []
    # add any specified var file paths
    if var_file_paths:
        for var_file_path in var_file_paths:
            packer_command_args.append(f"-var-file={var_file_path}")
    # add any specified vars
    if vars:
        for var_name, var_value in vars.items():
            packer_command_args.append(f"-var={var_name}={var_value}")
    # add any vars from files
    if vars_from_files:
        for var_name, file_path in vars_from_files.items():
            var_value = \
                read_value_from_file(
                    file_path,
                    working_dir=working_dir_path)
            packer_command_args.append(f"-var={var_name}={var_value}")
    # optionally check only syntax
    if syntax_only:
        packer_command_args.append('-syntax-only')
    # optionally check only builder
    if only_builder:
        packer_command_args.append(f"-only={only_builder}")
    # dump args on debug
    if debug:
        log('validate args:')
        log_pretty(packer_command_args)
    # execute validate command
    _packer('validate',
            *packer_command_args,
            template_file_path,
            working_dir=working_dir_path)
예제 #3
0
def do_out() -> None:
    # read the concourse input payload
    input_payload = _read_payload()
    # get debug setting from payload
    debug_enabled: bool = input_payload['params'].get('debug', False)
    # get the template file path from the payload
    template_file_path: str = input_payload['params']['template']
    # get the working dir path from the input
    working_dir_path = _get_working_dir_path()
    # set env vars, if provided
    if 'env_vars' in input_payload['params']:
        os.environ.update(input_payload['params']['env_vars'])
    # instantiate the var file paths and vars lists
    var_file_paths: Optional[List[str]] = None
    vars: Optional[Dict] = None
    vars_from_files: Optional[Dict] = None
    only_builder: Optional[str] = None
    # add var file paths, if provided
    if 'var_files' in input_payload['params']:
        var_file_paths = input_payload['params']['var_files']
    # add vars, if provided
    if 'vars' in input_payload['params']:
        vars = input_payload['params']['vars']
    # add vars from files, if provided
    if 'vars_from_files' in input_payload['params']:
        vars_from_files = input_payload['params']['vars_from_files']
    # set env vars from files, if provided
    if 'env_vars_from_files' in input_payload['params']:
        _process_env_var_files(input_payload['params']['env_vars_from_files'],
                               working_dir_path)
    # get the only builders, if provided
    if 'only' in input_payload['params']:
        only_builder: str = input_payload['params']['only']
    # dump details, if debug enabled
    if debug_enabled:
        log('var_file_paths:')
        log_pretty(var_file_paths)
        log('vars:')
        log_pretty(vars)
    # dump the current packer version
    lib.packer.version()
    # validate the template
    lib.packer.validate(working_dir_path,
                        template_file_path,
                        only_builder=only_builder,
                        var_file_paths=var_file_paths,
                        vars=vars,
                        vars_from_files=vars_from_files,
                        debug=debug_enabled)
    # build the template, getting the build manifest back
    build_manifest = lib.packer.build(working_dir_path,
                                      template_file_path,
                                      only_builder=only_builder,
                                      var_file_paths=var_file_paths,
                                      vars=vars,
                                      vars_from_files=vars_from_files,
                                      debug=debug_enabled)
    # dump build manifest, if debug
    if debug_enabled:
        log('build manifest:')
        log_pretty(build_manifest)
    # convert the manifest into a concourse output payload
    output_payload = _create_concourse_out_payload_from_packer_build_manifest(
        build_manifest)
    # dump output payload, if debug
    if debug_enabled:
        log('output payload:')
        log_pretty(output_payload)
    # write out the payload
    _write_payload(output_payload)
예제 #4
0
def _test__create_concourse_out_payload_from_packer_build_manifest(
        build_manifest: dict) -> None:
    log_pretty(
        lib.concourse._create_concourse_out_payload_from_packer_build_manifest(
            build_manifest))