Browse Source

remove options dict from state state jsons/apis for simiplicity

master
dave 5 years ago
parent
commit
dfb8ca7c01
  1. 64
      example/ubuntu.json
  2. 1
      requirements.txt
  3. 2
      zhypervisor/api/api.py
  4. 2
      zhypervisor/clients/qmachine.py
  5. 24
      zhypervisor/daemon.py
  6. 13
      zhypervisor/machine.py
  7. 6
      zhypervisor/util.py

64
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
}
}

1
requirements.txt

@ -0,0 +1 @@
CherryPy==8.5.0

2
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:

2
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):

24
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:

13
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

6
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()

Loading…
Cancel
Save