2019-09-25 07:27:24 -04:00
|
|
|
//go:generate struct-markdown
|
2019-10-14 10:43:59 -04:00
|
|
|
//go:generate mapstructure-to-hcl2 -type Config
|
|
|
|
|
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"
|
2019-09-10 10:52:42 -04:00
|
|
|
const defaultGpuPlatformID = "gpu-standard-v1"
|
|
|
|
const defaultPlatformID = "standard-v1"
|
2019-09-23 14:03:17 -04:00
|
|
|
const defaultMaxRetries = 3
|
2019-04-04 09:17:51 -04:00
|
|
|
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"`
|
|
|
|
|
2019-05-28 11:50:58 -04:00
|
|
|
// Non standard api endpoint URL.
|
2019-06-06 10:29:25 -04:00
|
|
|
Endpoint string `mapstructure:"endpoint" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The folder ID that will be used to launch instances and store images.
|
2019-06-06 10:29:25 -04:00
|
|
|
// Alternatively you may set value by environment variable YC_FOLDER_ID.
|
|
|
|
FolderID string `mapstructure:"folder_id" required:"true"`
|
|
|
|
// Path to file with Service Account key in json format. This
|
|
|
|
// is an alternative method to authenticate to Yandex.Cloud. Alternatively you may set environment variable
|
|
|
|
// YC_SERVICE_ACCOUNT_KEY_FILE.
|
2019-05-28 11:50:58 -04:00
|
|
|
ServiceAccountKeyFile string `mapstructure:"service_account_key_file" required:"false"`
|
|
|
|
// OAuth token to use to authenticate to Yandex.Cloud. Alternatively you may set
|
2019-06-06 10:29:25 -04:00
|
|
|
// value by environment variable YC_TOKEN.
|
|
|
|
Token string `mapstructure:"token" required:"true"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The name of the disk, if unset the instance name
|
2019-06-06 10:29:25 -04:00
|
|
|
// will be used.
|
|
|
|
DiskName string `mapstructure:"disk_name" required:"false"`
|
2019-09-25 09:03:18 -04:00
|
|
|
// The size of the disk in GB. This defaults to `10`, which is 10GB.
|
2019-06-06 10:29:25 -04:00
|
|
|
DiskSizeGb int `mapstructure:"disk_size_gb" required:"false"`
|
2019-09-25 09:03:18 -04:00
|
|
|
// Specify disk type for the launched instance. Defaults to `network-hdd`.
|
2019-06-06 10:29:25 -04:00
|
|
|
DiskType string `mapstructure:"disk_type" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The description of the resulting image.
|
2019-06-06 10:29:25 -04:00
|
|
|
ImageDescription string `mapstructure:"image_description" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The family name of the resulting image.
|
2019-06-06 10:29:25 -04:00
|
|
|
ImageFamily string `mapstructure:"image_family" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Key/value pair labels to
|
2019-06-06 10:29:25 -04:00
|
|
|
// apply to the created image.
|
|
|
|
ImageLabels map[string]string `mapstructure:"image_labels" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The unique name of the resulting image. Defaults to
|
2019-09-25 09:03:18 -04:00
|
|
|
// `packer-{{timestamp}}`.
|
2019-06-06 10:29:25 -04:00
|
|
|
ImageName string `mapstructure:"image_name" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// License IDs that indicate which licenses are attached to resulting image.
|
2019-06-06 10:29:25 -04:00
|
|
|
ImageProductIDs []string `mapstructure:"image_product_ids" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The number of cores available to the instance.
|
2019-06-06 10:29:25 -04:00
|
|
|
InstanceCores int `mapstructure:"instance_cores" required:"false"`
|
2019-09-25 07:27:24 -04:00
|
|
|
// The number of GPU available to the instance.
|
2019-09-20 04:54:38 -04:00
|
|
|
InstanceGpus int `mapstructure:"instance_gpus"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The amount of memory available to the instance, specified in gigabytes.
|
2019-06-06 10:29:25 -04:00
|
|
|
InstanceMemory int `mapstructure:"instance_mem_gb" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The name assigned to the instance.
|
2019-06-06 10:29:25 -04:00
|
|
|
InstanceName string `mapstructure:"instance_name" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Key/value pair labels to apply to
|
2019-06-06 10:29:25 -04:00
|
|
|
// the launched instance.
|
|
|
|
Labels map[string]string `mapstructure:"labels" required:"false"`
|
2019-09-25 09:03:18 -04:00
|
|
|
// Identifier of the hardware platform configuration for the instance. This defaults to `standard-v1`.
|
2019-06-06 10:29:25 -04:00
|
|
|
PlatformID string `mapstructure:"platform_id" required:"false"`
|
2019-09-23 14:03:17 -04:00
|
|
|
// The maximum number of times an API request is being executed
|
|
|
|
MaxRetries int `mapstructure:"max_retries"`
|
2019-06-12 08:28:11 -04:00
|
|
|
// Metadata applied to the launched instance.
|
2019-06-06 10:29:25 -04:00
|
|
|
Metadata map[string]string `mapstructure:"metadata" required:"false"`
|
2019-06-12 08:28:11 -04:00
|
|
|
// Metadata applied to the launched instance. Value are file paths.
|
|
|
|
MetadataFromFile map[string]string `mapstructure:"metadata_from_file"`
|
|
|
|
// Launch a preemptible instance. This defaults to `false`.
|
|
|
|
Preemptible bool `mapstructure:"preemptible"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// File path to save serial port output of the launched instance.
|
2019-06-06 10:29:25 -04:00
|
|
|
SerialLogFile string `mapstructure:"serial_log_file" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The source image family to create the new image
|
2019-06-06 10:29:25 -04:00
|
|
|
// from. You can also specify source_image_id instead. Just one of a source_image_id or
|
2019-09-25 09:03:18 -04:00
|
|
|
// source_image_family must be specified. Example: `ubuntu-1804-lts`
|
2019-06-06 10:29:25 -04:00
|
|
|
SourceImageFamily string `mapstructure:"source_image_family" required:"true"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The ID of the folder containing the source image.
|
2019-06-06 10:29:25 -04:00
|
|
|
SourceImageFolderID string `mapstructure:"source_image_folder_id" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The source image ID to use to create the new image
|
2019-06-06 10:29:25 -04:00
|
|
|
// from.
|
|
|
|
SourceImageID string `mapstructure:"source_image_id" required:"false"`
|
2019-09-25 07:27:24 -04:00
|
|
|
// The source image name to use to create the new image
|
|
|
|
// from. Name will be looked up in `source_image_folder_id`.
|
2019-09-20 04:54:38 -04:00
|
|
|
SourceImageName string `mapstructure:"source_image_name"`
|
2019-06-06 10:29:25 -04:00
|
|
|
// The Yandex VPC subnet id to use for
|
|
|
|
// the launched instance. Note, the zone of the subnet must match the
|
|
|
|
// zone in which the VM is launched.
|
|
|
|
SubnetID string `mapstructure:"subnet_id" required:"false"`
|
|
|
|
// If set to true, then launched instance will have external internet
|
|
|
|
// access.
|
|
|
|
UseIPv4Nat bool `mapstructure:"use_ipv4_nat" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Set to true to enable IPv6 for the instance being
|
2019-09-25 09:03:18 -04:00
|
|
|
// created. This defaults to `false`, or not enabled.
|
|
|
|
//
|
|
|
|
// -> **Note**: Usage of IPv6 will be available in the future.
|
2019-06-06 10:29:25 -04:00
|
|
|
UseIPv6 bool `mapstructure:"use_ipv6" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// If true, use the instance's internal IP address
|
2019-06-06 10:29:25 -04:00
|
|
|
// instead of its external IP during building.
|
|
|
|
UseInternalIP bool `mapstructure:"use_internal_ip" required:"false"`
|
2019-09-25 09:03:18 -04:00
|
|
|
// The name of the zone to launch the instance. This defaults to `ru-central1-a`.
|
2019-06-06 10:29:25 -04:00
|
|
|
Zone string `mapstructure:"zone" required:"false"`
|
2019-03-26 08:29:15 -04:00
|
|
|
|
2019-06-06 10:29:25 -04:00
|
|
|
ctx interpolate.Context
|
2019-05-28 11:50:58 -04:00
|
|
|
// The time to wait for instance state changes.
|
2019-09-25 09:03:18 -04:00
|
|
|
// Defaults to `5m`.
|
2019-05-28 11:50:58 -04:00
|
|
|
StateTimeout time.Duration `mapstructure:"state_timeout" required:"false"`
|
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 == "" {
|
2019-09-10 10:52:42 -04:00
|
|
|
c.PlatformID = defaultPlatformID
|
|
|
|
if c.InstanceGpus != 0 {
|
|
|
|
c.PlatformID = defaultGpuPlatformID
|
|
|
|
}
|
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.
|
2019-09-10 10:52:42 -04:00
|
|
|
if c.SourceImageID == "" {
|
|
|
|
if c.SourceImageFamily == "" && c.SourceImageName == "" {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("a source_image_name or source_image_family must be specified"))
|
|
|
|
}
|
|
|
|
if c.SourceImageFamily != "" && c.SourceImageName != "" {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("one of source_image_name or source_image_family must be specified, not both"))
|
|
|
|
}
|
2019-03-26 08:29:15 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-09-23 14:03:17 -04:00
|
|
|
if c.MaxRetries == 0 {
|
|
|
|
c.MaxRetries = defaultMaxRetries
|
|
|
|
}
|
|
|
|
|
2019-04-04 09:17:51 -04:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2019-09-10 10:52:42 -04:00
|
|
|
if c.PlatformID != defaultGpuPlatformID && c.InstanceGpus != 0 {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("for instances with gpu platform_id must be specified as `%s`", defaultGpuPlatformID))
|
|
|
|
}
|
|
|
|
|
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-06-06 09:41:58 -04:00
|
|
|
for key, file := range c.MetadataFromFile {
|
|
|
|
if _, err := os.Stat(file); err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("cannot access file '%s' with content for value of metadata key '%s': %s", file, key, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|