Interpolate file provisioner and add integration tests
This commit is contained in:
parent
6c06a2a048
commit
7fbbbffd5c
|
@ -95,7 +95,12 @@ 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{})
|
||||
}
|
||||
p.config.ctx.Data = generatedData
|
||||
|
||||
if p.config.Direction == "download" {
|
||||
return p.ProvisionDownload(ui, comm)
|
||||
} 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 {
|
||||
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))
|
||||
// ensure destination dir exists. p.config.Destination may either be a file or a dir.
|
||||
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 {
|
||||
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))
|
||||
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
package helper
|
||||
|
||||
import (
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
awscommon "github.com/hashicorp/packer/builder/amazon/common"
|
||||
"testing"
|
||||
)
|
||||
|
||||
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,78 @@
|
|||
// +build integration
|
||||
|
||||
package shell_integration
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/hashicorp/go-uuid"
|
||||
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"
|
||||
testshelper "github.com/hashicorp/packer/test/helper"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBuildShellProvisionerWithBuildVariablesSharing(t *testing.T) {
|
||||
UUID, _ := uuid.GenerateUUID()
|
||||
os.Setenv("PACKER_RUN_UUID", UUID)
|
||||
c := &command.BuildCommand{
|
||||
Meta: testMetaFile(t),
|
||||
}
|
||||
|
||||
file := "provisioner.shell." + UUID + ".txt"
|
||||
defer os.RemoveAll(file)
|
||||
|
||||
args := []string{
|
||||
filepath.Join("./test-fixtures", "shell-provisioner.json"),
|
||||
}
|
||||
if code := c.Run(args); code != 0 {
|
||||
ui := c.Meta.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())
|
||||
}
|
||||
|
||||
if _, err := os.Stat(file); err != nil {
|
||||
t.Errorf("Expected to find %s", file)
|
||||
} else {
|
||||
helper := testshelper.AWSHelper{
|
||||
Region: "us-east-1",
|
||||
AMIName: "packer-test-shell-interpolate",
|
||||
}
|
||||
helper.CleanUpAmi(t)
|
||||
}
|
||||
}
|
||||
|
||||
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 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,
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
{
|
||||
"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",
|
||||
"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