2019-10-14 10:43:59 -04:00
|
|
|
//go:generate mapstructure-to-hcl2 -type Config
|
|
|
|
|
2019-05-01 15:10:15 -04:00
|
|
|
// Package bsu contains a packer.Builder implementation that
|
2019-02-07 15:56:47 -05:00
|
|
|
// builds OMIs for Outscale OAPI.
|
|
|
|
//
|
|
|
|
// In general, there are two types of OMIs that can be created: ebs-backed or
|
|
|
|
// instance-store. This builder _only_ builds ebs-backed images.
|
|
|
|
package bsu
|
|
|
|
|
|
|
|
import (
|
2019-05-01 15:10:15 -04:00
|
|
|
"context"
|
2019-02-11 10:55:47 -05:00
|
|
|
"fmt"
|
2019-02-07 15:56:47 -05:00
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2019-02-07 15:56:47 -05:00
|
|
|
osccommon "github.com/hashicorp/packer/builder/osc/common"
|
2019-02-11 10:55:47 -05:00
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
2019-02-07 15:56:47 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-11-12 17:44:02 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/common"
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep/commonsteps"
|
2020-11-18 13:34:59 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/template/config"
|
2020-11-11 13:21:37 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/template/interpolate"
|
2019-02-07 15:56:47 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// The unique ID for this builder
|
|
|
|
const BuilderId = "oapi.outscale.bsu"
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
osccommon.AccessConfig `mapstructure:",squash"`
|
|
|
|
osccommon.OMIConfig `mapstructure:",squash"`
|
|
|
|
osccommon.BlockDevices `mapstructure:",squash"`
|
|
|
|
osccommon.RunConfig `mapstructure:",squash"`
|
|
|
|
VolumeRunTags osccommon.TagMap `mapstructure:"run_volume_tags"`
|
|
|
|
|
|
|
|
ctx interpolate.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
type Builder struct {
|
|
|
|
config Config
|
|
|
|
runner multistep.Runner
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() }
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
|
2019-02-07 17:13:35 -05:00
|
|
|
b.config.ctx.Funcs = osccommon.TemplateFuncs
|
|
|
|
err := config.Decode(&b.config, &config.DecodeOpts{
|
2020-10-09 20:01:55 -04:00
|
|
|
PluginType: BuilderId,
|
2019-02-07 17:13:35 -05:00
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &b.config.ctx,
|
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{
|
|
|
|
"omi_description",
|
|
|
|
"run_tags",
|
|
|
|
"run_volume_tags",
|
|
|
|
"spot_tags",
|
|
|
|
"snapshot_tags",
|
|
|
|
"tags",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, raws...)
|
|
|
|
if err != nil {
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, nil, err
|
2019-02-07 17:13:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.PackerConfig.PackerForce {
|
|
|
|
b.config.OMIForceDeregister = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accumulate any errors
|
|
|
|
var errs *packer.MultiError
|
|
|
|
errs = packer.MultiErrorAppend(errs, b.config.AccessConfig.Prepare(&b.config.ctx)...)
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
b.config.OMIConfig.Prepare(&b.config.AccessConfig, &b.config.ctx)...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, b.config.BlockDevices.Prepare(&b.config.ctx)...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, b.config.RunConfig.Prepare(&b.config.ctx)...)
|
|
|
|
|
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, nil, errs
|
2019-02-07 17:13:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
packer.LogSecretFilter.Set(b.config.AccessKey, b.config.SecretKey, b.config.Token)
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, nil, nil
|
2019-02-07 15:56:47 -05:00
|
|
|
}
|
|
|
|
|
2019-05-01 15:10:15 -04:00
|
|
|
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
|
2020-08-17 11:06:00 -04:00
|
|
|
oscConn := b.config.NewOSCClient()
|
|
|
|
|
2019-02-08 17:41:06 -05:00
|
|
|
// Setup the state bag and initial state for the steps
|
|
|
|
state := new(multistep.BasicStateBag)
|
|
|
|
state.Put("config", &b.config)
|
2020-08-17 11:06:00 -04:00
|
|
|
state.Put("osc", oscConn)
|
2020-08-26 13:25:46 -04:00
|
|
|
state.Put("accessConfig", &b.config.AccessConfig)
|
2019-02-08 17:41:06 -05:00
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
|
|
|
|
|
|
|
steps := []multistep.Step{
|
|
|
|
&osccommon.StepPreValidate{
|
|
|
|
DestOmiName: b.config.OMIName,
|
|
|
|
ForceDeregister: b.config.OMIForceDeregister,
|
|
|
|
},
|
2019-02-11 10:55:47 -05:00
|
|
|
&osccommon.StepSourceOMIInfo{
|
2019-04-05 18:06:17 -04:00
|
|
|
SourceOmi: b.config.SourceOmi,
|
|
|
|
OmiFilters: b.config.SourceOmiFilter,
|
|
|
|
OMIVirtType: b.config.OMIVirtType, //TODO: Remove if it is not used
|
2019-02-11 10:55:47 -05:00
|
|
|
},
|
|
|
|
&osccommon.StepNetworkInfo{
|
|
|
|
NetId: b.config.NetId,
|
|
|
|
NetFilter: b.config.NetFilter,
|
|
|
|
SecurityGroupIds: b.config.SecurityGroupIds,
|
|
|
|
SecurityGroupFilter: b.config.SecurityGroupFilter,
|
|
|
|
SubnetId: b.config.SubnetId,
|
|
|
|
SubnetFilter: b.config.SubnetFilter,
|
|
|
|
SubregionName: b.config.Subregion,
|
|
|
|
},
|
|
|
|
&osccommon.StepKeyPair{
|
|
|
|
Debug: b.config.PackerDebug,
|
|
|
|
Comm: &b.config.RunConfig.Comm,
|
2020-08-18 16:06:00 -04:00
|
|
|
DebugKeyPath: fmt.Sprintf("osc_%s", b.config.PackerBuildName),
|
2019-02-11 10:55:47 -05:00
|
|
|
},
|
2019-11-26 11:46:00 -05:00
|
|
|
&osccommon.StepPublicIp{
|
|
|
|
AssociatePublicIpAddress: b.config.AssociatePublicIpAddress,
|
|
|
|
Debug: b.config.PackerDebug,
|
|
|
|
},
|
2019-02-11 10:55:47 -05:00
|
|
|
&osccommon.StepSecurityGroup{
|
|
|
|
SecurityGroupFilter: b.config.SecurityGroupFilter,
|
|
|
|
SecurityGroupIds: b.config.SecurityGroupIds,
|
|
|
|
CommConfig: &b.config.RunConfig.Comm,
|
|
|
|
TemporarySGSourceCidr: b.config.TemporarySGSourceCidr,
|
|
|
|
},
|
|
|
|
&osccommon.StepCleanupVolumes{
|
|
|
|
BlockDevices: b.config.BlockDevices,
|
|
|
|
},
|
|
|
|
&osccommon.StepRunSourceVm{
|
|
|
|
BlockDevices: b.config.BlockDevices,
|
|
|
|
Comm: &b.config.RunConfig.Comm,
|
|
|
|
Ctx: b.config.ctx,
|
|
|
|
Debug: b.config.PackerDebug,
|
|
|
|
BsuOptimized: b.config.BsuOptimized,
|
|
|
|
EnableT2Unlimited: b.config.EnableT2Unlimited,
|
2019-02-12 16:12:19 -05:00
|
|
|
ExpectedRootDevice: osccommon.RunSourceVmBSUExpectedRootDevice,
|
2019-02-11 10:55:47 -05:00
|
|
|
IamVmProfile: b.config.IamVmProfile,
|
|
|
|
VmInitiatedShutdownBehavior: b.config.VmInitiatedShutdownBehavior,
|
|
|
|
VmType: b.config.VmType,
|
|
|
|
IsRestricted: false,
|
|
|
|
SourceOMI: b.config.SourceOmi,
|
|
|
|
Tags: b.config.RunTags,
|
|
|
|
UserData: b.config.UserData,
|
|
|
|
UserDataFile: b.config.UserDataFile,
|
|
|
|
VolumeTags: b.config.VolumeRunTags,
|
2020-08-20 21:37:09 -04:00
|
|
|
RawRegion: b.config.RawRegion,
|
2019-02-11 10:55:47 -05:00
|
|
|
},
|
|
|
|
&osccommon.StepGetPassword{
|
|
|
|
Debug: b.config.PackerDebug,
|
|
|
|
Comm: &b.config.RunConfig.Comm,
|
|
|
|
Timeout: b.config.WindowsPasswordTimeout,
|
|
|
|
BuildName: b.config.PackerBuildName,
|
|
|
|
},
|
|
|
|
&communicator.StepConnect{
|
|
|
|
Config: &b.config.RunConfig.Comm,
|
2020-08-20 21:37:09 -04:00
|
|
|
Host: osccommon.OscSSHHost(
|
|
|
|
oscConn.VmApi,
|
2019-07-18 12:47:15 -04:00
|
|
|
b.config.SSHInterface),
|
2019-02-11 10:55:47 -05:00
|
|
|
SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(),
|
|
|
|
},
|
2020-11-11 18:04:28 -05:00
|
|
|
&commonsteps.StepProvision{},
|
|
|
|
&commonsteps.StepCleanupTempKeys{
|
2019-02-11 10:55:47 -05:00
|
|
|
Comm: &b.config.RunConfig.Comm,
|
|
|
|
},
|
|
|
|
&osccommon.StepStopBSUBackedVm{
|
|
|
|
Skip: false,
|
|
|
|
DisableStopVm: b.config.DisableStopVm,
|
|
|
|
},
|
|
|
|
&osccommon.StepDeregisterOMI{
|
|
|
|
AccessConfig: &b.config.AccessConfig,
|
|
|
|
ForceDeregister: b.config.OMIForceDeregister,
|
|
|
|
ForceDeleteSnapshot: b.config.OMIForceDeleteSnapshot,
|
|
|
|
OMIName: b.config.OMIName,
|
|
|
|
Regions: b.config.OMIRegions,
|
|
|
|
},
|
2020-08-25 19:02:11 -04:00
|
|
|
&stepCreateOMI{
|
|
|
|
RawRegion: b.config.RawRegion,
|
|
|
|
},
|
2019-02-11 13:51:05 -05:00
|
|
|
&osccommon.StepUpdateOMIAttributes{
|
|
|
|
AccountIds: b.config.OMIAccountIDs,
|
|
|
|
SnapshotAccountIds: b.config.SnapshotAccountIDs,
|
2020-08-26 13:25:46 -04:00
|
|
|
RawRegion: b.config.RawRegion,
|
2019-02-11 13:51:05 -05:00
|
|
|
Ctx: b.config.ctx,
|
|
|
|
},
|
|
|
|
&osccommon.StepCreateTags{
|
|
|
|
Tags: b.config.OMITags,
|
|
|
|
SnapshotTags: b.config.SnapshotTags,
|
|
|
|
Ctx: b.config.ctx,
|
|
|
|
},
|
2019-02-08 17:41:06 -05:00
|
|
|
}
|
|
|
|
|
2020-11-11 18:04:28 -05:00
|
|
|
b.runner = commonsteps.NewRunner(steps, b.config.PackerConfig, ui)
|
2019-05-01 15:10:15 -04:00
|
|
|
b.runner.Run(ctx, state)
|
2019-02-08 17:41:06 -05:00
|
|
|
|
|
|
|
// If there was an error, return that
|
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
|
|
|
return nil, rawErr.(error)
|
|
|
|
}
|
|
|
|
|
2019-02-11 16:11:14 -05:00
|
|
|
//Build the artifact
|
2019-05-03 14:10:38 -04:00
|
|
|
if omis, ok := state.GetOk("omis"); ok {
|
2019-02-11 16:11:14 -05:00
|
|
|
// Build the artifact and return it
|
|
|
|
artifact := &osccommon.Artifact{
|
|
|
|
Omis: omis.(map[string]string),
|
|
|
|
BuilderIdValue: BuilderId,
|
2020-01-30 05:27:58 -05:00
|
|
|
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
|
2019-02-11 16:11:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return artifact, nil
|
|
|
|
}
|
|
|
|
|
2019-02-07 15:56:47 -05:00
|
|
|
return nil, nil
|
|
|
|
}
|