Skip to content

refactor: tools upload flavors #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions .web-docs/components/builder/iso/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,20 +278,22 @@ JSON Example:

<!-- Code generated from the comments of the ToolsConfig struct in builder/vmware/common/tools_config.go; DO NOT EDIT MANUALLY -->

- `tools_upload_flavor` (string) - The flavor of the VMware Tools ISO to
upload into the VM. Valid values are darwin, linux, and windows. By
default, this is empty, which means VMware tools won't be uploaded.

- `tools_upload_path` (string) - The path in the VM to upload the VMware tools. This only takes effect if
`tools_upload_flavor` is non-empty. This is a [configuration
template](/packer/docs/templates/legacy_json_templates/engine) that has a single valid variable:
`Flavor`, which will be the value of `tools_upload_flavor`. By default
the upload path is set to `{{.Flavor}}.iso`. This setting is not used
when `remote_type` is `esx5`.

- `tools_source_path` (string) - The path on your local machine to fetch the vmware tools from. If this
is not set but the tools_upload_flavor is set, then Packer will try to
load the VMware tools from the VMware installation directory.
- `tools_upload_flavor` (string) - The flavor of VMware tools to upload into the virtual machine based on
the guest operating system. Allowed values are `darwin` (macOS), `linux`,
and `windows`. Default is empty and no version will be uploaded.

- `tools_upload_path` (string) - The path in the virtual machine to upload the VMware Tools. This only
takes effect if `tools_upload_flavor` is non-empty. This is a
[configuration template](/packer/docs/templates/legacy_json_templates/engine)
that has a single valid variable, `Flavor`, which will be the value of
`tools_upload_flavor` when the upload path is set to `{{.Flavor}}.iso`.

~> **Note:** This setting is not used when `remote_type` is `esx5`.

- `tools_source_path` (string) - The local path on your machine to the VMware Tools ISO file.

~> **Note:** If not set, but the `tools_upload_flavor` is set, the plugin
will load the VMware Tools from the product installation directory.

<!-- End of code generated from the comments of the ToolsConfig struct in builder/vmware/common/tools_config.go; -->

Expand Down
30 changes: 16 additions & 14 deletions .web-docs/components/builder/vmx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,22 @@ JSON Example:

<!-- Code generated from the comments of the ToolsConfig struct in builder/vmware/common/tools_config.go; DO NOT EDIT MANUALLY -->

- `tools_upload_flavor` (string) - The flavor of the VMware Tools ISO to
upload into the VM. Valid values are darwin, linux, and windows. By
default, this is empty, which means VMware tools won't be uploaded.

- `tools_upload_path` (string) - The path in the VM to upload the VMware tools. This only takes effect if
`tools_upload_flavor` is non-empty. This is a [configuration
template](/packer/docs/templates/legacy_json_templates/engine) that has a single valid variable:
`Flavor`, which will be the value of `tools_upload_flavor`. By default
the upload path is set to `{{.Flavor}}.iso`. This setting is not used
when `remote_type` is `esx5`.

- `tools_source_path` (string) - The path on your local machine to fetch the vmware tools from. If this
is not set but the tools_upload_flavor is set, then Packer will try to
load the VMware tools from the VMware installation directory.
- `tools_upload_flavor` (string) - The flavor of VMware tools to upload into the virtual machine based on
the guest operating system. Allowed values are `darwin` (macOS), `linux`,
and `windows`. Default is empty and no version will be uploaded.

- `tools_upload_path` (string) - The path in the virtual machine to upload the VMware Tools. This only
takes effect if `tools_upload_flavor` is non-empty. This is a
[configuration template](/packer/docs/templates/legacy_json_templates/engine)
that has a single valid variable, `Flavor`, which will be the value of
`tools_upload_flavor` when the upload path is set to `{{.Flavor}}.iso`.

~> **Note:** This setting is not used when `remote_type` is `esx5`.

- `tools_source_path` (string) - The local path on your machine to the VMware Tools ISO file.

~> **Note:** If not set, but the `tools_upload_flavor` is set, the plugin
will load the VMware Tools from the product installation directory.

<!-- End of code generated from the comments of the ToolsConfig struct in builder/vmware/common/tools_config.go; -->

Expand Down
2 changes: 1 addition & 1 deletion builder/vmware/common/step_upload_tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (c *StepUploadTools) Run(ctx context.Context, state multistep.StateBag) mul
tools_source := state.Get("tools_upload_source").(string)
ui := state.Get("ui").(packersdk.Ui)

ui.Say(fmt.Sprintf("Uploading the '%s' VMware Tools", c.ToolsUploadFlavor))
ui.Say(fmt.Sprintf("Uploading VMware Tools (%s)...", c.ToolsUploadFlavor))
f, err := os.Open(tools_source)
if err != nil {
state.Put("error", fmt.Errorf("error opening VMware Tools ISO: %s", err))
Expand Down
61 changes: 42 additions & 19 deletions builder/vmware/common/tools_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,59 @@ package common

import (
"fmt"
"slices"
"strings"

"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
)

// Set the allowed values for the `ToolsUploadFlavor`.
const (
ToolsFlavorMacOS = "darwin"
ToolsFlavorLinux = "linux"
ToolsFlavorWindows = "windows"
)

// allowedToolsFlavorValues is a list of allowed values for the
// `ToolsUploadFlavor`.
var allowedToolsFlavorValues = []string{ToolsFlavorMacOS, ToolsFlavorLinux, ToolsFlavorWindows}

type ToolsConfig struct {
// The flavor of the VMware Tools ISO to
// upload into the VM. Valid values are darwin, linux, and windows. By
// default, this is empty, which means VMware tools won't be uploaded.
// The flavor of VMware tools to upload into the virtual machine based on
// the guest operating system. Allowed values are `darwin` (macOS), `linux`,
// and `windows`. Default is empty and no version will be uploaded.
ToolsUploadFlavor string `mapstructure:"tools_upload_flavor" required:"false"`
// The path in the VM to upload the VMware tools. This only takes effect if
// `tools_upload_flavor` is non-empty. This is a [configuration
// template](/packer/docs/templates/legacy_json_templates/engine) that has a single valid variable:
// `Flavor`, which will be the value of `tools_upload_flavor`. By default
// the upload path is set to `{{.Flavor}}.iso`. This setting is not used
// when `remote_type` is `esx5`.
// The path in the virtual machine to upload the VMware Tools. This only
// takes effect if `tools_upload_flavor` is non-empty. This is a
// [configuration template](/packer/docs/templates/legacy_json_templates/engine)
// that has a single valid variable, `Flavor`, which will be the value of
// `tools_upload_flavor` when the upload path is set to `{{.Flavor}}.iso`.
//
// ~> **Note:** This setting is not used when `remote_type` is `esx5`.
ToolsUploadPath string `mapstructure:"tools_upload_path" required:"false"`
// The path on your local machine to fetch the vmware tools from. If this
// is not set but the tools_upload_flavor is set, then Packer will try to
// load the VMware tools from the VMware installation directory.
// The local path on your machine to the VMware Tools ISO file.
//
// ~> **Note:** If not set, but the `tools_upload_flavor` is set, the plugin
// will load the VMware Tools from the product installation directory.
ToolsSourcePath string `mapstructure:"tools_source_path" required:"false"`
}

func (c *ToolsConfig) Prepare(ctx *interpolate.Context) []error {
errs := []error{}
if c.ToolsUploadPath == "" {
if c.ToolsSourcePath != "" && c.ToolsUploadFlavor == "" {
errs = append(errs, fmt.Errorf("provide either "+
"'tools_upload_flavor' or 'tools_upload_path' with "+
"'tools_source_path'"))
}
if c.ToolsUploadPath != "" {
return nil
}

var errs []error

if c.ToolsSourcePath != "" && c.ToolsUploadFlavor == "" {
errs = append(errs, fmt.Errorf("provide either 'tools_upload_flavor' or 'tools_upload_path' with 'tools_source_path'"))
} else if c.ToolsUploadFlavor != "" && !slices.Contains(allowedToolsFlavorValues, c.ToolsUploadFlavor) {
errs = append(errs, fmt.Errorf("invalid 'tools_upload_flavor' specified: %s; must be one of %s", c.ToolsUploadFlavor, strings.Join(allowedToolsFlavorValues, ", ")))
} else {
c.ToolsUploadPath = fmt.Sprintf("%s.iso", c.ToolsUploadFlavor)
}

if c.ToolsSourcePath == "" {
c.ToolsUploadPath = "{{ .Flavor }}.iso"
}

Expand Down
2 changes: 1 addition & 1 deletion builder/vmware/iso/testdata/minimal.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@
"tools_upload_flavor": "linux"
}
]
}
}
26 changes: 14 additions & 12 deletions docs-partials/builder/vmware/common/ToolsConfig-not-required.mdx
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
<!-- Code generated from the comments of the ToolsConfig struct in builder/vmware/common/tools_config.go; DO NOT EDIT MANUALLY -->

- `tools_upload_flavor` (string) - The flavor of the VMware Tools ISO to
upload into the VM. Valid values are darwin, linux, and windows. By
default, this is empty, which means VMware tools won't be uploaded.
- `tools_upload_flavor` (string) - The flavor of VMware tools to upload into the virtual machine based on
the guest operating system. Allowed values are `darwin` (macOS), `linux`,
and `windows`. Default is empty and no version will be uploaded.

- `tools_upload_path` (string) - The path in the VM to upload the VMware tools. This only takes effect if
`tools_upload_flavor` is non-empty. This is a [configuration
template](/packer/docs/templates/legacy_json_templates/engine) that has a single valid variable:
`Flavor`, which will be the value of `tools_upload_flavor`. By default
the upload path is set to `{{.Flavor}}.iso`. This setting is not used
when `remote_type` is `esx5`.
- `tools_upload_path` (string) - The path in the virtual machine to upload the VMware Tools. This only
takes effect if `tools_upload_flavor` is non-empty. This is a
[configuration template](/packer/docs/templates/legacy_json_templates/engine)
that has a single valid variable, `Flavor`, which will be the value of
`tools_upload_flavor` when the upload path is set to `{{.Flavor}}.iso`.

~> **Note:** This setting is not used when `remote_type` is `esx5`.

- `tools_source_path` (string) - The path on your local machine to fetch the vmware tools from. If this
is not set but the tools_upload_flavor is set, then Packer will try to
load the VMware tools from the VMware installation directory.
- `tools_source_path` (string) - The local path on your machine to the VMware Tools ISO file.

~> **Note:** If not set, but the `tools_upload_flavor` is set, the plugin
will load the VMware Tools from the product installation directory.

<!-- End of code generated from the comments of the ToolsConfig struct in builder/vmware/common/tools_config.go; -->