From cd74456026fba0ce645ea5ae73a30789faec6650 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Mon, 9 Nov 2020 03:16:44 -0800 Subject: [PATCH] Common provisioner helpers (#10229) * update docstrings to make it clear that plugins are servers and core is client * move provisioner guest helper functions into common dir. --- {provisioner => common/guestexec}/elevated.go | 2 +- {provisioner => common/guestexec}/elevated_test.go | 2 +- .../guestexec}/guest_commands.go | 2 +- .../guestexec}/guest_commands_test.go | 2 +- packer/rpc/client.go | 6 ++++-- packer/rpc/server.go | 4 +++- provisioner/chef-client/provisioner.go | 14 +++++++------- provisioner/chef-solo/provisioner.go | 12 ++++++------ provisioner/powershell/provisioner.go | 4 ++-- provisioner/puppet-masterless/provisioner.go | 14 +++++++------- provisioner/puppet-server/provisioner.go | 14 +++++++------- provisioner/salt-masterless/provisioner.go | 14 +++++++------- 12 files changed, 47 insertions(+), 43 deletions(-) rename {provisioner => common/guestexec}/elevated.go (99%) rename {provisioner => common/guestexec}/elevated_test.go (97%) rename {provisioner => common/guestexec}/guest_commands.go (99%) rename {provisioner => common/guestexec}/guest_commands_test.go (99%) diff --git a/provisioner/elevated.go b/common/guestexec/elevated.go similarity index 99% rename from provisioner/elevated.go rename to common/guestexec/elevated.go index 71addb563..dd46a450f 100644 --- a/provisioner/elevated.go +++ b/common/guestexec/elevated.go @@ -1,4 +1,4 @@ -package provisioner +package guestexec import ( "bytes" diff --git a/provisioner/elevated_test.go b/common/guestexec/elevated_test.go similarity index 97% rename from provisioner/elevated_test.go rename to common/guestexec/elevated_test.go index ac8759ef2..c9d44e817 100644 --- a/provisioner/elevated_test.go +++ b/common/guestexec/elevated_test.go @@ -1,4 +1,4 @@ -package provisioner +package guestexec import ( "regexp" diff --git a/provisioner/guest_commands.go b/common/guestexec/guest_commands.go similarity index 99% rename from provisioner/guest_commands.go rename to common/guestexec/guest_commands.go index ca2b238a4..549ba3ccb 100644 --- a/provisioner/guest_commands.go +++ b/common/guestexec/guest_commands.go @@ -1,4 +1,4 @@ -package provisioner +package guestexec import ( "fmt" diff --git a/provisioner/guest_commands_test.go b/common/guestexec/guest_commands_test.go similarity index 99% rename from provisioner/guest_commands_test.go rename to common/guestexec/guest_commands_test.go index ffbad4bbd..d6300c8e3 100644 --- a/provisioner/guest_commands_test.go +++ b/common/guestexec/guest_commands_test.go @@ -1,4 +1,4 @@ -package provisioner +package guestexec import ( "testing" diff --git a/packer/rpc/client.go b/packer/rpc/client.go index e8eadfbf5..a23437a72 100644 --- a/packer/rpc/client.go +++ b/packer/rpc/client.go @@ -10,8 +10,10 @@ import ( ) // Client is the client end that communicates with a Packer RPC server. -// Establishing a connection is up to the user, the Client can just -// communicate over any ReadWriteCloser. +// Establishing a connection is up to the user. The Client can communicate over +// any ReadWriteCloser. In Packer, each "plugin" (builder, provisioner, +// and post-processor) creates and launches a server. The the packer "core" +// creates and uses the client. type Client struct { mux *muxBroker client *rpc.Client diff --git a/packer/rpc/server.go b/packer/rpc/server.go index b583e2945..acfea8c7c 100644 --- a/packer/rpc/server.go +++ b/packer/rpc/server.go @@ -23,7 +23,9 @@ const ( ) // Server represents an RPC server for Packer. This must be paired on -// the other side with a Client. +// the other side with a Client. In Packer, each "plugin" (builder, provisioner, +// and post-processor) creates and launches a server. The client created and +// used by the packer "core" type Server struct { mux *muxBroker streamId uint32 diff --git a/provisioner/chef-client/provisioner.go b/provisioner/chef-client/provisioner.go index 60f13fa14..3d953aef8 100644 --- a/provisioner/chef-client/provisioner.go +++ b/provisioner/chef-client/provisioner.go @@ -17,10 +17,10 @@ import ( "github.com/hashicorp/hcl/v2/hcldec" "github.com/hashicorp/packer/common" + "github.com/hashicorp/packer/common/guestexec" "github.com/hashicorp/packer/common/uuid" "github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/packer" - "github.com/hashicorp/packer/provisioner" "github.com/hashicorp/packer/template/interpolate" ) @@ -32,13 +32,13 @@ type guestOSTypeConfig struct { } var guestOSTypeConfigs = map[string]guestOSTypeConfig{ - provisioner.UnixOSType: { + guestexec.UnixOSType: { executeCommand: "{{if .Sudo}}sudo {{end}}chef-client --no-color -c {{.ConfigPath}} -j {{.JsonPath}}", installCommand: "curl -L https://omnitruck.chef.io/install.sh | {{if .Sudo}}sudo {{end}}bash -s --{{if .Version}} -v {{.Version}}{{end}}", knifeCommand: "{{if .Sudo}}sudo {{end}}knife {{.Args}} {{.Flags}}", stagingDir: "/tmp/packer-chef-client", }, - provisioner.WindowsOSType: { + guestexec.WindowsOSType: { executeCommand: "c:/opscode/chef/bin/chef-client.bat --no-color -c {{.ConfigPath}} -j {{.JsonPath}}", installCommand: "powershell.exe -Command \". { iwr -useb https://omnitruck.chef.io/install.ps1 } | iex; Install-Project{{if .Version}} -version {{.Version}}{{end}}\"", knifeCommand: "c:/opscode/chef/bin/knife.bat {{.Args}} {{.Flags}}", @@ -86,7 +86,7 @@ type Provisioner struct { config Config communicator packer.Communicator guestOSTypeConfig guestOSTypeConfig - guestCommands *provisioner.GuestCommands + guestCommands *guestexec.GuestCommands generatedData map[string]interface{} } @@ -142,7 +142,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { } if p.config.GuestOSType == "" { - p.config.GuestOSType = provisioner.DefaultOSType + p.config.GuestOSType = guestexec.DefaultOSType } p.config.GuestOSType = strings.ToLower(p.config.GuestOSType) @@ -152,7 +152,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { return fmt.Errorf("Invalid guest_os_type: \"%s\"", p.config.GuestOSType) } - p.guestCommands, err = provisioner.NewGuestCommands(p.config.GuestOSType, !p.config.PreventSudo) + p.guestCommands, err = guestexec.NewGuestCommands(p.config.GuestOSType, !p.config.PreventSudo) if err != nil { return fmt.Errorf("Invalid guest_os_type: \"%s\"", p.config.GuestOSType) } @@ -581,7 +581,7 @@ func (p *Provisioner) executeChef(ui packer.Ui, comm packer.Communicator, config } if p.config.ElevatedUser != "" { - command, err = provisioner.GenerateElevatedRunner(command, p) + command, err = guestexec.GenerateElevatedRunner(command, p) if err != nil { return err } diff --git a/provisioner/chef-solo/provisioner.go b/provisioner/chef-solo/provisioner.go index 402ff73b9..cc9178794 100644 --- a/provisioner/chef-solo/provisioner.go +++ b/provisioner/chef-solo/provisioner.go @@ -17,9 +17,9 @@ import ( "github.com/hashicorp/hcl/v2/hcldec" "github.com/hashicorp/packer/common" + "github.com/hashicorp/packer/common/guestexec" "github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/packer" - "github.com/hashicorp/packer/provisioner" "github.com/hashicorp/packer/template/interpolate" ) @@ -30,12 +30,12 @@ type guestOSTypeConfig struct { } var guestOSTypeConfigs = map[string]guestOSTypeConfig{ - provisioner.UnixOSType: { + guestexec.UnixOSType: { executeCommand: "{{if .Sudo}}sudo {{end}}chef-solo --no-color -c {{.ConfigPath}} -j {{.JsonPath}}", installCommand: "curl -L https://omnitruck.chef.io/install.sh | {{if .Sudo}}sudo {{end}}bash -s --{{if .Version}} -v {{.Version}}{{end}}", stagingDir: "/tmp/packer-chef-solo", }, - provisioner.WindowsOSType: { + guestexec.WindowsOSType: { executeCommand: "c:/opscode/chef/bin/chef-solo.bat --no-color -c {{.ConfigPath}} -j {{.JsonPath}}", installCommand: "powershell.exe -Command \". { iwr -useb https://omnitruck.chef.io/install.ps1 } | iex; Install-Project{{if .Version}} -version {{.Version}}{{end}}\"", stagingDir: "C:/Windows/Temp/packer-chef-solo", @@ -70,7 +70,7 @@ type Config struct { type Provisioner struct { config Config guestOSTypeConfig guestOSTypeConfig - guestCommands *provisioner.GuestCommands + guestCommands *guestexec.GuestCommands } type ConfigTemplate struct { @@ -121,7 +121,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { } if p.config.GuestOSType == "" { - p.config.GuestOSType = provisioner.DefaultOSType + p.config.GuestOSType = guestexec.DefaultOSType } p.config.GuestOSType = strings.ToLower(p.config.GuestOSType) @@ -131,7 +131,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { return fmt.Errorf("Invalid guest_os_type: \"%s\"", p.config.GuestOSType) } - p.guestCommands, err = provisioner.NewGuestCommands(p.config.GuestOSType, !p.config.PreventSudo) + p.guestCommands, err = guestexec.NewGuestCommands(p.config.GuestOSType, !p.config.PreventSudo) if err != nil { return fmt.Errorf("Invalid guest_os_type: \"%s\"", p.config.GuestOSType) } diff --git a/provisioner/powershell/provisioner.go b/provisioner/powershell/provisioner.go index e7524b79a..fd3f34fda 100644 --- a/provisioner/powershell/provisioner.go +++ b/provisioner/powershell/provisioner.go @@ -18,13 +18,13 @@ import ( "github.com/hashicorp/hcl/v2/hcldec" "github.com/hashicorp/packer/common" + "github.com/hashicorp/packer/common/guestexec" "github.com/hashicorp/packer/common/retry" "github.com/hashicorp/packer/common/shell" "github.com/hashicorp/packer/common/uuid" "github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer/tmp" - "github.com/hashicorp/packer/provisioner" "github.com/hashicorp/packer/template/interpolate" ) @@ -522,7 +522,7 @@ func (p *Provisioner) createCommandTextPrivileged() (command string, err error) return "", fmt.Errorf("Error processing command: %s", err) } - command, err = provisioner.GenerateElevatedRunner(command, p) + command, err = guestexec.GenerateElevatedRunner(command, p) if err != nil { return "", fmt.Errorf("Error generating elevated runner: %s", err) } diff --git a/provisioner/puppet-masterless/provisioner.go b/provisioner/puppet-masterless/provisioner.go index 1c1307edd..a398a4204 100644 --- a/provisioner/puppet-masterless/provisioner.go +++ b/provisioner/puppet-masterless/provisioner.go @@ -14,9 +14,9 @@ import ( "github.com/hashicorp/hcl/v2/hcldec" "github.com/hashicorp/packer/common" + "github.com/hashicorp/packer/common/guestexec" "github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/packer" - "github.com/hashicorp/packer/provisioner" "github.com/hashicorp/packer/template/interpolate" ) @@ -88,7 +88,7 @@ type guestOSTypeConfig struct { // FIXME assumes both Packer host and target are same OS var guestOSTypeConfigs = map[string]guestOSTypeConfig{ - provisioner.UnixOSType: { + guestexec.UnixOSType: { tempDir: "/tmp", stagingDir: "/tmp/packer-puppet-masterless", executeCommand: "cd {{.WorkingDir}} && " + @@ -106,7 +106,7 @@ var guestOSTypeConfigs = map[string]guestOSTypeConfig{ facterVarsJoiner: " ", modulePathJoiner: ":", }, - provisioner.WindowsOSType: { + guestexec.WindowsOSType: { tempDir: filepath.ToSlash(os.Getenv("TEMP")), stagingDir: filepath.ToSlash(os.Getenv("SYSTEMROOT")) + "/Temp/packer-puppet-masterless", executeCommand: "cd {{.WorkingDir}} && " + @@ -129,7 +129,7 @@ type Provisioner struct { config Config communicator packer.Communicator guestOSTypeConfig guestOSTypeConfig - guestCommands *provisioner.GuestCommands + guestCommands *guestexec.GuestCommands generatedData map[string]interface{} } @@ -171,7 +171,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { // Set some defaults if p.config.GuestOSType == "" { - p.config.GuestOSType = provisioner.DefaultOSType + p.config.GuestOSType = guestexec.DefaultOSType } p.config.GuestOSType = strings.ToLower(p.config.GuestOSType) @@ -181,7 +181,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { return fmt.Errorf("Invalid guest_os_type: \"%s\"", p.config.GuestOSType) } - p.guestCommands, err = provisioner.NewGuestCommands(p.config.GuestOSType, !p.config.PreventSudo) + p.guestCommands, err = guestexec.NewGuestCommands(p.config.GuestOSType, !p.config.PreventSudo) if err != nil { return fmt.Errorf("Invalid guest_os_type: \"%s\"", p.config.GuestOSType) } @@ -338,7 +338,7 @@ func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.C } if p.config.ElevatedUser != "" { - command, err = provisioner.GenerateElevatedRunner(command, p) + command, err = guestexec.GenerateElevatedRunner(command, p) if err != nil { return err } diff --git a/provisioner/puppet-server/provisioner.go b/provisioner/puppet-server/provisioner.go index 6025e7998..c4625f508 100644 --- a/provisioner/puppet-server/provisioner.go +++ b/provisioner/puppet-server/provisioner.go @@ -13,9 +13,9 @@ import ( "github.com/hashicorp/hcl/v2/hcldec" "github.com/hashicorp/packer/common" + "github.com/hashicorp/packer/common/guestexec" "github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/packer" - "github.com/hashicorp/packer/provisioner" "github.com/hashicorp/packer/template/interpolate" ) @@ -85,7 +85,7 @@ type guestOSTypeConfig struct { // FIXME assumes both Packer host and target are same OS var guestOSTypeConfigs = map[string]guestOSTypeConfig{ - provisioner.UnixOSType: { + guestexec.UnixOSType: { tempDir: "/tmp", stagingDir: "/tmp/packer-puppet-server", executeCommand: "cd {{.WorkingDir}} && " + @@ -102,7 +102,7 @@ var guestOSTypeConfigs = map[string]guestOSTypeConfig{ facterVarsFmt: "FACTER_%s='%s'", facterVarsJoiner: " ", }, - provisioner.WindowsOSType: { + guestexec.WindowsOSType: { tempDir: filepath.ToSlash(os.Getenv("TEMP")), stagingDir: filepath.ToSlash(os.Getenv("SYSTEMROOT")) + "/Temp/packer-puppet-server", executeCommand: "cd {{.WorkingDir}} && " + @@ -124,7 +124,7 @@ type Provisioner struct { config Config communicator packer.Communicator guestOSTypeConfig guestOSTypeConfig - guestCommands *provisioner.GuestCommands + guestCommands *guestexec.GuestCommands generatedData map[string]interface{} } @@ -164,7 +164,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { } if p.config.GuestOSType == "" { - p.config.GuestOSType = provisioner.DefaultOSType + p.config.GuestOSType = guestexec.DefaultOSType } p.config.GuestOSType = strings.ToLower(p.config.GuestOSType) @@ -174,7 +174,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { return fmt.Errorf("Invalid guest_os_type: \"%s\"", p.config.GuestOSType) } - p.guestCommands, err = provisioner.NewGuestCommands(p.config.GuestOSType, !p.config.PreventSudo) + p.guestCommands, err = guestexec.NewGuestCommands(p.config.GuestOSType, !p.config.PreventSudo) if err != nil { return fmt.Errorf("Invalid guest_os_type: \"%s\"", p.config.GuestOSType) } @@ -291,7 +291,7 @@ func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.C } if p.config.ElevatedUser != "" { - command, err = provisioner.GenerateElevatedRunner(command, p) + command, err = guestexec.GenerateElevatedRunner(command, p) if err != nil { return err } diff --git a/provisioner/salt-masterless/provisioner.go b/provisioner/salt-masterless/provisioner.go index d343e3ef5..2e12b1af5 100644 --- a/provisioner/salt-masterless/provisioner.go +++ b/provisioner/salt-masterless/provisioner.go @@ -17,9 +17,9 @@ import ( "github.com/hashicorp/go-getter/v2" "github.com/hashicorp/hcl/v2/hcldec" "github.com/hashicorp/packer/common" + "github.com/hashicorp/packer/common/guestexec" "github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/packer" - "github.com/hashicorp/packer/provisioner" "github.com/hashicorp/packer/template/interpolate" ) @@ -83,7 +83,7 @@ type Config struct { type Provisioner struct { config Config guestOSTypeConfig guestOSTypeConfig - guestCommands *provisioner.GuestCommands + guestCommands *guestexec.GuestCommands } type guestOSTypeConfig struct { @@ -96,7 +96,7 @@ type guestOSTypeConfig struct { } var guestOSTypeConfigs = map[string]guestOSTypeConfig{ - provisioner.UnixOSType: { + guestexec.UnixOSType: { configDir: "/etc/salt", tempDir: "/tmp/salt", stateRoot: "/srv/salt", @@ -104,7 +104,7 @@ var guestOSTypeConfigs = map[string]guestOSTypeConfig{ bootstrapFetchCmd: "curl -L https://bootstrap.saltstack.com -o /tmp/install_salt.sh || wget -O /tmp/install_salt.sh https://bootstrap.saltstack.com", bootstrapRunCmd: "sh /tmp/install_salt.sh", }, - provisioner.WindowsOSType: { + guestexec.WindowsOSType: { configDir: "C:/salt/conf", tempDir: "C:/Windows/Temp/salt/", stateRoot: "C:/salt/state", @@ -130,7 +130,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { } if p.config.GuestOSType == "" { - p.config.GuestOSType = provisioner.DefaultOSType + p.config.GuestOSType = guestexec.DefaultOSType } else { p.config.GuestOSType = strings.ToLower(p.config.GuestOSType) } @@ -141,7 +141,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { return fmt.Errorf("Invalid guest_os_type: \"%s\"", p.config.GuestOSType) } - p.guestCommands, err = provisioner.NewGuestCommands(p.config.GuestOSType, !p.config.DisableSudo) + p.guestCommands, err = guestexec.NewGuestCommands(p.config.GuestOSType, !p.config.DisableSudo) if err != nil { return fmt.Errorf("Invalid guest_os_type: \"%s\"", p.config.GuestOSType) } @@ -413,7 +413,7 @@ func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.C // Prepends sudo to supplied command if config says to func (p *Provisioner) sudo(cmd string) string { - if p.config.DisableSudo || (p.config.GuestOSType == provisioner.WindowsOSType) { + if p.config.DisableSudo || (p.config.GuestOSType == guestexec.WindowsOSType) { return cmd }