Commit graph

31 commits

Author SHA1 Message Date
Lucas Bajolet
cd74430e0c command: add use-sequential options for commands
For all the commands that call Initialise, we introduce a new flag:
UseSequential.

This disables DAG scheduling for evaluating datasources and locals as a
fallback to the newly introduced DAG scheduling approach.

`hcl2_upgrade` is a special case here, as the template is always JSON,
there cannot be any datasource, so the DAG in this case becomes
meaningless, and is not integrated in this code path.
2024-10-29 16:10:29 -04:00
Wilken Rivera
bd0423d793 Add ignore-prerelease-flag to command help text 2024-03-12 13:19:21 -04:00
Lucas Bajolet
b5848b9913 command: add --release-only flag to validate/build
Since we added the capability for the plugin discovery to ignore
installed pre-releases of plugins, we give that capacity to the validate
and build commands, through a new command-line flag: release-only.
2024-03-01 09:04:15 -05:00
Wilken Rivera
e8d5436228 Remove functions for detecting plugin usage
With the removal of vendored_plugins we are no longer interested in tacking bundled plugins usage.
For plugins such as file, null, or anything bultin into Packer we don't track because there is no
way to install them outside of Packer, for now.
2023-10-24 06:00:42 -04:00
Lucas Bajolet
020f18e37f command: remove unused FlagSetFlags enum
The enumeration for FlagSetFlags, which presumably was added when the
Meta structure was introduced, aims to pre-populate the flagset for a
subcommand with a series of arguments.

However, despite it being documented, it is actually not used, and
therefore can safely be removed from the codebase.
2023-10-16 09:51:53 -04:00
hashicorp-copywrite[bot]
19055df3ec
[COMPLIANCE] License changes (#12568)
* Updating the license from MPL to Business Source License

Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at https://hashi.co/license-faq, and details of the license at www.hashicorp.com/bsl.

* Update copyright file headers to BUSL-1.1

---------

Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
2023-08-10 15:53:29 -07:00
Lucas Bajolet
a2930bda4f packer: warn on bundled plugins usage
Since bundled plugins will be removed in an upcoming version of Packer,
this commit adds a new warning message whenever a template uses one such
plugin.

This warning has been implemented on build, validate, console and the
inspect subcommands.

In addition to warning about the upcoming change and potential issue
this will cause, this warning message proposes solutions to the user so
they know what they'll have to do in order not to rely on those bundled
plugins later.
2023-07-18 15:36:12 -04:00
hashicorp-copywrite[bot]
b7df3ca36f
[COMPLIANCE] Add Copyright and License Headers (#12254)
Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
2023-03-02 15:37:05 -05:00
Lucas Bajolet
ca197afe9b hcp: remove superfluous return value on GetBuilds
This commit irons out one of the pain points of the HCP rework by
introducing a HCPPublisher interface, implemented both by the JSON Core,
and the HCL2 PackerConfig, which keeps a map of the build names used by
Packer to the build names pushed on HCP.

This in turn lets us go back to the old implementation of the GetBuilds
function, which returns a list of (filtered) builds, and eventually an
error if something went wrong while processing.
2022-12-22 16:06:34 -05:00
Wilken Rivera
3b9274aa04
command/validate: Add -evaluate-datasources flag to command help text (#12152)
```
Usage: packer validate [options] TEMPLATE

  Checks the template is valid by parsing the template and also
  checking the configuration with the various builders, provisioners, etc.

  If it is not valid, the errors will be shown and the command will exit
  with a non-zero exit status. If it is valid, it will exit with a zero
  exit status.

Options:

  -syntax-only                  Only check syntax. Do not verify config of the template.
  -except=foo,bar,baz           Validate all builds other than these.
  -only=foo,bar,baz             Validate only these builds.
  -machine-readable             Produce machine-readable output.
  -var 'key=value'              Variable for templates, can be used multiple times.
  -var-file=path                JSON or HCL2 file containing user variables, can be used multiple times.
  -no-warn-undeclared-var       Disable warnings for user variable files containing undeclared variables.
  -evaluate-datasources         Evaluate data sources during validation (HCL2 only, may incur costs); Defaults to false.

```
2022-12-12 13:14:25 -05:00
Lucas Bajolet
bb2384c56a command/validate: add option to eval datasources
When packer validate is invoked, it does not try to evaluate the
datasources before attempting to decide if the template is valid.

In many cases, this works, but sometimes it will fail as the value is
unknown by the validation code.

Since the validation code for all the elements of a Packer template is
left to be implemented by plugins, we cannot rely on checking for
unknown values everywhere, especially since the unknown references are
replaced automatically by a value of the right type for the
configuration expected.

So, in order for such configurations to be validable, we add an extra
option to packer validate, that will let users evaluate the datasources
from a template.
2022-11-17 09:29:55 -05:00
Wilken Rivera
57cbe4e203
core: Update validation options for undeclared variables (#12104)
* Update validation options for undeclared variables

In an effort to help users move from JSON to HCL2 templates the support for
variable definitions files are being updated to ignore undeclared
variable warnings on build execution. For legacy JSON templates builds no
warnings are displayed when var-files contain undeclared variables.

Since preferred mode HCL2 templates is to be explicit with variable
declarations - they must be declared to be used - validation for
undeclared variables still warns when running `packer validate`. A new
flag has been added to the validate command that can be used to disable
undeclared variable warnings.

* Update validation test for unused variables

Example Run
```
~>  go run . validate -no-warn-undeclared-var -var-file
command/test-fixtures/validate/var-file-tests/undeclared.pkrvars.hcl
command/test-fixtures/validate/var-file-tests/basic.pkr.hcl
The configuration is valid.

~>  go run . validate -var-file
command/test-fixtures/validate/var-file-tests/undeclared.pkrvars.hcl
command/test-fixtures/validate/var-file-tests/basic.pkr.hcl
Warning: Undefined variable

The variable "unused" was set but was not declared as an input variable.
To declare variable "unused" place this block in one of your .pkr.hcl
files,
such as variables.pkr.hcl

variable "unused" {
  type    = string
  default = null

}

The configuration is valid.

~>  go run . build -var-file
command/test-fixtures/validate/var-file-tests/undeclared.pkrvars.hcl
command/test-fixtures/validate/var-file-tests/basic.pkr.hcl
file.chocolate: output will be in this color.

Build 'file.chocolate' finished after 744 microseconds.

==> Wait completed after 798 microseconds

==> Builds finished. The artifacts of successful builds are:
--> file.chocolate: Stored file: chocolate.txt
```

* Rename Strict field to WarnOnUndeclaredVar

The field name Strict is a bit vague since it is only used for
checking against undeclared variables within a var-file definition.
To mitigate against potential overloading of this field it is
being renamed to be more explicit on its usage.

* command/build: Add warn-on-undeclared-var flag

Now that the default behaviour is to not display warnings for undeclared variables
an optional flag has been added to toggle the old behaviour.

```
~>  go run . build -warn-on-undeclared-var -var-file command/test-fixtures/validate/var-file-tests/undeclared.pkrvars.hcl command/test-fixtures/validate/var-file-tests/basic.pkr.hcl
Warning: Undefined variable

The variable "unused" was set but was not declared as an input variable.
To declare variable "unused" place this block in one of your .pkr.hcl files,
such as variables.pkr.hcl

variable "unused" {
  type    = string
  default = null

}

file.chocolate: output will be in this color.

Build 'file.chocolate' finished after 762 microseconds.

==> Wait completed after 799 microseconds

==> Builds finished. The artifacts of successful builds are:
--> file.chocolate: Stored file: chocolate.txt
```
2022-11-14 17:06:45 -05:00
Lucas Bajolet
dad07c6097 hcp: keep map of build names for hcp tracking
To be able to track HCP builds during a Packer build, we need to
propagate the name used for registering the build to HCP.

This works out-of-the-box for JSON templates, as the build's name is
always the builder's, so there's no confusion on that.

For HCP templates however, there's a possibility for a template to
define a name for the build, which is returned by a CoreBuild's Name()
function, in addition to the source's Type/Name.
The builds are registered to HCP with the source.String() function
however, which does not contain the build's name. This prevents any
build containing a name to work, as their name/source.String does not
match.

The name we registered the build with is stored in a CoreBuild as the
Type attribute, but cannot be safely accessed, as builds are types
within the command as packersdk.Build, which only exposes Name() for
this purpose.

In order to circumvent this problem, and as a way to present a prototype
of solution that will likely have to be discussed before we merge it,
this commit hotfixes the issue.
2022-11-14 13:31:35 -05:00
Rohit Kothari
6a569b964f
Show successful message upon successful packer validate (#11337)
* Show successful message upon successful packer validate

Co-authored-by: Adrien Delorme <azr@users.noreply.github.com>
2021-10-18 15:03:44 +02:00
Megan Marsh
dd167925d7
add machine readable to the cli help func and docs (#10658) 2021-02-19 11:17:47 +01:00
Sylvia Moss
291121dd55
(2) Implement datasources (#10440) 2021-01-20 10:37:16 +01:00
Tristan Watson
17c806fbf5
Removing obsolete packer HCL warnings from CLI for validate and command (#10461) 2021-01-11 11:50:23 +01:00
Megan Marsh
00cc425b84 docs tweaks 2020-10-14 12:58:04 -07: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
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
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
c71a792186 simplify/refactor core for build & validate 2020-04-30 16:36:01 +02:00
Adrien Delorme
d2964d59e9 document that the -var-file option still expects JSON files for now 2020-03-03 17:19:51 +01: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
Matthew Hooker
dde6805ee8
ignore empty top-level config keys when vetting fix 2018-07-02 13:57:11 -07:00
Matthew Hooker
a5e29e68da
cmd/validate: notify user if config is "fixable" 2018-06-25 22:21:16 -07:00
Jeremy Voorhis
bfc75eb9d9 Implement cli.CommandAutocomplete for most commands 2017-10-13 11:57:44 -07:00
Matthew Hooker
81522dced0
move packer to hashicorp 2017-04-04 13:39:01 -07:00
Mitchell Hashimoto
c49fe672b3 command/validate: bail if can't initialize build [GH-2139] 2015-05-29 16:09:37 -07:00
Mitchell Hashimoto
dc74ec5612 packer: remove Environment 2015-05-25 17:29:10 -07:00
Mitchell Hashimoto
8054e66db6 command: move more to this package, remove old packages 2014-10-27 20:31:02 -07:00
Renamed from command/validate/command.go (Browse further)