コード例 #1
0
ファイル: vlan.py プロジェクト: bonald/vim_cfg
    def post(self):
        '''
        add vlan
        '''
        res = Verify.dict(
            self.req_data, {
                'id': (lambda x: x.type('int') and x >= 1 and x <= 4095,
                       ApiError(400, 1, 'invalid vlan id')),
                'name?': (lambda x: x.type('string') and x.match(
                    r'^[a-zA-Z][a-zA-Z\.\-\_0-9]{0,15}$'),
                          ApiError(400, 2, 'invalid vlan name')),
            })

        vlan_id = res.get('id')
        vlan_name = res.get('name')

        # create vlan
        res = vcmd.execute('cdbctl create/cdb/l2/vlan/%d' % vlan_id)
        if res != 0:
            raise ApiError(400, 3, 'cannot create vlan')

        # modify name
        if vlan_name:
            res = vcmd.execute('cdbctl update/cdb/l2/vlan/%d/name/%s' %
                               (vlan_id, vlan_name))
            if res != 0:
                raise ApiError(400, 4, 'cannot modify name')
コード例 #2
0
def clear():
    """
    API:

    CLAER:

    Return: {
        error: bool,
        err_code: int,
        err_reason: string
    }
    """
    _err_reason = [
        '',  # err_code: 0
        'arp cache clear failed',  # err_code: 1
    ]

    obj = {'error': False, 'err_code': 0, 'err_reason': ''}

    status, lines = vcmd.get_status_lines('cdbctl update/cdb/l3/flush_arp')
    vcmd.execute('sleep 1')
    if status != 0:
        obj['error'] = True
        obj['err_code'] = status
        obj['err_reason'] = _err_reason[1]
        return obj

    return obj
コード例 #3
0
ファイル: save_conf.py プロジェクト: bonald/vim_cfg
def update():
    """
    API:
    
    PUT:
    
    Retrun: {
        error: bool,
        err_code: int,
        err_reason: string
    }
    """
    
    _err_reason = [
        '',                 # err_code: 0
        'build cfg file failed', # err_code: 1
        'copy cfg file failed', # err_code: 2
        'remove tmp cfg file failed', # err_code: 3
        'send trap failed', # err_code: 4
    ]
    
    obj = {'error': False, 'err_code': 0, 'err_reason': ''}
    
    # build config file
    tmp_filename = 'config-%d' % random.randint(1, 100000)
    status = vcmd.execute('cdbctl buildcfg/startup/%s/cdb' % tmp_filename)
    if status != 0:
        obj['error'] = True
        obj['err_code'] = 1
        obj['err_reason'] = _err_reason[1]
        return obj
    
    # copy config file
    status = vcmd.execute('cp /tmp/%s /mnt/flash/startup-config.conf' % tmp_filename)
    if status != 0:
        obj['error'] = True
        obj['err_code'] = 2
        obj['err_reason'] = _err_reason[2]
        return obj
        
    # copy config file
    status = vcmd.execute('rm /tmp/%s' % tmp_filename)
    if status != 0:
        obj['error'] = True
        obj['err_code'] = 3
        obj['err_reason'] = _err_reason[3]
        return obj
    
    # snmp
    status = vcmd.execute('cdbctl update/cdb/sys/send_trap/write_success')
    if status != 0:
        obj['error'] = True
        obj['err_code'] = 4
        obj['err_reason'] = _err_reason[4]
        return obj
    
    return obj
コード例 #4
0
def _get_files(param):
    """
    Return: [
       {filename: string, size: int, date: string, time: string, datetime: string, url: string,},
       ...
       ...
    ]
    """
    res = []
    static_flash_dir = r'%s/static/flash' % base.config.root_dir
    dirname = re.sub(r'^/mnt/flash/?', '', os.path.dirname(param))

    # check flash folder exist
    if (not os.path.exists(static_flash_dir)):
        vcmd.execute('ln -s /mnt/flash %s' % static_flash_dir)

    # get cmd lines
    cmd = 'ls -l --full-time %s | grep -r \'^-\' | awk \'{printf("%%s=%%s=%%s=%%s\\n", $5, $6, $7, $9)}\'' % param
    status, lines = vcmd.get_status_lines(cmd)
    if not lines:
        return res

    # make dict
    tmp = []
    for line in lines:
        tmp = line.split('=')
        if not tmp or len(tmp) != 4:
            continue
        elif tmp[0] and tmp[1] and tmp[2] and tmp[3]:

            # append data
            size, date, time, filename = tmp[0], tmp[1], re.sub(
                r'\.\d+$', '', tmp[2]), re.sub(r'^/mnt/flash/', '', tmp[3])

            if dirname:
                url = url_for('static',
                              filename='flash/%s/%s' % (dirname, filename))
            else:
                url = url_for('static', filename='flash/%s' % filename)

            res.append({
                'size': base.parse.parse_int(size, 0),
                'date': date,
                'time': time,
                'datetime': str(date) + '_' + str(time),
                'filename': filename,
                'url': url,
            })

    return res
コード例 #5
0
ファイル: log_mgr.py プロジェクト: bonald/vim_cfg
def flush():
    """
    API:

    DELETE: {
        vlan_ids: [int, int, ....]
    }

    Retrun: {
        error: bool,
        err_code: int,
        err_reason: string
    }

    """

    _err_reason = [
        '',  # err_code: 0
        'bad request',  # err_code: 1
        'cannot remove vlan 1',  # err_code: 2
        'excute error',  # err_code: 3
    ]

    obj = {'error': False, 'err_code': 0, 'err_reason': ''}
    res = vcmd.execute('cdbctl delete/cdb/app/syslog_cfg/clearbuf')
    # param check
    if res:
        obj['error'] = True
        obj['err_code'] = 1
        obj['err_reason'] = _err_reason[1]
        return obj
    return obj
コード例 #6
0
ファイル: ddos_prevent.py プロジェクト: bonald/vim_cfg
def get():
    """
    API:

    GET:

    Retrun: {
        arr: {[
            ddos_prevent
            ]
            },
            ...
            ...
        ],
        error: bool,
        err_code: int,
        err_reason: string
    }
    """
    _err_reason = [
        '',                    # err_code: 0
        'iptables prevent is not exist',         # err_code: 1
    ]

    obj = {'arr': {}, 'error': False, 'err_code': 0, 'err_reason': ''}

    vcmd.execute('cdbctl update/cdb/sys/iptables_prevent/pkt_statistic_update/1')
    tmp = vcmd.get_lines('cdbctl read/cdb/sys/iptables_prevent')
    if tmp:
        prevent_info = tmp[0]
        obj['arr'] = vcmd._line_to_obj_simple(prevent_info)
        #print obj['arr']
    else:
        obj['error'] = True
        obj['err_code'] = 1
        obj['err_reason'] = _err_reason[1]
    return obj
コード例 #7
0
ファイル: vlan.py プロジェクト: bonald/vim_cfg
def modify(req_data):
    """
    API:
    
    PUT: {
        id: vlan_id,
        name: vlan_name,
    }
    
    Return: {
        error: bool,
        err_code: int,
        err_reason: string
    }
    
    """
    
    _err_reason = [
        '',                     # err_code: 0
        'bad request param',    # err_code: 1
        'cannot set vlan name', # err_code: 2
    ]
    
    obj = {'error': False, 'err_code': 0, 'err_reason': ''}
    
    # param check
    res, err = base.check.dictionary(req_data, [
        ['id', 'int', '>=', 1, 'and', '<=', 4095, 'bad request: id', 1],
        ['name', 'string', 're', r'^[a-zA-Z][a-zA-Z\.\-\_0-9]{0,15}$', 'bad request: name', 1],
    ])
    if not res:
        obj['error'] = True
        obj['err_code'] = err['err_code']
        obj['err_reason'] = err['err_reason']
        return obj
    
    vlan_id = res['id']
    vlan_name = res['name']
    
    # modify vlan name
    res = vcmd.execute('cdbctl update/cdb/l2/vlan/%d/name/%s' % (vlan_id, vlan_name))
    if res != 0:
        obj['error'] = True
        obj['err_code'] = 2
        obj['err_reason'] = _err_reason[2]
        return obj
    
    return obj    
コード例 #8
0
ファイル: vlan.py プロジェクト: bonald/vim_cfg
    def put(self):
        '''
        modify vlan name
        '''
        res = Verify.dict(
            self.req_data, {
                'id': (lambda x: x.type('int') and x >= 1 and x <= 4095,
                       ApiError(400, 1, 'invalid vlan id')),
                'name': (lambda x: x.type('string') and x.match(
                    r'^[a-zA-Z][a-zA-Z\.\-\_0-9]{0,15}$'),
                         ApiError(400, 2, 'invalid vlan name')),
            })

        res = vcmd.execute('cdbctl update/cdb/l2/vlan/%d/name/%s' %
                           (res['id'], res['name']))
        if res != 0:
            raise ApiError(400, 3, 'cannot modify name')
コード例 #9
0
ファイル: vlan.py プロジェクト: bonald/vim_cfg
def range_delete(req_data):
    """
    API:
    
    POST: {
        range: vlan range string,
    }
    
    Return: {
        error: bool,
        err_code: int,
        err_reason: string
    }
    
    """
    _err_reason = [
        '',                     # err_code: 0
        'bad request',          # err_code: 1
        'cannot delete vlan',   # err_code: 2
    ]
    
    obj = {'error': False, 'err_code': 0, 'err_reason': ''}
    
    # param check
    res, err = base.check.dictionary(req_data, [
        ['range', 'string', 're', r'^((\d{1,4}(\-\d{1,4})?),)*(\d{1,4}(\-\d{1,4})?)$', 'bad request: range', 1],
    ])
    if not res:
        obj['error'] = True
        obj['err_code'] = err['err_code']
        obj['err_reason'] = err['err_reason']
        return obj
    
    range_str = res['range']
    cmd = 'cdbctl delete/cdb/l2/vlanrange/%s' % range_str
    res = vcmd.execute(cmd)
    if res != 0:
        obj['error'] = True
        obj['err_code'] = 2
        obj['err_reason'] = _err_reason[2]
        return obj
    
    return obj   
コード例 #10
0
def delete(username):
    """
    API:
    
    DELETE: {
       file_arr: [
            {
               'id': string,
            },
            ...
            ...
       ],
    }
    
    Retrun: {
        error: bool,
        err_code: int,
        err_reason: string
    }
    
    """

    _err_reason = [
        '',  # err_code: 0
        'bad request',  # err_code: 1
        'can not delete factory default user',  # err_code: 2
    ]

    obj = {'error': False, 'err_code': 0, 'err_reason': ''}

    cmd = 'cdbctl delete/cdb/app/user/%s' % username
    res = vcmd.execute(cmd)

    if res != 0:
        obj['error'] = True
        obj['err_code'] = 2
        obj['err_reason'] = _err_reason[2]
        return obj

    return obj
コード例 #11
0
def backup(file_type, filename, backup_filename):
    """
    API:
    
    PUT: /<file_type>/<filename>/<backup_filename>/
    
    Retrun: {
        error: bool,
        err_code: int,
        err_reason: string
    }
    
    """

    _err_reason = [
        '',  # err_code: 0
        'no such file: %s' % str(filename),  # err_code: 1
        'file "%s" already exists' % str(backup_filename),  # err_code: 2
        'copy failed',  # err_code: 3
    ]

    obj = {'error': False, 'err_code': 0, 'err_reason': ''}

    path_base, path_file, path_backup_file = '', '', ''
    if file_type == 'image':
        path_base = '/mnt/flash/boot/'
    elif file_type == 'start_conf':
        path_base = '/mnt/flash/'
    elif file_type == 'syslog':
        path_base = '/mnt/flash/syslogfile/'
    elif file_type == 'flash':
        path_base = '/mnt/flash/'
    path_file = path_base + str(filename)
    path_backup_file = path_base + str(backup_filename)

    # check filename
    if not os.path.exists(path_file):
        obj['error'] = True
        obj['err_code'] = 1
        obj['err_reason'] = _err_reason[1]
        return obj

    # check backup filename
    if os.path.exists(path_backup_file):
        obj['error'] = True
        obj['err_code'] = 2
        obj['err_reason'] = _err_reason[2]
        return obj

    ## 1. change path to 'wr', remove protect
    status, lines = vcmd.get_status_lines(
        'mount -t ubifs -o remount -rw ubi1:boot /mnt/flash/boot')

    # copy file
    res = vcmd.execute('cp %s %s' % (path_file, path_backup_file))

    ## 2. restore path to 'r', restore protect
    status, lines = vcmd.get_status_lines(
        'mount -t ubifs -o remount -r ubi1:boot /mnt/flash/boot')

    if res != 0:
        obj['error'] = True
        obj['err_code'] = 3
        obj['err_reason'] = _err_reason[3]
        return obj

    return obj
コード例 #12
0
def delete(req_data):
    """
    API:
    
    DELETE: {
       file_arr: [
            {
               'type': 'image' or 'start_conf' or 'syslog',
               'filename': string,
            },
            ...
            ...
       ],
    }
    
    Retrun: {
        error: bool,
        err_code: int,
        err_reason: string
    }
    
    """

    _err_reason = [
        '',  # err_code: 0
        'bad request',  # err_code: 1
        'delete failed',  # err_code: 2
    ]

    obj = {'error': False, 'err_code': 0, 'err_reason': ''}

    # param check
    file_arr = req_data.get('file_arr')
    if not type(file_arr) is types.ListType:  # bad request
        obj['error'] = True
        obj['err_code'] = 1
        obj['err_reason'] = _err_reason[1]
        return obj

    # make cmd
    paths = []
    for f in file_arr:
        if not f.get('filename'):
            continue
        if f.get('type') == 'image':
            paths.append('/mnt/flash/boot/' + str(f['filename']))
        elif f.get('type') == 'start_conf':
            paths.append('/mnt/flash/' + str(f['filename']))
        elif f.get('type') == 'syslog':
            paths.append('/mnt/flash/syslogfile/' + str(f['filename']))
        elif f.get('type') == 'flash':
            paths.append('/mnt/flash/' + str(f['filename']))
    if not paths:  # bad request
        obj['error'] = True
        obj['err_code'] = 1
        obj['err_reason'] = _err_reason[1]
        return obj

    # delete start
    ## 1. change path to 'wr', remove protect
    status, lines = vcmd.get_status_lines(
        'mount -t ubifs -o remount -rw ubi1:boot /mnt/flash/boot')
    res = 0
    if vcl.config.is_dev:  # remote
        cmd = 'rm ' + ' '.join(paths)
        res = vcmd.execute(cmd)

    else:
        for path in paths:  # local
            if os.path.exists(path):
                os.remove(path)
            else:
                res = 255

    ## 2. restore path to 'r', restore protect
    status, lines = vcmd.get_status_lines(
        'mount -t ubifs -o remount -r ubi1:boot /mnt/flash/boot')
    if res != 0:
        obj['error'] = True
        obj['err_code'] = 2
        obj['err_reason'] = _err_reason[2]
        return obj

    return obj
コード例 #13
0
ファイル: vlan.py プロジェクト: bonald/vim_cfg
def add(req_data):
    """
    API:
    
    POST: {
        id: vlan_id,
        name: vlan_name,
    }
    
    Return: {
        error: bool,
        err_code: int,
        err_reason: string
    }
    
    """
    
    _err_reason = [
        '',                     # err_code: 0
        'bad request',          # err_code: 1
        'cannot create vlan',   # err_code: 2
        'cannot set vlan name', # err_code: 3
    ]
    
    obj = {'error': False, 'err_code': 0, 'err_reason': ''}
    
    # param check
    res, err = base.check.dictionary(req_data, [
        ['id', 'int', '>=', 1, 'and', '<=', 4095, 'bad request: id', 1],
        #['name', 'string', 're', r'^[a-zA-Z][a-zA-Z\.\-\_0-9]{0,15}$', 'bad request: name', 1],
    ])
    if not res:
        obj['error'] = True
        obj['err_code'] = err['err_code']
        obj['err_reason'] = err['err_reason']
        return obj
    
    vlan_id = res['id']
    #vlan_name = res['name']
    vlan_name = str(req_data.get('name'))
    #print vlan_name
    if vlan_name:
        res, err = base.check.dictionary(req_data, [
            ['id', 'int', '>=', 1, 'and', '<=', 4095, 'bad request: id', 1],
            ['name', 'string', 're', r'^[a-zA-Z][a-zA-Z\.\-\_0-9]{0,15}$', 'illegality name', 1],
        ])
        if not res:
            obj['error'] = True
            obj['err_code'] = err['err_code']
            obj['err_reason'] = err['err_reason']
            return obj
    
    # add new vlan
    res = vcmd.execute('cdbctl create/cdb/l2/vlan/%d' % vlan_id)
    if res != 0:
        obj['error'] = True
        obj['err_code'] = 2
        obj['err_reason'] = _err_reason[2]
        return obj
    
    # modify vlan name
    if vlan_name:
        res = vcmd.execute('cdbctl update/cdb/l2/vlan/%d/name/%s' % (vlan_id, vlan_name))
        if res != 0:
            obj['error'] = True
            obj['err_code'] = 3
            obj['err_reason'] = _err_reason[3]
            return obj
    
    return obj