2017-03-03 03:56:17 -05:00
|
|
|
package ecs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2017-05-25 21:49:35 -04:00
|
|
|
"time"
|
|
|
|
|
2017-03-03 03:56:17 -05:00
|
|
|
"github.com/denverdino/aliyungo/common"
|
|
|
|
"github.com/denverdino/aliyungo/ecs"
|
2017-04-17 09:04:52 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2017-03-03 03:56:17 -05:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
)
|
|
|
|
|
2017-03-04 05:06:32 -05:00
|
|
|
type stepConfigAlicloudVSwitch struct {
|
2017-03-03 03:56:17 -05:00
|
|
|
VSwitchId string
|
|
|
|
ZoneId string
|
|
|
|
isCreate bool
|
|
|
|
CidrBlock string
|
|
|
|
VSwitchName string
|
|
|
|
}
|
|
|
|
|
2017-03-04 05:06:32 -05:00
|
|
|
func (s *stepConfigAlicloudVSwitch) Run(state multistep.StateBag) multistep.StepAction {
|
2017-03-03 03:56:17 -05:00
|
|
|
client := state.Get("client").(*ecs.Client)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
vpcId := state.Get("vpcid").(string)
|
|
|
|
config := state.Get("config").(Config)
|
|
|
|
|
|
|
|
if len(s.VSwitchId) != 0 {
|
|
|
|
vswitchs, _, err := client.DescribeVSwitches(&ecs.DescribeVSwitchesArgs{
|
|
|
|
VpcId: vpcId,
|
|
|
|
VSwitchId: s.VSwitchId,
|
|
|
|
ZoneId: s.ZoneId,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2017-05-25 21:49:35 -04:00
|
|
|
ui.Say(fmt.Sprintf("Failed querying vswitch: %s", err))
|
2017-03-03 03:56:17 -05:00
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
if len(vswitchs) > 0 {
|
|
|
|
vswitch := vswitchs[0]
|
|
|
|
state.Put("vswitchid", vswitch.VSwitchId)
|
|
|
|
s.isCreate = false
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
s.isCreate = false
|
2017-05-25 21:49:35 -04:00
|
|
|
message := fmt.Sprintf("The specified vswitch {%s} doesn't exist.", s.VSwitchId)
|
2017-03-03 03:56:17 -05:00
|
|
|
state.Put("error", errors.New(message))
|
|
|
|
ui.Say(message)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
|
|
|
|
}
|
|
|
|
if s.ZoneId == "" {
|
|
|
|
|
|
|
|
zones, err := client.DescribeZones(common.Region(config.AlicloudRegion))
|
|
|
|
if err != nil {
|
2017-03-04 05:06:32 -05:00
|
|
|
ui.Say(fmt.Sprintf("Query for available zones failed: %s", err))
|
2017-03-03 03:56:17 -05:00
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
var instanceTypes []string
|
|
|
|
for _, zone := range zones {
|
|
|
|
isVSwitchSupported := false
|
|
|
|
for _, resourceType := range zone.AvailableResourceCreation.ResourceTypes {
|
|
|
|
if resourceType == ecs.ResourceTypeVSwitch {
|
|
|
|
isVSwitchSupported = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if isVSwitchSupported {
|
|
|
|
for _, instanceType := range zone.AvailableInstanceTypes.InstanceTypes {
|
|
|
|
if instanceType == config.InstanceType {
|
|
|
|
s.ZoneId = zone.ZoneId
|
|
|
|
break
|
|
|
|
}
|
|
|
|
instanceTypes = append(instanceTypes, instanceType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.ZoneId == "" {
|
|
|
|
if len(instanceTypes) > 0 {
|
2017-03-04 05:06:32 -05:00
|
|
|
ui.Say(fmt.Sprintf("The instance type %s isn't available in this region."+
|
|
|
|
"\n You can either change the instance to one of following: %v \n"+
|
2017-03-03 03:56:17 -05:00
|
|
|
"or choose another region", config.InstanceType, instanceTypes))
|
|
|
|
|
2017-03-04 05:06:32 -05:00
|
|
|
state.Put("error", fmt.Errorf("The instance type %s isn't available in this region."+
|
|
|
|
"\n You can either change the instance to one of following: %v \n"+
|
2017-03-03 03:56:17 -05:00
|
|
|
"or choose another region", config.InstanceType, instanceTypes))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
} else {
|
2017-03-04 05:06:32 -05:00
|
|
|
ui.Say(fmt.Sprintf("The instance type %s isn't available in this region."+
|
|
|
|
"\n You can change to other regions \n", config.InstanceType))
|
2017-03-03 03:56:17 -05:00
|
|
|
|
2017-03-04 05:06:32 -05:00
|
|
|
state.Put("error", fmt.Errorf("The instance type %s isn't available in this region."+
|
|
|
|
"\n You can change to other regions \n", config.InstanceType))
|
2017-03-03 03:56:17 -05:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if config.CidrBlock == "" {
|
|
|
|
s.CidrBlock = "172.16.0.0/24" //use the default CirdBlock
|
|
|
|
}
|
|
|
|
ui.Say("Start creating vswitch...")
|
|
|
|
vswitchId, err := client.CreateVSwitch(&ecs.CreateVSwitchArgs{
|
|
|
|
CidrBlock: s.CidrBlock,
|
|
|
|
ZoneId: s.ZoneId,
|
|
|
|
VpcId: vpcId,
|
|
|
|
VSwitchName: s.VSwitchName,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Say(fmt.Sprintf("Create vswitch failed %v", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
if err := client.WaitForVSwitchAvailable(vpcId, s.VSwitchId, ALICLOUD_DEFAULT_TIMEOUT); err != nil {
|
|
|
|
state.Put("error", err)
|
2017-03-04 05:06:32 -05:00
|
|
|
ui.Error(fmt.Sprintf("Timeout waiting for vswitch to become avaiable: %v", err))
|
2017-03-03 03:56:17 -05:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
state.Put("vswitchid", vswitchId)
|
|
|
|
s.isCreate = true
|
|
|
|
s.VSwitchId = vswitchId
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2017-03-04 05:06:32 -05:00
|
|
|
func (s *stepConfigAlicloudVSwitch) Cleanup(state multistep.StateBag) {
|
2017-03-03 03:56:17 -05:00
|
|
|
if !s.isCreate {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
client := state.Get("client").(*ecs.Client)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
message(state, "vSwitch")
|
|
|
|
start := time.Now().Add(10 * time.Second)
|
|
|
|
for {
|
|
|
|
if err := client.DeleteVSwitch(s.VSwitchId); err != nil {
|
|
|
|
e, _ := err.(*common.Error)
|
2017-05-25 22:59:53 -04:00
|
|
|
if (e.Code == "IncorrectVSwitchStatus" || e.Code == "DependencyViolation" ||
|
|
|
|
e.Code == "DependencyViolation.HaVip" ||
|
|
|
|
e.Code == "IncorretRouteEntryStatus") && time.Now().Before(start) {
|
2017-03-03 03:56:17 -05:00
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
continue
|
|
|
|
}
|
2017-05-25 21:49:35 -04:00
|
|
|
ui.Error(fmt.Sprintf("Error deleting vswitch, it may still be around: %s", err))
|
2017-03-03 03:56:17 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|