def _write(self, key, data): path = self.prependPath(key) try: xshandle().write(self.transaction, path, data) except RuntimeError, ex: raise RuntimeError(ex.args[0], ('%s, while writing %s : %s' % (ex.args[1], path, str(data))))
def set_permissions(self, *args): if len(args) == 0: raise TypeError elif isinstance(args[0], str): self.callRebased(args[0], self.set_permissions, *args[1:]) else: if not self.path: raise RuntimeError('Cannot set permissions on the root') xshandle().set_permissions(self.transaction, self.path, list(args))
def remove(self, *args): """If no arguments are given, remove this transaction's path. Otherwise, treat each argument as a subpath to this transaction's path, and remove each of those instead. """ if len(args) == 0: xshandle().rm(self.transaction, self.path) else: for key in args: self._remove(key)
def list_recursive_(self, subdir, keys): ret = [] for key in keys: new_subdir = subdir + "/" + key l = xshandle().ls(self.transaction, new_subdir) if l: ret.append([key, self.list_recursive_(new_subdir, l)]) else: ret.append([key, xshandle().read(self.transaction, new_subdir)]) return ret
def list_recursive_(self, subdir, keys): ret = [] for key in keys: new_subdir = subdir + "/" + key l = xshandle().ls(self.transaction, new_subdir) if l: ret.append([key, self.list_recursive_(new_subdir, l)]) else: ret.append( [key, xshandle().read(self.transaction, new_subdir)]) return ret
def _read(self, key): path = self.prependPath(key) try: return xshandle().read(self.transaction, path) except RuntimeError, ex: raise RuntimeError(ex.args[0], '%s, while reading %s' % (ex.args[1], path))
def abort(self): if not self.in_transaction: return True self.in_transaction = False rc = xshandle().transaction_end(self.transaction, True) self.transaction = "0" return rc
def commit(self): if not self.in_transaction: raise RuntimeError self.in_transaction = False rc = xshandle().transaction_end(self.transaction, False) self.transaction = "0" return rc
def _get_permissions(self, key): path = self.prependPath(key) try: return xshandle().get_permissions(self.transaction, path) except RuntimeError, ex: raise RuntimeError( ex.args[0], '%s, while getting permissions from %s' % (ex.args[1], path))
def _get_permissions(self, key): path = self.prependPath(key) try: return xshandle().get_permissions(self.transaction, path) except RuntimeError, ex: raise RuntimeError(ex.args[0], '%s, while getting permissions from %s' % (ex.args[1], path))
def __init__(self, path = ""): assert path is not None self.in_transaction = False # Set this temporarily -- if this # constructor fails, then we need to # protect __del__. self.path = path.rstrip("/") self.transaction = xshandle().transaction_start() self.in_transaction = True
def __init__(self, path=""): self.in_transaction = False # Set this temporarily -- if this # constructor fails, then we need to # protect __del__. assert path is not None self.path = path.rstrip("/") self.transaction = xshandle().transaction_start() self.in_transaction = True
def __init__(self, receive_prefix = "data/host", send_prefix = "data/guest", data_prefix = "vm-data", *args, **kwargs): # pylint: disable=C0301 """Initialize a communication "bus" with the Xen Hypervisor. ### Description Sets up watches on paths we'll be receiving data on in xenstore and initializes pathing information used elsewhere. """ super(XenCommunicator, self).__init__(*args, **kwargs) self._receive_prefix = receive_prefix self._send_prefix = send_prefix self._network_prefix = data_prefix + "/networking" self._hostname_prefix = data_prefix + "/hostname" self._queue = Queue.Queue() self.xs = xs.xshandle() # pylint: disable=C0103 def xs_watch(path): logger.info("Received a watch even on %s", path) if path in [ self._receive_prefix, data_prefix ]: return True transaction = self.xs.transaction_start() message = self.xs.read(transaction, path) self.xs.transaction_end(transaction) logger.info("Received message, %s", message) self._queue.put((path, message)) transaction = self.xs.transaction_start() message = self.xs.rm(transaction, path) self.xs.transaction_end(transaction) return True self.watches = [] self.watches.append(xswatch(self._receive_prefix, xs_watch)) transaction = self.xs.transaction_start() entries = self.xs.ls(transaction, self._receive_prefix) self.xs.transaction_end(transaction) logger.debug("Missed messages: %s", entries) if entries is not None: for path in [ self._receive_prefix + "/" + entry for entry in entries ]: # pylint: disable=C0301 xs_watch(path)
def watchStart(): global watchThread global xs xslock.acquire() try: if watchThread: return xs = xshandle() watchThread = threading.Thread(name="Watcher", target=watchMain) watchThread.setDaemon(True) watchThread.start() finally: xslock.release()
def read(self, *args): """If no arguments are given, return the value at this transaction's path. If one argument is given, treat that argument as a subpath to this transaction's path, and return the value at that path. Otherwise, treat each argument as a subpath to this transaction's path, and return a list composed of the values at each of those instead. """ if len(args) == 0: return xshandle().read(self.transaction, self.path) if len(args) == 1: return self._read(args[0]) ret = [] for key in args: ret.append(self._read(key)) return ret
def list(self, *args): """If no arguments are given, list this transaction's path, returning the entries therein, or the empty list if no entries are found. Otherwise, treat each argument as a subpath to this transaction's path, and return the cumulative listing of each of those instead. """ if len(args) == 0: ret = xshandle().ls(self.transaction, self.path) if ret is None: return [] else: return ret else: ret = [] for key in args: ret.extend(self._list(key)) return ret
def _remove(self, key): path = self.prependPath(key) return xshandle().rm(self.transaction, path)
def _list(self, key): path = self.prependPath(key) l = xshandle().ls(self.transaction, path) if l: return map(lambda x: key + "/" + x, l) return []
def mkdir(self, *args): if len(args) == 0: xshandle().mkdir(self.transaction, self.path) else: for key in args: xshandle().mkdir(self.transaction, self.prependPath(key))
def __del__(self): if self.in_transaction: xshandle().transaction_end(self.transaction, True)