def test_env_var_match(monkeypatch): envars = { 'LEAPP_DEVEL_SKIP_WIP': '0', 'LEAPP_DEVEL_SKIP_DIP': '1', 'LEAPP_DEVEL_SKIP_RIP': '2' } monkeypatch.setattr(api, 'current_actor', CurrentActorMocked(envars)) assert get_env('LEAPP_DEVEL_SKIP_WIP') == '0' assert not get_env('LEAPP_DEVEL_SKIP_PIP')
def read_or_fetch(filename, directory="/etc/leapp/files", service=None, allow_empty=False): """ Return contents of a file or fetch them from an online service if the file does not exist. """ logger = api.current_logger() local_path = os.path.join(directory, filename) # try to get the data locally if not os.path.exists(local_path): logger.warning("File {lp} does not exist, falling back to online service".format(lp=local_path)) else: try: with open(local_path) as f: data = f.read() if not allow_empty and not data: _raise_error(local_path, "File {lp} exists but is empty".format(lp=local_path)) logger.warning("File {lp} successfully read ({l} bytes)".format(lp=local_path, l=len(data))) return data except EnvironmentError: _raise_error(local_path, "File {lp} exists but couldn't be read".format(lp=local_path)) except Exception as e: raise e # if the data is not present locally, fetch it from the online service service = service or get_env("LEAPP_SERVICE_HOST", default=SERVICE_HOST_DEFAULT) service_path = "{s}/api/pes/{f}".format(s=service, f=filename) proxy = get_env("LEAPP_PROXY_HOST") proxies = {"https": proxy} if proxy else None cert = ("/etc/pki/consumer/cert.pem", "/etc/pki/consumer/key.pem") response = None try: response = _request_data(service_path, cert=cert, proxies=proxies) except requests.exceptions.RequestException as e: logger.error(e) _raise_error(local_path, "Could not fetch {f} from {sp} (unreachable address).".format( f=filename, sp=service_path)) # almost certainly missing certs except (OSError, IOError) as e: logger.error(e) _raise_error(local_path, ("Could not fetch {f} from {sp} (missing certificates). Is the machine" " registered?".format(f=filename, sp=service_path))) if response.status_code != 200: _raise_error(local_path, "Could not fetch {f} from {sp} (error code: {e}).".format( f=filename, sp=service_path, e=response.status_code)) if not allow_empty and not response.text: _raise_error(local_path, "File {lp} successfully retrieved but it's empty".format(lp=local_path)) logger.warning("File {sp} successfully retrieved and read ({l} bytes)".format( sp=service_path, l=len(response.text))) return response.text
def _backup_to_persistent_package_cache(userspace_dir): if get_env('LEAPP_DEVEL_USE_PERSISTENT_PACKAGE_CACHE', None) == '1': # Clean up any dead bodies, just in case run(['rm', '-rf', PERSISTENT_PACKAGE_CACHE_DIR]) if os.path.exists(os.path.join(userspace_dir, 'var', 'cache', 'dnf')): with mounting.NspawnActions( base_dir=userspace_dir) as target_context: target_context.copytree_from('/var/cache/dnf', PERSISTENT_PACKAGE_CACHE_DIR)
def _restore_persistent_package_cache(userspace_dir): if get_env('LEAPP_DEVEL_USE_PERSISTENT_PACKAGE_CACHE', None) == '1': if os.path.exists(PERSISTENT_PACKAGE_CACHE_DIR): with mounting.NspawnActions( base_dir=userspace_dir) as target_context: target_context.copytree_to(PERSISTENT_PACKAGE_CACHE_DIR, '/var/cache/dnf') # We always want to remove the persistent cache here to unclutter the system run(['rm', '-rf', PERSISTENT_PACKAGE_CACHE_DIR])
def _check_deprecated_rhsm_skip(): # we do not plan to cover this case by tests as it is purely # devel/testing stuff, that becomes deprecated now # just log the warning now (better than nothing?); deprecation process will # be specified in close future if get_env('LEAPP_DEVEL_SKIP_RHSM', '0') == '1': api.current_logger().warning( 'The LEAPP_DEVEL_SKIP_RHSM has been deprecated. Use' ' LEAPP_NO_RHSM istead or use the --no-rhsm option for' ' leapp. as well custom repofile has not been defined.' ' Please read documentation about new "skip rhsm" solution.')
def read_or_fetch(filename, directory="/etc/leapp/files", service=None, allow_empty=False, encoding='utf-8'): """ Return the contents of a text file or fetch them from an online service if the file does not exist. :param str filename: The name of the file to read or fetch. :param str directory: Directory that should contain the file. :param str service: URL to the service providing the data if the file is missing. :param bool allow_empty: Raise an error if the resulting data are empty. :param str encoding: Encoding to use when decoding the raw binary data. :returns: Text contents of the file. Text is decoded using the provided encoding. :rtype: str """ logger = api.current_logger() local_path = os.path.join(directory, filename) # try to get the data locally if not os.path.exists(local_path): logger.warning( "File {lp} does not exist, falling back to online service".format( lp=local_path)) else: try: with io.open(local_path, encoding=encoding) as f: data = f.read() if not allow_empty and not data: _raise_error( local_path, "File {lp} exists but is empty".format(lp=local_path)) logger.warning( "File {lp} successfully read ({l} bytes)".format( lp=local_path, l=len(data))) return data except EnvironmentError: _raise_error( local_path, "File {lp} exists but couldn't be read".format(lp=local_path)) except Exception as e: raise e # if the data is not present locally, fetch it from the online service service = service or get_env("LEAPP_SERVICE_HOST", default=SERVICE_HOST_DEFAULT) service_path = "{s}/api/pes/{f}".format(s=service, f=filename) proxy = get_env("LEAPP_PROXY_HOST") proxies = {"https": proxy} if proxy else None cert = ("/etc/pki/consumer/cert.pem", "/etc/pki/consumer/key.pem") response = None try: response = _request_data(service_path, cert=cert, proxies=proxies) except requests.exceptions.RequestException as e: logger.error(e) _raise_error( local_path, "Could not fetch {f} from {sp} (unreachable address).".format( f=filename, sp=service_path)) # almost certainly missing certs except (OSError, IOError) as e: logger.error(e) _raise_error(local_path, ( "Could not fetch {f} from {sp} (missing certificates). Is the machine" " registered?".format(f=filename, sp=service_path))) if response.status_code != 200: _raise_error( local_path, "Could not fetch {f} from {sp} (error code: {e}).".format( f=filename, sp=service_path, e=response.status_code)) if not allow_empty and not response.content: _raise_error( local_path, "File {lp} successfully retrieved but it's empty".format( lp=local_path)) logger.warning( "File {sp} successfully retrieved and read ({l} bytes)".format( sp=service_path, l=len(response.content))) return response.content.decode(encoding)
def process(): if get_env('LEAPP_NO_NETWORK_RENAMING', '0') == '1': api.current_logger().info( 'Skipping handling of possibly renamed network interfaces: leapp executed with LEAPP_NO_NETWORK_RENAMING=1' ) return rhel7_ifaces = next(api.consume(PersistentNetNamesFacts)).interfaces rhel8_ifaces = next( api.consume(PersistentNetNamesFactsInitramfs)).interfaces rhel7_ifaces_map = {iface.mac: iface for iface in rhel7_ifaces} rhel8_ifaces_map = {iface.mac: iface for iface in rhel8_ifaces} initrd_files = [] missing_ifaces = [] renamed_interfaces = [] if rhel7_ifaces != rhel8_ifaces: for iface in rhel7_ifaces: rhel7_name = rhel7_ifaces_map[iface.mac].name try: rhel8_name = rhel8_ifaces_map[iface.mac].name except KeyError: missing_ifaces.append(iface) api.current_logger().warning( 'The device with MAC "{}" is not detected in the upgrade' ' environment. Required driver: "{}".' ' Original interface name: "{}".'.format( iface.mac, iface.driver, iface.name)) continue if rhel7_name != rhel8_name and get_env( 'LEAPP_NO_NETWORK_RENAMING', '0') != '1': api.current_logger().warning( 'Detected interface rename {} -> {}.'.format( rhel7_name, rhel8_name)) if re.search('eth[0-9]+', iface.name) is not None: api.current_logger().warning( 'Interface named using eth prefix, refusing to generate link file' ) renamed_interfaces.append( RenamedInterface(**{ 'rhel7_name': rhel7_name, 'rhel8_name': rhel8_name })) continue initrd_files.append(generate_link_file(iface)) if missing_ifaces: msg = ('Some network devices have not been detected inside the' ' upgrade environment and so related network interfaces' ' could be renamed on the upgraded system.') # Note(pstodulk): # This usually happens when required (RHEL 8 compatible) # drivers are not included in the upgrade initramfs. # We can add more information later. Currently we cannot provide # better instructions for users before (at least): # a) networking work in the upgrade initramfs (PR #583) # b) it's possible to influence the upgrade initramfs (PR #517) # TODO(pstodulk): gen report msg api.current_logger().warning(msg) api.produce(RenamedInterfaces(renamed=renamed_interfaces)) api.produce(InitrdIncludes(files=initrd_files)) # TODO: cover actor by tests in future. I am skipping writting of tests # now as some refactoring and bugfixing related to this actor # is planned already. api.produce(TargetInitramfsTasks(include_files=initrd_files))
def _the_enablerepo_option_used(): return config.get_env('LEAPP_ENABLE_REPOS', None) is not None
def process(self): skip_check = get_env('LEAPP_DEVEL_SKIP_CHECK_OS_RELEASE', False) if (skip_check or version.is_supported_version()) and next( self.consume(RepositoriesMapping), None): userspacegen.perform()
def skip_rhsm(): """Check whether we should skip RHSM related code.""" return get_env('LEAPP_NO_RHSM', '0') == '1'
def process(): if not config.get_env('LEAPP_ENABLE_REPOS'): return api.current_logger().info('The --enablerepo option has been used,') for repoid in config.get_env('LEAPP_ENABLE_REPOS').split(','): api.produce(CustomTargetRepository(repoid=repoid))
def test_env_var_match(monkeypatch): monkeypatch.setattr(api, 'current_actor', CurrentActorMocked) assert get_env('LEAPP_DEVEL_SKIP_WIP') == '0' assert not get_env('LEAPP_DEVEL_SKIP_PIP')