2019-05-31 08:27:41 -04:00
|
|
|
//go:generate struct-markdown
|
2019-10-14 10:43:59 -04:00
|
|
|
//go:generate mapstructure-to-hcl2 -type Config
|
2019-05-31 08:27:41 -04:00
|
|
|
|
2019-01-23 08:04:05 -05:00
|
|
|
package hyperone
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2020-03-13 13:04:48 -04:00
|
|
|
"github.com/hashicorp/packer/hcl2template"
|
2019-01-23 08:04:05 -05:00
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
|
|
|
"github.com/hashicorp/packer/helper/config"
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-11-12 17:44:02 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/common"
|
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/json"
|
2020-11-11 13:21:37 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/template/interpolate"
|
2020-11-12 17:44:02 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/uuid"
|
2019-01-23 08:04:05 -05:00
|
|
|
"github.com/mitchellh/go-homedir"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
configPath = "~/.h1-cli/conf.json"
|
|
|
|
tokenEnv = "HYPERONE_TOKEN"
|
|
|
|
|
|
|
|
defaultDiskType = "ssd"
|
|
|
|
defaultImageService = "564639bc052c084e2f2e3266"
|
|
|
|
defaultStateTimeout = 5 * time.Minute
|
|
|
|
defaultUserName = "guru"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
Comm communicator.Config `mapstructure:",squash"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Custom API endpoint URL, compatible with HyperOne.
|
2019-06-06 10:29:25 -04:00
|
|
|
// It can also be specified via environment variable HYPERONE_API_URL.
|
|
|
|
APIURL string `mapstructure:"api_url" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The authentication token used to access your account.
|
2019-06-06 10:29:25 -04:00
|
|
|
// This can be either a session token or a service account token.
|
|
|
|
// If not defined, the builder will attempt to find it in the following order:
|
|
|
|
Token string `mapstructure:"token" required:"true"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The id or name of the project. This field is required
|
2019-06-06 10:29:25 -04:00
|
|
|
// only if using session tokens. It should be skipped when using service
|
|
|
|
// account authentication.
|
|
|
|
Project string `mapstructure:"project" required:"true"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Login (an e-mail) on HyperOne platform. Set this
|
2019-06-06 10:29:25 -04:00
|
|
|
// if you want to fetch the token by SSH authentication.
|
2019-05-28 11:50:58 -04:00
|
|
|
TokenLogin string `mapstructure:"token_login" required:"false"`
|
|
|
|
// Timeout for waiting on the API to complete
|
2019-06-06 10:29:25 -04:00
|
|
|
// a request. Defaults to 5m.
|
2019-05-28 11:50:58 -04:00
|
|
|
StateTimeout time.Duration `mapstructure:"state_timeout" required:"false"`
|
|
|
|
// ID or name of the image to launch server from.
|
2019-06-06 10:29:25 -04:00
|
|
|
SourceImage string `mapstructure:"source_image" required:"true"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The name of the resulting image. Defaults to
|
2020-03-23 20:02:12 -04:00
|
|
|
// `packer-{{timestamp}}`
|
2019-06-06 10:29:25 -04:00
|
|
|
// (see configuration templates for more info).
|
|
|
|
ImageName string `mapstructure:"image_name" 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"`
|
2020-03-13 13:04:48 -04:00
|
|
|
// Key/value pair tags to add to the created image.
|
2019-12-03 08:18:49 -05:00
|
|
|
ImageTags map[string]string `mapstructure:"image_tags" required:"false"`
|
2020-03-16 09:55:03 -04:00
|
|
|
// Same as [`image_tags`](#image_tags) but defined as a singular repeatable
|
2020-04-16 09:37:22 -04:00
|
|
|
// block containing a `key` and a `value` field. In HCL2 mode the
|
2020-04-01 18:54:21 -04:00
|
|
|
// [`dynamic_block`](/docs/configuration/from-1.5/expressions#dynamic-blocks)
|
2020-03-13 13:04:48 -04:00
|
|
|
// will allow you to create those programatically.
|
2020-04-16 09:37:22 -04:00
|
|
|
ImageTag hcl2template.KeyValues `mapstructure:"image_tag" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The service of the resulting image.
|
2019-06-06 10:29:25 -04:00
|
|
|
ImageService string `mapstructure:"image_service" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// ID or name of the type this server should be created with.
|
2019-06-06 10:29:25 -04:00
|
|
|
VmType string `mapstructure:"vm_type" required:"true"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The name of the created server.
|
2019-06-06 10:29:25 -04:00
|
|
|
VmName string `mapstructure:"vm_name" required:"false"`
|
2020-04-16 09:37:22 -04:00
|
|
|
// Key/value pair tags to add to the created server.
|
2019-12-03 08:18:49 -05:00
|
|
|
VmTags map[string]string `mapstructure:"vm_tags" required:"false"`
|
2020-03-16 09:55:03 -04:00
|
|
|
// Same as [`vm_tags`](#vm_tags) but defined as a singular repeatable block
|
2020-04-16 09:37:22 -04:00
|
|
|
// containing a `key` and a `value` field. In HCL2 mode the
|
2020-04-01 18:54:21 -04:00
|
|
|
// [`dynamic_block`](/docs/configuration/from-1.5/expressions#dynamic-blocks)
|
2020-03-13 13:04:48 -04:00
|
|
|
// will allow you to create those programatically.
|
2020-03-17 06:14:32 -04:00
|
|
|
VmTag hcl2template.NameValues `mapstructure:"vm_tag" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The name of the created disk.
|
2019-06-06 10:29:25 -04:00
|
|
|
DiskName string `mapstructure:"disk_name" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The type of the created disk. Defaults to ssd.
|
2019-06-06 10:29:25 -04:00
|
|
|
DiskType string `mapstructure:"disk_type" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Size of the created disk, in GiB.
|
|
|
|
DiskSize float32 `mapstructure:"disk_size" required:"true"`
|
|
|
|
// The ID of the network to attach to the created server.
|
2019-06-06 10:29:25 -04:00
|
|
|
Network string `mapstructure:"network" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The ID of the private IP within chosen network
|
2019-06-06 10:29:25 -04:00
|
|
|
// that should be assigned to the created server.
|
|
|
|
PrivateIP string `mapstructure:"private_ip" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The ID of the public IP that should be assigned to
|
2019-06-06 10:29:25 -04:00
|
|
|
// the created server. If network is chosen, the public IP will be associated
|
|
|
|
// with server's private IP.
|
|
|
|
PublicIP string `mapstructure:"public_ip" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Custom service of public network adapter.
|
2019-06-06 10:29:25 -04:00
|
|
|
// Can be useful when using custom api_url. Defaults to public.
|
2019-05-28 11:50:58 -04:00
|
|
|
PublicNetAdpService string `mapstructure:"public_netadp_service" required:"false"`
|
2019-01-23 08:04:05 -05:00
|
|
|
|
2020-06-26 08:50:39 -04:00
|
|
|
ChrootDevice string `mapstructure:"chroot_device"`
|
2020-02-20 05:05:27 -05:00
|
|
|
ChrootDisk bool `mapstructure:"chroot_disk"`
|
|
|
|
ChrootDiskSize float32 `mapstructure:"chroot_disk_size"`
|
|
|
|
ChrootDiskType string `mapstructure:"chroot_disk_type"`
|
|
|
|
ChrootMountPath string `mapstructure:"chroot_mount_path"`
|
|
|
|
ChrootMounts [][]string `mapstructure:"chroot_mounts"`
|
|
|
|
ChrootCopyFiles []string `mapstructure:"chroot_copy_files"`
|
2020-03-23 20:02:12 -04:00
|
|
|
// How to run shell commands. This defaults to `{{.Command}}`. This may be
|
2020-02-20 05:05:27 -05:00
|
|
|
// useful to set if you want to set environmental variables or perhaps run
|
|
|
|
// it with sudo or so on. This is a configuration template where the
|
|
|
|
// .Command variable is replaced with the command to be run. Defaults to
|
2020-03-23 20:02:12 -04:00
|
|
|
// `{{.Command}}`.
|
2020-02-20 05:05:27 -05:00
|
|
|
ChrootCommandWrapper string `mapstructure:"chroot_command_wrapper"`
|
2019-01-28 06:30:15 -05:00
|
|
|
|
2020-02-20 05:05:27 -05:00
|
|
|
MountOptions []string `mapstructure:"mount_options"`
|
|
|
|
MountPartition string `mapstructure:"mount_partition"`
|
|
|
|
// A series of commands to execute after attaching the root volume and
|
|
|
|
// before mounting the chroot. This is not required unless using
|
|
|
|
// from_scratch. If so, this should include any partitioning and filesystem
|
2020-03-23 20:02:12 -04:00
|
|
|
// creation commands. The path to the device is provided by `{{.Device}}`.
|
2020-02-20 05:05:27 -05:00
|
|
|
PreMountCommands []string `mapstructure:"pre_mount_commands"`
|
|
|
|
// As pre_mount_commands, but the commands are executed after mounting the
|
|
|
|
// root device and before the extra mount and copy steps. The device and
|
2020-03-23 20:02:12 -04:00
|
|
|
// mount path are provided by `{{.Device}}` and `{{.MountPath}}`.
|
2019-01-28 06:30:15 -05:00
|
|
|
PostMountCommands []string `mapstructure:"post_mount_commands"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// List of SSH keys by name or id to be added
|
2019-06-06 10:29:25 -04:00
|
|
|
// to the server on launch.
|
|
|
|
SSHKeys []string `mapstructure:"ssh_keys" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// User data to launch with the server. Packer will not
|
2019-06-06 10:29:25 -04:00
|
|
|
// automatically wait for a user script to finish before shutting down the
|
|
|
|
// instance, this must be handled in a provisioner.
|
|
|
|
UserData string `mapstructure:"user_data" required:"false"`
|
2019-01-23 08:04:05 -05:00
|
|
|
|
|
|
|
ctx interpolate.Context
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
|
2019-01-23 08:04:05 -05:00
|
|
|
|
|
|
|
var md mapstructure.Metadata
|
|
|
|
err := config.Decode(c, &config.DecodeOpts{
|
|
|
|
Metadata: &md,
|
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &c.ctx,
|
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{
|
|
|
|
"run_command",
|
2019-01-28 06:30:15 -05:00
|
|
|
"chroot_command_wrapper",
|
|
|
|
"post_mount_commands",
|
|
|
|
"pre_mount_commands",
|
|
|
|
"mount_path",
|
2019-01-23 08:04:05 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, raws...)
|
|
|
|
if err != nil {
|
2019-12-17 05:25:56 -05:00
|
|
|
return nil, err
|
2019-01-23 08:04:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
cliConfig, err := loadCLIConfig()
|
|
|
|
if err != nil {
|
2019-12-17 05:25:56 -05:00
|
|
|
return nil, err
|
2019-01-23 08:04:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Defaults
|
|
|
|
if c.Comm.SSHUsername == "" {
|
|
|
|
c.Comm.SSHUsername = defaultUserName
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Comm.SSHTimeout == 0 {
|
|
|
|
c.Comm.SSHTimeout = 10 * time.Minute
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:03:44 -05:00
|
|
|
if c.APIURL == "" {
|
|
|
|
c.APIURL = os.Getenv("HYPERONE_API_URL")
|
|
|
|
}
|
|
|
|
|
2019-01-23 08:04:05 -05:00
|
|
|
if c.Token == "" {
|
|
|
|
c.Token = os.Getenv(tokenEnv)
|
|
|
|
|
|
|
|
if c.Token == "" {
|
|
|
|
c.Token = cliConfig.Profile.APIKey
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:03:44 -05:00
|
|
|
// Fetching token by SSH is available only for the default API endpoint
|
|
|
|
if c.TokenLogin != "" && c.APIURL == "" {
|
2019-01-23 08:04:05 -05:00
|
|
|
c.Token, err = fetchTokenBySSH(c.TokenLogin)
|
|
|
|
if err != nil {
|
2019-12-17 05:25:56 -05:00
|
|
|
return nil, err
|
2019-01-23 08:04:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Project == "" {
|
|
|
|
c.Project = cliConfig.Profile.Project.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.StateTimeout == 0 {
|
|
|
|
c.StateTimeout = defaultStateTimeout
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ImageName == "" {
|
|
|
|
name, err := interpolate.Render("packer-{{timestamp}}", nil)
|
|
|
|
if err != nil {
|
2019-12-17 05:25:56 -05:00
|
|
|
return nil, err
|
2019-01-23 08:04:05 -05:00
|
|
|
}
|
|
|
|
c.ImageName = name
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ImageService == "" {
|
|
|
|
c.ImageService = defaultImageService
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.VmName == "" {
|
|
|
|
c.VmName = fmt.Sprintf("packer-%s", uuid.TimeOrderedUUID())
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.DiskType == "" {
|
|
|
|
c.DiskType = defaultDiskType
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:03:44 -05:00
|
|
|
if c.PublicNetAdpService == "" {
|
|
|
|
c.PublicNetAdpService = "public"
|
|
|
|
}
|
|
|
|
|
2019-01-28 06:30:15 -05:00
|
|
|
if c.ChrootCommandWrapper == "" {
|
|
|
|
c.ChrootCommandWrapper = "{{.Command}}"
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ChrootDiskSize == 0 {
|
|
|
|
c.ChrootDiskSize = c.DiskSize
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ChrootDiskType == "" {
|
|
|
|
c.ChrootDiskType = c.DiskType
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ChrootMountPath == "" {
|
2019-02-01 11:46:35 -05:00
|
|
|
path, err := interpolate.Render("/mnt/packer-hyperone-volumes/{{timestamp}}", nil)
|
|
|
|
if err != nil {
|
2019-12-17 05:25:56 -05:00
|
|
|
return nil, err
|
2019-02-01 11:46:35 -05:00
|
|
|
}
|
|
|
|
c.ChrootMountPath = path
|
2019-01-28 06:30:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.ChrootMounts == nil {
|
|
|
|
c.ChrootMounts = make([][]string, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.ChrootMounts) == 0 {
|
|
|
|
c.ChrootMounts = [][]string{
|
|
|
|
{"proc", "proc", "/proc"},
|
|
|
|
{"sysfs", "sysfs", "/sys"},
|
|
|
|
{"bind", "/dev", "/dev"},
|
|
|
|
{"devpts", "devpts", "/dev/pts"},
|
|
|
|
{"binfmt_misc", "binfmt_misc", "/proc/sys/fs/binfmt_misc"},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ChrootCopyFiles == nil {
|
|
|
|
c.ChrootCopyFiles = []string{"/etc/resolv.conf"}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.MountPartition == "" {
|
|
|
|
c.MountPartition = "1"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validation
|
2019-01-23 08:04:05 -05:00
|
|
|
var errs *packer.MultiError
|
2020-03-16 10:46:08 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.ImageTag.CopyOn(&c.ImageTags)...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, c.VmTag.CopyOn(&c.VmTags)...)
|
2020-03-16 07:32:57 -04:00
|
|
|
|
2019-01-23 08:04:05 -05:00
|
|
|
if es := c.Comm.Prepare(&c.ctx); len(es) > 0 {
|
|
|
|
errs = packer.MultiErrorAppend(errs, es...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Token == "" {
|
|
|
|
errs = packer.MultiErrorAppend(errs, errors.New("token is required"))
|
|
|
|
}
|
|
|
|
|
2019-02-01 12:57:43 -05:00
|
|
|
if c.VmType == "" {
|
|
|
|
errs = packer.MultiErrorAppend(errs, errors.New("vm type is required"))
|
2019-01-23 08:04:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.DiskSize == 0 {
|
|
|
|
errs = packer.MultiErrorAppend(errs, errors.New("disk size is required"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.SourceImage == "" {
|
|
|
|
errs = packer.MultiErrorAppend(errs, errors.New("source image is required"))
|
|
|
|
}
|
|
|
|
|
2019-01-28 06:30:15 -05:00
|
|
|
if c.ChrootDisk {
|
|
|
|
if len(c.PreMountCommands) == 0 {
|
|
|
|
errs = packer.MultiErrorAppend(errs, errors.New("pre-mount commands are required for chroot disk"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, mounts := range c.ChrootMounts {
|
|
|
|
if len(mounts) != 3 {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("each chroot_mounts entry should have three elements"))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 08:04:05 -05:00
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
2019-12-17 05:25:56 -05:00
|
|
|
return nil, errs
|
2019-01-23 08:04:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
packer.LogSecretFilter.Set(c.Token)
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
return nil, nil
|
2019-01-23 08:04:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type cliConfig struct {
|
|
|
|
Profile struct {
|
|
|
|
APIKey string `json:"apiKey"`
|
|
|
|
Project struct {
|
2019-12-03 08:19:30 -05:00
|
|
|
ID string `json:"id"`
|
2019-01-23 08:04:05 -05:00
|
|
|
} `json:"project"`
|
|
|
|
} `json:"profile"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadCLIConfig() (cliConfig, error) {
|
|
|
|
path, err := homedir.Expand(configPath)
|
|
|
|
if err != nil {
|
|
|
|
return cliConfig{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
// Config not found
|
|
|
|
return cliConfig{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
content, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return cliConfig{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var c cliConfig
|
|
|
|
err = json.Unmarshal(content, &c)
|
|
|
|
if err != nil {
|
|
|
|
return cliConfig{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPublicIP(state multistep.StateBag) (string, error) {
|
|
|
|
return state.Get("public_ip").(string), nil
|
|
|
|
}
|