Commit graph

65 commits

Author SHA1 Message Date
anshulSharma
e8d5ddb38a
formatting multiple hcl files (#13362)
* formatting multiple hcl files

* recursive formating

* test case fixing

* test case fix

* removed not required code

* existing test code fix

* go formatting

* more test cases for new cases

* doc changes

* added additional check on error message in negative test case
2025-05-02 10:16:00 +05:30
Martin Grogan
5e6e1a2217 hcl: add get GetHCPPackerRegistry method to PackerConfig
This method will be used to validate and fetch the correct
HCPPackerRefistry block to be used in HCP build

hcl: check for multiple HCP Packer registry block

hcl: support for top level HCP Packer Registry

hcl: deprecation warning for build block based hcp config
2025-03-11 14:49:59 -04:00
Lucas Bajolet
ac899c44c2 hcl2template: don't error on empty bucket slug
When a user defines a `hcp_packer_registry` block in their `build`
without a `bucket_name`, but they define it in their environment, Packer
should not report the bucket_name being wrong.
2024-12-12 11:29:01 -05:00
Lucas Bajolet
dff49df129 hcl2template: check bucket name at parse-time
Not validating the bucket's name during parse leads to configurations
being marked as valid, even if the bucket name is not, which will fail
during a real build afterwards.

To avoid this problem and fail with an error as quickly as possible, we
add a check during parsing, so that it gets reported for validate as
well.
2024-01-31 09:56:57 -05:00
Lucas Bajolet
c0e7e7bd3c hcl2: report error on build without sources
When a template describes a build block without a source reference, the
build should be considered invalid as we won't have a CoreBuild produced
as a result of the need to have both.

In current versions of Packer, this will produce an error message
hinting that nothing will happen because of the lack of either build or
source block.

This commit takes the defined block, and points out to it as missing a
source block as being the reason why nothing is happening, making it
clearer what is required for an HCL2 build to be processed.
2022-09-30 15:39:27 -04:00
Lucas Bajolet
a298617ecb command/build: warn and exit when nothing to build
When a `packer build' is run on a config that does not define anything
to be built, the process would run normally, and only state that no
artifacts were produced after builds were run successfully.

This is misleading as we reach the end of the command without having a
proper warning that nothing was even attempted to be built.

This commit adds a warning when that happens, and leaves immediately
after.
2022-09-13 15:58:28 -04:00
Lucas Bajolet
7cd095062a parser: extract HCP logic out of the HCL parser
Before this commit, some HCP-related logic was embedded in the parser,
making all of it common to all commands.

This complexifies the logic as some parts rely on the template being in
a HCP-valid environment, and the datasources need to be evaluated in
order for some operations to be executed.

To simplify this and avoid those pitfalls, we move this logic to its own
set of functions that will be performed after the parsing is done, on
any valid HCL2 or JSON template.
2022-08-12 11:54:21 -04:00
Wilken Rivera
760169b34e
Move to using ectx to preload build names (#11432)
* Add basic post-processor test

Closes #11411
2021-12-07 12:24:12 +01:00
Wilken Rivera
4a0fbd911a
Add support for variable interpolation to hcp_packer_registry and build blocks. (#11421)
* Add variable interpolation support to hcp_packer_registry block

* Add tests for variable interpolation in hcp_packer_registry block

* Add variable interpolation support to the build block (#11425)

* Add simple test to build command
2021-12-01 11:35:51 +01:00
Wilken Rivera
06b35c39de
hcp packer registry build labels (#11401)
* Add basic support for build_labels argument

* Update support for build_labels configuration argument

* Update complete test-fixture with a build_labels configuration
* Add test for deprecated labels argument
* Add deprecation for hcp_packer_registry.labels

When using the now deprecated labels argument of the new bucket_labels a
Warning will be presented to the user.

```
~>  HCP_PACKER_BUILD_FINGERPRINT=356786543567865456789656789 packer
build source.pkr.hcl
Warning: the argument hcp_packer_registry.labels has been deprecated and will be removed in a future release; please use hcp_packer_registry.bucket_labels
```

When trying to use both bucket_labels and labels together an error is
presented to the user.
```
~>  HCP_PACKER_BUILD_FINGERPRINT=ss6786543567865456789656789 packer
build source.pkr.hcl
Error: hcp_packer_registry.labels and hcp_packer_registry.bucket_labels are mutely exclusive; please use the recommended argument hcp_packer_registry.bucket_labels

  on source.pkr.hcl line 17:
    (source code not available)

```

* Update documentation for build_labels

* Apply suggestions from code review

Co-authored-by: Adrien Delorme <azr@users.noreply.github.com>

* Update hcl2template/types.build.hcp_packer_registry.go

Co-authored-by: Adrien Delorme <azr@users.noreply.github.com>
2021-11-18 09:40:08 +01:00
Megan Marsh
e7ba4d390d
Merge pull request #11318 from hashicorp/fix_source_accessor
fix source accessor to allow use in source block
2021-10-11 10:40:34 -07:00
Megan Marsh
76c1059680 add tests, and simplify code 2021-10-11 10:29:36 -07:00
Adrien Delorme
3135714465
rename all things 'inexist[e|a]nt' to 'nonexistent' & fix tests (#11325)
+ a typo
2021-10-11 15:48:51 +02:00
Kerim Satirli
bc6f1e36e6
Fixes typos in docs (#11322)
* fixes typos in base docs

* fixes typos in example code

* fixes typos in website docs

* fixes link to Terraform provider

* fixes typo in function name
2021-10-11 10:19:49 +02:00
Megan Marsh
48de1fc7da allow datasources to use other datasources
create a null data source for testing. We can choose to document if we want to, but it's a convenience for us

add a test to catch cyclic datasource dependency, update tests to include out of order data sources, and update the code to clean up the returned diagnostics generated from the recursive evaluation

PR review comments
2021-09-03 16:10:39 -07:00
Megan Marsh
4c22169c90 more test fix 2021-08-23 15:29:58 -07:00
Megan Marsh
2dd8c0dd26 fix tests 2021-08-23 14:18:27 -07:00
Wilken Rivera
e8b5b4b516
Fix {{packer_version}} interpolation regression (#11200)
* Add Packer version to main context at variable interpolation

Test Results Before Change
```
=== RUN   TestCoreBuild_packerVersion
    testing.go:30: err: template: root:1:2: executing "root" at <packer_version>: error calling packer_version: packer_version not available
--- FAIL: TestCoreBuild_packerVersion (0.00s)
```

Test Results After Change
```
=== RUN   TestCoreBuild_packerVersion
--- PASS: TestCoreBuild_packerVersion (0.00s)
```

* Add packer_core_version to interpolation context for HCL configs

Test Results Before Change
```
=== RUN   TestParse_build/provisioner_with_packer_version_interpolation
    common_test.go:109: Parser.getBuilds() unexpected diagnostics. testdata/build/provisioner_packer_version_interpolation.pkr.hcl:8,5-24: Failed preparing provisioner-block
 "shell" ""; render 'slice_string': template: root:1:2: executing "root" at <packer_version>: error calling packer_version: packer_version not available in:

        {{packer_version}}
--- FAIL: TestParse_build (0.01s)
```

Test Results After Change
```
--- PASS: TestParse_build (0.02s)
--- PASS: TestParse_build/provisioner_with_packer_version_interpolation (0.00s)
```
2021-08-12 10:55:32 +02:00
Wilken Rivera
a534c743cf
HCP Packer registry enabled Packer client (#11154)
* Add working registry pkg

* Add custom error for handling the loading of PAR environment variables

* Working Publish to Build, with proper error handling for bucket names

* Update hcp-sdk-go to use branch instead of mod replace directive

* Update Packer build status configuration

* Add support for HCP_PACKER_BUILD_FINGERPRINT env

* Add support for publishing one or more PARtifacts from a single build

* add git shas to this branch

* Add ability to set provider name if available

* Add working RegistryBuilder type

* Add RegistryPostProcessor as wrapper post-processor

* When in PAR mode a empty RegistryPostProcessor is added to the end of
  the post-processor list to publish all final image data.

* Add support for updating a build from PAR that is not in a DONE state

* Fix a small issue with creation the initial builds for an empty
  iteration.

* Add PAR URL to post-processor display

* Implement hcp_packer_registry block (#11168)

* Update vendored Amazon plugin to v1.0.1-dev

* Fix panic when running a Packer registry build in a clean directory

* Remove the publishing of post-processor metadata from the registry
  post-processor.

* Remove metadata add from registry_builder

* Update registry builder to skip a build that was found to be DONE

Co-authored-by: Megan Marsh <megan@hashicorp.com>
Co-authored-by: Sylvia Moss <moss@hashicorp.com>
2021-08-05 09:25:19 -04:00
Adrien Delorme
029729225d tests and fixes 2021-02-15 13:47:39 +01:00
Sylvia Moss
774c5903f6
Add error-cleanup-provisioner to HCL2 (#10604) 2021-02-11 10:23:15 +01:00
Sylvia Moss
3ddb17ad86
Don't allow data sources to be used inside data sources (#10559) 2021-02-04 11:25: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
Megan Marsh
fbbda0f9d9
Sensitive locals (#10509)
* Allow locals to be delcared as individual blocks, and give them the Sensitive flag

* add docs for new local block

* linting

* add tests

* modified parsing to use schema, check for dupes properly

* update comment

fix wording a liiitle

* add tests for duplicate variables definition in two different files

* remove unnecessary slice initialisation

* fix crash by returning when decode error is hit

* parseLocalVariables: only treat a local vars if its not nil

also return in case of error
return locals in case of error too

* fix duplicate_locals test for windows

Co-authored-by: Adrien Delorme <azr@users.noreply.github.com>
2021-01-26 10:21:44 +01:00
Sylvia Moss
291121dd55
(2) Implement datasources (#10440) 2021-01-20 10:37:16 +01:00
Wilken Rivera
4e58987026
command/fmt: Ensure all variable files ending in .pkrvars.hcl get formatted (#10377)
Before change
```
⇶  packer fmt -check /tmp/unformatted.pkrvars.hcl
Error: Cannot tell whether /tmp/unformatted.pkrvars.hcl contains HCL2 configuration data

⇶  echo $?
1
```

After fix
```
⇶  packer fmt -check /tmp/unformatted.pkrvars.hcl
/tmp/unformatted.pkrvars.hcl

⇶  echo $?
3

⇶  packer fmt -check command/test-fixtures/fmt
command/test-fixtures/fmt/unformatted.pkr.hcl
command/test-fixtures/fmt/unformatted.auto.pkrvars.hcl
command/test-fixtures/fmt/unformatted.pkrvars.hcl

```
2020-12-14 10:29:58 -05:00
Wilken Rivera
acabc1c1aa
Add packer fmt command (#10225)
* Add packer fmt command

This change adds a new command that allows users to format one or more
HCL2 Packer configuration template files.

Related to: #9174

* command/fmt: Add check flag

Packer's fmt command now supports a check flag that will output the name
of any file that would be changed by the HCL2 formatting engine. The
check flag is mutually exclusive with the write flag and will only check
if formatting is needed.

The update write flag will now overwrite the source files with the newly
formatted HCL2 source unless the `-write=false` or `-check` is passed at
the command line.

* Returns a diagnostic error if Format is unable to show a diff - equivalent to `terraform fmt`
* Updates testing to run against #Format and not the private methods of the HCL2Formatter; fixes ShowDiff test failure on Windows
* Updates comments for exported functions

* Add docs for fmt command
2020-11-11 11:49:39 -05:00
Adrien Delorme
6911495fc4 add VariableAssignment struct that help describe an input var assignment 2020-11-02 15:22:29 +01:00
Adrien Delorme
b892414e84 add failing test case 2020-10-30 15:40:31 +01:00
Adrien Delorme
4e08ea6a92 add a test 2020-10-30 12:41:29 +01:00
Adrien Delorme
180dc4a54e add test 2020-09-30 12:02:52 +02:00
Sylvia Moss
9181a435aa
Make max_retries a string to allow variable interpolation (#9673) 2020-08-05 17:41:20 +02:00
Adrien Delorme
c7b35dd6bc
HCL2: add post-processors block to run multiple post-processor after a build (#9638)
added `post-processors` block to run chained post-processors after a build.
Before this, defining multiple `post-processor` blocks after
provisioning steps would run them sequentially, now doing this makes them start
from the build's artifact. To queue post-processors you now have to define them
in a `post-processors` block.

This is a breaking change.
2020-07-28 10:02:37 +02:00
Adrien Delorme
0ed10f921f add test 2020-07-06 11:48:39 +02:00
Adrien Delorme
db6c3adbba add hcl2 provisioner type that reprepares itself 2020-07-02 18:02:19 +02:00
Adrien Delorme
284d46a1e0 WIP 2020-07-02 15:56:49 +02:00
Adrien Delorme
3d371a2d5d
Add complete HCL2 examples + allow to name a singular build.source blocks (#9490)
* in the examples/hcl folder
* add possibility to name singular build.source blocks to differentiate their output and to filter on them
2020-07-02 11:07:59 +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
Jeremiah Snapp
0d7c5dc670
Enable only/except for provisioners and post-processors (#9357) 2020-06-05 11:51:23 +02:00
Adrien Delorme
f09fd790b7
Merge branch 'master' into master 2020-06-02 15:12:44 +02:00
Adrien Delorme
125ffe36c3 Allow to override some source fields from the build section
Using `hcl.MergeBodies`
2020-05-25 17:09:37 +02:00
Adrienne Cohea
6c6039798c
Add test. 2020-05-17 23:27:06 -07:00
Sylvia Moss
553b1fb9f8
Add RetriedProvisioner to allow retry provisioners (#9061) 2020-04-16 11:58:54 +02:00
Adrien Delorme
840e67f775
HCL2: when we see a map generate an attribute spec instead of a block spec (#9035)
* mapstructure-to-hcl2: when we see a map generate an attribute spec and not a block spec

this will alow to do

tags = {
 key = "value"
}

instead of

tags {
  key = "value"
}

This will also enable using variables directly for those tags

* generate code

* update tests
2020-04-14 16:05:13 +02:00
Adrien Delorme
710ebdcef5 add more test for var files 2020-03-12 17:08:53 +01:00
Adrien Delorme
0ccff0d5b9 all variables must have a value.
A variable's default value can be set to null to force user to set it.
2020-03-09 17:25:56 +01:00
Adrien Delorme
6d8cce501e tweak validation & add tests 2020-03-09 16:16:59 +01:00
Adrien Delorme
9b649594c6 update tests 2020-03-04 13:01:18 +01:00
Adrien Delorme
59cc246942 Merge remote-tracking branch 'origin/master' into fix_8730 2020-03-04 12:30:01 +01:00
Adrien Delorme
50896d4ddf WIP 2020-03-03 11:15:56 +01:00