2017-01-13 02:51:36 -05:00
|
|
|
// Package puppetmasterless implements a provisioner for Packer that executes
|
2013-09-08 02:08:56 -04:00
|
|
|
// Puppet on the remote machine, configured to apply a local manifest
|
|
|
|
// versus connecting to a Puppet master.
|
2013-09-08 01:27:25 -04:00
|
|
|
package puppetmasterless
|
2013-08-01 20:05:23 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2015-05-27 17:50:20 -04:00
|
|
|
|
2017-08-14 23:01:24 -04:00
|
|
|
"github.com/hashicorp/packer/common"
|
2018-12-07 06:08:11 -05:00
|
|
|
commonhelper "github.com/hashicorp/packer/helper/common"
|
2017-08-14 23:01:24 -04:00
|
|
|
"github.com/hashicorp/packer/helper/config"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/provisioner"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2013-08-01 20:05:23 -04:00
|
|
|
)
|
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
2015-05-27 17:50:20 -04:00
|
|
|
ctx interpolate.Context
|
2013-08-01 20:05:23 -04:00
|
|
|
|
2018-05-01 16:38:01 -04:00
|
|
|
// If true, staging directory is removed after executing puppet.
|
|
|
|
CleanStagingDir bool `mapstructure:"clean_staging_directory"`
|
|
|
|
|
|
|
|
// The Guest OS Type (unix or windows)
|
|
|
|
GuestOSType string `mapstructure:"guest_os_type"`
|
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
// The command used to execute Puppet.
|
|
|
|
ExecuteCommand string `mapstructure:"execute_command"`
|
2013-08-01 20:05:23 -04:00
|
|
|
|
2015-11-03 17:55:03 -05:00
|
|
|
// Additional arguments to pass when executing Puppet
|
|
|
|
ExtraArguments []string `mapstructure:"extra_arguments"`
|
2015-11-03 12:30:55 -05:00
|
|
|
|
2013-09-09 01:59:43 -04:00
|
|
|
// Additional facts to set when executing Puppet
|
|
|
|
Facter map[string]string
|
|
|
|
|
2013-09-09 16:24:17 -04:00
|
|
|
// Path to a hiera configuration file to upload and use.
|
|
|
|
HieraConfigPath string `mapstructure:"hiera_config_path"`
|
|
|
|
|
2018-05-01 16:38:01 -04:00
|
|
|
// If true, packer will ignore all exit-codes from a puppet run
|
|
|
|
IgnoreExitCodes bool `mapstructure:"ignore_exit_codes"`
|
|
|
|
|
2013-08-01 20:05:23 -04:00
|
|
|
// An array of local paths of modules to upload.
|
2013-09-08 02:08:56 -04:00
|
|
|
ModulePaths []string `mapstructure:"module_paths"`
|
2013-08-01 20:05:23 -04:00
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
// The main manifest file to apply to kick off the entire thing.
|
2013-08-01 20:05:23 -04:00
|
|
|
ManifestFile string `mapstructure:"manifest_file"`
|
|
|
|
|
2013-12-11 14:20:22 -05:00
|
|
|
// A directory of manifest files that will be uploaded to the remote
|
|
|
|
// machine.
|
2013-11-25 02:52:41 -05:00
|
|
|
ManifestDir string `mapstructure:"manifest_dir"`
|
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
// If true, `sudo` will NOT be used to execute Puppet.
|
2013-08-01 20:05:23 -04:00
|
|
|
PreventSudo bool `mapstructure:"prevent_sudo"`
|
2013-09-08 02:08:56 -04:00
|
|
|
|
2018-05-01 17:16:47 -04:00
|
|
|
// The directory that contains the puppet binary.
|
2018-05-01 16:38:01 -04:00
|
|
|
// E.g. if it can't be found on the standard path.
|
|
|
|
PuppetBinDir string `mapstructure:"puppet_bin_dir"`
|
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
// The directory where files will be uploaded. Packer requires write
|
|
|
|
// permissions in this directory.
|
|
|
|
StagingDir string `mapstructure:"staging_directory"`
|
2015-01-10 18:25:48 -05:00
|
|
|
|
|
|
|
// The directory from which the command will be executed.
|
|
|
|
// Packer requires the directory to exist when running puppet.
|
|
|
|
WorkingDir string `mapstructure:"working_directory"`
|
2018-12-07 06:08:11 -05:00
|
|
|
|
|
|
|
// Instructs the communicator to run the remote script as a Windows
|
|
|
|
// scheduled task, effectively elevating the remote user by impersonating
|
|
|
|
// a logged-in user
|
|
|
|
ElevatedUser string `mapstructure:"elevated_user"`
|
|
|
|
ElevatedPassword string `mapstructure:"elevated_password"`
|
2016-05-02 11:17:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type guestOSTypeConfig struct {
|
2017-08-21 00:28:58 -04:00
|
|
|
executeCommand string
|
|
|
|
facterVarsFmt string
|
2017-09-13 21:35:54 -04:00
|
|
|
facterVarsJoiner string
|
2017-08-21 00:28:58 -04:00
|
|
|
modulePathJoiner string
|
2018-05-01 16:38:01 -04:00
|
|
|
stagingDir string
|
|
|
|
tempDir string
|
2016-05-02 11:17:04 -04:00
|
|
|
}
|
|
|
|
|
2018-04-27 20:40:13 -04:00
|
|
|
// FIXME assumes both Packer host and target are same OS
|
2016-05-02 11:17:04 -04:00
|
|
|
var guestOSTypeConfigs = map[string]guestOSTypeConfig{
|
2017-01-13 03:11:18 -05:00
|
|
|
provisioner.UnixOSType: {
|
2018-04-27 20:40:13 -04:00
|
|
|
tempDir: "/tmp",
|
2016-05-02 11:17:04 -04:00
|
|
|
stagingDir: "/tmp/packer-puppet-masterless",
|
|
|
|
executeCommand: "cd {{.WorkingDir}} && " +
|
2017-10-03 14:39:33 -04:00
|
|
|
`{{if ne .FacterVars ""}}{{.FacterVars}} {{end}}` +
|
|
|
|
"{{if .Sudo}}sudo -E {{end}}" +
|
|
|
|
`{{if ne .PuppetBinDir ""}}{{.PuppetBinDir}}/{{end}}` +
|
2017-01-31 05:09:04 -05:00
|
|
|
"puppet apply --detailed-exitcodes " +
|
|
|
|
"{{if .Debug}}--debug {{end}}" +
|
|
|
|
`{{if ne .ModulePath ""}}--modulepath='{{.ModulePath}}' {{end}}` +
|
2017-10-03 14:39:33 -04:00
|
|
|
`{{if ne .HieraConfigPath ""}}--hiera_config='{{.HieraConfigPath}}' {{end}}` +
|
|
|
|
`{{if ne .ManifestDir ""}}--manifestdir='{{.ManifestDir}}' {{end}}` +
|
2018-05-08 10:12:22 -04:00
|
|
|
`{{if ne .ExtraArguments ""}}{{.ExtraArguments}} {{end}}` +
|
2016-05-02 11:17:04 -04:00
|
|
|
"{{.ManifestFile}}",
|
2017-08-21 00:28:58 -04:00
|
|
|
facterVarsFmt: "FACTER_%s='%s'",
|
2017-09-13 21:35:54 -04:00
|
|
|
facterVarsJoiner: " ",
|
2017-08-21 00:28:58 -04:00
|
|
|
modulePathJoiner: ":",
|
2016-05-02 11:17:04 -04:00
|
|
|
},
|
2017-01-13 03:11:18 -05:00
|
|
|
provisioner.WindowsOSType: {
|
2018-04-27 20:40:13 -04:00
|
|
|
tempDir: filepath.ToSlash(os.Getenv("TEMP")),
|
|
|
|
stagingDir: filepath.ToSlash(os.Getenv("SYSTEMROOT")) + "/Temp/packer-puppet-masterless",
|
2016-05-02 11:17:04 -04:00
|
|
|
executeCommand: "cd {{.WorkingDir}} && " +
|
2018-04-27 20:40:13 -04:00
|
|
|
`{{if ne .FacterVars ""}}{{.FacterVars}} && {{end}}` +
|
2017-10-03 14:39:33 -04:00
|
|
|
`{{if ne .PuppetBinDir ""}}{{.PuppetBinDir}}/{{end}}` +
|
2017-01-31 05:09:04 -05:00
|
|
|
"puppet apply --detailed-exitcodes " +
|
|
|
|
"{{if .Debug}}--debug {{end}}" +
|
|
|
|
`{{if ne .ModulePath ""}}--modulepath='{{.ModulePath}}' {{end}}` +
|
2017-10-03 14:39:33 -04:00
|
|
|
`{{if ne .HieraConfigPath ""}}--hiera_config='{{.HieraConfigPath}}' {{end}}` +
|
|
|
|
`{{if ne .ManifestDir ""}}--manifestdir='{{.ManifestDir}}' {{end}}` +
|
2018-05-08 10:12:22 -04:00
|
|
|
`{{if ne .ExtraArguments ""}}{{.ExtraArguments}} {{end}}` +
|
2016-05-02 11:17:04 -04:00
|
|
|
"{{.ManifestFile}}",
|
2017-10-03 14:39:33 -04:00
|
|
|
facterVarsFmt: `SET "FACTER_%s=%s"`,
|
2017-09-13 21:35:54 -04:00
|
|
|
facterVarsJoiner: " & ",
|
2017-08-21 00:28:58 -04:00
|
|
|
modulePathJoiner: ";",
|
2016-05-02 11:17:04 -04:00
|
|
|
},
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Provisioner struct {
|
2016-05-02 11:17:04 -04:00
|
|
|
config Config
|
2018-12-07 06:08:11 -05:00
|
|
|
communicator packer.Communicator
|
2016-05-02 11:17:04 -04:00
|
|
|
guestOSTypeConfig guestOSTypeConfig
|
|
|
|
guestCommands *provisioner.GuestCommands
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
type ExecuteTemplate struct {
|
2018-05-08 12:44:15 -04:00
|
|
|
Debug bool
|
|
|
|
ExtraArguments string
|
|
|
|
FacterVars string
|
|
|
|
HieraConfigPath string
|
|
|
|
ModulePath string
|
2018-05-08 12:21:04 -04:00
|
|
|
ModulePathJoiner string
|
2018-05-08 12:44:15 -04:00
|
|
|
ManifestFile string
|
|
|
|
ManifestDir string
|
|
|
|
PuppetBinDir string
|
|
|
|
Sudo bool
|
|
|
|
WorkingDir string
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
|
2018-12-07 06:08:11 -05:00
|
|
|
type EnvVarsTemplate struct {
|
|
|
|
WinRMPassword string
|
|
|
|
}
|
|
|
|
|
2013-08-01 20:05:23 -04:00
|
|
|
func (p *Provisioner) Prepare(raws ...interface{}) error {
|
2018-12-07 06:08:11 -05:00
|
|
|
// Create passthrough for winrm password so we can fill it in once we know
|
|
|
|
// it
|
|
|
|
p.config.ctx.Data = &EnvVarsTemplate{
|
|
|
|
WinRMPassword: `{{.WinRMPassword}}`,
|
|
|
|
}
|
|
|
|
|
2015-05-27 17:50:20 -04:00
|
|
|
err := config.Decode(&p.config, &config.DecodeOpts{
|
2015-06-22 15:26:54 -04:00
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &p.config.ctx,
|
2015-05-27 17:50:20 -04:00
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{
|
|
|
|
"execute_command",
|
2017-01-31 05:09:04 -05:00
|
|
|
"extra_arguments",
|
2015-05-27 17:50:20 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, raws...)
|
2013-09-08 02:08:56 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set some defaults
|
2017-08-21 00:28:58 -04:00
|
|
|
if p.config.GuestOSType == "" {
|
|
|
|
p.config.GuestOSType = provisioner.DefaultOSType
|
|
|
|
}
|
|
|
|
p.config.GuestOSType = strings.ToLower(p.config.GuestOSType)
|
|
|
|
|
|
|
|
var ok bool
|
|
|
|
p.guestOSTypeConfig, ok = guestOSTypeConfigs[p.config.GuestOSType]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Invalid guest_os_type: \"%s\"", p.config.GuestOSType)
|
|
|
|
}
|
|
|
|
|
|
|
|
p.guestCommands, err = provisioner.NewGuestCommands(p.config.GuestOSType, !p.config.PreventSudo)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Invalid guest_os_type: \"%s\"", p.config.GuestOSType)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.ExecuteCommand == "" {
|
|
|
|
p.config.ExecuteCommand = p.guestOSTypeConfig.executeCommand
|
|
|
|
}
|
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
if p.config.StagingDir == "" {
|
2017-08-21 00:28:58 -04:00
|
|
|
p.config.StagingDir = p.guestOSTypeConfig.stagingDir
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
|
2015-01-10 18:25:48 -05:00
|
|
|
if p.config.WorkingDir == "" {
|
2015-01-10 18:29:01 -05:00
|
|
|
p.config.WorkingDir = p.config.StagingDir
|
2015-01-10 18:25:48 -05:00
|
|
|
}
|
|
|
|
|
2015-06-15 18:29:23 -04:00
|
|
|
if p.config.Facter == nil {
|
|
|
|
p.config.Facter = make(map[string]string)
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
2015-06-15 18:29:23 -04:00
|
|
|
p.config.Facter["packer_build_name"] = p.config.PackerBuildName
|
|
|
|
p.config.Facter["packer_builder_type"] = p.config.PackerBuilderType
|
2013-09-09 01:59:43 -04:00
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
// Validation
|
2015-05-27 17:50:20 -04:00
|
|
|
var errs *packer.MultiError
|
2013-09-09 16:24:17 -04:00
|
|
|
if p.config.HieraConfigPath != "" {
|
2013-12-06 22:30:06 -05:00
|
|
|
info, err := os.Stat(p.config.HieraConfigPath)
|
2013-09-09 16:24:17 -04:00
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("hiera_config_path is invalid: %s", err))
|
|
|
|
} else if info.IsDir() {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("hiera_config_path must point to a file"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-11 14:21:51 -05:00
|
|
|
if p.config.ManifestDir != "" {
|
|
|
|
info, err := os.Stat(p.config.ManifestDir)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("manifest_dir is invalid: %s", err))
|
|
|
|
} else if !info.IsDir() {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("manifest_dir must point to a directory"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
if p.config.ManifestFile == "" {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("A manifest_file must be specified."))
|
|
|
|
} else {
|
2014-12-16 09:49:57 -05:00
|
|
|
_, err := os.Stat(p.config.ManifestFile)
|
2013-09-08 02:08:56 -04:00
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("manifest_file is invalid: %s", err))
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-08 02:31:28 -04:00
|
|
|
for i, path := range p.config.ModulePaths {
|
|
|
|
info, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("module_path[%d] is invalid: %s", i, err))
|
|
|
|
} else if !info.IsDir() {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
2014-12-16 01:14:19 -05:00
|
|
|
fmt.Errorf("module_path[%d] must point to a directory", i))
|
2013-09-08 02:31:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
|
|
|
return errs
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
|
2013-09-09 17:03:40 -04:00
|
|
|
ui.Say("Provisioning with Puppet...")
|
2018-12-07 06:08:11 -05:00
|
|
|
p.communicator = comm
|
2013-09-08 02:08:56 -04:00
|
|
|
ui.Message("Creating Puppet staging directory...")
|
|
|
|
if err := p.createDir(ui, comm, p.config.StagingDir); err != nil {
|
|
|
|
return fmt.Errorf("Error creating staging directory: %s", err)
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
|
2013-09-09 16:24:17 -04:00
|
|
|
// Upload hiera config if set
|
|
|
|
remoteHieraConfigPath := ""
|
|
|
|
if p.config.HieraConfigPath != "" {
|
|
|
|
var err error
|
|
|
|
remoteHieraConfigPath, err = p.uploadHieraConfig(ui, comm)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error uploading hiera config: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-25 02:52:41 -05:00
|
|
|
// Upload manifest dir if set
|
|
|
|
remoteManifestDir := ""
|
|
|
|
if p.config.ManifestDir != "" {
|
2013-12-11 14:20:22 -05:00
|
|
|
ui.Message(fmt.Sprintf(
|
|
|
|
"Uploading manifest directory from: %s", p.config.ManifestDir))
|
|
|
|
remoteManifestDir = fmt.Sprintf("%s/manifests", p.config.StagingDir)
|
|
|
|
err := p.uploadDirectory(ui, comm, remoteManifestDir, p.config.ManifestDir)
|
2013-11-25 02:52:41 -05:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error uploading manifest dir: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-01 20:05:23 -04:00
|
|
|
// Upload all modules
|
2013-09-08 02:08:56 -04:00
|
|
|
modulePaths := make([]string, 0, len(p.config.ModulePaths))
|
|
|
|
for i, path := range p.config.ModulePaths {
|
2013-09-08 02:43:06 -04:00
|
|
|
ui.Message(fmt.Sprintf("Uploading local modules from: %s", path))
|
2013-09-08 02:08:56 -04:00
|
|
|
targetPath := fmt.Sprintf("%s/module-%d", p.config.StagingDir, i)
|
|
|
|
if err := p.uploadDirectory(ui, comm, targetPath, path); err != nil {
|
|
|
|
return fmt.Errorf("Error uploading modules: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
modulePaths = append(modulePaths, targetPath)
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Upload manifests
|
2013-09-08 02:08:56 -04:00
|
|
|
remoteManifestFile, err := p.uploadManifests(ui, comm)
|
2013-08-01 20:05:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error uploading manifests: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-09-09 01:59:43 -04:00
|
|
|
// Compile the facter variables
|
|
|
|
facterVars := make([]string, 0, len(p.config.Facter))
|
|
|
|
for k, v := range p.config.Facter {
|
2017-08-21 00:28:58 -04:00
|
|
|
facterVars = append(facterVars, fmt.Sprintf(p.guestOSTypeConfig.facterVarsFmt, k, v))
|
2013-09-09 01:59:43 -04:00
|
|
|
}
|
|
|
|
|
2017-01-31 05:09:04 -05:00
|
|
|
data := ExecuteTemplate{
|
2018-05-08 12:44:15 -04:00
|
|
|
ExtraArguments: "",
|
|
|
|
FacterVars: strings.Join(facterVars, p.guestOSTypeConfig.facterVarsJoiner),
|
|
|
|
HieraConfigPath: remoteHieraConfigPath,
|
|
|
|
ManifestDir: remoteManifestDir,
|
|
|
|
ManifestFile: remoteManifestFile,
|
|
|
|
ModulePath: strings.Join(modulePaths, p.guestOSTypeConfig.modulePathJoiner),
|
2018-05-08 12:21:04 -04:00
|
|
|
ModulePathJoiner: p.guestOSTypeConfig.modulePathJoiner,
|
2018-05-08 12:44:15 -04:00
|
|
|
PuppetBinDir: p.config.PuppetBinDir,
|
|
|
|
Sudo: !p.config.PreventSudo,
|
|
|
|
WorkingDir: p.config.WorkingDir,
|
2015-05-27 17:50:20 -04:00
|
|
|
}
|
2017-01-31 05:09:04 -05:00
|
|
|
|
|
|
|
p.config.ctx.Data = &data
|
|
|
|
_ExtraArguments, err := interpolate.Render(strings.Join(p.config.ExtraArguments, " "), &p.config.ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
data.ExtraArguments = _ExtraArguments
|
|
|
|
|
2015-05-27 17:50:20 -04:00
|
|
|
command, err := interpolate.Render(p.config.ExecuteCommand, &p.config.ctx)
|
2013-09-08 02:08:56 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-08-01 20:05:23 -04:00
|
|
|
|
2018-12-07 06:08:11 -05:00
|
|
|
if p.config.ElevatedUser != "" {
|
|
|
|
command, err = provisioner.GenerateElevatedRunner(command, p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
cmd := &packer.RemoteCmd{
|
|
|
|
Command: command,
|
|
|
|
}
|
2013-08-01 20:05:23 -04:00
|
|
|
|
2013-09-08 02:43:06 -04:00
|
|
|
ui.Message(fmt.Sprintf("Running Puppet: %s", command))
|
2013-09-08 02:08:56 -04:00
|
|
|
if err := cmd.StartWithUi(comm, ui); err != nil {
|
2016-05-02 11:17:04 -04:00
|
|
|
return fmt.Errorf("Got an error starting command: %s", err)
|
2013-09-08 02:08:56 -04:00
|
|
|
}
|
|
|
|
|
2016-03-13 23:39:18 -04:00
|
|
|
if cmd.ExitStatus != 0 && cmd.ExitStatus != 2 && !p.config.IgnoreExitCodes {
|
2013-09-08 02:08:56 -04:00
|
|
|
return fmt.Errorf("Puppet exited with a non-zero exit status: %d", cmd.ExitStatus)
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
|
2015-07-17 16:24:23 -04:00
|
|
|
if p.config.CleanStagingDir {
|
|
|
|
if err := p.removeDir(ui, comm, p.config.StagingDir); err != nil {
|
|
|
|
return fmt.Errorf("Error removing staging directory: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-01 20:05:23 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) Cancel() {
|
|
|
|
// Just hard quit. It isn't a big deal if what we're doing keeps
|
|
|
|
// running on the other side.
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2013-09-09 16:24:17 -04:00
|
|
|
func (p *Provisioner) uploadHieraConfig(ui packer.Ui, comm packer.Communicator) (string, error) {
|
|
|
|
ui.Message("Uploading hiera configuration...")
|
|
|
|
f, err := os.Open(p.config.HieraConfigPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
path := fmt.Sprintf("%s/hiera.yaml", p.config.StagingDir)
|
2014-05-10 00:03:35 -04:00
|
|
|
if err := comm.Upload(path, f, nil); err != nil {
|
2013-09-09 16:24:17 -04:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return path, nil
|
|
|
|
}
|
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
func (p *Provisioner) uploadManifests(ui packer.Ui, comm packer.Communicator) (string, error) {
|
|
|
|
// Create the remote manifests directory...
|
|
|
|
ui.Message("Uploading manifests...")
|
|
|
|
remoteManifestsPath := fmt.Sprintf("%s/manifests", p.config.StagingDir)
|
|
|
|
if err := p.createDir(ui, comm, remoteManifestsPath); err != nil {
|
|
|
|
return "", fmt.Errorf("Error creating manifests directory: %s", err)
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
|
2015-07-16 22:15:16 -04:00
|
|
|
// NOTE! manifest_file may either be a directory or a file, as puppet apply
|
|
|
|
// now accepts either one.
|
|
|
|
|
|
|
|
fi, err := os.Stat(p.config.ManifestFile)
|
2013-08-01 20:05:23 -04:00
|
|
|
if err != nil {
|
2015-07-16 22:15:16 -04:00
|
|
|
return "", fmt.Errorf("Error inspecting manifest file: %s", err)
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
|
2015-07-16 22:15:16 -04:00
|
|
|
if fi.IsDir() {
|
|
|
|
// If manifest_file is a directory we'll upload the whole thing
|
|
|
|
ui.Message(fmt.Sprintf(
|
|
|
|
"Uploading manifest directory from: %s", p.config.ManifestFile))
|
|
|
|
|
|
|
|
remoteManifestDir := fmt.Sprintf("%s/manifests", p.config.StagingDir)
|
|
|
|
err := p.uploadDirectory(ui, comm, remoteManifestDir, p.config.ManifestFile)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Error uploading manifest dir: %s", err)
|
|
|
|
}
|
|
|
|
return remoteManifestDir, nil
|
2017-01-13 02:51:36 -05:00
|
|
|
}
|
|
|
|
// Otherwise manifest_file is a file and we'll upload it
|
|
|
|
ui.Message(fmt.Sprintf(
|
|
|
|
"Uploading manifest file from: %s", p.config.ManifestFile))
|
2015-06-15 17:44:54 -04:00
|
|
|
|
2017-01-13 02:51:36 -05:00
|
|
|
f, err := os.Open(p.config.ManifestFile)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2013-08-01 20:05:23 -04:00
|
|
|
|
2017-01-13 02:51:36 -05:00
|
|
|
manifestFilename := filepath.Base(p.config.ManifestFile)
|
|
|
|
remoteManifestFile := fmt.Sprintf("%s/%s", remoteManifestsPath, manifestFilename)
|
|
|
|
if err := comm.Upload(remoteManifestFile, f, nil); err != nil {
|
|
|
|
return "", err
|
2015-07-16 22:15:16 -04:00
|
|
|
}
|
2017-01-13 02:51:36 -05:00
|
|
|
return remoteManifestFile, nil
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
func (p *Provisioner) createDir(ui packer.Ui, comm packer.Communicator, dir string) error {
|
2016-05-02 11:17:04 -04:00
|
|
|
ui.Message(fmt.Sprintf("Creating directory: %s", dir))
|
|
|
|
|
|
|
|
cmd := &packer.RemoteCmd{Command: p.guestCommands.CreateDir(dir)}
|
2013-08-01 20:05:23 -04:00
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
if err := cmd.StartWithUi(comm, ui); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-08-01 20:05:23 -04:00
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
if cmd.ExitStatus != 0 {
|
|
|
|
return fmt.Errorf("Non-zero exit status.")
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
|
2017-09-15 13:48:50 -04:00
|
|
|
// Chmod the directory to 0777 just so that we can access it as our user
|
|
|
|
cmd = &packer.RemoteCmd{Command: p.guestCommands.Chmod(dir, "0777")}
|
|
|
|
if err := cmd.StartWithUi(comm, ui); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if cmd.ExitStatus != 0 {
|
|
|
|
return fmt.Errorf("Non-zero exit status. See output above for more info.")
|
|
|
|
}
|
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-17 16:24:23 -04:00
|
|
|
func (p *Provisioner) removeDir(ui packer.Ui, comm packer.Communicator, dir string) error {
|
2018-12-07 06:08:11 -05:00
|
|
|
cmd := &packer.RemoteCmd{Command: p.guestCommands.RemoveDir(dir)}
|
2015-07-17 16:24:23 -04:00
|
|
|
if err := cmd.StartWithUi(comm, ui); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if cmd.ExitStatus != 0 {
|
|
|
|
return fmt.Errorf("Non-zero exit status.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
func (p *Provisioner) uploadDirectory(ui packer.Ui, comm packer.Communicator, dst string, src string) error {
|
|
|
|
if err := p.createDir(ui, comm, dst); err != nil {
|
|
|
|
return err
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
// Make sure there is a trailing "/" so that the directory isn't
|
|
|
|
// created on the other side.
|
|
|
|
if src[len(src)-1] != '/' {
|
|
|
|
src = src + "/"
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
|
2013-09-08 02:08:56 -04:00
|
|
|
return comm.UploadDir(dst, src, nil)
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
2018-12-07 06:08:11 -05:00
|
|
|
|
|
|
|
func getWinRMPassword(buildName string) string {
|
|
|
|
winRMPass, _ := commonhelper.RetrieveSharedState("winrm_password", buildName)
|
|
|
|
packer.LogSecretFilter.Set(winRMPass)
|
|
|
|
return winRMPass
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) Communicator() packer.Communicator {
|
|
|
|
return p.communicator
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) ElevatedUser() string {
|
|
|
|
return p.config.ElevatedUser
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) ElevatedPassword() string {
|
|
|
|
// Replace ElevatedPassword for winrm users who used this feature
|
|
|
|
p.config.ctx.Data = &EnvVarsTemplate{
|
|
|
|
WinRMPassword: getWinRMPassword(p.config.PackerBuildName),
|
|
|
|
}
|
|
|
|
|
|
|
|
elevatedPassword, _ := interpolate.Render(p.config.ElevatedPassword, &p.config.ctx)
|
|
|
|
|
|
|
|
return elevatedPassword
|
|
|
|
}
|