161 lines
4.5 KiB
Python
Executable File
161 lines
4.5 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
from pyVmomi import vim
|
|
import pyVim.connect
|
|
import atexit
|
|
import argparse
|
|
#import time
|
|
import json
|
|
import siteconf
|
|
|
|
site_conf = siteconf.read()
|
|
|
|
|
|
def get_args():
|
|
parser = argparse.ArgumentParser(
|
|
description='Set VM network')
|
|
|
|
parser.add_argument('-s', '--site',
|
|
required=True,
|
|
action='store',
|
|
help='name of the site to connect to')
|
|
parser.add_argument('--list-datastores',
|
|
required=False,
|
|
action='store_true',
|
|
help='list all accessable datastores')
|
|
parser.add_argument('--list-hosts',
|
|
required=False,
|
|
action='store_true',
|
|
help='list all hosts')
|
|
parser.add_argument('--list-config',
|
|
required=False,
|
|
action='store_true',
|
|
help='list relevant configuration')
|
|
|
|
args = parser.parse_args()
|
|
return args
|
|
|
|
def get_datastores(content):
|
|
obj_view = content.viewManager.CreateContainerView(
|
|
content.rootFolder, [vim.Datastore], True)
|
|
ds_list = obj_view.view
|
|
obj_view.Destroy()
|
|
result = []
|
|
for ds in ds_list:
|
|
if not ds.summary.accessible:
|
|
continue
|
|
result.append(ds)
|
|
return result
|
|
|
|
def get_all_objs(content, vimtype):
|
|
obj = {}
|
|
container = content.viewManager.CreateContainerView(
|
|
content.rootFolder, vimtype, True)
|
|
for managed_object_ref in container.view:
|
|
obj.update({managed_object_ref: managed_object_ref.name})
|
|
return obj
|
|
|
|
#def list_datastores(content):
|
|
# datastores = get_datastores(content)
|
|
# for ds in datastores:
|
|
# cap = ds.summary.capacity / 1024**4
|
|
# free = ds.summary.freeSpace / 1024**4
|
|
# ratio = free / cap
|
|
# print(f"{ds.summary.name} {ratio:.0%} {free:.3f} TB / {cap:.3f} TB")
|
|
|
|
|
|
def main():
|
|
args = get_args()
|
|
|
|
if not args.site in site_conf:
|
|
print(f"site not found: {args.site}")
|
|
exit(1)
|
|
|
|
host = site_conf[args.site]['vcenter']
|
|
user = site_conf[args.site]['vcenter_user']
|
|
passwd = site_conf[args.site]['vcenter_passwd']
|
|
|
|
si = pyVim.connect.SmartConnect(
|
|
host = host,
|
|
user = user,
|
|
pwd = passwd,
|
|
disableSslCertValidation = True
|
|
)
|
|
atexit.register(pyVim.connect.Disconnect, si)
|
|
content = si.RetrieveContent()
|
|
if args.list_datastores + args.list_hosts + args.list_config > 1:
|
|
print("More than 1 command given.")
|
|
elif args.list_datastores:
|
|
datastores = get_datastores(content)
|
|
names = []
|
|
for ds in datastores:
|
|
names.append(ds.summary.name)
|
|
names.sort()
|
|
print(names)
|
|
elif args.list_hosts:
|
|
hosts = get_all_objs(content, [vim.HostSystem])
|
|
hostlist = {}
|
|
for h in hosts:
|
|
hostlist[hosts[h]] = {
|
|
'connectionState': h.runtime.connectionState,
|
|
'powerState': h.runtime.powerState,
|
|
'standbyMode': h.runtime.standbyMode,
|
|
'inMaintenanceMode': h.runtime.inMaintenanceMode,
|
|
'inQuarantineMode': h.runtime.inQuarantineMode,
|
|
}
|
|
print(hostlist)
|
|
elif args.list_config:
|
|
print({
|
|
'vcenter_addr': site_conf[args.site]['vcenter'],
|
|
'vcenter_user': site_conf[args.site]['vcenter_user']
|
|
});
|
|
else:
|
|
print("No command given.")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
|
|
# vim: set tabstop=4 shiftwidth=4 expandtab smarttab:
|
|
|
|
####!/usr/bin/env python3
|
|
###import argparse
|
|
###import siteconf
|
|
###
|
|
###site_conf = siteconf.read()
|
|
###
|
|
###
|
|
###def get_args():
|
|
### parser = argparse.ArgumentParser(
|
|
### description='Set VM network')
|
|
###
|
|
### parser.add_argument('-s', '--site',
|
|
### required=True,
|
|
### action='store',
|
|
### help='name of the site to connect to')
|
|
###
|
|
### args = parser.parse_args()
|
|
### return args
|
|
###
|
|
###
|
|
###
|
|
###def main():
|
|
### args = get_args()
|
|
###
|
|
### if not args.site in site_conf:
|
|
### print(f"site not found: {args.site}")
|
|
### exit(1)
|
|
###
|
|
### vms = get_vms(content)
|
|
### for vm in vms:
|
|
### for d in vm.config.hardware.device:
|
|
### if hasattr(d, 'macAddress'):
|
|
### print(f'{d.macAddress} {vm.name}')
|
|
###
|
|
###
|
|
###
|
|
###
|
|
###if __name__ == "__main__":
|
|
### main()
|
|
###
|
|
#### vim: set tabstop=4 shiftwidth=4 expandtab smarttab:
|