mirror of https://github.com/Kodomo/esxi-vm
Use a dictionnary for options instead of working on strings.
parent
3fc2aa89b4
commit
f6e17f5c5d
|
|
@ -36,7 +36,7 @@ Requirements
|
||||||
|
|
||||||
* It's **highly recommenced** 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.
|
* It's **highly recommenced** 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 2.7 [paramiko](http://www.paramiko.org) and [PyYAML](https://github.com/yaml/pyyaml) are software requirements.
|
* Python 3, [paramiko](http://www.paramiko.org) and [PyYAML](https://github.com/yaml/pyyaml) are software requirements.
|
||||||
|
|
||||||
```
|
```
|
||||||
pip install paramiko pyyaml
|
pip install paramiko pyyaml
|
||||||
|
|
|
||||||
142
esxi-vm-create
142
esxi-vm-create
|
|
@ -6,6 +6,7 @@ import paramiko # For remote ssh
|
||||||
|
|
||||||
from esxi_vm_functions import *
|
from esxi_vm_functions import *
|
||||||
|
|
||||||
|
|
||||||
# Defaults and Variable setup
|
# Defaults and Variable setup
|
||||||
ConfigData = setup_config()
|
ConfigData = setup_config()
|
||||||
NAME = ""
|
NAME = ""
|
||||||
|
|
@ -290,7 +291,7 @@ if ISO != "" and not ISOfound:
|
||||||
ErrorMessages += " ISO {} not found. Use full path to ISO".format(ISO)
|
ErrorMessages += " ISO {} not found. Use full path to ISO".format(ISO)
|
||||||
CheckHasErrors = True
|
CheckHasErrors = True
|
||||||
|
|
||||||
# Check if DSPATH/NAME aready exists
|
# Check if DSPATH/NAME already exists
|
||||||
try:
|
try:
|
||||||
FullPath = DSPATH + "/" + NAME
|
FullPath = DSPATH + "/" + NAME
|
||||||
(stdin, stdout, stderr) = ssh.exec_command("ls -d " + FullPath)
|
(stdin, stdout, stderr) = ssh.exec_command("ls -d " + FullPath)
|
||||||
|
|
@ -302,56 +303,66 @@ try:
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
vmx = []
|
vmx = {
|
||||||
vmx.append('config.version = "8"')
|
'config.version': '8',
|
||||||
vmx.append('virtualHW.version = "8"')
|
'virtualHW.version': '8',
|
||||||
vmx.append('vmci0.present = "TRUE"')
|
'vmci0.present': 'TRUE',
|
||||||
vmx.append('displayName = "{}"'.format(NAME))
|
'displayName': NAME,
|
||||||
vmx.append('floppy0.present = "FALSE"')
|
'floppy0.present': 'FALSE',
|
||||||
vmx.append('numvcpus = "{}"'.format(CPU))
|
'numvcpus': CPU,
|
||||||
vmx.append('scsi0.present = "TRUE"')
|
'scsi0.present': 'TRUE',
|
||||||
vmx.append('scsi0.sharedBus = "none"')
|
'scsi0.sharedBus': 'none',
|
||||||
vmx.append('scsi0.virtualDev = "pvscsi"')
|
'scsi0.virtualDev': 'pvscsi',
|
||||||
vmx.append('memsize = "{}"'.format(MEM * 1024))
|
'memsize': str(MEM * 1024),
|
||||||
vmx.append('scsi0:0.present = "TRUE"')
|
'scsi0:0.present': 'TRUE',
|
||||||
vmx.append('scsi0:0.fileName = "{}.vmdk"'.format(NAME))
|
'scsi0:0.fileName': "{}.vmdk".format(NAME),
|
||||||
vmx.append('scsi0:0.deviceType = "scsi-hardDisk"')
|
'scsi0:0.deviceType': 'scsi-hardDisk',
|
||||||
if ISO == "":
|
'pciBridge0.present': 'TRUE',
|
||||||
vmx.append('ide1:0.present = "TRUE"')
|
'pciBridge4.present': 'TRUE',
|
||||||
vmx.append('ide1:0.fileName = "emptyBackingString"')
|
'pciBridge4.virtualDev': 'pcieRootPort',
|
||||||
vmx.append('ide1:0.deviceType = "atapi-cdrom"')
|
'pciBridge4.functions': '8',
|
||||||
vmx.append('ide1:0.startConnected = "FALSE"')
|
'pciBridge5.present': 'TRUE',
|
||||||
vmx.append('ide1:0.clientDevice = "TRUE"')
|
'pciBridge5.virtualDev': 'pcieRootPort',
|
||||||
else:
|
'pciBridge5.functions': '8',
|
||||||
vmx.append('ide1:0.present = "TRUE"')
|
'pciBridge6.present': 'TRUE',
|
||||||
vmx.append('ide1:0.fileName = "{}"'.format(ISO))
|
'pciBridge6.virtualDev': 'pcieRootPort',
|
||||||
vmx.append('ide1:0.deviceType = "cdrom-image"')
|
'pciBridge6.functions': '8',
|
||||||
vmx.append('pciBridge0.present = "TRUE"')
|
'pciBridge7.present': 'TRUE',
|
||||||
vmx.append('pciBridge4.present = "TRUE"')
|
'pciBridge7.virtualDev': 'pcieRootPort',
|
||||||
vmx.append('pciBridge4.virtualDev = "pcieRootPort"')
|
'pciBridge7.functions': '8',
|
||||||
vmx.append('pciBridge4.functions = "8"')
|
'guestOS': GUESTOS
|
||||||
vmx.append('pciBridge5.present = "TRUE"')
|
}
|
||||||
vmx.append('pciBridge5.virtualDev = "pcieRootPort"')
|
|
||||||
vmx.append('pciBridge5.functions = "8"')
|
if ISO == "":
|
||||||
vmx.append('pciBridge6.present = "TRUE"')
|
vmx.update({
|
||||||
vmx.append('pciBridge6.virtualDev = "pcieRootPort"')
|
'ide1:0.present': 'TRUE',
|
||||||
vmx.append('pciBridge6.functions = "8"')
|
'ide1:0.fileName': 'emptyBackingString',
|
||||||
vmx.append('pciBridge7.present = "TRUE"')
|
'ide1:0.deviceType': 'atapi-cdrom',
|
||||||
vmx.append('pciBridge7.virtualDev = "pcieRootPort"')
|
'ide1:0.startConnected': 'FALSE',
|
||||||
vmx.append('pciBridge7.functions = "8"')
|
'ide1:0.clientDevice': 'TRUE'
|
||||||
vmx.append('guestOS = "{}"'.format(GUESTOS))
|
})
|
||||||
if NET != "None":
|
else:
|
||||||
vmx.append('ethernet0.virtualDev = "vmxnet3"')
|
vmx.update({
|
||||||
vmx.append('ethernet0.present = "TRUE"')
|
'ide1:0.present': 'TRUE',
|
||||||
vmx.append('ethernet0.networkName = "{}"'.format(NET))
|
'ide1:0.fileName': ISO,
|
||||||
if MAC == "":
|
'ide1:0.deviceType': 'atapi-cdrom',
|
||||||
vmx.append('ethernet0.addressType = "generated"')
|
'ide1:0.startConnected': 'TRUE',
|
||||||
else:
|
})
|
||||||
vmx.append('ethernet0.addressType = "static"')
|
|
||||||
vmx.append('ethernet0.address = "{}"'.format(MAC))
|
if NET != "None":
|
||||||
|
vmx.update({
|
||||||
|
'ethernet0.virtualDev': 'vmxnet3',
|
||||||
|
'ethernet0.present': 'TRUE',
|
||||||
|
'ethernet0.networkName': NET
|
||||||
|
})
|
||||||
|
if MAC == "":
|
||||||
|
vmx.update({'ethernet0.addressType': 'generated'})
|
||||||
|
else:
|
||||||
|
vmx.update({
|
||||||
|
'ethernet0.addressType': 'static',
|
||||||
|
'ethernet0.address': MAC
|
||||||
|
})
|
||||||
|
|
||||||
#
|
|
||||||
# Merge extra VMX options
|
|
||||||
for VMXopt in VMXOPTS:
|
for VMXopt in VMXOPTS:
|
||||||
try:
|
try:
|
||||||
k, v = VMXopt.split("=")
|
k, v = VMXopt.split("=")
|
||||||
|
|
@ -360,23 +371,12 @@ for VMXopt in VMXOPTS:
|
||||||
v = ""
|
v = ""
|
||||||
key = k.lstrip().strip()
|
key = k.lstrip().strip()
|
||||||
value = v.lstrip().strip()
|
value = v.lstrip().strip()
|
||||||
for i in vmx:
|
vmx[key] = value
|
||||||
try:
|
|
||||||
ikey, ivalue = i.split("=")
|
|
||||||
except Exception:
|
|
||||||
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 != '':
|
if isVerbose and VMXOPTS != '':
|
||||||
print("VMX file:")
|
print("VMX file:")
|
||||||
for i in vmx:
|
for k, v in vmx.items():
|
||||||
print(i)
|
print('{} = "{}"'.format(k, v))
|
||||||
|
|
||||||
MyVM = FullPath + "/" + NAME
|
MyVM = FullPath + "/" + NAME
|
||||||
if CheckHasErrors:
|
if CheckHasErrors:
|
||||||
|
|
@ -388,27 +388,27 @@ if not isDryRun and not CheckHasErrors:
|
||||||
try:
|
try:
|
||||||
if isVerbose:
|
if isVerbose:
|
||||||
print("Create {}.vmx file".format(NAME))
|
print("Create {}.vmx file".format(NAME))
|
||||||
(stdin, stdout, stderr) = ssh.exec_command("mkdir " + FullPath)
|
(stdin, stdout, stderr) = ssh.exec_command("mkdir {}".format(FullPath))
|
||||||
type(stdin)
|
type(stdin)
|
||||||
for line in vmx:
|
for k, v in vmx.items():
|
||||||
(stdin, stdout, stderr) = ssh.exec_command("echo '" + line + "' >>" + MyVM + ".vmx")
|
(stdin, stdout, stderr) = ssh.exec_command("echo '{} = \"{}\"' >> {}.vmx".format(k, v, MyVM))
|
||||||
type(stdin)
|
type(stdin)
|
||||||
|
|
||||||
if isVerbose:
|
if isVerbose:
|
||||||
print("Create {}.vmdk file".format(NAME))
|
print("Create {}.vmdk file".format(NAME))
|
||||||
(stdin, stdout, stderr) = \
|
(stdin, stdout, stderr) = \
|
||||||
ssh.exec_command("vmkfstools -c " + str(HDISK) + "G -d " + DISKFORMAT + " " + MyVM + ".vmdk")
|
ssh.exec_command("vmkfstools -c {}G -d {} {}.vmdk".format(HDISK, DISKFORMAT, MyVM))
|
||||||
type(stdin)
|
type(stdin)
|
||||||
|
|
||||||
if isVerbose:
|
if isVerbose:
|
||||||
print("Register VM")
|
print("Register VM")
|
||||||
(stdin, stdout, stderr) = ssh.exec_command("vim-cmd solo/registervm " + MyVM + ".vmx")
|
(stdin, stdout, stderr) = ssh.exec_command("vim-cmd solo/registervm {}.vmx".format(MyVM))
|
||||||
type(stdin)
|
type(stdin)
|
||||||
VMID = int(stdout.readlines()[0])
|
VMID = int(stdout.readlines()[0])
|
||||||
|
|
||||||
if isVerbose:
|
if isVerbose:
|
||||||
print("Power ON VM")
|
print("Power ON VM")
|
||||||
(stdin, stdout, stderr) = ssh.exec_command("vim-cmd vmsvc/power.on " + str(VMID))
|
(stdin, stdout, stderr) = ssh.exec_command("vim-cmd vmsvc/power.on {}".format(VMID))
|
||||||
type(stdin)
|
type(stdin)
|
||||||
if stderr.readlines():
|
if stderr.readlines():
|
||||||
print("Error Powering-on VM.")
|
print("Error Powering-on VM.")
|
||||||
|
|
@ -416,7 +416,7 @@ if not isDryRun and not CheckHasErrors:
|
||||||
|
|
||||||
if NET != "None":
|
if NET != "None":
|
||||||
(stdin, stdout, stderr) = ssh.exec_command(
|
(stdin, stdout, stderr) = ssh.exec_command(
|
||||||
"grep -i 'ethernet0.*ddress = ' " + MyVM + ".vmx |tail -1|awk '{print $NF}'")
|
"grep -i 'ethernet0.*ddress = ' {}.vmx |tail -1|awk '{print $NF}'".format(MyVM))
|
||||||
type(stdin)
|
type(stdin)
|
||||||
GeneratedMAC = str(stdout.readlines()[0]).strip('\n"')
|
GeneratedMAC = str(stdout.readlines()[0]).strip('\n"')
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue