コード例 #1
0
def cleanup_udev(rootdir):
    """
    Cleanup eventual HWADDR - device name definitions in udev rules.

    Parameters
    ----------
    rootdir: str
        Full path of udev root dir.

    Returns
    -------

    """
    _logger.debug('__ Update network udev rules.')
    udevrootdir = rootdir + '/etc/udev'
    _logger.debug('udev root is %s' % udevrootdir)
    pregex = fnmatch.translate('*rules')
    rulefiles = []
    if os.path.isdir(udevrootdir):
        #
        # Backup
        _ = system_tools.backup_dir(udevrootdir)
        #
        # Cleanup
        for root, dirs, files in os.walk(udevrootdir):
            for fn in files:
                fullfn = os.path.join(root, fn)
                #
                # Look for .rules file
                if re.search(pregex, fullfn):
                    rulefiles.append(fullfn)
                    macmatch = False
                    with open(fullfn, 'r') as g:
                        #
                        # Look for network naming rules
                        for line in g:
                            if re.match("(.*)KERNEL==\"eth(.*)", line):
                                macmatch = True
                                _logger.debug('Found rule in %s.' % fullfn)
                                break
                    if macmatch:
                        #
                        # Rewrite the network naming rules
                        mv_fullfn = fullfn + '_save'
                        try:
                            shutil.move(fullfn, mv_fullfn)
                            fndata = open(mv_fullfn, 'r').read()
                            newf = open(fullfn, 'w')
                            for lx in fndata.splitlines():
                                if not re.match("(.*)KERNEL==\"eth(.*)", lx):
                                    newf.write('%s\n' % lx)
                            newf.close()
                            os.remove(mv_fullfn)
                        except Exception as e:
                            _logger.error('  Failed to rewrite udev network '
                                          'naming rules: %s' % str(e))
                            return False
    else:
        _logger.debug('Directory %s not found.')
    return True
コード例 #2
0
def reconfigure_interfaces(rootdir):
    """
    Parse the network interfaces file.

    Parameters
    ----------
    rootdir: str
        Full path of image root dir as loopback mounted.

    Returns
    -------
        list: list of nic.
        dict: the interfaces configuration.
    """
    _logger.debug('__ The network interfaces configuration.')
    int_data = dict()
    int_nics = list()
    root_path = rootdir + get_config_data('default_interfaces')
    net_ifcfg_config = root_path + '/interfaces'

    if os.path.isfile(net_ifcfg_config):
        int_data[get_config_data('default_interfaces')] = list()
        _logger.debug('%s file exists' % net_ifcfg_config)
        try:
            with open(net_ifcfg_config, 'r') as inf:
                for ln in inf:
                    int_data[get_config_data('default_interfaces')].append(ln)
                    if 'iface' in ln.split():
                        if ln.split()[1] != 'lo':
                            int_nics.append(ln.split()[1])
                    else:
                        _logger.debug('no iface in %s' % ln)
        except Exception as e:
            _logger.error('  Error occured while reading %s: %s' %
                          (net_ifcfg_config, str(e)))
        #
        # rewrite
        if len(int_nics) == 0:
            _logger.debug('No interface definitions found in %s' %
                          net_ifcfg_config)
        else:
            try:
                #
                # backup
                bck_root_path = system_tools.backup_dir(root_path)
                _logger.debug('Copied %s to %s' % (root_path, bck_root_path))
                #
                # remove dir
                shutil.rmtree(root_path + '/interfaces.d')
                #
                # recreate interfaces config
                with open(net_ifcfg_config, 'w') as fi:
                    fi.writelines(
                        ln.replace('_XXXX_', int_nics[0]) + '\n'
                        for ln in get_config_data('default_interfaces_config'))
                migrate_tools.result_msg(
                    msg='Network interfaces file rewritten.', result=False)
            except Exception as e:
                _logger.error('  Failed to write new interfaces configuration '
                              'file %s: %s' % (net_ifcfg_config, str(e)))
    else:
        _logger.debug('No network interfaces configuration.')
    return int_nics, int_data
コード例 #3
0
def reconfigure_networkmanager(rootdir):
    """
    Replace the networkmanager configuration with Oracle Cloud Infrastructure
    compatible version.

    Parameters
    ----------
    rootdir: str
        Full path of image root dir as loopback mounted.
    Returns
    -------
        list: list of nic.
        dict: the network manager system-connections configurations
    """
    _logger.debug('__ The NetworkManager configuration.')
    netwmg_data = dict()
    netwmg_nics = list()
    network_config_dir = rootdir + get_config_data('default_nwconnections')
    _logger.debug('Network manager dir: %s' % network_config_dir)
    nw_mgr_cfg = rootdir + get_config_data('default_nwmconfig')
    _logger.debug('Network manager conf: %s' % nw_mgr_cfg)
    #
    # backup
    try:
        #
        # copy
        if os.path.isfile(nw_mgr_cfg):
            bck_nw_mgr_cfg = system_tools.exec_rename(nw_mgr_cfg)
            if bool(bck_nw_mgr_cfg):
                _logger.debug('Copied %s to %s' % (nw_mgr_cfg, bck_nw_mgr_cfg))
            else:
                _logger.warning(
                    'Failed to backup network manager configuration.')
        else:
            _logger.debug('No %s found.' % nw_mgr_cfg)
        #
        if os.path.isdir(network_config_dir):
            bck_network_config_dir = system_tools.backup_dir(
                network_config_dir)
            _logger.debug('Copied %s to %s' %
                          (network_config_dir, bck_network_config_dir))
        else:
            _logger.debug('%s not found.' % network_config_dir)
    except Exception as e:
        migrate_tools.error_msg('Failed to backup the networkmanager '
                                'configuration: %s' % str(e))
    #
    #
    if os.path.isdir(network_config_dir):
        _logger.debug('NetworkManager/%s directory exists.' %
                      network_config_dir)
        #
        # contains nwm keyfiles?
        nwm_files = glob(network_config_dir + '/*')
        if len(nwm_files) > 0:
            system_tools.exec_rmdir(network_config_dir)
            system_tools.exec_mkdir(network_config_dir)
            _logger.debug('%s emptied.' % network_config_dir)
        else:
            _logger.debug('No network manager keyfiles found.')
        #
        # update networkmanager configuration
        # TODO: write config file with configparser
        nwm_config_data = get_config_data('default_nwm_conf_file')
        with open(nw_mgr_cfg, 'w') as nwmf:
            nwmf.write('\n'.join(str(x) for x in nwm_config_data))
            migrate_tools.result_msg(
                msg='Networkmanager configuration updated.', result=False)
    else:
        _logger.debug(msg='  No NetworkManager configuration present.')

    return netwmg_nics, netwmg_data