def run(self): log.info('cmd task%d starts working ...' % self.que) while True: with self.con: while True: # try to get a task if len(self.queue): task = self.queue[0] del self.queue[0] break self.con.wait() key, message, ret_queue, ignore = task log.info( '%s: get a message {key: %s, message: %s, ignore:' ' %s}, from queue%d. ' 'current queue length: %d' % (self.name, key, message, ignore, self.que, len(self.queue))) if ignore: rc, output = 0, "" else: if isinstance(message[0], list): for args in message: rc, output = call_system_sh(args) if rc != 0: break else: rc, output = call_system_sh(message) if ret_queue: ret_queue.put((rc, output))
def init_system(force=False): if not force: args = [ SYSCONF_SCRIPT, 'CHECK_SYSTEM_BOOTUP', ] rc, output = call_system_sh(args) log.info('System bootup %ss ago', output) if rc == 0 and int(output) > SYSTEM_BOOTUP_WINDOW: log.info('Skip init_system.') return global is_system_bootup is_system_bootup = True log.info('Init system now, clear all routers ...') args = [ ROUTER_SCRIPT, 'delete', 'all_routers', ] rc, output = call_system_sh(args) if rc != 0: log.error('Init system failed, exit after 10 seconds.') time.sleep(10) sys.exit(-1)
def update_bridge(bridge, bridge_name): log.debug('bridge_name: %s' % bridge_name) if bridge.name == UPLINK_BRIDGE and \ bridge.ip is not None and bridge.gateway is not None: args = [ SYSCONF_SCRIPT, bridge.name, bridge.ip.address, bridge.ip.netmask, bridge.gateway, ] if bridge.name == TUNNEL_BRIDGE and bridge.qos is not None: args = [ TUNNEL_SCRIPT, 'set-qos', str(bridge.qos.min_bandwidth), str(bridge.qos.max_bandwidth), ] rc, output = call_system_sh(args) if rc != 0: return json_response(status=SERVER_ERROR, description=output), HTTP_INTERNAL_SERVER_ERROR return json_response(status=SUCCESS), HTTP_OK
def delete_ovsnat(ovsnat_id): id_seg = ovsnat_id.split('-') if id_seg is None or len(id_seg) != 3: return json_response(status=RESOURCE_NOT_FOUND), HTTP_NOT_FOUND if id_seg[0] not in OvsNat.bridge.choices: return json_response(status=RESOURCE_NOT_FOUND), HTTP_NOT_FOUND bridge = id_seg[0] if not id_seg[1].isdigit() or int(id_seg[1]) < MIN_PORT or \ int(id_seg[1]) > MAX_PORT: return json_response(status=RESOURCE_NOT_FOUND), HTTP_NOT_FOUND target_port = id_seg[1] if not is_valid_ip(id_seg[2]): return json_response(status=RESOURCE_NOT_FOUND), HTTP_NOT_FOUND target_ip = id_seg[2] args = [ OVSNAT_SCRIPT, 'del-nat', bridge, target_port, target_ip, ] rc, output = call_system_sh(args) if rc != 0: return json_response(status=SERVER_ERROR, description=output), HTTP_INTERNAL_SERVER_ERROR return json_response(status=SUCCESS), HTTP_OK
def delete_tunnel_flow(flow_id): id_seg = flow_id.split('-') if id_seg is None or len(id_seg) != 2: return json_response(status=RESOURCE_NOT_FOUND), HTTP_NOT_FOUND if not id_seg[0].isdigit() or int(id_seg[0]) < 1 or \ int(id_seg[0]) > MAX_SUBNET: return json_response(status=RESOURCE_NOT_FOUND), HTTP_NOT_FOUND subnet_id = int(id_seg[0]) if not id_seg[1].isdigit() or int(id_seg[1]) < 0 or \ int(id_seg[1]) > MAX_TUNNEL_MAC: return json_response(status=RESOURCE_NOT_FOUND), HTTP_NOT_FOUND vif_id = int(id_seg[1]) args = [ TUNNEL_SCRIPT, 'clear-policy', TUNNEL_BRIDGE, TUNNEL_FLOW_COOKIE_FORMAT % (subnet_id, vif_id), str(-1), ] rc, output = call_system_sh(args) if rc != 0: log.error(output) return json_response(status=SERVER_ERROR), HTTP_INTERNAL_SERVER_ERROR return json_response(status=SUCCESS), HTTP_OK
def create_tunnel_flow(tunnel_flow): log.debug('tunnel_flow: %r' % tunnel_flow.to_native()) if tunnel_flow.vif_id == 0: args = [ TUNNEL_SCRIPT, 'set-vl2-policy', TUNNEL_FLOW_COOKIE_FORMAT % (tunnel_flow.subnet_id, 0), str(tunnel_flow.subnet_id), str(tunnel_flow.vlantag), ] else: args = [ TUNNEL_SCRIPT, 'set-vif-policy', TUNNEL_FLOW_COOKIE_FORMAT % ( tunnel_flow.subnet_id, tunnel_flow.vif_id), str(tunnel_flow.subnet_id), str(tunnel_flow.vlantag), tunnel_flow.vif_mac, ] rc, output = call_system_sh(args) if rc != 0: log.error(output) return json_response(status=SERVER_ERROR), HTTP_INTERNAL_SERVER_ERROR return json_response(status=SUCCESS), HTTP_OK
def get_nsp_stat(realtime, hit_time): cache_index = 1 if realtime is True else 0 now = time.time() args = [NSP_STAT_SCRIPT, 'realtime' if realtime else 'general'] with sys_cmd_lock: rc, output = call_system_sh(args) if rc != 0: return json_response( status=SERVER_ERROR, description=output), HTTP_INTERNAL_SERVER_ERROR try: nsp_stat = NspStat(json.loads(output)) nsp_stat.validate() json_resp = json_response(status=SUCCESS, data=nsp_stat.to_primitive(), type="NSP_STAT") except Exception as e: log.error(e) log.error(output) return json_response(status=SERVER_ERROR, description=str(e)), HTTP_INTERNAL_SERVER_ERROR with nsp_stat_cache_lock: nsp_stat_cache[cache_index] = NspStatCacheNode(now, hit_time, json_resp) return json_resp, HTTP_OK
def delete_tunnel(remote_ip): PingTask.del_peer_ip(remote_ip) args = [ TUNNEL_SCRIPT, 'del-tunnel', remote_ip, ] rc, output = call_system_sh(args) if rc != 0: return json_response( status=SERVER_ERROR, description=output ), HTTP_INTERNAL_SERVER_ERROR return json_response(status=SUCCESS), HTTP_OK
def create_tunnel(tunnel): log.debug('tunnel: %r' % tunnel.to_native()) PingTask.add_peer_ip(tunnel.remote_ip) args = [ TUNNEL_SCRIPT, 'add-tunnel', tunnel.protocol, tunnel.remote_ip, 'flow', ] rc, output = call_system_sh(args) if rc != 0: return json_response( status=SERVER_ERROR, description=output ), HTTP_INTERNAL_SERVER_ERROR return json_response(status=SUCCESS), HTTP_OK
def create_ovsnat(ovsnat): log.debug('ovsnat: %r' % ovsnat.to_native()) args = [ OVSNAT_SCRIPT, 'add-nat', ovsnat.bridge, str(ovsnat.port), ovsnat.ip, ovsnat.mac, str(ovsnat.target_port), ovsnat.target_ip, ovsnat.target_mac, ] rc, output = call_system_sh(args) if rc != 0: return json_response(status=SERVER_ERROR, description=output), HTTP_INTERNAL_SERVER_ERROR return json_response(status=SUCCESS), HTTP_OK