Merge pull request #8883 from hashicorp/fix_8835
Interpolate shell inline config
This commit is contained in:
commit
e94ff70199
|
@ -0,0 +1,43 @@
|
||||||
|
package testshelper
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/aws/aws-sdk-go/aws"
|
||||||
|
"github.com/aws/aws-sdk-go/service/ec2"
|
||||||
|
awscommon "github.com/hashicorp/packer/builder/amazon/common"
|
||||||
|
)
|
||||||
|
|
||||||
|
type AWSHelper struct {
|
||||||
|
Region string
|
||||||
|
AMIName string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *AWSHelper) CleanUpAmi(t *testing.T) {
|
||||||
|
accessConfig := &awscommon.AccessConfig{}
|
||||||
|
session, err := accessConfig.Session()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("AWSAMICleanUp: Unable to create aws session %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
regionconn := ec2.New(session.Copy(&aws.Config{
|
||||||
|
Region: aws.String(a.Region),
|
||||||
|
}))
|
||||||
|
|
||||||
|
resp, err := regionconn.DescribeImages(&ec2.DescribeImagesInput{
|
||||||
|
Owners: aws.StringSlice([]string{"self"}),
|
||||||
|
Filters: []*ec2.Filter{{
|
||||||
|
Name: aws.String("name"),
|
||||||
|
Values: aws.StringSlice([]string{a.AMIName}),
|
||||||
|
}}})
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("AWSAMICleanUp: Unable to find Image %s: %s", a.AMIName, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = regionconn.DeregisterImage(&ec2.DeregisterImageInput{
|
||||||
|
ImageId: resp.Images[0].ImageId,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("AWSAMICleanUp: Unable to Deregister Image %s", err.Error())
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,87 @@
|
||||||
|
package testshelper
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
amazonebsbuilder "github.com/hashicorp/packer/builder/amazon/ebs"
|
||||||
|
"github.com/hashicorp/packer/command"
|
||||||
|
"github.com/hashicorp/packer/packer"
|
||||||
|
fileprovisioner "github.com/hashicorp/packer/provisioner/file"
|
||||||
|
"github.com/hashicorp/packer/provisioner/shell"
|
||||||
|
)
|
||||||
|
|
||||||
|
// fileExists returns true if the filename is found
|
||||||
|
func FileExists(filename string) bool {
|
||||||
|
if _, err := os.Stat(filename); err == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// testCoreConfigBuilder creates a packer CoreConfig that has a file builder
|
||||||
|
// available. This allows us to test a builder that writes files to disk.
|
||||||
|
func testCoreConfigBuilder(t *testing.T) *packer.CoreConfig {
|
||||||
|
components := packer.ComponentFinder{
|
||||||
|
BuilderStore: packer.MapOfBuilder{
|
||||||
|
"amazon-ebs": func() (packer.Builder, error) { return &amazonebsbuilder.Builder{}, nil },
|
||||||
|
},
|
||||||
|
ProvisionerStore: packer.MapOfProvisioner{
|
||||||
|
"shell": func() (packer.Provisioner, error) { return &shell.Provisioner{}, nil },
|
||||||
|
"file": func() (packer.Provisioner, error) { return &fileprovisioner.Provisioner{}, nil },
|
||||||
|
},
|
||||||
|
PostProcessorStore: packer.MapOfPostProcessor{},
|
||||||
|
}
|
||||||
|
return &packer.CoreConfig{
|
||||||
|
Components: components,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestMetaFile creates a Meta object that includes a file builder
|
||||||
|
func TestMetaFile(t *testing.T) command.Meta {
|
||||||
|
var out, err bytes.Buffer
|
||||||
|
return command.Meta{
|
||||||
|
CoreConfig: testCoreConfigBuilder(t),
|
||||||
|
Ui: &packer.BasicUi{
|
||||||
|
Writer: &out,
|
||||||
|
ErrorWriter: &err,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func CleanupFiles(moreFiles ...string) {
|
||||||
|
for _, file := range moreFiles {
|
||||||
|
os.RemoveAll(file)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func FatalCommand(t *testing.T, m command.Meta) {
|
||||||
|
ui := m.Ui.(*packer.BasicUi)
|
||||||
|
out := ui.Writer.(*bytes.Buffer)
|
||||||
|
err := ui.ErrorWriter.(*bytes.Buffer)
|
||||||
|
t.Fatalf(
|
||||||
|
"Bad exit code.\n\nStdout:\n\n%s\n\nStderr:\n\n%s",
|
||||||
|
out.String(),
|
||||||
|
err.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
const TestEnvVar = "PACKER_ACC"
|
||||||
|
|
||||||
|
func AccTestPreValidate(t *testing.T) {
|
||||||
|
// We only run acceptance tests if an env var is set because they're
|
||||||
|
// slow and generally require some outside configuration.
|
||||||
|
if os.Getenv(TestEnvVar) == "" {
|
||||||
|
t.Skip(fmt.Sprintf(
|
||||||
|
"Acceptance tests skipped unless env '%s' set",
|
||||||
|
TestEnvVar))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// We require verbose mode so that the user knows what is going on.
|
||||||
|
if !testing.Verbose() {
|
||||||
|
t.Fatal("Acceptance tests must be run with the -v flag on tests")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
|
@ -95,7 +95,12 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
|
||||||
return nil
|
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{})
|
||||||
|
}
|
||||||
|
p.config.ctx.Data = generatedData
|
||||||
|
|
||||||
if p.config.Direction == "download" {
|
if p.config.Direction == "download" {
|
||||||
return p.ProvisionDownload(ui, comm)
|
return p.ProvisionDownload(ui, comm)
|
||||||
} else {
|
} else {
|
||||||
|
@ -105,7 +110,15 @@ func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.C
|
||||||
|
|
||||||
func (p *Provisioner) ProvisionDownload(ui packer.Ui, comm packer.Communicator) error {
|
func (p *Provisioner) ProvisionDownload(ui packer.Ui, comm packer.Communicator) error {
|
||||||
for _, src := range p.config.Sources {
|
for _, src := range p.config.Sources {
|
||||||
dst := p.config.Destination
|
src, err := interpolate.Render(src, &p.config.ctx)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Error interpolating source: %s", err)
|
||||||
|
}
|
||||||
|
dst, err := interpolate.Render(p.config.Destination, &p.config.ctx)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Error interpolating destination: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
ui.Say(fmt.Sprintf("Downloading %s => %s", src, dst))
|
ui.Say(fmt.Sprintf("Downloading %s => %s", src, dst))
|
||||||
// ensure destination dir exists. p.config.Destination may either be a file or a dir.
|
// ensure destination dir exists. p.config.Destination may either be a file or a dir.
|
||||||
dir := dst
|
dir := dst
|
||||||
|
@ -146,7 +159,15 @@ func (p *Provisioner) ProvisionDownload(ui packer.Ui, comm packer.Communicator)
|
||||||
|
|
||||||
func (p *Provisioner) ProvisionUpload(ui packer.Ui, comm packer.Communicator) error {
|
func (p *Provisioner) ProvisionUpload(ui packer.Ui, comm packer.Communicator) error {
|
||||||
for _, src := range p.config.Sources {
|
for _, src := range p.config.Sources {
|
||||||
dst := p.config.Destination
|
src, err := interpolate.Render(src, &p.config.ctx)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Error interpolating source: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
dst, err := interpolate.Render(p.config.Destination, &p.config.ctx)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Error interpolating destination: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
ui.Say(fmt.Sprintf("Uploading %s => %s", src, dst))
|
ui.Say(fmt.Sprintf("Uploading %s => %s", src, dst))
|
||||||
|
|
||||||
|
|
|
@ -70,12 +70,6 @@ type Provisioner struct {
|
||||||
config Config
|
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) ConfigSpec() hcldec.ObjectSpec { return p.config.FlatMapstructure().HCL2Spec() }
|
||||||
|
|
||||||
func (p *Provisioner) Prepare(raws ...interface{}) error {
|
func (p *Provisioner) Prepare(raws ...interface{}) error {
|
||||||
|
@ -181,7 +175,11 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
|
||||||
return nil
|
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))
|
scripts := make([]string, len(p.config.Scripts))
|
||||||
copy(scripts, 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 := bufio.NewWriter(tf)
|
||||||
writer.WriteString(fmt.Sprintf("#!%s\n", p.config.InlineShebang))
|
writer.WriteString(fmt.Sprintf("#!%s\n", p.config.InlineShebang))
|
||||||
for _, command := range p.config.Inline {
|
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 {
|
if _, err := writer.WriteString(command + "\n"); err != nil {
|
||||||
return fmt.Errorf("Error preparing shell script: %s", err)
|
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()
|
defer f.Close()
|
||||||
|
|
||||||
// Compile the command
|
// Compile the command
|
||||||
p.config.ctx.Data = &ExecuteCommandTemplate{
|
// These are extra variables that will be made available for interpolation.
|
||||||
Vars: flattenedEnvVars,
|
generatedData["Vars"] = flattenedEnvVars
|
||||||
EnvVarFile: p.config.envVarFile,
|
generatedData["EnvVarFile"] = p.config.envVarFile
|
||||||
Path: p.config.RemotePath,
|
generatedData["Path"] = p.config.RemotePath
|
||||||
}
|
p.config.ctx.Data = generatedData
|
||||||
|
|
||||||
command, err := interpolate.Render(p.config.ExecuteCommand, &p.config.ctx)
|
command, err := interpolate.Render(p.config.ExecuteCommand, &p.config.ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error processing command: %s", err)
|
return fmt.Errorf("Error processing command: %s", err)
|
||||||
|
|
|
@ -0,0 +1,41 @@
|
||||||
|
package shell_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hashicorp/go-uuid"
|
||||||
|
"github.com/hashicorp/packer/command"
|
||||||
|
testshelper "github.com/hashicorp/packer/helper/tests"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestBuildShellProvisionerWithBuildVariablesSharing(t *testing.T) {
|
||||||
|
testshelper.AccTestPreValidate(t)
|
||||||
|
|
||||||
|
UUID, _ := uuid.GenerateUUID()
|
||||||
|
os.Setenv("PACKER_RUN_UUID", UUID)
|
||||||
|
c := &command.BuildCommand{
|
||||||
|
Meta: testshelper.TestMetaFile(t),
|
||||||
|
}
|
||||||
|
|
||||||
|
file := "provisioner.shell." + UUID + ".txt"
|
||||||
|
defer testshelper.CleanupFiles(file)
|
||||||
|
|
||||||
|
args := []string{
|
||||||
|
filepath.Join("./test-fixtures", "shell-provisioner.json"),
|
||||||
|
}
|
||||||
|
if code := c.Run(args); code != 0 {
|
||||||
|
testshelper.FatalCommand(t, c.Meta)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !testshelper.FileExists(file) {
|
||||||
|
t.Errorf("Expected to find %s", file)
|
||||||
|
} else {
|
||||||
|
helper := testshelper.AWSHelper{
|
||||||
|
Region: "us-east-1",
|
||||||
|
AMIName: "packer-test-shell-interpolate",
|
||||||
|
}
|
||||||
|
helper.CleanUpAmi(t)
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
{
|
||||||
|
"builders": [
|
||||||
|
{
|
||||||
|
"type": "amazon-ebs",
|
||||||
|
"ami_name": "packer-test-shell-interpolate",
|
||||||
|
"instance_type": "m1.small",
|
||||||
|
"region": "us-east-1",
|
||||||
|
"ssh_username": "ubuntu",
|
||||||
|
"source_ami": "ami-0568456c",
|
||||||
|
"force_deregister" : true,
|
||||||
|
"tags": {
|
||||||
|
"packer-test": "true"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"provisioners": [
|
||||||
|
{
|
||||||
|
"type": "shell",
|
||||||
|
"inline": [
|
||||||
|
"echo {{ build `ID`}} > provisioner.{{ build `PackerRunUUID`}}.txt"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "file",
|
||||||
|
"source": "provisioner.{{ build `PackerRunUUID`}}.txt",
|
||||||
|
"destination": "provisioner.shell.{{ build `PackerRunUUID`}}.txt",
|
||||||
|
"direction": "download"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
Loading…
Reference in New Issue