Source code for accre.proxmox
"""
Convenience wrapper for access to the ACCRE ProxMox infrastructure
cluster read-only API using the proxmoxer library.
See https://pypi.org/project/proxmoxer/ for API details.
"""
from proxmoxer import ProxmoxAPI
from accre.config import get_config
CONFIG = get_config()
[docs]class ACCREProxmox(ProxmoxAPI):
"""
ACCRE Wrapper for the proxmoxer ProxmoxAPI class providing
pre-configured access and authentication options, allowing
one to easily instantiate a ProxmoxAPI without providing
arguments.
Additional ACCRE-specific convenience methods are provided.
"""
def __init__(self, failover=True):
"""
Initialize a ProxmoxAPI connecting to the ACCRE cluter.
:param bool failover: If true, attempt to connect to
each hypervisor in the configured list. Otherwise
only attempt to connect to the first configured
hypervisor.
"""
verify_str = CONFIG['proxmox']['verify_ssl']
if verify_str.lower() == 'false':
verify = False
else:
verify = True
hvs = CONFIG['proxmox']['hosts'].split(',')
for idx, hv in enumerate(hvs):
try:
super().__init__(
hv,
user=CONFIG['proxmox']['user'],
password=CONFIG['proxmox']['password'],
verify_ssl=verify
)
return
except Exception as e:
if not failover or idx == len(hvs) - 1:
raise
[docs] def node_vm_table(self, node):
"""
Return a human readable ascii table of VMs and their properties
for a specified hypervisor node.
:param str node: Hypervisor node name to query
:returns: ASCII table of vms on the given node and their properties
:rtype: str
"""
tblfmt = '{0: <20.20} {1:<7.7} {2:>4.4} {3:>7.7} {4:>10.10}'
all_vms = self.cluster.resources.get(type='vm')
vms = [vm for vm in all_vms if vm['node'] == node]
lines = []
lines.append(tblfmt.format(
'NAME', 'STATUS', 'CPUS', 'MEM', 'DISK'
))
lines.append(tblfmt.format(
'-'*30, '-'*30, '-'*30, '-'*30, '-'*30
))
for vm in vms:
lines.append(tblfmt.format(
vm['name'],
vm['status'],
str(vm['maxcpu']),
str(round(float(vm['maxmem']) / 1024**3, 2)) + 'GB',
str(round(float(vm['maxdisk']) / 1024**3, 2)) + 'GB'
))
return '\n'.join(lines)