Beispiel #1
0
def smb_del_cfg(user_name):
    # cfgfile = smb_get_cfgfile_name(in_name)
    cfgfile = '/etc/samba/user.smb.conf'
    if not os.path.isfile(cfgfile):
        return 0
    phase_name = '[' + user_name + ']'
    new_cfg_str = ''
    retval = net_common.get_info_from_file(cfgfile)
    if retval[0] != 0:
        _logger.error("del user {} get cfg info from {} failed".format(user_name, cfgfile))
        return -1
    strlist = retval[1].split('\n\n')
    update_flag = 0
    for i in range(len(strlist)):
        mstr = strlist[i].strip('\n').strip(' ')
        if len(mstr) <= 2:
            continue
        if mstr.startswith(phase_name):
            _logger.debug('del user {} cfg info {} at str success'.format(user_name, mstr))
            update_flag = 1
            continue
        else:
            new_cfg_str += mstr + '\n\n'
    if update_flag == 1:
        retval = net_common.set_info_to_file(cfgfile, new_cfg_str, 'w')
        if retval != 0:
            _logger.error("del user {} update cfg to file {} failed".format(user_name, cfgfile))
            return -1
        else:
            _logger.debug("del user {} update cfg to file {} success".format(user_name, cfgfile))
    return 0
Beispiel #2
0
def smb_add_cfg(user_name, read_only):
    cfgfile = '/etc/samba/user.smb.conf'
    phase_name = '[' + user_name + ']'
    # add_cfg_str = '[' + user_name + ']\n' + 'comment = This is ' + user_name + '\n' + 'path = ' + smb_get_userpath(
    #     user_name) + '\n' + 'read only = yes\n' + 'valid users = ' + user_name + '\n\n'
    add_cfg_str = '[{user_name}]\n' \
                  'path = {path}\n' \
                  'comment = My shared folder\n' \
                  'force group = root\n' \
                  'force user = root\n' \
                  'valid users = {user_name}\n'.format(user_name=user_name, path=smb_get_userpath(user_name))
    if read_only:
        add_cfg_str += 'read only = yes\n\n'
    else:
        add_cfg_str += 'readonly = no\n'
        add_cfg_str += 'writable = yes\n\n'
    new_cfg_str = ''
    if os.path.isfile(cfgfile):
        retval = net_common.get_info_from_file(cfgfile)
        if retval[0] != 0:
            _logger.error("add user {} get cfg info from {} failed".format(user_name, cfgfile))
            return -1
        if retval[1].find(phase_name) < 0:
            new_cfg_str = retval[1] + add_cfg_str
    else:
        new_cfg_str = add_cfg_str

    if new_cfg_str != '':
        retval = net_common.set_info_to_file(cfgfile, new_cfg_str, 'w')
        if retval != 0:
            _logger.error("add user {} add cfg to file {} failed".format(user_name, cfgfile))
            return -1
        else:
            _logger.debug("add user {} add cfg to file {} success".format(user_name, cfgfile))
    return 0
Beispiel #3
0
def set_root_passwd(passwdinfo):
    cmdline = 'mount -no remount, rw /boot'
    retval = net_common.get_info_from_syscmd(cmdline)
    _logger.debug('remount /boot {} {}'.format(retval[0], retval[1]))
    tmpfile = '/dev/shm/tempstr'
    mpasswd = json.loads(passwdinfo)
    _logger.debug("set root pwd info {}       {}".format(passwdinfo, mpasswd))
    retval = net_common.set_info_to_file(tmpfile, mpasswd, 'w')
    if retval != 0:
        _logger.error('set passwd write file failed,ret {}'.format(retval))
        xlogging.raise_system_error('set passwd failed,{}'.format(retval),
                                    'set passwd failed,{}'.format(retval), -1,
                                    _logger)
    cmdline = 'set_passwd -f {}'.format(tmpfile)
    retval = net_common.get_info_from_syscmd(cmdline)
    if retval[0] != 0:
        _logger.error('set passwd failed,ret {} {}'.format(
            retval[0], retval[1]))
        xlogging.raise_system_error('set passwd failed,{}'.format(retval[0]),
                                    'set passwd failed,{}'.format(retval[0]),
                                    -1, _logger)
    else:
        _logger.debug('set passwd success')

    cmdline = 'umount /boot'
    retval = net_common.get_info_from_syscmd(cmdline)
    _logger.debug('umount /boot {} {}'.format(retval[0], retval[1]))

    cmdline = 'mount -o ro /dev/mapper/boot /boot'
    retval = net_common.get_info_from_syscmd(cmdline)
    _logger.debug('mount /dev/mapper/boot /boot {} {}'.format(
        retval[0], retval[1]))
Beispiel #4
0
def net_create_cfg_file(in_net_name):
    cfg_new_file = net_cfg_file_pre + in_net_name
    cfg_origin_file = ''
    file_str = ''
    for fn in os.listdir(net_cfg_file_dir):
        # print("fn is {}".format(fn))
        if os.path.isfile(os.path.join(net_cfg_file_dir,
                                       fn)) and fn.startswith('ifcfg-eno'):
            cfg_origin_file = os.path.join(net_cfg_file_dir, fn)
            _logger.debug(
                "get an origin net cfg file {}".format(cfg_origin_file))
            break
    if cfg_origin_file != '':
        retval = net_common.get_info_from_file(cfg_origin_file)
        if retval[0] == 0:
            file_str = retval[1]

    if file_str == '':
        file_str = 'TYPE=Ethernet\n' \
                   'BOOTPROTO=static\n' \
                   'DEFROUTE=yes\n' \
                   'PEERDNS=yes\n' \
                   'PEERROUTES=yes\n' \
                   'IPV4_FAILURE_FATAL=no\n' \
                   'IPV6INIT=yes\n' \
                   'IPV6_AUTOCONF=yes\n' \
                   'IPV6_DEFROUTE=yes\n' \
                   'IPV6_PEERDNS=yes\n' \
                   'IPV6_PEERROUTES=yes\n' \
                   'IPV6_FAILURE_FATAL=no\n' \
                   'ONBOOT=yes\n'
        _logger.debug("set file str as init")
    _logger.debug("get file str \n{}".format(file_str))

    mdict = {
        'IPADDR': ['d', ''],
        'NETMASK': ['d', ''],
        'GATEWAY': ['d', ''],
        'DNS': ['d', ''],
        'UUID': ['d', ''],
        'MASTER': ['d', ''],
        'SLAVE': ['d', ''],
        'BOOTPROTO': ['ma', '=' + 'static'],
        'ONBOOT': ['ma', '=yes'],
        'NAME': ['ma', '=' + in_net_name],
        'DEVICE': ['ma', '=' + in_net_name],
    }
    retval = net_common.set_itemdict_in_str(file_str, mdict)
    file_str = retval[1]
    _logger.debug("get modify file str \n{}".format(file_str))

    retval = net_common.set_info_to_file(cfg_new_file, file_str, 'w')
    if retval != 0:
        _logger.error("set info to file faild {}".format(cfg_new_file))
        return -1
    _logger.debug("create new net cfg file success {}".format(cfg_new_file))
    return 0
Beispiel #5
0
def net_set_info(in_net_infos):
    # test code
    # test_list = [['interface',['eno16777984'],['172.16.6.165','255.255.248.0','']],
    #              ['interface',['eno33557248','eno50336512','eno67115776'],['172.16.6.167','255.255.248.0','172.16.1.1']],
    #              ['interface',['eno33557248'],['172.16.6.168','255.255.248.0','']],
    #              ['dns',['172.16.1.2','172.16.1.3']]
    #              ]
    # in_net_infos = json.dumps(test_list)
    # test code end
    with net_lock:
        _logger.debug("net_set_info json info {}".format(in_net_infos))
        in_net_list = json.loads(in_net_infos)
        _logger.debug("net set info list {}".format(in_net_list))
        if len(in_net_list[0]
               [2]) != 3 or in_net_list[0][0] != 'interface' or len(
                   in_net_list[0][1]) <= 0:
            xlogging.raise_system_error("net set failed", "net set failed", -1,
                                        _logger)
        bond_index_list = list()
        phy_index_list = list()
        dns_list = list()
        cfg_files = list()
        for i in range(len(in_net_list)):
            if in_net_list[i][0] == 'interface':
                if len(in_net_list[i][1]) > 1:
                    bond_index_list.append(i)
                    _logger.debug("interface {},net info {}".format(
                        in_net_list[i][1], in_net_list[i][2]))
                else:
                    phy_index_list.append(i)
                    _logger.debug("phy adapter {},net info {}".format(
                        in_net_list[i][1], in_net_list[i][2]))
            elif in_net_list[i][0] == 'dns':
                dns_list = in_net_list[i][1]
            else:
                _logger.error("invalid flag {}".format(in_net_list[i][0]))

        bond_list_len = len(bond_index_list)
        phy_list_len = len(phy_index_list)
        _logger.debug(
            "in_net_list len {} bond_list_len {} phy_list_len {} dns list {}".
            format(len(in_net_list), bond_list_len, phy_list_len, dns_list))
        # set bond driver info
        cmd_info = 'rmmod bonding;rm -rf /etc/sysconfig/network-scripts/ifcfg-bond*'
        net_common.get_info_from_syscmd(cmd_info)

        net_common.get_info_from_syscmd(cmd_info)
        if bond_list_len > 0:
            cfg_file = '/etc/modprobe.d/bonding.conf'
            cfg_files.append(cfg_file)
            bond_cfg_str = 'alias bond0 bonding\noptions bonding mode=6 miimon=200 max_bonds=' + str(
                bond_list_len) + '\n'
            retval = net_common.set_info_to_file(cfg_file, bond_cfg_str, 'w')
            if retval != 0:
                _logger.error("set cfg file {} failed".format(cfg_file))
                xlogging.raise_system_error(
                    "net set failed", "net set failed at set bond driver info",
                    -2, _logger)
            cmd_info = 'modprobe bonding'
            net_common.get_info_from_syscmd(cmd_info)

        for i in range(bond_list_len):
            cfg_file = '/etc/sysconfig/network-scripts/ifcfg-bond' + str(i)
            cfg_files.append(cfg_file)
            file_str = 'ONBOOT=yes\n' \
                       'BOOTPROTO=static\n' \
                       'USERCTL=no\n'
            file_str += 'DEVICE=bond' + str(i) + '\n'
            file_str += 'IPADDR=' + in_net_list[
                bond_index_list[i]][2][0] + '\n'
            file_str += 'NETMASK=' + in_net_list[
                bond_index_list[i]][2][1] + '\n'
            file_str += 'GATEWAY=' + in_net_list[
                bond_index_list[i]][2][2] + '\n'
            retval = net_common.set_info_to_file(cfg_file, file_str, 'w')
            if retval != 0:
                _logger.error("set cfg file {} failed,info is \n{}".format(
                    cfg_file, file_str))
                xlogging.raise_system_error(
                    "net set failed", "net set failed at set bond cfg file",
                    -3, _logger)
            # 设置虚拟网卡 设备名 bond0:0 bond0:1 ...
            if len(in_net_list[bond_index_list[i]]) > 3:
                for k in range(len(in_net_list[bond_index_list[i]][3])):
                    cfg_file = '/etc/sysconfig/network-scripts/ifcfg-bond' + str(
                        i) + ':' + str(k)
                    cfg_files.append(cfg_file)
                    ip_and_mask = in_net_list[bond_index_list[i]][3][k].split(
                        '/')
                    file_str = 'ONBOOT=yes\n' \
                               'BOOTPROTO=static\n' \
                               'USERCTL=no\n'
                    file_str += 'DEVICE=bond' + str(i) + ':' + str(k) + '\n'
                    file_str += 'IPADDR=' + ip_and_mask[0] + '\n'
                    file_str += 'NETMASK=' + ip_and_mask[1] + '\n'
                    retval = net_common.set_info_to_file(
                        cfg_file, file_str, 'w')
                    if retval != 0:
                        _logger.error(
                            "set cfg file {} failed,info is \n{}".format(
                                cfg_file, file_str))
                        xlogging.raise_system_error(
                            "net set failed",
                            "net set failed at set bond cfg file", -3, _logger)
            for j in range(len(in_net_list[bond_index_list[i]][1])):
                cfg_file = '/etc/sysconfig/network-scripts/ifcfg-' + in_net_list[
                    bond_index_list[i]][1][j]
                cfg_files.append(cfg_file)
                mdict = {
                    'SLAVE': ['ma', '=yes'],
                    'MASTER': ['ma', '=bond' + str(i)]
                }
                retval = net_common.set_itemdict_in_file(cfg_file, mdict)
                if retval != 0:
                    _logger.error("set file {} failed,ret value is {}".format(
                        cfg_file, retval))
                    xlogging.raise_system_error(
                        "net set failed",
                        "net set failed at set bond phy adapter cfg file", -4,
                        _logger)

        cmd_info = 'rm -rf /etc/sysconfig/network-scripts/ifcfg-[^bond]*:*'
        net_common.get_info_from_syscmd(cmd_info)

        for i in range(phy_list_len):
            cfg_file = '/etc/sysconfig/network-scripts/ifcfg-' + in_net_list[
                phy_index_list[i]][1][0]
            cfg_files.append(cfg_file)
            mdict = {
                'SLAVE': ['d', ''],
                'MASTER': ['d', ''],
                'BOOTPROTO': ['ma', '=static'],
                'ONBOOT': ['ma', '=yes'],
                'IPADDR': ['ma', '=' + in_net_list[phy_index_list[i]][2][0]],
                'NETMASK': ['ma', '=' + in_net_list[phy_index_list[i]][2][1]],
                'GATEWAY': ['ma', '=' + in_net_list[phy_index_list[i]][2][2]]
            }
            retval = net_common.set_itemdict_in_file(cfg_file, mdict)
            if retval != 0:
                _logger.error("set file {} failed,ret value is {}".format(
                    cfg_file, retval))
                xlogging.raise_system_error(
                    "net set failed",
                    "net set failed at set phy adapter cfg file", -5, _logger)
            # 设置虚拟网卡
            if len(in_net_list[phy_index_list[i]]) > 3:
                for k in range(len(in_net_list[phy_index_list[i]][3])):
                    cfg_file = '/etc/sysconfig/network-scripts/ifcfg-' + in_net_list[
                        phy_index_list[i]][1][0] + ':' + str(k)
                    cfg_files.append(cfg_file)
                    ip_and_mask = in_net_list[phy_index_list[i]][3][k].split(
                        '/')
                    file_str = 'ONBOOT=yes\n' \
                               'BOOTPROTO=static\n' \
                               'USERCTL=no\n'
                    file_str += 'DEVICE=' + in_net_list[
                        phy_index_list[i]][1][0] + ':' + str(k) + '\n'
                    file_str += 'IPADDR=' + ip_and_mask[0] + '\n'
                    file_str += 'NETMASK=' + ip_and_mask[1] + '\n'
                    retval = net_common.set_info_to_file(
                        cfg_file, file_str, 'w')
                    if retval != 0:
                        _logger.error(
                            "set cfg file {} failed,info is \n{}".format(
                                cfg_file, file_str))
                        xlogging.raise_system_error(
                            "net set failed",
                            "net set failed at set bond cfg file", -3, _logger)

        # clean ifcfg begin
        ifcfg_dir_path = '/etc/sysconfig/network-scripts'
        ifcfg_full_path_prefix = ifcfg_dir_path + '/ifcfg-'
        for file_full_name in os.listdir(ifcfg_dir_path):
            file_full_path = os.path.join(ifcfg_dir_path, file_full_name)
            if (os.path.isfile(file_full_path)) and (
                    file_full_path.startswith(ifcfg_full_path_prefix)) and (
                        file_full_path not in cfg_files):
                _logger.warning(r'will remove unnecessary ifcfg : {}'.format(
                    file_full_path))
                os.remove(file_full_path)
        # clean ifcfg end

        retval = net_set_dns(dns_list)
        if retval != 0:
            _logger.error("set dns str {} failed,ret value is {}".format(
                dns_list, retval))
            xlogging.raise_system_error("net set failed",
                                        "net set failed at set dns cfg file",
                                        -6, _logger)

        retval = net_common.get_info_from_syscmd("cat /etc/resolv.conf")
        if retval[0] == 0:
            _logger.info("after set dns is \n{}".format(retval[1]))
        else:
            _logger.error("read dns cfg file failed")

        retval = net_restart_network()
        if retval != 0:
            _logger.error(
                "restart_network failed,ret value is {}".format(retval))
            xlogging.raise_system_error("net set failed",
                                        "net set failed at restart network",
                                        -7, _logger)
        return 0