diff --git a/opennebula-vm-etcd/config.py b/opennebula/config.py similarity index 100% rename from opennebula-vm-etcd/config.py rename to opennebula/config.py diff --git a/opennebula-vm-etcd/etcd_wrapper.py b/opennebula/etcd_wrapper.py similarity index 100% rename from opennebula-vm-etcd/etcd_wrapper.py rename to opennebula/etcd_wrapper.py diff --git a/opennebula/get-vms.py b/opennebula/get-vms.py new file mode 100644 index 0000000..5839922 --- /dev/null +++ b/opennebula/get-vms.py @@ -0,0 +1,103 @@ +import json +import os + +from enum import IntEnum +from xmlrpc.client import ServerProxy as RPCClient + +from xmltodict import parse + +# Constants +ALL_VM_STATES = -1 +START_ID = -1 # First id whatever it is +END_ID = -1 # Last id whatever it is + + +def put_under_list(obj): + if not isinstance(obj, list): + return [obj] + return obj + + +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.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(): + username = os.environ['ONE_USERNAME'] + password = os.environ['ONE_PASSWORD'] + one_auth = f"{username}:{password}" + + with RPCClient('https://opennebula.ungleich.ch:2634/RPC2') as rpc_client: + success, response, *_ = rpc_client.one.vmpool.infoextended( + one_auth, + 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'] + print(f"id = {vm_id}") + parsed_vm = VM(vm) + print(f"vm = {parsed_vm}") + else: + print(response) + + +if __name__ == "__main__": + main() diff --git a/opennebula-vm-etcd/put-vm-info-into-etcd.py b/opennebula/put-vm-info-into-etcd.py similarity index 100% rename from opennebula-vm-etcd/put-vm-info-into-etcd.py rename to opennebula/put-vm-info-into-etcd.py diff --git a/opennebula-vm-etcd/vm-queries.py b/opennebula/vm-queries.py similarity index 100% rename from opennebula-vm-etcd/vm-queries.py rename to opennebula/vm-queries.py