Merge pull request #5 from marinsalinas/step_cleanup_volumes

refactor: change step_cleanup_volumes to new OSC SDK
This commit is contained in:
Marin Salinas 2020-08-21 15:03:47 -05:00 committed by GitHub
commit 8166dad533
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 311 additions and 58 deletions

View File

@ -164,6 +164,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
UserData: b.config.UserData, UserData: b.config.UserData,
UserDataFile: b.config.UserDataFile, UserDataFile: b.config.UserDataFile,
VolumeTags: b.config.VolumeRunTags, VolumeTags: b.config.VolumeRunTags,
RawRegion: b.config.RawRegion,
}, },
&osccommon.StepGetPassword{ &osccommon.StepGetPassword{
Debug: b.config.PackerDebug, Debug: b.config.PackerDebug,
@ -173,8 +174,8 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
}, },
&communicator.StepConnect{ &communicator.StepConnect{
Config: &b.config.RunConfig.Comm, Config: &b.config.RunConfig.Comm,
Host: osccommon.SSHHost( Host: osccommon.OscSSHHost(
oapiconn, oscConn.VmApi,
b.config.SSHInterface), b.config.SSHInterface),
SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(), SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(),
}, },

View File

@ -9,6 +9,7 @@ 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-go/oapi"
"github.com/outscale/osc-sdk-go/osc"
) )
type stepCreateOMI struct { type stepCreateOMI struct {
@ -18,7 +19,7 @@ type stepCreateOMI struct {
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) oapiconn := state.Get("oapi").(*oapi.Client)
vm := state.Get("vm").(oapi.Vm) vm := state.Get("vm").(osc.Vm)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
// Create the image // Create the image

View File

@ -8,6 +8,7 @@ import (
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/hashicorp/packer/template/interpolate" "github.com/hashicorp/packer/template/interpolate"
"github.com/outscale/osc-go/oapi" "github.com/outscale/osc-go/oapi"
"github.com/outscale/osc-sdk-go/osc"
) )
// BlockDevice // BlockDevice
@ -123,6 +124,52 @@ func buildBlockDevicesVmCreation(b []BlockDevice) []oapi.BlockDeviceMappingVmCre
return blockDevices return blockDevices
} }
func buildOscBlockDevicesVmCreation(b []BlockDevice) []osc.BlockDeviceMappingVmCreation {
log.Printf("[DEBUG] Launch Block Device %#v", b)
var blockDevices []osc.BlockDeviceMappingVmCreation
for _, blockDevice := range b {
mapping := osc.BlockDeviceMappingVmCreation{
DeviceName: blockDevice.DeviceName,
}
if blockDevice.NoDevice {
mapping.NoDevice = ""
} else if blockDevice.VirtualName != "" {
if strings.HasPrefix(blockDevice.VirtualName, "ephemeral") {
mapping.VirtualDeviceName = blockDevice.VirtualName
}
} else {
bsu := osc.BsuToCreate{
DeleteOnVmDeletion: blockDevice.DeleteOnVmDeletion,
}
if blockDevice.VolumeType != "" {
bsu.VolumeType = blockDevice.VolumeType
}
if blockDevice.VolumeSize > 0 {
bsu.VolumeSize = int32(blockDevice.VolumeSize)
}
// IOPS is only valid for io1 type
if blockDevice.VolumeType == "io1" {
bsu.Iops = int32(blockDevice.IOPS)
}
if blockDevice.SnapshotId != "" {
bsu.SnapshotId = blockDevice.SnapshotId
}
mapping.Bsu = bsu
}
blockDevices = append(blockDevices, mapping)
}
return blockDevices
}
func (b *BlockDevice) Prepare(ctx *interpolate.Context) error { func (b *BlockDevice) Prepare(ctx *interpolate.Context) error {
if b.DeviceName == "" { if b.DeviceName == "" {
return fmt.Errorf("The `device_name` must be specified " + return fmt.Errorf("The `device_name` must be specified " +
@ -152,3 +199,7 @@ func (b *OMIBlockDevices) BuildOMIDevices() []oapi.BlockDeviceMappingImage {
func (b *LaunchBlockDevices) BuildLaunchDevices() []oapi.BlockDeviceMappingVmCreation { func (b *LaunchBlockDevices) BuildLaunchDevices() []oapi.BlockDeviceMappingVmCreation {
return buildBlockDevicesVmCreation(b.LaunchMappings) return buildBlockDevicesVmCreation(b.LaunchMappings)
} }
func (b *LaunchBlockDevices) BuildOSCLaunchDevices() []osc.BlockDeviceMappingVmCreation {
return buildOscBlockDevicesVmCreation(b.LaunchMappings)
}

View File

@ -1,18 +1,27 @@
package common package common
import ( import (
"context"
"errors" "errors"
"fmt" "fmt"
"time" "time"
"net/http"
"github.com/antihax/optional"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
"github.com/outscale/osc-go/oapi" "github.com/outscale/osc-go/oapi"
"github.com/outscale/osc-sdk-go/osc"
) )
type oapiDescriber interface { type oapiDescriber interface {
POST_ReadVms(oapi.ReadVmsRequest) (*oapi.POST_ReadVmsResponses, error) POST_ReadVms(oapi.ReadVmsRequest) (*oapi.POST_ReadVmsResponses, error)
} }
type oscDescriber interface {
ReadVms(ctx context.Context, localVarOptionals *osc.ReadVmsOpts) (osc.ReadVmsResponse, *http.Response, error)
}
var ( var (
// modified in tests // modified in tests
sshHostSleepDuration = time.Second sshHostSleepDuration = time.Second
@ -26,7 +35,7 @@ func SSHHost(e oapiDescriber, sshInterface string) func(multistep.StateBag) (str
// <= with current structure to check result of describing `tries` times // <= with current structure to check result of describing `tries` times
for j := 0; j <= tries; j++ { for j := 0; j <= tries; j++ {
var host string var host string
i := state.Get("vm").(oapi.Vm) i := state.Get("vm").(osc.Vm)
if sshInterface != "" { if sshInterface != "" {
switch sshInterface { switch sshInterface {
@ -83,3 +92,72 @@ func SSHHost(e oapiDescriber, sshInterface string) func(multistep.StateBag) (str
return "", errors.New("couldn't determine address for vm") return "", errors.New("couldn't determine address for vm")
} }
} }
// SSHHost returns a function that can be given to the SSH communicator
// for determining the SSH address based on the vm DNS name.
func OscSSHHost(e oscDescriber, sshInterface string) func(multistep.StateBag) (string, error) {
return func(state multistep.StateBag) (string, error) {
const tries = 2
// <= with current structure to check result of describing `tries` times
for j := 0; j <= tries; j++ {
var host string
i := state.Get("vm").(osc.Vm)
if sshInterface != "" {
switch sshInterface {
case "public_ip":
if i.PublicIp != "" {
host = i.PublicIp
}
case "public_dns":
if i.PublicDnsName != "" {
host = i.PublicDnsName
}
case "private_ip":
if i.PrivateIp != "" {
host = i.PrivateIp
}
case "private_dns":
if i.PrivateDnsName != "" {
host = i.PrivateDnsName
}
default:
panic(fmt.Sprintf("Unknown interface type: %s", sshInterface))
}
} else if i.NetId != "" {
if i.PublicIp != "" {
host = i.PublicIp
} else if i.PrivateIp != "" {
host = i.PrivateIp
}
} else if i.PublicDnsName != "" {
host = i.PublicDnsName
}
if host != "" {
return host, nil
}
r, _, err := e.ReadVms(context.Background(), &osc.ReadVmsOpts{
ReadVmsRequest: optional.NewInterface(osc.ReadVmsRequest{
Filters: osc.FiltersVm{
VmIds: []string{i.VmId},
},
}),
})
if err != nil {
return "", err
}
if len(r.Vms) == 0 {
return "", fmt.Errorf("vm not found: %s", i.VmId)
}
state.Put("vm", r.Vms[0])
time.Sleep(sshHostSleepDuration)
}
return "", errors.New("couldn't determine address for vm")
}
}

View File

@ -1,11 +1,14 @@
package common package common
import ( import (
"context"
"fmt" "fmt"
"log" "log"
"github.com/antihax/optional"
"github.com/hashicorp/packer/common" "github.com/hashicorp/packer/common"
"github.com/outscale/osc-go/oapi" "github.com/outscale/osc-go/oapi"
"github.com/outscale/osc-sdk-go/osc"
) )
type stateRefreshFunc func() (string, error) type stateRefreshFunc func() (string, error)
@ -24,18 +27,37 @@ func waitUntilForVmRunning(conn *oapi.Client, vmID string) error {
return err return err
} }
func waitUntilForOscVmRunning(conn *osc.APIClient, vmID string) error {
errCh := make(chan error, 1)
go waitForState(errCh, "running", waitUntilOscVmStateFunc(conn, vmID))
err := <-errCh
return err
}
func waitUntilVmDeleted(conn *oapi.Client, vmID string) error { func waitUntilVmDeleted(conn *oapi.Client, vmID string) error {
errCh := make(chan error, 1) errCh := make(chan error, 1)
go waitForState(errCh, "terminated", waitUntilVmStateFunc(conn, vmID)) go waitForState(errCh, "terminated", waitUntilVmStateFunc(conn, vmID))
return <-errCh return <-errCh
} }
func waitUntilOscVmDeleted(conn *osc.APIClient, vmID string) error {
errCh := make(chan error, 1)
go waitForState(errCh, "terminated", waitUntilOscVmStateFunc(conn, vmID))
return <-errCh
}
func waitUntilVmStopped(conn *oapi.Client, vmID string) error { func waitUntilVmStopped(conn *oapi.Client, vmID string) error {
errCh := make(chan error, 1) errCh := make(chan error, 1)
go waitForState(errCh, "stopped", waitUntilVmStateFunc(conn, vmID)) go waitForState(errCh, "stopped", waitUntilVmStateFunc(conn, vmID))
return <-errCh return <-errCh
} }
func waitUntilOscVmStopped(conn *osc.APIClient, vmID string) error {
errCh := make(chan error, 1)
go waitForState(errCh, "stopped", waitUntilOscVmStateFunc(conn, vmID))
return <-errCh
}
func WaitUntilSnapshotCompleted(conn *oapi.Client, id string) error { func WaitUntilSnapshotCompleted(conn *oapi.Client, id string) error {
errCh := make(chan error, 1) errCh := make(chan error, 1)
go waitForState(errCh, "completed", waitUntilSnapshotStateFunc(conn, id)) go waitForState(errCh, "completed", waitUntilSnapshotStateFunc(conn, id))
@ -113,6 +135,36 @@ func waitUntilVmStateFunc(conn *oapi.Client, id string) stateRefreshFunc {
} }
} }
func waitUntilOscVmStateFunc(conn *osc.APIClient, id string) stateRefreshFunc {
return func() (string, error) {
log.Printf("[Debug] Retrieving state for VM with id %s", id)
resp, _, err := conn.VmApi.ReadVms(context.Background(), &osc.ReadVmsOpts{
ReadVmsRequest: optional.NewInterface(osc.ReadVmsRequest{
Filters: osc.FiltersVm{
VmIds: []string{id},
},
}),
})
log.Printf("[Debug] Read Response %+v", resp)
if err != nil {
return "", err
}
//TODO: check if needed
// if resp == nil {
// return "", fmt.Errorf("Vm with ID %s not Found", id)
// }
if len(resp.Vms) == 0 {
return "pending", nil
}
return resp.Vms[0].State, nil
}
}
func waitUntilVolumeLinkedStateFunc(conn *oapi.Client, id string) stateRefreshFunc { func waitUntilVolumeLinkedStateFunc(conn *oapi.Client, id string) stateRefreshFunc {
return func() (string, error) { return func() (string, error) {
log.Printf("[Debug] Check if volume with id %s exists", id) log.Printf("[Debug] Check if volume with id %s exists", id)

View File

@ -5,29 +5,32 @@ import (
"fmt" "fmt"
"reflect" "reflect"
"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"
) )
// stepCleanupVolumes cleans up any orphaned volumes that were not designated to // StepCleanupVolumes cleans up any orphaned volumes that were not designated to
// remain after termination of the vm. These volumes are typically ones // remain after termination of the vm. These volumes are typically ones
// that are marked as "delete on terminate:false" in the source_ami of a build. // that are marked as "delete on terminate:false" in the source_ami of a build.
type StepCleanupVolumes struct { type StepCleanupVolumes struct {
BlockDevices BlockDevices BlockDevices BlockDevices
} }
//Run ...
func (s *StepCleanupVolumes) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { func (s *StepCleanupVolumes) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
// stepCleanupVolumes is for Cleanup only // stepCleanupVolumes is for Cleanup only
return multistep.ActionContinue return multistep.ActionContinue
} }
// Cleanup ...
func (s *StepCleanupVolumes) Cleanup(state multistep.StateBag) { func (s *StepCleanupVolumes) Cleanup(state multistep.StateBag) {
oapiconn := state.Get("oapi").(*oapi.Client) oscconn := state.Get("osc").(*osc.APIClient)
vmRaw := state.Get("vm") vmRaw := state.Get("vm")
var vm oapi.Vm var vm osc.Vm
if vmRaw != nil { if vmRaw != nil {
vm = vmRaw.(oapi.Vm) vm = vmRaw.(osc.Vm)
} }
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
if vm.VmId == "" { if vm.VmId == "" {
@ -42,7 +45,7 @@ func (s *StepCleanupVolumes) Cleanup(state multistep.StateBag) {
var vl []string var vl []string
volList := make(map[string]string) volList := make(map[string]string)
for _, bdm := range vm.BlockDeviceMappings { for _, bdm := range vm.BlockDeviceMappings {
if !reflect.DeepEqual(bdm.Bsu, oapi.BsuCreated{}) { if !reflect.DeepEqual(bdm.Bsu, osc.BsuCreated{}) {
vl = append(vl, bdm.Bsu.VolumeId) vl = append(vl, bdm.Bsu.VolumeId)
volList[bdm.Bsu.VolumeId] = bdm.DeviceName volList[bdm.Bsu.VolumeId] = bdm.DeviceName
} }
@ -50,10 +53,12 @@ func (s *StepCleanupVolumes) Cleanup(state multistep.StateBag) {
// Using the volume list from the cached Vm, check with Outscale for up to // Using the volume list from the cached Vm, check with Outscale for up to
// date information on them // date information on them
resp, err := oapiconn.POST_ReadVolumes(oapi.ReadVolumesRequest{ resp, _, err := oscconn.VolumeApi.ReadVolumes(context.Background(), &osc.ReadVolumesOpts{
Filters: oapi.FiltersVolume{ ReadVolumesRequest: optional.NewInterface(osc.ReadVolumesRequest{
VolumeIds: vl, Filters: osc.FiltersVolume{
}, VolumeIds: vl,
},
}),
}) })
if err != nil { if err != nil {
@ -63,13 +68,13 @@ func (s *StepCleanupVolumes) Cleanup(state multistep.StateBag) {
// If any of the returned volumes are in a "deleting" stage or otherwise not // If any of the returned volumes are in a "deleting" stage or otherwise not
// available, remove them from the list of volumes // available, remove them from the list of volumes
for _, v := range resp.OK.Volumes { for _, v := range resp.Volumes {
if v.State != "" && v.State != "available" { if v.State != "" && v.State != "available" {
delete(volList, v.VolumeId) delete(volList, v.VolumeId)
} }
} }
if len(resp.OK.Volumes) == 0 { if len(resp.Volumes) == 0 {
ui.Say("No volumes to clean up, skipping") ui.Say("No volumes to clean up, skipping")
return return
} }
@ -87,10 +92,11 @@ func (s *StepCleanupVolumes) Cleanup(state multistep.StateBag) {
// Destroy remaining volumes // Destroy remaining volumes
for k := range volList { for k := range volList {
ui.Say(fmt.Sprintf("Destroying volume (%s)...", k)) ui.Say(fmt.Sprintf("Destroying volume (%s)...", k))
_, err := oapiconn.POST_DeleteVolume(oapi.DeleteVolumeRequest{VolumeId: k}) _, _, err := oscconn.VolumeApi.DeleteVolume(context.Background(), &osc.DeleteVolumeOpts{
DeleteVolumeRequest: optional.NewInterface(osc.DeleteVolumeRequest{VolumeId: k}),
})
if err != nil { if err != nil {
ui.Say(fmt.Sprintf("Error deleting volume: %s", err)) ui.Say(fmt.Sprintf("Error deleting volume: %s", err))
} }
} }
} }

View File

@ -15,6 +15,7 @@ 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-go/oapi"
"github.com/outscale/osc-sdk-go/osc"
) )
// StepGetPassword reads the password from a Windows server and sets it // StepGetPassword reads the password from a Windows server and sets it
@ -103,7 +104,7 @@ func (s *StepGetPassword) Cleanup(multistep.StateBag) {}
func (s *StepGetPassword) waitForPassword(state multistep.StateBag, cancel <-chan struct{}) (string, error) { func (s *StepGetPassword) waitForPassword(state multistep.StateBag, cancel <-chan struct{}) (string, error) {
oapiconn := state.Get("oapi").(*oapi.Client) oapiconn := state.Get("oapi").(*oapi.Client)
vm := state.Get("vm").(oapi.Vm) vm := state.Get("vm").(osc.Vm)
privateKey := s.Comm.SSHPrivateKey privateKey := s.Comm.SSHPrivateKey
for { for {

View File

@ -8,6 +8,7 @@ import (
"log" "log"
"reflect" "reflect"
"github.com/antihax/optional"
"github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/aws/awserr"
"github.com/outscale/osc-go/oapi" "github.com/outscale/osc-go/oapi"
"github.com/outscale/osc-sdk-go/osc" "github.com/outscale/osc-sdk-go/osc"
@ -40,13 +41,13 @@ type StepRunSourceVm struct {
UserData string UserData string
UserDataFile string UserDataFile string
VolumeTags TagMap VolumeTags TagMap
RawRegion string
vmId string vmId string
} }
func (s *StepRunSourceVm) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { func (s *StepRunSourceVm) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
oapiconn := state.Get("oapi").(*oapi.Client) oscconn := state.Get("osc").(*osc.APIClient)
securityGroupIds := state.Get("securityGroupIds").([]string) securityGroupIds := state.Get("securityGroupIds").([]string)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
@ -90,7 +91,9 @@ func (s *StepRunSourceVm) Run(ctx context.Context, state multistep.StateBag) mul
s.Tags["Name"] = "Packer Builder" s.Tags["Name"] = "Packer Builder"
} }
oapiTags, err := s.Tags.OAPITags(s.Ctx, oapiconn.GetConfig().Region, state) rawRegion := s.RawRegion
oscTags, err := s.Tags.OSCTags(s.Ctx, rawRegion, state)
if err != nil { if err != nil {
err := fmt.Errorf("Error tagging source vm: %s", err) err := fmt.Errorf("Error tagging source vm: %s", err)
state.Put("error", err) state.Put("error", err)
@ -98,7 +101,7 @@ func (s *StepRunSourceVm) Run(ctx context.Context, state multistep.StateBag) mul
return multistep.ActionHalt return multistep.ActionHalt
} }
volTags, err := s.VolumeTags.OAPITags(s.Ctx, oapiconn.GetConfig().Region, state) volTags, err := s.VolumeTags.OSCTags(s.Ctx, rawRegion, state)
if err != nil { if err != nil {
err := fmt.Errorf("Error tagging volumes: %s", err) err := fmt.Errorf("Error tagging volumes: %s", err)
state.Put("error", err) state.Put("error", err)
@ -107,15 +110,15 @@ func (s *StepRunSourceVm) Run(ctx context.Context, state multistep.StateBag) mul
} }
subregion := state.Get("subregion_name").(string) subregion := state.Get("subregion_name").(string)
runOpts := oapi.CreateVmsRequest{ runOpts := osc.CreateVmsRequest{
ImageId: s.SourceOMI, ImageId: s.SourceOMI,
VmType: s.VmType, VmType: s.VmType,
UserData: userData, UserData: userData,
MaxVmsCount: 1, MaxVmsCount: 1,
MinVmsCount: 1, MinVmsCount: 1,
Placement: oapi.Placement{SubregionName: subregion}, Placement: osc.Placement{SubregionName: subregion},
BsuOptimized: s.BsuOptimized, BsuOptimized: s.BsuOptimized,
BlockDeviceMappings: s.BlockDevices.BuildLaunchDevices(), BlockDeviceMappings: s.BlockDevices.BuildOSCLaunchDevices(),
//IamVmProfile: oapi.IamVmProfileSpecification{Name: &s.IamVmProfile}, //IamVmProfile: oapi.IamVmProfileSpecification{Name: &s.IamVmProfile},
} }
@ -156,24 +159,27 @@ func (s *StepRunSourceVm) Run(ctx context.Context, state multistep.StateBag) mul
runOpts.KeypairName = s.Comm.SSHKeyPairName runOpts.KeypairName = s.Comm.SSHKeyPairName
} }
subnetId := state.Get("subnet_id").(string) subnetID := state.Get("subnet_id").(string)
runOpts.SubnetId = subnetId runOpts.SubnetId = subnetID
runOpts.SecurityGroupIds = securityGroupIds runOpts.SecurityGroupIds = securityGroupIds
if s.ExpectedRootDevice == "bsu" { if s.ExpectedRootDevice == "bsu" {
runOpts.VmInitiatedShutdownBehavior = s.VmInitiatedShutdownBehavior runOpts.VmInitiatedShutdownBehavior = s.VmInitiatedShutdownBehavior
} }
runResp, err := oapiconn.POST_CreateVms(runOpts) runResp, _, err := oscconn.VmApi.CreateVms(context.Background(), &osc.CreateVmsOpts{
CreateVmsRequest: optional.NewInterface(runOpts),
})
if err != nil { if err != nil {
err := fmt.Errorf("Error launching source vm: %s", err) err := fmt.Errorf("Error launching source vm: %s", err)
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt
} }
vmId = runResp.OK.Vms[0].VmId vmId = runResp.Vms[0].VmId
volumeId := runResp.OK.Vms[0].BlockDeviceMappings[0].Bsu.VolumeId volumeId := runResp.Vms[0].BlockDeviceMappings[0].Bsu.VolumeId
// Set the vm ID so that the cleanup works properly // Set the vm ID so that the cleanup works properly
s.vmId = vmId s.vmId = vmId
@ -181,12 +187,12 @@ func (s *StepRunSourceVm) Run(ctx context.Context, state multistep.StateBag) mul
ui.Message(fmt.Sprintf("Vm ID: %s", vmId)) ui.Message(fmt.Sprintf("Vm ID: %s", vmId))
ui.Say(fmt.Sprintf("Waiting for vm (%v) to become ready...", vmId)) ui.Say(fmt.Sprintf("Waiting for vm (%v) to become ready...", vmId))
request := oapi.ReadVmsRequest{ request := osc.ReadVmsRequest{
Filters: oapi.FiltersVm{ Filters: osc.FiltersVm{
VmIds: []string{vmId}, VmIds: []string{vmId},
}, },
} }
if err := waitUntilForVmRunning(oapiconn, vmId); err != nil { if err := waitUntilForOscVmRunning(oscconn, vmId); err != nil {
err := fmt.Errorf("Error waiting for vm (%s) to become ready: %s", vmId, err) err := fmt.Errorf("Error waiting for vm (%s) to become ready: %s", vmId, err)
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())
@ -194,8 +200,8 @@ func (s *StepRunSourceVm) Run(ctx context.Context, state multistep.StateBag) mul
} }
//Set Vm tags and vollume tags //Set Vm tags and vollume tags
if len(oapiTags) > 0 { if len(oscTags) > 0 {
if err := CreateTags(oapiconn, s.vmId, ui, oapiTags); err != nil { if err := CreateOSCTags(oscconn, s.vmId, ui, oscTags); err != nil {
err := fmt.Errorf("Error creating tags for vm (%s): %s", s.vmId, err) err := fmt.Errorf("Error creating tags for vm (%s): %s", s.vmId, err)
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())
@ -204,7 +210,7 @@ func (s *StepRunSourceVm) Run(ctx context.Context, state multistep.StateBag) mul
} }
if len(volTags) > 0 { if len(volTags) > 0 {
if err := CreateTags(oapiconn, volumeId, ui, volTags); err != nil { if err := CreateOSCTags(oscconn, volumeId, ui, volTags); err != nil {
err := fmt.Errorf("Error creating tags for volume (%s): %s", volumeId, err) err := fmt.Errorf("Error creating tags for volume (%s): %s", volumeId, err)
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())
@ -214,7 +220,9 @@ func (s *StepRunSourceVm) Run(ctx context.Context, state multistep.StateBag) mul
if publicip_id, ok := state.Get("publicip_id").(string); ok { if publicip_id, ok := state.Get("publicip_id").(string); ok {
ui.Say(fmt.Sprintf("Linking temporary PublicIp %s to instance %s", publicip_id, vmId)) ui.Say(fmt.Sprintf("Linking temporary PublicIp %s to instance %s", publicip_id, vmId))
_, err := oapiconn.POST_LinkPublicIp(oapi.LinkPublicIpRequest{PublicIpId: publicip_id, VmId: vmId}) _, _, err := oscconn.PublicIpApi.LinkPublicIp(context.Background(), &osc.LinkPublicIpOpts{
LinkPublicIpRequest: optional.NewInterface(osc.LinkPublicIpRequest{PublicIpId: publicip_id, VmId: vmId}),
})
if err != nil { if err != nil {
state.Put("error", fmt.Errorf("Error linking PublicIp to VM: %s", err)) state.Put("error", fmt.Errorf("Error linking PublicIp to VM: %s", err))
ui.Error(err.Error()) ui.Error(err.Error())
@ -222,9 +230,11 @@ func (s *StepRunSourceVm) Run(ctx context.Context, state multistep.StateBag) mul
} }
} }
resp, err := oapiconn.POST_ReadVms(request) resp, _, err := oscconn.VmApi.ReadVms(context.Background(), &osc.ReadVmsOpts{
ReadVmsRequest: optional.NewInterface(request),
})
r := resp.OK r := resp
if err != nil || len(r.Vms) == 0 { if err != nil || len(r.Vms) == 0 {
err := fmt.Errorf("Error finding source vm.") err := fmt.Errorf("Error finding source vm.")
@ -257,12 +267,14 @@ func (s *StepRunSourceVm) Run(ctx context.Context, state multistep.StateBag) mul
// do that now. // do that now.
if s.IsRestricted { if s.IsRestricted {
oapiTags.Report(ui) oscTags.Report(ui)
// Retry creating tags for about 2.5 minutes // Retry creating tags for about 2.5 minutes
err = retry.Retry(0.2, 30, 11, func(_ uint) (bool, error) { err = retry.Retry(0.2, 30, 11, func(_ uint) (bool, error) {
_, err := oapiconn.POST_CreateTags(oapi.CreateTagsRequest{ _, _, err := oscconn.TagApi.CreateTags(context.Background(), &osc.CreateTagsOpts{
Tags: oapiTags, CreateTagsRequest: optional.NewInterface(osc.CreateTagsRequest{
ResourceIds: []string{vmId}, Tags: oscTags,
ResourceIds: []string{vmId},
}),
}) })
if err == nil { if err == nil {
return true, nil return true, nil
@ -295,7 +307,7 @@ func (s *StepRunSourceVm) Run(ctx context.Context, state multistep.StateBag) mul
if len(volumeIds) > 0 && s.VolumeTags.IsSet() { if len(volumeIds) > 0 && s.VolumeTags.IsSet() {
ui.Say("Adding tags to source BSU Volumes") ui.Say("Adding tags to source BSU Volumes")
volumeTags, err := s.VolumeTags.OAPITags(s.Ctx, oapiconn.GetConfig().Region, state) volumeTags, err := s.VolumeTags.OSCTags(s.Ctx, rawRegion, state)
if err != nil { if err != nil {
err := fmt.Errorf("Error tagging source BSU Volumes on %s: %s", vm.VmId, err) err := fmt.Errorf("Error tagging source BSU Volumes on %s: %s", vm.VmId, err)
state.Put("error", err) state.Put("error", err)
@ -304,9 +316,11 @@ func (s *StepRunSourceVm) Run(ctx context.Context, state multistep.StateBag) mul
} }
volumeTags.Report(ui) volumeTags.Report(ui)
_, err = oapiconn.POST_CreateTags(oapi.CreateTagsRequest{ _, _, err = oscconn.TagApi.CreateTags(context.Background(), &osc.CreateTagsOpts{
ResourceIds: volumeIds, CreateTagsRequest: optional.NewInterface(osc.CreateTagsRequest{
Tags: volumeTags, ResourceIds: volumeIds,
Tags: volumeTags,
}),
}) })
if err != nil { if err != nil {
@ -322,19 +336,20 @@ func (s *StepRunSourceVm) Run(ctx context.Context, state multistep.StateBag) mul
} }
func (s *StepRunSourceVm) Cleanup(state multistep.StateBag) { func (s *StepRunSourceVm) Cleanup(state multistep.StateBag) {
oscconn := state.Get("osc").(*osc.APIClient)
oapiconn := state.Get("oapi").(*oapi.Client)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
// Terminate the source vm if it exists // Terminate the source vm if it exists
if s.vmId != "" { if s.vmId != "" {
ui.Say("Terminating the source OUTSCALE vm...") ui.Say("Terminating the source OUTSCALE vm...")
if _, err := oapiconn.POST_DeleteVms(oapi.DeleteVmsRequest{VmIds: []string{s.vmId}}); err != nil { if _, _, err := oscconn.VmApi.DeleteVms(context.Background(), &osc.DeleteVmsOpts{
DeleteVmsRequest: optional.NewInterface(osc.DeleteVmsRequest{VmIds: []string{s.vmId}}),
}); err != nil {
ui.Error(fmt.Sprintf("Error terminating vm, may still be around: %s", err)) ui.Error(fmt.Sprintf("Error terminating vm, may still be around: %s", err))
return return
} }
if err := waitUntilVmDeleted(oapiconn, s.vmId); err != nil { if err := waitUntilOscVmDeleted(oscconn, s.vmId); err != nil {
ui.Error(err.Error()) ui.Error(err.Error())
} }
} }

View File

@ -4,11 +4,12 @@ import (
"context" "context"
"fmt" "fmt"
"github.com/antihax/optional"
"github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/aws/awserr"
"github.com/hashicorp/packer/common" "github.com/hashicorp/packer/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"
) )
type StepStopBSUBackedVm struct { type StepStopBSUBackedVm struct {
@ -17,8 +18,8 @@ type StepStopBSUBackedVm struct {
} }
func (s *StepStopBSUBackedVm) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { func (s *StepStopBSUBackedVm) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
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)
// Skip when it is a spot vm // Skip when it is a spot vm
@ -43,8 +44,10 @@ func (s *StepStopBSUBackedVm) Run(ctx context.Context, state multistep.StateBag)
err := common.Retry(10, 60, 6, func(i uint) (bool, error) { err := common.Retry(10, 60, 6, func(i uint) (bool, error) {
ui.Message(fmt.Sprintf("Stopping vm, attempt %d", i+1)) ui.Message(fmt.Sprintf("Stopping vm, attempt %d", i+1))
_, err = oapiconn.POST_StopVms(oapi.StopVmsRequest{ _, _, err = oscconn.VmApi.StopVms(context.Background(), &osc.StopVmsOpts{
VmIds: []string{vm.VmId}, StopVmsRequest: optional.NewInterface(osc.StopVmsRequest{
VmIds: []string{vm.VmId},
}),
}) })
if err == nil { if err == nil {
@ -78,7 +81,7 @@ func (s *StepStopBSUBackedVm) Run(ctx context.Context, state multistep.StateBag)
// Wait for the vm to actually stop // Wait for the vm to actually stop
ui.Say("Waiting for the vm to stop...") ui.Say("Waiting for the vm to stop...")
err = waitUntilVmStopped(oapiconn, vm.VmId) err = waitUntilOscVmStopped(oscconn, vm.VmId)
if err != nil { if err != nil {
err := fmt.Errorf("Error waiting for vm to stop: %s", err) err := fmt.Errorf("Error waiting for vm to stop: %s", err)

View File

@ -1,16 +1,20 @@
package common package common
import ( import (
"context"
"fmt" "fmt"
"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/hashicorp/packer/template/interpolate" "github.com/hashicorp/packer/template/interpolate"
"github.com/outscale/osc-go/oapi" "github.com/outscale/osc-go/oapi"
"github.com/outscale/osc-sdk-go/osc"
) )
type TagMap map[string]string type TagMap map[string]string
type OAPITags []oapi.ResourceTag type OAPITags []oapi.ResourceTag
type OSCTags []osc.ResourceTag
func (t OAPITags) Report(ui packer.Ui) { func (t OAPITags) Report(ui packer.Ui) {
for _, tag := range t { for _, tag := range t {
@ -19,6 +23,13 @@ func (t OAPITags) Report(ui packer.Ui) {
} }
} }
func (t OSCTags) Report(ui packer.Ui) {
for _, tag := range t {
ui.Message(fmt.Sprintf("Adding tag: \"%s\": \"%s\"",
tag.Key, tag.Value))
}
}
func (t TagMap) IsSet() bool { func (t TagMap) IsSet() bool {
return len(t) > 0 return len(t) > 0
} }
@ -44,6 +55,27 @@ func (t TagMap) OAPITags(ctx interpolate.Context, region string, state multistep
return oapiTags, nil return oapiTags, nil
} }
func (t TagMap) OSCTags(ctx interpolate.Context, region string, state multistep.StateBag) (OSCTags, error) {
var oscTags []osc.ResourceTag
ctx.Data = extractBuildInfo(region, state)
for key, value := range t {
interpolatedKey, err := interpolate.Render(key, &ctx)
if err != nil {
return nil, fmt.Errorf("Error processing tag: %s:%s - %s", key, value, err)
}
interpolatedValue, err := interpolate.Render(value, &ctx)
if err != nil {
return nil, fmt.Errorf("Error processing tag: %s:%s - %s", key, value, err)
}
oscTags = append(oscTags, osc.ResourceTag{
Key: interpolatedKey,
Value: interpolatedValue,
})
}
return oscTags, nil
}
func CreateTags(conn *oapi.Client, resourceID string, ui packer.Ui, tags OAPITags) error { func CreateTags(conn *oapi.Client, resourceID string, ui packer.Ui, tags OAPITags) error {
tags.Report(ui) tags.Report(ui)
@ -54,3 +86,16 @@ func CreateTags(conn *oapi.Client, resourceID string, ui packer.Ui, tags OAPITag
return err return err
} }
func CreateOSCTags(conn *osc.APIClient, resourceID string, ui packer.Ui, tags OSCTags) error {
tags.Report(ui)
_, _, err := conn.TagApi.CreateTags(context.Background(), &osc.CreateTagsOpts{
CreateTagsRequest: optional.NewInterface(osc.CreateTagsRequest{
ResourceIds: []string{resourceID},
Tags: tags,
}),
})
return err
}