2019-03-04 11:02:55 -05:00
|
|
|
package chroot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2020-09-30 11:38:31 -04:00
|
|
|
"github.com/antihax/optional"
|
2019-03-04 11:02:55 -05:00
|
|
|
osccommon "github.com/hashicorp/packer/builder/osc/common"
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2020-09-30 11:38:31 -04:00
|
|
|
"github.com/outscale/osc-sdk-go/osc"
|
2019-03-04 11:02:55 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// StepCreateOMI creates the OMI.
|
|
|
|
type StepCreateOMI struct {
|
|
|
|
RootVolumeSize int64
|
2020-09-30 11:38:31 -04:00
|
|
|
RawRegion string
|
2019-03-04 11:02:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepCreateOMI) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
|
|
|
config := state.Get("config").(*Config)
|
2020-09-30 11:38:31 -04:00
|
|
|
osconn := state.Get("osc").(*osc.APIClient)
|
2019-03-04 11:02:55 -05:00
|
|
|
snapshotId := state.Get("snapshot_id").(string)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2019-03-04 11:02:55 -05:00
|
|
|
|
|
|
|
ui.Say("Creating the OMI...")
|
|
|
|
|
|
|
|
var (
|
2020-09-30 11:38:31 -04:00
|
|
|
registerOpts osc.CreateImageRequest
|
|
|
|
mappings []osc.BlockDeviceMappingImage
|
|
|
|
image osc.Image
|
2019-03-04 11:02:55 -05:00
|
|
|
rootDeviceName string
|
|
|
|
)
|
|
|
|
|
|
|
|
if config.FromScratch {
|
2020-09-30 11:38:31 -04:00
|
|
|
mappings = config.OMIBlockDevices.BuildOscOMIDevices()
|
2019-03-04 11:02:55 -05:00
|
|
|
rootDeviceName = config.RootDeviceName
|
|
|
|
} else {
|
2020-09-30 11:38:31 -04:00
|
|
|
image = state.Get("source_image").(osc.Image)
|
2019-03-04 11:02:55 -05:00
|
|
|
mappings = image.BlockDeviceMappings
|
|
|
|
rootDeviceName = image.RootDeviceName
|
|
|
|
}
|
|
|
|
|
2020-09-30 11:38:31 -04:00
|
|
|
newMappings := make([]osc.BlockDeviceMappingImage, len(mappings))
|
2019-03-04 11:02:55 -05:00
|
|
|
for i, device := range mappings {
|
|
|
|
newDevice := device
|
|
|
|
|
|
|
|
//FIX: Temporary fix
|
|
|
|
gibSize := newDevice.Bsu.VolumeSize / (1024 * 1024 * 1024)
|
|
|
|
newDevice.Bsu.VolumeSize = gibSize
|
|
|
|
|
|
|
|
if newDevice.DeviceName == rootDeviceName {
|
2020-09-30 11:38:31 -04:00
|
|
|
if newDevice.Bsu != (osc.BsuToCreate{}) {
|
2019-03-04 11:02:55 -05:00
|
|
|
newDevice.Bsu.SnapshotId = snapshotId
|
|
|
|
} else {
|
2020-09-30 11:38:31 -04:00
|
|
|
newDevice.Bsu = osc.BsuToCreate{SnapshotId: snapshotId}
|
2019-03-04 11:02:55 -05:00
|
|
|
}
|
|
|
|
|
2020-09-30 11:38:31 -04:00
|
|
|
if config.FromScratch || int32(s.RootVolumeSize) > newDevice.Bsu.VolumeSize {
|
|
|
|
newDevice.Bsu.VolumeSize = int32(s.RootVolumeSize)
|
2019-03-04 11:02:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newMappings[i] = newDevice
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.FromScratch {
|
2020-09-30 11:38:31 -04:00
|
|
|
registerOpts = osc.CreateImageRequest{
|
2019-03-04 11:02:55 -05:00
|
|
|
ImageName: config.OMIName,
|
|
|
|
Architecture: "x86_64",
|
|
|
|
RootDeviceName: rootDeviceName,
|
|
|
|
BlockDeviceMappings: newMappings,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
registerOpts = buildRegisterOpts(config, image, newMappings)
|
|
|
|
}
|
|
|
|
|
2020-09-30 11:38:31 -04:00
|
|
|
registerResp, _, err := osconn.ImageApi.CreateImage(context.Background(), &osc.CreateImageOpts{
|
|
|
|
CreateImageRequest: optional.NewInterface(registerOpts),
|
|
|
|
})
|
2019-03-04 11:02:55 -05:00
|
|
|
if err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("Error registering OMI: %s", err))
|
|
|
|
ui.Error(state.Get("error").(error).Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2020-09-30 11:38:31 -04:00
|
|
|
imageID := registerResp.Image.ImageId
|
2019-03-04 11:02:55 -05:00
|
|
|
|
|
|
|
// Set the OMI ID in the state
|
|
|
|
ui.Say(fmt.Sprintf("OMI: %s", imageID))
|
|
|
|
omis := make(map[string]string)
|
2020-09-30 11:38:31 -04:00
|
|
|
omis[s.RawRegion] = imageID
|
2019-03-04 11:02:55 -05:00
|
|
|
state.Put("omis", omis)
|
|
|
|
|
|
|
|
ui.Say("Waiting for OMI to become ready...")
|
2020-09-30 11:38:31 -04:00
|
|
|
if err := osccommon.WaitUntilOscImageAvailable(osconn, imageID); err != nil {
|
2019-03-04 11:02:55 -05:00
|
|
|
err := fmt.Errorf("Error waiting for OMI: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepCreateOMI) Cleanup(state multistep.StateBag) {}
|
|
|
|
|
2020-09-30 11:38:31 -04:00
|
|
|
func buildRegisterOpts(config *Config, image osc.Image, mappings []osc.BlockDeviceMappingImage) osc.CreateImageRequest {
|
|
|
|
registerOpts := osc.CreateImageRequest{
|
2019-03-04 11:02:55 -05:00
|
|
|
ImageName: config.OMIName,
|
|
|
|
Architecture: image.Architecture,
|
|
|
|
RootDeviceName: image.RootDeviceName,
|
|
|
|
BlockDeviceMappings: mappings,
|
|
|
|
}
|
|
|
|
return registerOpts
|
|
|
|
}
|