2019-10-14 10:43:59 -04:00
|
|
|
//go:generate mapstructure-to-hcl2 -type Config
|
|
|
|
|
2019-05-09 12:50:44 -04:00
|
|
|
// Package chroot is able to create an Outscale OMI without requiring
|
2019-02-15 12:41:36 -05:00
|
|
|
// the launch of a new instance for every build. It does this by attaching
|
|
|
|
// and mounting the root volume of another OMI and chrooting into that
|
|
|
|
// directory. It then creates an OMI from that attached drive.
|
|
|
|
package chroot
|
|
|
|
|
|
|
|
import (
|
2019-05-01 15:10:15 -04:00
|
|
|
"context"
|
2019-02-15 12:41:36 -05:00
|
|
|
"errors"
|
|
|
|
"runtime"
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2019-02-15 12:41:36 -05:00
|
|
|
osccommon "github.com/hashicorp/packer/builder/osc/common"
|
|
|
|
"github.com/hashicorp/packer/common"
|
2020-11-11 18:04:28 -05:00
|
|
|
"github.com/hashicorp/packer/common/commonsteps"
|
2019-02-15 15:52:41 -05:00
|
|
|
"github.com/hashicorp/packer/helper/config"
|
2019-02-15 12:41:36 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-11-11 13:21:37 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/template/interpolate"
|
2019-02-15 12:41:36 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// The unique ID for this builder
|
|
|
|
const BuilderId = "oapi.outscale.chroot"
|
|
|
|
|
|
|
|
// Config is the configuration that is chained through the steps and
|
|
|
|
// settable from the template.
|
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
osccommon.OMIBlockDevices `mapstructure:",squash"`
|
|
|
|
osccommon.OMIConfig `mapstructure:",squash"`
|
|
|
|
osccommon.AccessConfig `mapstructure:",squash"`
|
|
|
|
|
2019-02-15 12:44:46 -05:00
|
|
|
ChrootMounts [][]string `mapstructure:"chroot_mounts"`
|
|
|
|
CommandWrapper string `mapstructure:"command_wrapper"`
|
|
|
|
CopyFiles []string `mapstructure:"copy_files"`
|
|
|
|
DevicePath string `mapstructure:"device_path"`
|
|
|
|
NVMEDevicePath string `mapstructure:"nvme_device_path"`
|
|
|
|
FromScratch bool `mapstructure:"from_scratch"`
|
|
|
|
MountOptions []string `mapstructure:"mount_options"`
|
|
|
|
MountPartition string `mapstructure:"mount_partition"`
|
|
|
|
MountPath string `mapstructure:"mount_path"`
|
|
|
|
PostMountCommands []string `mapstructure:"post_mount_commands"`
|
|
|
|
PreMountCommands []string `mapstructure:"pre_mount_commands"`
|
|
|
|
RootDeviceName string `mapstructure:"root_device_name"`
|
|
|
|
RootVolumeSize int64 `mapstructure:"root_volume_size"`
|
|
|
|
RootVolumeType string `mapstructure:"root_volume_type"`
|
|
|
|
SourceOMI string `mapstructure:"source_omi"`
|
|
|
|
SourceOMIFilter osccommon.OmiFilterOptions `mapstructure:"source_omi_filter"`
|
|
|
|
RootVolumeTags osccommon.TagMap `mapstructure:"root_volume_tags"`
|
|
|
|
|
2019-02-15 12:41:36 -05:00
|
|
|
ctx interpolate.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
type wrappedCommandTemplate struct {
|
|
|
|
Command string
|
|
|
|
}
|
|
|
|
|
|
|
|
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-15 15:52:41 -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-15 15:52:41 -05:00
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &b.config.ctx,
|
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{
|
|
|
|
"omi_description",
|
|
|
|
"snapshot_tags",
|
|
|
|
"tags",
|
|
|
|
"root_volume_tags",
|
|
|
|
"command_wrapper",
|
|
|
|
"post_mount_commands",
|
|
|
|
"pre_mount_commands",
|
|
|
|
"mount_path",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, raws...)
|
|
|
|
if err != nil {
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, nil, err
|
2019-02-15 15:52:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.PackerConfig.PackerForce {
|
|
|
|
b.config.OMIForceDeregister = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Defaults
|
|
|
|
if b.config.ChrootMounts == nil {
|
|
|
|
b.config.ChrootMounts = make([][]string, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(b.config.ChrootMounts) == 0 {
|
|
|
|
b.config.ChrootMounts = [][]string{
|
|
|
|
{"proc", "proc", "/proc"},
|
|
|
|
{"sysfs", "sysfs", "/sys"},
|
|
|
|
{"bind", "/dev", "/dev"},
|
|
|
|
{"devpts", "devpts", "/dev/pts"},
|
|
|
|
{"binfmt_misc", "binfmt_misc", "/proc/sys/fs/binfmt_misc"},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set default copy file if we're not giving our own
|
|
|
|
if b.config.CopyFiles == nil {
|
|
|
|
b.config.CopyFiles = make([]string, 0)
|
|
|
|
if !b.config.FromScratch {
|
|
|
|
b.config.CopyFiles = []string{"/etc/resolv.conf"}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.CommandWrapper == "" {
|
|
|
|
b.config.CommandWrapper = "{{.Command}}"
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.MountPath == "" {
|
|
|
|
b.config.MountPath = "/mnt/packer-outscale-chroot-volumes/{{.Device}}"
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.MountPartition == "" {
|
|
|
|
b.config.MountPartition = "1"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accumulate any errors or warnings
|
|
|
|
var errs *packer.MultiError
|
|
|
|
var warns []string
|
|
|
|
|
|
|
|
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)...)
|
|
|
|
|
|
|
|
for _, mounts := range b.config.ChrootMounts {
|
|
|
|
if len(mounts) != 3 {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("Each chroot_mounts entry should be three elements."))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.FromScratch {
|
|
|
|
if b.config.SourceOMI != "" || !b.config.SourceOMIFilter.Empty() {
|
|
|
|
warns = append(warns, "source_omi and source_omi_filter are unused when from_scratch is true")
|
|
|
|
}
|
|
|
|
if b.config.RootVolumeSize == 0 {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("root_volume_size is required with from_scratch."))
|
|
|
|
}
|
|
|
|
if len(b.config.PreMountCommands) == 0 {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("pre_mount_commands is required with from_scratch."))
|
|
|
|
}
|
|
|
|
if b.config.OMIVirtType == "" {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("omi_virtualization_type is required with from_scratch."))
|
|
|
|
}
|
|
|
|
if b.config.RootDeviceName == "" {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("root_device_name is required with from_scratch."))
|
|
|
|
}
|
|
|
|
if len(b.config.OMIMappings) == 0 {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("omi_block_device_mappings is required with from_scratch."))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if b.config.SourceOMI == "" && b.config.SourceOMIFilter.Empty() {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("source_omi or source_omi_filter is required."))
|
|
|
|
}
|
|
|
|
if len(b.config.OMIMappings) != 0 {
|
|
|
|
warns = append(warns, "omi_block_device_mappings are unused when from_scratch is false")
|
|
|
|
}
|
|
|
|
if b.config.RootDeviceName != "" {
|
|
|
|
warns = append(warns, "root_device_name is unused when from_scratch is false")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, warns, errs
|
2019-02-15 15:52:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
packer.LogSecretFilter.Set(b.config.AccessKey, b.config.SecretKey, b.config.Token)
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, warns, nil
|
2019-02-15 12:41:36 -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) {
|
2019-02-15 12:41:36 -05:00
|
|
|
if runtime.GOOS != "linux" {
|
|
|
|
return nil, errors.New("The outscale-chroot builder only works on Linux environments.")
|
|
|
|
}
|
2019-02-21 16:57:07 -05:00
|
|
|
|
2020-09-30 11:38:31 -04:00
|
|
|
oscConn := b.config.NewOSCClient()
|
2019-02-21 16:57:07 -05:00
|
|
|
wrappedCommand := func(command string) (string, error) {
|
|
|
|
ctx := b.config.ctx
|
|
|
|
ctx.Data = &wrappedCommandTemplate{Command: command}
|
|
|
|
return interpolate.Render(b.config.CommandWrapper, &ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the state bag and initial state for the steps
|
|
|
|
state := new(multistep.BasicStateBag)
|
|
|
|
state.Put("config", &b.config)
|
2020-09-30 11:38:31 -04:00
|
|
|
state.Put("osc", oscConn)
|
2019-02-21 16:57:07 -05:00
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
|
|
|
state.Put("wrappedCommand", CommandWrapper(wrappedCommand))
|
|
|
|
|
|
|
|
// Build the steps
|
|
|
|
steps := []multistep.Step{
|
|
|
|
&osccommon.StepPreValidate{
|
|
|
|
DestOmiName: b.config.OMIName,
|
|
|
|
ForceDeregister: b.config.OMIForceDeregister,
|
|
|
|
},
|
|
|
|
&StepVmInfo{},
|
|
|
|
}
|
|
|
|
|
2019-02-22 10:50:46 -05:00
|
|
|
if !b.config.FromScratch {
|
|
|
|
steps = append(steps,
|
|
|
|
&osccommon.StepSourceOMIInfo{
|
2019-04-05 18:06:17 -04:00
|
|
|
SourceOmi: b.config.SourceOMI,
|
|
|
|
OmiFilters: b.config.SourceOMIFilter,
|
|
|
|
OMIVirtType: b.config.OMIVirtType,
|
2019-02-22 10:50:46 -05:00
|
|
|
},
|
|
|
|
&StepCheckRootDevice{},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-02-22 14:45:00 -05:00
|
|
|
steps = append(steps,
|
2019-05-09 12:50:44 -04:00
|
|
|
&StepFlock{},
|
2019-02-22 14:45:00 -05:00
|
|
|
&StepPrepareDevice{},
|
2019-02-23 21:49:51 -05:00
|
|
|
&StepCreateVolume{
|
|
|
|
RootVolumeType: b.config.RootVolumeType,
|
|
|
|
RootVolumeSize: b.config.RootVolumeSize,
|
|
|
|
RootVolumeTags: b.config.RootVolumeTags,
|
|
|
|
Ctx: b.config.ctx,
|
|
|
|
},
|
2019-02-25 16:37:06 -05:00
|
|
|
&StepLinkVolume{},
|
2019-05-09 12:50:44 -04:00
|
|
|
&StepEarlyUnflock{},
|
|
|
|
&StepPreMountCommands{
|
2019-02-25 17:31:08 -05:00
|
|
|
Commands: b.config.PreMountCommands,
|
|
|
|
},
|
2019-02-27 16:26:51 -05:00
|
|
|
&StepMountDevice{
|
|
|
|
MountOptions: b.config.MountOptions,
|
|
|
|
MountPartition: b.config.MountPartition,
|
|
|
|
},
|
2019-05-09 12:50:44 -04:00
|
|
|
&StepPostMountCommands{
|
2019-02-28 10:52:44 -05:00
|
|
|
Commands: b.config.PostMountCommands,
|
|
|
|
},
|
2019-05-09 12:50:44 -04:00
|
|
|
&StepMountExtra{},
|
|
|
|
&StepCopyFiles{},
|
|
|
|
&StepChrootProvision{},
|
|
|
|
&StepEarlyCleanup{},
|
2020-09-30 11:38:31 -04:00
|
|
|
&StepSnapshot{
|
|
|
|
RawRegion: b.config.RawRegion,
|
|
|
|
},
|
2019-03-04 11:02:55 -05:00
|
|
|
&osccommon.StepDeregisterOMI{
|
|
|
|
AccessConfig: &b.config.AccessConfig,
|
|
|
|
ForceDeregister: b.config.OMIForceDeregister,
|
|
|
|
ForceDeleteSnapshot: b.config.OMIForceDeleteSnapshot,
|
|
|
|
OMIName: b.config.OMIName,
|
|
|
|
Regions: b.config.OMIRegions,
|
|
|
|
},
|
|
|
|
&StepCreateOMI{
|
|
|
|
RootVolumeSize: b.config.RootVolumeSize,
|
2020-09-30 11:38:31 -04:00
|
|
|
RawRegion: b.config.RawRegion,
|
2019-03-04 11:02:55 -05:00
|
|
|
},
|
2019-03-05 12:58:59 -05:00
|
|
|
&osccommon.StepUpdateOMIAttributes{
|
|
|
|
AccountIds: b.config.OMIAccountIDs,
|
|
|
|
SnapshotAccountIds: b.config.SnapshotAccountIDs,
|
|
|
|
Ctx: b.config.ctx,
|
|
|
|
},
|
|
|
|
&osccommon.StepCreateTags{
|
|
|
|
Tags: b.config.OMITags,
|
|
|
|
SnapshotTags: b.config.SnapshotTags,
|
|
|
|
Ctx: b.config.ctx,
|
|
|
|
},
|
2019-02-22 14:45:00 -05:00
|
|
|
)
|
|
|
|
|
2019-02-21 16:57:07 -05:00
|
|
|
// Run!
|
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-21 16:57:07 -05:00
|
|
|
|
2019-02-22 14:45:00 -05:00
|
|
|
// If there was an error, return that
|
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
|
|
|
return nil, rawErr.(error)
|
|
|
|
}
|
|
|
|
|
2019-03-05 12:58:59 -05:00
|
|
|
// If there are no OMIs, then just return
|
|
|
|
if _, ok := state.GetOk("omis"); !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the artifact and return it
|
|
|
|
artifact := &osccommon.Artifact{
|
|
|
|
Omis: state.Get("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-03-05 12:58:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return artifact, nil
|
2019-02-15 12:41:36 -05:00
|
|
|
}
|