refactor: change chroot builder to new SDK

This commit is contained in:
Marin Salinas 2020-09-30 10:38:31 -05:00
parent c0c0f003f9
commit aa3ec3fd0f
7 changed files with 100 additions and 89 deletions

View File

@ -8,9 +8,7 @@ package chroot
import ( import (
"context" "context"
"crypto/tls"
"errors" "errors"
"net/http"
"runtime" "runtime"
"github.com/hashicorp/hcl/v2/hcldec" "github.com/hashicorp/hcl/v2/hcldec"
@ -20,7 +18,6 @@ import (
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate" "github.com/hashicorp/packer/template/interpolate"
"github.com/outscale/osc-go/oapi"
) )
// The unique ID for this builder // The unique ID for this builder
@ -193,19 +190,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
return nil, errors.New("The outscale-chroot builder only works on Linux environments.") return nil, errors.New("The outscale-chroot builder only works on Linux environments.")
} }
clientConfig, err := b.config.Config() oscConn := b.config.NewOSCClient()
if err != nil {
return nil, err
}
skipClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
oapiconn := oapi.NewClient(clientConfig, skipClient)
wrappedCommand := func(command string) (string, error) { wrappedCommand := func(command string) (string, error) {
ctx := b.config.ctx ctx := b.config.ctx
ctx.Data = &wrappedCommandTemplate{Command: command} ctx.Data = &wrappedCommandTemplate{Command: command}
@ -215,8 +200,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
// 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("osc", oscConn)
state.Put("clientConfig", clientConfig)
state.Put("hook", hook) state.Put("hook", hook)
state.Put("ui", ui) state.Put("ui", ui)
state.Put("wrappedCommand", CommandWrapper(wrappedCommand)) state.Put("wrappedCommand", CommandWrapper(wrappedCommand))
@ -266,7 +250,9 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
&StepCopyFiles{}, &StepCopyFiles{},
&StepChrootProvision{}, &StepChrootProvision{},
&StepEarlyCleanup{}, &StepEarlyCleanup{},
&StepSnapshot{}, &StepSnapshot{
RawRegion: b.config.RawRegion,
},
&osccommon.StepDeregisterOMI{ &osccommon.StepDeregisterOMI{
AccessConfig: &b.config.AccessConfig, AccessConfig: &b.config.AccessConfig,
ForceDeregister: b.config.OMIForceDeregister, ForceDeregister: b.config.OMIForceDeregister,
@ -276,6 +262,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
}, },
&StepCreateOMI{ &StepCreateOMI{
RootVolumeSize: b.config.RootVolumeSize, RootVolumeSize: b.config.RootVolumeSize,
RawRegion: b.config.RawRegion,
}, },
&osccommon.StepUpdateOMIAttributes{ &osccommon.StepUpdateOMIAttributes{
AccountIds: b.config.OMIAccountIDs, AccountIds: b.config.OMIAccountIDs,
@ -307,7 +294,6 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
artifact := &osccommon.Artifact{ artifact := &osccommon.Artifact{
Omis: state.Get("omis").(map[string]string), Omis: state.Get("omis").(map[string]string),
BuilderIdValue: BuilderId, BuilderIdValue: BuilderId,
Config: clientConfig,
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")}, StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
} }

View File

@ -6,14 +6,14 @@ import (
"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"
) )
// StepCheckRootDevice makes sure the root device on the OMI is BSU-backed. // StepCheckRootDevice makes sure the root device on the OMI is BSU-backed.
type StepCheckRootDevice struct{} type StepCheckRootDevice struct{}
func (s *StepCheckRootDevice) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { func (s *StepCheckRootDevice) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
image := state.Get("source_image").(oapi.Image) image := state.Get("source_image").(osc.Image)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
ui.Say("Checking the root device on source OMI...") ui.Say("Checking the root device on source OMI...")

View File

@ -4,42 +4,44 @@ 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-sdk-go/osc"
) )
// StepCreateOMI creates the OMI. // StepCreateOMI creates the OMI.
type StepCreateOMI struct { type StepCreateOMI struct {
RootVolumeSize int64 RootVolumeSize int64
RawRegion string
} }
func (s *StepCreateOMI) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { func (s *StepCreateOMI) 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) osconn := state.Get("osc").(*osc.APIClient)
snapshotId := state.Get("snapshot_id").(string) snapshotId := state.Get("snapshot_id").(string)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
ui.Say("Creating the OMI...") ui.Say("Creating the OMI...")
var ( var (
registerOpts oapi.CreateImageRequest registerOpts osc.CreateImageRequest
mappings []oapi.BlockDeviceMappingImage mappings []osc.BlockDeviceMappingImage
image oapi.Image image osc.Image
rootDeviceName string rootDeviceName string
) )
if config.FromScratch { if config.FromScratch {
mappings = config.OMIBlockDevices.BuildOMIDevices() mappings = config.OMIBlockDevices.BuildOscOMIDevices()
rootDeviceName = config.RootDeviceName rootDeviceName = config.RootDeviceName
} else { } else {
image = state.Get("source_image").(oapi.Image) image = state.Get("source_image").(osc.Image)
mappings = image.BlockDeviceMappings mappings = image.BlockDeviceMappings
rootDeviceName = image.RootDeviceName rootDeviceName = image.RootDeviceName
} }
newMappings := make([]oapi.BlockDeviceMappingImage, len(mappings)) newMappings := make([]osc.BlockDeviceMappingImage, len(mappings))
for i, device := range mappings { for i, device := range mappings {
newDevice := device newDevice := device
@ -48,14 +50,14 @@ func (s *StepCreateOMI) Run(ctx context.Context, state multistep.StateBag) multi
newDevice.Bsu.VolumeSize = gibSize newDevice.Bsu.VolumeSize = gibSize
if newDevice.DeviceName == rootDeviceName { if newDevice.DeviceName == rootDeviceName {
if newDevice.Bsu != (oapi.BsuToCreate{}) { if newDevice.Bsu != (osc.BsuToCreate{}) {
newDevice.Bsu.SnapshotId = snapshotId newDevice.Bsu.SnapshotId = snapshotId
} else { } else {
newDevice.Bsu = oapi.BsuToCreate{SnapshotId: snapshotId} newDevice.Bsu = osc.BsuToCreate{SnapshotId: snapshotId}
} }
if config.FromScratch || s.RootVolumeSize > newDevice.Bsu.VolumeSize { if config.FromScratch || int32(s.RootVolumeSize) > newDevice.Bsu.VolumeSize {
newDevice.Bsu.VolumeSize = s.RootVolumeSize newDevice.Bsu.VolumeSize = int32(s.RootVolumeSize)
} }
} }
@ -63,7 +65,7 @@ func (s *StepCreateOMI) Run(ctx context.Context, state multistep.StateBag) multi
} }
if config.FromScratch { if config.FromScratch {
registerOpts = oapi.CreateImageRequest{ registerOpts = osc.CreateImageRequest{
ImageName: config.OMIName, ImageName: config.OMIName,
Architecture: "x86_64", Architecture: "x86_64",
RootDeviceName: rootDeviceName, RootDeviceName: rootDeviceName,
@ -73,23 +75,25 @@ func (s *StepCreateOMI) Run(ctx context.Context, state multistep.StateBag) multi
registerOpts = buildRegisterOpts(config, image, newMappings) registerOpts = buildRegisterOpts(config, image, newMappings)
} }
registerResp, err := oapiconn.POST_CreateImage(registerOpts) registerResp, _, err := osconn.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())
return multistep.ActionHalt return multistep.ActionHalt
} }
imageID := registerResp.OK.Image.ImageId imageID := registerResp.Image.ImageId
// Set the OMI ID in the state // Set the OMI ID in the state
ui.Say(fmt.Sprintf("OMI: %s", imageID)) ui.Say(fmt.Sprintf("OMI: %s", imageID))
omis := make(map[string]string) omis := make(map[string]string)
omis[oapiconn.GetConfig().Region] = imageID omis[s.RawRegion] = imageID
state.Put("omis", omis) state.Put("omis", omis)
ui.Say("Waiting for OMI to become ready...") ui.Say("Waiting for OMI to become ready...")
if err := osccommon.WaitUntilImageAvailable(oapiconn, imageID); err != nil { if err := osccommon.WaitUntilOscImageAvailable(osconn, 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())
@ -101,8 +105,8 @@ func (s *StepCreateOMI) Run(ctx context.Context, state multistep.StateBag) multi
func (s *StepCreateOMI) Cleanup(state multistep.StateBag) {} func (s *StepCreateOMI) Cleanup(state multistep.StateBag) {}
func buildRegisterOpts(config *Config, image oapi.Image, mappings []oapi.BlockDeviceMappingImage) oapi.CreateImageRequest { func buildRegisterOpts(config *Config, image osc.Image, mappings []osc.BlockDeviceMappingImage) osc.CreateImageRequest {
registerOpts := oapi.CreateImageRequest{ registerOpts := osc.CreateImageRequest{
ImageName: config.OMIName, ImageName: config.OMIName,
Architecture: image.Architecture, Architecture: image.Architecture,
RootDeviceName: image.RootDeviceName, RootDeviceName: image.RootDeviceName,

View File

@ -6,11 +6,12 @@ import (
"fmt" "fmt"
"log" "log"
"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/hashicorp/packer/template/interpolate" "github.com/hashicorp/packer/template/interpolate"
"github.com/outscale/osc-go/oapi" "github.com/outscale/osc-sdk-go/osc"
) )
// StepCreateVolume creates a new volume from the snapshot of the root // StepCreateVolume creates a new volume from the snapshot of the root
@ -23,18 +24,19 @@ type StepCreateVolume struct {
RootVolumeSize int64 RootVolumeSize int64
RootVolumeType string RootVolumeType string
RootVolumeTags osccommon.TagMap RootVolumeTags osccommon.TagMap
RawRegion string
Ctx interpolate.Context Ctx interpolate.Context
} }
func (s *StepCreateVolume) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { func (s *StepCreateVolume) 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) oscconn := state.Get("osc").(*osc.APIClient)
vm := state.Get("vm").(oapi.Vm) vm := state.Get("vm").(osc.Vm)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
var err error var err error
volTags, err := s.RootVolumeTags.OAPITags(s.Ctx, oapiconn.GetConfig().Region, state) volTags, err := s.RootVolumeTags.OSCTags(s.Ctx, s.RawRegion, state)
if err != nil { if err != nil {
state.Put("error", err) state.Put("error", err)
@ -42,7 +44,7 @@ func (s *StepCreateVolume) Run(ctx context.Context, state multistep.StateBag) mu
return multistep.ActionHalt return multistep.ActionHalt
} }
var createVolume *oapi.CreateVolumeRequest var createVolume *osc.CreateVolumeRequest
if config.FromScratch { if config.FromScratch {
rootVolumeType := osccommon.VolumeTypeGp2 rootVolumeType := osccommon.VolumeTypeGp2
if s.RootVolumeType == "io1" { if s.RootVolumeType == "io1" {
@ -53,17 +55,17 @@ func (s *StepCreateVolume) Run(ctx context.Context, state multistep.StateBag) mu
} else if s.RootVolumeType != "" { } else if s.RootVolumeType != "" {
rootVolumeType = s.RootVolumeType rootVolumeType = s.RootVolumeType
} }
createVolume = &oapi.CreateVolumeRequest{ createVolume = &osc.CreateVolumeRequest{
SubregionName: vm.Placement.SubregionName, SubregionName: vm.Placement.SubregionName,
Size: s.RootVolumeSize, Size: int32(s.RootVolumeSize),
VolumeType: rootVolumeType, VolumeType: rootVolumeType,
} }
} else { } else {
// Determine the root device snapshot // Determine the root device snapshot
image := state.Get("source_image").(oapi.Image) image := state.Get("source_image").(osc.Image)
log.Printf("Searching for root device of the image (%s)", image.RootDeviceName) log.Printf("Searching for root device of the image (%s)", image.RootDeviceName)
var rootDevice *oapi.BlockDeviceMappingImage var rootDevice *osc.BlockDeviceMappingImage
for _, device := range image.BlockDeviceMappings { for _, device := range image.BlockDeviceMappings {
if device.DeviceName == image.RootDeviceName { if device.DeviceName == image.RootDeviceName {
rootDevice = &device rootDevice = &device
@ -82,7 +84,9 @@ func (s *StepCreateVolume) Run(ctx context.Context, state multistep.StateBag) mu
log.Printf("Create args: %+v", createVolume) log.Printf("Create args: %+v", createVolume)
createVolumeResp, err := oapiconn.POST_CreateVolume(*createVolume) createVolumeResp, _, err := oscconn.VolumeApi.CreateVolume(context.Background(), &osc.CreateVolumeOpts{
CreateVolumeRequest: optional.NewInterface(*createVolume),
})
if err != nil { if err != nil {
err := fmt.Errorf("Error creating root volume: %s", err) err := fmt.Errorf("Error creating root volume: %s", err)
state.Put("error", err) state.Put("error", err)
@ -91,12 +95,12 @@ func (s *StepCreateVolume) Run(ctx context.Context, state multistep.StateBag) mu
} }
// Set the volume ID so we remember to delete it later // Set the volume ID so we remember to delete it later
s.volumeId = createVolumeResp.OK.Volume.VolumeId s.volumeId = createVolumeResp.Volume.VolumeId
log.Printf("Volume ID: %s", s.volumeId) log.Printf("Volume ID: %s", s.volumeId)
//Create tags for volume //Create tags for volume
if len(volTags) > 0 { if len(volTags) > 0 {
if err := osccommon.CreateTags(oapiconn, s.volumeId, ui, volTags); err != nil { if err := osccommon.CreateOSCTags(oscconn, s.volumeId, ui, volTags); err != nil {
err := fmt.Errorf("Error creating tags for volume: %s", err) err := fmt.Errorf("Error creating tags for volume: %s", err)
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())
@ -105,7 +109,7 @@ func (s *StepCreateVolume) Run(ctx context.Context, state multistep.StateBag) mu
} }
// Wait for the volume to become ready // Wait for the volume to become ready
err = osccommon.WaitUntilVolumeAvailable(oapiconn, s.volumeId) err = osccommon.WaitUntilOscVolumeAvailable(oscconn, s.volumeId)
if err != nil { if err != nil {
err := fmt.Errorf("Error waiting for volume: %s", err) err := fmt.Errorf("Error waiting for volume: %s", err)
state.Put("error", err) state.Put("error", err)
@ -122,32 +126,34 @@ func (s *StepCreateVolume) 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("Deleting the created BSU volume...") ui.Say("Deleting the created BSU volume...")
_, err := oapiconn.POST_DeleteVolume(oapi.DeleteVolumeRequest{VolumeId: s.volumeId}) _, _, err := oscconn.VolumeApi.DeleteVolume(context.Background(), &osc.DeleteVolumeOpts{
DeleteVolumeRequest: optional.NewInterface(osc.DeleteVolumeRequest{VolumeId: s.volumeId}),
})
if err != nil { if err != nil {
ui.Error(fmt.Sprintf("Error deleting BSU volume: %s", err)) ui.Error(fmt.Sprintf("Error deleting BSU volume: %s", err))
} }
} }
func (s *StepCreateVolume) buildCreateVolumeInput(suregionName string, rootDevice *oapi.BlockDeviceMappingImage) (*oapi.CreateVolumeRequest, error) { func (s *StepCreateVolume) buildCreateVolumeInput(suregionName string, rootDevice *osc.BlockDeviceMappingImage) (*osc.CreateVolumeRequest, error) {
if rootDevice == nil { if rootDevice == nil {
return nil, fmt.Errorf("Couldn't find root device!") return nil, fmt.Errorf("Couldn't find root device")
} }
//FIX: Temporary fix //FIX: Temporary fix
gibSize := rootDevice.Bsu.VolumeSize / (1024 * 1024 * 1024) gibSize := rootDevice.Bsu.VolumeSize / (1024 * 1024 * 1024)
createVolumeInput := &oapi.CreateVolumeRequest{ createVolumeInput := &osc.CreateVolumeRequest{
SubregionName: suregionName, SubregionName: suregionName,
Size: gibSize, Size: gibSize,
SnapshotId: rootDevice.Bsu.SnapshotId, SnapshotId: rootDevice.Bsu.SnapshotId,
VolumeType: rootDevice.Bsu.VolumeType, VolumeType: rootDevice.Bsu.VolumeType,
Iops: rootDevice.Bsu.Iops, Iops: rootDevice.Bsu.Iops,
} }
if s.RootVolumeSize > rootDevice.Bsu.VolumeSize { if int32(s.RootVolumeSize) > rootDevice.Bsu.VolumeSize {
createVolumeInput.Size = s.RootVolumeSize createVolumeInput.Size = int32(s.RootVolumeSize)
} }
if s.RootVolumeType == "" || s.RootVolumeType == rootDevice.Bsu.VolumeType { if s.RootVolumeType == "" || s.RootVolumeType == rootDevice.Bsu.VolumeType {

View File

@ -4,10 +4,11 @@ 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-sdk-go/osc"
) )
// StepLinkVolume attaches the previously created volume to an // StepLinkVolume attaches the previously created volume to an
@ -22,9 +23,9 @@ type StepLinkVolume struct {
} }
func (s *StepLinkVolume) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { func (s *StepLinkVolume) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
oapiconn := state.Get("oapi").(*oapi.Client) oscconn := state.Get("osc").(*osc.APIClient)
device := state.Get("device").(string) device := state.Get("device").(string)
vm := state.Get("vm").(oapi.Vm) vm := state.Get("vm").(osc.Vm)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
volumeId := state.Get("volume_id").(string) volumeId := state.Get("volume_id").(string)
@ -33,10 +34,12 @@ func (s *StepLinkVolume) Run(ctx context.Context, state multistep.StateBag) mult
linkVolume := device linkVolume := device
ui.Say(fmt.Sprintf("Attaching the root volume to %s", linkVolume)) ui.Say(fmt.Sprintf("Attaching the root volume to %s", linkVolume))
_, err := oapiconn.POST_LinkVolume(oapi.LinkVolumeRequest{ _, _, err := oscconn.VolumeApi.LinkVolume(context.Background(), &osc.LinkVolumeOpts{
VmId: vm.VmId, LinkVolumeRequest: optional.NewInterface(osc.LinkVolumeRequest{
VolumeId: volumeId, VmId: vm.VmId,
DeviceName: linkVolume, VolumeId: volumeId,
DeviceName: linkVolume,
}),
}) })
if err != nil { if err != nil {
@ -51,7 +54,7 @@ func (s *StepLinkVolume) Run(ctx context.Context, state multistep.StateBag) mult
s.volumeId = volumeId s.volumeId = volumeId
// Wait for the volume to become attached // Wait for the volume to become attached
err = osccommon.WaitUntilVolumeIsLinked(oapiconn, s.volumeId) err = osccommon.WaitUntilOscVolumeIsLinked(oscconn, s.volumeId)
if err != nil { if err != nil {
err := fmt.Errorf("Error waiting for volume: %s", err) err := fmt.Errorf("Error waiting for volume: %s", err)
state.Put("error", err) state.Put("error", err)
@ -75,11 +78,14 @@ func (s *StepLinkVolume) CleanupFunc(state multistep.StateBag) error {
return nil return nil
} }
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("Detaching BSU volume...") ui.Say("Detaching BSU volume...")
_, err := oapiconn.POST_UnlinkVolume(oapi.UnlinkVolumeRequest{VolumeId: s.volumeId}) _, _, err := oscconn.VolumeApi.UnlinkVolume(context.Background(), &osc.UnlinkVolumeOpts{
UnlinkVolumeRequest: optional.NewInterface(osc.UnlinkVolumeRequest{VolumeId: s.volumeId}),
})
if err != nil { if err != nil {
return fmt.Errorf("Error detaching BSU volume: %s", err) return fmt.Errorf("Error detaching BSU volume: %s", err)
} }
@ -87,7 +93,7 @@ func (s *StepLinkVolume) CleanupFunc(state multistep.StateBag) error {
s.attached = false s.attached = false
// Wait for the volume to detach // Wait for the volume to detach
err = osccommon.WaitUntilVolumeIsUnlinked(oapiconn, s.volumeId) err = osccommon.WaitUntilOscVolumeIsUnlinked(oscconn, s.volumeId)
if err != nil { if err != nil {
return fmt.Errorf("Error waiting for volume: %s", err) return fmt.Errorf("Error waiting for volume: %s", err)
} }

View File

@ -5,10 +5,11 @@ import (
"fmt" "fmt"
"time" "time"
"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-sdk-go/osc"
) )
// StepSnapshot creates a snapshot of the created volume. // StepSnapshot creates a snapshot of the created volume.
@ -17,19 +18,22 @@ import (
// snapshot_id string - ID of the created snapshot // snapshot_id string - ID of the created snapshot
type StepSnapshot struct { type StepSnapshot struct {
snapshotId string snapshotId string
RawRegion string
} }
func (s *StepSnapshot) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { func (s *StepSnapshot) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
oapiconn := state.Get("oapi").(*oapi.Client) oscconn := state.Get("osc").(*osc.APIClient)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
volumeId := state.Get("volume_id").(string) volumeId := state.Get("volume_id").(string)
ui.Say("Creating snapshot...") ui.Say("Creating snapshot...")
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{
VolumeId: volumeId, CreateSnapshotRequest: optional.NewInterface(osc.CreateSnapshotRequest{
Description: description, VolumeId: volumeId,
Description: description,
}),
}) })
if err != nil { if err != nil {
err := fmt.Errorf("Error creating snapshot: %s", err) err := fmt.Errorf("Error creating snapshot: %s", err)
@ -39,11 +43,11 @@ func (s *StepSnapshot) Run(ctx context.Context, state multistep.StateBag) multis
} }
// Set the snapshot ID so we can delete it later // Set the snapshot ID so we can delete it later
s.snapshotId = createSnapResp.OK.Snapshot.SnapshotId s.snapshotId = createSnapResp.Snapshot.SnapshotId
ui.Message(fmt.Sprintf("Snapshot ID: %s", s.snapshotId)) ui.Message(fmt.Sprintf("Snapshot ID: %s", s.snapshotId))
// Wait for the snapshot to be ready // Wait for the snapshot to be ready
err = osccommon.WaitUntilSnapshotDone(oapiconn, s.snapshotId) err = osccommon.WaitUntilOscSnapshotDone(oscconn, s.snapshotId)
if err != nil { if err != nil {
err := fmt.Errorf("Error waiting for snapshot: %s", err) err := fmt.Errorf("Error waiting for snapshot: %s", err)
state.Put("error", err) state.Put("error", err)
@ -54,7 +58,7 @@ func (s *StepSnapshot) Run(ctx context.Context, state multistep.StateBag) multis
state.Put("snapshot_id", s.snapshotId) state.Put("snapshot_id", s.snapshotId)
snapshots := map[string][]string{ snapshots := map[string][]string{
oapiconn.GetConfig().Region: {s.snapshotId}, s.RawRegion: {s.snapshotId},
} }
state.Put("snapshots", snapshots) state.Put("snapshots", snapshots)
@ -70,10 +74,12 @@ func (s *StepSnapshot) 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 snapshot since we cancelled or halted...") ui.Say("Removing snapshot since we cancelled or halted...")
_, err := oapiconn.POST_DeleteSnapshot(oapi.DeleteSnapshotRequest{SnapshotId: s.snapshotId}) _, _, err := oscconn.SnapshotApi.DeleteSnapshot(context.Background(), &osc.DeleteSnapshotOpts{
DeleteSnapshotRequest: optional.NewInterface(osc.DeleteSnapshotRequest{SnapshotId: s.snapshotId}),
})
if err != nil { if err != nil {
ui.Error(fmt.Sprintf("Error: %s", err)) ui.Error(fmt.Sprintf("Error: %s", err))
} }

View File

@ -5,16 +5,17 @@ import (
"fmt" "fmt"
"log" "log"
"github.com/antihax/optional"
"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"
) )
// StepVmInfo verifies that this builder is running on an Outscale vm. // StepVmInfo verifies that this builder is running on an Outscale vm.
type StepVmInfo struct{} type StepVmInfo struct{}
func (s *StepVmInfo) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { func (s *StepVmInfo) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
oapiconn := state.Get("oapi").(*oapi.Client) oscconn := state.Get("osc").(*osc.APIClient)
//session := state.Get("clientConfig").(*session.Session) //session := state.Get("clientConfig").(*session.Session)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
@ -36,9 +37,11 @@ func (s *StepVmInfo) Run(_ context.Context, state multistep.StateBag) multistep.
log.Printf("[Debug] VmID got: %s", string(vmID)) log.Printf("[Debug] VmID got: %s", string(vmID))
// Query the entire vm metadata // Query the entire vm metadata
resp, err := oapiconn.POST_ReadVms(oapi.ReadVmsRequest{Filters: oapi.FiltersVm{ resp, _, err := oscconn.VmApi.ReadVms(context.Background(), &osc.ReadVmsOpts{
VmIds: []string{string(vmID)}, ReadVmsRequest: optional.NewInterface(osc.ReadVmsRequest{Filters: osc.FiltersVm{
}}) VmIds: []string{string(vmID)},
}}),
})
if err != nil { if err != nil {
err := fmt.Errorf("Error getting vm data: %s", err) err := fmt.Errorf("Error getting vm data: %s", err)
state.Put("error", err) state.Put("error", err)
@ -46,10 +49,10 @@ func (s *StepVmInfo) Run(_ context.Context, state multistep.StateBag) multistep.
return multistep.ActionHalt return multistep.ActionHalt
} }
vmsResp := resp.OK vmsResp := resp
if len(vmsResp.Vms) == 0 { if len(vmsResp.Vms) == 0 {
err := fmt.Errorf("Error getting vm data: no vm found.") err := fmt.Errorf("Error getting vm data: no vm found")
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt