packer-cn/builder/azure/chroot/builder.go

372 lines
11 KiB
Go
Raw Normal View History

2019-05-16 20:24:53 -04:00
package chroot
import (
"context"
"errors"
2019-05-27 02:20:11 -04:00
"fmt"
2019-06-03 01:27:33 -04:00
"github.com/Azure/go-autorest/autorest/azure"
2019-06-03 04:33:31 -04:00
"github.com/Azure/go-autorest/autorest/to"
2019-05-27 02:20:11 -04:00
"log"
2019-05-16 20:24:53 -04:00
"runtime"
2019-06-03 01:27:33 -04:00
"strings"
2019-05-16 20:24:53 -04:00
2019-05-31 14:39:43 -04:00
"github.com/hashicorp/packer/builder/amazon/chroot"
2019-05-16 20:24:53 -04:00
azcommon "github.com/hashicorp/packer/builder/azure/common"
2019-05-27 02:20:11 -04:00
"github.com/hashicorp/packer/builder/azure/common/client"
2019-05-16 20:24:53 -04:00
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
2019-05-31 16:02:25 -04:00
"github.com/Azure/azure-sdk-for-go/profiles/latest/compute/mgmt/compute"
2019-05-16 20:24:53 -04:00
)
type Config struct {
common.PackerConfig `mapstructure:",squash"`
2019-05-31 18:15:56 -04:00
ClientConfig client.Config `mapstructure:",squash"`
2019-06-03 04:33:31 -04:00
FromScratch bool `mapstructure:"from_scratch"`
Source string `mapstructure:"source"`
2019-05-27 02:20:11 -04:00
2019-05-31 14:49:35 -04:00
CommandWrapper string `mapstructure:"command_wrapper"`
PreMountCommands []string `mapstructure:"pre_mount_commands"`
MountOptions []string `mapstructure:"mount_options"`
MountPartition string `mapstructure:"mount_partition"`
MountPath string `mapstructure:"mount_path"`
PostMountCommands []string `mapstructure:"post_mount_commands"`
ChrootMounts [][]string `mapstructure:"chroot_mounts"`
CopyFiles []string `mapstructure:"copy_files"`
2019-06-03 01:27:33 -04:00
TemporaryOSDiskName string `mapstructure:"temporary_os_disk_name"`
2019-05-31 16:02:25 -04:00
OSDiskSizeGB int32 `mapstructure:"os_disk_size_gb"`
OSDiskStorageAccountType string `mapstructure:"os_disk_storage_account_type"`
OSDiskCacheType string `mapstructure:"os_disk_cache_type"`
2019-06-03 01:27:33 -04:00
ImageResourceID string `mapstructure:"image_resource_id"`
ImageOSState string `mapstructure:"image_os_state"`
ImageHyperVGeneration string `mapstructure:"image_hyperv_generation"`
2019-05-27 02:20:11 -04:00
2019-05-16 20:24:53 -04:00
ctx interpolate.Context
}
2019-06-03 01:27:33 -04:00
func (c *Config) GetContext() interpolate.Context {
return c.ctx
}
2019-05-16 20:24:53 -04:00
type Builder struct {
config Config
runner multistep.Runner
}
func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
b.config.ctx.Funcs = azcommon.TemplateFuncs
err := config.Decode(&b.config, &config.DecodeOpts{
Interpolate: true,
InterpolateContext: &b.config.ctx,
InterpolateFilter: &interpolate.RenderFilter{
Exclude: []string{
2019-05-31 14:49:35 -04:00
// these fields are interpolated in the steps,
// when more information is available
"command_wrapper",
"post_mount_commands",
"pre_mount_commands",
"mount_path",
2019-05-16 20:24:53 -04:00
},
},
}, raws...)
2019-06-03 04:33:31 -04:00
var errs *packer.MultiError
var warns []string
2019-05-31 16:02:25 -04:00
// Defaults
2019-05-31 18:15:56 -04:00
err = b.config.ClientConfig.SetDefaultValues()
if err != nil {
return nil, err
}
2019-05-31 16:02:25 -04:00
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 {
if !b.config.FromScratch {
b.config.CopyFiles = []string{"/etc/resolv.conf"}
}
}
if b.config.CommandWrapper == "" {
b.config.CommandWrapper = "{{.Command}}"
}
if b.config.MountPath == "" {
2019-05-31 18:15:56 -04:00
b.config.MountPath = "/mnt/packer-azure-chroot-disks/{{.Device}}"
2019-05-31 16:02:25 -04:00
}
if b.config.MountPartition == "" {
b.config.MountPartition = "1"
}
2019-06-03 01:27:33 -04:00
if b.config.TemporaryOSDiskName == "" {
2019-06-03 04:33:31 -04:00
if def, err := interpolate.Render("PackerTemp-{{timestamp}}", &b.config.ctx); err == nil {
b.config.TemporaryOSDiskName = def
} else {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("unable to render temporary disk name: %s", err))
}
2019-06-03 01:27:33 -04:00
}
2019-05-27 02:20:11 -04:00
if b.config.OSDiskStorageAccountType == "" {
b.config.OSDiskStorageAccountType = string(compute.PremiumLRS)
}
2019-05-31 16:02:25 -04:00
if b.config.OSDiskCacheType == "" {
b.config.OSDiskCacheType = string(compute.CachingTypesReadOnly)
}
2019-05-31 18:15:56 -04:00
if b.config.ImageOSState == "" {
b.config.ImageOSState = string(compute.Generalized)
2019-06-03 01:27:33 -04:00
}
2019-05-31 18:15:56 -04:00
2019-06-03 01:27:33 -04:00
if b.config.ImageHyperVGeneration == "" {
b.config.ImageHyperVGeneration = string(compute.V1)
2019-05-31 18:15:56 -04:00
}
2019-05-16 20:24:53 -04:00
// checks, accumulate any errors or warnings
2019-05-27 02:20:11 -04:00
if b.config.FromScratch {
2019-06-03 04:33:31 -04:00
if b.config.Source != "" {
errs = packer.MultiErrorAppend(
errs, errors.New("source cannot be specified when building from_scratch"))
}
2019-05-27 02:20:11 -04:00
if b.config.OSDiskSizeGB == 0 {
errs = packer.MultiErrorAppend(
2019-05-31 18:15:56 -04:00
errs, errors.New("os_disk_size_gb is required with from_scratch"))
2019-05-27 02:20:11 -04:00
}
if len(b.config.PreMountCommands) == 0 {
errs = packer.MultiErrorAppend(
2019-05-31 14:49:35 -04:00
errs, errors.New("pre_mount_commands is required with from_scratch"))
}
2019-05-31 18:15:56 -04:00
} else {
2019-06-03 04:33:31 -04:00
if _, err := client.ParsePlatformImageURN(b.config.Source); err == nil {
log.Println("Source is platform image:", b.config.Source)
} else {
errs = packer.MultiErrorAppend(
errs, fmt.Errorf("source: %q is not a valid platform image specifier", b.config.Source))
}
2019-05-27 02:20:11 -04:00
}
2019-05-31 18:15:56 -04:00
if err := checkDiskCacheType(b.config.OSDiskCacheType); err != nil {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("os_disk_cache_type: %v", err))
2019-05-16 20:24:53 -04:00
}
2019-06-03 01:27:33 -04:00
2019-05-31 18:15:56 -04:00
if err := checkStorageAccountType(b.config.OSDiskStorageAccountType); err != nil {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("os_disk_storage_account_type: %v", err))
}
2019-06-03 01:27:33 -04:00
if b.config.ImageResourceID == "" {
errs = packer.MultiErrorAppend(errs, errors.New("image_resource_id is required"))
} else {
r, err := azure.ParseResourceID(b.config.ImageResourceID)
if err != nil ||
!strings.EqualFold(r.Provider, "Microsoft.Compute") ||
!strings.EqualFold(r.ResourceType, "images") {
errs = packer.MultiErrorAppend(fmt.Errorf(
"image_resource_id: %q is not a valid image resource id", b.config.ImageResourceID))
}
}
if err := checkOSState(b.config.ImageOSState); err != nil {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("image_os_state: %v", err))
}
if err := checkHyperVGeneration(b.config.ImageHyperVGeneration); err != nil {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("image_hyperv_generation: %v", err))
}
2019-05-31 18:15:56 -04:00
if errs != nil {
return warns, errs
}
packer.LogSecretFilter.Set(b.config.ClientConfig.ClientSecret, b.config.ClientConfig.ClientJWT)
return warns, nil
}
func checkOSState(s string) interface{} {
for _, v := range compute.PossibleOperatingSystemStateTypesValues() {
if compute.OperatingSystemStateTypes(s) == v {
return nil
}
}
2019-06-03 01:27:33 -04:00
return fmt.Errorf("%q is not a valid value %v",
2019-05-31 18:15:56 -04:00
s, compute.PossibleOperatingSystemStateTypesValues())
}
func checkDiskCacheType(s string) interface{} {
for _, v := range compute.PossibleCachingTypesValues() {
if compute.CachingTypes(s) == v {
return nil
}
}
2019-06-03 01:27:33 -04:00
return fmt.Errorf("%q is not a valid value %v",
2019-05-31 18:15:56 -04:00
s, compute.PossibleCachingTypesValues())
}
func checkStorageAccountType(s string) interface{} {
2019-06-02 15:29:45 -04:00
for _, v := range compute.PossibleDiskStorageAccountTypesValues() {
if compute.DiskStorageAccountTypes(s) == v {
2019-05-31 18:15:56 -04:00
return nil
}
}
2019-06-03 01:27:33 -04:00
return fmt.Errorf("%q is not a valid value %v",
2019-06-02 15:29:45 -04:00
s, compute.PossibleDiskStorageAccountTypesValues())
2019-05-16 20:24:53 -04:00
}
2019-06-03 01:27:33 -04:00
func checkHyperVGeneration(s string) interface{} {
for _, v := range compute.PossibleHyperVGenerationValues() {
if compute.HyperVGeneration(s) == v {
return nil
}
}
return fmt.Errorf("%q is not a valid value %v",
s, compute.PossibleHyperVGenerationValues())
}
2019-05-16 20:24:53 -04:00
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
if runtime.GOOS != "linux" {
2019-05-31 14:49:35 -04:00
return nil, errors.New("the azure-chroot builder only works on Linux environments")
2019-05-16 20:24:53 -04:00
}
2019-05-31 18:15:56 -04:00
err := b.config.ClientConfig.FillParameters()
if err != nil {
return nil, fmt.Errorf("error setting Azure client defaults: %v", err)
}
azcli, err := client.New(b.config.ClientConfig, ui.Say)
if err != nil {
return nil, fmt.Errorf("error creating Azure client: %v", err)
}
2019-05-27 02:20:11 -04:00
wrappedCommand := func(command string) (string, error) {
ictx := b.config.ctx
ictx.Data = &struct{ Command string }{Command: command}
return interpolate.Render(b.config.CommandWrapper, &ictx)
}
2019-05-16 20:24:53 -04:00
// Setup the state bag and initial state for the steps
state := new(multistep.BasicStateBag)
state.Put("config", &b.config)
state.Put("hook", hook)
state.Put("ui", ui)
2019-05-31 16:02:25 -04:00
state.Put("azureclient", azcli)
2019-05-31 14:39:43 -04:00
state.Put("wrappedCommand", chroot.CommandWrapper(wrappedCommand))
2019-05-16 20:24:53 -04:00
2019-05-27 02:20:11 -04:00
info, err := azcli.MetadataClient().GetComputeInfo()
if err != nil {
log.Printf("MetadataClient().GetComputeInfo(): error: %+v", err)
err := fmt.Errorf(
"Error retrieving information ARM resource ID and location" +
"of the VM that Packer is running on.\n" +
"Please verify that Packer is running on a proper Azure VM.")
ui.Error(err.Error())
return nil, err
}
state.Put("instance", info)
2019-05-16 20:24:53 -04:00
// Build the steps
var steps []multistep.Step
2019-06-03 04:33:31 -04:00
if b.config.FromScratch {
2019-05-27 02:20:11 -04:00
steps = append(steps,
&StepCreateNewDisk{
SubscriptionID: info.SubscriptionID,
ResourceGroup: info.ResourceGroupName,
2019-06-03 01:27:33 -04:00
DiskName: b.config.TemporaryOSDiskName,
2019-05-27 02:20:11 -04:00
DiskSizeGB: b.config.OSDiskSizeGB,
DiskStorageAccountType: b.config.OSDiskStorageAccountType,
2019-06-03 01:27:33 -04:00
HyperVGeneration: b.config.ImageHyperVGeneration,
Location: info.Location,
2019-05-27 02:20:11 -04:00
})
2019-06-03 04:33:31 -04:00
} else {
if pi, err := client.ParsePlatformImageURN(b.config.Source); err == nil {
if strings.EqualFold(pi.Version, "latest") {
vmi, err := azcli.VirtualMachineImagesClient().GetLatest(ctx, pi.Publisher, pi.Offer, pi.Sku, info.Location)
if err != nil {
return nil, fmt.Errorf("error retieving latest version of %q: %v", b.config.Source, err)
}
pi.Version = to.String(vmi.Name)
log.Println("Resolved latest version of source image:", pi.Version)
}
steps = append(steps,
&StepCreateNewDisk{
SubscriptionID: info.SubscriptionID,
ResourceGroup: info.ResourceGroupName,
DiskName: b.config.TemporaryOSDiskName,
DiskSizeGB: b.config.OSDiskSizeGB,
DiskStorageAccountType: b.config.OSDiskStorageAccountType,
HyperVGeneration: b.config.ImageHyperVGeneration,
Location: info.Location,
PlatformImage: pi,
})
} else {
panic("Unknown image source: " + b.config.Source)
}
2019-05-27 02:20:11 -04:00
}
steps = append(steps,
2019-05-31 16:02:25 -04:00
&StepAttachDisk{}, // uses os_disk_resource_id and sets 'device' in stateBag
2019-05-31 14:39:43 -04:00
&chroot.StepPreMountCommands{
Commands: b.config.PreMountCommands,
},
2019-05-31 14:39:43 -04:00
&StepMountDevice{
MountOptions: b.config.MountOptions,
MountPartition: b.config.MountPartition,
MountPath: b.config.MountPath,
},
&chroot.StepPostMountCommands{
Commands: b.config.PostMountCommands,
},
2019-05-31 14:49:35 -04:00
&chroot.StepMountExtra{
ChrootMounts: b.config.ChrootMounts,
},
&chroot.StepCopyFiles{
Files: b.config.CopyFiles,
},
&chroot.StepChrootProvision{},
2019-05-31 15:01:47 -04:00
&chroot.StepEarlyCleanup{},
2019-05-31 16:02:25 -04:00
&StepCreateImage{
ImageResourceID: b.config.ImageResourceID,
ImageOSState: b.config.ImageOSState,
OSDiskCacheType: b.config.OSDiskCacheType,
OSDiskStorageAccountType: b.config.OSDiskStorageAccountType,
2019-06-03 01:27:33 -04:00
Location: info.Location,
2019-05-31 16:02:25 -04:00
},
)
2019-05-16 20:24:53 -04:00
// Run!
b.runner = common.NewRunner(steps, b.config.PackerConfig, ui)
b.runner.Run(ctx, state)
// If there was an error, return that
if rawErr, ok := state.GetOk("error"); ok {
return nil, rawErr.(error)
}
return nil, nil
}
var _ packer.Builder = &Builder{}