builder/amazon/instance: upload bundle
This commit is contained in:
parent
ad1015f35c
commit
5e6695264a
|
@ -26,14 +26,15 @@ 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"`
|
||||||
BundleDestination string `mapstructure:"bundle_destination"`
|
BundleDestination string `mapstructure:"bundle_destination"`
|
||||||
BundlePrefix string `mapstructure:"bundle_prefix"`
|
BundlePrefix string `mapstructure:"bundle_prefix"`
|
||||||
BundleVolCommand string `mapstructure:"bundle_vol_command"`
|
BundleUploadCommand string `mapstructure:"bundle_upload_command"`
|
||||||
S3Bucket string `mapstructure:"s3_bucket"`
|
BundleVolCommand string `mapstructure:"bundle_vol_command"`
|
||||||
X509CertPath string `mapstructure:"x509_cert_path"`
|
S3Bucket string `mapstructure:"s3_bucket"`
|
||||||
X509KeyPath string `mapstructure:"x509_key_path"`
|
X509CertPath string `mapstructure:"x509_cert_path"`
|
||||||
X509UploadPath string `mapstructure:"x509_upload_path"`
|
X509KeyPath string `mapstructure:"x509_key_path"`
|
||||||
|
X509UploadPath string `mapstructure:"x509_upload_path"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Builder struct {
|
type Builder struct {
|
||||||
|
@ -55,15 +56,15 @@ func (b *Builder) Prepare(raws ...interface{}) error {
|
||||||
b.config.BundlePrefix = "image"
|
b.config.BundlePrefix = "image"
|
||||||
}
|
}
|
||||||
|
|
||||||
// Accumulate any errors
|
if b.config.BundleUploadCommand == "" {
|
||||||
errs := common.CheckUnusedConfig(md)
|
b.config.BundleUploadCommand = "sudo -n ec2-upload-bundle " +
|
||||||
errs = packer.MultiErrorAppend(errs, b.config.AccessConfig.Prepare()...)
|
"-b {{.BucketName}} " +
|
||||||
errs = packer.MultiErrorAppend(errs, b.config.RunConfig.Prepare()...)
|
"-m {{.ManifestPath}} " +
|
||||||
|
"-a {{.AccessKey}} " +
|
||||||
if b.config.AccountId == "" {
|
"-s {{.SecretKey}} " +
|
||||||
errs = packer.MultiErrorAppend(errs, errors.New("account_id is required"))
|
"-d {{.BundleDirectory}} " +
|
||||||
} else {
|
"--batch " +
|
||||||
b.config.AccountId = strings.Replace(b.config.AccountId, "-", "", -1)
|
"--retry"
|
||||||
}
|
}
|
||||||
|
|
||||||
if b.config.BundleVolCommand == "" {
|
if b.config.BundleVolCommand == "" {
|
||||||
|
@ -78,6 +79,17 @@ func (b *Builder) Prepare(raws ...interface{}) error {
|
||||||
"--batch"
|
"--batch"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Accumulate any errors
|
||||||
|
errs := common.CheckUnusedConfig(md)
|
||||||
|
errs = packer.MultiErrorAppend(errs, b.config.AccessConfig.Prepare()...)
|
||||||
|
errs = packer.MultiErrorAppend(errs, b.config.RunConfig.Prepare()...)
|
||||||
|
|
||||||
|
if b.config.AccountId == "" {
|
||||||
|
errs = packer.MultiErrorAppend(errs, errors.New("account_id is required"))
|
||||||
|
} else {
|
||||||
|
b.config.AccountId = strings.Replace(b.config.AccountId, "-", "", -1)
|
||||||
|
}
|
||||||
|
|
||||||
if b.config.S3Bucket == "" {
|
if b.config.S3Bucket == "" {
|
||||||
errs = packer.MultiErrorAppend(errs, errors.New("s3_bucket is required"))
|
errs = packer.MultiErrorAppend(errs, errors.New("s3_bucket is required"))
|
||||||
}
|
}
|
||||||
|
@ -150,6 +162,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
||||||
&common.StepProvision{},
|
&common.StepProvision{},
|
||||||
&StepUploadX509Cert{},
|
&StepUploadX509Cert{},
|
||||||
&StepBundleVolume{},
|
&StepBundleVolume{},
|
||||||
|
&StepUploadBundle{},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run!
|
// Run!
|
||||||
|
|
|
@ -0,0 +1,57 @@
|
||||||
|
package instance
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"github.com/mitchellh/multistep"
|
||||||
|
"github.com/mitchellh/packer/packer"
|
||||||
|
"text/template"
|
||||||
|
)
|
||||||
|
|
||||||
|
type uploadCmdData struct {
|
||||||
|
AccessKey string
|
||||||
|
BucketName string
|
||||||
|
BundleDirectory string
|
||||||
|
ManifestPath string
|
||||||
|
SecretKey string
|
||||||
|
}
|
||||||
|
|
||||||
|
type StepUploadBundle struct{}
|
||||||
|
|
||||||
|
func (s *StepUploadBundle) Run(state map[string]interface{}) multistep.StepAction {
|
||||||
|
comm := state["communicator"].(packer.Communicator)
|
||||||
|
config := state["config"].(*Config)
|
||||||
|
ui := state["ui"].(packer.Ui)
|
||||||
|
|
||||||
|
var uploadCmd bytes.Buffer
|
||||||
|
tData := uploadCmdData{
|
||||||
|
AccessKey: config.AccessKey,
|
||||||
|
BucketName: config.S3Bucket,
|
||||||
|
BundleDirectory: config.BundleDestination,
|
||||||
|
ManifestPath: fmt.Sprintf(
|
||||||
|
"%s/%s.manifest.xml", config.BundleDestination, config.BundlePrefix),
|
||||||
|
SecretKey: config.SecretKey,
|
||||||
|
}
|
||||||
|
t := template.Must(template.New("uploadCmd").Parse(config.BundleUploadCommand))
|
||||||
|
t.Execute(&uploadCmd, tData)
|
||||||
|
|
||||||
|
ui.Say("Uploading the bundle...")
|
||||||
|
cmd := &packer.RemoteCmd{Command: uploadCmd.String()}
|
||||||
|
if err := cmd.StartWithUi(comm, ui); err != nil {
|
||||||
|
state["error"] = fmt.Errorf("Error uploading volume: %s", err)
|
||||||
|
ui.Error(state["error"].(error).Error())
|
||||||
|
return multistep.ActionHalt
|
||||||
|
}
|
||||||
|
|
||||||
|
if cmd.ExitStatus != 0 {
|
||||||
|
state["error"] = fmt.Errorf(
|
||||||
|
"Bundle upload failed. Please see the output above for more\n" +
|
||||||
|
"details on what went wrong.")
|
||||||
|
ui.Error(state["error"].(error).Error())
|
||||||
|
return multistep.ActionHalt
|
||||||
|
}
|
||||||
|
|
||||||
|
return multistep.ActionContinue
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *StepUploadBundle) Cleanup(state map[string]interface{}) {}
|
Loading…
Reference in New Issue