|
|
|
|
@ -117,6 +117,9 @@ type Builder struct {
|
|
|
|
|
runner multistep.Runner
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// verify interface implementation
|
|
|
|
|
var _ packer.Builder = &Builder{}
|
|
|
|
|
|
|
|
|
|
func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() }
|
|
|
|
|
|
|
|
|
|
func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
|
|
|
|
|
@ -335,25 +338,48 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
|
|
|
|
|
|
|
|
|
|
state.Put("instance", info)
|
|
|
|
|
|
|
|
|
|
// Build the step array from the config
|
|
|
|
|
steps := buildsteps(b.config, info)
|
|
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Build the artifact and return it
|
|
|
|
|
artifact := &azcommon.Artifact{
|
|
|
|
|
Resources: []string{b.config.ImageResourceID},
|
|
|
|
|
BuilderIdValue: BuilderId,
|
|
|
|
|
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return artifact, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func buildsteps(config Config, info *client.ComputeInfo) []multistep.Step {
|
|
|
|
|
// Build the steps
|
|
|
|
|
var steps []multistep.Step
|
|
|
|
|
|
|
|
|
|
if b.config.FromScratch {
|
|
|
|
|
if config.FromScratch {
|
|
|
|
|
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,
|
|
|
|
|
DiskName: config.TemporaryOSDiskName,
|
|
|
|
|
DiskSizeGB: config.OSDiskSizeGB,
|
|
|
|
|
DiskStorageAccountType: config.OSDiskStorageAccountType,
|
|
|
|
|
HyperVGeneration: config.ImageHyperVGeneration,
|
|
|
|
|
Location: info.Location,
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
switch b.config.sourceType {
|
|
|
|
|
switch config.sourceType {
|
|
|
|
|
case sourcePlatformImage:
|
|
|
|
|
|
|
|
|
|
if pi, err := client.ParsePlatformImageURN(b.config.Source); err == nil {
|
|
|
|
|
if pi, err := client.ParsePlatformImageURN(config.Source); err == nil {
|
|
|
|
|
if strings.EqualFold(pi.Version, "latest") {
|
|
|
|
|
steps = append(steps, &StepResolvePlatformImageVersion{
|
|
|
|
|
PlatformImage: pi,
|
|
|
|
|
@ -361,93 +387,74 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
|
DiskName: config.TemporaryOSDiskName,
|
|
|
|
|
DiskSizeGB: config.OSDiskSizeGB,
|
|
|
|
|
DiskStorageAccountType: config.OSDiskStorageAccountType,
|
|
|
|
|
HyperVGeneration: config.ImageHyperVGeneration,
|
|
|
|
|
Location: info.Location,
|
|
|
|
|
PlatformImage: pi,
|
|
|
|
|
|
|
|
|
|
SkipCleanup: b.config.OSDiskSkipCleanup,
|
|
|
|
|
SkipCleanup: config.OSDiskSkipCleanup,
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
panic("Unknown image source: " + b.config.Source)
|
|
|
|
|
panic("Unknown image source: " + config.Source + " err: " + err.Error())
|
|
|
|
|
}
|
|
|
|
|
case sourceDisk:
|
|
|
|
|
steps = append(steps,
|
|
|
|
|
&StepVerifySourceDisk{
|
|
|
|
|
SourceDiskResourceID: b.config.Source,
|
|
|
|
|
SourceDiskResourceID: config.Source,
|
|
|
|
|
SubscriptionID: info.SubscriptionID,
|
|
|
|
|
Location: info.Location,
|
|
|
|
|
},
|
|
|
|
|
&StepCreateNewDisk{
|
|
|
|
|
SubscriptionID: info.SubscriptionID,
|
|
|
|
|
ResourceGroup: info.ResourceGroupName,
|
|
|
|
|
DiskName: b.config.TemporaryOSDiskName,
|
|
|
|
|
DiskSizeGB: b.config.OSDiskSizeGB,
|
|
|
|
|
DiskStorageAccountType: b.config.OSDiskStorageAccountType,
|
|
|
|
|
HyperVGeneration: b.config.ImageHyperVGeneration,
|
|
|
|
|
SourceDiskResourceID: b.config.Source,
|
|
|
|
|
DiskName: config.TemporaryOSDiskName,
|
|
|
|
|
DiskSizeGB: config.OSDiskSizeGB,
|
|
|
|
|
DiskStorageAccountType: config.OSDiskStorageAccountType,
|
|
|
|
|
HyperVGeneration: config.ImageHyperVGeneration,
|
|
|
|
|
SourceDiskResourceID: config.Source,
|
|
|
|
|
Location: info.Location,
|
|
|
|
|
|
|
|
|
|
SkipCleanup: b.config.OSDiskSkipCleanup,
|
|
|
|
|
SkipCleanup: config.OSDiskSkipCleanup,
|
|
|
|
|
})
|
|
|
|
|
default:
|
|
|
|
|
panic(fmt.Errorf("Unknown source type: %+q", b.config.sourceType))
|
|
|
|
|
panic(fmt.Errorf("Unknown source type: %+q", config.sourceType))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
steps = append(steps,
|
|
|
|
|
&StepAttachDisk{}, // uses os_disk_resource_id and sets 'device' in stateBag
|
|
|
|
|
&chroot.StepPreMountCommands{
|
|
|
|
|
Commands: b.config.PreMountCommands,
|
|
|
|
|
Commands: config.PreMountCommands,
|
|
|
|
|
},
|
|
|
|
|
&StepMountDevice{
|
|
|
|
|
MountOptions: b.config.MountOptions,
|
|
|
|
|
MountPartition: b.config.MountPartition,
|
|
|
|
|
MountPath: b.config.MountPath,
|
|
|
|
|
MountOptions: config.MountOptions,
|
|
|
|
|
MountPartition: config.MountPartition,
|
|
|
|
|
MountPath: config.MountPath,
|
|
|
|
|
},
|
|
|
|
|
&chroot.StepPostMountCommands{
|
|
|
|
|
Commands: b.config.PostMountCommands,
|
|
|
|
|
Commands: config.PostMountCommands,
|
|
|
|
|
},
|
|
|
|
|
&chroot.StepMountExtra{
|
|
|
|
|
ChrootMounts: b.config.ChrootMounts,
|
|
|
|
|
ChrootMounts: config.ChrootMounts,
|
|
|
|
|
},
|
|
|
|
|
&chroot.StepCopyFiles{
|
|
|
|
|
Files: b.config.CopyFiles,
|
|
|
|
|
Files: config.CopyFiles,
|
|
|
|
|
},
|
|
|
|
|
&chroot.StepChrootProvision{},
|
|
|
|
|
&chroot.StepEarlyCleanup{},
|
|
|
|
|
&StepCreateImage{
|
|
|
|
|
ImageResourceID: b.config.ImageResourceID,
|
|
|
|
|
ImageResourceID: config.ImageResourceID,
|
|
|
|
|
ImageOSState: string(compute.Generalized),
|
|
|
|
|
OSDiskCacheType: b.config.OSDiskCacheType,
|
|
|
|
|
OSDiskStorageAccountType: b.config.OSDiskStorageAccountType,
|
|
|
|
|
OSDiskCacheType: config.OSDiskCacheType,
|
|
|
|
|
OSDiskStorageAccountType: config.OSDiskStorageAccountType,
|
|
|
|
|
Location: info.Location,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Build the artifact and return it
|
|
|
|
|
artifact := &azcommon.Artifact{
|
|
|
|
|
Resources: []string{b.config.ImageResourceID},
|
|
|
|
|
BuilderIdValue: BuilderId,
|
|
|
|
|
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return artifact, nil
|
|
|
|
|
return steps
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var _ packer.Builder = &Builder{}
|
|
|
|
|
|