2019-10-14 10:43:59 -04:00
|
|
|
//go:generate mapstructure-to-hcl2 -type Config,AlicloudDiskDevice
|
|
|
|
|
2017-03-03 03:56:17 -05:00
|
|
|
// The alicloud contains a packer.Builder implementation that
|
|
|
|
// builds ecs images for alicloud.
|
|
|
|
package ecs
|
|
|
|
|
|
|
|
import (
|
2019-03-22 09:53:28 -04:00
|
|
|
"context"
|
2017-07-19 23:13:59 -04:00
|
|
|
"fmt"
|
2017-12-28 21:03:55 -05:00
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2017-04-17 09:04:52 -04:00
|
|
|
"github.com/hashicorp/packer/common"
|
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
|
|
|
"github.com/hashicorp/packer/helper/config"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-17 09:04:52 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2017-03-03 03:56:17 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// The unique ID for this builder
|
|
|
|
const BuilderId = "alibaba.alicloud"
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
AlicloudAccessConfig `mapstructure:",squash"`
|
|
|
|
AlicloudImageConfig `mapstructure:",squash"`
|
|
|
|
RunConfig `mapstructure:",squash"`
|
|
|
|
|
|
|
|
ctx interpolate.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
type Builder struct {
|
|
|
|
config Config
|
|
|
|
runner multistep.Runner
|
|
|
|
}
|
|
|
|
|
|
|
|
type InstanceNetWork string
|
|
|
|
|
|
|
|
const (
|
|
|
|
ALICLOUD_DEFAULT_SHORT_TIMEOUT = 180
|
|
|
|
ALICLOUD_DEFAULT_TIMEOUT = 1800
|
|
|
|
ALICLOUD_DEFAULT_LONG_TIMEOUT = 3600
|
|
|
|
)
|
|
|
|
|
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) {
|
2017-03-03 03:56:17 -05:00
|
|
|
err := config.Decode(&b.config, &config.DecodeOpts{
|
|
|
|
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
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 20:45:37 -05:00
|
|
|
if b.config.PackerConfig.PackerForce {
|
|
|
|
b.config.AlicloudImageForceDelete = true
|
|
|
|
b.config.AlicloudImageForceDeleteSnapshots = true
|
|
|
|
}
|
|
|
|
|
2017-03-03 03:56:17 -05:00
|
|
|
// Accumulate any errors
|
|
|
|
var errs *packer.MultiError
|
|
|
|
errs = packer.MultiErrorAppend(errs, b.config.AlicloudAccessConfig.Prepare(&b.config.ctx)...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, b.config.AlicloudImageConfig.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
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
2018-08-10 17:25:14 -04:00
|
|
|
packer.LogSecretFilter.Set(b.config.AlicloudAccessKey, b.config.AlicloudSecretKey)
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, nil, nil
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
2019-03-22 09:53:28 -04:00
|
|
|
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
|
2017-03-03 03:56:17 -05:00
|
|
|
|
|
|
|
client, err := b.config.Client()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
state := new(multistep.BasicStateBag)
|
2018-09-18 09:40:57 -04:00
|
|
|
state.Put("config", &b.config)
|
2017-03-03 03:56:17 -05:00
|
|
|
state.Put("client", client)
|
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
|
|
|
state.Put("networktype", b.chooseNetworkType())
|
|
|
|
var steps []multistep.Step
|
2017-03-04 05:06:32 -05:00
|
|
|
|
|
|
|
// Build the steps
|
|
|
|
steps = []multistep.Step{
|
|
|
|
&stepPreValidate{
|
|
|
|
AlicloudDestImageName: b.config.AlicloudImageName,
|
2018-03-13 03:36:38 -04:00
|
|
|
ForceDelete: b.config.AlicloudImageForceDelete,
|
2017-03-04 05:06:32 -05:00
|
|
|
},
|
|
|
|
&stepCheckAlicloudSourceImage{
|
|
|
|
SourceECSImageId: b.config.AlicloudSourceImage,
|
|
|
|
},
|
2018-06-15 00:32:53 -04:00
|
|
|
&stepConfigAlicloudKeyPair{
|
2018-08-28 11:46:41 -04:00
|
|
|
Debug: b.config.PackerDebug,
|
|
|
|
Comm: &b.config.Comm,
|
|
|
|
DebugKeyPath: fmt.Sprintf("ecs_%s.pem", b.config.PackerBuildName),
|
|
|
|
RegionId: b.config.AlicloudRegion,
|
2017-03-04 05:06:32 -05:00
|
|
|
},
|
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
if b.chooseNetworkType() == InstanceNetworkVpc {
|
2017-03-04 05:06:32 -05:00
|
|
|
steps = append(steps,
|
2017-03-03 03:56:17 -05:00
|
|
|
&stepConfigAlicloudVPC{
|
|
|
|
VpcId: b.config.VpcId,
|
|
|
|
CidrBlock: b.config.CidrBlock,
|
|
|
|
VpcName: b.config.VpcName,
|
|
|
|
},
|
2017-03-04 05:06:32 -05:00
|
|
|
&stepConfigAlicloudVSwitch{
|
2017-03-03 03:56:17 -05:00
|
|
|
VSwitchId: b.config.VSwitchId,
|
|
|
|
ZoneId: b.config.ZoneId,
|
|
|
|
CidrBlock: b.config.CidrBlock,
|
|
|
|
VSwitchName: b.config.VSwitchName,
|
2017-03-04 05:06:32 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
steps = append(steps,
|
|
|
|
&stepConfigAlicloudSecurityGroup{
|
|
|
|
SecurityGroupId: b.config.SecurityGroupId,
|
|
|
|
SecurityGroupName: b.config.SecurityGroupId,
|
|
|
|
RegionId: b.config.AlicloudRegion,
|
|
|
|
},
|
|
|
|
&stepCreateAlicloudInstance{
|
|
|
|
IOOptimized: b.config.IOOptimized,
|
|
|
|
InstanceType: b.config.InstanceType,
|
|
|
|
UserData: b.config.UserData,
|
|
|
|
UserDataFile: b.config.UserDataFile,
|
|
|
|
RegionId: b.config.AlicloudRegion,
|
|
|
|
InternetChargeType: b.config.InternetChargeType,
|
|
|
|
InternetMaxBandwidthOut: b.config.InternetMaxBandwidthOut,
|
2018-03-13 04:04:50 -04:00
|
|
|
InstanceName: b.config.InstanceName,
|
2017-03-04 05:06:32 -05:00
|
|
|
ZoneId: b.config.ZoneId,
|
|
|
|
})
|
2019-04-25 22:37:49 -04:00
|
|
|
if b.chooseNetworkType() == InstanceNetworkVpc {
|
2018-06-15 00:32:53 -04:00
|
|
|
steps = append(steps, &stepConfigAlicloudEIP{
|
2017-03-04 05:06:32 -05:00
|
|
|
AssociatePublicIpAddress: b.config.AssociatePublicIpAddress,
|
|
|
|
RegionId: b.config.AlicloudRegion,
|
2017-05-25 06:27:54 -04:00
|
|
|
InternetChargeType: b.config.InternetChargeType,
|
2018-06-23 04:34:45 -04:00
|
|
|
InternetMaxBandwidthOut: b.config.InternetMaxBandwidthOut,
|
2018-09-11 04:56:57 -04:00
|
|
|
SSHPrivateIp: b.config.SSHPrivateIp,
|
2017-03-04 05:06:32 -05:00
|
|
|
})
|
2017-03-03 03:56:17 -05:00
|
|
|
} else {
|
2017-03-04 05:06:32 -05:00
|
|
|
steps = append(steps, &stepConfigAlicloudPublicIP{
|
2018-09-11 04:56:57 -04:00
|
|
|
RegionId: b.config.AlicloudRegion,
|
|
|
|
SSHPrivateIp: b.config.SSHPrivateIp,
|
2017-03-04 05:06:32 -05:00
|
|
|
})
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
2017-03-04 05:06:32 -05:00
|
|
|
steps = append(steps,
|
2018-06-15 00:32:53 -04:00
|
|
|
&stepAttachKeyPair{},
|
2017-03-04 05:06:32 -05:00
|
|
|
&stepRunAlicloudInstance{},
|
|
|
|
&communicator.StepConnect{
|
|
|
|
Config: &b.config.RunConfig.Comm,
|
|
|
|
Host: SSHHost(
|
|
|
|
client,
|
|
|
|
b.config.SSHPrivateIp),
|
2018-08-22 11:02:23 -04:00
|
|
|
SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(),
|
2017-03-04 05:06:32 -05:00
|
|
|
},
|
|
|
|
&common.StepProvision{},
|
2018-09-14 14:03:23 -04:00
|
|
|
&common.StepCleanupTempKeys{
|
|
|
|
Comm: &b.config.RunConfig.Comm,
|
|
|
|
},
|
2017-03-04 05:06:32 -05:00
|
|
|
&stepStopAlicloudInstance{
|
2018-09-25 07:21:28 -04:00
|
|
|
ForceStop: b.config.ForceStopInstance,
|
|
|
|
DisableStop: b.config.DisableStopInstance,
|
2017-03-04 05:06:32 -05:00
|
|
|
},
|
|
|
|
&stepDeleteAlicloudImageSnapshots{
|
2018-03-13 03:36:38 -04:00
|
|
|
AlicloudImageForceDeleteSnapshots: b.config.AlicloudImageForceDeleteSnapshots,
|
|
|
|
AlicloudImageForceDelete: b.config.AlicloudImageForceDelete,
|
2017-03-04 05:06:32 -05:00
|
|
|
AlicloudImageName: b.config.AlicloudImageName,
|
2018-11-28 08:34:21 -05:00
|
|
|
AlicloudImageDestinationRegions: b.config.AlicloudImageConfig.AlicloudImageDestinationRegions,
|
|
|
|
AlicloudImageDestinationNames: b.config.AlicloudImageConfig.AlicloudImageDestinationNames,
|
2018-11-19 02:25:12 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
if b.config.AlicloudImageIgnoreDataDisks {
|
2018-11-25 02:46:03 -05:00
|
|
|
steps = append(steps, &stepCreateAlicloudSnapshot{
|
|
|
|
WaitSnapshotReadyTimeout: b.getSnapshotReadyTimeout(),
|
|
|
|
})
|
2018-11-19 02:25:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
steps = append(steps,
|
|
|
|
&stepCreateAlicloudImage{
|
|
|
|
AlicloudImageIgnoreDataDisks: b.config.AlicloudImageIgnoreDataDisks,
|
2018-11-25 02:46:03 -05:00
|
|
|
WaitSnapshotReadyTimeout: b.getSnapshotReadyTimeout(),
|
2017-03-04 05:06:32 -05:00
|
|
|
},
|
2018-09-16 06:16:13 -04:00
|
|
|
&stepCreateTags{
|
|
|
|
Tags: b.config.AlicloudImageTags,
|
|
|
|
},
|
2018-06-15 00:32:53 -04:00
|
|
|
&stepRegionCopyAlicloudImage{
|
2017-03-04 05:06:32 -05:00
|
|
|
AlicloudImageDestinationRegions: b.config.AlicloudImageDestinationRegions,
|
|
|
|
AlicloudImageDestinationNames: b.config.AlicloudImageDestinationNames,
|
|
|
|
RegionId: b.config.AlicloudRegion,
|
|
|
|
},
|
2018-06-15 00:32:53 -04:00
|
|
|
&stepShareAlicloudImage{
|
2017-03-04 05:06:32 -05:00
|
|
|
AlicloudImageShareAccounts: b.config.AlicloudImageShareAccounts,
|
|
|
|
AlicloudImageUNShareAccounts: b.config.AlicloudImageUNShareAccounts,
|
|
|
|
RegionId: b.config.AlicloudRegion,
|
|
|
|
})
|
2017-03-03 03:56:17 -05:00
|
|
|
|
|
|
|
// Run!
|
|
|
|
b.runner = common.NewRunner(steps, b.config.PackerConfig, ui)
|
2019-03-22 09:53:28 -04:00
|
|
|
b.runner.Run(ctx, state)
|
2017-03-03 03:56:17 -05:00
|
|
|
|
|
|
|
// If there was an error, return that
|
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
|
|
|
return nil, rawErr.(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are no ECS images, then just return
|
|
|
|
if _, ok := state.GetOk("alicloudimages"); !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the artifact and return it
|
|
|
|
artifact := &Artifact{
|
|
|
|
AlicloudImages: state.Get("alicloudimages").(map[string]string),
|
|
|
|
BuilderIdValue: BuilderId,
|
|
|
|
Client: client,
|
|
|
|
}
|
|
|
|
|
|
|
|
return artifact, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) chooseNetworkType() InstanceNetWork {
|
2017-06-11 04:01:09 -04:00
|
|
|
if b.isVpcNetRequired() {
|
2019-04-25 22:37:49 -04:00
|
|
|
return InstanceNetworkVpc
|
2017-03-03 03:56:17 -05:00
|
|
|
} else {
|
2019-04-25 22:37:49 -04:00
|
|
|
return InstanceNetworkClassic
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
2017-06-11 04:01:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) isVpcNetRequired() bool {
|
|
|
|
// UserData and KeyPair only works in VPC
|
|
|
|
return b.isVpcSpecified() || b.isUserDataNeeded() || b.isKeyPairNeeded()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) isVpcSpecified() bool {
|
|
|
|
return b.config.VpcId != "" || b.config.VSwitchId != ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) isUserDataNeeded() bool {
|
|
|
|
// Public key setup requires userdata
|
2018-08-23 10:35:07 -04:00
|
|
|
if b.config.RunConfig.Comm.SSHPrivateKeyFile != "" {
|
2017-06-11 04:01:09 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.config.UserData != "" || b.config.UserDataFile != ""
|
|
|
|
}
|
2017-03-03 03:56:17 -05:00
|
|
|
|
2017-06-11 04:01:09 -04:00
|
|
|
func (b *Builder) isKeyPairNeeded() bool {
|
2018-08-28 11:46:41 -04:00
|
|
|
return b.config.Comm.SSHKeyPairName != "" || b.config.Comm.SSHTemporaryKeyPairName != ""
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
2018-11-25 02:46:03 -05:00
|
|
|
|
|
|
|
func (b *Builder) getSnapshotReadyTimeout() int {
|
|
|
|
if b.config.WaitSnapshotReadyTimeout > 0 {
|
|
|
|
return b.config.WaitSnapshotReadyTimeout
|
|
|
|
}
|
|
|
|
|
|
|
|
return ALICLOUD_DEFAULT_LONG_TIMEOUT
|
|
|
|
}
|