From a3740bb9be69a8665dd4849797cc47bc56a25197 Mon Sep 17 00:00:00 2001 From: Moss Date: Thu, 12 Mar 2020 17:54:31 +0100 Subject: [PATCH] Interpolate shell inline config --- provisioner/shell/provisioner.go | 28 ++++++++++++++++------------ template.json | 29 +++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 template.json diff --git a/provisioner/shell/provisioner.go b/provisioner/shell/provisioner.go index 3828c07b4..38ab4c474 100644 --- a/provisioner/shell/provisioner.go +++ b/provisioner/shell/provisioner.go @@ -70,12 +70,6 @@ type Provisioner struct { config Config } -type ExecuteCommandTemplate struct { - Vars string - EnvVarFile string - Path string -} - func (p *Provisioner) ConfigSpec() hcldec.ObjectSpec { return p.config.FlatMapstructure().HCL2Spec() } func (p *Provisioner) Prepare(raws ...interface{}) error { @@ -181,7 +175,11 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { return nil } -func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, _ map[string]interface{}) error { +func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, generatedData map[string]interface{}) error { + if generatedData == nil { + generatedData = make(map[string]interface{}) + } + scripts := make([]string, len(p.config.Scripts)) copy(scripts, p.config.Scripts) @@ -201,6 +199,11 @@ func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.C writer := bufio.NewWriter(tf) writer.WriteString(fmt.Sprintf("#!%s\n", p.config.InlineShebang)) for _, command := range p.config.Inline { + p.config.ctx.Data = generatedData + command, err := interpolate.Render(command, &p.config.ctx); + if err != nil { + return fmt.Errorf("Error interpolating Inline: %s", err) + } if _, err := writer.WriteString(command + "\n"); err != nil { return fmt.Errorf("Error preparing shell script: %s", err) } @@ -279,11 +282,12 @@ func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.C defer f.Close() // Compile the command - p.config.ctx.Data = &ExecuteCommandTemplate{ - Vars: flattenedEnvVars, - EnvVarFile: p.config.envVarFile, - Path: p.config.RemotePath, - } + // These are extra variables that will be made available for interpolation. + generatedData["Vars"] = flattenedEnvVars + generatedData["EnvVarFile"] = p.config.envVarFile + generatedData["Path"] = p.config.RemotePath + p.config.ctx.Data = generatedData + command, err := interpolate.Render(p.config.ExecuteCommand, &p.config.ctx) if err != nil { return fmt.Errorf("Error processing command: %s", err) diff --git a/template.json b/template.json new file mode 100644 index 000000000..fff4778a5 --- /dev/null +++ b/template.json @@ -0,0 +1,29 @@ +{ + "builders": [ + { + "type": "amazon-ebs", + "ami_name": "moss-packer-whee", + "instance_type": "t2.micro", + "source_ami_filter": { + "filters": { + "virtualization-type": "hvm", + "name": "ubuntu/images/*ubuntu-xenial-16.04-amd64-server-*", + "root-device-type": "ebs" + }, + "owners": ["099720109477"], + "most_recent": true + }, + "ssh_username": "ubuntu" + } + ], + "provisioners": [ + { + "type": "shell-local", + "inline": ["echo MOSS packer run uuid is '{{ build `PackerRunUUID`}}'"] + }, + { + "type": "shell", + "inline": ["echo MOSS packer run uuid is '{{ build `PackerRunUUID`}}'"] + } + ] +}