remove options dict from state state jsons/apis for simiplicity

This commit is contained in:
dave 2016-12-28 23:52:39 -08:00
parent 4d2723e048
commit dfb8ca7c01
7 changed files with 51 additions and 61 deletions

View File

@ -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 Normal file
View File

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

View File

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

View File

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

View File

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

View File

@ -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

View File

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