update db_export

This commit is contained in:
kjg 2020-02-28 00:19:29 +09:00
commit 0498c2fb02
3 changed files with 83 additions and 33 deletions

View file

@ -2,25 +2,17 @@ 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 ldap_list3 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']
f = open("list",'w')
f.close()
def put_under_list(obj):
if not isinstance(obj, list):
return [obj]
return obj
opnserver = config['oca']['opn_server']
class VMState(IntEnum):
INIT = 0
@ -60,49 +52,33 @@ class VM:
template = vm['TEMPLATE']
self.disk = put_under_list(template.get('DISK', []))
self.graphics = template.get('GRAPHICS', {})
self.memory = template.get('MEMORY', None)
self.nic = put_under_list(template.get('NIC', []))
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),
}
self.snapshots = put_under_list(vm.get('SNAPSHOTS', []))
def get_data(self):
return {
attr: getattr(self, attr)
for attr in dir(self)
if not attr.startswith('__') and not callable(getattr(self, attr))
}
def main():
with RPCClient(confi['oca']['server']) as rpc_client:
with RPCClient(opnserver) as rpc_client:
success, response, *_ = rpc_client.one.vmpool.infoextended(
session_string , -2, -1, -1, 3
session_string , VmFilterFlag.AllResources.value, START_ID, END_ID, VMState.ACTIVE.value
)
if success:
vms = json.loads(json.dumps(parse(response)))['VM_POOL']['VM']
f = open("list",'w')
for entry in vm_list.entries:
temp_uname = entry.mail
#print(temp_uname)
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']
#print(vm_user, vm_id, vm_port, vm_host)
if vm['UNAME'] == temp_uname:
print(entry.uid, vm_id, vm_port, vm_host, file=f)
#temp_line = entry.uid
#f.write(temp_line)
#print(entry.uid, vm_id, vm_port, vm_host)
setconn(entry.uid, vm_id, vm_port, vm_host)
#print(config['ldap']['admin_dn'])
f.close()
else:
print(response)