2017-03-03 03:56:17 -05:00
|
|
|
package ecs
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2017-03-03 03:56:17 -05:00
|
|
|
"fmt"
|
2017-05-25 21:49:35 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
|
2017-03-03 03:56:17 -05:00
|
|
|
"github.com/denverdino/aliyungo/common"
|
|
|
|
"github.com/denverdino/aliyungo/ecs"
|
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"
|
2017-03-03 03:56:17 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type stepCreateAlicloudInstance struct {
|
|
|
|
IOOptimized bool
|
|
|
|
InstanceType string
|
|
|
|
UserData string
|
|
|
|
UserDataFile string
|
|
|
|
instanceId string
|
|
|
|
RegionId string
|
|
|
|
InternetChargeType string
|
|
|
|
InternetMaxBandwidthOut int
|
2018-03-13 04:04:50 -04:00
|
|
|
InstanceName string
|
2017-03-03 03:56:17 -05:00
|
|
|
ZoneId string
|
|
|
|
instance *ecs.InstanceAttributesType
|
|
|
|
}
|
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *stepCreateAlicloudInstance) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2017-03-03 03:56:17 -05:00
|
|
|
client := state.Get("client").(*ecs.Client)
|
|
|
|
config := state.Get("config").(Config)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
source_image := state.Get("source_image").(*ecs.ImageType)
|
|
|
|
network_type := state.Get("networktype").(InstanceNetWork)
|
|
|
|
securityGroupId := state.Get("securitygroupid").(string)
|
|
|
|
var instanceId string
|
|
|
|
var err error
|
|
|
|
|
|
|
|
ioOptimized := ecs.IoOptimizedNone
|
|
|
|
if s.IOOptimized {
|
|
|
|
ioOptimized = ecs.IoOptimizedOptimized
|
|
|
|
}
|
|
|
|
password := config.Comm.SSHPassword
|
|
|
|
if password == "" && config.Comm.WinRMPassword != "" {
|
|
|
|
password = config.Comm.WinRMPassword
|
|
|
|
}
|
2017-06-01 22:33:12 -04:00
|
|
|
ui.Say("Creating instance.")
|
2017-03-03 03:56:17 -05:00
|
|
|
if network_type == VpcNet {
|
|
|
|
userData, err := s.getUserData(state)
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
vswitchId := state.Get("vswitchid").(string)
|
|
|
|
instanceId, err = client.CreateInstance(&ecs.CreateInstanceArgs{
|
|
|
|
RegionId: common.Region(s.RegionId),
|
|
|
|
ImageId: source_image.ImageId,
|
|
|
|
InstanceType: s.InstanceType,
|
|
|
|
InternetChargeType: common.InternetChargeType(s.InternetChargeType), //"PayByTraffic",
|
|
|
|
InternetMaxBandwidthOut: s.InternetMaxBandwidthOut,
|
|
|
|
UserData: userData,
|
|
|
|
IoOptimized: ioOptimized,
|
|
|
|
VSwitchId: vswitchId,
|
|
|
|
SecurityGroupId: securityGroupId,
|
2018-03-13 04:04:50 -04:00
|
|
|
InstanceName: s.InstanceName,
|
2017-03-03 03:56:17 -05:00
|
|
|
Password: password,
|
|
|
|
ZoneId: s.ZoneId,
|
|
|
|
DataDisk: diskDeviceToDiskType(config.AlicloudImageConfig.ECSImagesDiskMappings),
|
|
|
|
})
|
|
|
|
if err != nil {
|
2017-05-25 21:49:35 -04:00
|
|
|
err := fmt.Errorf("Error creating instance: %s", err)
|
2017-03-03 03:56:17 -05:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if s.InstanceType == "" {
|
|
|
|
s.InstanceType = "PayByTraffic"
|
|
|
|
}
|
|
|
|
if s.InternetMaxBandwidthOut == 0 {
|
|
|
|
s.InternetMaxBandwidthOut = 5
|
|
|
|
}
|
|
|
|
instanceId, err = client.CreateInstance(&ecs.CreateInstanceArgs{
|
|
|
|
RegionId: common.Region(s.RegionId),
|
|
|
|
ImageId: source_image.ImageId,
|
|
|
|
InstanceType: s.InstanceType,
|
|
|
|
InternetChargeType: common.InternetChargeType(s.InternetChargeType), //"PayByTraffic",
|
|
|
|
InternetMaxBandwidthOut: s.InternetMaxBandwidthOut,
|
|
|
|
IoOptimized: ioOptimized,
|
|
|
|
SecurityGroupId: securityGroupId,
|
2018-03-13 04:04:50 -04:00
|
|
|
InstanceName: s.InstanceName,
|
2017-03-03 03:56:17 -05:00
|
|
|
Password: password,
|
|
|
|
ZoneId: s.ZoneId,
|
|
|
|
DataDisk: diskDeviceToDiskType(config.AlicloudImageConfig.ECSImagesDiskMappings),
|
|
|
|
})
|
|
|
|
if err != nil {
|
2017-05-25 21:49:35 -04:00
|
|
|
err := fmt.Errorf("Error creating instance: %s", err)
|
2017-03-03 03:56:17 -05:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = client.WaitForInstance(instanceId, ecs.Stopped, ALICLOUD_DEFAULT_TIMEOUT)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating instance: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
instance, err := client.DescribeInstanceAttribute(instanceId)
|
|
|
|
if err != nil {
|
2017-05-25 21:49:35 -04:00
|
|
|
ui.Say(err.Error())
|
2017-03-03 03:56:17 -05:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
s.instance = instance
|
|
|
|
state.Put("instance", instance)
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepCreateAlicloudInstance) Cleanup(state multistep.StateBag) {
|
|
|
|
if s.instance == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
message(state, "instance")
|
|
|
|
client := state.Get("client").(*ecs.Client)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
err := client.DeleteInstance(s.instance.InstanceId)
|
|
|
|
if err != nil {
|
2017-06-01 23:37:16 -04:00
|
|
|
ui.Say(fmt.Sprintf("Failed to clean up instance %s: %v", s.instance.InstanceId, err.Error()))
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepCreateAlicloudInstance) getUserData(state multistep.StateBag) (string, error) {
|
|
|
|
userData := s.UserData
|
|
|
|
if s.UserDataFile != "" {
|
|
|
|
data, err := ioutil.ReadFile(s.UserDataFile)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
userData = string(data)
|
|
|
|
}
|
|
|
|
log.Printf(userData)
|
|
|
|
return userData, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func diskDeviceToDiskType(diskDevices []AlicloudDiskDevice) []ecs.DataDiskType {
|
|
|
|
result := make([]ecs.DataDiskType, len(diskDevices))
|
|
|
|
for _, diskDevice := range diskDevices {
|
|
|
|
result = append(result, ecs.DataDiskType{
|
|
|
|
DiskName: diskDevice.DiskName,
|
|
|
|
Category: ecs.DiskCategory(diskDevice.DiskCategory),
|
|
|
|
Size: diskDevice.DiskSize,
|
|
|
|
SnapshotId: diskDevice.SnapshotId,
|
|
|
|
Description: diskDevice.Description,
|
|
|
|
DeleteWithInstance: diskDevice.DeleteWithInstance,
|
|
|
|
Device: diskDevice.Device,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|