image upload WIP

This commit is contained in:
Matthew Hooker 2018-09-12 16:16:08 -07:00
parent 265ee0b0b8
commit 2db0a03142
No known key found for this signature in database
GPG Key ID: 7B5F933D9CE8C6A1
4 changed files with 69 additions and 17 deletions

View File

@ -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

View File

@ -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{

View File

@ -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
}

View File

@ -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)
command := `#!/bin/sh
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 := 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)