From 1b01e82d8df952c43afa869216d644e5d333dd19 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 8 Aug 2013 16:36:48 -0700 Subject: [PATCH] provisioner/shell: convert to latest template stuff --- provisioner/shell/provisioner.go | 49 ++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/provisioner/shell/provisioner.go b/provisioner/shell/provisioner.go index c81c39e90..23eed60d6 100644 --- a/provisioner/shell/provisioner.go +++ b/provisioner/shell/provisioner.go @@ -13,7 +13,6 @@ import ( "log" "os" "strings" - "text/template" ) const DefaultRemotePath = "/tmp/script.sh" @@ -48,6 +47,8 @@ type config struct { // Packer configurations, these come from Packer itself PackerBuildName string `mapstructure:"packer_build_name"` PackerBuilderType string `mapstructure:"packer_builder_type"` + + tpl *common.Template } type Provisioner struct { @@ -65,6 +66,11 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { return err } + p.config.tpl, err = common.NewTemplate() + if err != nil { + return err + } + // Accumulate any errors errs := common.CheckUnusedConfig(md) @@ -101,6 +107,38 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { 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 { errs = packer.MultiErrorAppend(errs, 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, " ") // Compile the command - var command bytes.Buffer - t := template.Must(template.New("command").Parse(p.config.ExecuteCommand)) - t.Execute(&command, &ExecuteCommandTemplate{flattendVars, p.config.RemotePath}) + command := p.config.tpl.Process(p.config.ExecuteCommand, &ExecuteCommandTemplate{ + Vars: flattendVars, + Path: p.config.RemotePath, + }) - cmd := &packer.RemoteCmd{Command: command.String()} + cmd := &packer.RemoteCmd{Command: command} log.Printf("Executing command: %s", cmd.Command) if err := cmd.StartWithUi(comm, ui); err != nil { return fmt.Errorf("Failed executing command: %s", err)