From 8cf68cf676e6dd62e187a87ae6c4bbd5420e77df Mon Sep 17 00:00:00 2001 From: Jonathan Senkerik Date: Sat, 11 Aug 2018 07:28:07 -0400 Subject: [PATCH] 1.0.0, init release Former-commit-id: 4156a7d1859867b9f78feeea7eadc22ab1b322e2 --- README.md | 88 +++++++++++++++++++--------------- esxi/esxi.main.go | 9 ---- esxi/guest_create.go | 51 -------------------- esxi/guest_delete.go | 22 --------- esxi/resource_pool-read.go | 98 -------------------------------------- main.go | 2 +- version | 1 + 7 files changed, 52 insertions(+), 219 deletions(-) delete mode 100644 esxi/esxi.main.go delete mode 100644 esxi/guest_create.go delete mode 100644 esxi/guest_delete.go delete mode 100644 esxi/resource_pool-read.go create mode 100644 version diff --git a/README.md b/README.md index aae91fe..475e2d7 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,56 @@ +Terraform Provider +================== + +- Website: https://www.terraform.io +- [![Gitter chat](https://badges.gitter.im/hashicorp-terraform/Lobby.png)](https://gitter.im/hashicorp-terraform/Lobby) +- Mailing list: [Google Groups](http://groups.google.com/group/terraform-tool) + + +Requirements +------------ +- [Terraform](https://www.terraform.io/downloads.html) 0.10.1+ +- [Go](https://golang.org/doc/install) 1.9 (to build the provider plugin) +- [ovftool](https://www.vmware.com/support/developer/ovf/) from VMware. NOTE: ovftool installer for windows doesn't put ovftool.exe in your path. You can manually set your path, or install ovftool in the \HashiCorp\Vagrant\bin directory. +- You MUST enable ssh access on your ESXi hypervisor. + * Google 'How to enable ssh access on esxi' +- In general, you should know how to use terraform, esxi and some networking... + + +Building The Provider +--------------------- + +Clone repository to: `$GOPATH/src/github.com/terraform-providers/terraform-provider-esxi` + +```sh + +export GOPATH="/usr/lib" +go get -u golang.org/x/crypto/ssh + +mkdir -p $GOPATH/src/github.com/hashicorp +cd $GOPATH/src/github.com/hashicorp +git clone https://github.com/hashicorp/terraform.git + +mkdir -p $GOPATH/src/github.com/terraform-providers +cd $GOPATH/src/github.com/terraform-providers +git clone https://github.com/josenk/terraform-provider-esxi.git + +cd $GOPATH/src/github.com/terraform-providers/terraform-provider-esxi +go build -o terraform-provider-esxi_`cat version` + +cp terraform-provider-esxi_`cat version` /usr/local/bin +``` + Terraform-provider-esxi plugin ============================== This is a Terraform plugin that adds a VMware ESXi provider support. This allows Terraform to control and provision VMs directly on an ESXi hypervisor without a need for vCenter or VShpere. ESXi hypervisor is a free download from VMware! >https://www.vmware.com/go/get-free-esxi + What's New: ----------- * Terraform can read existing Guest VMs & Resource pools by name. (infra to code) -Documentation: -------------- -* More Documentation is needed. Some error messages are limited. I'm adding features and updating documentation as time permits... - * If you don't know terraform, I highly recommend you read through the introduction on the hashicorp website. >https://www.terraform.io/intro/getting-started/install.html @@ -27,6 +66,7 @@ Features and Compatibility * Terraform will Create, Destroy, Update & Read Guest VMs. * Terraform will Create, Destroy, Update & Read Extra Storage for Guests. + Requirements ------------ 1. This is a Terraform plugin, so you need Terraform installed... :-) @@ -36,10 +76,12 @@ Requirements * Google 'How to enable ssh access on esxi' 4. In general, you should know how to use terraform, esxi and some networking... + Why this plugin? ---------------- Not everyone has vCenter, vSphere, expensive APIs... These cost $$$. ESXi is free! + How to install -------------- Download and install Terraform on your local system using instructions from https://www.terraform.io/downloads.html. @@ -48,7 +90,6 @@ Download this plugin from github and place a copy of it in your path or current How to use and configure a main.tf file --------------------------------------- - 1. cd SOMEDIR 2. `vi main.tf` # Use the contents of this example main.tf as a template. Specify provider parameters to access your ESXi host. Modify the resources for resource pools and guest vm. @@ -60,47 +101,19 @@ provider "esxi" { esxi_password = "MyPassword" } -resource "esxi_resource_pool" "MyPool" { - resource_pool_name = "MyPool" - cpu_min = "100" - mem_min = "200" -} - -resource "esxi_virtual_disk" "vdisk1" { - virtual_disk_disk_store = "MyDiskStore" - virtual_disk_dir = "Terraform" - virtual_disk_size = 6 - virtual_disk_type = "thin" -} - resource "esxi_guest" "vmtest" { - depends_on = ["esxi_resource_pool_name.MyPool"] - guest_name = "v-test" + guest_name = "vmtest" + disk_store = "MyDiskStore" # - # Specify an existing guest to clone, an ovf source, or neither to build a guest vm from scratch + # Specify an existing guest to clone, an ovf source, or neither to build a bare-metal guest vm. # #clone_from_vm = "Templates/centos7" - #ovf_source = "/my_local_system_path/centos-7-min/centos-7.vmx" + #ovf_source = "/local_path/centos-7.vmx" - disk_store = "MyDiskStore" - resource_pool_name = "MyPool" network_interfaces = [ { virtual_network = "VM Network" - mac_address = "00:50:56:a1:b1:c1" - nic_type = "e1000" - }, - { - virtual_network = "VM Network 2" - nic_type = "e1000" - }, - ] - - virtual_disks = [ - { - virtual_disk_id = "${esxi_virtual_disk.vdisk1.id}" - slot = "0:1" }, ] } @@ -171,7 +184,6 @@ Configuration reference Known issues with vmware_esxi ----------------------------- * More features coming. -* I need help documenting. Version History diff --git a/esxi/esxi.main.go b/esxi/esxi.main.go deleted file mode 100644 index b8064be..0000000 --- a/esxi/esxi.main.go +++ /dev/null @@ -1,9 +0,0 @@ -package esxi - - -type SshConnectionInfo struct { - host string - port string - user string - pass string -} diff --git a/esxi/guest_create.go b/esxi/guest_create.go deleted file mode 100644 index bd5c29a..0000000 --- a/esxi/guest_create.go +++ /dev/null @@ -1,51 +0,0 @@ -package esxi - -import ( - "bytes" - "fmt" - "log" - "os/exec" - "strings" -) - - -func GuestCreate(esxi_hostname string, esxi_hostport string, esxi_username string, encoded_esxi_password string, - guest_name string, esxi_disk_store string, src_path string, esxi_resource_pool string) (string, int) { - - esxiSSHinfo := SshConnectionInfo{esxi_hostname, esxi_hostport, esxi_username, encoded_esxi_password} - - dst_path := fmt.Sprintf("vi://%s:%s@%s/%s", esxiSSHinfo.user, esxiSSHinfo.pass, esxiSSHinfo.host, esxi_resource_pool) - - ovf_cmd := fmt.Sprintf("ovftool --acceptAllEulas --noSSLVerify --X:useMacNaming=false " + - "-dm=thin --name='%s' --overwrite -ds='%s' '%s' '%s'",guest_name, esxi_disk_store, src_path, dst_path) - - - cmd := exec.Command("/bin/bash", "-c", ovf_cmd) - var out bytes.Buffer - - log.Println("[provider-esxi] ovf_cmd: " + ovf_cmd ) - - cmd.Stdout = &out - err := cmd.Run() - log.Printf("[provider-esxi] ovftool output: %q\n", out.String()) - if err != nil { - //log.Print(err) - log.Println("[provider-esxi] There was an ovftool Error: " + err.Error()) - return "There was an ovftool Error",1 - } - - remote_cmd := fmt.Sprintf("vim-cmd vmsvc/getallvms 2>/dev/null | sort -n | " + - "grep \"[0-9] * %s .*%s\" | awk '{print $1}' | " + - "tail -1", guest_name, guest_name) - - stdout, err := runRemoteSshCommand(esxiSSHinfo, remote_cmd, "get vmid") - vmid := strings.TrimSpace(string(stdout)) - log.Printf("[provider-esxi] get_vmid_cmd: %s", vmid) - if err != nil { - log.Printf("[provider-esxi] Failed get vmid_cmd: %s", stdout) - return "Failed", 1 - } - - - return vmid,0 -} diff --git a/esxi/guest_delete.go b/esxi/guest_delete.go deleted file mode 100644 index 85e39a7..0000000 --- a/esxi/guest_delete.go +++ /dev/null @@ -1,22 +0,0 @@ -package esxi - -import ( - "fmt" - "log" -) - - -func GuestDelete(esxi_hostname string, esxi_hostport string, esxi_username string, encoded_esxi_password string, - vmid string) int { - - esxiSSHinfo := SshConnectionInfo{esxi_hostname, esxi_hostport, esxi_username, encoded_esxi_password} - - remote_cmd := fmt.Sprintf("vim-cmd vmsvc/destroy %s", vmid) - - stdout, err := runRemoteSshCommand(esxiSSHinfo, remote_cmd, "get vmid") - if err != nil { - log.Printf("[provider-esxi] Failed destroy vmid: %s", stdout) - return 1 - } - return 0 - } diff --git a/esxi/resource_pool-read.go b/esxi/resource_pool-read.go deleted file mode 100644 index 24c140d..0000000 --- a/esxi/resource_pool-read.go +++ /dev/null @@ -1,98 +0,0 @@ -package esxi - -import ( - "fmt" - "log" - "strings" - "bufio" - "regexp" - "strconv" -) - - -func resourcePoolREAD(c *Config, pool_id string) (int, bool, int, string, int, bool, int, string, string, error) { - esxiSSHinfo := SshConnectionStruct{c.Esxi_hostname, c.Esxi_hostport, c.Esxi_username, c.Esxi_password} - log.Println("[provider-esxi / resourcePoolREAD] Begin" ) - var cpu_shares, mem_shares string - var cpu_min, cpu_max, mem_min, mem_max, tmpvar int - var cpu_min_expandable, mem_min_expandable bool - - remote_cmd := fmt.Sprintf("vim-cmd hostsvc/rsrc/pool_config_get %s", pool_id) - stdout, err := runRemoteSshCommand(esxiSSHinfo, remote_cmd, "resource pool_config_get") - - if strings.Contains(stdout, "deleted") == true { - log.Printf("[provider-esxi] Already deleted: %s", err) - return 0, false, 0, "", 0, false, 0, "", "", err - } - if err != nil { - log.Printf("[provider-esxi] Failed to get %s: %s", "resource pool_config_get", err) - return 0, false, 0, "", 0, false, 0, "", "", err - } - - is_cpu_flag := true - - scanner := bufio.NewScanner(strings.NewReader(stdout)) - for scanner.Scan() { - switch { - case strings.Contains(scanner.Text(),"memoryAllocation = "): - is_cpu_flag = false - - case strings.Contains(scanner.Text(),"reservation = "): - r,_ := regexp.Compile("[0-9]+") - if is_cpu_flag == true { - cpu_min,_ = strconv.Atoi(r.FindString(scanner.Text())) - } else { - mem_min,_ = strconv.Atoi(r.FindString(scanner.Text())) - } - - case strings.Contains(scanner.Text(),"expandableReservation = "): - r,_ := regexp.Compile("(true|false)") - if is_cpu_flag == true { - cpu_min_expandable,_ = strconv.ParseBool(r.FindString(scanner.Text())) - } else { - mem_min_expandable,_ = strconv.ParseBool(r.FindString(scanner.Text())) - } - - case strings.Contains(scanner.Text(),"limit = "): - r,_ := regexp.Compile("-?[0-9]+") - tmpvar,_ = strconv.Atoi(r.FindString(scanner.Text())) - if tmpvar < 0 { - tmpvar = 0 - } - if is_cpu_flag == true { - cpu_max = tmpvar - } else { - mem_max = tmpvar - } - - case strings.Contains(scanner.Text(),"shares = "): - r,_ := regexp.Compile("[0-9]+") - if is_cpu_flag == true { - cpu_shares = r.FindString(scanner.Text()) - } else { - mem_shares = r.FindString(scanner.Text()) - } - - case strings.Contains(scanner.Text(),"level = "): - r,_ := regexp.Compile("(low|high|normal)") - if r.FindString(scanner.Text()) != "" { - if is_cpu_flag == true { - cpu_shares = r.FindString(scanner.Text()) - } else { - mem_shares = r.FindString(scanner.Text()) - } - } - } - } - - resource_pool_name, err := getPoolNAME(c, pool_id) - if err != nil { - return 0, false, 0, "", 0, false, 0, "", "", err - } - - log.Printf("[provider-esxi / resourcePoolREAD] |%s|%s|%s|%s|%s|%s|%s|%s|%s|", - cpu_min, cpu_min_expandable, cpu_max, cpu_shares, mem_min, mem_min_expandable, - mem_max, mem_shares, resource_pool_name) - return cpu_min, cpu_min_expandable, cpu_max, cpu_shares, mem_min, - mem_min_expandable, mem_max, mem_shares, resource_pool_name, err -} diff --git a/main.go b/main.go index 285ac93..89bf5ef 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,7 @@ package main import ( "github.com/hashicorp/terraform/plugin" "github.com/hashicorp/terraform/terraform" - "./esxi" + "github.com/terraform-providers/terraform-provider-esxi/esxi" ) func main() { diff --git a/version b/version new file mode 100644 index 0000000..0ec25f7 --- /dev/null +++ b/version @@ -0,0 +1 @@ +v1.0.0