Commit Graph

78 Commits

Author SHA1 Message Date
Megan Marsh 079786f8d0
remove this logline -- it is spammy and seems like a leftover dev debug line (#10665) 2021-02-22 11:20:44 +01:00
Adrien Delorme ed091163be
HCL2 Parse packer.required_plugins block + packer init (#10304)
This adds the new `required_plugins` block to be nested under the packer block.

Example:
```hcl
packer {
  required_plugins {
    aws = {
      version = ">= 2.7.0"
      source = "azr/aws"
    }
    azure = ">= 2.7.0"
  }
}
```

For example on darwin_amd64 Packer will install those under :
* "${PACKER_HOME_DIR}/plugin/github.com/azr/amazon/packer-plugin-amazon_2.7.0_x5.0_darwin_amd64"
* "${PACKER_HOME_DIR}/plugin/github.com/hashicorp/azure/packer-plugin-azure_2.7.0_x5.0_darwin_amd64_x5"

+ docs
+ tests
2021-02-02 18:05:04 +01:00
Sylvia Moss 291121dd55
(2) Implement datasources (#10440) 2021-01-20 10:37:16 +01:00
Megan Marsh 88b7b0e14a remove sdk 2020-12-17 13:29:25 -08:00
Megan Marsh 39ab646236
move plugin and rpc code into sdk; other minor tweaks (#10359) 2020-12-09 12:39:54 +01:00
Megan Marsh b69d69095e move postprocessor to sdk, fix generation code 2020-12-01 14:48:55 -08:00
Megan Marsh ada91b24e9 extract builder interface to sdk 2020-12-01 13:42:42 -08:00
Megan Marsh 56a45b04ab Move hook and communicator definitions to packer-plugin-sdk 2020-11-30 14:30:30 -08:00
Megan Marsh a6fdeca099 move logsecretfilter into packer sdk 2020-11-30 14:30:30 -08:00
Megan Marsh 6402362018 create packer-plugin-sdk directory and begin moving the relevant folders into it. 2020-11-13 10:47:36 -08:00
Sathyajith Bhat 9c8daed65e
Fix typo in error message (#9775)
If a variable has not been passed, the error message shown has a tiny typo (varaibles instead of variables)
2020-08-16 21:39:08 -04:00
Sylvia Moss 9181a435aa
Make max_retries a string to allow variable interpolation (#9673) 2020-08-05 17:41:20 +02:00
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
Adrien Delorme c2975140cf
HCL2: allow to use keep_input_artifact in post processors (#9477)
* HCL2: allow to use keep_input_artifact in post processors
* add basic test
* add docs
2020-06-25 09:36:48 +02:00
Adrien Delorme 51d02f8c2d hcl2: inspect command 2020-06-23 11:58:57 +02:00
Moss daaeccd8b0 Skip consecutives post-processors 2020-06-09 17:35:53 +02:00
Moss bfb9df0039 fix except flag for JSON and HCL2 2020-06-09 15:26:45 +02:00
Wilken Rivera f672f5bd9b command/validate: Add support for HCL2 configuration files
* Update validate command to use FixConfig for checking against known
fixers
* Update validation command flag docs
* Add ConfigFixer method to PackerHandler Interface
* Implement ConfigFixer interface in PackerConfig
* Remove all stdout messaging (i.e calls to c.Ui.Say) in the validate
command. The command will only display hcl.Diagnotic messaging when there is an error or warning.

HCL2 Configs
```
⇶  packer validate docker_centos_shell_provisioner.pkr.hcl

```

JSON Configs
```
⇶  packer validate vmware-iso_ubuntu_minimal/vmware-iso_ubuntu_minimal.json
Error: Failed to prepare build: "vmware-iso"

1 error occurred:
        * Deprecated configuration key: 'iso_checksum_type'. Please call `packer fix`
against your template to update your template to be compatable with the current
version of Packer. Visit https://www.packer.io/docs/commands/fix/ for more
detail.

Warning: Fixable configuration found.
You may need to run `packer fix` to get your build to run correctly.
See debug log for more information.

  map[string]interface{}{
        "builders": []interface{}{
                map[string]interface{}{
                        ... // 3 identical entries
                        "guest_os_type":     string("ubuntu-64"),
                        "http_directory":    string("http"),
-                       "iso_checksum":
string("946a6077af6f5f95a51f82fdc44051c7aa19f9cfc5f737954845a6050543d7c2"),
+                       "iso_checksum":
string("sha256:946a6077af6f5f95a51f82fdc44051c7aa19f9cfc5f737954845a6050543d7c2"),
-                       "iso_checksum_type": string("sha256"),
                        "iso_url":
string("http://old-releases.ubuntu.com/releases/14.04.1/ubuntu-14.04.1-server-amd64.iso"),
                        "shutdown_command":  string("echo 'vagrant' | sudo -S shutdown -P now"),
                        ... // 4 identical entries
                },
        },
  }
```
2020-06-05 14:24:39 -04:00
Adrien Delorme bac9c74447
packer console for HCL2 (#9359) 2020-06-05 17:23:54 +02:00
Megan Marsh 45be673c0f move this to after the prepare() is successful so that we don't try to run builds that didn't prepare successfully. 2020-05-28 11:34:00 -07: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
Adrien Delorme 42a05e1e80 more refactoring 2020-05-08 16:41:47 +02:00
Adrien Delorme c71a792186 simplify/refactor core for build & validate 2020-04-30 16:36:01 +02:00
Sylvia Moss 553b1fb9f8
Add RetriedProvisioner to allow retry provisioners (#9061) 2020-04-16 11:58:54 +02:00
Megan Marsh 32595f71cf sort interpolation input to make the interpolation deterministic 2020-03-12 13:40:56 -07:00
Megan Marsh 17f59c4ddc fix regex and actually test the check function 2020-03-12 10:00:56 -07:00
Megan Marsh ad5495ac8f
Update packer/core.go
Co-Authored-By: Adrien Delorme <azr@users.noreply.github.com>
2020-03-12 09:50:02 -07:00
Megan Marsh 063dd5c133 fix interpolation when env vars interpolate before user vars 2020-03-11 09:55:40 -07:00
Sylvia Moss 328eb8ee96
Interpolate boot_command when defined by user variable (#8640) 2020-01-27 19:10:16 +01:00
Megan Marsh e228a5bcb9 log name of postprocessor running to disambiguate long chains of pps 2020-01-15 14:47:56 -08: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
Lars Lehtonen 67afbe6fb7
packer: remove bool constant comparisons 2019-11-04 12:54:52 -08:00
Adrien Delorme 819329228a Change back to make sure all durations are a time.Duration
It is simply the best/simplest solution and trying to prevent users from passing and integer here would be like opening a can of worms. Because:

* we cannot make mapstructure validate our duration string ( with an UnmarshalJSON func etc.)
* we cannot make mapstructure spit a string instead of a duration and packer will decode-encode-decode config.
* the hcl2 generated code asks for a string, so this will be enforced by default.
2019-10-31 16:12:07 +01:00
Adrien Delorme bf3d9841c6 Force durations to be passed a strings
Before this commit it was possible to set a duration using an integer or a float. Go's time.Duration is an int64 internally an mapstructure will take advantage of this and load the number as a int64 but `1` means one ns which is unexpected/confusing. To avoid confusion and enforce readability this forces users to pass a string with a unit for a duration; ex "56s".
2019-10-31 11:47:19 +01:00
Megan Marsh 5bd8fee708 Creates a final "cleanup" provisioner to run if an error occurs during a provisioning step, allowing users to perform any custom cleanup tasks that must happen on the VM before the VM is shut down and destroyed. 2019-09-24 16:08:15 -07:00
Adrien Delorme 3b8f3c5a8b remove any reference to Push 2019-09-16 16:38:17 +02:00
Megan Marsh e54b1cedd9 text/template turns out to swallow custom error types. 2019-07-08 15:39:46 -07:00
Megan Marsh 2bbc3d50d7 better error handling when using interpolate funcs; don't swallow func errors 2019-07-08 13:49:14 -07:00
Adrien Delorme 2eef6a1492
Merge pull request #7726 from hashicorp/packer-interpolate
Packer console
2019-06-12 11:00:11 +02:00
Adrien Delorme ddb4d77dc8
Update packer/core.go
remove commented log line
2019-06-11 11:09:22 +02:00
Megan Marsh ca99cbd2d2 remove loglines 2019-06-10 11:34:57 -07:00
Megan Marsh 1a9adc29b3 fixing interpolation
fix sensitive_vars test which never worked but somehow was passing before this change.
2019-06-10 11:30:52 -07:00
Megan Marsh b8ac1a800d implement a packer console analogous to the terraform console 2019-06-05 16:35:22 -07:00
Adrien Delorme d8d5631dc2 allow to set provisioner timeout from buildfile 2019-04-08 20:09:21 +02:00
Megan Marsh b52ee14776 remove unreachable code 2019-03-14 10:05:10 -07:00
Megan Marsh 86c3c44afe switch on err type not string 2019-03-13 14:59:05 -07:00
Megan Marsh bf0d7b3620 make it work and add tests 2019-03-13 10:49:13 -07:00
Megan Marsh 7cb31714ad Allow user variables to be interpreted within the variables section of the template. 2019-03-13 10:49:13 -07:00
Adrien Delorme 0f7065f6b1 post-processor except: don't match empty names 2019-02-20 12:38:03 +01:00