From 2db0a03142f1396a4ba546c057ae05b2d243616e Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Wed, 12 Sep 2018 16:16:08 -0700 Subject: [PATCH] image upload WIP --- builder/oracle/classic/builder.go | 5 +-- builder/oracle/classic/config.go | 13 ++----- builder/oracle/classic/pv_config.go | 40 +++++++++++++++++++++ builder/oracle/classic/step_create_image.go | 28 +++++++++++++-- 4 files changed, 69 insertions(+), 17 deletions(-) create mode 100644 builder/oracle/classic/pv_config.go diff --git a/builder/oracle/classic/builder.go b/builder/oracle/classic/builder.go index 51aa5c6e5..2e0dcf6e2 100644 --- a/builder/oracle/classic/builder.go +++ b/builder/oracle/classic/builder.go @@ -34,10 +34,7 @@ func (b *Builder) Prepare(rawConfig ...interface{}) ([]string, error) { var errs *packer.MultiError - if b.config.PersistentVolumeSize > 0 && b.config.Comm.Type != "ssh" { - errs = packer.MultiErrorAppend(errs, - fmt.Errorf("Persistent storage volumes are only supported on unix, and must use the ssh communicator.")) - } + errs = packer.MultiErrorAppend(errs, b.config.PVConfig.Prepare(&b.config.ctx).Errors...) if errs != nil && len(errs.Errors) > 0 { return nil, errs diff --git a/builder/oracle/classic/config.go b/builder/oracle/classic/config.go index a4d43691b..804508140 100644 --- a/builder/oracle/classic/config.go +++ b/builder/oracle/classic/config.go @@ -18,6 +18,7 @@ import ( type Config struct { common.PackerConfig `mapstructure:",squash"` + PVConfig `mapstructure:",squash"` Comm communicator.Config `mapstructure:",squash"` attribs map[string]interface{} @@ -29,16 +30,6 @@ type Config struct { apiEndpointURL *url.URL // Image - // PersistentVolumeSize lets us control the volume size by using persistent boot storage - PersistentVolumeSize int `mapstructure:"persistent_volume_size"` - /* TODO: - builder image list - default to OL image - make sure if set then PVS is above - some way to choose which connection to use for master - possible ignore everything for builder and always use SSH keys - */ - ImageName string `mapstructure:"image_name"` Shape string `mapstructure:"shape"` SourceImageList string `mapstructure:"source_image_list"` @@ -92,6 +83,8 @@ func NewConfig(raws ...interface{}) (*Config, error) { c.SnapshotTimeout = 20 * time.Minute } + // if using a persistent volume + // Validate that all required fields are present var errs *packer.MultiError required := map[string]string{ diff --git a/builder/oracle/classic/pv_config.go b/builder/oracle/classic/pv_config.go new file mode 100644 index 000000000..a2a1b7d1a --- /dev/null +++ b/builder/oracle/classic/pv_config.go @@ -0,0 +1,40 @@ +package classic + +import ( + "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/template/interpolate" +) + +type PVConfig struct { + // PersistentVolumeSize lets us control the volume size by using persistent boot storage + PersistentVolumeSize int `mapstructure:"persistent_volume_size"` + BuilderImageList string `mapstructure:"builder_image_list"` + BuilderInstallUploadToolCommand string `mapstructure:"builder_install_upload_tool_command"` + BuilderUploadImageCommand string `mapstructure:"builder_upload_image_command"` + /* TODO: + default to OL image + make sure if set then PVS is above + some way to choose which connection to use for master + possible ignore everything for builder and always use SSH keys + */ +} + +func (c *PVConfig) Prepare(ctx *interpolate.Context) (errs *packer.MultiError) { + if c.PersistentVolumeSize == 0 { + return nil + } + + if c.BuilderUploadImageCommand == "" { + c.BuilderUploadImageCommand = `curl --connect-timeout 5 \ +--max-time 3600 \ +--retry 5 \ +--retry-delay 0 \ +-o {{ .DiskImagePath }} \ +'...'` + } + /* + errs = packer.MultiErrorAppend(errs, + fmt.Errorf("Persistent storage volumes are only supported on unix, and must use the ssh communicator.")) + */ + return +} diff --git a/builder/oracle/classic/step_create_image.go b/builder/oracle/classic/step_create_image.go index 486997a5b..b5181e00f 100644 --- a/builder/oracle/classic/step_create_image.go +++ b/builder/oracle/classic/step_create_image.go @@ -7,24 +7,46 @@ import ( "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/template/interpolate" ) -type stepCreateImage struct{} +type stepCreateImage struct { + installUploadToolCommand string + uploadImageCommand string +} + +type uploadCmdData struct { + DiskImagePath string +} func (s *stepCreateImage) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { //hook := state.Get("hook").(packer.Hook) ui := state.Get("ui").(packer.Ui) comm := state.Get("communicator").(packer.Communicator) + config := state.Get("config").(*Config) + + config.ctx.Data = uploadCmdData{ + DiskImagePath: "./diskimage.tar.gz", + } + uploadImageCmd, err := interpolate.Render(s.uploadImageCommand, &config.ctx) + if err != nil { + err := fmt.Errorf("Error processing image upload command: %s", err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } - command := `#!/bin/sh + command := fmt.Sprintf(`#!/bin/sh set -e + %s mkdir /builder mkfs -t ext3 /dev/xvdb mount /dev/xvdb /builder chown opc:opc /builder cd /builder dd if=/dev/xvdc bs=8M status=progress | cp --sparse=always /dev/stdin diskimage.raw - tar czSf ./diskimage.tar.gz ./diskimage.raw` + tar czSf ./diskimage.tar.gz ./diskimage.raw + %s`, s.installUploadToolCommand, uploadImageCmd) dest := "/tmp/create-packer-diskimage.sh" comm.Upload(dest, strings.NewReader(command), nil)