File: comm_config.go

package info (click to toggle)
packer 1.6.6%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 32,016 kB
  • sloc: sh: 1,154; python: 619; makefile: 251; ruby: 205; xml: 97
file content (72 lines) | stat: -rw-r--r-- 2,639 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//go:generate struct-markdown
package qemu

import (
	"errors"

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

type CommConfig struct {
	Comm communicator.Config `mapstructure:",squash"`
	// The minimum port to use for the Communicator port on the host machine which is forwarded
	// to the SSH or WinRM port on the guest machine. By default this is 2222.
	HostPortMin int `mapstructure:"host_port_min" required:"false"`
	// The maximum port to use for the Communicator port on the host machine which is forwarded
	// to the SSH or WinRM port on the guest machine. Because Packer often runs in parallel,
	// Packer will choose a randomly available port in this range to use as the
	// host port. By default this is 4444.
	HostPortMax int `mapstructure:"host_port_max" required:"false"`
	// Defaults to false. When enabled, Packer
	// does not setup forwarded port mapping for communicator (SSH or WinRM) requests and uses ssh_port or winrm_port
	// on the host to communicate to the virtual machine.
	SkipNatMapping bool `mapstructure:"skip_nat_mapping" required:"false"`

	// These are deprecated, but we keep them around for backwards compatibility
	// TODO: remove later
	SSHHostPortMin int `mapstructure:"ssh_host_port_min" required:"false"`
	// TODO: remove later
	SSHHostPortMax int `mapstructure:"ssh_host_port_max"`
}

func (c *CommConfig) Prepare(ctx *interpolate.Context) (warnings []string, errs []error) {

	// Backwards compatibility
	if c.SSHHostPortMin != 0 {
		warnings = append(warnings, "ssh_host_port_min is deprecated and is being replaced by host_port_min. "+
			"Please, update your template to use host_port_min. In future versions of Packer, inclusion of ssh_host_port_min will error your builds.")
		c.HostPortMin = c.SSHHostPortMin
	}

	// Backwards compatibility
	if c.SSHHostPortMax != 0 {
		warnings = append(warnings, "ssh_host_port_max is deprecated and is being replaced by host_port_max. "+
			"Please, update your template to use host_port_max. In future versions of Packer, inclusion of ssh_host_port_max will error your builds.")
		c.HostPortMax = c.SSHHostPortMax
	}

	if c.Comm.SSHHost == "" && c.SkipNatMapping {
		c.Comm.SSHHost = "127.0.0.1"
	}

	if c.HostPortMin == 0 {
		c.HostPortMin = 2222
	}

	if c.HostPortMax == 0 {
		c.HostPortMax = 4444
	}

	errs = c.Comm.Prepare(ctx)
	if c.HostPortMin > c.HostPortMax {
		errs = append(errs,
			errors.New("host_port_min must be less than host_port_max"))
	}

	if c.HostPortMin < 0 {
		errs = append(errs, errors.New("host_port_min must be positive"))
	}

	return
}