make everything a uint

This commit is contained in:
Adrien Delorme 2019-03-18 17:19:03 +01:00
parent 4cb58446f7
commit c214f6735b
24 changed files with 58 additions and 57 deletions

View File

@ -37,7 +37,7 @@ type Config struct {
Network string `mapstructure:"network"` Network string `mapstructure:"network"`
Project string `mapstructure:"project"` Project string `mapstructure:"project"`
PublicIPAddress string `mapstructure:"public_ip_address"` PublicIPAddress string `mapstructure:"public_ip_address"`
PublicPort int `mapstructure:"public_port"` PublicPort uint `mapstructure:"public_port"`
SecurityGroups []string `mapstructure:"security_groups"` SecurityGroups []string `mapstructure:"security_groups"`
ServiceOffering string `mapstructure:"service_offering"` ServiceOffering string `mapstructure:"service_offering"`
PreventFirewallChanges bool `mapstructure:"prevent_firewall_changes"` PreventFirewallChanges bool `mapstructure:"prevent_firewall_changes"`

View File

@ -15,8 +15,8 @@ func commHost(state multistep.StateBag) (string, error) {
return ip, nil return ip, nil
} }
func commPort(state multistep.StateBag) (int, error) { func commPort(state multistep.StateBag) (uint, error) {
commPort, hasPort := state.Get("commPort").(int) commPort, hasPort := state.Get("commPort").(uint)
if !hasPort { if !hasPort {
return 0, fmt.Errorf("Failed to retrieve communication port") return 0, fmt.Errorf("Failed to retrieve communication port")
} }

View File

@ -13,8 +13,8 @@ import (
) )
type stepSetupNetworking struct { type stepSetupNetworking struct {
privatePort int privatePort uint
publicPort int publicPort uint
} }
func (s *stepSetupNetworking) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { func (s *stepSetupNetworking) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
@ -36,7 +36,7 @@ func (s *stepSetupNetworking) Run(_ context.Context, state multistep.StateBag) m
} else { } else {
// Generate a random public port used to configure our port forward. // Generate a random public port used to configure our port forward.
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
s.publicPort = 50000 + rand.Intn(10000) s.publicPort = uint(50000 + rand.Intn(10000))
} }
state.Put("commPort", s.publicPort) state.Put("commPort", s.publicPort)
@ -99,9 +99,9 @@ func (s *stepSetupNetworking) Run(_ context.Context, state multistep.StateBag) m
ui.Message("Creating port forward...") ui.Message("Creating port forward...")
p := client.Firewall.NewCreatePortForwardingRuleParams( p := client.Firewall.NewCreatePortForwardingRuleParams(
config.PublicIPAddress, config.PublicIPAddress,
s.privatePort, int(s.privatePort),
"TCP", "TCP",
s.publicPort, int(s.publicPort),
instanceID, instanceID,
) )
@ -143,8 +143,8 @@ func (s *stepSetupNetworking) Run(_ context.Context, state multistep.StateBag) m
p.SetAclid(network.Aclid) p.SetAclid(network.Aclid)
p.SetAction("allow") p.SetAction("allow")
p.SetCidrlist(config.CIDRList) p.SetCidrlist(config.CIDRList)
p.SetStartport(s.privatePort) p.SetStartport(int(s.privatePort))
p.SetEndport(s.privatePort) p.SetEndport(int(s.privatePort))
p.SetTraffictype("ingress") p.SetTraffictype("ingress")
// Create the network ACL rule. // Create the network ACL rule.
@ -166,8 +166,8 @@ func (s *stepSetupNetworking) Run(_ context.Context, state multistep.StateBag) m
// Configure the firewall rule. // Configure the firewall rule.
p.SetCidrlist(config.CIDRList) p.SetCidrlist(config.CIDRList)
p.SetStartport(s.publicPort) p.SetStartport(int(s.publicPort))
p.SetEndport(s.publicPort) p.SetEndport(int(s.publicPort))
fwRule, err := client.Firewall.CreateFirewallRule(p) fwRule, err := client.Firewall.CreateFirewallRule(p)
if err != nil { if err != nil {

View File

@ -54,8 +54,8 @@ func (s *stepCreateSecurityGroup) Run(_ context.Context, state multistep.StateBa
i.SetCidrlist(config.CIDRList) i.SetCidrlist(config.CIDRList)
i.SetProtocol("TCP") i.SetProtocol("TCP")
i.SetSecuritygroupid(sg.Id) i.SetSecuritygroupid(sg.Id)
i.SetStartport(config.Comm.Port()) i.SetStartport(int(config.Comm.Port()))
i.SetEndport(config.Comm.Port()) i.SetEndport(int(config.Comm.Port()))
if config.Project != "" { if config.Project != "" {
i.SetProjectid(config.Project) i.SetProjectid(config.Project)
} }

View File

@ -120,8 +120,8 @@ type Config struct {
SSHHostPortMax uint `mapstructure:"ssh_host_port_max"` SSHHostPortMax uint `mapstructure:"ssh_host_port_max"`
UseDefaultDisplay bool `mapstructure:"use_default_display"` UseDefaultDisplay bool `mapstructure:"use_default_display"`
VNCBindAddress string `mapstructure:"vnc_bind_address"` VNCBindAddress string `mapstructure:"vnc_bind_address"`
VNCPortMin int `mapstructure:"vnc_port_min"` VNCPortMin uint `mapstructure:"vnc_port_min"`
VNCPortMax int `mapstructure:"vnc_port_max"` VNCPortMax uint `mapstructure:"vnc_port_max"`
VMName string `mapstructure:"vm_name"` VMName string `mapstructure:"vm_name"`
// These are deprecated, but we keep them around for BC // These are deprecated, but we keep them around for BC

View File

@ -8,7 +8,7 @@ func commHost(state multistep.StateBag) (string, error) {
return "127.0.0.1", nil return "127.0.0.1", nil
} }
func commPort(state multistep.StateBag) (int, error) { func commPort(state multistep.StateBag) (uint, error) {
sshHostPort := state.Get("sshHostPort").(uint) sshHostPort := state.Get("sshHostPort").(uint)
return int(sshHostPort), nil return sshHostPort, nil
} }

View File

@ -11,8 +11,8 @@ func CommHost() func(multistep.StateBag) (string, error) {
} }
} }
func SSHPort() func(multistep.StateBag) (int, error) { func SSHPort() func(multistep.StateBag) (uint, error) {
return func(state multistep.StateBag) (int, error) { return func(state multistep.StateBag) (uint, error) {
config := state.Get("config").(*Config) config := state.Get("config").(*Config)
return config.Comm.SSHPort, nil return config.Comm.SSHPort, nil
} }

View File

@ -44,7 +44,7 @@ func (s *StepSSHConfig) Run(_ context.Context, state multistep.StateBag) multist
state.Put("error", err) state.Put("error", err)
return multistep.ActionHalt return multistep.ActionHalt
} }
config.Comm.SSHPort = port config.Comm.SSHPort = uint(port)
return multistep.ActionContinue return multistep.ActionContinue
} }

View File

@ -10,8 +10,8 @@ type RunConfig struct {
Headless bool `mapstructure:"headless"` Headless bool `mapstructure:"headless"`
VRDPBindAddress string `mapstructure:"vrdp_bind_address"` VRDPBindAddress string `mapstructure:"vrdp_bind_address"`
VRDPPortMin int `mapstructure:"vrdp_port_min"` VRDPPortMin uint `mapstructure:"vrdp_port_min"`
VRDPPortMax int `mapstructure:"vrdp_port_max"` VRDPPortMax uint `mapstructure:"vrdp_port_max"`
} }
func (c *RunConfig) Prepare(ctx *interpolate.Context) (errs []error) { func (c *RunConfig) Prepare(ctx *interpolate.Context) (errs []error) {

View File

@ -10,7 +10,7 @@ func CommHost(host string) func(multistep.StateBag) (string, error) {
} }
} }
func SSHPort(state multistep.StateBag) (int, error) { func SSHPort(state multistep.StateBag) (uint, error) {
sshHostPort := state.Get("sshHostPort").(int) sshHostPort := state.Get("sshHostPort").(uint)
return sshHostPort, nil return sshHostPort, nil
} }

View File

@ -11,8 +11,8 @@ import (
type SSHConfig struct { type SSHConfig struct {
Comm communicator.Config `mapstructure:",squash"` Comm communicator.Config `mapstructure:",squash"`
SSHHostPortMin int `mapstructure:"ssh_host_port_min"` SSHHostPortMin uint `mapstructure:"ssh_host_port_min"`
SSHHostPortMax int `mapstructure:"ssh_host_port_max"` SSHHostPortMax uint `mapstructure:"ssh_host_port_max"`
SSHSkipNatMapping bool `mapstructure:"ssh_skip_nat_mapping"` SSHSkipNatMapping bool `mapstructure:"ssh_skip_nat_mapping"`
// These are deprecated, but we keep them around for BC // These are deprecated, but we keep them around for BC

View File

@ -22,8 +22,8 @@ import (
// vrdp_port unit - The port that VRDP is configured to listen on. // vrdp_port unit - The port that VRDP is configured to listen on.
type StepConfigureVRDP struct { type StepConfigureVRDP struct {
VRDPBindAddress string VRDPBindAddress string
VRDPPortMin int VRDPPortMin uint
VRDPPortMax int VRDPPortMax uint
l *net.Listener l *net.Listener
} }

View File

@ -22,8 +22,8 @@ import (
// Produces: // Produces:
type StepForwardSSH struct { type StepForwardSSH struct {
CommConfig *communicator.Config CommConfig *communicator.Config
HostPortMin int HostPortMin uint
HostPortMax int HostPortMax uint
SkipNatMapping bool SkipNatMapping bool
l *net.Listener l *net.Listener
@ -40,7 +40,7 @@ func (s *StepForwardSSH) Run(ctx context.Context, state multistep.StateBag) mult
return multistep.ActionContinue return multistep.ActionContinue
} }
guestPort := s.CommConfig.Port() guestPort := uint(s.CommConfig.Port())
sshHostPort := guestPort sshHostPort := guestPort
if !s.SkipNatMapping { if !s.SkipNatMapping {
log.Printf("Looking for available communicator (SSH, WinRM, etc) port between %d and %d", log.Printf("Looking for available communicator (SSH, WinRM, etc) port between %d and %d",

View File

@ -10,8 +10,8 @@ type RunConfig struct {
Headless bool `mapstructure:"headless"` Headless bool `mapstructure:"headless"`
VNCBindAddress string `mapstructure:"vnc_bind_address"` VNCBindAddress string `mapstructure:"vnc_bind_address"`
VNCPortMin int `mapstructure:"vnc_port_min"` VNCPortMin uint `mapstructure:"vnc_port_min"`
VNCPortMax int `mapstructure:"vnc_port_max"` VNCPortMax uint `mapstructure:"vnc_port_max"`
VNCDisablePassword bool `mapstructure:"vnc_disable_password"` VNCDisablePassword bool `mapstructure:"vnc_disable_password"`
} }

View File

@ -18,12 +18,12 @@ import (
// vmx_path string // vmx_path string
// //
// Produces: // Produces:
// vnc_port int - The port that VNC is configured to listen on. // vnc_port uint - The port that VNC is configured to listen on.
type StepConfigureVNC struct { type StepConfigureVNC struct {
Enabled bool Enabled bool
VNCBindAddress string VNCBindAddress string
VNCPortMin int VNCPortMin uint
VNCPortMax int VNCPortMax uint
VNCDisablePassword bool VNCDisablePassword bool
l *net.Listener l *net.Listener
@ -31,7 +31,7 @@ type StepConfigureVNC struct {
type VNCAddressFinder interface { type VNCAddressFinder interface {
// UpdateVMX, sets driver specific VNC values to VMX data. // UpdateVMX, sets driver specific VNC values to VMX data.
UpdateVMX(vncAddress, vncPassword string, vncPort int, vmxData map[string]string) UpdateVMX(vncAddress, vncPassword string, vncPort uint, vmxData map[string]string)
} }
func VNCPassword(skipPassword bool) string { func VNCPassword(skipPassword bool) string {
@ -105,12 +105,14 @@ func (s *StepConfigureVNC) Run(ctx context.Context, state multistep.StateBag) mu
return multistep.ActionHalt return multistep.ActionHalt
} }
state.Put("vnc_port", s.l.Port)
state.Put("vnc_ip", s.l.Address)
state.Put("vnc_password", vncPassword) state.Put("vnc_password", vncPassword)
return multistep.ActionContinue return multistep.ActionContinue
} }
func (*StepConfigureVNC) UpdateVMX(address, password string, port int, data map[string]string) { func (StepConfigureVNC) UpdateVMX(address, password string, port uint, data map[string]string) {
data["remotedisplay.vnc.enabled"] = "TRUE" data["remotedisplay.vnc.enabled"] = "TRUE"
data["remotedisplay.vnc.port"] = fmt.Sprintf("%d", port) data["remotedisplay.vnc.port"] = fmt.Sprintf("%d", port)
data["remotedisplay.vnc.ip"] = address data["remotedisplay.vnc.ip"] = address

View File

@ -9,8 +9,8 @@ import (
// HTTPConfig contains configuration for the local HTTP Server // HTTPConfig contains configuration for the local HTTP Server
type HTTPConfig struct { type HTTPConfig struct {
HTTPDir string `mapstructure:"http_directory"` HTTPDir string `mapstructure:"http_directory"`
HTTPPortMin int `mapstructure:"http_port_min"` HTTPPortMin uint `mapstructure:"http_port_min"`
HTTPPortMax int `mapstructure:"http_port_max"` HTTPPortMax uint `mapstructure:"http_port_max"`
} }
func (c *HTTPConfig) Prepare(ctx *interpolate.Context) []error { func (c *HTTPConfig) Prepare(ctx *interpolate.Context) []error {

View File

@ -7,7 +7,6 @@ import (
"log" "log"
"math/rand" "math/rand"
"net" "net"
"strconv"
"github.com/gofrs/flock" "github.com/gofrs/flock"
@ -25,7 +24,7 @@ type Listener struct {
// Listener can be closed but Port will be file locked by packer until // Listener can be closed but Port will be file locked by packer until
// Close is called. // Close is called.
net.Listener net.Listener
Port int Port uint
Address string Address string
lock *flock.Flock lock *flock.Flock
} }
@ -44,7 +43,7 @@ type ListenRangeConfig struct {
// tcp", "udp" // tcp", "udp"
Network string Network string
Addr string Addr string
Min, Max int Min, Max uint
net.ListenConfig net.ListenConfig
} }
@ -65,9 +64,9 @@ func (lc ListenRangeConfig) Listen(ctx context.Context) (*Listener, error) {
return nil, err return nil, err
} }
port := rand.Intn(portRange) + lc.Min port := uint(rand.Intn(portRange)) + lc.Min
lockFilePath, err := packer.CachePath("port", strconv.Itoa(port)) lockFilePath, err := packer.CachePath("port", fmt.Sprintf("%d", port))
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -23,8 +23,8 @@ import (
// http_port int - The port the HTTP server started on. // http_port int - The port the HTTP server started on.
type StepHTTPServer struct { type StepHTTPServer struct {
HTTPDir string HTTPDir string
HTTPPortMin int HTTPPortMin uint
HTTPPortMax int HTTPPortMax uint
l *net.Listener l *net.Listener
} }

View File

@ -27,7 +27,7 @@ type Communicator struct {
func New(config *Config) (*Communicator, error) { func New(config *Config) (*Communicator, error) {
endpoint := &winrm.Endpoint{ endpoint := &winrm.Endpoint{
Host: config.Host, Host: config.Host,
Port: config.Port, Port: int(config.Port), // it's a uint
HTTPS: config.Https, HTTPS: config.Https,
Insecure: config.Insecure, Insecure: config.Insecure,

View File

@ -9,7 +9,7 @@ import (
// Config is used to configure the WinRM connection // Config is used to configure the WinRM connection
type Config struct { type Config struct {
Host string Host string
Port int Port uint
Username string Username string
Password string Password string
Timeout time.Duration Timeout time.Duration

View File

@ -25,7 +25,7 @@ type Config struct {
// SSH // SSH
SSHHost string `mapstructure:"ssh_host"` SSHHost string `mapstructure:"ssh_host"`
SSHPort int `mapstructure:"ssh_port"` SSHPort uint `mapstructure:"ssh_port"`
SSHUsername string `mapstructure:"ssh_username"` SSHUsername string `mapstructure:"ssh_username"`
SSHPassword string `mapstructure:"ssh_password"` SSHPassword string `mapstructure:"ssh_password"`
SSHKeyPairName string `mapstructure:"ssh_keypair_name"` SSHKeyPairName string `mapstructure:"ssh_keypair_name"`
@ -60,7 +60,7 @@ type Config struct {
WinRMUser string `mapstructure:"winrm_username"` WinRMUser string `mapstructure:"winrm_username"`
WinRMPassword string `mapstructure:"winrm_password"` WinRMPassword string `mapstructure:"winrm_password"`
WinRMHost string `mapstructure:"winrm_host"` WinRMHost string `mapstructure:"winrm_host"`
WinRMPort int `mapstructure:"winrm_port"` WinRMPort uint `mapstructure:"winrm_port"`
WinRMTimeout time.Duration `mapstructure:"winrm_timeout"` WinRMTimeout time.Duration `mapstructure:"winrm_timeout"`
WinRMUseSSL bool `mapstructure:"winrm_use_ssl"` WinRMUseSSL bool `mapstructure:"winrm_use_ssl"`
WinRMInsecure bool `mapstructure:"winrm_insecure"` WinRMInsecure bool `mapstructure:"winrm_insecure"`
@ -150,7 +150,7 @@ func (c *Config) SSHConfigFunc() func(multistep.StateBag) (*ssh.ClientConfig, er
} }
// Port returns the port that will be used for access based on config. // Port returns the port that will be used for access based on config.
func (c *Config) Port() int { func (c *Config) Port() uint {
switch c.Type { switch c.Type {
case "ssh": case "ssh":
return c.SSHPort return c.SSHPort

View File

@ -28,14 +28,14 @@ type StepConnect struct {
// SSHConfig should return the default configuration for // SSHConfig should return the default configuration for
// connecting via SSH. // connecting via SSH.
SSHConfig func(multistep.StateBag) (*gossh.ClientConfig, error) SSHConfig func(multistep.StateBag) (*gossh.ClientConfig, error)
SSHPort func(multistep.StateBag) (int, error) SSHPort func(multistep.StateBag) (uint, error)
// The fields below are callbacks to assist with connecting to WinRM. // The fields below are callbacks to assist with connecting to WinRM.
// //
// WinRMConfig should return the default configuration for // WinRMConfig should return the default configuration for
// connecting via WinRM. // connecting via WinRM.
WinRMConfig func(multistep.StateBag) (*WinRMConfig, error) WinRMConfig func(multistep.StateBag) (*WinRMConfig, error)
WinRMPort func(multistep.StateBag) (int, error) WinRMPort func(multistep.StateBag) (uint, error)
// CustomConnect can be set to have custom connectors for specific // CustomConnect can be set to have custom connectors for specific
// types. These take highest precedence so you can also override // types. These take highest precedence so you can also override

View File

@ -27,7 +27,7 @@ type StepConnectSSH struct {
Config *Config Config *Config
Host func(multistep.StateBag) (string, error) Host func(multistep.StateBag) (string, error)
SSHConfig func(multistep.StateBag) (*gossh.ClientConfig, error) SSHConfig func(multistep.StateBag) (*gossh.ClientConfig, error)
SSHPort func(multistep.StateBag) (int, error) SSHPort func(multistep.StateBag) (uint, error)
} }
func (s *StepConnectSSH) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { func (s *StepConnectSSH) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {

View File

@ -30,7 +30,7 @@ type StepConnectWinRM struct {
Config *Config Config *Config
Host func(multistep.StateBag) (string, error) Host func(multistep.StateBag) (string, error)
WinRMConfig func(multistep.StateBag) (*WinRMConfig, error) WinRMConfig func(multistep.StateBag) (*WinRMConfig, error)
WinRMPort func(multistep.StateBag) (int, error) WinRMPort func(multistep.StateBag) (uint, error)
} }
func (s *StepConnectWinRM) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { func (s *StepConnectWinRM) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {