2021-04-16 05:52:03 -04:00
|
|
|
//go:generate packer-sdc mapstructure-to-hcl2 -type Config
|
2019-10-14 10:43:59 -04:00
|
|
|
|
2013-07-22 00:20:39 -04:00
|
|
|
// This package implements a provisioner for Packer that executes a
|
2016-08-03 15:56:51 -04:00
|
|
|
// saltstack state within the remote machine
|
2013-08-01 02:01:03 -04:00
|
|
|
package saltmasterless
|
2013-07-22 00:20:39 -04:00
|
|
|
|
|
|
|
import (
|
2015-08-23 07:56:16 -04:00
|
|
|
"bytes"
|
2019-03-19 13:11:19 -04:00
|
|
|
"context"
|
2015-08-23 07:56:16 -04:00
|
|
|
"errors"
|
2013-07-22 00:20:39 -04:00
|
|
|
"fmt"
|
2013-07-27 02:35:43 -04:00
|
|
|
"os"
|
2014-08-07 01:07:27 -04:00
|
|
|
"path/filepath"
|
2020-08-06 14:02:45 -04:00
|
|
|
"regexp"
|
2017-12-12 13:36:38 -05:00
|
|
|
"strings"
|
2015-05-27 17:50:20 -04:00
|
|
|
|
2020-08-10 09:41:44 -04:00
|
|
|
"github.com/hashicorp/go-getter/v2"
|
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"
|
2013-07-22 00:20:39 -04:00
|
|
|
)
|
|
|
|
|
2013-08-01 02:05:15 -04:00
|
|
|
type Config struct {
|
2013-08-09 17:24:09 -04:00
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
|
2013-07-26 17:14:41 -04:00
|
|
|
// If true, run the salt-bootstrap script
|
|
|
|
SkipBootstrap bool `mapstructure:"skip_bootstrap"`
|
|
|
|
BootstrapArgs string `mapstructure:"bootstrap_args"`
|
2013-07-27 02:35:43 -04:00
|
|
|
|
2014-05-09 10:08:41 -04:00
|
|
|
DisableSudo bool `mapstructure:"disable_sudo"`
|
|
|
|
|
2016-08-03 15:56:51 -04:00
|
|
|
// Custom state to run instead of highstate
|
|
|
|
CustomState string `mapstructure:"custom_state"`
|
|
|
|
|
2013-08-10 13:20:02 -04:00
|
|
|
// Local path to the minion config
|
|
|
|
MinionConfig string `mapstructure:"minion_config"`
|
|
|
|
|
2017-06-03 19:11:42 -04:00
|
|
|
// Local path to the minion grains
|
|
|
|
GrainsFile string `mapstructure:"grains_file"`
|
|
|
|
|
2013-07-27 02:35:43 -04:00
|
|
|
// Local path to the salt state tree
|
|
|
|
LocalStateTree string `mapstructure:"local_state_tree"`
|
2013-07-27 03:00:01 -04:00
|
|
|
|
2013-08-27 19:48:24 -04:00
|
|
|
// Local path to the salt pillar roots
|
|
|
|
LocalPillarRoots string `mapstructure:"local_pillar_roots"`
|
2013-08-27 19:37:06 -04:00
|
|
|
|
2015-07-27 06:36:39 -04:00
|
|
|
// Remote path to the salt state tree
|
|
|
|
RemoteStateTree string `mapstructure:"remote_state_tree"`
|
|
|
|
|
|
|
|
// Remote path to the salt pillar roots
|
|
|
|
RemotePillarRoots string `mapstructure:"remote_pillar_roots"`
|
|
|
|
|
2013-07-27 03:00:01 -04:00
|
|
|
// Where files will be copied before moving to the /srv/salt directory
|
|
|
|
TempConfigDir string `mapstructure:"temp_config_dir"`
|
2013-08-09 17:24:09 -04:00
|
|
|
|
2015-08-23 08:40:05 -04:00
|
|
|
// Don't exit packer if salt-call returns an error code
|
|
|
|
NoExitOnFailure bool `mapstructure:"no_exit_on_failure"`
|
|
|
|
|
2016-08-03 15:56:51 -04:00
|
|
|
// Set the logging level for the salt-call run
|
2015-08-23 08:47:49 -04:00
|
|
|
LogLevel string `mapstructure:"log_level"`
|
|
|
|
|
2016-11-11 09:15:16 -05:00
|
|
|
// Arguments to pass to salt-call
|
|
|
|
SaltCallArgs string `mapstructure:"salt_call_args"`
|
|
|
|
|
2017-06-06 11:03:03 -04:00
|
|
|
// Directory containing salt-call
|
|
|
|
SaltBinDir string `mapstructure:"salt_bin_dir"`
|
|
|
|
|
2015-08-23 07:56:16 -04:00
|
|
|
// Command line args passed onto salt-call
|
2020-05-26 10:46:31 -04:00
|
|
|
CmdArgs string `mapstructure-to-hcl2:",skip"`
|
2015-08-23 07:56:16 -04:00
|
|
|
|
2017-12-12 13:36:38 -05:00
|
|
|
// The Guest OS Type (unix or windows)
|
|
|
|
GuestOSType string `mapstructure:"guest_os_type"`
|
|
|
|
|
2020-08-06 01:22:19 -04:00
|
|
|
// An array of private or community git source formulas
|
|
|
|
Formulas []string `mapstructure:"formulas"`
|
|
|
|
|
2015-05-27 17:50:20 -04:00
|
|
|
ctx interpolate.Context
|
2013-07-22 00:20:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Provisioner struct {
|
2017-12-12 13:36:38 -05:00
|
|
|
config Config
|
|
|
|
guestOSTypeConfig guestOSTypeConfig
|
2020-11-09 06:16:44 -05:00
|
|
|
guestCommands *guestexec.GuestCommands
|
2017-12-12 13:36:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type guestOSTypeConfig struct {
|
|
|
|
tempDir string
|
|
|
|
stateRoot string
|
|
|
|
pillarRoot string
|
|
|
|
configDir string
|
|
|
|
bootstrapFetchCmd string
|
|
|
|
bootstrapRunCmd string
|
|
|
|
}
|
|
|
|
|
|
|
|
var guestOSTypeConfigs = map[string]guestOSTypeConfig{
|
2020-11-09 06:16:44 -05:00
|
|
|
guestexec.UnixOSType: {
|
2017-12-12 13:36:38 -05:00
|
|
|
configDir: "/etc/salt",
|
2018-03-13 19:11:11 -04:00
|
|
|
tempDir: "/tmp/salt",
|
|
|
|
stateRoot: "/srv/salt",
|
|
|
|
pillarRoot: "/srv/pillar",
|
2021-03-12 14:59:34 -05:00
|
|
|
bootstrapFetchCmd: "curl -L https://bootstrap.saltproject.io -o /tmp/install_salt.sh || wget -O /tmp/install_salt.sh https://bootstrap.saltproject.io",
|
2017-12-12 13:36:38 -05:00
|
|
|
bootstrapRunCmd: "sh /tmp/install_salt.sh",
|
|
|
|
},
|
2020-11-09 06:16:44 -05:00
|
|
|
guestexec.WindowsOSType: {
|
2017-12-12 13:36:38 -05:00
|
|
|
configDir: "C:/salt/conf",
|
|
|
|
tempDir: "C:/Windows/Temp/salt/",
|
2017-12-12 16:39:13 -05:00
|
|
|
stateRoot: "C:/salt/state",
|
|
|
|
pillarRoot: "C:/salt/pillar/",
|
2021-03-12 14:59:34 -05:00
|
|
|
bootstrapFetchCmd: "powershell -Command \"[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]'Tls,Tls11,Tls12'; Invoke-WebRequest -Uri 'https://winbootstrap.saltproject.io' -OutFile 'C:/Windows/Temp/bootstrap-salt.ps1'\"",
|
2017-12-12 13:36:38 -05:00
|
|
|
bootstrapRunCmd: "Powershell C:/Windows/Temp/bootstrap-salt.ps1",
|
|
|
|
},
|
2013-07-22 00:20:39 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (p *Provisioner) ConfigSpec() hcldec.ObjectSpec { return p.config.FlatMapstructure().HCL2Spec() }
|
|
|
|
|
2013-07-22 00:20:39 -04: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: "salt-masterless",
|
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{},
|
|
|
|
},
|
|
|
|
}, raws...)
|
2013-08-09 17:24:09 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-12-12 13:36:38 -05:00
|
|
|
if p.config.GuestOSType == "" {
|
2020-11-09 06:16:44 -05:00
|
|
|
p.config.GuestOSType = guestexec.DefaultOSType
|
2017-12-12 13:36:38 -05:00
|
|
|
} else {
|
|
|
|
p.config.GuestOSType = strings.ToLower(p.config.GuestOSType)
|
|
|
|
}
|
|
|
|
|
|
|
|
var ok bool
|
|
|
|
p.guestOSTypeConfig, ok = guestOSTypeConfigs[p.config.GuestOSType]
|
|
|
|
if !ok {
|
2017-12-12 13:49:10 -05:00
|
|
|
return fmt.Errorf("Invalid guest_os_type: \"%s\"", p.config.GuestOSType)
|
2017-12-12 13:36:38 -05:00
|
|
|
}
|
|
|
|
|
2020-11-09 06:16:44 -05:00
|
|
|
p.guestCommands, err = guestexec.NewGuestCommands(p.config.GuestOSType, !p.config.DisableSudo)
|
2017-12-12 13:36:38 -05:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Invalid guest_os_type: \"%s\"", p.config.GuestOSType)
|
|
|
|
}
|
|
|
|
|
2013-08-01 02:05:15 -04:00
|
|
|
if p.config.TempConfigDir == "" {
|
2017-12-12 13:36:38 -05:00
|
|
|
p.config.TempConfigDir = p.guestOSTypeConfig.tempDir
|
2013-07-22 00:20:39 -04:00
|
|
|
}
|
|
|
|
|
2020-11-19 15:07:02 -05:00
|
|
|
var errs *packersdk.MultiError
|
2013-08-09 17:24:09 -04:00
|
|
|
|
2014-08-06 21:23:15 -04:00
|
|
|
// require a salt state tree
|
2015-08-21 11:08:48 -04:00
|
|
|
err = validateDirConfig(p.config.LocalStateTree, "local_state_tree", true)
|
|
|
|
if err != nil {
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.MultiErrorAppend(errs, err)
|
2013-08-11 19:17:59 -04:00
|
|
|
}
|
2013-07-27 03:00:01 -04:00
|
|
|
|
2020-08-06 14:02:45 -04:00
|
|
|
if p.config.Formulas != nil && len(p.config.Formulas) > 0 {
|
|
|
|
|
|
|
|
validURLs := hasValidFormulaURLs(p.config.Formulas)
|
|
|
|
if !validURLs {
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("Invalid formula URL. Please verify the git URLs also contain a '//' subdir"))
|
2020-08-06 14:02:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-21 11:08:48 -04:00
|
|
|
err = validateDirConfig(p.config.LocalPillarRoots, "local_pillar_roots", false)
|
|
|
|
if err != nil {
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.MultiErrorAppend(errs, err)
|
2013-08-27 19:37:06 -04:00
|
|
|
}
|
|
|
|
|
2015-08-21 11:08:48 -04:00
|
|
|
err = validateFileConfig(p.config.MinionConfig, "minion_config", false)
|
|
|
|
if err != nil {
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.MultiErrorAppend(errs, err)
|
2013-08-10 13:20:02 -04:00
|
|
|
}
|
|
|
|
|
2015-08-23 07:56:16 -04:00
|
|
|
if p.config.MinionConfig != "" && (p.config.RemoteStateTree != "" || p.config.RemotePillarRoots != "") {
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.MultiErrorAppend(errs,
|
2015-08-23 08:40:05 -04:00
|
|
|
errors.New("remote_state_tree and remote_pillar_roots only apply when minion_config is not used"))
|
2015-08-23 07:56:16 -04:00
|
|
|
}
|
|
|
|
|
2017-06-03 19:11:42 -04:00
|
|
|
err = validateFileConfig(p.config.GrainsFile, "grains_file", false)
|
|
|
|
if err != nil {
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.MultiErrorAppend(errs, err)
|
2017-06-03 19:11:42 -04:00
|
|
|
}
|
|
|
|
|
2015-08-23 07:56:16 -04:00
|
|
|
// build the command line args to pass onto salt
|
|
|
|
var cmd_args bytes.Buffer
|
|
|
|
|
2016-08-03 15:56:51 -04:00
|
|
|
if p.config.CustomState == "" {
|
|
|
|
cmd_args.WriteString(" state.highstate")
|
2016-10-11 17:43:50 -04:00
|
|
|
} else {
|
2016-08-03 15:56:51 -04:00
|
|
|
cmd_args.WriteString(" state.sls ")
|
|
|
|
cmd_args.WriteString(p.config.CustomState)
|
|
|
|
}
|
|
|
|
|
2015-08-23 07:56:16 -04:00
|
|
|
if p.config.MinionConfig == "" {
|
|
|
|
// pass --file-root and --pillar-root if no minion_config is supplied
|
|
|
|
if p.config.RemoteStateTree != "" {
|
|
|
|
cmd_args.WriteString(" --file-root=")
|
|
|
|
cmd_args.WriteString(p.config.RemoteStateTree)
|
|
|
|
} else {
|
|
|
|
cmd_args.WriteString(" --file-root=")
|
2017-12-12 13:36:38 -05:00
|
|
|
cmd_args.WriteString(p.guestOSTypeConfig.stateRoot)
|
2015-08-23 07:56:16 -04:00
|
|
|
}
|
|
|
|
if p.config.RemotePillarRoots != "" {
|
|
|
|
cmd_args.WriteString(" --pillar-root=")
|
|
|
|
cmd_args.WriteString(p.config.RemotePillarRoots)
|
|
|
|
} else {
|
|
|
|
cmd_args.WriteString(" --pillar-root=")
|
2017-12-12 13:36:38 -05:00
|
|
|
cmd_args.WriteString(p.guestOSTypeConfig.pillarRoot)
|
2015-08-23 07:56:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-23 08:40:05 -04:00
|
|
|
if !p.config.NoExitOnFailure {
|
|
|
|
cmd_args.WriteString(" --retcode-passthrough")
|
|
|
|
}
|
|
|
|
|
2015-08-23 08:47:49 -04:00
|
|
|
if p.config.LogLevel == "" {
|
|
|
|
cmd_args.WriteString(" -l info")
|
|
|
|
} else {
|
|
|
|
cmd_args.WriteString(" -l ")
|
|
|
|
cmd_args.WriteString(p.config.LogLevel)
|
|
|
|
}
|
|
|
|
|
2016-11-11 09:15:16 -05:00
|
|
|
if p.config.SaltCallArgs != "" {
|
|
|
|
cmd_args.WriteString(" ")
|
|
|
|
cmd_args.WriteString(p.config.SaltCallArgs)
|
|
|
|
}
|
|
|
|
|
2015-08-23 07:56:16 -04:00
|
|
|
p.config.CmdArgs = cmd_args.String()
|
|
|
|
|
2013-08-01 02:05:15 -04:00
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
|
|
|
return errs
|
2013-07-22 00:20:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (p *Provisioner) Provision(ctx context.Context, ui packersdk.Ui, comm packersdk.Communicator, _ map[string]interface{}) error {
|
2013-07-22 00:20:39 -04:00
|
|
|
var err error
|
2014-08-07 01:07:27 -04:00
|
|
|
var src, dst string
|
2020-08-06 01:22:19 -04:00
|
|
|
var formulas []string
|
|
|
|
|
|
|
|
if p.config.Formulas != nil && len(p.config.Formulas) > 0 {
|
|
|
|
ui.Say("Downloading Salt formulas...")
|
|
|
|
client := new(getter.Client)
|
|
|
|
for _, i := range p.config.Formulas {
|
2020-08-10 09:41:44 -04:00
|
|
|
req := getter.Request{
|
|
|
|
Src: i,
|
|
|
|
}
|
2020-08-06 01:22:19 -04:00
|
|
|
// Use //subdirectory name when creating in local_state_tree directory
|
|
|
|
state := strings.Split(i, "//")
|
|
|
|
last := state[len(state)-1]
|
|
|
|
path := filepath.Join(p.config.LocalStateTree, last)
|
|
|
|
formulas = append(formulas, path)
|
|
|
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
|
|
|
ui.Message(fmt.Sprintf("%s => %s", i, path))
|
|
|
|
if err = os.Mkdir(path, 0755); err != nil {
|
|
|
|
return fmt.Errorf("Unable to create Salt state directory: %s", err)
|
|
|
|
}
|
2020-08-10 09:41:44 -04:00
|
|
|
req.Dst = path
|
|
|
|
req.Mode = getter.ModeAny
|
|
|
|
if _, err := client.Get(ctx, &req); err != nil {
|
2020-08-06 01:22:19 -04:00
|
|
|
return fmt.Errorf("Unable to download Salt formula from %s: %s", i, err)
|
|
|
|
}
|
2020-08-06 14:02:45 -04:00
|
|
|
} else {
|
|
|
|
ui.Message(fmt.Sprintf("Found existing formula at: %s", path))
|
2020-08-06 01:22:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-22 00:20:39 -04:00
|
|
|
|
2013-08-01 02:07:15 -04:00
|
|
|
ui.Say("Provisioning with Salt...")
|
2013-07-26 17:14:41 -04:00
|
|
|
if !p.config.SkipBootstrap {
|
2020-11-19 18:10:00 -05:00
|
|
|
cmd := &packersdk.RemoteCmd{
|
2016-12-02 14:02:22 -05:00
|
|
|
// Fallback on wget if curl failed for any reason (such as not being installed)
|
2017-12-12 13:36:38 -05:00
|
|
|
Command: fmt.Sprintf(p.guestOSTypeConfig.bootstrapFetchCmd),
|
2014-12-22 15:08:32 -05:00
|
|
|
}
|
|
|
|
ui.Message(fmt.Sprintf("Downloading saltstack bootstrap to /tmp/install_salt.sh"))
|
2019-04-03 11:14:55 -04:00
|
|
|
if err = cmd.RunWithUi(ctx, comm, ui); err != nil {
|
2014-12-16 01:46:45 -05:00
|
|
|
return fmt.Errorf("Unable to download Salt: %s", err)
|
2014-12-22 15:08:32 -05:00
|
|
|
}
|
2020-11-19 18:10:00 -05:00
|
|
|
cmd = &packersdk.RemoteCmd{
|
2017-12-12 17:11:29 -05:00
|
|
|
Command: fmt.Sprintf("%s %s", p.sudo(p.guestOSTypeConfig.bootstrapRunCmd), p.config.BootstrapArgs),
|
2013-07-27 21:12:18 -04:00
|
|
|
}
|
2014-12-16 01:46:45 -05:00
|
|
|
ui.Message(fmt.Sprintf("Installing Salt with command %s", cmd.Command))
|
2019-04-03 11:14:55 -04:00
|
|
|
if err = cmd.RunWithUi(ctx, comm, ui); err != nil {
|
2014-12-16 01:46:45 -05:00
|
|
|
return fmt.Errorf("Unable to install Salt: %s", err)
|
2013-07-22 00:20:39 -04:00
|
|
|
}
|
|
|
|
}
|
2013-07-27 02:35:43 -04:00
|
|
|
|
2015-07-27 07:00:57 -04:00
|
|
|
ui.Message(fmt.Sprintf("Creating remote temporary directory: %s", p.config.TempConfigDir))
|
2014-08-07 01:07:27 -04:00
|
|
|
if err := p.createDir(ui, comm, p.config.TempConfigDir); err != nil {
|
2015-07-27 07:00:57 -04:00
|
|
|
return fmt.Errorf("Error creating remote temporary directory: %s", err)
|
2013-07-27 02:35:43 -04:00
|
|
|
}
|
|
|
|
|
2013-08-27 18:47:11 -04:00
|
|
|
if p.config.MinionConfig != "" {
|
|
|
|
ui.Message(fmt.Sprintf("Uploading minion config: %s", p.config.MinionConfig))
|
2014-08-07 01:07:27 -04:00
|
|
|
src = p.config.MinionConfig
|
|
|
|
dst = filepath.ToSlash(filepath.Join(p.config.TempConfigDir, "minion"))
|
|
|
|
if err = p.uploadFile(ui, comm, dst, src); err != nil {
|
2013-08-27 18:47:11 -04:00
|
|
|
return fmt.Errorf("Error uploading local minion config file to remote: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-08-07 01:07:27 -04:00
|
|
|
// move minion config into /etc/salt
|
2017-12-12 13:36:38 -05:00
|
|
|
ui.Message(fmt.Sprintf("Make sure directory %s exists", p.guestOSTypeConfig.configDir))
|
|
|
|
if err := p.createDir(ui, comm, p.guestOSTypeConfig.configDir); err != nil {
|
2015-07-27 06:28:19 -04:00
|
|
|
return fmt.Errorf("Error creating remote salt configuration directory: %s", err)
|
|
|
|
}
|
2014-08-07 01:07:27 -04:00
|
|
|
src = filepath.ToSlash(filepath.Join(p.config.TempConfigDir, "minion"))
|
2017-12-12 13:36:38 -05:00
|
|
|
dst = filepath.ToSlash(filepath.Join(p.guestOSTypeConfig.configDir, "minion"))
|
2014-08-07 01:07:27 -04:00
|
|
|
if err = p.moveFile(ui, comm, dst, src); err != nil {
|
2017-12-12 13:36:38 -05:00
|
|
|
return fmt.Errorf("Unable to move %s/minion to %s/minion: %s", p.config.TempConfigDir, p.guestOSTypeConfig.configDir, err)
|
2013-08-27 18:47:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-03 19:11:42 -04:00
|
|
|
if p.config.GrainsFile != "" {
|
|
|
|
ui.Message(fmt.Sprintf("Uploading grains file: %s", p.config.GrainsFile))
|
|
|
|
src = p.config.GrainsFile
|
|
|
|
dst = filepath.ToSlash(filepath.Join(p.config.TempConfigDir, "grains"))
|
|
|
|
if err = p.uploadFile(ui, comm, dst, src); err != nil {
|
|
|
|
return fmt.Errorf("Error uploading local grains file to remote: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// move grains file into /etc/salt
|
2017-12-12 13:36:38 -05:00
|
|
|
ui.Message(fmt.Sprintf("Make sure directory %s exists", p.guestOSTypeConfig.configDir))
|
|
|
|
if err := p.createDir(ui, comm, p.guestOSTypeConfig.configDir); err != nil {
|
2017-06-03 19:11:42 -04:00
|
|
|
return fmt.Errorf("Error creating remote salt configuration directory: %s", err)
|
|
|
|
}
|
|
|
|
src = filepath.ToSlash(filepath.Join(p.config.TempConfigDir, "grains"))
|
2017-12-12 13:36:38 -05:00
|
|
|
dst = filepath.ToSlash(filepath.Join(p.guestOSTypeConfig.configDir, "grains"))
|
2017-06-03 19:11:42 -04:00
|
|
|
if err = p.moveFile(ui, comm, dst, src); err != nil {
|
2017-12-12 13:36:38 -05:00
|
|
|
return fmt.Errorf("Unable to move %s/grains to %s/grains: %s", p.config.TempConfigDir, p.guestOSTypeConfig.configDir, err)
|
2017-06-03 19:11:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-01 02:07:15 -04:00
|
|
|
ui.Message(fmt.Sprintf("Uploading local state tree: %s", p.config.LocalStateTree))
|
2014-08-07 01:07:27 -04:00
|
|
|
src = p.config.LocalStateTree
|
|
|
|
dst = filepath.ToSlash(filepath.Join(p.config.TempConfigDir, "states"))
|
|
|
|
if err = p.uploadDir(ui, comm, dst, src, []string{".git"}); err != nil {
|
2013-07-27 02:35:43 -04:00
|
|
|
return fmt.Errorf("Error uploading local state tree to remote: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-07-27 06:36:39 -04:00
|
|
|
// move state tree from temporary directory
|
2014-08-07 01:07:27 -04:00
|
|
|
src = filepath.ToSlash(filepath.Join(p.config.TempConfigDir, "states"))
|
2015-08-23 07:56:16 -04:00
|
|
|
if p.config.RemoteStateTree != "" {
|
|
|
|
dst = p.config.RemoteStateTree
|
|
|
|
} else {
|
2017-12-12 13:36:38 -05:00
|
|
|
dst = p.guestOSTypeConfig.stateRoot
|
2015-08-23 07:56:16 -04:00
|
|
|
}
|
2017-12-12 16:39:13 -05:00
|
|
|
|
2017-12-13 12:20:27 -05:00
|
|
|
if err = p.statPath(ui, comm, dst); err != nil {
|
|
|
|
if err = p.removeDir(ui, comm, dst); err != nil {
|
|
|
|
return fmt.Errorf("Unable to clear salt tree: %s", err)
|
|
|
|
}
|
2015-07-27 06:36:39 -04:00
|
|
|
}
|
2017-12-12 16:39:13 -05:00
|
|
|
|
2014-08-07 01:07:27 -04:00
|
|
|
if err = p.moveFile(ui, comm, dst, src); err != nil {
|
2015-07-27 06:36:39 -04:00
|
|
|
return fmt.Errorf("Unable to move %s/states to %s: %s", p.config.TempConfigDir, dst, err)
|
2013-08-27 19:37:06 -04:00
|
|
|
}
|
|
|
|
|
2020-08-06 01:22:19 -04:00
|
|
|
// Remove the local Salt formulas if present
|
|
|
|
if p.config.Formulas != nil {
|
|
|
|
for _, f := range formulas {
|
2020-08-06 14:02:45 -04:00
|
|
|
if _, err := os.Stat(f); !os.IsNotExist(err) && f != p.config.LocalStateTree {
|
2020-08-06 01:22:19 -04:00
|
|
|
ui.Message(fmt.Sprintf("Removing Salt formula: %s", f))
|
2020-08-06 14:48:25 -04:00
|
|
|
defer os.RemoveAll(f)
|
2020-08-06 01:22:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-27 19:48:24 -04:00
|
|
|
if p.config.LocalPillarRoots != "" {
|
|
|
|
ui.Message(fmt.Sprintf("Uploading local pillar roots: %s", p.config.LocalPillarRoots))
|
2014-08-07 01:07:27 -04:00
|
|
|
src = p.config.LocalPillarRoots
|
|
|
|
dst = filepath.ToSlash(filepath.Join(p.config.TempConfigDir, "pillar"))
|
|
|
|
if err = p.uploadDir(ui, comm, dst, src, []string{".git"}); err != nil {
|
2013-08-27 19:48:24 -04:00
|
|
|
return fmt.Errorf("Error uploading local pillar roots to remote: %s", err)
|
2013-08-27 19:37:06 -04:00
|
|
|
}
|
|
|
|
|
2015-07-27 06:36:39 -04:00
|
|
|
// move pillar root from temporary directory
|
2014-08-07 01:07:27 -04:00
|
|
|
src = filepath.ToSlash(filepath.Join(p.config.TempConfigDir, "pillar"))
|
2015-08-23 07:56:16 -04:00
|
|
|
if p.config.RemotePillarRoots != "" {
|
|
|
|
dst = p.config.RemotePillarRoots
|
|
|
|
} else {
|
2017-12-12 13:36:38 -05:00
|
|
|
dst = p.guestOSTypeConfig.pillarRoot
|
2015-08-23 07:56:16 -04:00
|
|
|
}
|
2017-12-12 18:01:00 -05:00
|
|
|
|
2017-12-13 12:20:27 -05:00
|
|
|
if err = p.statPath(ui, comm, dst); err != nil {
|
|
|
|
if err = p.removeDir(ui, comm, dst); err != nil {
|
|
|
|
return fmt.Errorf("Unable to clear pillar root: %s", err)
|
|
|
|
}
|
2015-07-27 06:36:39 -04:00
|
|
|
}
|
2017-12-12 18:01:00 -05:00
|
|
|
|
2014-08-07 01:07:27 -04:00
|
|
|
if err = p.moveFile(ui, comm, dst, src); err != nil {
|
2015-07-27 06:36:39 -04:00
|
|
|
return fmt.Errorf("Unable to move %s/pillar to %s: %s", p.config.TempConfigDir, dst, err)
|
2013-08-27 19:37:06 -04:00
|
|
|
}
|
2013-07-27 02:35:43 -04:00
|
|
|
}
|
|
|
|
|
2020-11-23 04:29:12 -05:00
|
|
|
if p.config.GuestOSType == guestexec.WindowsOSType {
|
2020-11-03 11:39:51 -05:00
|
|
|
ui.Message("Downloading Git for Windows")
|
2020-11-30 17:34:20 -05:00
|
|
|
cmd1 := &packersdk.RemoteCmd{Command: fmt.Sprintf("powershell [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Invoke-WebRequest -Uri https://github.com/git-for-windows/git/releases/download/v2.28.0.windows.1/Git-2.28.0-64-bit.exe -OutFile $env:TEMP/Git.exe")}
|
2020-11-03 11:39:51 -05:00
|
|
|
if err = cmd1.RunWithUi(ctx, comm, ui); (err != nil || cmd1.ExitStatus() != 0) && !p.config.NoExitOnFailure {
|
|
|
|
if err == nil {
|
|
|
|
err = fmt.Errorf("Bad exit status: %d", cmd1.ExitStatus())
|
2020-11-02 22:27:11 -05:00
|
|
|
}
|
|
|
|
|
2020-11-03 11:39:51 -05:00
|
|
|
return fmt.Errorf("Unable to Download Git for Windows: %s", err)
|
|
|
|
}
|
2020-11-02 22:27:11 -05:00
|
|
|
|
2020-11-03 11:39:51 -05:00
|
|
|
ui.Message("Installing Git for Windows")
|
2020-11-30 17:34:20 -05:00
|
|
|
cmd2 := &packersdk.RemoteCmd{Command: fmt.Sprintf("powershell Start-Process -FilePath $env:TEMP/Git.exe /SILENT -Wait")}
|
2020-11-03 11:39:51 -05:00
|
|
|
if err = cmd2.RunWithUi(ctx, comm, ui); (err != nil || cmd2.ExitStatus() != 0) && !p.config.NoExitOnFailure {
|
|
|
|
if err == nil {
|
|
|
|
err = fmt.Errorf("Bad exit status: %d", cmd2.ExitStatus())
|
2020-11-02 22:27:11 -05:00
|
|
|
}
|
|
|
|
|
2020-11-03 11:39:51 -05:00
|
|
|
return fmt.Errorf("Unable to Install Git for Windows: %s", err)
|
|
|
|
}
|
2020-11-02 22:27:11 -05:00
|
|
|
|
2020-11-03 11:39:51 -05:00
|
|
|
ui.Message("Cleaning Up After Git for Windows")
|
2020-11-30 17:34:20 -05:00
|
|
|
cmd3 := &packersdk.RemoteCmd{Command: fmt.Sprintf("powershell Remove-Item $env:TEMP/Git.exe")}
|
2020-11-03 11:39:51 -05:00
|
|
|
if err = cmd3.RunWithUi(ctx, comm, ui); (err != nil || cmd3.ExitStatus() != 0) && !p.config.NoExitOnFailure {
|
|
|
|
if err == nil {
|
|
|
|
err = fmt.Errorf("Bad exit status: %d", cmd3.ExitStatus())
|
2020-11-02 22:27:11 -05:00
|
|
|
}
|
|
|
|
|
2020-11-03 11:39:51 -05:00
|
|
|
return fmt.Errorf("Unable to Clean-up After Git for Windows: %s", err)
|
|
|
|
}
|
2020-10-31 21:26:33 -04:00
|
|
|
|
2020-11-03 11:39:51 -05:00
|
|
|
ui.Message("Running salt-call --local winrepo.update_git_repos")
|
2020-11-30 17:34:20 -05:00
|
|
|
cmd4 := &packersdk.RemoteCmd{Command: fmt.Sprintf("%s --local winrepo.update_git_repos", filepath.Join(p.config.SaltBinDir, "salt-call"))}
|
2020-11-03 11:39:51 -05:00
|
|
|
if err = cmd4.RunWithUi(ctx, comm, ui); (err != nil || cmd4.ExitStatus() != 0) && !p.config.NoExitOnFailure {
|
|
|
|
if err == nil {
|
|
|
|
err = fmt.Errorf("Bad exit status: %d", cmd4.ExitStatus())
|
2020-10-31 21:26:33 -04:00
|
|
|
}
|
|
|
|
|
2020-11-03 11:39:51 -05:00
|
|
|
return fmt.Errorf("Error executing salt-call --local winrepo.update_git_repos: %s", err)
|
|
|
|
}
|
2020-10-31 21:26:33 -04:00
|
|
|
|
2020-11-03 11:39:51 -05:00
|
|
|
ui.Message("Running salt-call --local pkg.refresh_db")
|
2020-11-30 17:34:20 -05:00
|
|
|
cmd5 := &packersdk.RemoteCmd{Command: fmt.Sprintf("%s --local pkg.refresh_db", filepath.Join(p.config.SaltBinDir, "salt-call"))}
|
2020-11-03 11:39:51 -05:00
|
|
|
if err = cmd5.RunWithUi(ctx, comm, ui); (err != nil || cmd5.ExitStatus() != 0) && !p.config.NoExitOnFailure {
|
|
|
|
if err == nil {
|
|
|
|
err = fmt.Errorf("Bad exit status: %d", cmd5.ExitStatus())
|
2020-10-31 21:26:33 -04:00
|
|
|
}
|
2020-11-03 11:39:51 -05:00
|
|
|
|
|
|
|
return fmt.Errorf("Error executing salt-call --local pkg.refresh_db: %s", err)
|
2020-10-31 21:26:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-03 15:56:51 -04:00
|
|
|
ui.Message(fmt.Sprintf("Running: salt-call --local %s", p.config.CmdArgs))
|
2020-11-19 18:10:00 -05:00
|
|
|
cmd := &packersdk.RemoteCmd{Command: p.sudo(fmt.Sprintf("%s --local %s", filepath.Join(p.config.SaltBinDir, "salt-call"), p.config.CmdArgs))}
|
2020-04-27 04:48:19 -04:00
|
|
|
if err = cmd.RunWithUi(ctx, comm, ui); (err != nil || cmd.ExitStatus() != 0) && !p.config.NoExitOnFailure {
|
2013-08-12 12:19:11 -04:00
|
|
|
if err == nil {
|
2019-04-03 11:14:55 -04:00
|
|
|
err = fmt.Errorf("Bad exit status: %d", cmd.ExitStatus())
|
2013-08-12 12:19:11 -04:00
|
|
|
}
|
|
|
|
|
2016-08-03 15:56:51 -04:00
|
|
|
return fmt.Errorf("Error executing salt-call: %s", err)
|
2013-07-27 02:35:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-09 10:08:41 -04:00
|
|
|
// Prepends sudo to supplied command if config says to
|
|
|
|
func (p *Provisioner) sudo(cmd string) string {
|
2020-11-09 06:16:44 -05:00
|
|
|
if p.config.DisableSudo || (p.config.GuestOSType == guestexec.WindowsOSType) {
|
2014-05-09 10:08:41 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
return "sudo " + cmd
|
|
|
|
}
|
|
|
|
|
2015-08-21 11:08:48 -04:00
|
|
|
func validateDirConfig(path string, name string, required bool) error {
|
2017-11-09 18:20:37 -05:00
|
|
|
if required && path == "" {
|
2015-08-21 11:08:48 -04:00
|
|
|
return fmt.Errorf("%s cannot be empty", name)
|
|
|
|
} else if required == false && path == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
info, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: path '%s' is invalid: %s", name, path, err)
|
|
|
|
} else if !info.IsDir() {
|
|
|
|
return fmt.Errorf("%s: path '%s' must point to a directory", name, path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateFileConfig(path string, name string, required bool) error {
|
|
|
|
if required == true && path == "" {
|
|
|
|
return fmt.Errorf("%s cannot be empty", name)
|
|
|
|
} else if required == false && path == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
info, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: path '%s' is invalid: %s", name, path, err)
|
|
|
|
} else if info.IsDir() {
|
|
|
|
return fmt.Errorf("%s: path '%s' must point to a file", name, path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-06 14:02:45 -04:00
|
|
|
func hasValidFormulaURLs(s []string) bool {
|
2020-08-07 18:38:41 -04:00
|
|
|
re := regexp.MustCompile(`^(.*).git\/\/[a-zA-Z0-9-_]+(\?.*)?$`)
|
2020-08-06 14:02:45 -04:00
|
|
|
|
|
|
|
for _, u := range s {
|
|
|
|
if !re.MatchString(u) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (p *Provisioner) uploadFile(ui packersdk.Ui, comm packersdk.Communicator, dst, src string) error {
|
2013-08-10 13:20:02 -04:00
|
|
|
f, err := os.Open(src)
|
|
|
|
if err != nil {
|
2014-08-07 01:07:27 -04:00
|
|
|
return fmt.Errorf("Error opening: %s", err)
|
2013-08-10 13:20:02 -04:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2019-07-02 18:06:04 -04:00
|
|
|
_, temp_dst := filepath.Split(dst)
|
|
|
|
|
|
|
|
if err = comm.Upload(temp_dst, f, nil); err != nil {
|
2014-08-07 01:07:27 -04:00
|
|
|
return fmt.Errorf("Error uploading %s: %s", src, err)
|
|
|
|
}
|
2019-07-02 18:06:04 -04:00
|
|
|
|
2020-08-07 13:53:07 -04:00
|
|
|
err = p.moveFile(ui, comm, dst, temp_dst)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error moving file to destination: %s", err)
|
|
|
|
}
|
2019-07-02 18:06:04 -04:00
|
|
|
|
2014-08-07 01:07:27 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (p *Provisioner) moveFile(ui packersdk.Ui, comm packersdk.Communicator, dst string, src string) error {
|
2019-04-03 11:14:55 -04:00
|
|
|
ctx := context.TODO()
|
|
|
|
|
2014-08-07 01:07:27 -04:00
|
|
|
ui.Message(fmt.Sprintf("Moving %s to %s", src, dst))
|
2020-11-19 18:10:00 -05:00
|
|
|
cmd := &packersdk.RemoteCmd{
|
2018-03-13 19:11:11 -04:00
|
|
|
Command: p.sudo(p.guestCommands.MovePath(src, dst)),
|
2017-12-12 17:22:10 -05:00
|
|
|
}
|
2019-04-03 11:14:55 -04:00
|
|
|
if err := cmd.RunWithUi(ctx, comm, ui); err != nil || cmd.ExitStatus() != 0 {
|
2014-08-07 01:07:27 -04:00
|
|
|
if err == nil {
|
2019-04-03 11:14:55 -04:00
|
|
|
err = fmt.Errorf("Bad exit status: %d", cmd.ExitStatus())
|
2014-08-07 01:07:27 -04:00
|
|
|
}
|
|
|
|
|
2015-07-27 07:00:57 -04:00
|
|
|
return fmt.Errorf("Unable to move %s to %s: %s", src, dst, err)
|
2013-08-10 13:20:02 -04:00
|
|
|
}
|
2014-08-07 01:07:27 -04:00
|
|
|
return nil
|
|
|
|
}
|
2013-08-23 18:58:30 -04:00
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (p *Provisioner) createDir(ui packersdk.Ui, comm packersdk.Communicator, dir string) error {
|
2014-08-07 01:07:27 -04:00
|
|
|
ui.Message(fmt.Sprintf("Creating directory: %s", dir))
|
2020-11-19 18:10:00 -05:00
|
|
|
cmd := &packersdk.RemoteCmd{
|
2017-12-12 13:36:38 -05:00
|
|
|
Command: p.guestCommands.CreateDir(dir),
|
2014-08-07 01:07:27 -04:00
|
|
|
}
|
2019-04-03 11:14:55 -04:00
|
|
|
ctx := context.TODO()
|
|
|
|
if err := cmd.RunWithUi(ctx, comm, ui); err != nil {
|
2014-08-07 01:07:27 -04:00
|
|
|
return err
|
|
|
|
}
|
2019-04-03 11:14:55 -04:00
|
|
|
if cmd.ExitStatus() != 0 {
|
2014-08-07 01:07:27 -04:00
|
|
|
return fmt.Errorf("Non-zero exit status.")
|
|
|
|
}
|
2013-08-23 18:58:30 -04:00
|
|
|
return nil
|
2013-08-10 13:20:02 -04:00
|
|
|
}
|
2014-08-07 01:07:27 -04:00
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (p *Provisioner) statPath(ui packersdk.Ui, comm packersdk.Communicator, path string) error {
|
2019-04-03 11:14:55 -04:00
|
|
|
ctx := context.TODO()
|
2017-12-12 16:39:13 -05:00
|
|
|
ui.Message(fmt.Sprintf("Verifying Path: %s", path))
|
2020-11-19 18:10:00 -05:00
|
|
|
cmd := &packersdk.RemoteCmd{
|
2017-12-12 16:39:13 -05:00
|
|
|
Command: p.guestCommands.StatPath(path),
|
|
|
|
}
|
2019-04-03 11:14:55 -04:00
|
|
|
if err := cmd.RunWithUi(ctx, comm, ui); err != nil {
|
2017-12-12 16:39:13 -05:00
|
|
|
return err
|
|
|
|
}
|
2019-04-03 11:14:55 -04:00
|
|
|
if cmd.ExitStatus() != 0 {
|
2017-12-12 16:39:13 -05:00
|
|
|
return fmt.Errorf("Non-zero exit status.")
|
|
|
|
}
|
|
|
|
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()
|
2015-07-27 06:36:39 -04:00
|
|
|
ui.Message(fmt.Sprintf("Removing directory: %s", dir))
|
2020-11-19 18:10:00 -05:00
|
|
|
cmd := &packersdk.RemoteCmd{
|
2017-12-12 13:36:38 -05:00
|
|
|
Command: p.guestCommands.RemoveDir(dir),
|
2015-07-27 06:36:39 -04:00
|
|
|
}
|
2019-04-03 11:14:55 -04:00
|
|
|
if err := cmd.RunWithUi(ctx, comm, ui); err != nil {
|
2015-07-27 06:36:39 -04:00
|
|
|
return err
|
|
|
|
}
|
2019-04-03 11:14:55 -04:00
|
|
|
if cmd.ExitStatus() != 0 {
|
2015-07-27 06:36:39 -04:00
|
|
|
return fmt.Errorf("Non-zero exit status.")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (p *Provisioner) uploadDir(ui packersdk.Ui, comm packersdk.Communicator, dst, src string, ignore []string) error {
|
2019-07-02 18:06:04 -04:00
|
|
|
_, temp_dst := filepath.Split(dst)
|
|
|
|
if err := comm.UploadDir(temp_dst, src, ignore); err != nil {
|
2014-08-07 01:07:27 -04:00
|
|
|
return err
|
|
|
|
}
|
2019-07-02 18:06:04 -04:00
|
|
|
return p.moveFile(ui, comm, dst, temp_dst)
|
2014-08-07 01:07:27 -04:00
|
|
|
}
|