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.
This commit is contained in:
Megan Marsh 2020-11-09 03:16:44 -08:00 committed by GitHub
parent b7568f5a4d
commit cd74456026
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 47 additions and 43 deletions

View File

@ -1,4 +1,4 @@
package provisioner
package guestexec
import (
"bytes"

View File

@ -1,4 +1,4 @@
package provisioner
package guestexec
import (
"regexp"

View File

@ -1,4 +1,4 @@
package provisioner
package guestexec
import (
"fmt"

View File

@ -1,4 +1,4 @@
package provisioner
package guestexec
import (
"testing"

View File

@ -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

View File

@ -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

View File

@ -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
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}