2019-02-23 21:49:51 -05:00
|
|
|
package chroot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2020-09-30 11:38:31 -04:00
|
|
|
"github.com/antihax/optional"
|
2019-02-23 21:49:51 -05:00
|
|
|
osccommon "github.com/hashicorp/packer/builder/osc/common"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-11 13:21:37 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/template/interpolate"
|
2020-09-30 11:38:31 -04:00
|
|
|
"github.com/outscale/osc-sdk-go/osc"
|
2019-02-23 21:49:51 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// StepCreateVolume creates a new volume from the snapshot of the root
|
|
|
|
// device of the OMI.
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// volume_id string - The ID of the created volume
|
|
|
|
type StepCreateVolume struct {
|
|
|
|
volumeId string
|
|
|
|
RootVolumeSize int64
|
|
|
|
RootVolumeType string
|
|
|
|
RootVolumeTags osccommon.TagMap
|
2020-09-30 11:38:31 -04:00
|
|
|
RawRegion string
|
2019-02-23 21:49:51 -05:00
|
|
|
Ctx interpolate.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepCreateVolume) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
|
|
|
config := state.Get("config").(*Config)
|
2020-09-30 11:38:31 -04:00
|
|
|
oscconn := state.Get("osc").(*osc.APIClient)
|
|
|
|
vm := state.Get("vm").(osc.Vm)
|
2019-02-23 21:49:51 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
2020-09-30 11:38:31 -04:00
|
|
|
volTags, err := s.RootVolumeTags.OSCTags(s.Ctx, s.RawRegion, state)
|
2019-02-23 21:49:51 -05:00
|
|
|
|
2019-04-03 15:30:52 -04:00
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2020-09-30 11:38:31 -04:00
|
|
|
var createVolume *osc.CreateVolumeRequest
|
2019-02-23 21:49:51 -05:00
|
|
|
if config.FromScratch {
|
|
|
|
rootVolumeType := osccommon.VolumeTypeGp2
|
|
|
|
if s.RootVolumeType == "io1" {
|
|
|
|
err := errors.New("Cannot use io1 volume when building from scratch")
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
} else if s.RootVolumeType != "" {
|
|
|
|
rootVolumeType = s.RootVolumeType
|
|
|
|
}
|
2020-09-30 11:38:31 -04:00
|
|
|
createVolume = &osc.CreateVolumeRequest{
|
2019-02-23 21:49:51 -05:00
|
|
|
SubregionName: vm.Placement.SubregionName,
|
2020-09-30 11:38:31 -04:00
|
|
|
Size: int32(s.RootVolumeSize),
|
2019-02-23 21:49:51 -05:00
|
|
|
VolumeType: rootVolumeType,
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Determine the root device snapshot
|
2020-09-30 11:38:31 -04:00
|
|
|
image := state.Get("source_image").(osc.Image)
|
2019-02-23 21:49:51 -05:00
|
|
|
log.Printf("Searching for root device of the image (%s)", image.RootDeviceName)
|
2020-09-30 11:38:31 -04:00
|
|
|
var rootDevice *osc.BlockDeviceMappingImage
|
2019-02-23 21:49:51 -05:00
|
|
|
for _, device := range image.BlockDeviceMappings {
|
|
|
|
if device.DeviceName == image.RootDeviceName {
|
2019-02-25 13:02:51 -05:00
|
|
|
rootDevice = &device
|
2019-02-23 21:49:51 -05:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Creating the root volume...")
|
|
|
|
createVolume, err = s.buildCreateVolumeInput(vm.Placement.SubregionName, rootDevice)
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
2019-03-06 15:54:23 -05:00
|
|
|
|
2019-02-23 21:49:51 -05:00
|
|
|
log.Printf("Create args: %+v", createVolume)
|
|
|
|
|
2020-09-30 11:38:31 -04:00
|
|
|
createVolumeResp, _, err := oscconn.VolumeApi.CreateVolume(context.Background(), &osc.CreateVolumeOpts{
|
|
|
|
CreateVolumeRequest: optional.NewInterface(*createVolume),
|
|
|
|
})
|
2019-02-23 21:49:51 -05:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating root volume: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the volume ID so we remember to delete it later
|
2020-09-30 11:38:31 -04:00
|
|
|
s.volumeId = createVolumeResp.Volume.VolumeId
|
2019-02-23 21:49:51 -05:00
|
|
|
log.Printf("Volume ID: %s", s.volumeId)
|
|
|
|
|
2019-03-06 15:54:23 -05:00
|
|
|
//Create tags for volume
|
|
|
|
if len(volTags) > 0 {
|
2020-09-30 11:38:31 -04:00
|
|
|
if err := osccommon.CreateOSCTags(oscconn, s.volumeId, ui, volTags); err != nil {
|
2019-04-15 14:58:02 -04:00
|
|
|
err := fmt.Errorf("Error creating tags for volume: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2019-03-06 15:54:23 -05:00
|
|
|
}
|
|
|
|
|
2019-02-23 21:49:51 -05:00
|
|
|
// Wait for the volume to become ready
|
2020-09-30 11:38:31 -04:00
|
|
|
err = osccommon.WaitUntilOscVolumeAvailable(oscconn, s.volumeId)
|
2019-02-23 21:49:51 -05:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error waiting for volume: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
state.Put("volume_id", s.volumeId)
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepCreateVolume) Cleanup(state multistep.StateBag) {
|
|
|
|
if s.volumeId == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-30 11:38:31 -04:00
|
|
|
oscconn := state.Get("osc").(*osc.APIClient)
|
2019-02-23 21:49:51 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
ui.Say("Deleting the created BSU volume...")
|
2020-09-30 11:38:31 -04:00
|
|
|
_, _, err := oscconn.VolumeApi.DeleteVolume(context.Background(), &osc.DeleteVolumeOpts{
|
|
|
|
DeleteVolumeRequest: optional.NewInterface(osc.DeleteVolumeRequest{VolumeId: s.volumeId}),
|
|
|
|
})
|
2019-02-23 21:49:51 -05:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error deleting BSU volume: %s", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-30 11:38:31 -04:00
|
|
|
func (s *StepCreateVolume) buildCreateVolumeInput(suregionName string, rootDevice *osc.BlockDeviceMappingImage) (*osc.CreateVolumeRequest, error) {
|
2019-02-23 21:49:51 -05:00
|
|
|
if rootDevice == nil {
|
2020-09-30 11:38:31 -04:00
|
|
|
return nil, fmt.Errorf("Couldn't find root device")
|
2019-02-23 21:49:51 -05:00
|
|
|
}
|
2019-02-25 13:02:51 -05:00
|
|
|
|
|
|
|
//FIX: Temporary fix
|
|
|
|
gibSize := rootDevice.Bsu.VolumeSize / (1024 * 1024 * 1024)
|
2020-09-30 11:38:31 -04:00
|
|
|
createVolumeInput := &osc.CreateVolumeRequest{
|
2019-02-23 21:49:51 -05:00
|
|
|
SubregionName: suregionName,
|
2019-02-25 13:02:51 -05:00
|
|
|
Size: gibSize,
|
2019-02-23 21:49:51 -05:00
|
|
|
SnapshotId: rootDevice.Bsu.SnapshotId,
|
|
|
|
VolumeType: rootDevice.Bsu.VolumeType,
|
|
|
|
Iops: rootDevice.Bsu.Iops,
|
|
|
|
}
|
2020-09-30 11:38:31 -04:00
|
|
|
if int32(s.RootVolumeSize) > rootDevice.Bsu.VolumeSize {
|
|
|
|
createVolumeInput.Size = int32(s.RootVolumeSize)
|
2019-02-23 21:49:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if s.RootVolumeType == "" || s.RootVolumeType == rootDevice.Bsu.VolumeType {
|
|
|
|
return createVolumeInput, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.RootVolumeType == "io1" {
|
|
|
|
return nil, fmt.Errorf("Root volume type cannot be io1, because existing root volume type was %s", rootDevice.Bsu.VolumeType)
|
|
|
|
}
|
|
|
|
|
|
|
|
createVolumeInput.VolumeType = s.RootVolumeType
|
|
|
|
// non io1 cannot set iops
|
|
|
|
createVolumeInput.Iops = 0
|
|
|
|
|
|
|
|
return createVolumeInput, nil
|
|
|
|
}
|