template dict
This commit is contained in:
parent
0f4b5a4c61
commit
bcde984c95
26
etalon_clone
26
etalon_clone
@ -110,7 +110,7 @@ def clone_vm(content, template, vm_name, datacenter_name, datastore_name,
|
||||
clonespec.location = relospec
|
||||
clonespec.powerOn = False
|
||||
|
||||
print("cloning VM...")
|
||||
print(f"cloning to VM '{vm_name}'...")
|
||||
task = template.Clone(folder=destfolder, name=vm_name, spec=clonespec)
|
||||
wait_for_task(task)
|
||||
|
||||
@ -136,7 +136,7 @@ def extend_disk(content, vm_name, extend_to_kb):
|
||||
dev_changes.append(disk_spec)
|
||||
spec = vim.vm.ConfigSpec()
|
||||
spec.deviceChange = dev_changes
|
||||
print("extending disk '"+ dev.deviceInfo.label +"' to "+ str(extend_to_kb) +" kB")
|
||||
print(f"extending disk '{dev.deviceInfo.label}' to {str(extend_to_kb)} kB")
|
||||
task = vm.ReconfigVM_Task(spec=spec)
|
||||
wait_for_task(task)
|
||||
|
||||
@ -145,13 +145,13 @@ def set_cpu_mem(content, vm_name, cpu, mem_mb):
|
||||
spec = vim.vm.ConfigSpec()
|
||||
spec.numCPUs = cpu
|
||||
spec.memoryMB = mem_mb
|
||||
print("Set cpu="+ str(cpu) +", memory="+ str(mem_mb) +" MB")
|
||||
print(f"set cpu={str(cpu)}, memory={str(mem_mb)} MB")
|
||||
task = vm.ReconfigVM_Task(spec=spec)
|
||||
wait_for_task(task)
|
||||
|
||||
def power_on(content, vm_name):
|
||||
vm = get_obj(content, [vim.VirtualMachine], vm_name)
|
||||
print("Powering on '"+ vm_name +"'")
|
||||
print(f"powering on '{vm_name}'")
|
||||
task = vm.PowerOn()
|
||||
wait_for_task(task)
|
||||
|
||||
@ -162,7 +162,6 @@ def main():
|
||||
print(f"site not found: {args.site}")
|
||||
exit(1)
|
||||
site = site_conf[args.site];
|
||||
#pprint(site)
|
||||
|
||||
si = pyVim.connect.SmartConnect(
|
||||
host = site["vcenter"],
|
||||
@ -175,16 +174,29 @@ def main():
|
||||
|
||||
content = si.RetrieveContent()
|
||||
template_name = site["template"]
|
||||
if args.template is not None:
|
||||
if args.template is None:
|
||||
if len(site["template"]) == 1:
|
||||
template_name, = site["template"].values()
|
||||
else:
|
||||
print("specify `-t TEMPLATE` with:")
|
||||
for t in site["template"]:
|
||||
print(f" - {t} ({site['template'][t]})")
|
||||
print(f" - or any template in vsphere")
|
||||
exit(1)
|
||||
else:
|
||||
if args.template in site["template"]:
|
||||
template_name = site["template"][args.template]
|
||||
else:
|
||||
template_name = args.template
|
||||
template = get_obj(content, [vim.VirtualMachine], template_name)
|
||||
if not template:
|
||||
print("template not found")
|
||||
exit(1)
|
||||
print(f"template: {template_name}")
|
||||
|
||||
vm = get_obj(content, [vim.VirtualMachine], args.vm_name)
|
||||
if vm:
|
||||
print("VM '"+ args.vm_name +"' exists.")
|
||||
print(f"VM '{args.vm_name}' exists.")
|
||||
exit(1)
|
||||
|
||||
datastore = site["datastore"]
|
||||
|
||||
@ -19,6 +19,11 @@ sub read {
|
||||
$site_conf{$site} = {};
|
||||
} elsif ($line =~ /^([A-Za-z0-9_-]+)\s+(.+)/) {
|
||||
$site_conf{$site}->{$1} = $2;
|
||||
} elsif ($line =~ /^([A-Za-z0-9_-]+)%\s+([A-Za-z0-9_-]+)\s+(.+)/) {
|
||||
if (ref($site_conf{$site}->{$1}) ne 'HASH') {
|
||||
$site_conf{$site}->{$1} = {};
|
||||
}
|
||||
$site_conf{$site}->{$1}->{$2} = $3;
|
||||
} else {
|
||||
die "invalid line in site_conf: $line\n";
|
||||
}
|
||||
|
||||
@ -18,11 +18,16 @@ def read():
|
||||
if r := re.match('\[([A-Za-z0-9_-]+)\]', line):
|
||||
site = r[1]
|
||||
site_conf[site] = {}
|
||||
elif r := re.match('([A-Za-z0-9_-]+)\s+(.+)', line):
|
||||
continue
|
||||
if site == None:
|
||||
print(f"configuration line before 1st block: {line}")
|
||||
exit(1)
|
||||
if r := re.match('([A-Za-z0-9_-]+)\s+(.+)', line):
|
||||
site_conf[site][r[1]] = r[2]
|
||||
elif r := re.match('([A-Za-z0-9_-]+)%\s+([A-Za-z0-9_-]+)\s+(.+)', line):
|
||||
if r[1] not in site_conf[site] or type(site_conf[site][r[1]]) is not dict:
|
||||
site_conf[site][r[1]] = {}
|
||||
site_conf[site][r[1]][r[2]] = r[3]
|
||||
else:
|
||||
print(f"invalid line in site_conf: {line}")
|
||||
exit(1)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user