2019-10-14 10:43:59 -04:00
|
|
|
//go:generate mapstructure-to-hcl2 -type Config
|
|
|
|
|
2019-06-13 03:16:49 -04:00
|
|
|
// The ucloud-uhost contains a packer.Builder implementation that
|
|
|
|
// builds uhost images for UCloud UHost instance.
|
|
|
|
package uhost
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-10-14 10:43:59 -04:00
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2019-10-12 04:46:21 -04:00
|
|
|
ucloudcommon "github.com/hashicorp/packer/builder/ucloud/common"
|
2019-06-13 03:16:49 -04:00
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
|
|
|
"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-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
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-06-13 03:16:49 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// The unique ID for this builder
|
|
|
|
const BuilderId = "ucloud.uhost"
|
|
|
|
|
|
|
|
type Config struct {
|
2019-10-12 04:46:21 -04:00
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
ucloudcommon.AccessConfig `mapstructure:",squash"`
|
|
|
|
ucloudcommon.ImageConfig `mapstructure:",squash"`
|
|
|
|
ucloudcommon.RunConfig `mapstructure:",squash"`
|
2019-06-13 03:16:49 -04:00
|
|
|
|
|
|
|
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-06-13 03:16:49 -04:00
|
|
|
err := config.Decode(&b.config, &config.DecodeOpts{
|
2020-10-09 20:01:55 -04:00
|
|
|
PluginType: BuilderId,
|
2019-06-13 03:16:49 -04:00
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &b.config.ctx,
|
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{
|
|
|
|
"run_command",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, raws...)
|
|
|
|
b.config.ctx.EnableEnv = true
|
|
|
|
if err != nil {
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, nil, err
|
2019-06-13 03:16:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Accumulate any errors
|
2020-11-19 15:07:02 -05:00
|
|
|
var errs *packersdk.MultiError
|
|
|
|
errs = packersdk.MultiErrorAppend(errs, b.config.AccessConfig.Prepare(&b.config.ctx)...)
|
|
|
|
errs = packersdk.MultiErrorAppend(errs, b.config.ImageConfig.Prepare(&b.config.ctx)...)
|
|
|
|
errs = packersdk.MultiErrorAppend(errs, b.config.RunConfig.Prepare(&b.config.ctx)...)
|
2019-06-13 03:16:49 -04:00
|
|
|
|
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, nil, errs
|
2019-06-13 03:16:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
packer.LogSecretFilter.Set(b.config.PublicKey, b.config.PrivateKey)
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, nil, nil
|
2019-06-13 03:16:49 -04:00
|
|
|
}
|
|
|
|
|
2020-11-19 14:54:31 -05:00
|
|
|
func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packer.Hook) (packer.Artifact, error) {
|
2019-06-13 03:16:49 -04:00
|
|
|
|
|
|
|
client, err := b.config.Client()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the state bag and initial state for the steps
|
|
|
|
state := new(multistep.BasicStateBag)
|
|
|
|
state.Put("config", &b.config)
|
|
|
|
state.Put("client", client)
|
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
|
|
|
|
|
|
|
var steps []multistep.Step
|
|
|
|
|
|
|
|
// Build the steps
|
|
|
|
steps = []multistep.Step{
|
|
|
|
&stepPreValidate{
|
2019-06-14 02:50:46 -04:00
|
|
|
ProjectId: b.config.ProjectId,
|
2019-06-13 03:16:49 -04:00
|
|
|
Region: b.config.Region,
|
|
|
|
Zone: b.config.Zone,
|
|
|
|
ImageDestinations: b.config.ImageDestinations,
|
|
|
|
},
|
|
|
|
|
|
|
|
&stepCheckSourceImageId{
|
|
|
|
SourceUHostImageId: b.config.SourceImageId,
|
|
|
|
},
|
|
|
|
|
|
|
|
&stepConfigVPC{
|
|
|
|
VPCId: b.config.VPCId,
|
|
|
|
},
|
|
|
|
&stepConfigSubnet{
|
|
|
|
SubnetId: b.config.SubnetId,
|
|
|
|
},
|
|
|
|
&stepConfigSecurityGroup{
|
|
|
|
SecurityGroupId: b.config.SecurityGroupId,
|
|
|
|
},
|
|
|
|
|
|
|
|
&stepCreateInstance{
|
2020-06-24 08:31:05 -04:00
|
|
|
InstanceType: b.config.InstanceType,
|
|
|
|
Region: b.config.Region,
|
|
|
|
Zone: b.config.Zone,
|
|
|
|
SourceImageId: b.config.SourceImageId,
|
|
|
|
InstanceName: b.config.InstanceName,
|
|
|
|
BootDiskType: b.config.BootDiskType,
|
|
|
|
UsePrivateIp: b.config.UseSSHPrivateIp,
|
|
|
|
EipBandwidth: b.config.EipBandwidth,
|
|
|
|
EipChargeMode: b.config.EipChargeMode,
|
|
|
|
UserData: b.config.UserData,
|
|
|
|
UserDataFile: b.config.UserDataFile,
|
|
|
|
MinCpuPlatform: b.config.MinCpuPlatform,
|
2019-06-13 03:16:49 -04:00
|
|
|
},
|
|
|
|
&communicator.StepConnect{
|
|
|
|
Config: &b.config.RunConfig.Comm,
|
2019-10-12 04:46:21 -04:00
|
|
|
Host: ucloudcommon.SSHHost(
|
2019-06-13 03:16:49 -04:00
|
|
|
b.config.UseSSHPrivateIp),
|
|
|
|
SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(),
|
|
|
|
},
|
2020-11-11 18:04:28 -05:00
|
|
|
&commonsteps.StepProvision{},
|
2019-06-13 03:16:49 -04:00
|
|
|
&stepStopInstance{},
|
|
|
|
&stepCreateImage{},
|
|
|
|
&stepCopyUCloudImage{
|
2019-10-18 02:52:20 -04:00
|
|
|
ImageDestinations: b.config.ImageDestinations,
|
|
|
|
RegionId: b.config.Region,
|
|
|
|
ProjectId: b.config.ProjectId,
|
|
|
|
WaitImageReadyTimeout: b.config.WaitImageReadyTimeout,
|
2019-06-13 03:16:49 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run!
|
2020-11-11 18:04:28 -05:00
|
|
|
b.runner = commonsteps.NewRunner(steps, b.config.PackerConfig, ui)
|
2019-06-13 03:16:49 -04:00
|
|
|
b.runner.Run(ctx, state)
|
|
|
|
|
|
|
|
// If there was an error, return that
|
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
|
|
|
return nil, rawErr.(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are no ucloud images, then just return
|
|
|
|
if _, ok := state.GetOk("ucloud_images"); !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the artifact and return it
|
2019-10-12 04:46:21 -04:00
|
|
|
artifact := &ucloudcommon.Artifact{
|
|
|
|
UCloudImages: state.Get("ucloud_images").(*ucloudcommon.ImageInfoSet),
|
2019-06-13 03:16:49 -04:00
|
|
|
BuilderIdValue: BuilderId,
|
|
|
|
Client: client,
|
2020-01-30 05:27:58 -05:00
|
|
|
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
|
2019-06-13 03:16:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return artifact, nil
|
|
|
|
}
|