standardize across both puppet-{masterless,server}

move comments outside of datastructure

remove duplicated section

fix line-endings

Golang doesn't use C-style comments

run gofmt for alignment and whitespace management

remove danling "options" and fix class reference

syncronize tests to new command structure
This commit is contained in:
Matthew Patton 2018-04-27 20:40:13 -04:00
parent 5eb497a2c5
commit 8bb7798ea7
4 changed files with 28 additions and 58 deletions

1
.gitattributes vendored
View File

@ -1,2 +1,3 @@
common/test-fixtures/root/* eol=lf common/test-fixtures/root/* eol=lf
*.go eol=lf

View File

@ -65,12 +65,10 @@ type Config struct {
// If true, packer will ignore all exit-codes from a puppet run // If true, packer will ignore all exit-codes from a puppet run
IgnoreExitCodes bool `mapstructure:"ignore_exit_codes"` IgnoreExitCodes bool `mapstructure:"ignore_exit_codes"`
// The Guest OS Type (unix or windows)
GuestOSType string `mapstructure:"guest_os_type"`
} }
type guestOSTypeConfig struct { type guestOSTypeConfig struct {
tempDir string
stagingDir string stagingDir string
executeCommand string executeCommand string
facterVarsFmt string facterVarsFmt string
@ -78,8 +76,10 @@ type guestOSTypeConfig struct {
modulePathJoiner string modulePathJoiner string
} }
// FIXME assumes both Packer host and target are same OS
var guestOSTypeConfigs = map[string]guestOSTypeConfig{ var guestOSTypeConfigs = map[string]guestOSTypeConfig{
provisioner.UnixOSType: { provisioner.UnixOSType: {
tempDir: "/tmp",
stagingDir: "/tmp/packer-puppet-masterless", stagingDir: "/tmp/packer-puppet-masterless",
executeCommand: "cd {{.WorkingDir}} && " + executeCommand: "cd {{.WorkingDir}} && " +
`{{if ne .FacterVars ""}}{{.FacterVars}} {{end}}` + `{{if ne .FacterVars ""}}{{.FacterVars}} {{end}}` +
@ -97,9 +97,10 @@ var guestOSTypeConfigs = map[string]guestOSTypeConfig{
modulePathJoiner: ":", modulePathJoiner: ":",
}, },
provisioner.WindowsOSType: { provisioner.WindowsOSType: {
stagingDir: "C:/Windows/Temp/packer-puppet-masterless", tempDir: filepath.ToSlash(os.Getenv("TEMP")),
stagingDir: filepath.ToSlash(os.Getenv("SYSTEMROOT")) + "/Temp/packer-puppet-masterless",
executeCommand: "cd {{.WorkingDir}} && " + executeCommand: "cd {{.WorkingDir}} && " +
"{{.FacterVars}} && " + `{{if ne .FacterVars ""}}{{.FacterVars}} && {{end}}` +
`{{if ne .PuppetBinDir ""}}{{.PuppetBinDir}}/{{end}}` + `{{if ne .PuppetBinDir ""}}{{.PuppetBinDir}}/{{end}}` +
"puppet apply --detailed-exitcodes " + "puppet apply --detailed-exitcodes " +
"{{if .Debug}}--debug {{end}}" + "{{if .Debug}}--debug {{end}}" +
@ -121,7 +122,6 @@ type Provisioner struct {
} }
type ExecuteTemplate struct { type ExecuteTemplate struct {
WorkingDir string
FacterVars string FacterVars string
HieraConfigPath string HieraConfigPath string
ModulePath string ModulePath string

View File

@ -53,7 +53,7 @@ func TestGuestOSConfig_empty_unix(t *testing.T) {
} }
expected := "cd /tmp/packer-puppet-masterless && " + expected := "cd /tmp/packer-puppet-masterless && " +
"sudo -E puppet apply --verbose --modulepath='' --detailed-exitcodes /r/m/f" "sudo -E puppet apply --detailed-exitcodes /r/m/f"
assert.Equal(t, expected, command) assert.Equal(t, expected, command)
} }
@ -89,8 +89,8 @@ func TestGuestOSConfig_full_unix(t *testing.T) {
expected := "cd /tmp/packer-puppet-masterless && FACTER_lhs='rhs' FACTER_foo='bar' " + expected := "cd /tmp/packer-puppet-masterless && FACTER_lhs='rhs' FACTER_foo='bar' " +
"sudo -E puppet apply " + "sudo -E puppet apply " +
"--verbose --modulepath='/m/p:/a/b' --hiera_config='/h/c/p' " + "--detailed-exitcodes --modulepath='/m/p:/a/b' --hiera_config='/h/c/p' " +
"--manifestdir='/r/m/d' --detailed-exitcodes /r/m/f" "--manifestdir='/r/m/d' /r/m/f"
assert.Equal(t, expected, command) assert.Equal(t, expected, command)
} }
@ -115,7 +115,7 @@ func TestGuestOSConfig_empty_windows(t *testing.T) {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
expected := "cd C:/Windows/Temp/packer-puppet-masterless && && puppet apply --verbose --modulepath='' --detailed-exitcodes /r/m/f" expected := "cd C:/Windows/Temp/packer-puppet-masterless && puppet apply --detailed-exitcodes /r/m/f"
assert.Equal(t, expected, command) assert.Equal(t, expected, command)
} }
@ -152,8 +152,8 @@ func TestGuestOSConfig_full_windows(t *testing.T) {
expected := "cd C:/Windows/Temp/packer-puppet-masterless && " + expected := "cd C:/Windows/Temp/packer-puppet-masterless && " +
"SET \"FACTER_lhs=rhs\" & SET \"FACTER_foo=bar\" && " + "SET \"FACTER_lhs=rhs\" & SET \"FACTER_foo=bar\" && " +
"puppet apply --verbose --modulepath='/m/p;/a/b' --hiera_config='/h/c/p' " + "puppet apply --detailed-exitcodes --modulepath='/m/p;/a/b' --hiera_config='/h/c/p' " +
"--manifestdir='/r/m/d' --detailed-exitcodes /r/m/f" "--manifestdir='/r/m/d' /r/m/f"
assert.Equal(t, expected, command) assert.Equal(t, expected, command)
} }

View File

@ -5,6 +5,7 @@ package puppetserver
import ( import (
"fmt" "fmt"
"os" "os"
"path/filepath"
"strings" "strings"
"github.com/hashicorp/packer/common" "github.com/hashicorp/packer/common"
@ -14,44 +15,6 @@ import (
"github.com/hashicorp/packer/template/interpolate" "github.com/hashicorp/packer/template/interpolate"
) )
type guestOSTypeConfig struct {
executeCommand string
facterVarsFmt string
facterVarsJoiner string
stagingDir string
}
var guestOSTypeConfigs = map[string]guestOSTypeConfig{
provisioner.UnixOSType: {
executeCommand: "{{.FacterVars}} {{if .Sudo}}sudo -E {{end}}" +
"{{if ne .PuppetBinDir \"\"}}{{.PuppetBinDir}}/{{end}}puppet agent " +
"--onetime --no-daemonize " +
"{{if ne .PuppetServer \"\"}}--server='{{.PuppetServer}}' {{end}}" +
"{{if ne .Options \"\"}}{{.Options}} {{end}}" +
"{{if ne .PuppetNode \"\"}}--certname={{.PuppetNode}} {{end}}" +
"{{if ne .ClientCertPath \"\"}}--certdir='{{.ClientCertPath}}' {{end}}" +
"{{if ne .ClientPrivateKeyPath \"\"}}--privatekeydir='{{.ClientPrivateKeyPath}}' {{end}}" +
"--detailed-exitcodes",
facterVarsFmt: "FACTER_%s='%s'",
facterVarsJoiner: " ",
stagingDir: "/tmp/packer-puppet-server",
},
provisioner.WindowsOSType: {
executeCommand: "{{.FacterVars}} " +
"{{if ne .PuppetBinDir \"\"}}{{.PuppetBinDir}}/{{end}}puppet agent " +
"--onetime --no-daemonize " +
"{{if ne .PuppetServer \"\"}}--server='{{.PuppetServer}}' {{end}}" +
"{{if ne .Options \"\"}}{{.Options}} {{end}}" +
"{{if ne .PuppetNode \"\"}}--certname={{.PuppetNode}} {{end}}" +
"{{if ne .ClientCertPath \"\"}}--certdir='{{.ClientCertPath}}' {{end}}" +
"{{if ne .ClientPrivateKeyPath \"\"}}--privatekeydir='{{.ClientPrivateKeyPath}}' {{end}}" +
"--detailed-exitcodes",
facterVarsFmt: "SET \"FACTER_%s=%s\"",
facterVarsJoiner: " & ",
stagingDir: "C:/Windows/Temp/packer-puppet-server",
},
}
type Config struct { type Config struct {
common.PackerConfig `mapstructure:",squash"` common.PackerConfig `mapstructure:",squash"`
ctx interpolate.Context ctx interpolate.Context
@ -87,6 +50,10 @@ type Config struct {
// permissions in this directory. // permissions in this directory.
StagingDir string `mapstructure:"staging_dir"` StagingDir string `mapstructure:"staging_dir"`
// The directory from which the command will be executed.
// Packer requires the directory to exist when running puppet.
WorkingDir string `mapstructure:"working_directory"`
// The directory that contains the puppet binary. // The directory that contains the puppet binary.
// E.g. if it can't be found on the standard path. // E.g. if it can't be found on the standard path.
PuppetBinDir string `mapstructure:"puppet_bin_dir"` PuppetBinDir string `mapstructure:"puppet_bin_dir"`
@ -103,12 +70,13 @@ type guestOSTypeConfig struct {
facterVarsJoiner string facterVarsJoiner string
} }
// FIXME assumes both Packer host and target are same OS
var guestOSTypeConfigs = map[string]guestOSTypeConfig{ var guestOSTypeConfigs = map[string]guestOSTypeConfig{
provisioner.UnixOSType: { provisioner.UnixOSType: {
tempDir: "/tmp", tempDir: "/tmp",
stagingDir: "/tmp/packer-puppet-server", stagingDir: "/tmp/packer-puppet-server",
executeCommand: "cd {{.WorkingDir}} && " + executeCommand: "cd {{.WorkingDir}} && " +
"{{.FacterVars}}" + `{{if ne .FacterVars ""}}{{.FacterVars}} {{end}}` +
"{{if .Sudo}}sudo -E {{end}}" + "{{if .Sudo}}sudo -E {{end}}" +
`{{if ne .PuppetBinDir ""}}{{.PuppetBinDir}}/{{end}}` + `{{if ne .PuppetBinDir ""}}{{.PuppetBinDir}}/{{end}}` +
"puppet agent --onetime --no-daemonize --detailed-exitcodes " + "puppet agent --onetime --no-daemonize --detailed-exitcodes " +
@ -122,10 +90,10 @@ var guestOSTypeConfigs = map[string]guestOSTypeConfig{
facterVarsJoiner: " ", facterVarsJoiner: " ",
}, },
provisioner.WindowsOSType: { provisioner.WindowsOSType: {
tempDir: path.filepath.ToSlash(os.Getenv("TEMP")), tempDir: filepath.ToSlash(os.Getenv("TEMP")),
stagingDir: path.filepath.ToSlash(os.Getenv("SYSTEMROOT")) + "/Temp/packer-puppet-server", stagingDir: filepath.ToSlash(os.Getenv("SYSTEMROOT")) + "/Temp/packer-puppet-server",
executeCommand: "cd {{.WorkingDir}} && " + executeCommand: "cd {{.WorkingDir}} && " +
"{{.FacterVars}} " + `{{if ne .FacterVars ""}}{{.FacterVars}} && {{end}}` +
`{{if ne .PuppetBinDir ""}}{{.PuppetBinDir}}/{{end}}` + `{{if ne .PuppetBinDir ""}}{{.PuppetBinDir}}/{{end}}` +
"puppet agent --onetime --no-daemonize --detailed-exitcodes " + "puppet agent --onetime --no-daemonize --detailed-exitcodes " +
"{{if .Debug}}--debug {{end}}" + "{{if .Debug}}--debug {{end}}" +
@ -151,9 +119,11 @@ type ExecuteTemplate struct {
ClientPrivateKeyPath string ClientPrivateKeyPath string
PuppetNode string PuppetNode string
PuppetServer string PuppetServer string
ExtraArguments string
PuppetBinDir string PuppetBinDir string
Sudo bool Sudo bool
WorkingDir string
Debug bool
ExtraArguments string
} }
func (p *Provisioner) Prepare(raws ...interface{}) error { func (p *Provisioner) Prepare(raws ...interface{}) error {
@ -274,7 +244,6 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
ClientPrivateKeyPath: remoteClientPrivateKeyPath, ClientPrivateKeyPath: remoteClientPrivateKeyPath,
PuppetNode: p.config.PuppetNode, PuppetNode: p.config.PuppetNode,
PuppetServer: p.config.PuppetServer, PuppetServer: p.config.PuppetServer,
Options: p.config.Options,
PuppetBinDir: p.config.PuppetBinDir, PuppetBinDir: p.config.PuppetBinDir,
Sudo: !p.config.PreventSudo, Sudo: !p.config.PreventSudo,
WorkingDir: p.config.WorkingDir, WorkingDir: p.config.WorkingDir,