~ttx/nova/d4-merge

« back to all changes in this revision

Viewing changes to plugins/xenserver/xenapi/etc/xapi.d/plugins/xenhost

  • Committer: Thierry Carrez
  • Date: 2011-08-23 12:23:07 UTC
  • mfrom: (1130.75.258 nova)
  • Revision ID: thierry@openstack.org-20110823122307-f0vtuyg1ikc14n87
Merge diablo-4 development from trunk (rev1479)

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
pluginlib.configure_logging("xenhost")
40
40
 
41
41
host_data_pattern = re.compile(r"\s*(\S+) \([^\)]+\) *: ?(.*)")
 
42
config_file_path = "/usr/etc/xenhost.conf"
42
43
 
43
44
 
44
45
def jsonify(fnc):
103
104
    return {"status": status}
104
105
 
105
106
 
 
107
def _write_config_dict(dct):
 
108
    conf_file = file(config_file_path, "w")
 
109
    json.dump(dct, conf_file)
 
110
    conf_file.close()
 
111
 
 
112
 
 
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
 
116
    is returned.
 
117
    """
 
118
    try:
 
119
        conf_file = file(config_file_path)
 
120
        config_dct = json.load(conf_file)
 
121
        conf_file.close()
 
122
    except IOError:
 
123
        # File doesn't exist
 
124
        config_dct = {}
 
125
        # Create the file
 
126
        _write_config_dict(config_dct)
 
127
    return config_dct
 
128
 
 
129
 
 
130
@jsonify
 
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"]
 
135
    try:
 
136
        dct = json.loads(params)
 
137
    except Exception, e:
 
138
        dct = params
 
139
    key = dct["key"]
 
140
    ret = conf.get(key)
 
141
    if ret is None:
 
142
        # Can't jsonify None
 
143
        return "None"
 
144
    return ret
 
145
 
 
146
 
 
147
@jsonify
 
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"]
 
152
    try:
 
153
        dct = json.loads(params)
 
154
    except Exception, e:
 
155
        dct = params
 
156
    key = dct["key"]
 
157
    val = dct["value"]
 
158
    if val is None:
 
159
        # Delete the key, if present
 
160
        conf.pop(key, None)
 
161
    else:
 
162
        conf.update({key: val})
 
163
    _write_config_dict(conf)
 
164
 
 
165
 
 
166
def _power_action(action):
 
167
    host_uuid = _get_host_uuid()
 
168
    # Host must be disabled first
 
169
    result = _run_command("xe host-disable")
 
170
    if result:
 
171
        raise pluginlib.PluginError(result)
 
172
    # All running VMs must be shutdown
 
173
    result = _run_command("xe vm-shutdown --multiple power-state=running")
 
174
    if result:
 
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
 
180
    if result:
 
181
        raise pluginlib.PluginError(result)
 
182
    return {"power_action": action}
 
183
 
 
184
 
 
185
@jsonify
 
186
def host_reboot(self, arg_dict):
 
187
    """Reboots the host."""
 
188
    return _power_action("reboot")
 
189
 
 
190
 
 
191
@jsonify
 
192
def host_shutdown(self, arg_dict):
 
193
    """Reboots the host."""
 
194
    return _power_action("shutdown")
 
195
 
 
196
 
 
197
@jsonify
 
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.
 
201
    """
 
202
    return _power_action("startup")
 
203
 
 
204
 
106
205
@jsonify
107
206
def host_data(self, arg_dict):
108
207
    """Runs the commands on the xenstore host to return the current status
115
214
    # We have the raw dict of values. Extract those that we need,
116
215
    # and convert the data types as needed.
117
216
    ret_dict = cleanup(parsed_data)
 
217
    # Add any config settings
 
218
    config = _get_config_dict()
 
219
    ret_dict.update(config)
118
220
    return ret_dict
119
221
 
120
222
 
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})