示例#1
0
def rc(args=None):

    platform = __n__['platform']
    share_dir = __n__['share_dir']

    init_script = None
    command = {}
    chmod = 'chmod 755 /etc/init.d/nlan'
    if platform == 'debian':
        init_script = os.path.join(share_dir, 'nlan_debian')
        init_script_docker = os.path.join(share_dir, 'nlan_debian_docker')
        command['enable'] = [
            'cp {} /etc/init.d/nlan'.format(init_script), chmod,
            'update-rc.d nlan defaults'
        ]
        command['update'] = ['cp {} /etc/init.d/nlan'.format(init_script)]
        command['enable_docker'] = [
            'cp {} /etc/init.d/nlan'.format(init_script_docker), chmod,
            'update-rc.d nlan defaults'
        ]
        command['update_docker'] = [
            'cp {} /etc/init.d/nlan'.format(init_script_docker)
        ]
        command['disable'] = ['rm /etc/init.d/nlan', 'update-rc.d nlan remove']
        command['status'] = ['ls /etc/rc2.d/']
    elif platform == 'openwrt':
        init_script = os.path.join(share_dir, 'nlan_openwrt')
        command['enable'] = [
            'cp {} /etc/init.d/nlan'.format(init_script), chmod,
            '/etc/init.d/nlan enable'
        ]
        command['update'] = ['cp {} /etc/init.d/nlan'.format(init_script)]
        command['disable'] = [
            '/etc/init.d/nlan disable', 'rm /etc/init.d/nlan'
        ]
        command['status'] = ['ls /etc/rc.d/']

    if args in command:
        # Caution: this is just a workaround to cope with
        # the situation that OpenWrt's 'enable' always returns
        # exit code = 1, even if it is successful.
        if platform == 'openwrt' and args == 'enable':
            for l in command[args]:
                # Does not raise an exception even if exit code = 1
                cmd(l)
            # Tests if S85nlan exists
            exitcode = check_cmd('test -f /etc/rc.d/S85nlan')
            if exitcode == 0:
                pass
            else:
                raise Exception('system.rc: nlan enable failure')
        else:
            for l in command[args]:
                print output_cmd(l)
    else:
        return "Usage:\nsystem.rc (enable|update|disable|status|enable_docker|update_docker)"
示例#2
0
def rc(args=None):

    platform = __n__['platform']
    share_dir = __n__['share_dir']

    init_script = None 
    command = {} 
    chmod = 'chmod 755 /etc/init.d/nlan'
    if platform == 'debian':
        init_script = os.path.join(share_dir, 'nlan_debian')
        init_script_docker = os.path.join(share_dir, 'nlan_debian_docker')
        command['enable'] = ['cp {} /etc/init.d/nlan'.format(init_script), chmod, 'update-rc.d nlan defaults']
        command['update'] = ['cp {} /etc/init.d/nlan'.format(init_script)]
        command['enable_docker'] = ['cp {} /etc/init.d/nlan'.format(init_script_docker), chmod, 'update-rc.d nlan defaults']
        command['update_docker'] = ['cp {} /etc/init.d/nlan'.format(init_script_docker)]
        command['disable'] = ['rm /etc/init.d/nlan', 'update-rc.d nlan remove']
        command['status'] = ['ls /etc/rc2.d/']
    elif platform == 'openwrt':
        init_script = os.path.join(share_dir, 'nlan_openwrt')
        command['enable'] = ['cp {} /etc/init.d/nlan'.format(init_script), chmod, '/etc/init.d/nlan enable']
        command['update'] = ['cp {} /etc/init.d/nlan'.format(init_script)]
        command['disable'] = ['/etc/init.d/nlan disable', 'rm /etc/init.d/nlan']
        command['status'] = ['ls /etc/rc.d/']

    if args in command:
        # Caution: this is just a workaround to cope with
        # the situation that OpenWrt's 'enable' always returns
        # exit code = 1, even if it is successful.
        if platform == 'openwrt' and args == 'enable':
            for l in command[args]:
                # Does not raise an exception even if exit code = 1
                cmd(l)
            # Tests if S85nlan exists
            exitcode = check_cmd('test -f /etc/rc.d/S85nlan')
            if exitcode == 0:
                pass
            else:
                raise Exception('system.rc: nlan enable failure')
        else:
            for l in command[args]:
                print output_cmd(l)
    else:
        return "Usage:\nsystem.rc (enable|update|disable|status|enable_docker|update_docker)"
示例#3
0
 elif not crud and option: # --scp, --scpmod, --raw
     main(router=router, operation=option, doc=args, loglevel=loglevel, git=git, verbose=verbose, mime=mime)
 elif options.schema:
     if len(args) == 1:
         print argsmodel.schema_help(args[0])
     else:
         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:
示例#4
0
def do(scenario, dirname, interactive):

    for l in scenario:
        if 'do' in l:
            do_file = os.path.join(dirname, l['do'])
            with open(do_file, 'r') as f:
                scenario = yaml.load(f.read())
            do(scenario, dirname, interactive)
        if 'sleep' in l:
            time.sleep(int(l['sleep']))
        if 'comment' in l:
            rp = "TEST: {}".format(l['comment'])
            print bar[:5], rp, bar[5+len(rp):]
        if interactive:
            raw_input("Press Enter to continue...") 
        if 'command' in l:
            cmdutil.check_cmd(l['command'])
        if 'nlan' in l:
            ll = l['nlan']
            options = None
            args = None
            if 'options' in ll:
                options = ll['options']
            if 'args' in ll:
                v = ll['args']
                if isinstance(v, dict):
                    if options == '--delete':
                        args = _args_od(v, reverse=True)
                    else:
                        args = _args_od(v)
                    prev = args
                    args = str(args)
                else:
                    args = v.split()
            if 'router' in ll:
                router = ll['router']
            try:
                result = nlan.main(router=router, operation=options, doc=args, output_stdout=True)
                if 'assert' in ll:
                    if ll['assert']:  # not null
                        #args1 = dict(eval(result[0]['stdout']))
                        args1 = dict(result[0]['stdout'])
                        args2 = ll['assert']
                        if args2 == '_prev':
                            args2 = remove_item(prev, '_index')
                        diff = None
                        try:
                            diff = list(dictdiffer.diff(args1, args2))
                            assert len(diff) == 0
                            _print_success()
                        except Exception as e:
                            _print_failure()
                            print diff 
                            print traceback.format_exc()
                    else:  # null
                        if result[0]['stdout']:
                            _print_failure()
                            print result[0]['stdout']
                        else:
                            _print_success()
            except NlanException as e:
                result = e.get_result() 
                type_ = result[0]['response']['exception']
                if 'assertRaises' in ll:
                    assert type_ == ll['assertRaises']['type'] 
                    _print_success()
                else:
                    raise