2013-07-25 00:29:21 -04:00
|
|
|
package instance
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-07-24 19:30:30 -04:00
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2013-07-25 00:29:21 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type uploadCmdData struct {
|
|
|
|
AccessKey string
|
|
|
|
BucketName string
|
|
|
|
BundleDirectory string
|
|
|
|
ManifestPath string
|
2014-08-18 14:42:32 -04:00
|
|
|
Region string
|
2013-07-25 00:29:21 -04:00
|
|
|
SecretKey string
|
2017-08-28 14:51:37 -04:00
|
|
|
Token string
|
2013-07-25 00:29:21 -04:00
|
|
|
}
|
|
|
|
|
2014-07-24 19:30:30 -04:00
|
|
|
type StepUploadBundle struct {
|
|
|
|
Debug bool
|
|
|
|
}
|
2013-07-25 00:29:21 -04:00
|
|
|
|
2013-08-31 16:03:13 -04:00
|
|
|
func (s *StepUploadBundle) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
comm := state.Get("communicator").(packer.Communicator)
|
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
manifestName := state.Get("manifest_name").(string)
|
|
|
|
manifestPath := state.Get("manifest_path").(string)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-07-25 00:29:21 -04:00
|
|
|
|
2015-08-13 09:58:15 -04:00
|
|
|
accessKey := config.AccessKey
|
|
|
|
secretKey := config.SecretKey
|
2017-03-01 19:43:09 -05:00
|
|
|
session, err := config.AccessConfig.Session()
|
2017-10-30 17:17:19 -04:00
|
|
|
region := *session.Config.Region
|
2017-03-01 19:43:09 -05:00
|
|
|
accessConfig := session.Config
|
2017-08-28 14:51:37 -04:00
|
|
|
var token string
|
2015-08-13 09:58:15 -04:00
|
|
|
if err == nil && accessKey == "" && secretKey == "" {
|
|
|
|
credentials, err := accessConfig.Credentials.Get()
|
|
|
|
if err == nil {
|
|
|
|
accessKey = credentials.AccessKeyID
|
|
|
|
secretKey = credentials.SecretAccessKey
|
2017-08-28 14:51:37 -04:00
|
|
|
token = credentials.SessionToken
|
2015-08-13 09:58:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-27 14:47:45 -04:00
|
|
|
config.ctx.Data = uploadCmdData{
|
2015-08-13 09:58:15 -04:00
|
|
|
AccessKey: accessKey,
|
2013-07-25 00:29:21 -04:00
|
|
|
BucketName: config.S3Bucket,
|
|
|
|
BundleDirectory: config.BundleDestination,
|
2013-07-25 00:51:46 -04:00
|
|
|
ManifestPath: manifestPath,
|
2015-04-05 17:58:48 -04:00
|
|
|
Region: region,
|
2015-08-13 09:58:15 -04:00
|
|
|
SecretKey: secretKey,
|
2017-08-28 14:51:37 -04:00
|
|
|
Token: token,
|
2015-05-27 14:47:45 -04:00
|
|
|
}
|
2015-06-22 12:22:42 -04:00
|
|
|
config.BundleUploadCommand, err = interpolate.Render(config.BundleUploadCommand, &config.ctx)
|
2013-08-08 18:27:12 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error processing bundle upload command: %s", err)
|
2013-08-31 16:03:13 -04:00
|
|
|
state.Put("error", err)
|
2013-08-08 18:27:12 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
2013-07-25 00:29:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Uploading the bundle...")
|
2013-08-08 18:27:12 -04:00
|
|
|
cmd := &packer.RemoteCmd{Command: config.BundleUploadCommand}
|
2014-07-24 19:30:30 -04:00
|
|
|
|
|
|
|
if s.Debug {
|
|
|
|
ui.Say(fmt.Sprintf("Running: %s", config.BundleUploadCommand))
|
|
|
|
}
|
|
|
|
|
2013-07-25 00:29:21 -04:00
|
|
|
if err := cmd.StartWithUi(comm, ui); err != nil {
|
2013-08-31 16:03:13 -04:00
|
|
|
state.Put("error", fmt.Errorf("Error uploading volume: %s", err))
|
|
|
|
ui.Error(state.Get("error").(error).Error())
|
2013-07-25 00:29:21 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
if cmd.ExitStatus != 0 {
|
2017-10-05 20:31:39 -04:00
|
|
|
if cmd.ExitStatus == 3 {
|
|
|
|
ui.Error(fmt.Sprintf("Please check that the bucket `%s` "+
|
|
|
|
"does not exist, or exists and is writable. This error "+
|
|
|
|
"indicates that the bucket may be owned by somebody else.",
|
|
|
|
config.S3Bucket))
|
|
|
|
}
|
2013-08-31 16:03:13 -04:00
|
|
|
state.Put("error", fmt.Errorf(
|
|
|
|
"Bundle upload failed. Please see the output above for more\n"+
|
|
|
|
"details on what went wrong."))
|
|
|
|
ui.Error(state.Get("error").(error).Error())
|
2013-07-25 00:29:21 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-08-31 16:03:13 -04:00
|
|
|
state.Put("remote_manifest_path", fmt.Sprintf(
|
|
|
|
"%s/%s", config.S3Bucket, manifestName))
|
2013-07-25 01:19:04 -04:00
|
|
|
|
2013-07-25 00:29:21 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 16:03:13 -04:00
|
|
|
func (s *StepUploadBundle) Cleanup(state multistep.StateBag) {}
|