Commit Graph

78 Commits

Author SHA1 Message Date
Adrien Delorme 44616d3bff
refactor initialization out from packer configs + tests (#9627)
The initialization of packer core in JSON also validates that `null` variables were set, except in the case of `packer validate --syntax-only` , but after the refactor to allow to have all commands work with HCL2 and JSON this subtlety was lost.

This refactors the initialisation of the core in order to allow to have `packer validate --syntax-only` not error in case a variable is not set. Since these calls are refactored this works for HCL2 too.

fix #9478
2020-07-24 10:58:03 +02:00
Megan Marsh 42e98f15ae make sure the no-op progress bar gets used for machine readable uis 2020-06-17 11:57:12 -07:00
Moss 4f26534072 add on-error flag option to run cleanup provisioner 2020-06-16 15:34:00 +02:00
Adrien Delorme bac9c74447
packer console for HCL2 (#9359) 2020-06-05 17:23:54 +02:00
Megan Marsh 3dc4ba2d68
Json vs HCL2 parity refactor (#9301)
* refactor so that json and hcl2 templates are both prepared in the same place in the build call, to make code easier to reason about. Remove overly verbose error output which isn't useful in vast majority of cases

* fix tests

* check err msg

* hcl2template.PackerConfig.GetBuilds: raise a diagnostic in case the packer core build perpare call errors

Co-authored-by: Adrien Delorme <adrien.delorme@icloud.com>
2020-05-28 10:43:58 +02:00
Megan Marsh ee5635722b enable force, debug, and on-error for hcl2 builds 2020-05-14 16:22:51 -07:00
Megan Marsh 533fbc1381
Merge pull request #9139 from hashicorp/hcl2_commands_refactor
Hcl2 commands refactor
2020-05-14 11:16:12 -07:00
Adrien Delorme 52b2151b21 go generate ./packer 2020-05-12 16:19:26 +02:00
Adrien Delorme 22a36ef97a Move vars from Meta to cla args; this forces us to use it in GetConfigFromJSON which in turn forces us into updating console and validate 2020-05-12 11:24:22 +02:00
Adrien Delorme 534e98c4ef pass Meta command line arguments around 2020-05-11 17:56:14 +02:00
Adrien Delorme b35ec44d06 Update build.go 2020-05-08 18:01:34 +02:00
Adrien Delorme efcc6af06c fix tests 2020-05-08 17:46:33 +02:00
Adrien Delorme 42a05e1e80 more refactoring 2020-05-08 16:41:47 +02:00
Adrien Delorme 2f63e4e79d refactor term interrupts & have RunContext take a conf struct now 2020-05-08 12:05:14 +02:00
nqb be08eddd8d
remove useless comment
-var-file allow an HCL file
2020-05-01 11:18:47 +02:00
Adrien Delorme 4047113746 Update build.go 2020-04-30 16:40:16 +02:00
Adrien Delorme 2c556ed8c0 remove clarified comment 2020-04-30 16:37:23 +02:00
Adrien Delorme c71a792186 simplify/refactor core for build & validate 2020-04-30 16:36:01 +02:00
Adrien Delorme 7aaee62970 command/build: don't reset error code in case an error hapened 2020-04-30 12:10:13 +02:00
Adrien Delorme 5c2b8da63b get builds from PackerConfig instead of parser
to allow just reading the config and to not start anything. This will allow to later on run `validate --syntax-only`.

Note that none of the builder/provisioner/post-processor config will be read but simply ignored. HCL2 still needs the body to be properly formatted and it should detect most syntax errors.
2020-04-29 16:36:40 +02:00
Tom Dyas c0a6623ea2
teach HCL mode builds to honor -only and -except options (#8947) 2020-04-28 15:03:24 +02:00
Adrien Delorme c8300b620a allow to use hcl files as var files in HCL mode
fix #8781
2020-03-12 15:41:40 +01:00
Adrien Delorme d2964d59e9 document that the `-var-file` option still expects JSON files for now 2020-03-03 17:19:51 +01:00
Adrien Delorme 193dad46e6
Hcl2 input variables, local variables and functions (#8588)
Mainly redefine or reused what Terraform did.

* allow to used `variables`, `variable` and `local` blocks
* import the following functions and their docs from Terraform: abs, abspath, basename, base64decode, base64encode, bcrypt, can, ceil, chomp, chunklist, cidrhost, cidrnetmask, cidrsubnet, cidrsubnets, coalesce, coalescelist, compact, concat, contains, convert, csvdecode, dirname, distinct, element, file, fileexists, fileset, flatten, floor, format, formatdate, formatlist, indent, index, join, jsondecode, jsonencode, keys, length, log, lookup, lower, max, md5, merge, min, parseint, pathexpand, pow, range, reverse, rsadecrypt, setintersection, setproduct, setunion, sha1, sha256, sha512, signum, slice, sort, split, strrev, substr, timestamp, timeadd, title, trim, trimprefix, trimspace, trimsuffix, try, upper, urlencode, uuidv4, uuidv5, values, yamldecode, yamlencode, zipmap.
2020-02-06 11:49:21 +01:00
Megan Marsh 5bacd37014 fix pps 2019-12-17 16:13:21 -08:00
nywilken 3d56f8ac80 command/build: Fix URL to HCL2 syntax guide
* small typo fixes
2019-12-17 15:36:04 -05:00
Adrien Delorme 0785c2f6fc
build using HCL2 (#8423)
This follows #8232 which added the code to generate the code required to parse
HCL files for each packer component.

All old config files of packer will keep on working the same. Packer takes one
argument. When a directory is passed, all files in the folder with a name
ending with  “.pkr.hcl” or “.pkr.json” will be parsed using the HCL2 format.
When a file ending with “.pkr.hcl” or “.pkr.json” is passed it will be parsed
using the HCL2 format. For every other case; the old packer style will be used.

## 1. the hcl2template pkg can create a packer.Build from a set of HCL (v2) files

I had to make the packer.coreBuild (which is our one and only packer.Build ) a public struct with public fields

## 2. Components interfaces get a new ConfigSpec Method to read a file from an HCL file.

  This is a breaking change for packer plugins.

a packer component can be a: builder/provisioner/post-processor

each component interface now gets a `ConfigSpec() hcldec.ObjectSpec`
which allows packer to tell what is the layout of the hcl2 config meant
to configure that specific component.

This ObjectSpec is sent through the wire (RPC) and a cty.Value is now
sent through the already existing configuration entrypoints:

 Provisioner.Prepare(raws ...interface{}) error
 Builder.Prepare(raws ...interface{}) ([]string, error)
 PostProcessor.Configure(raws ...interface{}) error

close #1768


Example hcl files:

```hcl
// file amazon-ebs-kms-key/run.pkr.hcl
build {
    sources = [
        "source.amazon-ebs.first",
    ]

    provisioner "shell" {
        inline = [
            "sleep 5"
        ]
    }

    post-processor "shell-local" {
        inline = [
            "sleep 5"
        ]
    }
}

// amazon-ebs-kms-key/source.pkr.hcl

source "amazon-ebs" "first" {

    ami_name = "hcl2-test"
    region = "us-east-1"
    instance_type = "t2.micro"

    kms_key_id = "c729958f-c6ba-44cd-ab39-35ab68ce0a6c"
    encrypt_boot = true
    source_ami_filter {
        filters {
          virtualization-type = "hvm"
          name =  "amzn-ami-hvm-????.??.?.????????-x86_64-gp2"
          root-device-type = "ebs"
        }
        most_recent = true
        owners = ["amazon"]
    }
    launch_block_device_mappings {
        device_name = "/dev/xvda"
        volume_size = 20
        volume_type = "gp2"
        delete_on_termination = "true"
    }
    launch_block_device_mappings {
        device_name = "/dev/xvdf"
        volume_size = 500
        volume_type = "gp2"
        delete_on_termination = true
        encrypted = true
    }

    ami_regions = ["eu-central-1"]
    run_tags {
        Name = "packer-solr-something"
        stack-name = "DevOps Tools"
    }
    
    communicator = "ssh"
    ssh_pty = true
    ssh_username = "ec2-user"
    associate_public_ip_address = true
}
```
2019-12-17 11:25:56 +01:00
Moss 7466c4fdca Return exit code 1 when builder type is not found 2019-12-10 18:55:18 +01:00
Thomas Meckel d3202497ae First working version of virtualbox/vm builder 2019-07-26 12:29:44 +02:00
Adrien Delorme 5efab58ed8 allow to have timestamped colorless ui messages 2019-06-19 15:04:13 +02:00
Adrien Delorme cb2d89af6f simplify path parsing by making at string instead of an array + add tests 2019-05-07 11:51:21 +02:00
Adrien Delorme a4b8570991 refactor arg parsing into it's own cfg maker & test it 2019-05-07 11:43:18 +02:00
Adrien Delorme 7e8c42d243 BuildCommand: put config in a local struct 2019-05-07 11:15:35 +02:00
Adrien Delorme 2890687b2b fix race condition in BuildCommand
a map[string]error was being written on unprotectedly
2019-05-06 15:42:23 +02:00
Adrien Delorme af15ed3583 nil signals are probably just a cancellation in disguise 2019-05-06 15:29:59 +02:00
Adrien Delorme dd2785ff08 BuildCommand.Run: avoid triggering a cancellation on termination 2019-05-06 12:26:22 +02:00
Adrien Delorme 0094d2878c Build: move BuildCommand.Run into a RunContext command to allow testing for cancellation, put cancellation in `Run` 2019-05-06 12:19:59 +02:00
Adrien Delorme d40d3eca88 swap semaquire and wg.Add to avoid a deadlock 2019-05-03 08:19:33 +02:00
Adrien Delorme 263f318e82 be true to docs 2019-05-02 17:48:34 +02:00
Adrien Delorme 1dca416f87 move the semacquire to the main build loop so that the build order is kept
* a goroutine could start before another !
2019-05-02 16:58:28 +02:00
Adrien Delorme 9281fada2d prevent a breaking change so that we can merge the `-parallel-builds` option first. 2019-05-02 16:24:28 +02:00
Brett Wandel aaf56ffd26 added: testing 2019-05-02 08:38:56 +10:00
Brett Wandel 76b5c1995f fixed: small changes to cancellation on ctrl-c 2019-04-19 17:10:36 +10:00
Brett Wandel 92e75f838a added the ability to limit number of builds running in parallel 2019-04-13 22:31:54 +10:00
Adrien Delorme a4bf94dd3c change Builder to be passed a context for cancellation
we have to to give it to our hook
2019-04-03 15:55:55 +02:00
Adrien Delorme 045f2f41bd display received signal in debug mode 2019-03-22 14:25:06 +01:00
Adrien Delorme 9f82b75e57 Use the hashicorp/go-getter to download files
* removed packer.Cache and references since packer.Cache is never used except in the download step. The download step now uses the new func packer.CachePath(targetPath) for this, the behavior is the same.
* removed download code from packer that was reimplemented into the go-getter library: progress bar, http download restart, checksuming from file, skip already downloaded files, symlinking, make a download cancellable by context.
* on windows if packer is running without symlinking rights and we are getting a local file, the file will be copied instead to avoid errors.
* added unit tests for step_download that are now CI tested on windows, mac & linux.
* files are now downloaded under cache dir `sha1(filename + "?checksum=" + checksum) + file_extension`
* since the output dir is based on the source url and the checksum, when the checksum fails, the file is auto deleted.
* a download file is protected and locked by a file lock,
* updated docs
* updated go modules and vendors
2019-03-13 12:11:58 +01:00
Andrew Widdersheim a5b753820c
Fix build commands -except help message
The `-except` option needed some updates that were missed in 270f851e.
2019-03-05 10:54:35 -05:00
Josh Soref 66738ccaf4 Try to make help more consistent
Also try to synchronize the completion script
2018-10-10 21:34:35 -04:00
Adrien Delorme 626a3cc2a4 have -timestamp-ui match what's in doc
this was just an omission
also `=true` is not a necessary precision in this case

#6784
2018-10-03 14:40:13 +02:00