support disk resize after clone

This commit is contained in:
Grant Gongaware 2017-02-13 13:09:55 -08:00
parent 776fee509c
commit 00f195faf6
2 changed files with 40 additions and 2 deletions

View File

@ -8,6 +8,7 @@ import (
// "github.com/hashicorp/terraform/terraform"
// "github.com/mitchellh/go-linereader"
"io"
"log"
"strconv"
"strings"
)
@ -46,11 +47,13 @@ echo Preprovision done at $(date)
func preProvisionUbuntu(d *schema.ResourceData) error {
// Get a new communicator
log.Print("[DEBUG] connecting to SSH on ubuntu")
comm, err := communicator.New(d.State())
if err != nil {
return err
}
log.Print("[DEBUG] sending os_network_config")
err = runCommand(comm, fmt.Sprintf(eth0Payload, strings.Trim(strconv.Quote(d.Get("os_network_config").(string)), "\"")))
if err != nil {
return err
@ -58,16 +61,20 @@ func preProvisionUbuntu(d *schema.ResourceData) error {
hostname := d.Get("name").(string)
pScript := fmt.Sprintf(ubuntuPreprovisionScript, hostname, strings.Split(hostname, ".")[0])
log.Print("[DEBUG] sending provisionPayload")
err = runCommand(comm, fmt.Sprintf(provisionPayload, strings.Trim(strconv.Quote(pScript), "\"")))
if err != nil {
return err
}
log.Print("[DEBUG] running provisionPayload")
err = runCommand(comm, "sudo bash /tmp/tf_preprovision.sh >> /tmp/tf_preprovision.log 2>&1")
if err != nil {
return err
}
log.Print("[DEBUG] disconnecting SSH")
comm.Disconnect()
return err

View File

@ -4,6 +4,7 @@ import (
"fmt"
pxapi "github.com/Telmate/proxmox-api-go/proxmox"
"github.com/hashicorp/terraform/helper/schema"
"log"
"strconv"
"time"
)
@ -105,6 +106,7 @@ func resourceVmQemu() *schema.Resource {
func resourceVmQemuCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*providerConfiguration).Client
vmName := d.Get("name").(string)
disk_gb := d.Get("disk_gb").(float64)
config := pxapi.ConfigQemu{
Name: vmName,
Description: d.Get("desc").(string),
@ -112,12 +114,13 @@ func resourceVmQemuCreate(d *schema.ResourceData, meta interface{}) error {
Memory: d.Get("memory").(int),
QemuCores: d.Get("cores").(int),
QemuSockets: d.Get("sockets").(int),
DiskSize: d.Get("disk_gb").(float64),
DiskSize: disk_gb,
QemuOs: d.Get("qemu_os").(string),
QemuNicModel: d.Get("nic").(string),
QemuBrige: d.Get("bridge").(string),
QemuVlanTag: d.Get("vlan").(int),
}
log.Print("[DEBUG] checking for duplicate name")
dupVmr, _ := client.GetVmRefByName(vmName)
if dupVmr != nil {
return fmt.Errorf("Duplicate VM name (%s) with vmId: %d", vmName, dupVmr.VmId())
@ -137,11 +140,21 @@ func resourceVmQemuCreate(d *schema.ResourceData, meta interface{}) error {
if err != nil {
return err
}
log.Print("[DEBUG] cloning VM")
err = config.CloneVm(sourceVmr, vmr, client)
if err != nil {
return err
}
// TODO - resize disk
clonedConfig, err := pxapi.NewConfigQemuFromApi(vmr, client)
if disk_gb > clonedConfig.DiskSize {
log.Print("[DEBUG] resizing disk")
_, err = client.ResizeQemuDisk(vmr, "virtio0", int(disk_gb-clonedConfig.DiskSize))
if err != nil {
return err
}
}
} else if d.Get("iso").(string) != "" {
config.QemuIso = d.Get("iso").(string)
err := config.CreateVm(vmr, client)
@ -152,10 +165,12 @@ func resourceVmQemuCreate(d *schema.ResourceData, meta interface{}) error {
d.SetId(strconv.Itoa(vmr.VmId()))
log.Print("[DEBUG] starting VM")
_, err = client.StartVm(vmr)
if err != nil {
return err
}
log.Print("[DEBUG] setting up SSH forward")
sshPort, err := pxapi.SshForwardUsernet(vmr, client)
if err != nil {
return err
@ -186,6 +201,22 @@ func resourceVmQemuCreate(d *schema.ResourceData, meta interface{}) error {
}
func resourceVmQemuUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*providerConfiguration).Client
vmr, err := client.GetVmRefByName(d.Get("name").(string))
if err != nil {
return err
}
sshPort, err := pxapi.SshForwardUsernet(vmr, client)
if err != nil {
return err
}
d.SetConnInfo(map[string]string{
"type": "ssh",
"host": d.Get("ssh_forward_ip").(string),
"port": sshPort,
"user": d.Get("ssh_user").(string),
"private_key": d.Get("ssh_private_key").(string),
})
return nil
}