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 (
"context"
"crypto/tls"
"errors"
"net/http"
"runtime"
"github.com/hashicorp/hcl/v2/hcldec"
@ -20,7 +18,6 @@ import (
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
"github.com/outscale/osc-go/oapi"
)
// 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.")
}
clientConfig, err := b.config.Config()
if err != nil {
return nil, err
}
skipClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
oapiconn := oapi.NewClient(clientConfig, skipClient)
oscConn := b.config.NewOSCClient()
wrappedCommand := func(command string) (string, error) {
ctx := b.config.ctx
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
state := new(multistep.BasicStateBag)
state.Put("config", &b.config)
state.Put("oapi", oapiconn)
state.Put("clientConfig", clientConfig)
state.Put("osc", oscConn)
state.Put("hook", hook)
state.Put("ui", ui)
state.Put("wrappedCommand", CommandWrapper(wrappedCommand))
@ -266,7 +250,9 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
&StepCopyFiles{},
&StepChrootProvision{},
&StepEarlyCleanup{},
&StepSnapshot{},
&StepSnapshot{
RawRegion: b.config.RawRegion,
},
&osccommon.StepDeregisterOMI{
AccessConfig: &b.config.AccessConfig,
ForceDeregister: b.config.OMIForceDeregister,
@ -276,6 +262,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
},
&StepCreateOMI{
RootVolumeSize: b.config.RootVolumeSize,
RawRegion: b.config.RawRegion,
},
&osccommon.StepUpdateOMIAttributes{
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{
Omis: state.Get("omis").(map[string]string),
BuilderIdValue: BuilderId,
Config: clientConfig,
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/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.
type StepCheckRootDevice struct{}
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.Say("Checking the root device on source OMI...")

View File

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

View File

@ -6,11 +6,12 @@ import (
"fmt"
"log"
"github.com/antihax/optional"
osccommon "github.com/hashicorp/packer/builder/osc/common"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"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
@ -23,18 +24,19 @@ type StepCreateVolume struct {
RootVolumeSize int64
RootVolumeType string
RootVolumeTags osccommon.TagMap
RawRegion string
Ctx interpolate.Context
}
func (s *StepCreateVolume) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*Config)
oapiconn := state.Get("oapi").(*oapi.Client)
vm := state.Get("vm").(oapi.Vm)
oscconn := state.Get("osc").(*osc.APIClient)
vm := state.Get("vm").(osc.Vm)
ui := state.Get("ui").(packer.Ui)
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 {
state.Put("error", err)
@ -42,7 +44,7 @@ func (s *StepCreateVolume) Run(ctx context.Context, state multistep.StateBag) mu
return multistep.ActionHalt
}
var createVolume *oapi.CreateVolumeRequest
var createVolume *osc.CreateVolumeRequest
if config.FromScratch {
rootVolumeType := osccommon.VolumeTypeGp2
if s.RootVolumeType == "io1" {
@ -53,17 +55,17 @@ func (s *StepCreateVolume) Run(ctx context.Context, state multistep.StateBag) mu
} else if s.RootVolumeType != "" {
rootVolumeType = s.RootVolumeType
}
createVolume = &oapi.CreateVolumeRequest{
createVolume = &osc.CreateVolumeRequest{
SubregionName: vm.Placement.SubregionName,
Size: s.RootVolumeSize,
Size: int32(s.RootVolumeSize),
VolumeType: rootVolumeType,
}
} else {
// 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)
var rootDevice *oapi.BlockDeviceMappingImage
var rootDevice *osc.BlockDeviceMappingImage
for _, device := range image.BlockDeviceMappings {
if device.DeviceName == image.RootDeviceName {
rootDevice = &device
@ -82,7 +84,9 @@ func (s *StepCreateVolume) Run(ctx context.Context, state multistep.StateBag) mu
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 {
err := fmt.Errorf("Error creating root volume: %s", 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
s.volumeId = createVolumeResp.OK.Volume.VolumeId
s.volumeId = createVolumeResp.Volume.VolumeId
log.Printf("Volume ID: %s", s.volumeId)
//Create tags for volume
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)
state.Put("error", err)
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
err = osccommon.WaitUntilVolumeAvailable(oapiconn, s.volumeId)
err = osccommon.WaitUntilOscVolumeAvailable(oscconn, s.volumeId)
if err != nil {
err := fmt.Errorf("Error waiting for volume: %s", err)
state.Put("error", err)
@ -122,32 +126,34 @@ func (s *StepCreateVolume) Cleanup(state multistep.StateBag) {
return
}
oapiconn := state.Get("oapi").(*oapi.Client)
oscconn := state.Get("osc").(*osc.APIClient)
ui := state.Get("ui").(packer.Ui)
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 {
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 {
return nil, fmt.Errorf("Couldn't find root device!")
return nil, fmt.Errorf("Couldn't find root device")
}
//FIX: Temporary fix
gibSize := rootDevice.Bsu.VolumeSize / (1024 * 1024 * 1024)
createVolumeInput := &oapi.CreateVolumeRequest{
createVolumeInput := &osc.CreateVolumeRequest{
SubregionName: suregionName,
Size: gibSize,
SnapshotId: rootDevice.Bsu.SnapshotId,
VolumeType: rootDevice.Bsu.VolumeType,
Iops: rootDevice.Bsu.Iops,
}
if s.RootVolumeSize > rootDevice.Bsu.VolumeSize {
createVolumeInput.Size = s.RootVolumeSize
if int32(s.RootVolumeSize) > rootDevice.Bsu.VolumeSize {
createVolumeInput.Size = int32(s.RootVolumeSize)
}
if s.RootVolumeType == "" || s.RootVolumeType == rootDevice.Bsu.VolumeType {

View File

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

View File

@ -5,10 +5,11 @@ import (
"fmt"
"time"
"github.com/antihax/optional"
osccommon "github.com/hashicorp/packer/builder/osc/common"
"github.com/hashicorp/packer/helper/multistep"
"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.
@ -17,19 +18,22 @@ import (
// snapshot_id string - ID of the created snapshot
type StepSnapshot struct {
snapshotId string
RawRegion string
}
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)
volumeId := state.Get("volume_id").(string)
ui.Say("Creating snapshot...")
description := fmt.Sprintf("Packer: %s", time.Now().String())
createSnapResp, err := oapiconn.POST_CreateSnapshot(oapi.CreateSnapshotRequest{
VolumeId: volumeId,
Description: description,
createSnapResp, _, err := oscconn.SnapshotApi.CreateSnapshot(context.Background(), &osc.CreateSnapshotOpts{
CreateSnapshotRequest: optional.NewInterface(osc.CreateSnapshotRequest{
VolumeId: volumeId,
Description: description,
}),
})
if err != nil {
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
s.snapshotId = createSnapResp.OK.Snapshot.SnapshotId
s.snapshotId = createSnapResp.Snapshot.SnapshotId
ui.Message(fmt.Sprintf("Snapshot ID: %s", s.snapshotId))
// Wait for the snapshot to be ready
err = osccommon.WaitUntilSnapshotDone(oapiconn, s.snapshotId)
err = osccommon.WaitUntilOscSnapshotDone(oscconn, s.snapshotId)
if err != nil {
err := fmt.Errorf("Error waiting for snapshot: %s", 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)
snapshots := map[string][]string{
oapiconn.GetConfig().Region: {s.snapshotId},
s.RawRegion: {s.snapshotId},
}
state.Put("snapshots", snapshots)
@ -70,10 +74,12 @@ func (s *StepSnapshot) Cleanup(state multistep.StateBag) {
_, halted := state.GetOk(multistep.StateHalted)
if cancelled || halted {
oapiconn := state.Get("oapi").(*oapi.Client)
oscconn := state.Get("osc").(*osc.APIClient)
ui := state.Get("ui").(packer.Ui)
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 {
ui.Error(fmt.Sprintf("Error: %s", err))
}

View File

@ -5,16 +5,17 @@ import (
"fmt"
"log"
"github.com/antihax/optional"
"github.com/hashicorp/packer/helper/multistep"
"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.
type StepVmInfo struct{}
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)
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))
// Query the entire vm metadata
resp, err := oapiconn.POST_ReadVms(oapi.ReadVmsRequest{Filters: oapi.FiltersVm{
VmIds: []string{string(vmID)},
}})
resp, _, err := oscconn.VmApi.ReadVms(context.Background(), &osc.ReadVmsOpts{
ReadVmsRequest: optional.NewInterface(osc.ReadVmsRequest{Filters: osc.FiltersVm{
VmIds: []string{string(vmID)},
}}),
})
if err != nil {
err := fmt.Errorf("Error getting vm data: %s", err)
state.Put("error", err)
@ -46,10 +49,10 @@ func (s *StepVmInfo) Run(_ context.Context, state multistep.StateBag) multistep.
return multistep.ActionHalt
}
vmsResp := resp.OK
vmsResp := resp
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)
ui.Error(err.Error())
return multistep.ActionHalt