ssh test file working

This commit is contained in:
Grant Gongaware 2017-02-10 16:06:14 -08:00
parent 4c6ca729d5
commit 6195fc0705
3 changed files with 110 additions and 6 deletions

View File

@ -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
@ -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

75
proxmox/preprovision.go Normal file
View File

@ -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)
// }
// }

View File

@ -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,
},
},
}
}
@ -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
}