print argsmodel.schema_help(None) elif not crud and not option and len(args) > 0 and args[0].endswith('yaml'): # Batch operation for v in args: if v.endswith('yaml'): if os.path.exists(os.path.join(NLAN_ETC, 'state', v)): cmd_list = yamldiff.crud_diff(v, git=git) if len(cmd_list) != 0: main(router=router, operation='--batch', cmd_list=cmd_list, loglevel=loglevel, verbose=verbose, mime=mime) if git == 0: cmdutil.check_cmd('git', GIT_OPTIONS, 'add', v) cmdutil.check_cmd('git', GIT_OPTIONS, 'commit -m updated') elif not crud and not option and len(args) > 0: # NLAN rpc module execution main(router=router, doc=args, loglevel=loglevel, verbose=verbose, mime=mime) elif crud and len(args) > 0: # CRUD operation operation = option.lstrip('-') doc = str(argsmodel.parse_args(operation, args[0], *args[1:])) main(router=router, operation=option, doc=doc, loglevel=loglevel, verbose=verbose, mime=mime) elif options.secondary_ip: print "adding a secondary IP address..." add_ip = lambda container, ip_and_mask: cmdutil.check_cmd('docker exec', container, 'ip address add', ip_and_mask, 'dev eth0') #del_ip = lambda container, ip_and_mask: cmdutil.check_cmd('docker exec', container, 'ip address del', ip_and_mask, 'dev eth0') for router, attr in ROSTER.iteritems(): if 'docker' in attr and attr['docker']: add_ip(router, attr['host']+'/16') elif options.flush_arp: print "flushing arp table..." #flush_arp = lambda ip: cmdutil.check_cmd('sudo arp -d', ip) #for router, attr in ROSTER.iteritems(): # if 'docker' in attr and attr['docker']: # flush_arp(attr['host']) cmdutil.check_cmd('sudo ip neigh flush all')
_init(loglevel=loglevel) if options.schema: if len(args) == 1: print argsmodel.schema_help(args[0]) else: print argsmodel.schema_help(None) sys.exit(0) data = None if operation and len(args) == 0: # Obtains data from nlan.py via SSH data = sys.stdin.read().replace('"','') elif operation and len(args) > 0: # Obtains data from command arguments data = argsmodel.parse_args(operation, args[0], *args[1:]) else: # RPC w/ command arguments data = args operation = 'rpc_args' exit = 0 if options.init_action: __n__['init'] = options.init_action exit = _linux_init() else: __n__['init'] = False exit = _route(operation=operation, data=data) __n__['logger'].info('NLAN Agent execution completed') stdout.seek(0) sys.stdout.write(stdout.read())
def __call__(self, environ, start_response): # Request method method = environ['REQUEST_METHOD'] # /// URLs /// # /<router>/config/<module>/<_index> # /<router>/rpc/<module>/<func> path = environ['PATH_INFO'].split('/') router = None module_type = None # 'config' or 'rpc' module = None # name of a config module or a rpc module _index = None module_func = None if len(path) > 1: router = path[1] if len(path) > 2: module_type = path[2] if len(path) > 3: module = path[3] if len(path) > 4: if module_type == 'config': # Calls a config module _index = path[4] elif module_type == 'rpc': # Calls a rpc module module_func = '{}.{}'.format(module, path[4]) # /// Methods /// # Method NLAN operation # ------------------------ # POST --add or none(rpc) # PUT --update # DELETE --delete # GET --get # OPTIONS --schema # /// Query parameters /// # ------------------------------------------------------- # For POST(config)/PUT # Query parameters correspond to those --schema outputs # (excluding "_index"). # ------------------------------------------------------- # For POST(rpc)/DELETE/GET # args=<value1>,<value2>,... # ------------------------------------------------------- # For OPTIONS # args=<module_name> # ------------------------------------------------------- operations = { 'POST': '--add', 'PUT': '--update', 'DELETE': '--delete', 'GET': '--get' } doc = None query = environ['QUERY_STRING'] if method == 'POST' and module_type == 'config' or method == 'PUT': # add/update q = query.split('&') if _index: q.append('_index={}'.format(_index)) doc = str(argsmodel.parse_args('add', module, *q)) elif method == 'DELETE' or method == 'GET': # delete/get qq = query.split('=') q = [] if qq[0] == 'params': q = qq[1].split(',') if _index: q.append('_index={}'.format(_index)) doc = str(argsmodel.parse_args('get', module, *q)) elif method == 'POST' and module_type == 'rpc': doc = [] doc.append(module_func) q = query.split('=') if q[0] == 'params': doc.extend(q[1].split(',')) elif method == 'OPTIONS': # schema if query: qq = query.split('=') module = qq[1] # String buffer as a HTTP Response body out = StringIO() try: if method in operations and module_type == 'config': operation = operations[method] results = nlan.main(router=router, operation=operation, doc=doc, output_stdout=True, rest_output=True) print >> out, json.dumps(results) start_response('200 OK', [('Content-type', 'application/json')]) elif method == 'POST' and module_type == 'rpc': results = nlan.main(router=router, doc=doc, output_stdout=True, rest_output=True) print >> out, json.dumps(results) start_response('200 OK', [('Content-type', 'application/json')]) elif method == 'OPTIONS': print >> out, argsmodel.schema_help(module, list_output=True) start_response('200 OK', [('Content-type', 'application/json')]) else: print >> out, 'Not Implemented' start_response('501 Not Implemented', [('Content-type', 'text/plain')]) except NlanException as e: results = e.get_result() print >> out, json.dumps(results) start_response('500 Internal Server Error', [('Content-type', 'application/json')]) except Exception as e: print >> out, traceback.format_exc() start_response('500 Internal Server Error', [('Content-type', 'application/json')]) # Return the result as a body of HTTP Response out.seek(0) return out
def __call__(self, environ, start_response): # Request method method = environ['REQUEST_METHOD'] # /// URLs /// # /<router>/config/<module>/<_index> # /<router>/rpc/<module>/<func> path = environ['PATH_INFO'].split('/') router = None module_type = None # 'config' or 'rpc' module = None # name of a config module or a rpc module _index = None module_func = None if len(path) > 1: router = path[1] if len(path) > 2: module_type = path[2] if len(path) > 3: module = path[3] if len(path) > 4: if module_type == 'config': # Calls a config module _index = path[4] elif module_type == 'rpc': # Calls a rpc module module_func = '{}.{}'.format(module,path[4]) # /// Methods /// # Method NLAN operation # ------------------------ # POST --add or none(rpc) # PUT --update # DELETE --delete # GET --get # OPTIONS --schema # /// Query parameters /// # ------------------------------------------------------- # For POST(config)/PUT # Query parameters correspond to those --schema outputs # (excluding "_index"). # ------------------------------------------------------- # For POST(rpc)/DELETE/GET # args=<value1>,<value2>,... # ------------------------------------------------------- # For OPTIONS # args=<module_name> # ------------------------------------------------------- operations = {'POST': '--add', 'PUT': '--update', 'DELETE': '--delete', 'GET': '--get'} doc = None query = environ['QUERY_STRING'] if method == 'POST' and module_type == 'config' or method == 'PUT': # add/update q = query.split('&') if _index: q.append('_index={}'.format(_index)) doc = str(argsmodel.parse_args('add', module, *q)) elif method == 'DELETE' or method == 'GET': # delete/get qq = query.split('=') q = [] if qq[0] == 'params': q = qq[1].split(',') if _index: q.append('_index={}'.format(_index)) doc = str(argsmodel.parse_args('get', module, *q)) elif method == 'POST' and module_type == 'rpc': doc = [] doc.append(module_func) q = query.split('=') if q[0] == 'params': doc.extend(q[1].split(',')) elif method == 'OPTIONS': # schema if query: qq = query.split('=') module = qq[1] # String buffer as a HTTP Response body out = StringIO() try: if method in operations and module_type == 'config': operation = operations[method] results = nlan.main(router=router, operation=operation, doc=doc, output_stdout=True, rest_output=True) print >>out, json.dumps(results) start_response('200 OK', [('Content-type', 'application/json')]) elif method == 'POST' and module_type == 'rpc': results = nlan.main(router=router, doc=doc, output_stdout=True, rest_output=True) print >>out, json.dumps(results) start_response('200 OK', [('Content-type', 'application/json')]) elif method == 'OPTIONS': print >>out, argsmodel.schema_help(module, list_output=True) start_response('200 OK', [('Content-type', 'application/json')]) else: print >>out, 'Not Implemented' start_response('501 Not Implemented', [('Content-type', 'text/plain')]) except NlanException as e: results = e.get_result() print >>out, json.dumps(results) start_response('500 Internal Server Error', [('Content-type', 'application/json')]) except Exception as e: print >>out, traceback.format_exc() start_response('500 Internal Server Error', [('Content-type', 'application/json')]) # Return the result as a body of HTTP Response out.seek(0) return out
_init(loglevel=loglevel) if options.schema: if len(args) == 1: print argsmodel.schema_help(args[0]) else: print argsmodel.schema_help(None) sys.exit(0) data = None if operation and len(args) == 0: # Obtains data from nlan.py via SSH data = sys.stdin.read().replace('"', '') elif operation and len(args) > 0: # Obtains data from command arguments data = argsmodel.parse_args(operation, args[0], *args[1:]) else: # RPC w/ command arguments data = args operation = 'rpc_args' exit = 0 if options.init_action: __n__['init'] = options.init_action exit = _linux_init() else: __n__['init'] = False exit = _route(operation=operation, data=data) __n__['logger'].info('NLAN Agent execution completed') stdout.seek(0) sys.stdout.write(stdout.read())