Merge pull request #7564 from zaenk/proxmox-builder-static-ssh-host

ProxMox builder: Use ssh_host config as host for the communicator
This commit is contained in:
Adrien Delorme 2019-05-01 11:04:04 +02:00 committed by GitHub
commit b7be4f9c74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 63 additions and 2 deletions

View File

@ -71,7 +71,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
},
&communicator.StepConnect{
Config: &b.config.Comm,
Host: getVMIP,
Host: commHost(b.config.Comm.Host()),
SSHConfig: b.config.Comm.SSHConfigFunc(),
},
&common.StepProvision{},
@ -98,6 +98,19 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
return artifact, nil
}
// Returns ssh_host or winrm_host (see communicator.Config.Host) config
// parameter when set, otherwise gets the host IP from running VM
func commHost(host string) func(state multistep.StateBag) (string, error) {
if host != "" {
return func(state multistep.StateBag) (string, error) {
return host, nil
}
}
return getVMIP
}
// Reads the first non-loopback interface's IP address from the VM.
// qemu-guest-agent package must be installed on the VM
func getVMIP(state multistep.StateBag) (string, error) {
c := state.Get("proxmoxClient").(*proxmox.Client)
vmRef := state.Get("vmRef").(*proxmox.VmRef)

View File

@ -43,6 +43,7 @@ type Config struct {
NICs []nicConfig `mapstructure:"network_adapters"`
Disks []diskConfig `mapstructure:"disks"`
ISOFile string `mapstructure:"iso_file"`
Agent bool `mapstructure:"qemu_agent"`
TemplateName string `mapstructure:"template_name"`
TemplateDescription string `mapstructure:"template_description"`
@ -68,6 +69,8 @@ type diskConfig struct {
func NewConfig(raws ...interface{}) (*Config, []string, error) {
c := new(Config)
// Agent defaults to true
c.Agent = true
var md mapstructure.Metadata
err := config.Decode(c, &config.DecodeOpts{

View File

@ -93,6 +93,7 @@ func TestBasicExampleFromDocsIsValid(t *testing.T) {
// OS not set, using default 'other'
// NIC 0 model not set, using default 'e1000'
// Disk 0 cache mode not set, using default 'none'
// Agent not set, default is true
if b.config.Memory != 512 {
t.Errorf("Expected Memory to be 512, got %d", b.config.Memory)
@ -112,4 +113,40 @@ func TestBasicExampleFromDocsIsValid(t *testing.T) {
if b.config.Disks[0].CacheMode != "none" {
t.Errorf("Expected disk cache mode to be 'none', got %s", b.config.Disks[0].CacheMode)
}
if b.config.Agent != true {
t.Errorf("Expected Agent to be true, got %t", b.config.Agent)
}
}
func TestAgentSetToFalse(t *testing.T) {
// only the mandatory attributes are specified
const config = `{
"builders": [
{
"type": "proxmox",
"proxmox_url": "https://my-proxmox.my-domain:8006/api2/json",
"username": "apiuser@pve",
"password": "supersecret",
"iso_file": "local:iso/Fedora-Server-dvd-x86_64-29-1.2.iso",
"ssh_username": "root",
"node": "my-proxmox",
"qemu_agent": false
}
]
}`
tpl, err := template.Parse(strings.NewReader(config))
if err != nil {
t.Fatal(err)
}
b := &Builder{}
warn, err := b.Prepare(tpl.Builders["proxmox"].Config)
if err != nil {
t.Fatal(err, warn)
}
if b.config.Agent != false {
t.Errorf("Expected Agent to be false, got %t", b.config.Agent)
}
}

View File

@ -21,10 +21,15 @@ func (s *stepStartVM) Run(ctx context.Context, state multistep.StateBag) multist
client := state.Get("proxmoxClient").(*proxmox.Client)
c := state.Get("config").(*Config)
agent := "1"
if c.Agent == false {
agent = "0"
}
ui.Say("Creating VM")
config := proxmox.ConfigQemu{
Name: c.VMName,
Agent: "1",
Agent: agent,
Description: "Packer ephemeral build VM",
Memory: c.Memory,
QemuCores: c.Cores,

View File

@ -146,6 +146,9 @@ builder.
- `unmount_iso` (bool) - If true, remove the mounted ISO from the template
after finishing. Defaults to `false`.
- `qemu_agent` (boolean) - Disables QEMU Agent option for this VM. When enabled,
then `qemu-guest-agent` must be installed on the guest. When disabled, then
`ssh_host` should be used. Defaults to `true`.
## Example: Fedora with kickstart