2020-01-31 18:05:25 +00:00
|
|
|
|
import json
|
2020-01-28 10:34:09 +00:00
|
|
|
|
|
|
|
|
|
from enum import IntEnum
|
2020-01-31 18:05:25 +00:00
|
|
|
|
from xmlrpc.client import ServerProxy as RPCClient
|
|
|
|
|
|
|
|
|
|
from xmltodict import parse
|
|
|
|
|
|
2020-01-28 10:34:09 +00:00
|
|
|
|
from config import config, etcd_client
|
2020-01-31 18:05:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Constants
|
|
|
|
|
ALL_VM_STATES = -1
|
|
|
|
|
START_ID = -1 # First id whatever it is
|
|
|
|
|
END_ID = -1 # Last id whatever it is
|
2020-01-28 10:34:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def put_under_list(obj):
|
|
|
|
|
if not isinstance(obj, list):
|
|
|
|
|
return [obj]
|
|
|
|
|
return obj
|
|
|
|
|
|
|
|
|
|
|
2020-01-31 18:05:25 +00:00
|
|
|
|
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
|
2020-01-28 10:34:09 +00:00
|
|
|
|
|
2020-01-31 18:05:25 +00:00
|
|
|
|
|
|
|
|
|
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
|
2020-01-28 10:34:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class VM:
|
2020-01-31 18:05:25 +00:00
|
|
|
|
def __init__(self, vm: dict):
|
|
|
|
|
self.id = vm.get('ID', None)
|
2020-01-28 10:34:09 +00:00
|
|
|
|
self.owner = {
|
2020-01-31 18:05:25 +00:00
|
|
|
|
'id': vm.get('UID', None),
|
|
|
|
|
'name': vm.get('UNAME', None),
|
|
|
|
|
'gname': vm.get('GNAME', None)
|
2020-01-28 10:34:09 +00:00
|
|
|
|
}
|
2020-01-31 18:05:25 +00:00
|
|
|
|
self.name = vm.get('NAME', None)
|
|
|
|
|
self.status = vm.get('STATE', None)
|
|
|
|
|
if self.status:
|
|
|
|
|
self.status = VMState(int(self.status)).name.lower()
|
2020-01-28 10:34:09 +00:00
|
|
|
|
|
2020-01-31 18:05:25 +00:00
|
|
|
|
template = vm['TEMPLATE']
|
2020-01-28 10:34:09 +00:00
|
|
|
|
|
2020-01-31 18:05:25 +00:00
|
|
|
|
self.disk = put_under_list(template.get('DISK', []))
|
|
|
|
|
self.graphics = template.get('GRAPHICS', {})
|
2020-01-28 10:34:09 +00:00
|
|
|
|
self.memory = template.get('MEMORY', None)
|
2020-01-31 18:05:25 +00:00
|
|
|
|
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', []))
|
2020-01-28 10:34:09 +00:00
|
|
|
|
|
|
|
|
|
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():
|
2020-01-31 18:05:25 +00:00
|
|
|
|
with RPCClient('https://opennebula.ungleich.ch:2634/RPC2') as rpc_client:
|
|
|
|
|
success, response, *_ = rpc_client.one.vmpool.infoextended(
|
|
|
|
|
config['oca']['client_secrets'], VmFilterFlag.AllResources.value, START_ID, END_ID, ALL_VM_STATES
|
|
|
|
|
)
|
|
|
|
|
if success:
|
|
|
|
|
vms = json.loads(json.dumps(parse(response)))['VM_POOL']['VM']
|
|
|
|
|
for i, vm in enumerate(vms):
|
|
|
|
|
vm_id = vm['ID']
|
|
|
|
|
etcd_client.put(f'/opennebula/vm/{vm_id}', vm)
|
|
|
|
|
|
|
|
|
|
parsed_vm = VM(vm)
|
|
|
|
|
etcd_client.put(f'/opennebula/parsed_vm/{parsed_vm.id}', parsed_vm.get_data())
|
|
|
|
|
else:
|
|
|
|
|
print(response)
|
2020-01-28 10:34:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|