From dfb8ca7c0171a749f41278c8cfe81e391b5c9897 Mon Sep 17 00:00:00 2001 From: dave Date: Wed, 28 Dec 2016 23:52:39 -0800 Subject: [PATCH] remove options dict from state state jsons/apis for simiplicity --- example/ubuntu.json | 64 ++++++++++++++++----------------- requirements.txt | 1 + zhypervisor/api/api.py | 2 +- zhypervisor/clients/qmachine.py | 2 +- zhypervisor/daemon.py | 24 ++++++------- zhypervisor/machine.py | 13 +++---- zhypervisor/util.py | 6 ++-- 7 files changed, 51 insertions(+), 61 deletions(-) create mode 100644 requirements.txt diff --git a/example/ubuntu.json b/example/ubuntu.json index de0a70c..7352896 100644 --- a/example/ubuntu.json +++ b/example/ubuntu.json @@ -1,38 +1,34 @@ { "machine_id": "ubuntu", - "spec": { - "options": { - "type": "q", - "autostart": true, - "respawn": true - }, - "properties": { - "cores": 2, - "mem": 1024, - "drives": [ - { - "disk": "ubuntu-root", - "index": 0, - "if": "virtio" - }, - { - "disk": "multipreseed-14.04.iso", - "index": 1, - "media": "cdrom" - } - ], - "netifaces": [ - { - "type": "nic", - "vlan": 0, - "model": "e1000", - "macaddr": "82:25:60:41:07:98" - }, - { - "type": "tap" - } - ], - "vnc": 10 - } + "properties": { + "type": "q", + "autostart": true, + "respawn": true, + "cores": 2, + "mem": 1024, + "drives": [ + { + "disk": "ubuntu-root", + "index": 0, + "if": "virtio" + }, + { + "disk": "multipreseed-14.04.iso", + "index": 1, + "media": "cdrom" + } + ], + "netifaces": [ + { + "type": "nic", + "vlan": 0, + "model": "e1000", + "macaddr": "82:25:60:41:07:98" + }, + { + "type": "tap" + } + ], + "vnc": 10 } } diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..d17738d --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +CherryPy==8.5.0 diff --git a/zhypervisor/api/api.py b/zhypervisor/api/api.py index 53bb5fe..78be7ca 100644 --- a/zhypervisor/api/api.py +++ b/zhypervisor/api/api.py @@ -235,7 +235,7 @@ class ZApiMachines(): machine = {"machine_id": _machine_id, "_status": machine_spec.machine.get_status()} if not summary: - machine.update({"spec": machine_spec.serialize()}) + machine.update({"properties": machine_spec.serialize()}) machines[_machine_id] = machine if machine_id is not None: diff --git a/zhypervisor/clients/qmachine.py b/zhypervisor/clients/qmachine.py index 553e783..7b1eeae 100644 --- a/zhypervisor/clients/qmachine.py +++ b/zhypervisor/clients/qmachine.py @@ -46,7 +46,7 @@ class QMachine(Machine): proc.wait() logging.info("qemu process has exited") self.proc = None - if not self.block_respawns and self.spec.options.get("respawn", False): + if not self.block_respawns and self.spec.properties.get("respawn", False): self.start_machine() def stop_machine(self): diff --git a/zhypervisor/daemon.py b/zhypervisor/daemon.py index af6d321..6366a66 100644 --- a/zhypervisor/daemon.py +++ b/zhypervisor/daemon.py @@ -57,7 +57,7 @@ class ZHypervisorDaemon(object): Load all disks and ensure reachability """ for disk in self.state.get_disks(): - self.add_disk(disk["disk_id"], {"options": disk["options"], "properties": disk["properties"]}) + self.add_disk(disk["disk_id"], disk["properties"]) def init_machines(self): """ @@ -65,11 +65,11 @@ class ZHypervisorDaemon(object): """ for machine_info in self.state.get_machines(): machine_id = machine_info["machine_id"] - self.add_machine(machine_id, machine_info["spec"]) + self.add_machine(machine_id, machine_info["properties"]) # Launch if machine is an autostarted machine machine = self.machines[machine_id] - if machine.options.get("autostart", False) and machine.machine.get_status() == "stopped": + if machine.properties.get("autostart", False) and machine.machine.get_status() == "stopped": machine.start() def signal_handler(self, signum, frame): @@ -106,8 +106,8 @@ class ZHypervisorDaemon(object): Create a disk """ assert disk_id not in self.disks, "Cannot update disks, only create supported" - disk_type = disk_spec["options"]["type"] - disk_datastore = disk_spec["options"]["datastore"] + disk_type = disk_spec["type"] + disk_datastore = disk_spec["datastore"] datastore = self.datastores[disk_datastore] if disk_type == "qdisk": disk = QDisk(datastore, disk_id, disk_spec) @@ -137,14 +137,13 @@ class ZHypervisorDaemon(object): """ Create or update a machine. :param machine_id: alphanumeric id of machine to modify/create - :param machine_spec: dictionary of machine options - see example/ubuntu.json + :param machine_spec: dictionary of machine properties - see example/ubuntu.json :param write: commit machinge changes to on-disk state """ # Find / create the machine if machine_id in self.machines: machine = self.machines[machine_id] - machine.options = machine_spec["options"] - machine.properties = machine_spec["properties"] + machine.properties = machine_spec else: machine = MachineSpec(self, machine_id, machine_spec) self.machines[machine_id] = machine @@ -190,7 +189,7 @@ class ZDataStore(object): except: if init_ok: with open(metainfo_path, "w") as f: - json.dump({}, f) + json.dump({}, f, sort_keys=True, indent=4) else: raise logging.info("Initialized datastore %s at %s", name, self.root_path) @@ -229,7 +228,7 @@ class ZConfig(object): """ with open(os.path.join(self.machine_data_dir, "{}.json".format(machine_id)), "w") as f: json.dump({"machine_id": machine_id, - "spec": machine_spec}, f, indent=4) + "properties": machine_spec}, f, indent=4, sort_keys=True) def write_machine_o(self, machine_obj): """ @@ -258,8 +257,7 @@ class ZConfig(object): def write_disk(self, disk_id, disk_spec): with open(os.path.join(self.disk_data_dir, "{}.json".format(disk_id)), "w") as f: disk = {"disk_id": disk_id, - "options": disk_spec["options"], - "properties": disk_spec["properties"]} + "properties": disk_spec} json.dump(disk, f, indent=4) def remove_disk(self, disk_id): @@ -283,7 +281,7 @@ def main(): "default": { "path": "/opt/z/datastore/machines/" } - }}, f, indent=4) + }}, f, indent=4, sort_keys=True) return with open(args.config) as f: diff --git a/zhypervisor/machine.py b/zhypervisor/machine.py index 2fd41d2..9bec820 100644 --- a/zhypervisor/machine.py +++ b/zhypervisor/machine.py @@ -16,14 +16,13 @@ class MachineSpec(object): self.master = master self.machine_id = machine_id - self.options = spec["options"] - self.properties = spec["properties"] + self.properties = spec # TODO replace if/else with better system - if self.options["type"] == "q": + if self.properties["type"] == "q": self.machine = QMachine(self) else: - raise Exception("Unknown machine type: {}".format(self.options["type"])) + raise Exception("Unknown machine type: {}".format(self.properties["type"])) def start(self): """ @@ -43,12 +42,10 @@ class MachineSpec(object): """ Write the machine's config to disk """ - self.master.add_machine(self.machine_id, {"options": self.options, "properties": self.properties}, - write=True) + self.master.add_machine(self.machine_id, self.properties, write=True) def serialize(self): """ Return a serializable form of this machine's specs """ - return {"options": self.options, - "properties": self.properties} + return self.properties diff --git a/zhypervisor/util.py b/zhypervisor/util.py index a22fccb..db8e3c7 100644 --- a/zhypervisor/util.py +++ b/zhypervisor/util.py @@ -70,8 +70,7 @@ class ZDisk(object): def __init__(self, datastore, disk_id, spec): self.datastore = datastore self.disk_id = disk_id - self.options = spec["options"] - self.properties = spec["properties"] + self.properties = spec self.validate() def validate(self): @@ -88,8 +87,7 @@ class ZDisk(object): os.makedirs(self.get_path(), exist_ok=True) def serialize(self): - return {"options": self.options, - "properties": self.properties} + return self.properties def delete(self): raise NotImplemented()