def _get_tree (path): """ return a dict , parsed from xenstore-ls """ cmd = "xenstore-ls %s" % (path) out = call_cmd (cmd) lines = out.split ("\n") _static = { 'node': {}, 'last_key': None, 'last_value': None, 'last_space': 0, } stack = [] def __process (space, key, value): #print _static['last_space'], space, _static['last_key'], _static['last_value'], key, value if _static['last_key'] is None: _static['last_space'] = 0 elif _static['last_value'] == "" and _static['last_space'] + 1 == space: _static['node'][_static['last_key']] = {} stack.append (_static['node']) _static['node'] = _static['node'][_static['last_key']] elif _static['last_space'] > space: _static['node'][_static['last_key']] = _static['last_value'] for i in xrange (0, _static['last_space'] - space): _static['node'] = stack.pop () else: assert _static['last_space'] == space _static['node'][_static['last_key']] = _static['last_value'] _static['last_key'] = key _static['last_value'] = value _static['last_space'] = space return reg_key_value = re.compile (r"^(\s*)([\w\-]+)\s*=\s*\"(.*)\".*$") for line in lines: om = re.match (reg_key_value, line) if not om: raise Exception ("unexpect format: %s" % (line)) space = len (om.group (1)) key = om.group (2) value = om.group (3) __process (space, key, value) __process (0, None, None) return _static['node']
def destroy (domain): call_cmd ("xm destroy %s" % (domain))
def shutdown (domain): call_cmd ("xm shutdown %s" % (domain))
def restore (path): call_cmd ("xm restore %s" % (path))
def save (domain, path): call_cmd ("xm save %s %s" % (domain, path))
def create (xen_config): if not os.path.exists (xen_config): raise Exception ("%s not exist" % (xen_config)) call_cmd ("xm create %s" % (xen_config))
def _read (path): return call_cmd ("xenstore-read %s" % (path))
def _list (path): out = call_cmd ("xenstore-list %s" % (path)) return out.split ("\n")
def mem_total (): """ return mem total (MB) in Xen """ out = call_cmd ("xl info | grep total_memory") return int(out.strip("\r\n").split (":")[1].strip ())
def mem_free (): """ return mem free (MB) in Xen """ out = call_cmd ("xl info | grep free_memory") return int(out.strip("\r\n").split (":")[1].strip ())