Skip to content

Commit

Permalink
untested cloud-init support
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant Gongaware committed Jul 13, 2018
1 parent 821ceb8 commit 83722fe
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 29 deletions.
61 changes: 57 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,45 @@ provider "proxmox" {
pm_tls_insecure = true
}
resource "proxmox_vm_qemu" "test" {
/* Uses cloud-init options from Proxmox 5.2 */
resource "proxmox_vm_qemu" "cloudinit-test" {
name = "tftest1.xyz.com"
desc = "tf description"
target_node = "proxmox1-xx"
clone = "ci-ubuntu-template"
storage = "local"
cores = 3
sockets = 1
memory = 2560
disk_gb = 4
nic = "virtio"
bridge = "vmbr0"
ssh_user = "root"
ssh_private_key = <<EOF
-----BEGIN RSA PRIVATE KEY-----
private ssh key root
-----END RSA PRIVATE KEY-----
EOF
os_type = "cloud-init"
ipconfig0 = "ip=10.0.2.99, gw=10.0.2.2"
sshkeys = <<EOF
ssh-rsa AAAAB3NzaC1kj...key1
ssh-rsa AAAAB3NzaC1kj...key2
EOF
provisioner "remote-exec" {
inline = [
"ip a"
]
}
}
/* Uses custom eth1 user-net SSH portforward */
resource "proxmox_vm_qemu" "prepprovision-test" {
name = "tftest1.xyz.com"
desc = "tf description"
target_node = "proxmox1-xx"
Expand Down Expand Up @@ -80,12 +118,27 @@ Optimally, you could create a VM resource you will use a clone base with an ISO,

Interesting parameters:

**ssh_forward_ip** - should be the IP or hostname of the target node or bridge IP. This is where proxmox will create a port forward to your VM with via a user_net.
**os_type** -
* cloud-init - from Proxmox 5.2
* ubuntu -(https://github.com/Telmate/terraform-ubuntu-proxmox-iso)
* centos - (TODO: centos iso template)

**ssh_forward_ip** - should be the IP or hostname of the target node or bridge IP. This is where proxmox will create a port forward to your VM with via a user_net. (for pre-cloud-init provisioning)

### Cloud-Init

**os_type** - ubuntu (https://github.com/Telmate/terraform-ubuntu-proxmox-iso) or centos (TODO: centos iso template)
Cloud-init VMs must be cloned from a cloud-init ready template.
See: https://pve.proxmox.com/wiki/Cloud-Init_Support

* ciuser - User name to change ssh keys and password for instead of the image’s configured default user.
* cipassword - Password to assign the user.
* searchdomain - Sets DNS search domains for a container.
* nameserver - Sets DNS server IP address for a container.
* sshkeys - public ssh keys, one per line
* ipconfig0 - [gw=<GatewayIPv4>] [,gw6=<GatewayIPv6>] [,ip=<IPv4Format/CIDR>] [,ip6=<IPv6Format/CIDR>]
* ipconfig1 - optional, same as ipconfig0 format

### Preprovision (internal)
### Preprovision (internal alternative to Cloud-Init)

There is a pre-provision phase which is used to set a hostname, intialize eth0, and resize the VM disk to available space. This is done over SSH with the ssh_forward_ip, ssh_user and ssh_private_key.

Expand Down
9 changes: 5 additions & 4 deletions proxmox/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ type providerConfiguration struct {
Client *pxapi.Client
MaxParallel int
CurrentParallel int
MaxVmId int
MaxVMID int
Mutex *sync.Mutex
Cond *sync.Cond
}

// Provider - Terrafrom properties for proxmox
func Provider() *schema.Provider {
return &schema.Provider{

Expand Down Expand Up @@ -76,7 +77,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
Client: client,
MaxParallel: d.Get("pm_parallel").(int),
CurrentParallel: 0,
MaxVmId: -1,
MaxVMID: -1,
Mutex: &mut,
Cond: sync.NewCond(&mut),
}, nil
Expand All @@ -97,11 +98,11 @@ func getClient(pm_api_url string, pm_user string, pm_password string, pm_tls_ins

func nextVmId(pconf *providerConfiguration) (nextId int, err error) {
pconf.Mutex.Lock()
pconf.MaxVmId, err = pconf.Client.GetNextID(pconf.MaxVmId + 1)
pconf.MaxVMID, err = pconf.Client.GetNextID(pconf.MaxVMID + 1)
if err != nil {
return 0, err
}
nextId = pconf.MaxVmId
nextId = pconf.MaxVMID
pconf.Mutex.Unlock()
return nextId, nil
}
Expand Down
8 changes: 4 additions & 4 deletions proxmox/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/terraform/terraform"
)

// Provisioner - Terrafrom properties for proxmox-provisioner
func Provisioner() terraform.ResourceProvisioner {
return &schema.Provisioner{
Schema: map[string]*schema.Schema{
Expand All @@ -27,7 +28,7 @@ func Provisioner() terraform.ResourceProvisioner {
}
}

var currentClient *pxapi.Client = nil
var currentClient *pxapi.Client

func applyFn(ctx context.Context) error {
data := ctx.Value(schema.ProvConfigDataKey).(*schema.ResourceData)
Expand All @@ -36,11 +37,11 @@ func applyFn(ctx context.Context) error {
connInfo := state.Ephemeral.ConnInfo

act := data.Get("action").(string)
targetNode, _, vmId, err := parseResourceId(state.ID)
targetNode, _, vmID, err := parseResourceId(state.ID)
if err != nil {
return err
}
vmr := pxapi.NewVmRef(vmId)
vmr := pxapi.NewVmRef(vmID)
vmr.SetNode(targetNode)
client := currentClient
if client == nil {
Expand Down Expand Up @@ -69,5 +70,4 @@ func applyFn(ctx context.Context) error {
default:
return fmt.Errorf("Unkown action: %s", act)
}
return nil
}
Loading

0 comments on commit 83722fe

Please sign in to comment.