provisioner/shell: convert to latest template stuff

This commit is contained in:
Mitchell Hashimoto 2013-08-08 16:36:48 -07:00
parent bf67c6c36e
commit f4b0e2248f
1 changed files with 44 additions and 5 deletions

View File

@ -13,7 +13,6 @@ import (
"log" "log"
"os" "os"
"strings" "strings"
"text/template"
) )
const DefaultRemotePath = "/tmp/script.sh" const DefaultRemotePath = "/tmp/script.sh"
@ -48,6 +47,8 @@ type config struct {
// Packer configurations, these come from Packer itself // Packer configurations, these come from Packer itself
PackerBuildName string `mapstructure:"packer_build_name"` PackerBuildName string `mapstructure:"packer_build_name"`
PackerBuilderType string `mapstructure:"packer_builder_type"` PackerBuilderType string `mapstructure:"packer_builder_type"`
tpl *common.Template
} }
type Provisioner struct { type Provisioner struct {
@ -65,6 +66,11 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
return err return err
} }
p.config.tpl, err = common.NewTemplate()
if err != nil {
return err
}
// Accumulate any errors // Accumulate any errors
errs := common.CheckUnusedConfig(md) errs := common.CheckUnusedConfig(md)
@ -101,6 +107,38 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
p.config.Scripts = []string{p.config.Script} p.config.Scripts = []string{p.config.Script}
} }
templates := map[string]*string{
"inline_shebang": &p.config.InlineShebang,
"script": &p.config.Script,
"remote_path": &p.config.RemotePath,
}
for n, ptr := range templates {
var err error
*ptr, err = p.config.tpl.Process(*ptr, nil)
if err != nil {
errs = packer.MultiErrorAppend(
errs, fmt.Errorf("Error processing %s: %s", n, err))
}
}
sliceTemplates := map[string][]string{
"inline": p.config.Inline,
"scripts": p.config.Scripts,
"environment_vars": p.config.Vars,
}
for n, slice := range sliceTemplates {
for i, elem := range slice {
var err error
slice[i], err = p.config.tpl.Process(elem, nil)
if err != nil {
errs = packer.MultiErrorAppend(
errs, fmt.Errorf("Error processing %s[%d]: %s", n, i, err))
}
}
}
if len(p.config.Scripts) == 0 && p.config.Inline == nil { if len(p.config.Scripts) == 0 && p.config.Inline == nil {
errs = packer.MultiErrorAppend(errs, errs = packer.MultiErrorAppend(errs,
errors.New("Either a script file or inline script must be specified.")) errors.New("Either a script file or inline script must be specified."))
@ -193,11 +231,12 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
flattendVars := strings.Join(envVars, " ") flattendVars := strings.Join(envVars, " ")
// Compile the command // Compile the command
var command bytes.Buffer command := p.config.tpl.Process(p.config.ExecuteCommand, &ExecuteCommandTemplate{
t := template.Must(template.New("command").Parse(p.config.ExecuteCommand)) Vars: flattendVars,
t.Execute(&command, &ExecuteCommandTemplate{flattendVars, p.config.RemotePath}) Path: p.config.RemotePath,
})
cmd := &packer.RemoteCmd{Command: command.String()} cmd := &packer.RemoteCmd{Command: command}
log.Printf("Executing command: %s", cmd.Command) log.Printf("Executing command: %s", cmd.Command)
if err := cmd.StartWithUi(comm, ui); err != nil { if err := cmd.StartWithUi(comm, ui); err != nil {
return fmt.Errorf("Failed executing command: %s", err) return fmt.Errorf("Failed executing command: %s", err)