2019-10-14 10:43:59 -04:00
|
|
|
//go:generate mapstructure-to-hcl2 -type Config
|
|
|
|
|
2015-06-14 14:23:29 -04:00
|
|
|
// This package implements a provisioner for Packer that executes
|
|
|
|
// shell scripts within the remote machine.
|
|
|
|
package shell
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2019-03-19 13:11:19 -04:00
|
|
|
"context"
|
2015-06-14 14:23:29 -04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/common"
|
2019-04-08 11:57:27 -04:00
|
|
|
"github.com/hashicorp/packer/common/retry"
|
2019-03-14 06:57:54 -04:00
|
|
|
"github.com/hashicorp/packer/common/shell"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/helper/config"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2018-12-12 09:45:00 -05:00
|
|
|
"github.com/hashicorp/packer/packer/tmp"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2015-06-14 14:23:29 -04:00
|
|
|
)
|
|
|
|
|
2018-12-12 09:45:00 -05:00
|
|
|
//FIXME query remote host or use %SYSTEMROOT%, %TEMP% and more creative filename
|
2015-06-14 14:23:29 -04:00
|
|
|
const DefaultRemotePath = "c:/Windows/Temp/script.bat"
|
|
|
|
|
|
|
|
var retryableSleep = 2 * time.Second
|
|
|
|
|
|
|
|
type Config struct {
|
2019-03-14 06:57:54 -04:00
|
|
|
shell.Provisioner `mapstructure:",squash"`
|
2015-06-14 14:23:29 -04:00
|
|
|
|
2019-11-21 19:43:13 -05:00
|
|
|
shell.ProvisionerRemoteSpecific `mapstructure:",squash"`
|
|
|
|
|
2015-06-14 14:23:29 -04:00
|
|
|
// The timeout for retrying to start the process. Until this timeout
|
|
|
|
// is reached, if the provisioner can't start a process, it retries.
|
|
|
|
// This can be set high to allow for reboots.
|
|
|
|
StartRetryTimeout time.Duration `mapstructure:"start_retry_timeout"`
|
|
|
|
|
|
|
|
ctx interpolate.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
type Provisioner struct {
|
|
|
|
config Config
|
|
|
|
}
|
|
|
|
|
|
|
|
type ExecuteCommandTemplate struct {
|
|
|
|
Vars string
|
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) Prepare(raws ...interface{}) error {
|
|
|
|
err := config.Decode(&p.config, &config.DecodeOpts{
|
2015-06-22 15:26:54 -04:00
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &p.config.ctx,
|
2015-06-14 14:23:29 -04:00
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{
|
|
|
|
"execute_command",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, raws...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.EnvVarFormat == "" {
|
|
|
|
p.config.EnvVarFormat = `set "%s=%s" && `
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.ExecuteCommand == "" {
|
|
|
|
p.config.ExecuteCommand = `{{.Vars}}"{{.Path}}"`
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.Inline != nil && len(p.config.Inline) == 0 {
|
|
|
|
p.config.Inline = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.StartRetryTimeout == 0 {
|
|
|
|
p.config.StartRetryTimeout = 5 * time.Minute
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.RemotePath == "" {
|
|
|
|
p.config.RemotePath = DefaultRemotePath
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.Scripts == nil {
|
|
|
|
p.config.Scripts = make([]string, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.Vars == nil {
|
|
|
|
p.config.Vars = make([]string, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
var errs error
|
|
|
|
if p.config.Script != "" && len(p.config.Scripts) > 0 {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
errors.New("Only one of script or scripts can be specified."))
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.Script != "" {
|
|
|
|
p.config.Scripts = []string{p.config.Script}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(p.config.Scripts) == 0 && p.config.Inline == nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
errors.New("Either a script file or inline script must be specified."))
|
|
|
|
} else if len(p.config.Scripts) > 0 && p.config.Inline != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
errors.New("Only a script file or an inline script can be specified, not both."))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, path := range p.config.Scripts {
|
|
|
|
if _, err := os.Stat(path); err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("Bad script '%s': %s", path, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do a check for bad environment variables, such as '=foo', 'foobar'
|
|
|
|
for _, kv := range p.config.Vars {
|
|
|
|
vs := strings.SplitN(kv, "=", 2)
|
|
|
|
if len(vs) != 2 || vs[0] == "" {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("Environment variable not in format 'key=value': %s", kv))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-28 23:45:25 -04:00
|
|
|
return errs
|
2015-06-14 14:23:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function takes the inline scripts, concatenates them
|
|
|
|
// into a temporary file and returns a string containing the location
|
|
|
|
// of said file.
|
|
|
|
func extractScript(p *Provisioner) (string, error) {
|
2018-12-12 09:45:00 -05:00
|
|
|
temp, err := tmp.File("windows-shell-provisioner")
|
2015-06-14 14:23:29 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Unable to create temporary file for inline scripts: %s", err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
writer := bufio.NewWriter(temp)
|
|
|
|
for _, command := range p.config.Inline {
|
|
|
|
log.Printf("Found command: %s", command)
|
|
|
|
if _, err := writer.WriteString(command + "\n"); err != nil {
|
|
|
|
return "", fmt.Errorf("Error preparing shell script: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := writer.Flush(); err != nil {
|
|
|
|
return "", fmt.Errorf("Error preparing shell script: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
temp.Close()
|
|
|
|
|
|
|
|
return temp.Name(), nil
|
|
|
|
}
|
|
|
|
|
2019-03-19 13:11:19 -04:00
|
|
|
func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator) error {
|
2015-06-14 14:23:29 -04:00
|
|
|
ui.Say(fmt.Sprintf("Provisioning with windows-shell..."))
|
|
|
|
scripts := make([]string, len(p.config.Scripts))
|
|
|
|
copy(scripts, p.config.Scripts)
|
|
|
|
|
|
|
|
if p.config.Inline != nil {
|
|
|
|
temp, err := extractScript(p)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Unable to extract inline scripts into a file: %s", err))
|
|
|
|
}
|
|
|
|
scripts = append(scripts, temp)
|
2018-04-25 19:20:17 -04:00
|
|
|
// Remove temp script containing the inline commands when done
|
|
|
|
defer os.Remove(temp)
|
2015-06-14 14:23:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, path := range scripts {
|
|
|
|
ui.Say(fmt.Sprintf("Provisioning with shell script: %s", path))
|
|
|
|
|
|
|
|
log.Printf("Opening %s for reading", path)
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error opening shell script: %s", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
// Create environment variables to set before executing the command
|
2018-03-13 03:50:25 -04:00
|
|
|
flattenedVars := p.createFlattenedEnvVars()
|
2015-06-14 14:23:29 -04:00
|
|
|
|
|
|
|
// Compile the command
|
|
|
|
p.config.ctx.Data = &ExecuteCommandTemplate{
|
2018-03-13 03:50:25 -04:00
|
|
|
Vars: flattenedVars,
|
2015-06-14 14:23:29 -04:00
|
|
|
Path: p.config.RemotePath,
|
|
|
|
}
|
|
|
|
command, err := interpolate.Render(p.config.ExecuteCommand, &p.config.ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error processing command: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Upload the file and run the command. Do this in the context of
|
|
|
|
// a single retryable function so that we don't end up with
|
|
|
|
// the case that the upload succeeded, a restart is initiated,
|
|
|
|
// and then the command is executed but the file doesn't exist
|
|
|
|
// any longer.
|
|
|
|
var cmd *packer.RemoteCmd
|
2019-04-08 11:57:27 -04:00
|
|
|
err = retry.Config{StartTimeout: p.config.StartRetryTimeout}.Run(ctx, func(ctx context.Context) error {
|
2015-06-14 14:23:29 -04:00
|
|
|
if _, err := f.Seek(0, 0); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := comm.Upload(p.config.RemotePath, f, nil); err != nil {
|
|
|
|
return fmt.Errorf("Error uploading script: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd = &packer.RemoteCmd{Command: command}
|
2019-04-03 11:14:55 -04:00
|
|
|
return cmd.RunWithUi(ctx, comm, ui)
|
2015-06-14 14:23:29 -04:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the original file since we copied it
|
|
|
|
f.Close()
|
|
|
|
|
2019-04-03 11:14:55 -04:00
|
|
|
if err := p.config.ValidExitCode(cmd.ExitStatus()); err != nil {
|
2019-03-14 07:47:22 -04:00
|
|
|
return err
|
2015-06-14 14:23:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-23 06:48:20 -05:00
|
|
|
func (p *Provisioner) createFlattenedEnvVars() (flattened string) {
|
2015-06-14 14:23:29 -04:00
|
|
|
flattened = ""
|
|
|
|
envVars := make(map[string]string)
|
|
|
|
|
|
|
|
// Always available Packer provided env vars
|
|
|
|
envVars["PACKER_BUILD_NAME"] = p.config.PackerBuildName
|
|
|
|
envVars["PACKER_BUILDER_TYPE"] = p.config.PackerBuilderType
|
2018-12-05 19:39:46 -05:00
|
|
|
|
|
|
|
// expose ip address variables
|
2017-01-17 20:17:36 -05:00
|
|
|
httpAddr := common.GetHTTPAddr()
|
|
|
|
if httpAddr != "" {
|
|
|
|
envVars["PACKER_HTTP_ADDR"] = httpAddr
|
|
|
|
}
|
2018-12-05 19:39:46 -05:00
|
|
|
httpIP := common.GetHTTPIP()
|
|
|
|
if httpIP != "" {
|
|
|
|
envVars["PACKER_HTTP_IP"] = httpIP
|
|
|
|
}
|
|
|
|
httpPort := common.GetHTTPPort()
|
|
|
|
if httpPort != "" {
|
|
|
|
envVars["PACKER_HTTP_PORT"] = httpPort
|
|
|
|
}
|
2015-06-14 14:23:29 -04:00
|
|
|
|
|
|
|
// Split vars into key/value components
|
|
|
|
for _, envVar := range p.config.Vars {
|
2017-01-18 13:11:20 -05:00
|
|
|
keyValue := strings.SplitN(envVar, "=", 2)
|
2015-06-14 14:23:29 -04:00
|
|
|
envVars[keyValue[0]] = keyValue[1]
|
|
|
|
}
|
|
|
|
// Create a list of env var keys in sorted order
|
|
|
|
var keys []string
|
|
|
|
for k := range envVars {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
// Re-assemble vars using OS specific format pattern and flatten
|
|
|
|
for _, key := range keys {
|
|
|
|
flattened += fmt.Sprintf(p.config.EnvVarFormat, key, envVars[key])
|
|
|
|
}
|
|
|
|
return
|
2016-11-06 11:02:00 -05:00
|
|
|
}
|