Skip to content

Commit

Permalink
Add -o option, fix isSummary save, add beta esxi-vm-destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
josenk committed Oct 24, 2017
1 parent 625e4f2 commit 5809a2d
Show file tree
Hide file tree
Showing 6 changed files with 300 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
esxi_vm_functions.pyc
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
all:
@echo "make install"

install:
install -m 755 ./esxi-vm-create /usr/local/bin/
install -m 755 ./esxi-vm-destroy /usr/local/bin/
install -m 755 ./esxi_vm_functions.py /usr/local/bin/
@echo "Install Success."

uninstall:
rm -fr /usr/local/bin/esxi-vm-create
rm -fr /usr/local/bin/esxi-vm-destroy
rm -fr /usr/local/bin/esxi_vm_functions.py
rm -fr /usr/local/bin/esxi_vm_functions.pyc
28 changes: 18 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ Usage

By default the Network set set to "None". A full or partial MAC address can be specified. A partial MAC address argument would be 3 Hex pairs which would then be prepended by VMware's OEM "00:50:56".

By default the VM is powered on. If an ISO was specified, then it will boot the ISO image. Otherwise, the VM will attempt a PXE boot if a Network Interface was specified. You could customize the ISO image to specify the kickstart file, or PXE boot using COBBLER, Foreman, Razor, or your favorite provisioning tool.
By default the VM is powered on. If an ISO was specified, then it will boot the ISO image. Otherwise, the VM will attempt a PXE boot if a Network Interface was specified. You could customize the ISO image to specify the kickstart file, or PXE boot using COBBLER, Foreman, Razor, or your favorite provisioning tool.

To help with automated provisioning, the script will output the full MAC address and exit code 0 on success. You can specify --summary to get a more detailed summary of the VM that was created.


Requirements
------------

You must enable ssh access on your ESXi server. The VMware VIX API tools are not required.
You must enable ssh access on your ESXi server. Google 'how to enable ssh access on esxi' for instructions. The VMware VIX API tools are not required.

It's HIGHLY RECOMMENDED to use password-less authentication by copying your ssh public keys to the ESXi host, otherwise your ESXi root password could be stored in clear-text in your home directory.

Python and paramiko is a software requirement.

```
yum -y install python python-paramiko
```
Expand All @@ -50,9 +50,11 @@ Command Line Args

```
./esxi-vm-create --help
usage: esxi-vm-create [-h] [-d] [-H HOST] [-U USER] [-P PASSWORD] [-n NAME]
[-c CPU] [-m MEM] [-v HDISK] [-i ISO] [-N NET] [-M MAC]
[-S STORE] [-g GUESTOS] [-V] [--summary] [-u]
[-S STORE] [-g GUESTOS] [-o VMXOPTS] [-V] [--summary]
[-u]
ESXi Create VM utility.
Expand All @@ -65,20 +67,21 @@ optional arguments:
ESXi Host password (*****)
-n NAME, --name NAME VM name
-c CPU, --cpu CPU Number of vCPUS (2)
-m MEM, --mem MEM Memory in GB (2)
-m MEM, --mem MEM Memory in GB (4)
-v HDISK, --vdisk HDISK
Size of virt hdisk (12)
Size of virt hdisk (20)
-i ISO, --iso ISO CDROM ISO Path | None (None)
-N NET, --net NET Network Interface | None (192.168.1)
-N NET, --net NET Network Interface | None (None)
-M MAC, --mac MAC MAC address
-S STORE, --store STORE
vmfs Store | LeastUsed (DS_3TB_m)
vmfs Store | LeastUsed (LeastUsed)
-g GUESTOS, --guestos GUESTOS
Guest OS. (centos-64)
-o VMXOPTS, --options VMXOPTS
Comma list of VMX Options.
-V, --verbose Enable Verbose mode (False)
--summary Display Summary (False)
-u, --updateDefaults Update Default VM settings stored in ~/.esxi-vm.yml
```


Expand Down Expand Up @@ -176,6 +179,12 @@ Guest OS: centos-64
MAC: 00:0c:29:ea:a0:42
00:0c:29:ea:a0:42
```

Merge/Add extra VMX options, saved as default.
```
./esxi-vm-create -o 'floppy0.present = "TRUE",svga.autodetect = "TRUE",svga.present = "TRUE"' -u
Saving new Defaults to ~/.esxi-vm.yml
```

License
Expand All @@ -202,4 +211,3 @@ Support
Website : http://www.jintegrate.co

github : http://github.com/josenk/

44 changes: 39 additions & 5 deletions esxi-vm-create
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ STORE = ConfigData['STORE']
NET = ConfigData['NET']
ISO = ConfigData['ISO']
GUESTOS = ConfigData['GUESTOS']
VMXOPTS = ConfigData['VMXOPTS']

ErrorMessages = ""
MAC = ""
Expand Down Expand Up @@ -60,6 +61,7 @@ parser.add_argument("-N", "--net", dest='NET', type=str, help="Network Interface
parser.add_argument("-M", "--mac", dest='MAC', type=str, help="MAC address")
parser.add_argument("-S", "--store", dest='STORE', type=str, help="vmfs Store | LeastUsed (" + str(STORE) + ")")
parser.add_argument("-g", "--guestos", dest='GUESTOS', type=str, help="Guest OS. (" + str(GUESTOS) + ")")
parser.add_argument("-o", "--options", dest='VMXOPTS', type=str, default='NIL', help="Comma list of VMX Options.")
parser.add_argument('-V', '--verbose', dest='isVerbosearg', action='store_true', help="Enable Verbose mode (" + str(isVerbose) + ")")
parser.add_argument('--summary', dest='isSummaryarg', action='store_true', help="Display Summary (" + str(isSummary) + ")")
parser.add_argument("-u", "--updateDefaults", dest='UPDATE', action='store_true', help="Update Default VM settings stored in ~/.esxi-vm.yml")
Expand Down Expand Up @@ -100,11 +102,17 @@ if STORE == "":
STORE = "LeastUsed"
if args.GUESTOS:
GUESTOS=args.GUESTOS
if args.VMXOPTS == '' and VMXOPTS != '':
VMXOPTS=''
if args.VMXOPTS and args.VMXOPTS != 'NIL':
VMXOPTS=args.VMXOPTS.split(",")


if args.UPDATE:
print "Saving new Defaults to ~/.esxi-vm.yml"
ConfigData['isDryRun'] = isDryRun
ConfigData['isVerbose'] = isVerbose
ConfigData['isSummary'] = isSummary
ConfigData['HOST'] = HOST
ConfigData['USER'] = USER
ConfigData['PASSWORD'] = PASSWORD
Expand All @@ -117,6 +125,7 @@ if args.UPDATE:
ConfigData['NET'] = NET
ConfigData['ISO'] = ISO
ConfigData['GUESTOS'] = GUESTOS
ConfigData['VMXOPTS'] = VMXOPTS
SaveConfig(ConfigData)
if NAME == "":
sys.exit(0)
Expand Down Expand Up @@ -194,7 +203,7 @@ if MAC != "":
print "ERROR: " + MAC + " Invalid MAC address."
ErrorMessages += " " + MAC + " Invalid MAC address."
CheckHasErrors = True


#
# Get from ESXi host if ISO exists
Expand Down Expand Up @@ -353,6 +362,33 @@ if NET != "None":
VMX.append('ethernet0.addressType = "static"')
VMX.append('ethernet0.address = "' + MAC + '"')

#
# Merge extra VMX options
for VMXopt in VMXOPTS:
try:
k,v = VMXopt.split("=")
except:
k=""
v=""
key = k.lstrip().strip()
value = v.lstrip().strip()
for i in VMX:
try:
ikey,ivalue = i.split("=")
except:
break
if ikey.lstrip().strip().lower() == key.lower():
index = VMX.index(i)
VMX[index] = ikey + " = " + value
break
else:
if key != '' and value != '':
VMX.append(key + " = " + value)

if isVerbose and VMXOPTS != '':
print "VMX file:"
for i in VMX:
print i

MyVM = FullPath + "/" + NAME
if CheckHasErrors:
Expand Down Expand Up @@ -384,7 +420,7 @@ if not isDryRun and not CheckHasErrors:
(stdin, stdout, stderr) = ssh.exec_command("vim-cmd solo/registervm " + MyVM + ".vmx")
type(stdin)
VMID = int(stdout.readlines()[0])

# Power on VM
if isVerbose:
print "Power ON VM"
Expand Down Expand Up @@ -453,7 +489,7 @@ if isSummary:
if isVerbose:
print "Format: " + DISKFORMAT
print "DS Store: " + DSSTORE
print "Network: " + NET
print "Network: " + NET
if ISO:
print "ISO: " + ISO
if isVerbose:
Expand All @@ -472,5 +508,3 @@ else:
else:
print GeneratedMAC
sys.exit(0)


Loading

0 comments on commit 5809a2d

Please sign in to comment.