Example #1
0
    def a10_remove_cloud_init(self):
        """
        Remove cloud-init package and configuration

        Returns
        -------
            bool: True on success, False otherwise.
        """
        _logger.debug('__ Remove existing cloud_init config.')
        try:
            #
            # write 90_dpkg.cfg
            dpkg_cfg_path = '/etc/cloud/cloud.cfg.d'
            if os.path.exists(dpkg_cfg_path):
                with open(dpkg_cfg_path + '/90_dpkg.cfg', 'w') as f:
                    f.write('datasource_list: [None]\n')
                _logger.debug('%s (re)written.', dpkg_cfg_path)
            else:
                _logger.debug('%s does not exist.', dpkg_cfg_path)
            #
            # remove cloud-init package
            purge_output = self._exec_apt(['purge', 'cloud-init', '-y'])
            _logger.debug('cloud-init purged: %s', purge_output)
            #
            # remove /etc/cloud
            cloud_cfg_path = '/etc/cloud'
            backup_cloud_path = system_tools.exec_rename(cloud_cfg_path)
            if bool(backup_cloud_path):
                _logger.debug('%s renamed to %s', cloud_cfg_path,
                              backup_cloud_path)
            #
            # remove /var/lib/cloud
            var_lib_cloud_path = '/var/lib/cloud'
            backup_var_lib_cloud_path = system_tools.exec_rename(
                var_lib_cloud_path)
            if bool(backup_var_lib_cloud_path):
                _logger.debug('%s renamed to %s', var_lib_cloud_path,
                              backup_var_lib_cloud_path)
            #
            # remove logs
            cloud_init_log = '/var/log/cloud-init.log'
            backup_cloud_init_log = system_tools.exec_rename(cloud_init_log)
            if bool(cloud_init_log):
                _logger.debug('%s renamed to %s', cloud_init_log,
                              backup_cloud_init_log)
            #
            pause_msg(msg='cloud-init removed', pause_flag='_OCI_CHROOT')
            return True
        except Exception as e:
            _logger.warning(
                'Failed to purge cloud-init completely which might cause issues '
                'at instance creation: %s', str(e))
            return False
Example #2
0
    def a10_remove_cloud_init(self):
        """
        Remove the cloud-init software and the configuration data.

        Returns
        -------
            bool: True on success, False otherwise.
        """
        _logger.debug('__ Remove existing cloud_init config.')
        try:
            pkg_mgr = self._exec_yum \
                if self.package_tool['pkg_mgr'] == 'yum' else self._exec_dnf
            package_erase = self.package_tool['package_remove'] + [
                'cloud-init'
            ]
            remove_output = pkg_mgr(package_erase)
            _logger.debug('cloud-init removed: %s', remove_output)
            #
            # remove /etc/cloud
            cloud_cfg_path = '/etc/cloud'
            backup_cloud_path = system_tools.exec_rename(cloud_cfg_path)
            if bool(backup_cloud_path):
                _logger.debug('%s renamed to %s', cloud_cfg_path,
                              backup_cloud_path)
            #
            # remove /var/lib/cloud
            var_lib_cloud_path = '/var/lib/cloud'
            backup_var_lib_cloud_path = system_tools.exec_rename(
                var_lib_cloud_path)
            if bool(backup_var_lib_cloud_path):
                _logger.debug('%s renamed to %s', var_lib_cloud_path,
                              backup_var_lib_cloud_path)
            #
            # remove logs
            cloud_init_log = '/var/log/cloud-init.log'
            backup_cloud_init_log = system_tools.exec_rename(cloud_init_log)
            if bool(cloud_init_log):
                _logger.debug('%s renamed to %s', cloud_init_log,
                              backup_cloud_init_log)
            #
            pause_msg(msg='cloud-init removed', pause_flag='_OCI_CHROOT')
            return True
        except Exception as e:
            _logger.warning(
                'Failed to remove cloud-init completely which might cause issues at instance '
                'creation: %s', str(e))
            return False
def reconfigure_ifcfg_config(rootdir):
    """
    Modify the network configuration in the image file to prevent
    conflicts during import. This is only for ol-type linux.

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

    Returns
    -------
        list: list of nic.
        dict: the interfaces configuration.
    """
    #
    # Rename the config files
    _logger.debug('__ The network ifcfg configuration.')
    ifcfg_list = list()
    ifcfg_data = dict()
    ifrootdir = rootdir + get_config_data('default_ifcfg')
    if os.path.isdir(ifrootdir):
        for cfgfile in glob(ifrootdir + '/ifcfg-*'):
            _logger.debug('Checking configfile: %s' % cfgfile)
            try:
                with open(cfgfile, 'r') as f:
                    # nl = filter(None, [x[:x.find('#')] for x in f])
                    nl = [_f for _f in [x[:x.find('#')] for x in f] if _f]
                ifcfg = dict(dl.replace('"', '').split('=') for dl in nl)
                if 'DEVICE' in ifcfg:
                    devname = ifcfg['DEVICE']
                else:
                    _logger.debug('Missing device name in %s' % cfgfile)
                    devname = cfgfile.split('/')[-1]
                ifcfg_list.append(devname)
                ifcfg_data[devname] = ifcfg
                _logger.debug('Network interface: %s' % devname)
            except Exception as e:
                _logger.error(
                    '  Problem reading network configuration file %s: '
                    '%s' % (cfgfile, str(e)))
    else:
        _logger.debug('No ifcfg network configuration.')
    #
    # backup
    for fn in glob(ifrootdir + '/ifcfg-*'):
        if 'ifcfg-lo' not in fn:
            fn_bck = system_tools.exec_rename(fn)
            if bool(fn_bck):
                _logger.debug('Network config file %s successfully '
                              'renamed to %s' % (fn, fn_bck))
            else:
                _logger.debug('Failed to backup network configuration '
                              'file %s to %s.' % (fn, fn_bck))
                # migrate_tools.error_msg('Failed to backup network configuration '
                #                         'file %s to %s.' % (fn, fn_bck))
                # raise OciMigrateException('Failed to rename network config '
                #                           'file %s to %s' % (fn, fn_bck))
        else:
            _logger.debug('ifcfg-lo found.')
    #
    # Generate new default network configuration.
    if len(ifcfg_list) > 0:
        nic0 = sorted(ifcfg_list)[0]
        dhcpniccfg = ifrootdir + '/ifcfg-%s' % nic0
        _logger.debug('Replacing network config file %s' % dhcpniccfg)
        try:
            with open(dhcpniccfg, 'w') as f:
                f.writelines(
                    ln.replace('_XXXX_', nic0) + '\n'
                    for ln in get_config_data('default_ifcfg_config'))
            migrate_tools.result_msg(
                msg='Replaced ifcfg network configuration.', result=False)
        except Exception as e:
            _logger.error('  Failed to write %s/ifcfg-eth0' % ifrootdir)
            migrate_tools.error_msg('Failed to write %s: %s' %
                                    (dhcpniccfg, str(e)))
            raise OciMigrateException('Failed to write %s: %s' %
                                      (dhcpniccfg, str(e)))
    else:
        _logger.debug('No ifcfg definitions found.')

    return ifcfg_list, ifcfg_data
def reconfigure_systemd_networkd(rootdir):
    """
    Parse the systemd network configuration.

    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 systemd-networkd configuration.')
    sys_data = dict()
    sys_nics = list()
    nw_ignore = ['container-host0', 'container-ve', 'container-vz']

    for root_path in get_config_data('default_systemd'):
        networkd_root = rootdir + root_path
        if os.path.isdir(networkd_root):
            _logger.debug('systemd network directory exists.')
            systemd_files = glob(root_path + '/*.network')
            if len(systemd_files) > 0:
                for sf in sorted(systemd_files):
                    ignore = False
                    for ig in nw_ignore:
                        if ig in sf:
                            ignore = True
                            break
                    if not ignore:
                        systemtd_net_config = ConfigParser()
                        sys_data[sf] = dict()
                        try:
                            sv = systemtd_net_config.read(sf)
                            if 'Match' in systemtd_net_config.sections():
                                ifname = systemtd_net_config.get(
                                    'Match', 'Name')
                                sys_nics.append(ifname)
                            else:
                                _logger.debug('-- No Match section in %s' % sf)
                            #
                            for sec in systemtd_net_config.sections():
                                sys_data[sf][sec] = systemtd_net_config.items(
                                    sec)
                                _logger.debug('%s' % sys_data[sf][sec])
                        except Exception as e:
                            _logger.error('  Failed to parse %s: %s' %
                                          (sf, str(e)))
                        #
                        # rename - backup
                        bcknm = system_tools.exec_rename(sf)
                        if bool(bcknm):
                            _logger.debug(
                                'Network config file %s renamed to %s' %
                                (sf, bcknm))
                        else:
                            _logger.error('  Failed to rename %s' % sf)
                            raise OciMigrateException('Failed to rename %s ' %
                                                      sf)
            else:
                _logger.debug('No systemd-networkd configuration.')
        else:
            _logger.debug('%s does not exist.' %
                          get_config_data('default_systemd'))
    #
    # write new config
    if len(sys_nics) > 0:
        nicname = sorted(sys_nics)[0]
        with open(rootdir + get_config_data('default_systemd_file'),
                  'w') as sdf:
            sdf.writelines(
                ln.replace('_XXXX_', nicname) + '\n'
                for ln in get_config_data('default_systemd_config'))
        migrate_tools.result_msg(
            msg='systemd-networkd configuration rewritten.', result=True)
    else:
        _logger.debug('No systemd-networkd configuration.')
    return sorted(sys_nics), sys_data
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
def reconfigure_netplan(rootdir):
    """
    Parse the yaml netplan files and look for network interface names.

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

    Returns
    -------
        list: list of nic.
        dict: the netplan network configurations.
    """

    _logger.debug('__ The netplan configuration.')
    netplan_data = dict()
    netplan_nics = list()
    root_path = rootdir + get_config_data('default_netplan')
    if os.path.isdir(root_path):
        _logger.debug('netplan directory exists.')
        #
        # contains yaml files?
        yaml_files = glob(root_path + '/*.yaml')
        if len(yaml_files) > 0:
            for yf in sorted(yaml_files):
                try:
                    with open(yf, 'r') as yfd:
                        yaml_data = yaml.safe_load(yfd)
                        netplan_data[yf] = yaml_data
                        _logger.debug('netplan: %s' % yaml_data)
                except Exception as e:
                    _logger.error('  Failed to parse %s: %s' % (yf, str(e)))
                    migrate_tools.error_msg('Failed to parse %s: %s' %
                                            (yf, str(e)))
                    break
                #
                if 'network' in yaml_data:
                    if 'ethernets' in yaml_data['network']:
                        for k, _ in sorted(
                                yaml_data['network']['ethernets'].items()):
                            netplan_nics.append(k)
                    else:
                        _logger.debug('ethernets key missing.')
                else:
                    _logger.debug('network key missing.')
            if len(netplan_nics) == 0:
                _logger.debug('No netplan definitions found in %s' % root_path)
            else:
                nicname = sorted(netplan_nics)[0]
                #
                # rename and recreate
                try:
                    #
                    # backup
                    if not bool(system_tools.exec_rename(root_path)):
                        _logger.warning('Failed to backup %s.' % root_path)
                    #
                    # recreate dir
                    os.mkdir(root_path)
                    mode755 = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH | \
                              stat.S_IWUSR | stat.S_IRUSR | stat.S_IRGRP | \
                              stat.S_IROTH
                    os.chmod(root_path, mode755)
                    _logger.debug((' Recreated %s' % root_path))
                    #
                    # recreate netplan config
                    #
                    # __GT__ commenting out this one to avoid conflicts,
                    #  cloud-init recreates it from scratch.
                    #
                    # netplan_config = get_config_data('default_netplan_config')
                    # netplan_config['network']['ethernets'][nicname] \
                    #    = netplan_config['network']['ethernets'].pop('_XXXX_')
                    # with open(root_path + '/'
                    #          + get_config_data('default_netplan_file'), 'w') \
                    #        as yf:
                    #    yaml.safe_dump(netplan_config, yf, default_flow_style=False)
                    # migrate_tools.result_msg(msg='Netplan network configuration '
                    #                         'files replaced.', result=True)
                except Exception as e:
                    migrate_tools.error_msg(
                        'Failed to write new netplan '
                        'configuration file %s: %s' %
                        (get_config_data('default_netplan_file'), str(e)))
                    raise OciMigrateException(
                        'Failed to write new netplan '
                        'configuration file %s: %s' %
                        (get_config_data('default_netplan_file'), str(e)))
        else:
            _logger.debug('  No netplan yaml config files found.')
    else:
        _logger.debug('No netplan configuration found.')

    return netplan_nics, netplan_data