From e4afdea3cb692096130d9fe4bf098e6515c4a7c8 Mon Sep 17 00:00:00 2001 From: tomasfratrik Date: Tue, 2 Jul 2024 10:11:35 +0200 Subject: [PATCH] put logic to common function --- commands/command_utils.py | 25 +++++++++++++++++++++++++ commands/preupgrade/__init__.py | 17 +---------------- commands/upgrade/__init__.py | 17 +---------------- 3 files changed, 27 insertions(+), 32 deletions(-) diff --git a/commands/command_utils.py b/commands/command_utils.py index 338978dd64..17581017d4 100644 --- a/commands/command_utils.py +++ b/commands/command_utils.py @@ -1,6 +1,7 @@ import json import os import re +import resource from leapp.exceptions import CommandError from leapp.utils import path @@ -140,3 +141,27 @@ def vet_upgrade_path(args): flavor=flavor, choices=','.join(supported_target_versions))) return (target_release, flavor) + + +def set_resource_limits(): + """ + Set resource limits for the maximum number of open file descriptors and the maximum writable file size. + + :raises: `CommandError` if the resource limits cannot be set + """ + + soft_nofile, _ = resource.getrlimit(resource.RLIMIT_NOFILE) + soft_fsize, _ = resource.getrlimit(resource.RLIMIT_FSIZE) + nofile_limit = 1024*16 + + if soft_nofile < nofile_limit: + try: + resource.setrlimit(resource.RLIMIT_NOFILE, (nofile_limit, nofile_limit)) + except OSError as err: + raise CommandError('Failed to set limit for maximum number of open file descriptors: {}'.format(err)) + + if soft_fsize != resource.RLIM_INFINITY: + try: + resource.setrlimit(resource.RLIMIT_FSIZE, (resource.RLIM_INFINITY, resource.RLIM_INFINITY)) + except OSError as err: + raise CommandError('Failed to set limit for maximum writeable file size: {}'.format(err)) diff --git a/commands/preupgrade/__init__.py b/commands/preupgrade/__init__.py index 4a6a6ccdf8..a9fa40e077 100644 --- a/commands/preupgrade/__init__.py +++ b/commands/preupgrade/__init__.py @@ -1,5 +1,4 @@ import os -import resource import sys import uuid @@ -60,21 +59,7 @@ def preupgrade(args, breadcrumbs): except LeappError as exc: raise CommandError(exc.message) - soft_nofile, _ = resource.getrlimit(resource.RLIMIT_NOFILE) - soft_fsize, _ = resource.getrlimit(resource.RLIMIT_FSIZE) - nofile_limit = 1024*16 - - if soft_nofile < nofile_limit: - try: - resource.setrlimit(resource.RLIMIT_NOFILE, (nofile_limit, nofile_limit)) - except OSError as err: - raise CommandError('Failed to set limit for maximum number of open file descriptors: {}'.format(err)) - - if soft_fsize != resource.RLIM_INFINITY: - try: - resource.setrlimit(resource.RLIMIT_FSIZE, (resource.RLIM_INFINITY, resource.RLIM_INFINITY)) - except OSError as err: - raise CommandError('Failed to set limit for maximum writeable file size: {}'.format(err)) + command_utils.set_resource_limits() workflow = repositories.lookup_workflow('IPUWorkflow')() util.warn_if_unsupported(configuration) diff --git a/commands/upgrade/__init__.py b/commands/upgrade/__init__.py index 5275629194..c7487fded8 100644 --- a/commands/upgrade/__init__.py +++ b/commands/upgrade/__init__.py @@ -1,5 +1,4 @@ import os -import resource import sys import uuid @@ -91,21 +90,7 @@ def upgrade(args, breadcrumbs): except LeappError as exc: raise CommandError(exc.message) - soft_nofile, _ = resource.getrlimit(resource.RLIMIT_NOFILE) - soft_fsize, _ = resource.getrlimit(resource.RLIMIT_FSIZE) - nofile_limit = 1024*16 - - if soft_nofile < nofile_limit: - try: - resource.setrlimit(resource.RLIMIT_NOFILE, (nofile_limit, nofile_limit)) - except OSError as err: - raise CommandError('Failed to set limit for maximum number of open file descriptors: {}'.format(err)) - - if soft_fsize != resource.RLIM_INFINITY: - try: - resource.setrlimit(resource.RLIMIT_FSIZE, (resource.RLIM_INFINITY, resource.RLIM_INFINITY)) - except OSError as err: - raise CommandError('Failed to set limit for maximum writeable file size: {}'.format(err)) + command_utils.set_resource_limits() workflow = repositories.lookup_workflow('IPUWorkflow')(auto_reboot=args.reboot) util.process_whitelist_experimental(repositories, workflow, configuration, logger)