2021-02-10 04:59:44 -05:00
//go:generate mapstructure-to-hcl2 -type Config,nicConfig,diskConfig,vgaConfig,additionalISOsConfig
2019-10-14 10:43:59 -04:00
2019-04-11 12:52:21 -04:00
package proxmox
import (
"errors"
"fmt"
"log"
"net/url"
"os"
2021-02-10 04:59:44 -05:00
"strconv"
2020-02-26 02:44:27 -05:00
"strings"
2019-04-11 12:52:21 -04:00
"time"
2020-12-17 16:29:25 -05:00
"github.com/hashicorp/packer-plugin-sdk/bootcommand"
"github.com/hashicorp/packer-plugin-sdk/common"
"github.com/hashicorp/packer-plugin-sdk/communicator"
"github.com/hashicorp/packer-plugin-sdk/multistep/commonsteps"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-sdk/template/config"
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
"github.com/hashicorp/packer-plugin-sdk/uuid"
2019-04-11 12:52:21 -04:00
"github.com/mitchellh/mapstructure"
)
type Config struct {
common . PackerConfig ` mapstructure:",squash" `
2020-11-11 18:04:28 -05:00
commonsteps . HTTPConfig ` mapstructure:",squash" `
2019-04-11 12:52:21 -04:00
bootcommand . BootConfig ` mapstructure:",squash" `
2019-10-31 10:49:34 -04:00
BootKeyInterval time . Duration ` mapstructure:"boot_key_interval" `
2019-04-11 12:52:21 -04:00
Comm communicator . Config ` mapstructure:",squash" `
ProxmoxURLRaw string ` mapstructure:"proxmox_url" `
2019-10-14 10:10:35 -04:00
proxmoxURL * url . URL
2019-04-11 12:52:21 -04:00
SkipCertValidation bool ` mapstructure:"insecure_skip_tls_verify" `
Username string ` mapstructure:"username" `
Password string ` mapstructure:"password" `
Node string ` mapstructure:"node" `
2019-07-10 16:04:10 -04:00
Pool string ` mapstructure:"pool" `
2019-04-11 12:52:21 -04:00
VMName string ` mapstructure:"vm_name" `
VMID int ` mapstructure:"vm_id" `
2020-11-14 01:27:55 -05:00
Boot string ` mapstructure:"boot" `
2019-10-06 05:14:04 -04:00
Memory int ` mapstructure:"memory" `
Cores int ` mapstructure:"cores" `
2019-10-08 04:45:15 -04:00
CPUType string ` mapstructure:"cpu_type" `
2019-10-06 05:14:04 -04:00
Sockets int ` mapstructure:"sockets" `
OS string ` mapstructure:"os" `
2020-03-13 21:22:47 -04:00
VGA vgaConfig ` mapstructure:"vga" `
2019-10-06 05:14:04 -04:00
NICs [ ] nicConfig ` mapstructure:"network_adapters" `
Disks [ ] diskConfig ` mapstructure:"disks" `
Agent bool ` mapstructure:"qemu_agent" `
SCSIController string ` mapstructure:"scsi_controller" `
2020-03-22 20:20:02 -04:00
Onboot bool ` mapstructure:"onboot" `
2020-07-15 13:47:24 -04:00
DisableKVM bool ` mapstructure:"disable_kvm" `
2019-04-11 12:52:21 -04:00
TemplateName string ` mapstructure:"template_name" `
TemplateDescription string ` mapstructure:"template_description" `
2020-04-10 17:19:33 -04:00
CloudInit bool ` mapstructure:"cloud_init" `
CloudInitStoragePool string ` mapstructure:"cloud_init_storage_pool" `
2021-02-10 04:59:44 -05:00
AdditionalISOFiles [ ] additionalISOsConfig ` mapstructure:"additional_iso_files" `
VMInterface string ` mapstructure:"vm_interface" `
2020-08-31 04:48:24 -04:00
2020-10-06 05:54:04 -04:00
Ctx interpolate . Context ` mapstructure-to-hcl2:",skip" `
2019-04-11 12:52:21 -04:00
}
2021-02-10 04:59:44 -05:00
type additionalISOsConfig struct {
2020-11-11 18:04:28 -05:00
commonsteps . ISOConfig ` mapstructure:",squash" `
Device string ` mapstructure:"device" `
ISOFile string ` mapstructure:"iso_file" `
ISOStoragePool string ` mapstructure:"iso_storage_pool" `
Unmount bool ` mapstructure:"unmount" `
ShouldUploadISO bool
DownloadPathKey string
2020-10-06 05:42:49 -04:00
}
2019-04-11 12:52:21 -04:00
type nicConfig struct {
2020-07-15 18:07:02 -04:00
Model string ` mapstructure:"model" `
PacketQueues int ` mapstructure:"packet_queues" `
MACAddress string ` mapstructure:"mac_address" `
Bridge string ` mapstructure:"bridge" `
VLANTag string ` mapstructure:"vlan_tag" `
Firewall bool ` mapstructure:"firewall" `
2019-04-11 12:52:21 -04:00
}
type diskConfig struct {
Type string ` mapstructure:"type" `
StoragePool string ` mapstructure:"storage_pool" `
StoragePoolType string ` mapstructure:"storage_pool_type" `
Size string ` mapstructure:"disk_size" `
CacheMode string ` mapstructure:"cache_mode" `
DiskFormat string ` mapstructure:"format" `
2020-09-25 08:59:54 -04:00
IOThread bool ` mapstructure:"io_thread" `
2019-04-11 12:52:21 -04:00
}
2020-03-13 21:22:47 -04:00
type vgaConfig struct {
Type string ` mapstructure:"type" `
Memory int ` mapstructure:"memory" `
}
2019-04-11 12:52:21 -04:00
2020-09-04 17:53:09 -04:00
func ( c * Config ) Prepare ( upper interface { } , raws ... interface { } ) ( [ ] string , [ ] string , error ) {
2019-04-30 14:23:34 -04:00
// Agent defaults to true
c . Agent = true
2020-04-10 17:19:33 -04:00
// Do not add a cloud-init cdrom by default
c . CloudInit = false
2019-04-11 12:52:21 -04:00
var md mapstructure . Metadata
2020-09-04 17:53:09 -04:00
err := config . Decode ( upper , & config . DecodeOpts {
2019-04-11 12:52:21 -04:00
Metadata : & md ,
Interpolate : true ,
2020-09-04 17:53:09 -04:00
InterpolateContext : & c . Ctx ,
2019-04-11 12:52:21 -04:00
InterpolateFilter : & interpolate . RenderFilter {
Exclude : [ ] string {
"boot_command" ,
} ,
} ,
} , raws ... )
if err != nil {
2020-09-04 17:53:09 -04:00
return nil , nil , err
2019-04-11 12:52:21 -04:00
}
2020-11-19 15:07:02 -05:00
var errs * packersdk . MultiError
2020-09-04 17:53:09 -04:00
var warnings [ ] string
2020-11-19 17:03:11 -05:00
packersdk . LogSecretFilter . Set ( c . Password )
2019-04-11 12:52:21 -04:00
// Defaults
if c . ProxmoxURLRaw == "" {
c . ProxmoxURLRaw = os . Getenv ( "PROXMOX_URL" )
}
if c . Username == "" {
c . Username = os . Getenv ( "PROXMOX_USERNAME" )
}
if c . Password == "" {
c . Password = os . Getenv ( "PROXMOX_PASSWORD" )
}
2020-11-11 18:04:28 -05:00
if c . BootKeyInterval == 0 && os . Getenv ( bootcommand . PackerKeyEnv ) != "" {
2019-10-31 10:49:34 -04:00
var err error
2020-11-11 18:04:28 -05:00
c . BootKeyInterval , err = time . ParseDuration ( os . Getenv ( bootcommand . PackerKeyEnv ) )
2019-10-31 10:49:34 -04:00
if err != nil {
2020-11-19 15:07:02 -05:00
errs = packersdk . MultiErrorAppend ( errs , err )
2019-10-31 10:49:34 -04:00
}
2019-04-11 12:52:21 -04:00
}
2019-10-31 10:49:34 -04:00
if c . BootKeyInterval == 0 {
2019-09-09 16:33:48 -04:00
c . BootKeyInterval = 5 * time . Millisecond
2019-04-11 12:52:21 -04:00
}
if c . VMName == "" {
// Default to packer-[time-ordered-uuid]
c . VMName = fmt . Sprintf ( "packer-%s" , uuid . TimeOrderedUUID ( ) )
}
if c . Memory < 16 {
log . Printf ( "Memory %d is too small, using default: 512" , c . Memory )
c . Memory = 512
}
if c . Cores < 1 {
log . Printf ( "Number of cores %d is too small, using default: 1" , c . Cores )
c . Cores = 1
}
if c . Sockets < 1 {
log . Printf ( "Number of sockets %d is too small, using default: 1" , c . Sockets )
c . Sockets = 1
}
2019-10-06 06:39:53 -04:00
if c . CPUType == "" {
log . Printf ( "CPU type not set, using default 'kvm64'" )
c . CPUType = "kvm64"
}
2019-04-11 12:52:21 -04:00
if c . OS == "" {
log . Printf ( "OS not set, using default 'other'" )
c . OS = "other"
}
for idx := range c . NICs {
if c . NICs [ idx ] . Model == "" {
log . Printf ( "NIC %d model not set, using default 'e1000'" , idx )
c . NICs [ idx ] . Model = "e1000"
}
}
for idx := range c . Disks {
if c . Disks [ idx ] . Type == "" {
log . Printf ( "Disk %d type not set, using default 'scsi'" , idx )
c . Disks [ idx ] . Type = "scsi"
}
if c . Disks [ idx ] . Size == "" {
log . Printf ( "Disk %d size not set, using default '20G'" , idx )
c . Disks [ idx ] . Size = "20G"
}
if c . Disks [ idx ] . CacheMode == "" {
log . Printf ( "Disk %d cache mode not set, using default 'none'" , idx )
c . Disks [ idx ] . CacheMode = "none"
}
2020-09-25 08:59:54 -04:00
if c . Disks [ idx ] . IOThread {
// io thread is only supported by virtio-scsi-single controller
if c . SCSIController != "virtio-scsi-single" {
2020-11-19 15:07:02 -05:00
errs = packersdk . MultiErrorAppend ( errs , fmt . Errorf ( "io thread option requires virtio-scsi-single controller" ) )
2020-09-25 08:59:54 -04:00
} else {
// ... and only for virtio and scsi disks
if ! ( c . Disks [ idx ] . Type == "scsi" || c . Disks [ idx ] . Type == "virtio" ) {
2020-11-19 15:07:02 -05:00
errs = packersdk . MultiErrorAppend ( errs , fmt . Errorf ( "io thread option requires scsi or a virtio disk" ) )
2020-09-25 08:59:54 -04:00
}
}
}
2020-06-15 08:00:32 -04:00
// For any storage pool types which aren't in rxStorageTypes in proxmox-api/proxmox/config_qemu.go:890
// (currently zfspool|lvm|rbd|cephfs), the format parameter is mandatory. Make sure this is still up to date
2019-09-08 12:40:29 -04:00
// when updating the vendored code!
2020-06-15 08:00:32 -04:00
if ! contains ( [ ] string { "zfspool" , "lvm" , "rbd" , "cephfs" } , c . Disks [ idx ] . StoragePoolType ) && c . Disks [ idx ] . DiskFormat == "" {
2020-11-19 15:07:02 -05:00
errs = packersdk . MultiErrorAppend ( errs , fmt . Errorf ( "disk format must be specified for pool type %q" , c . Disks [ idx ] . StoragePoolType ) )
2019-09-08 12:40:29 -04:00
}
2019-04-11 12:52:21 -04:00
}
2019-10-06 05:14:04 -04:00
if c . SCSIController == "" {
log . Printf ( "SCSI controller not set, using default 'lsi'" )
c . SCSIController = "lsi"
}
2019-04-11 12:52:21 -04:00
2020-11-19 15:07:02 -05:00
errs = packersdk . MultiErrorAppend ( errs , c . Comm . Prepare ( & c . Ctx ) ... )
errs = packersdk . MultiErrorAppend ( errs , c . BootConfig . Prepare ( & c . Ctx ) ... )
errs = packersdk . MultiErrorAppend ( errs , c . HTTPConfig . Prepare ( & c . Ctx ) ... )
2020-01-04 16:50:35 -05:00
2019-04-11 12:52:21 -04:00
// Required configurations that will display errors if not set
if c . Username == "" {
2020-11-19 15:07:02 -05:00
errs = packersdk . MultiErrorAppend ( errs , errors . New ( "username must be specified" ) )
2019-04-11 12:52:21 -04:00
}
if c . Password == "" {
2020-11-19 15:07:02 -05:00
errs = packersdk . MultiErrorAppend ( errs , errors . New ( "password must be specified" ) )
2019-04-11 12:52:21 -04:00
}
if c . ProxmoxURLRaw == "" {
2020-11-19 15:07:02 -05:00
errs = packersdk . MultiErrorAppend ( errs , errors . New ( "proxmox_url must be specified" ) )
2019-04-11 12:52:21 -04:00
}
2019-10-14 10:10:35 -04:00
if c . proxmoxURL , err = url . Parse ( c . ProxmoxURLRaw ) ; err != nil {
2020-11-19 15:07:02 -05:00
errs = packersdk . MultiErrorAppend ( errs , fmt . Errorf ( "Could not parse proxmox_url: %s" , err ) )
2019-04-11 12:52:21 -04:00
}
if c . Node == "" {
2020-11-19 15:07:02 -05:00
errs = packersdk . MultiErrorAppend ( errs , errors . New ( "node must be specified" ) )
2019-04-11 12:52:21 -04:00
}
2020-02-26 02:44:27 -05:00
if strings . ContainsAny ( c . TemplateName , " " ) {
2020-11-19 15:07:02 -05:00
errs = packersdk . MultiErrorAppend ( errs , errors . New ( "template_name must not contain spaces" ) )
2020-02-26 02:44:27 -05:00
}
2019-04-11 12:52:21 -04:00
for idx := range c . NICs {
if c . NICs [ idx ] . Bridge == "" {
2020-11-19 15:07:02 -05:00
errs = packersdk . MultiErrorAppend ( errs , fmt . Errorf ( "network_adapters[%d].bridge must be specified" , idx ) )
2019-04-11 12:52:21 -04:00
}
2020-07-15 18:07:02 -04:00
if c . NICs [ idx ] . Model != "virtio" && c . NICs [ idx ] . PacketQueues > 0 {
2020-11-19 15:07:02 -05:00
errs = packersdk . MultiErrorAppend ( errs , fmt . Errorf ( "network_adapters[%d].packet_queues can only be set for 'virtio' driver" , idx ) )
2019-04-11 12:52:21 -04:00
}
}
for idx := range c . Disks {
if c . Disks [ idx ] . StoragePool == "" {
2020-11-19 15:07:02 -05:00
errs = packersdk . MultiErrorAppend ( errs , fmt . Errorf ( "disks[%d].storage_pool must be specified" , idx ) )
2019-04-11 12:52:21 -04:00
}
if c . Disks [ idx ] . StoragePoolType == "" {
2020-11-19 15:07:02 -05:00
errs = packersdk . MultiErrorAppend ( errs , fmt . Errorf ( "disks[%d].storage_pool_type must be specified" , idx ) )
2019-04-11 12:52:21 -04:00
}
}
2021-02-10 04:59:44 -05:00
for idx := range c . AdditionalISOFiles {
// Check AdditionalISO config
// Either a pre-uploaded ISO should be referenced in iso_file, OR a URL
// (possibly to a local file) to an ISO file that will be downloaded and
// then uploaded to Proxmox.
if c . AdditionalISOFiles [ idx ] . ISOFile != "" {
c . AdditionalISOFiles [ idx ] . ShouldUploadISO = false
} else {
c . AdditionalISOFiles [ idx ] . DownloadPathKey = "downloaded_additional_iso_path_" + strconv . Itoa ( idx )
isoWarnings , isoErrors := c . AdditionalISOFiles [ idx ] . ISOConfig . Prepare ( & c . Ctx )
errs = packersdk . MultiErrorAppend ( errs , isoErrors ... )
warnings = append ( warnings , isoWarnings ... )
c . AdditionalISOFiles [ idx ] . ShouldUploadISO = true
}
if c . AdditionalISOFiles [ idx ] . Device == "" {
log . Printf ( "AdditionalISOFile %d Device not set, using default 'ide3'" , idx )
c . AdditionalISOFiles [ idx ] . Device = "ide3"
}
if strings . HasPrefix ( c . AdditionalISOFiles [ idx ] . Device , "ide" ) {
busnumber , err := strconv . Atoi ( c . AdditionalISOFiles [ idx ] . Device [ 3 : ] )
if err != nil {
errs = packersdk . MultiErrorAppend ( errs , fmt . Errorf ( "%s is not a valid bus index" , c . AdditionalISOFiles [ idx ] . Device [ 3 : ] ) )
}
if busnumber == 2 {
errs = packersdk . MultiErrorAppend ( errs , fmt . Errorf ( "IDE bus 2 is used by boot ISO" ) )
}
if busnumber > 3 {
errs = packersdk . MultiErrorAppend ( errs , fmt . Errorf ( "IDE bus index can't be higher than 3" ) )
}
}
if strings . HasPrefix ( c . AdditionalISOFiles [ idx ] . Device , "sata" ) {
busnumber , err := strconv . Atoi ( c . AdditionalISOFiles [ idx ] . Device [ 4 : ] )
if err != nil {
errs = packersdk . MultiErrorAppend ( errs , fmt . Errorf ( "%s is not a valid bus index" , c . AdditionalISOFiles [ idx ] . Device [ 4 : ] ) )
}
if busnumber > 5 {
errs = packersdk . MultiErrorAppend ( errs , fmt . Errorf ( "SATA bus index can't be higher than 5" ) )
}
}
if strings . HasPrefix ( c . AdditionalISOFiles [ idx ] . Device , "scsi" ) {
busnumber , err := strconv . Atoi ( c . AdditionalISOFiles [ idx ] . Device [ 4 : ] )
if err != nil {
errs = packersdk . MultiErrorAppend ( errs , fmt . Errorf ( "%s is not a valid bus index" , c . AdditionalISOFiles [ idx ] . Device [ 4 : ] ) )
}
if busnumber > 30 {
errs = packersdk . MultiErrorAppend ( errs , fmt . Errorf ( "SCSI bus index can't be higher than 30" ) )
}
}
if ( c . AdditionalISOFiles [ idx ] . ISOFile == "" && len ( c . AdditionalISOFiles [ idx ] . ISOConfig . ISOUrls ) == 0 ) || ( c . AdditionalISOFiles [ idx ] . ISOFile != "" && len ( c . AdditionalISOFiles [ idx ] . ISOConfig . ISOUrls ) != 0 ) {
errs = packersdk . MultiErrorAppend ( errs , fmt . Errorf ( "either iso_file or iso_url, but not both, must be specified for AdditionalISO file %s" , c . AdditionalISOFiles [ idx ] . Device ) )
}
}
2019-04-11 12:52:21 -04:00
if errs != nil && len ( errs . Errors ) > 0 {
2020-09-04 17:53:09 -04:00
return nil , warnings , errs
2019-04-11 12:52:21 -04:00
}
2020-09-04 17:53:09 -04:00
return nil , warnings , nil
2019-04-11 12:52:21 -04:00
}
2019-09-08 12:40:29 -04:00
func contains ( haystack [ ] string , needle string ) bool {
for _ , candidate := range haystack {
if candidate == needle {
return true
}
}
return false
}