103
104
return {"status": status}
107
def _write_config_dict(dct):
108
conf_file = file(config_file_path, "w")
109
json.dump(dct, conf_file)
113
def _get_config_dict():
114
"""Returns a dict containing the key/values in the config file.
115
If the file doesn't exist, it is created, and an empty dict
119
conf_file = file(config_file_path)
120
config_dct = json.load(conf_file)
126
_write_config_dict(config_dct)
131
def get_config(self, arg_dict):
132
"""Return the value stored for the specified key, or None if no match."""
133
conf = _get_config_dict()
134
params = arg_dict["params"]
136
dct = json.loads(params)
148
def set_config(self, arg_dict):
149
"""Write the specified key/value pair, overwriting any existing value."""
150
conf = _get_config_dict()
151
params = arg_dict["params"]
153
dct = json.loads(params)
159
# Delete the key, if present
162
conf.update({key: val})
163
_write_config_dict(conf)
166
def _power_action(action):
167
host_uuid = _get_host_uuid()
168
# Host must be disabled first
169
result = _run_command("xe host-disable")
171
raise pluginlib.PluginError(result)
172
# All running VMs must be shutdown
173
result = _run_command("xe vm-shutdown --multiple power-state=running")
175
raise pluginlib.PluginError(result)
176
cmds = {"reboot": "xe host-reboot", "startup": "xe host-power-on",
177
"shutdown": "xe host-shutdown"}
178
result = _run_command(cmds[action])
179
# Should be empty string
181
raise pluginlib.PluginError(result)
182
return {"power_action": action}
186
def host_reboot(self, arg_dict):
187
"""Reboots the host."""
188
return _power_action("reboot")
192
def host_shutdown(self, arg_dict):
193
"""Reboots the host."""
194
return _power_action("shutdown")
198
def host_start(self, arg_dict):
199
"""Starts the host. Currently not feasible, since the host
200
runs on the same machine as Xen.
202
return _power_action("startup")
107
206
def host_data(self, arg_dict):
108
207
"""Runs the commands on the xenstore host to return the current status
156
258
# out["host_suspend-image-sr-uuid"] = dct.get("suspend-image-sr-uuid", "")
157
259
# out["host_crash-dump-sr-uuid"] = dct.get("crash-dump-sr-uuid", "")
158
260
# out["host_local-cache-sr"] = dct.get("local-cache-sr", "")
261
out["enabled"] = dct.get("enabled", "true") == "true"
159
262
out["host_memory"] = omm = {}
160
263
omm["total"] = safe_int(dct.get("memory-total", ""))
161
264
omm["overhead"] = safe_int(dct.get("memory-overhead", ""))
217
320
if __name__ == "__main__":
218
321
XenAPIPlugin.dispatch(
219
322
{"host_data": host_data,
220
"set_host_enabled": set_host_enabled})
323
"set_host_enabled": set_host_enabled,
324
"host_shutdown": host_shutdown,
325
"host_reboot": host_reboot,
326
"host_start": host_start,
327
"get_config": get_config,
328
"set_config": set_config})