2019-10-14 10:43:59 -04:00
|
|
|
//go:generate mapstructure-to-hcl2 -type Config
|
|
|
|
|
2017-01-12 19:45:18 -05:00
|
|
|
// Package puppetserver implements a provisioner for Packer that executes
|
2014-01-07 11:32:57 -05:00
|
|
|
// Puppet on the remote machine connecting to a Puppet master.
|
|
|
|
package puppetserver
|
|
|
|
|
|
|
|
import (
|
2019-03-19 13:11:19 -04:00
|
|
|
"context"
|
2014-01-07 11:32:57 -05:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2018-04-27 20:40:13 -04:00
|
|
|
"path/filepath"
|
2014-01-07 11:32:57 -05:00
|
|
|
"strings"
|
2015-05-27 17:50:20 -04:00
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/common"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/guestexec"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/template/config"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
|
2014-01-07 11:32:57 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
2015-05-27 17:50:20 -04:00
|
|
|
ctx interpolate.Context
|
2016-09-29 17:13:04 -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"`
|
|
|
|
|
|
|
|
// A path to the client certificate
|
|
|
|
ClientCertPath string `mapstructure:"client_cert_path"`
|
|
|
|
|
|
|
|
// A path to a directory containing the client private keys
|
|
|
|
ClientPrivateKeyPath string `mapstructure:"client_private_key_path"`
|
|
|
|
|
2016-06-13 09:12:28 -04:00
|
|
|
// The command used to execute Puppet.
|
|
|
|
ExecuteCommand string `mapstructure:"execute_command"`
|
2014-01-07 11:32:57 -05:00
|
|
|
|
2017-01-31 05:09:04 -05:00
|
|
|
// Additional argument to pass when executing Puppet.
|
|
|
|
ExtraArguments []string `mapstructure:"extra_arguments"`
|
|
|
|
|
2018-05-01 16:38:01 -04:00
|
|
|
// Additional facts to set when executing Puppet
|
|
|
|
Facter map[string]string
|
|
|
|
|
2017-01-12 19:45:18 -05:00
|
|
|
// The Guest OS Type (unix or windows)
|
|
|
|
GuestOSType string `mapstructure:"guest_os_type"`
|
|
|
|
|
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"`
|
2014-01-07 11:32:57 -05:00
|
|
|
|
2018-05-01 16:38:01 -04:00
|
|
|
// If true, `sudo` will NOT be used to execute Puppet.
|
|
|
|
PreventSudo bool `mapstructure:"prevent_sudo"`
|
2014-01-07 11:32:57 -05:00
|
|
|
|
2018-05-01 16:38:01 -04:00
|
|
|
// The directory that contains the puppet binary.
|
|
|
|
// E.g. if it can't be found on the standard path.
|
|
|
|
PuppetBinDir string `mapstructure:"puppet_bin_dir"`
|
2014-01-07 11:32:57 -05:00
|
|
|
|
2014-01-08 07:03:03 -05:00
|
|
|
// The hostname of the Puppet node.
|
|
|
|
PuppetNode string `mapstructure:"puppet_node"`
|
|
|
|
|
|
|
|
// The hostname of the Puppet server.
|
2014-01-07 11:32:57 -05:00
|
|
|
PuppetServer string `mapstructure:"puppet_server"`
|
|
|
|
|
2014-01-08 07:03:03 -05:00
|
|
|
// The directory where files will be uploaded. Packer requires write
|
|
|
|
// permissions in this directory.
|
2014-01-07 11:32:57 -05:00
|
|
|
StagingDir string `mapstructure:"staging_dir"`
|
2015-05-14 18:05:44 -04:00
|
|
|
|
2018-04-27 20:40:13 -04: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-06 10:09:57 -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"`
|
2014-01-07 11:32:57 -05:00
|
|
|
}
|
|
|
|
|
2017-01-31 05:09:04 -05:00
|
|
|
type guestOSTypeConfig struct {
|
|
|
|
executeCommand string
|
|
|
|
facterVarsFmt string
|
|
|
|
facterVarsJoiner string
|
2018-05-01 16:38:01 -04:00
|
|
|
stagingDir string
|
|
|
|
tempDir string
|
2017-01-31 05:09:04 -05:00
|
|
|
}
|
|
|
|
|
2018-04-27 20:40:13 -04:00
|
|
|
// FIXME assumes both Packer host and target are same OS
|
2017-01-31 05:09:04 -05:00
|
|
|
var guestOSTypeConfigs = map[string]guestOSTypeConfig{
|
2020-11-09 06:16:44 -05:00
|
|
|
guestexec.UnixOSType: {
|
2018-04-27 20:40:13 -04:00
|
|
|
tempDir: "/tmp",
|
2017-01-31 05:09:04 -05:00
|
|
|
stagingDir: "/tmp/packer-puppet-server",
|
|
|
|
executeCommand: "cd {{.WorkingDir}} && " +
|
2018-04-27 20:40:13 -04:00
|
|
|
`{{if ne .FacterVars ""}}{{.FacterVars}} {{end}}` +
|
2017-01-31 05:09:04 -05:00
|
|
|
"{{if .Sudo}}sudo -E {{end}}" +
|
|
|
|
`{{if ne .PuppetBinDir ""}}{{.PuppetBinDir}}/{{end}}` +
|
|
|
|
"puppet agent --onetime --no-daemonize --detailed-exitcodes " +
|
|
|
|
"{{if .Debug}}--debug {{end}}" +
|
|
|
|
`{{if ne .PuppetServer ""}}--server='{{.PuppetServer}}' {{end}}` +
|
2018-05-08 09:53:18 -04:00
|
|
|
`{{if ne .PuppetNode ""}}--certname='{{.PuppetNode}}' {{end}}` +
|
2017-01-31 05:09:04 -05:00
|
|
|
`{{if ne .ClientCertPath ""}}--certdir='{{.ClientCertPath}}' {{end}}` +
|
|
|
|
`{{if ne .ClientPrivateKeyPath ""}}--privatekeydir='{{.ClientPrivateKeyPath}}' {{end}}` +
|
2018-05-08 10:12:22 -04:00
|
|
|
`{{if ne .ExtraArguments ""}}{{.ExtraArguments}} {{end}}`,
|
2017-01-31 05:09:04 -05:00
|
|
|
facterVarsFmt: "FACTER_%s='%s'",
|
|
|
|
facterVarsJoiner: " ",
|
|
|
|
},
|
2020-11-09 06:16:44 -05:00
|
|
|
guestexec.WindowsOSType: {
|
2018-04-27 20:40:13 -04:00
|
|
|
tempDir: filepath.ToSlash(os.Getenv("TEMP")),
|
|
|
|
stagingDir: filepath.ToSlash(os.Getenv("SYSTEMROOT")) + "/Temp/packer-puppet-server",
|
2017-01-31 05:09:04 -05:00
|
|
|
executeCommand: "cd {{.WorkingDir}} && " +
|
2018-04-27 20:40:13 -04:00
|
|
|
`{{if ne .FacterVars ""}}{{.FacterVars}} && {{end}}` +
|
2017-01-31 05:09:04 -05:00
|
|
|
`{{if ne .PuppetBinDir ""}}{{.PuppetBinDir}}/{{end}}` +
|
|
|
|
"puppet agent --onetime --no-daemonize --detailed-exitcodes " +
|
|
|
|
"{{if .Debug}}--debug {{end}}" +
|
|
|
|
`{{if ne .PuppetServer ""}}--server='{{.PuppetServer}}' {{end}}` +
|
2018-05-08 09:53:18 -04:00
|
|
|
`{{if ne .PuppetNode ""}}--certname='{{.PuppetNode}}' {{end}}` +
|
2017-01-31 05:09:04 -05:00
|
|
|
`{{if ne .ClientCertPath ""}}--certdir='{{.ClientCertPath}}' {{end}}` +
|
|
|
|
`{{if ne .ClientPrivateKeyPath ""}}--privatekeydir='{{.ClientPrivateKeyPath}}' {{end}}` +
|
2018-05-08 10:12:22 -04:00
|
|
|
`{{if ne .ExtraArguments ""}}{{.ExtraArguments}} {{end}}`,
|
2017-01-31 05:09:04 -05:00
|
|
|
facterVarsFmt: `SET "FACTER_%s=%s"`,
|
|
|
|
facterVarsJoiner: " & ",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-01-07 11:32:57 -05:00
|
|
|
type Provisioner struct {
|
2017-01-12 19:45:18 -05:00
|
|
|
config Config
|
2020-11-19 18:10:00 -05:00
|
|
|
communicator packersdk.Communicator
|
2017-01-12 19:45:18 -05:00
|
|
|
guestOSTypeConfig guestOSTypeConfig
|
2020-11-09 06:16:44 -05:00
|
|
|
guestCommands *guestexec.GuestCommands
|
2019-12-12 19:42:53 -05:00
|
|
|
generatedData map[string]interface{}
|
2014-01-07 11:32:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type ExecuteTemplate struct {
|
|
|
|
ClientCertPath string
|
|
|
|
ClientPrivateKeyPath string
|
2018-05-01 16:38:01 -04:00
|
|
|
Debug bool
|
|
|
|
ExtraArguments string
|
|
|
|
FacterVars string
|
2014-01-07 11:32:57 -05:00
|
|
|
PuppetNode string
|
|
|
|
PuppetServer string
|
2016-10-15 09:50:37 -04:00
|
|
|
PuppetBinDir string
|
2014-01-09 02:51:48 -05:00
|
|
|
Sudo bool
|
2018-04-27 20:40:13 -04:00
|
|
|
WorkingDir string
|
2014-01-07 11:32:57 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 10:09:57 -05:00
|
|
|
type EnvVarsTemplate struct {
|
|
|
|
WinRMPassword string
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (p *Provisioner) ConfigSpec() hcldec.ObjectSpec { return p.config.FlatMapstructure().HCL2Spec() }
|
|
|
|
|
2014-01-07 11:32:57 -05:00
|
|
|
func (p *Provisioner) Prepare(raws ...interface{}) error {
|
2015-05-27 17:50:20 -04:00
|
|
|
err := config.Decode(&p.config, &config.DecodeOpts{
|
2020-10-09 20:01:55 -04:00
|
|
|
PluginType: "puppet-server",
|
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{
|
2016-06-13 09:12:28 -04:00
|
|
|
Exclude: []string{
|
|
|
|
"execute_command",
|
2017-01-31 05:09:04 -05:00
|
|
|
"extra_arguments",
|
2016-06-13 09:12:28 -04:00
|
|
|
},
|
2015-05-27 17:50:20 -04:00
|
|
|
},
|
|
|
|
}, raws...)
|
2014-01-07 11:32:57 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-29 17:13:04 -04:00
|
|
|
|
2017-01-12 19:45:18 -05:00
|
|
|
if p.config.GuestOSType == "" {
|
2020-11-09 06:16:44 -05:00
|
|
|
p.config.GuestOSType = guestexec.DefaultOSType
|
2017-01-12 19:45:18 -05:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-11-09 06:16:44 -05:00
|
|
|
p.guestCommands, err = guestexec.NewGuestCommands(p.config.GuestOSType, !p.config.PreventSudo)
|
2017-01-12 19:45:18 -05:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Invalid guest_os_type: \"%s\"", p.config.GuestOSType)
|
|
|
|
}
|
|
|
|
|
2016-06-13 09:12:28 -04:00
|
|
|
if p.config.ExecuteCommand == "" {
|
2017-01-12 19:45:18 -05:00
|
|
|
p.config.ExecuteCommand = p.guestOSTypeConfig.executeCommand
|
2016-06-13 09:12:28 -04:00
|
|
|
}
|
2016-09-29 17:13:04 -04:00
|
|
|
|
2014-01-07 11:32:57 -05:00
|
|
|
if p.config.StagingDir == "" {
|
2017-01-12 19:45:18 -05:00
|
|
|
p.config.StagingDir = p.guestOSTypeConfig.stagingDir
|
2014-01-07 11:32:57 -05:00
|
|
|
}
|
|
|
|
|
2018-04-28 01:13:23 -04:00
|
|
|
if p.config.WorkingDir == "" {
|
|
|
|
p.config.WorkingDir = p.config.StagingDir
|
|
|
|
}
|
|
|
|
|
2016-12-14 23:47:35 -05:00
|
|
|
if p.config.Facter == nil {
|
|
|
|
p.config.Facter = make(map[string]string)
|
|
|
|
}
|
|
|
|
p.config.Facter["packer_build_name"] = p.config.PackerBuildName
|
|
|
|
p.config.Facter["packer_builder_type"] = p.config.PackerBuilderType
|
|
|
|
|
2020-11-19 15:07:02 -05:00
|
|
|
var errs *packersdk.MultiError
|
2014-01-07 11:32:57 -05:00
|
|
|
if p.config.ClientCertPath != "" {
|
|
|
|
info, err := os.Stat(p.config.ClientCertPath)
|
|
|
|
if err != nil {
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.MultiErrorAppend(errs,
|
2014-01-07 11:32:57 -05:00
|
|
|
fmt.Errorf("client_cert_dir is invalid: %s", err))
|
|
|
|
} else if !info.IsDir() {
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.MultiErrorAppend(errs,
|
2014-01-07 11:32:57 -05:00
|
|
|
fmt.Errorf("client_cert_dir must point to a directory"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.ClientPrivateKeyPath != "" {
|
|
|
|
info, err := os.Stat(p.config.ClientPrivateKeyPath)
|
|
|
|
if err != nil {
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.MultiErrorAppend(errs,
|
2014-01-07 11:32:57 -05:00
|
|
|
fmt.Errorf("client_private_key_dir is invalid: %s", err))
|
|
|
|
} else if !info.IsDir() {
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.MultiErrorAppend(errs,
|
2014-01-07 11:32:57 -05:00
|
|
|
fmt.Errorf("client_private_key_dir must point to a directory"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (p *Provisioner) Provision(ctx context.Context, ui packersdk.Ui, comm packersdk.Communicator, generatedData map[string]interface{}) error {
|
2014-01-07 11:32:57 -05:00
|
|
|
ui.Say("Provisioning with Puppet...")
|
2018-12-06 10:09:57 -05:00
|
|
|
p.communicator = comm
|
2019-12-12 19:42:53 -05:00
|
|
|
p.generatedData = generatedData
|
2014-01-07 11:32:57 -05: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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Upload client cert dir if set
|
|
|
|
remoteClientCertPath := ""
|
|
|
|
if p.config.ClientCertPath != "" {
|
|
|
|
ui.Message(fmt.Sprintf(
|
|
|
|
"Uploading client cert from: %s", p.config.ClientCertPath))
|
|
|
|
remoteClientCertPath = fmt.Sprintf("%s/certs", p.config.StagingDir)
|
|
|
|
err := p.uploadDirectory(ui, comm, remoteClientCertPath, p.config.ClientCertPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error uploading client cert: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Upload client cert dir if set
|
|
|
|
remoteClientPrivateKeyPath := ""
|
|
|
|
if p.config.ClientPrivateKeyPath != "" {
|
|
|
|
ui.Message(fmt.Sprintf(
|
|
|
|
"Uploading client private keys from: %s", p.config.ClientPrivateKeyPath))
|
|
|
|
remoteClientPrivateKeyPath = fmt.Sprintf("%s/private_keys", p.config.StagingDir)
|
|
|
|
err := p.uploadDirectory(ui, comm, remoteClientPrivateKeyPath, p.config.ClientPrivateKeyPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error uploading client private keys: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compile the facter variables
|
|
|
|
facterVars := make([]string, 0, len(p.config.Facter))
|
|
|
|
for k, v := range p.config.Facter {
|
2017-01-12 19:45:18 -05:00
|
|
|
facterVars = append(facterVars, fmt.Sprintf(p.guestOSTypeConfig.facterVarsFmt, k, v))
|
2014-01-07 11:32:57 -05:00
|
|
|
}
|
|
|
|
|
2017-01-31 05:09:04 -05:00
|
|
|
data := ExecuteTemplate{
|
2014-01-07 11:32:57 -05:00
|
|
|
ClientCertPath: remoteClientCertPath,
|
|
|
|
ClientPrivateKeyPath: remoteClientPrivateKeyPath,
|
2018-05-01 16:38:01 -04:00
|
|
|
ExtraArguments: "",
|
|
|
|
FacterVars: strings.Join(facterVars, p.guestOSTypeConfig.facterVarsJoiner),
|
2014-01-07 11:32:57 -05:00
|
|
|
PuppetNode: p.config.PuppetNode,
|
|
|
|
PuppetServer: p.config.PuppetServer,
|
2016-10-15 09:50:37 -04:00
|
|
|
PuppetBinDir: p.config.PuppetBinDir,
|
2014-01-09 02:51:48 -05:00
|
|
|
Sudo: !p.config.PreventSudo,
|
2017-01-31 05:09:04 -05:00
|
|
|
WorkingDir: p.config.WorkingDir,
|
|
|
|
}
|
|
|
|
|
|
|
|
p.config.ctx.Data = &data
|
|
|
|
_ExtraArguments, err := interpolate.Render(strings.Join(p.config.ExtraArguments, " "), &p.config.ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-05-27 17:50:20 -04:00
|
|
|
}
|
2017-01-31 05:09:04 -05:00
|
|
|
data.ExtraArguments = _ExtraArguments
|
|
|
|
|
2016-06-13 09:12:28 -04:00
|
|
|
command, err := interpolate.Render(p.config.ExecuteCommand, &p.config.ctx)
|
2014-01-07 11:32:57 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-06 10:09:57 -05:00
|
|
|
if p.config.ElevatedUser != "" {
|
2020-11-09 06:16:44 -05:00
|
|
|
command, err = guestexec.GenerateElevatedRunner(command, p)
|
2018-12-06 10:09:57 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
cmd := &packersdk.RemoteCmd{
|
2014-01-07 11:32:57 -05:00
|
|
|
Command: command,
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message(fmt.Sprintf("Running Puppet: %s", command))
|
2019-04-03 11:14:55 -04:00
|
|
|
if err := cmd.RunWithUi(ctx, comm, ui); err != nil {
|
2014-01-07 11:32:57 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-03 11:14:55 -04:00
|
|
|
if cmd.ExitStatus() != 0 && cmd.ExitStatus() != 2 && !p.config.IgnoreExitCodes {
|
|
|
|
return fmt.Errorf("Puppet exited with a non-zero exit status: %d", cmd.ExitStatus())
|
2014-01-07 11:32:57 -05:00
|
|
|
}
|
|
|
|
|
2018-05-01 16:38:01 -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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-07 11:32:57 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (p *Provisioner) createDir(ui packersdk.Ui, comm packersdk.Communicator, dir string) error {
|
2017-01-12 19:45:18 -05:00
|
|
|
ui.Message(fmt.Sprintf("Creating directory: %s", dir))
|
2019-04-03 11:14:55 -04:00
|
|
|
ctx := context.TODO()
|
2014-01-07 11:32:57 -05:00
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
cmd := &packersdk.RemoteCmd{Command: p.guestCommands.CreateDir(dir)}
|
2019-04-03 11:14:55 -04:00
|
|
|
if err := cmd.RunWithUi(ctx, comm, ui); err != nil {
|
2014-01-07 11:32:57 -05:00
|
|
|
return err
|
|
|
|
}
|
2019-04-03 11:14:55 -04:00
|
|
|
if cmd.ExitStatus() != 0 {
|
2017-01-12 19:45:18 -05:00
|
|
|
return fmt.Errorf("Non-zero exit status. See output above for more info.")
|
|
|
|
}
|
2014-01-07 11:32:57 -05:00
|
|
|
|
2017-01-12 19:45:18 -05:00
|
|
|
// Chmod the directory to 0777 just so that we can access it as our user
|
2020-11-19 18:10:00 -05:00
|
|
|
cmd = &packersdk.RemoteCmd{Command: p.guestCommands.Chmod(dir, "0777")}
|
2019-04-03 11:14:55 -04:00
|
|
|
if err := cmd.RunWithUi(ctx, comm, ui); err != nil {
|
2017-01-12 19:45:18 -05:00
|
|
|
return err
|
|
|
|
}
|
2019-04-03 11:14:55 -04:00
|
|
|
if cmd.ExitStatus() != 0 {
|
2017-01-12 19:45:18 -05:00
|
|
|
return fmt.Errorf("Non-zero exit status. See output above for more info.")
|
2014-01-07 11:32:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (p *Provisioner) removeDir(ui packersdk.Ui, comm packersdk.Communicator, dir string) error {
|
2019-04-03 11:14:55 -04:00
|
|
|
ctx := context.TODO()
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
cmd := &packersdk.RemoteCmd{Command: p.guestCommands.RemoveDir(dir)}
|
2019-04-03 11:14:55 -04:00
|
|
|
if err := cmd.RunWithUi(ctx, comm, ui); err != nil {
|
2018-05-01 17:04:40 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-03 11:14:55 -04:00
|
|
|
if cmd.ExitStatus() != 0 {
|
2018-05-01 17:04:40 -04:00
|
|
|
return fmt.Errorf("Non-zero exit status.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (p *Provisioner) uploadDirectory(ui packersdk.Ui, comm packersdk.Communicator, dst string, src string) error {
|
2014-01-07 11:32:57 -05:00
|
|
|
if err := p.createDir(ui, comm, dst); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure there is a trailing "/" so that the directory isn't
|
|
|
|
// created on the other side.
|
|
|
|
if src[len(src)-1] != '/' {
|
|
|
|
src = src + "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
return comm.UploadDir(dst, src, nil)
|
|
|
|
}
|
2018-12-06 10:09:57 -05:00
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (p *Provisioner) Communicator() packersdk.Communicator {
|
2018-12-06 13:00:22 -05:00
|
|
|
return p.communicator
|
2018-12-06 10:09:57 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 13:00:22 -05:00
|
|
|
func (p *Provisioner) ElevatedUser() string {
|
|
|
|
return p.config.ElevatedUser
|
|
|
|
}
|
2018-12-06 10:09:57 -05:00
|
|
|
|
2018-12-06 13:00:22 -05:00
|
|
|
func (p *Provisioner) ElevatedPassword() string {
|
2018-12-06 10:09:57 -05:00
|
|
|
// Replace ElevatedPassword for winrm users who used this feature
|
2019-12-12 19:42:53 -05:00
|
|
|
p.config.ctx.Data = p.generatedData
|
2018-12-06 10:09:57 -05:00
|
|
|
|
2018-12-06 13:00:22 -05:00
|
|
|
elevatedPassword, _ := interpolate.Render(p.config.ElevatedPassword, &p.config.ctx)
|
2018-12-06 10:09:57 -05:00
|
|
|
|
2018-12-06 13:00:22 -05:00
|
|
|
return elevatedPassword
|
2018-12-06 10:09:57 -05:00
|
|
|
}
|