2019-05-31 08:27:41 -04:00
|
|
|
//go:generate struct-markdown
|
|
|
|
|
2017-03-03 03:56:17 -05:00
|
|
|
package ecs
|
|
|
|
|
|
|
|
import (
|
2020-01-02 07:09:35 -05:00
|
|
|
"encoding/json"
|
2017-03-03 03:56:17 -05:00
|
|
|
"fmt"
|
2020-01-02 07:09:35 -05:00
|
|
|
"io/ioutil"
|
2019-04-26 03:12:07 -04:00
|
|
|
"os"
|
2020-01-02 07:09:35 -05:00
|
|
|
"runtime"
|
2019-04-26 03:12:07 -04:00
|
|
|
"time"
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
|
2017-04-17 09:04:52 -04:00
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2019-04-25 22:37:49 -04:00
|
|
|
"github.com/hashicorp/packer/version"
|
2020-01-02 07:09:35 -05:00
|
|
|
"github.com/mitchellh/go-homedir"
|
2017-03-03 03:56:17 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config of alicloud
|
|
|
|
type AlicloudAccessConfig struct {
|
2020-01-02 07:09:35 -05:00
|
|
|
// This is the Alicloud access key. It must be provided when profile not exist, but it can also be
|
2019-08-29 08:39:25 -04:00
|
|
|
// sourced from the ALICLOUD_ACCESS_KEY environment variable.
|
2020-01-02 07:09:35 -05:00
|
|
|
AlicloudAccessKey string `mapstructure:"access_key" required:"false"`
|
|
|
|
// This is the Alicloud secret key. It must be provided when profile not exist, but it can also be
|
2019-08-29 08:39:25 -04:00
|
|
|
// sourced from the ALICLOUD_SECRET_KEY environment variable.
|
2020-01-02 07:09:35 -05:00
|
|
|
AlicloudSecretKey string `mapstructure:"secret_key" required:"false"`
|
|
|
|
// This is the Alicloud region. It must be provided when profile not exist, but it can also be
|
2019-08-29 08:39:25 -04:00
|
|
|
// sourced from the ALICLOUD_REGION environment variables.
|
2020-01-02 07:09:35 -05:00
|
|
|
AlicloudRegion string `mapstructure:"region" required:"false"`
|
2019-08-29 08:39:25 -04:00
|
|
|
// The region validation can be skipped if this value is true, the default
|
|
|
|
// value is false.
|
2019-06-06 10:29:25 -04:00
|
|
|
AlicloudSkipValidation bool `mapstructure:"skip_region_validation" required:"false"`
|
2019-12-19 22:28:31 -05:00
|
|
|
// The image validation can be skipped if this value is true, the default
|
|
|
|
// value is false.
|
|
|
|
AlicloudSkipImageValidation bool `mapstructure:"skip_image_validation" required:"false"`
|
2020-01-02 07:09:35 -05:00
|
|
|
// This is th Alicloud profile. If access_key not exist, is must be provided, but it can also be
|
|
|
|
// sourced from the ALICLOUD_PROFILE environment variables.
|
|
|
|
AlicloudProfile string `mapstructure:"profile" required:"false"`
|
|
|
|
// This is the Alicloud shared credentials file path. If this file path exist, os will read access key
|
|
|
|
// and secret key from this file.
|
|
|
|
AlicloudSharedCredentialsFile string `mapstructure:"shared_credentials_file" required:"false"`
|
2019-08-29 08:39:25 -04:00
|
|
|
// STS access token, can be set through template or by exporting as
|
2019-09-20 09:10:02 -04:00
|
|
|
// environment variable such as `export SECURITY_TOKEN=value`.
|
2019-06-06 10:29:25 -04:00
|
|
|
SecurityToken string `mapstructure:"security_token" required:"false"`
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
client *ClientWrapper
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
const Packer = "HashiCorp-Packer"
|
|
|
|
const DefaultRequestReadTimeout = 10 * time.Second
|
|
|
|
|
2017-03-03 03:56:17 -05:00
|
|
|
// Client for AlicloudClient
|
2019-04-25 22:37:49 -04:00
|
|
|
func (c *AlicloudAccessConfig) Client() (*ClientWrapper, error) {
|
|
|
|
if c.client != nil {
|
|
|
|
return c.client, nil
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
2017-11-27 21:22:16 -05:00
|
|
|
if c.SecurityToken == "" {
|
|
|
|
c.SecurityToken = os.Getenv("SECURITY_TOKEN")
|
|
|
|
}
|
|
|
|
|
2020-01-02 07:09:35 -05:00
|
|
|
var getProviderConfig = func(str string, key string) string {
|
|
|
|
value, err := getConfigFromProfile(c, key)
|
|
|
|
if err == nil && value != nil {
|
|
|
|
str = value.(string)
|
|
|
|
}
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.AlicloudAccessKey == "" || c.AlicloudSecretKey == "" {
|
|
|
|
c.AlicloudAccessKey = getProviderConfig(c.AlicloudAccessKey, "access_key_id")
|
|
|
|
c.AlicloudSecretKey = getProviderConfig(c.AlicloudSecretKey, "access_key_secret")
|
|
|
|
c.AlicloudRegion = getProviderConfig(c.AlicloudRegion, "region_id")
|
|
|
|
c.SecurityToken = getProviderConfig(c.SecurityToken, "sts_token")
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := ecs.NewClientWithStsToken(c.AlicloudRegion, c.AlicloudAccessKey, c.AlicloudSecretKey, c.SecurityToken)
|
2019-04-25 22:37:49 -04:00
|
|
|
if err != nil {
|
2017-03-03 03:56:17 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
client.AppendUserAgent(Packer, version.FormattedVersion())
|
|
|
|
client.SetReadTimeout(DefaultRequestReadTimeout)
|
|
|
|
c.client = &ClientWrapper{client}
|
|
|
|
|
|
|
|
return c.client, nil
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AlicloudAccessConfig) Prepare(ctx *interpolate.Context) []error {
|
|
|
|
var errs []error
|
|
|
|
if err := c.Config(); err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
if c.AlicloudRegion == "" {
|
|
|
|
c.AlicloudRegion = os.Getenv("ALICLOUD_REGION")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.AlicloudRegion == "" {
|
|
|
|
errs = append(errs, fmt.Errorf("region option or ALICLOUD_REGION must be provided in template file or environment variables."))
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs) > 0 {
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AlicloudAccessConfig) Config() error {
|
|
|
|
if c.AlicloudAccessKey == "" {
|
|
|
|
c.AlicloudAccessKey = os.Getenv("ALICLOUD_ACCESS_KEY")
|
|
|
|
}
|
|
|
|
if c.AlicloudSecretKey == "" {
|
|
|
|
c.AlicloudSecretKey = os.Getenv("ALICLOUD_SECRET_KEY")
|
|
|
|
}
|
2020-01-02 07:09:35 -05:00
|
|
|
if c.AlicloudProfile == "" {
|
|
|
|
c.AlicloudProfile = os.Getenv("ALICLOUD_PROFILE")
|
|
|
|
}
|
|
|
|
if c.AlicloudSharedCredentialsFile == "" {
|
|
|
|
c.AlicloudSharedCredentialsFile = os.Getenv("ALICLOUD_SHARED_CREDENTIALS_FILE")
|
|
|
|
}
|
|
|
|
if (c.AlicloudAccessKey == "" || c.AlicloudSecretKey == "") && c.AlicloudProfile == "" {
|
2017-05-25 21:49:35 -04:00
|
|
|
return fmt.Errorf("ALICLOUD_ACCESS_KEY and ALICLOUD_SECRET_KEY must be set in template file or environment variables.")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
func (c *AlicloudAccessConfig) ValidateRegion(region string) error {
|
|
|
|
|
|
|
|
supportedRegions, err := c.getSupportedRegions()
|
|
|
|
if err != nil {
|
2017-03-03 03:56:17 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
for _, supportedRegion := range supportedRegions {
|
|
|
|
if region == supportedRegion {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Not a valid alicloud region: %s", region)
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
func (c *AlicloudAccessConfig) getSupportedRegions() ([]string, error) {
|
|
|
|
client, err := c.Client()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
regionsRequest := ecs.CreateDescribeRegionsRequest()
|
|
|
|
regionsResponse, err := client.DescribeRegions(regionsRequest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-03 03:56:17 -05:00
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
validRegions := make([]string, len(regionsResponse.Regions.Region))
|
|
|
|
for _, valid := range regionsResponse.Regions.Region {
|
|
|
|
validRegions = append(validRegions, valid.RegionId)
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
return validRegions, nil
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
2020-01-02 07:09:35 -05:00
|
|
|
|
|
|
|
func getConfigFromProfile(c *AlicloudAccessConfig, ProfileKey string) (interface{}, error) {
|
|
|
|
providerConfig := make(map[string]interface{})
|
|
|
|
current := c.AlicloudProfile
|
|
|
|
if current != "" {
|
|
|
|
profilePath, err := homedir.Expand(c.AlicloudSharedCredentialsFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if profilePath == "" {
|
|
|
|
profilePath = fmt.Sprintf("%s/.aliyun/config.json", os.Getenv("HOME"))
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
profilePath = fmt.Sprintf("%s/.aliyun/config.json", os.Getenv("USERPROFILE"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, err = os.Stat(profilePath)
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
data, err := ioutil.ReadFile(profilePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config := map[string]interface{}{}
|
|
|
|
err = json.Unmarshal(data, &config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, v := range config["profiles"].([]interface{}) {
|
|
|
|
if current == v.(map[string]interface{})["name"] {
|
|
|
|
providerConfig = v.(map[string]interface{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mode := ""
|
|
|
|
if v, ok := providerConfig["mode"]; ok {
|
|
|
|
mode = v.(string)
|
|
|
|
} else {
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
switch ProfileKey {
|
|
|
|
case "access_key_id", "access_key_secret":
|
|
|
|
if mode == "EcsRamRole" {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
case "ram_role_name":
|
|
|
|
if mode != "EcsRamRole" {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
case "sts_token":
|
|
|
|
if mode != "StsToken" {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
case "ram_role_arn", "ram_session_name":
|
|
|
|
if mode != "RamRoleArn" {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
case "expired_seconds":
|
|
|
|
if mode != "RamRoleArn" {
|
|
|
|
return float64(0), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return providerConfig[ProfileKey], nil
|
|
|
|
}
|