2020-03-03 07:55:30 +00:00
|
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
from enum import IntEnum
|
|
|
|
|
from xmlrpc.client import ServerProxy as RPCClient
|
|
|
|
|
from xmltodict import parse
|
|
|
|
|
from config import config
|
|
|
|
|
from ldap_list import vm_list
|
|
|
|
|
from db_export import setconn
|
|
|
|
|
|
|
|
|
|
# Constants
|
|
|
|
|
ALL_VM_STATES = -1
|
|
|
|
|
START_ID = -1 # First id whatever it is
|
|
|
|
|
END_ID = -1 # Last id whatever it is
|
|
|
|
|
session_string = config['oca']['client_secrets']
|
|
|
|
|
opnserver = config['oca']['opn_server']
|
|
|
|
|
|
|
|
|
|
class VMState(IntEnum):
|
|
|
|
|
INIT = 0
|
|
|
|
|
PENDING = 1
|
|
|
|
|
HOLD = 2
|
|
|
|
|
ACTIVE = 3
|
|
|
|
|
STOPPED = 4
|
|
|
|
|
SUSPENDED = 5
|
|
|
|
|
DONE = 6
|
|
|
|
|
FAILED = 7
|
|
|
|
|
POWEROFF = 8
|
|
|
|
|
UNDEPLOYED = 9
|
|
|
|
|
CLONING = 10
|
|
|
|
|
CLONING_FAILURE = 11
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class VmFilterFlag(IntEnum):
|
|
|
|
|
UIDUserResources = 0 # UID User’s Resources
|
|
|
|
|
UserAndItsGroupsResources = -1 # Resources belonging to the user and any of his groups
|
|
|
|
|
AllResources = -2 # All resources
|
|
|
|
|
UserResources = -3 # Resources belonging to the user
|
|
|
|
|
UserPrimaryGroupResources = -4 # Resources belonging to the user’s primary group
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class VM:
|
|
|
|
|
def __init__(self, vm: dict):
|
|
|
|
|
self.id = vm.get('ID', None)
|
|
|
|
|
self.owner = {
|
|
|
|
|
'id': vm.get('UID', None),
|
|
|
|
|
'name': vm.get('UNAME', None),
|
|
|
|
|
'gname': vm.get('GNAME', None)
|
|
|
|
|
}
|
|
|
|
|
self.name = vm.get('NAME', None)
|
|
|
|
|
self.status = vm.get('STATE', None)
|
|
|
|
|
if self.status:
|
|
|
|
|
self.status = VMState(int(self.status)).name.lower()
|
|
|
|
|
|
|
|
|
|
template = vm['TEMPLATE']
|
|
|
|
|
|
|
|
|
|
self.graphics = template.get('GRAPHICS', {})
|
|
|
|
|
self.memory = template.get('MEMORY', None)
|
|
|
|
|
self.vcpu = template.get('VCPU', None)
|
|
|
|
|
self.host = {
|
|
|
|
|
'name': ((vm.get('HISTORY_RECORDS', {}) or {}).get('HISTORY', {}) or {}).get('HOSTNAME', None),
|
|
|
|
|
'id': ((vm.get('HISTORY_RECORDS', {}) or {}).get('HISTORY', {}) or {}).get('HID', None),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
with RPCClient(opnserver) as rpc_client:
|
|
|
|
|
success, response, *_ = rpc_client.one.vmpool.infoextended(
|
|
|
|
|
session_string , VmFilterFlag.AllResources.value, START_ID, END_ID, VMState.ACTIVE.value
|
|
|
|
|
)
|
|
|
|
|
if success:
|
|
|
|
|
vms = json.loads(json.dumps(parse(response)))['VM_POOL']['VM']
|
|
|
|
|
for entry in vm_list.entries:
|
2020-04-08 05:18:56 +00:00
|
|
|
|
temp_uname = entry.uid
|
2020-03-03 07:55:30 +00:00
|
|
|
|
for i, vm in enumerate(vms):
|
|
|
|
|
vm_user = vm['UNAME']
|
|
|
|
|
vm_id = vm['ID']
|
|
|
|
|
vm_port = vm['TEMPLATE']['GRAPHICS'].get('PORT')
|
|
|
|
|
vm_host = vm['HISTORY_RECORDS']['HISTORY']['HOSTNAME']
|
|
|
|
|
if vm['UNAME'] == temp_uname:
|
|
|
|
|
#print(entry.uid, vm_id, vm_port, vm_host)
|
|
|
|
|
setconn(entry.uid, vm_id, vm_port, vm_host)
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
print(response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|
|
|
|
|
|