refactor: change bsusurrogate builder to new SDK
This commit is contained in:
parent
007f6cce4c
commit
b844b7f1c7
|
@ -117,18 +117,21 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
|
||||||
|
|
||||||
oapiconn := oapi.NewClient(clientConfig, skipClient)
|
oapiconn := oapi.NewClient(clientConfig, skipClient)
|
||||||
|
|
||||||
|
oscConn := b.config.NewOSCClient()
|
||||||
|
|
||||||
// Setup the state bag and initial state for the steps
|
// Setup the state bag and initial state for the steps
|
||||||
state := new(multistep.BasicStateBag)
|
state := new(multistep.BasicStateBag)
|
||||||
state.Put("config", &b.config)
|
state.Put("config", &b.config)
|
||||||
state.Put("oapi", oapiconn)
|
state.Put("oapi", oapiconn)
|
||||||
state.Put("clientConfig", clientConfig)
|
state.Put("osc", oscConn)
|
||||||
|
state.Put("accessConfig", &b.config.AccessConfig)
|
||||||
state.Put("hook", hook)
|
state.Put("hook", hook)
|
||||||
state.Put("ui", ui)
|
state.Put("ui", ui)
|
||||||
|
|
||||||
//VMStep
|
//VMStep
|
||||||
|
|
||||||
omiDevices := b.config.BuildOMIDevices()
|
omiDevices := b.config.BuildOscOMIDevices()
|
||||||
launchDevices := b.config.BuildLaunchDevices()
|
launchOSCDevices := b.config.BuildOSCLaunchDevices()
|
||||||
|
|
||||||
steps := []multistep.Step{
|
steps := []multistep.Step{
|
||||||
&osccommon.StepPreValidate{
|
&osccommon.StepPreValidate{
|
||||||
|
@ -207,7 +210,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
|
||||||
DisableStopVm: b.config.DisableStopVm,
|
DisableStopVm: b.config.DisableStopVm,
|
||||||
},
|
},
|
||||||
&StepSnapshotVolumes{
|
&StepSnapshotVolumes{
|
||||||
LaunchDevices: launchDevices,
|
LaunchDevices: launchOSCDevices,
|
||||||
},
|
},
|
||||||
&osccommon.StepDeregisterOMI{
|
&osccommon.StepDeregisterOMI{
|
||||||
AccessConfig: &b.config.AccessConfig,
|
AccessConfig: &b.config.AccessConfig,
|
||||||
|
@ -219,7 +222,8 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
|
||||||
&StepRegisterOMI{
|
&StepRegisterOMI{
|
||||||
RootDevice: b.config.RootDevice,
|
RootDevice: b.config.RootDevice,
|
||||||
OMIDevices: omiDevices,
|
OMIDevices: omiDevices,
|
||||||
LaunchDevices: launchDevices,
|
LaunchDevices: launchOSCDevices,
|
||||||
|
RawRegion: b.config.RawRegion,
|
||||||
},
|
},
|
||||||
&osccommon.StepUpdateOMIAttributes{
|
&osccommon.StepUpdateOMIAttributes{
|
||||||
AccountIds: b.config.OMIAccountIDs,
|
AccountIds: b.config.OMIAccountIDs,
|
||||||
|
|
|
@ -24,7 +24,7 @@ const testBuilderAccBasic = `
|
||||||
"type": "test",
|
"type": "test",
|
||||||
"region": "eu-west-2",
|
"region": "eu-west-2",
|
||||||
"vm_type": "t2.micro",
|
"vm_type": "t2.micro",
|
||||||
"source_omi": "ami-65efcc11",
|
"source_omi": "ami-abe953fa",
|
||||||
"ssh_username": "outscale",
|
"ssh_username": "outscale",
|
||||||
"omi_name": "packer-test {{timestamp}}",
|
"omi_name": "packer-test {{timestamp}}",
|
||||||
"omi_virtualization_type": "hvm",
|
"omi_virtualization_type": "hvm",
|
||||||
|
|
|
@ -4,23 +4,27 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/antihax/optional"
|
||||||
osccommon "github.com/hashicorp/packer/builder/osc/common"
|
osccommon "github.com/hashicorp/packer/builder/osc/common"
|
||||||
"github.com/hashicorp/packer/helper/multistep"
|
"github.com/hashicorp/packer/helper/multistep"
|
||||||
"github.com/hashicorp/packer/packer"
|
"github.com/hashicorp/packer/packer"
|
||||||
"github.com/outscale/osc-go/oapi"
|
"github.com/outscale/osc-go/oapi"
|
||||||
|
"github.com/outscale/osc-sdk-go/osc"
|
||||||
)
|
)
|
||||||
|
|
||||||
// StepRegisterOMI creates the OMI.
|
// StepRegisterOMI creates the OMI.
|
||||||
type StepRegisterOMI struct {
|
type StepRegisterOMI struct {
|
||||||
RootDevice RootBlockDevice
|
RootDevice RootBlockDevice
|
||||||
OMIDevices []oapi.BlockDeviceMappingImage
|
OMIDevices []osc.BlockDeviceMappingImage
|
||||||
LaunchDevices []oapi.BlockDeviceMappingVmCreation
|
LaunchDevices []osc.BlockDeviceMappingVmCreation
|
||||||
image *oapi.Image
|
image *osc.Image
|
||||||
|
RawRegion string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StepRegisterOMI) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
func (s *StepRegisterOMI) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
||||||
config := state.Get("config").(*Config)
|
config := state.Get("config").(*Config)
|
||||||
oapiconn := state.Get("oapi").(*oapi.Client)
|
oapiconn := state.Get("oapi").(*oapi.Client)
|
||||||
|
oscconn := state.Get("osc").(*osc.APIClient)
|
||||||
snapshotIds := state.Get("snapshot_ids").(map[string]string)
|
snapshotIds := state.Get("snapshot_ids").(map[string]string)
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
|
|
||||||
|
@ -28,14 +32,16 @@ func (s *StepRegisterOMI) Run(ctx context.Context, state multistep.StateBag) mul
|
||||||
|
|
||||||
blockDevices := s.combineDevices(snapshotIds)
|
blockDevices := s.combineDevices(snapshotIds)
|
||||||
|
|
||||||
registerOpts := oapi.CreateImageRequest{
|
registerOpts := osc.CreateImageRequest{
|
||||||
ImageName: config.OMIName,
|
ImageName: config.OMIName,
|
||||||
Architecture: "x86_64",
|
Architecture: "x86_64",
|
||||||
RootDeviceName: s.RootDevice.DeviceName,
|
RootDeviceName: s.RootDevice.DeviceName,
|
||||||
BlockDeviceMappings: blockDevices,
|
BlockDeviceMappings: blockDevices,
|
||||||
}
|
}
|
||||||
|
|
||||||
registerResp, err := oapiconn.POST_CreateImage(registerOpts)
|
registerResp, _, err := oscconn.ImageApi.CreateImage(context.Background(), &osc.CreateImageOpts{
|
||||||
|
CreateImageRequest: optional.NewInterface(registerOpts),
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
state.Put("error", fmt.Errorf("Error registering OMI: %s", err))
|
state.Put("error", fmt.Errorf("Error registering OMI: %s", err))
|
||||||
ui.Error(state.Get("error").(error).Error())
|
ui.Error(state.Get("error").(error).Error())
|
||||||
|
@ -43,24 +49,26 @@ func (s *StepRegisterOMI) Run(ctx context.Context, state multistep.StateBag) mul
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the OMI ID in the state
|
// Set the OMI ID in the state
|
||||||
ui.Say(fmt.Sprintf("OMI: %s", registerResp.OK.Image.ImageId))
|
ui.Say(fmt.Sprintf("OMI: %s", registerResp.Image.ImageId))
|
||||||
omis := make(map[string]string)
|
omis := make(map[string]string)
|
||||||
omis[oapiconn.GetConfig().Region] = registerResp.OK.Image.ImageId
|
omis[oapiconn.GetConfig().Region] = registerResp.Image.ImageId
|
||||||
state.Put("omis", omis)
|
state.Put("omis", omis)
|
||||||
|
|
||||||
// Wait for the image to become ready
|
// Wait for the image to become ready
|
||||||
ui.Say("Waiting for OMI to become ready...")
|
ui.Say("Waiting for OMI to become ready...")
|
||||||
if err := osccommon.WaitUntilImageAvailable(oapiconn, registerResp.OK.Image.ImageId); err != nil {
|
if err := osccommon.WaitUntilOscImageAvailable(oscconn, registerResp.Image.ImageId); err != nil {
|
||||||
err := fmt.Errorf("Error waiting for OMI: %s", err)
|
err := fmt.Errorf("Error waiting for OMI: %s", err)
|
||||||
state.Put("error", err)
|
state.Put("error", err)
|
||||||
ui.Error(err.Error())
|
ui.Error(err.Error())
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
|
|
||||||
imagesResp, err := oapiconn.POST_ReadImages(oapi.ReadImagesRequest{
|
imagesResp, _, err := oscconn.ImageApi.ReadImages(context.Background(), &osc.ReadImagesOpts{
|
||||||
Filters: oapi.FiltersImage{
|
ReadImagesRequest: optional.NewInterface(osc.ReadImagesRequest{
|
||||||
ImageIds: []string{registerResp.OK.Image.ImageId},
|
Filters: osc.FiltersImage{
|
||||||
|
ImageIds: []string{registerResp.Image.ImageId},
|
||||||
},
|
},
|
||||||
|
}),
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -69,12 +77,12 @@ func (s *StepRegisterOMI) Run(ctx context.Context, state multistep.StateBag) mul
|
||||||
ui.Error(err.Error())
|
ui.Error(err.Error())
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
s.image = &imagesResp.OK.Images[0]
|
s.image = &imagesResp.Images[0]
|
||||||
|
|
||||||
snapshots := make(map[string][]string)
|
snapshots := make(map[string][]string)
|
||||||
for _, blockDeviceMapping := range imagesResp.OK.Images[0].BlockDeviceMappings {
|
for _, blockDeviceMapping := range imagesResp.Images[0].BlockDeviceMappings {
|
||||||
if blockDeviceMapping.Bsu.SnapshotId != "" {
|
if blockDeviceMapping.Bsu.SnapshotId != "" {
|
||||||
snapshots[oapiconn.GetConfig().Region] = append(snapshots[oapiconn.GetConfig().Region], blockDeviceMapping.Bsu.SnapshotId)
|
snapshots[s.RawRegion] = append(snapshots[s.RawRegion], blockDeviceMapping.Bsu.SnapshotId)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
state.Put("snapshots", snapshots)
|
state.Put("snapshots", snapshots)
|
||||||
|
@ -93,19 +101,23 @@ func (s *StepRegisterOMI) Cleanup(state multistep.StateBag) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
oapiconn := state.Get("oapi").(*oapi.Client)
|
oscconn := state.Get("osc").(*osc.APIClient)
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
|
|
||||||
ui.Say("Deregistering the OMI because cancellation or error...")
|
ui.Say("Deregistering the OMI because cancellation or error...")
|
||||||
deregisterOpts := oapi.DeleteImageRequest{ImageId: s.image.ImageId}
|
deregisterOpts := osc.DeleteImageRequest{ImageId: s.image.ImageId}
|
||||||
if _, err := oapiconn.POST_DeleteImage(deregisterOpts); err != nil {
|
_, _, err := oscconn.ImageApi.DeleteImage(context.Background(), &osc.DeleteImageOpts{
|
||||||
|
DeleteImageRequest: optional.NewInterface(deregisterOpts),
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
ui.Error(fmt.Sprintf("Error deregistering OMI, may still be around: %s", err))
|
ui.Error(fmt.Sprintf("Error deregistering OMI, may still be around: %s", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StepRegisterOMI) combineDevices(snapshotIds map[string]string) []oapi.BlockDeviceMappingImage {
|
func (s *StepRegisterOMI) combineDevices(snapshotIds map[string]string) []osc.BlockDeviceMappingImage {
|
||||||
devices := map[string]oapi.BlockDeviceMappingImage{}
|
devices := map[string]osc.BlockDeviceMappingImage{}
|
||||||
|
|
||||||
for _, device := range s.OMIDevices {
|
for _, device := range s.OMIDevices {
|
||||||
devices[device.DeviceName] = device
|
devices[device.DeviceName] = device
|
||||||
|
@ -125,18 +137,18 @@ func (s *StepRegisterOMI) combineDevices(snapshotIds map[string]string) []oapi.B
|
||||||
devices[device.DeviceName] = copyToDeviceMappingImage(device)
|
devices[device.DeviceName] = copyToDeviceMappingImage(device)
|
||||||
}
|
}
|
||||||
|
|
||||||
blockDevices := []oapi.BlockDeviceMappingImage{}
|
blockDevices := []osc.BlockDeviceMappingImage{}
|
||||||
for _, device := range devices {
|
for _, device := range devices {
|
||||||
blockDevices = append(blockDevices, device)
|
blockDevices = append(blockDevices, device)
|
||||||
}
|
}
|
||||||
return blockDevices
|
return blockDevices
|
||||||
}
|
}
|
||||||
|
|
||||||
func copyToDeviceMappingImage(device oapi.BlockDeviceMappingVmCreation) oapi.BlockDeviceMappingImage {
|
func copyToDeviceMappingImage(device osc.BlockDeviceMappingVmCreation) osc.BlockDeviceMappingImage {
|
||||||
deviceImage := oapi.BlockDeviceMappingImage{
|
deviceImage := osc.BlockDeviceMappingImage{
|
||||||
DeviceName: device.DeviceName,
|
DeviceName: device.DeviceName,
|
||||||
VirtualDeviceName: device.VirtualDeviceName,
|
VirtualDeviceName: device.VirtualDeviceName,
|
||||||
Bsu: oapi.BsuToCreate{
|
Bsu: osc.BsuToCreate{
|
||||||
DeleteOnVmDeletion: device.Bsu.DeleteOnVmDeletion,
|
DeleteOnVmDeletion: device.Bsu.DeleteOnVmDeletion,
|
||||||
Iops: device.Bsu.Iops,
|
Iops: device.Bsu.Iops,
|
||||||
SnapshotId: device.Bsu.SnapshotId,
|
SnapshotId: device.Bsu.SnapshotId,
|
||||||
|
|
|
@ -6,11 +6,12 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/antihax/optional"
|
||||||
multierror "github.com/hashicorp/go-multierror"
|
multierror "github.com/hashicorp/go-multierror"
|
||||||
osccommon "github.com/hashicorp/packer/builder/osc/common"
|
osccommon "github.com/hashicorp/packer/builder/osc/common"
|
||||||
"github.com/hashicorp/packer/helper/multistep"
|
"github.com/hashicorp/packer/helper/multistep"
|
||||||
"github.com/hashicorp/packer/packer"
|
"github.com/hashicorp/packer/packer"
|
||||||
"github.com/outscale/osc-go/oapi"
|
"github.com/outscale/osc-sdk-go/osc"
|
||||||
)
|
)
|
||||||
|
|
||||||
// StepSnapshotVolumes creates snapshots of the created volumes.
|
// StepSnapshotVolumes creates snapshots of the created volumes.
|
||||||
|
@ -18,14 +19,14 @@ import (
|
||||||
// Produces:
|
// Produces:
|
||||||
// snapshot_ids map[string]string - IDs of the created snapshots
|
// snapshot_ids map[string]string - IDs of the created snapshots
|
||||||
type StepSnapshotVolumes struct {
|
type StepSnapshotVolumes struct {
|
||||||
LaunchDevices []oapi.BlockDeviceMappingVmCreation
|
LaunchDevices []osc.BlockDeviceMappingVmCreation
|
||||||
snapshotIds map[string]string
|
snapshotIds map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StepSnapshotVolumes) snapshotVolume(ctx context.Context, deviceName string, state multistep.StateBag) error {
|
func (s *StepSnapshotVolumes) snapshotVolume(ctx context.Context, deviceName string, state multistep.StateBag) error {
|
||||||
oapiconn := state.Get("oapi").(*oapi.Client)
|
oscconn := state.Get("osc").(*osc.APIClient)
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
vm := state.Get("vm").(oapi.Vm)
|
vm := state.Get("vm").(osc.Vm)
|
||||||
|
|
||||||
var volumeId string
|
var volumeId string
|
||||||
for _, volume := range vm.BlockDeviceMappings {
|
for _, volume := range vm.BlockDeviceMappings {
|
||||||
|
@ -40,19 +41,21 @@ func (s *StepSnapshotVolumes) snapshotVolume(ctx context.Context, deviceName str
|
||||||
ui.Say(fmt.Sprintf("Creating snapshot of EBS Volume %s...", volumeId))
|
ui.Say(fmt.Sprintf("Creating snapshot of EBS Volume %s...", volumeId))
|
||||||
description := fmt.Sprintf("Packer: %s", time.Now().String())
|
description := fmt.Sprintf("Packer: %s", time.Now().String())
|
||||||
|
|
||||||
createSnapResp, err := oapiconn.POST_CreateSnapshot(oapi.CreateSnapshotRequest{
|
createSnapResp, _, err := oscconn.SnapshotApi.CreateSnapshot(context.Background(), &osc.CreateSnapshotOpts{
|
||||||
|
CreateSnapshotRequest: optional.NewInterface(osc.CreateSnapshotRequest{
|
||||||
VolumeId: volumeId,
|
VolumeId: volumeId,
|
||||||
Description: description,
|
Description: description,
|
||||||
|
}),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the snapshot ID so we can delete it later
|
// Set the snapshot ID so we can delete it later
|
||||||
s.snapshotIds[deviceName] = createSnapResp.OK.Snapshot.SnapshotId
|
s.snapshotIds[deviceName] = createSnapResp.Snapshot.SnapshotId
|
||||||
|
|
||||||
// Wait for snapshot to be created
|
// Wait for snapshot to be created
|
||||||
err = osccommon.WaitUntilSnapshotCompleted(oapiconn, createSnapResp.OK.Snapshot.SnapshotId)
|
err = osccommon.WaitUntilOscSnapshotCompleted(oscconn, createSnapResp.Snapshot.SnapshotId)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,7 +68,7 @@ func (s *StepSnapshotVolumes) Run(ctx context.Context, state multistep.StateBag)
|
||||||
var errs *multierror.Error
|
var errs *multierror.Error
|
||||||
for _, device := range s.LaunchDevices {
|
for _, device := range s.LaunchDevices {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func(device oapi.BlockDeviceMappingVmCreation) {
|
go func(device osc.BlockDeviceMappingVmCreation) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
if err := s.snapshotVolume(ctx, device.DeviceName, state); err != nil {
|
if err := s.snapshotVolume(ctx, device.DeviceName, state); err != nil {
|
||||||
errs = multierror.Append(errs, err)
|
errs = multierror.Append(errs, err)
|
||||||
|
@ -94,11 +97,13 @@ func (s *StepSnapshotVolumes) Cleanup(state multistep.StateBag) {
|
||||||
_, halted := state.GetOk(multistep.StateHalted)
|
_, halted := state.GetOk(multistep.StateHalted)
|
||||||
|
|
||||||
if cancelled || halted {
|
if cancelled || halted {
|
||||||
oapiconn := state.Get("oapi").(*oapi.Client)
|
oscconn := state.Get("osc").(*osc.APIClient)
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
ui.Say("Removing snapshots since we cancelled or halted...")
|
ui.Say("Removing snapshots since we cancelled or halted...")
|
||||||
for _, snapshotId := range s.snapshotIds {
|
for _, snapshotID := range s.snapshotIds {
|
||||||
_, err := oapiconn.POST_DeleteSnapshot(oapi.DeleteSnapshotRequest{SnapshotId: snapshotId})
|
_, _, err := oscconn.SnapshotApi.DeleteSnapshot(context.Background(), &osc.DeleteSnapshotOpts{
|
||||||
|
DeleteSnapshotRequest: optional.NewInterface(osc.DeleteSnapshotRequest{SnapshotId: snapshotID}),
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ui.Error(fmt.Sprintf("Error: %s", err))
|
ui.Error(fmt.Sprintf("Error: %s", err))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue