def start(host, vmid, ifaces, args=[]): assert getState(host, vmid) == generic.State.PREPARED, "VM already running" ilist = ["-i repy%d.%s,alias=%s" % (vmid, util.identifier(i), util.identifier(i)) for i in ifaces] if isinstance(args, basestring): args = [args] alist = [util.escape(a) for a in args] host.execute("tomato-repy -p %s -v %s %s > %s 2>&1 & echo $! > %s" % (_imagePath(vmid), " ".join(ilist), " ".join(alist), _logFile(vmid), _pidFile(vmid) )) assert getState(host, vmid) == generic.State.STARTED, "Repy device failed to start" for i in ifaces: waitForInterface(host, vmid, i)
def __init__(self, queue, input_thread, address, naxos_path, http_addr): self.id = None self.address = address self.naxos_path = naxos_path self.http_addr = http_addr self.input_thread = input_thread self.selector = selectors.DefaultSelector() self.queue = queue self.selector.register(queue, selectors.EVENT_READ) self.results = {} self.overlay_edges = set() self.running = True try: self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.connect(self.address) self.selector.register( self.sock, selectors.EVENT_READ | selectors.EVENT_WRITE) self.id = util.identifier(*self.sock.getsockname()) except (ConnectionRefusedError, ConnectionAbortedError, TimeoutError) as exception: sys.exit('Could not establish connection to %s: %s' % (self.address, exception)) self.periodic_runner = PeriodicRunner() self.periodic_runner.start() self.conn = Connection(self.sock, known=True) self.send({'do': 'client_hello', 'http_addr': self.http_addr}) self.input_thread.unblock()
def connect_to_node(self, addr, first_message='hello'): """Try to connect to another network node under the address addr. Send a Message of type first_message after successfully connecting. This first message also contains this node's listening address. Returns: Remote address of the new socket on success, None on failure. """ log.info('Trying to connect: %s', addr) try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect(addr) events = selectors.EVENT_READ | selectors.EVENT_WRITE self.selector.register(sock, events) conn = Connection(sock, known=True) self.connections[addr] = conn self.send(identifier(*addr), { 'do': first_message, 'listen_addr': self.listen_addr, }) return addr except (ConnectionRefusedError, ConnectionAbortedError, TimeoutError) as error: log.info('Could not establish connection to %s: %s' % (addr, error)) return None
def _remoteDir(name): return "%s/%s" % (config.REMOTE_DIR, util.identifier(name))
def interfaceExists(host, iface): import fileutil return fileutil.existsDir(host, "/sys/class/net/%s" % util.identifier(iface))
def interfaceBridge(host, iface): try: return util.lines(host.execute("[ -d /sys/class/net/%s/brport/bridge ] && basename $(readlink /sys/class/net/%s/brport/bridge)" % (util.identifier(iface), util.identifier(iface))))[0] except exceptions.CommandError: return False
def bridgeInterfaces(host, bridge): assert bridgeExists(host, bridge), "Bridge does not exist: %s" % bridge return host.execute("ls /sys/class/net/%s/brif" % util.identifier(bridge)).split()
def getTxBytes(host, iface): if not interfaceExists(host, iface): return 0.0 return int(host.execute("[ -f /sys/class/net/%s/statistics/tx_bytes ] && cat /sys/class/net/%s/statistics/tx_bytes || echo 0" % (util.identifier(iface), util.identifier(iface))))
def bridgeExists(host, bridge): import fileutil return fileutil.existsDir(host, "/sys/class/net/%s/brif" % util.identifier(bridge))
def iptablesRemoveRules(host, device): host.execute ( "iptables -S INPUT | fgrep 'i %s ' | sed 's/-A /-D /' | while read rule; do iptables $rule; done" % util.identifier(device) ) host.execute ( "ip6tables -S INPUT | fgrep 'i %s ' | sed 's/-A /-D /' | while read rule; do ip6tables $rule; done" % util.identifier(device) )
def setHostname(host, vmid, hostname): assert getState(host, vmid) != generic.State.CREATED, "VM not prepared" _vzctl(host, vmid, "set", ["--hostname", "%s" % util.identifier(hostname), "--save"])
def get_identifier(self): return util.identifier(*self.remote_listen_addr ) if self.remote_listen_addr is not None else -1
def iptablesRemoveRules(host, device): try: host.execute ( "iptables -S INPUT | fgrep 'i %s ' | sed 's/-A /-D /' | while read rule; do iptables $rule; done" % util.identifier(device) ) host.execute ( "ip6tables -S INPUT | fgrep 'i %s ' | sed 's/-A /-D /' | while read rule; do ip6tables $rule; done" % util.identifier(device) ) except exceptions.CommandError, exc: if "Resource temporarily unavailable" in exc.errorMessage: import time time.sleep(0.1) iptablesRemoveRules(host, device) else: raise
def unique_id_from_own_addr(self): """Deterministically generates a single integer ID from this network node's address.""" return identifier(*self.listen_addr)