Skip to content

Commit

Permalink
pre provision worked!
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant Gongaware committed Feb 13, 2017
1 parent 6195fc0 commit 776fee5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
48 changes: 47 additions & 1 deletion proxmox/preprovision.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,40 @@ import (
// "github.com/hashicorp/terraform/terraform"
// "github.com/mitchellh/go-linereader"
"io"
"strconv"
"strings"
)

const eth0Payload = "echo $'%s' > /tmp/tf_eth0_payload"
const provisionPayload = "echo $'%s' > /tmp/tf_preprovision.sh"

// preprovision VM (setup eth0 and hostname)
const ubuntuPreprovisionScript = `
BOX_HOSTNAME=%s
BOX_SHORT_HOSTNAME=%s
MY_IP=$(echo $SSH_CLIENT | awk "{ print \$1 }")
echo Using my ip $MY_IP to provision at $(date)
if [ -z "$(grep $BOX_SHORT_HOSTNAME /etc/hosts)" ]; then
echo 127.0.1.1 $BOX_HOSTNAME $BOX_SHORT_HOSTNAME >> /etc/hosts
else
echo Hosts file already set includes $BOX_SHORT_HOSTNAME
fi
echo $BOX_SHORT_HOSTNAME > /etc/hostname
hostname $BOX_SHORT_HOSTNAME
echo Hostname set $BOX_SHORT_HOSTNAME
if [ -z "$(grep eth0 /etc/network/interfaces)" ]; then
echo Setting up eth0 for $BOX_HOSTNAME
cat /tmp/tf_eth0_payload >> /etc/network/interfaces
else
echo eth0 already setup for $BOX_HOSTNAME
fi
echo Attempting to bring up eth0
ip route add $MY_IP via 10.0.2.2
ip route del default via 10.0.2.2
ifup eth0
echo Preprovision done at $(date)
`

func preProvisionUbuntu(d *schema.ResourceData) error {

Expand All @@ -20,7 +51,22 @@ func preProvisionUbuntu(d *schema.ResourceData) error {
return err
}

err = runCommand(comm, "echo cool > /tmp/test")
err = runCommand(comm, fmt.Sprintf(eth0Payload, strings.Trim(strconv.Quote(d.Get("os_network_config").(string)), "\"")))
if err != nil {
return err
}

hostname := d.Get("name").(string)
pScript := fmt.Sprintf(ubuntuPreprovisionScript, hostname, strings.Split(hostname, ".")[0])
err = runCommand(comm, fmt.Sprintf(provisionPayload, strings.Trim(strconv.Quote(pScript), "\"")))
if err != nil {
return err
}

err = runCommand(comm, "sudo bash /tmp/tf_preprovision.sh >> /tmp/tf_preprovision.log 2>&1")
if err != nil {
return err
}

comm.Disconnect()

Expand Down
3 changes: 3 additions & 0 deletions proxmox/resource_vm_qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
pxapi "github.com/Telmate/proxmox-api-go/proxmox"
"github.com/hashicorp/terraform/helper/schema"
"strconv"
"time"
)

func resourceVmQemu() *schema.Resource {
Expand Down Expand Up @@ -171,6 +172,8 @@ func resourceVmQemuCreate(d *schema.ResourceData, meta interface{}) error {
switch d.Get("os_type").(string) {

case "ubuntu":
// give sometime to bootup
time.Sleep(5 * time.Second)
err = preProvisionUbuntu(d)
if err != nil {
return err
Expand Down

0 comments on commit 776fee5

Please sign in to comment.