2019-03-26 08:29:15 -04:00
|
|
|
package yandex
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/packer/common"
|
|
|
|
"github.com/hashicorp/packer/common/uuid"
|
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
|
|
|
"github.com/hashicorp/packer/helper/config"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
|
|
|
|
|
|
|
"github.com/yandex-cloud/go-sdk/iamkey"
|
|
|
|
)
|
|
|
|
|
2019-04-04 09:17:51 -04:00
|
|
|
const defaultEndpoint = "api.cloud.yandex.net:443"
|
|
|
|
const defaultZone = "ru-central1-a"
|
|
|
|
|
2019-03-26 08:29:15 -04:00
|
|
|
var reImageFamily = regexp.MustCompile(`^[a-z]([-a-z0-9]{0,61}[a-z0-9])?$`)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
Communicator communicator.Config `mapstructure:",squash"`
|
|
|
|
|
|
|
|
Endpoint string `mapstructure:"endpoint"`
|
|
|
|
FolderID string `mapstructure:"folder_id"`
|
2019-04-09 10:46:41 -04:00
|
|
|
ServiceAccountKeyFile string `mapstructure:"service_account_key_file"`
|
|
|
|
Token string `mapstructure:"token"`
|
2019-03-26 08:29:15 -04:00
|
|
|
|
2019-04-09 10:46:41 -04:00
|
|
|
DiskName string `mapstructure:"disk_name"`
|
2019-03-26 08:29:15 -04:00
|
|
|
DiskSizeGb int `mapstructure:"disk_size_gb"`
|
|
|
|
DiskType string `mapstructure:"disk_type"`
|
|
|
|
ImageDescription string `mapstructure:"image_description"`
|
2019-04-09 10:46:41 -04:00
|
|
|
ImageFamily string `mapstructure:"image_family"`
|
2019-03-26 08:29:15 -04:00
|
|
|
ImageLabels map[string]string `mapstructure:"image_labels"`
|
2019-04-09 10:46:41 -04:00
|
|
|
ImageName string `mapstructure:"image_name"`
|
2019-03-26 08:29:15 -04:00
|
|
|
ImageProductIDs []string `mapstructure:"image_product_ids"`
|
2019-04-09 10:46:41 -04:00
|
|
|
InstanceCores int `mapstructure:"instance_cores"`
|
|
|
|
InstanceMemory int `mapstructure:"instance_mem_gb"`
|
2019-03-26 08:29:15 -04:00
|
|
|
InstanceName string `mapstructure:"instance_name"`
|
|
|
|
Labels map[string]string `mapstructure:"labels"`
|
2019-04-09 10:46:41 -04:00
|
|
|
PlatformID string `mapstructure:"platform_id"`
|
2019-03-26 08:29:15 -04:00
|
|
|
Metadata map[string]string `mapstructure:"metadata"`
|
2019-04-09 10:46:41 -04:00
|
|
|
SerialLogFile string `mapstructure:"serial_log_file"`
|
2019-03-26 08:29:15 -04:00
|
|
|
SourceImageFamily string `mapstructure:"source_image_family"`
|
|
|
|
SourceImageFolderID string `mapstructure:"source_image_folder_id"`
|
2019-04-09 10:46:41 -04:00
|
|
|
SourceImageID string `mapstructure:"source_image_id"`
|
|
|
|
SubnetID string `mapstructure:"subnet_id"`
|
2019-03-26 08:29:15 -04:00
|
|
|
UseIPv4Nat bool `mapstructure:"use_ipv4_nat"`
|
|
|
|
UseIPv6 bool `mapstructure:"use_ipv6"`
|
2019-04-09 10:46:41 -04:00
|
|
|
UseInternalIP bool `mapstructure:"use_internal_ip"`
|
|
|
|
Zone string `mapstructure:"zone"`
|
2019-03-26 08:29:15 -04:00
|
|
|
|
2019-04-09 10:46:41 -04:00
|
|
|
ctx interpolate.Context
|
2019-04-04 09:17:51 -04:00
|
|
|
StateTimeout time.Duration `mapstructure:"state_timeout"`
|
2019-03-26 08:29:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewConfig(raws ...interface{}) (*Config, []string, error) {
|
|
|
|
c := &Config{}
|
|
|
|
c.ctx.Funcs = TemplateFuncs
|
|
|
|
err := config.Decode(c, &config.DecodeOpts{
|
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &c.ctx,
|
|
|
|
}, raws...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var errs *packer.MultiError
|
|
|
|
|
|
|
|
if c.SerialLogFile != "" {
|
|
|
|
if _, err := os.Stat(c.SerialLogFile); os.IsExist(err) {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("Serial log file %s already exist", c.SerialLogFile))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.InstanceCores == 0 {
|
|
|
|
c.InstanceCores = 2
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.InstanceMemory == 0 {
|
|
|
|
c.InstanceMemory = 4
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.DiskSizeGb == 0 {
|
|
|
|
c.DiskSizeGb = 10
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.DiskType == "" {
|
|
|
|
c.DiskType = "network-hdd"
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ImageDescription == "" {
|
|
|
|
c.ImageDescription = "Created by Packer"
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ImageName == "" {
|
|
|
|
img, err := interpolate.Render("packer-{{timestamp}}", nil)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("Unable to render default image name: %s ", err))
|
|
|
|
} else {
|
|
|
|
c.ImageName = img
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.ImageFamily) > 63 {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
errors.New("Invalid image family: Must not be longer than 63 characters"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ImageFamily != "" {
|
|
|
|
if !reImageFamily.MatchString(c.ImageFamily) {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
errors.New("Invalid image family: The first character must be a "+
|
|
|
|
"lowercase letter, and all following characters must be a dash, "+
|
|
|
|
"lowercase letter, or digit, except the last character, which cannot be a dash"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.InstanceName == "" {
|
|
|
|
c.InstanceName = fmt.Sprintf("packer-%s", uuid.TimeOrderedUUID())
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.DiskName == "" {
|
|
|
|
c.DiskName = c.InstanceName + "-disk"
|
|
|
|
}
|
|
|
|
|
2019-04-09 10:46:41 -04:00
|
|
|
if c.PlatformID == "" {
|
|
|
|
c.PlatformID = "standard-v1"
|
2019-03-26 08:29:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if es := c.Communicator.Prepare(&c.ctx); len(es) > 0 {
|
|
|
|
errs = packer.MultiErrorAppend(errs, es...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process required parameters.
|
|
|
|
if c.SourceImageID == "" && c.SourceImageFamily == "" {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("a source_image_id or source_image_family must be specified"))
|
|
|
|
}
|
|
|
|
|
2019-04-04 09:17:51 -04:00
|
|
|
if c.Endpoint == "" {
|
|
|
|
c.Endpoint = defaultEndpoint
|
2019-03-26 08:29:15 -04:00
|
|
|
}
|
|
|
|
|
2019-04-04 09:17:51 -04:00
|
|
|
if c.Zone == "" {
|
|
|
|
c.Zone = defaultZone
|
|
|
|
}
|
|
|
|
|
|
|
|
// provision config by OS environment variables
|
|
|
|
if c.Token == "" {
|
|
|
|
c.Token = os.Getenv("YC_TOKEN")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ServiceAccountKeyFile == "" {
|
|
|
|
c.ServiceAccountKeyFile = os.Getenv("YC_SERVICE_ACCOUNT_KEY_FILE")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.FolderID == "" {
|
|
|
|
c.FolderID = os.Getenv("YC_FOLDER_ID")
|
2019-03-26 08:29:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.Token == "" && c.ServiceAccountKeyFile == "" {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("a token or service account key file must be specified"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Token != "" && c.ServiceAccountKeyFile != "" {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("one of token or service account key file must be specified, not both"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Token != "" {
|
|
|
|
packer.LogSecretFilter.Set(c.Token)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ServiceAccountKeyFile != "" {
|
|
|
|
if _, err := iamkey.ReadFromJSONFile(c.ServiceAccountKeyFile); err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
2019-04-04 09:17:51 -04:00
|
|
|
errs, fmt.Errorf("fail to read service account key file: %s", err))
|
2019-03-26 08:29:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.FolderID == "" {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("a folder_id must be specified"))
|
|
|
|
}
|
|
|
|
|
2019-04-04 09:17:51 -04:00
|
|
|
if c.StateTimeout == 0 {
|
|
|
|
c.StateTimeout = 5 * time.Minute
|
2019-03-26 08:29:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for any errors.
|
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
|
|
|
return nil, nil, errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil, nil
|
|
|
|
}
|