Commit 87717216 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

builder/amazon/instance: bundle volume and keep track of dir

parent 63474f47
...@@ -26,11 +26,14 @@ type Config struct { ...@@ -26,11 +26,14 @@ type Config struct {
awscommon.AccessConfig `mapstructure:",squash"` awscommon.AccessConfig `mapstructure:",squash"`
awscommon.RunConfig `mapstructure:",squash"` awscommon.RunConfig `mapstructure:",squash"`
AccountId string `mapstructure:"account_id"` AccountId string `mapstructure:"account_id"`
BundleVolCommand string `mapstructure:"bundle_vol_command"` BundleDestination string `mapstructure:"bundle_destination"`
X509CertPath string `mapstructure:"x509_cert_path"` BundlePrefix string `mapstructure:"bundle_prefix"`
X509KeyPath string `mapstructure:"x509_key_path"` BundleVolCommand string `mapstructure:"bundle_vol_command"`
X509UploadPath string `mapstructure:"x509_upload_path"` S3Bucket string `mapstructure:"s3_bucket"`
X509CertPath string `mapstructure:"x509_cert_path"`
X509KeyPath string `mapstructure:"x509_key_path"`
X509UploadPath string `mapstructure:"x509_upload_path"`
} }
type Builder struct { type Builder struct {
...@@ -44,6 +47,14 @@ func (b *Builder) Prepare(raws ...interface{}) error { ...@@ -44,6 +47,14 @@ func (b *Builder) Prepare(raws ...interface{}) error {
return err return err
} }
if b.config.BundleDestination == "" {
b.config.BundleDestination = "/tmp"
}
if b.config.BundlePrefix == "" {
b.config.BundlePrefix = "image"
}
// Accumulate any errors // Accumulate any errors
errs := common.CheckUnusedConfig(md) errs := common.CheckUnusedConfig(md)
errs = packer.MultiErrorAppend(errs, b.config.AccessConfig.Prepare()...) errs = packer.MultiErrorAppend(errs, b.config.AccessConfig.Prepare()...)
...@@ -62,9 +73,15 @@ func (b *Builder) Prepare(raws ...interface{}) error { ...@@ -62,9 +73,15 @@ func (b *Builder) Prepare(raws ...interface{}) error {
"-c {{.CertPath}} " + "-c {{.CertPath}} " +
"-r {{.Architecture}} " + "-r {{.Architecture}} " +
"-e {{.PrivatePath}} " + "-e {{.PrivatePath}} " +
"-d {{.Destination}} " +
"-p {{.Prefix}} " +
"--batch" "--batch"
} }
if b.config.S3Bucket == "" {
errs = packer.MultiErrorAppend(errs, errors.New("s3_bucket is required"))
}
if b.config.X509CertPath == "" { if b.config.X509CertPath == "" {
errs = packer.MultiErrorAppend(errs, errors.New("x509_cert_path is required")) errs = packer.MultiErrorAppend(errs, errors.New("x509_cert_path is required"))
} else if _, err := os.Stat(b.config.X509CertPath); err != nil { } else if _, err := os.Stat(b.config.X509CertPath); err != nil {
......
...@@ -17,6 +17,7 @@ func testConfig() map[string]interface{} { ...@@ -17,6 +17,7 @@ func testConfig() map[string]interface{} {
"account_id": "foo", "account_id": "foo",
"instance_type": "m1.small", "instance_type": "m1.small",
"region": "us-east-1", "region": "us-east-1",
"s3_bucket": "foo",
"source_ami": "foo", "source_ami": "foo",
"ssh_username": "bob", "ssh_username": "bob",
"x509_cert_path": tf.Name(), "x509_cert_path": tf.Name(),
...@@ -60,6 +61,36 @@ func TestBuilderPrepare_AccountId(t *testing.T) { ...@@ -60,6 +61,36 @@ func TestBuilderPrepare_AccountId(t *testing.T) {
} }
} }
func TestBuilderPrepare_BundleDestination(t *testing.T) {
b := &Builder{}
config := testConfig()
config["bundle_destination"] = ""
err := b.Prepare(config)
if err != nil {
t.Fatalf("err: %s", err)
}
if b.config.BundleDestination != "/tmp" {
t.Fatalf("bad: %s", b.config.BundleDestination)
}
}
func TestBuilderPrepare_BundlePrefix(t *testing.T) {
b := &Builder{}
config := testConfig()
config["bundle_prefix"] = ""
err := b.Prepare(config)
if err != nil {
t.Fatalf("err: %s", err)
}
if b.config.BundlePrefix != "image" {
t.Fatalf("bad: %s", b.config.BundlePrefix)
}
}
func TestBuilderPrepare_InvalidKey(t *testing.T) { func TestBuilderPrepare_InvalidKey(t *testing.T) {
var b Builder var b Builder
config := testConfig() config := testConfig()
...@@ -72,6 +103,23 @@ func TestBuilderPrepare_InvalidKey(t *testing.T) { ...@@ -72,6 +103,23 @@ func TestBuilderPrepare_InvalidKey(t *testing.T) {
} }
} }
func TestBuilderPrepare_S3Bucket(t *testing.T) {
b := &Builder{}
config := testConfig()
config["s3_bucket"] = ""
err := b.Prepare(config)
if err == nil {
t.Fatal("should have error")
}
config["s3_bucket"] = "foo"
err = b.Prepare(config)
if err != nil {
t.Errorf("err: %s", err)
}
}
func TestBuilderPrepare_X509CertPath(t *testing.T) { func TestBuilderPrepare_X509CertPath(t *testing.T) {
b := &Builder{} b := &Builder{}
config := testConfig() config := testConfig()
......
...@@ -13,7 +13,9 @@ type bundleCmdData struct { ...@@ -13,7 +13,9 @@ type bundleCmdData struct {
AccountId string AccountId string
Architecture string Architecture string
CertPath string CertPath string
Destination string
KeyPath string KeyPath string
Prefix string
PrivatePath string PrivatePath string
} }
...@@ -52,7 +54,9 @@ func (s *StepBundleVolume) Run(state map[string]interface{}) multistep.StepActio ...@@ -52,7 +54,9 @@ func (s *StepBundleVolume) Run(state map[string]interface{}) multistep.StepActio
AccountId: config.AccountId, AccountId: config.AccountId,
Architecture: instance.Architecture, Architecture: instance.Architecture,
CertPath: x509RemoteCertPath, CertPath: x509RemoteCertPath,
Destination: config.BundleDestination,
KeyPath: x509RemoteKeyPath, KeyPath: x509RemoteKeyPath,
Prefix: config.BundlePrefix,
PrivatePath: config.X509UploadPath, PrivatePath: config.X509UploadPath,
} }
t := template.Must(template.New("bundleCmd").Parse(config.BundleVolCommand)) t := template.Must(template.New("bundleCmd").Parse(config.BundleVolCommand))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment