Skip to content

Commit

Permalink
ssh test file working
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant Gongaware committed Feb 11, 2017
1 parent 4c6ca72 commit 6195fc0
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 6 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ resource "proxmox_vm_qemu" "test" {
name = "tftest1.xyz.com"
desc = "tf description"
target_node = "proxmox1-xx"
ssh_forward_ip = "10.0.0.1"
clone = "terraform-ubuntu1404-template"
storage = "local"
cores = 3
Expand All @@ -45,7 +45,15 @@ resource "proxmox_vm_qemu" "test" {
disk_gb = 4
nic = "virtio"
bridge = "vmbr1"
os_type = "ubnutu"
ssh_forward_ip = "10.0.0.1"
ssh_user = "terraform"
ssh_private_key = <<EOF
-----BEGIN RSA PRIVATE KEY-----
private ssh key terraform
-----END RSA PRIVATE KEY-----
EOF
os_type = "ubuntu"
os_network_config = <<EOF
auto eth0
iface eth0 inet dhcp
Expand Down
75 changes: 75 additions & 0 deletions proxmox/preprovision.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package proxmox

import (
"fmt"
"github.com/hashicorp/terraform/communicator"
"github.com/hashicorp/terraform/communicator/remote"
"github.com/hashicorp/terraform/helper/schema"
// "github.com/hashicorp/terraform/terraform"
// "github.com/mitchellh/go-linereader"
"io"
)

// preprovision VM (setup eth0 and hostname)

func preProvisionUbuntu(d *schema.ResourceData) error {

// Get a new communicator
comm, err := communicator.New(d.State())
if err != nil {
return err
}

err = runCommand(comm, "echo cool > /tmp/test")

comm.Disconnect()

return err
}

// runCommand is used to run already prepared commands
func runCommand(
comm communicator.Communicator,
command string) error {

_, outW := io.Pipe()
_, errW := io.Pipe()
//outDoneCh := make(chan struct{})
//errDoneCh := make(chan struct{})
// go copyOutput(o, outR, outDoneCh)
// go copyOutput(o, errR, errDoneCh)

cmd := &remote.Cmd{
Command: command,
Stdout: outW,
Stderr: errW,
}

err := comm.Start(cmd)
if err != nil {
return fmt.Errorf("Error executing command %q: %v", cmd.Command, err)
}

cmd.Wait()
if cmd.ExitStatus != 0 {
err = fmt.Errorf(
"Command %q exited with non-zero exit status: %d", cmd.Command, cmd.ExitStatus)
}

// Wait for output to clean up
outW.Close()
errW.Close()
//<-outDoneCh
//<-errDoneCh

return err
}

//
// func copyOutput(o terraform.UIOutput, r io.Reader, doneCh chan<- struct{}) {
// defer close(doneCh)
// lr := linereader.New(r)
// for line := range lr.Ch {
// o.Output(line)
// }
// }
29 changes: 25 additions & 4 deletions proxmox/resource_vm_qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ func resourceVmQemu() *schema.Resource {
Optional: true,
ForceNew: true,
},
"ssh_user": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"ssh_private_key": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
}
}
Expand Down Expand Up @@ -151,13 +161,24 @@ func resourceVmQemuCreate(d *schema.ResourceData, meta interface{}) error {
}

d.SetConnInfo(map[string]string{
"type": "ssh",
"host": d.Get("ssh_forward_ip").(string),
"port": sshPort,
"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),
})

// TODO - preprovision VM (setup eth0 and hostname)
switch d.Get("os_type").(string) {

case "ubuntu":
err = preProvisionUbuntu(d)
if err != nil {
return err
}

default:
return fmt.Errorf("Unknown os_type: %s", d.Get("os_type").(string))
}
return nil
}

Expand Down

0 comments on commit 6195fc0

Please sign in to comment.