Skip to content

Commit

Permalink
Add support for input parameters (#102)
Browse files Browse the repository at this point in the history
* Add support for input parameters

This patch introduces the support for input parameters that will come
from the Insights UI. Regarding the environment variables value that
will be passed down to us, there is no change that we need to do in
order to receive it. The only change that was necessary was to adapt the
code to receive the CLI switches. In this case, we are handling it
pretty simple, only identifying the two possibilities for now and
working with it. In the future, if more options appear, we can improve
the code.

* Skip environmnet variables that have value as 0

If an environment variable has value as 0, we will skip setting them in
the subprocess. The reason for this is that convert2rhel does not handle
well the case for values in those env vars. Convert2rhel currently only
checks for the presence of the env var in the os.environ object. If it
is set, then it will skip the check, otherwise, it will follow the
execution normally.
  • Loading branch information
r0x0d authored Jul 2, 2024
1 parent 0b9a3cc commit 7c75a7f
Show file tree
Hide file tree
Showing 5 changed files with 386 additions and 55 deletions.
67 changes: 57 additions & 10 deletions convert2rhel_insights_tasks/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,12 +657,65 @@ def install_or_update_convert2rhel(required_files):
return False, None


def run_convert2rhel():
def prepare_environment_variables(env):
"""Prepare environment variables to be used in subprocess
This metod will prepare any environment variables before they are sent down
to the subprocess that will convert2rhel. Currently, this is meant to be a
workaround since convert2rhel does not parse the value of the environment
variables, but only check the presence of them in os.environ.
With this function, we are make sure that any variables that have the value
0 are ignored before setting them in the subprocess env context, this will
prevent convert2rhel to wrongly skipping checks because it was pre-defined
in the insights playbook.
:param env: The environment variables before setting them in subprocess.
:type env: dict[str, Any]
"""
for variable, value in env.items():
if variable.startswith("CONVERT2RHEL_") and value == 0:
env.pop(variable)
return env


def run_convert2rhel(env):
"""
Run the convert2rhel tool assigning the correct environment variables.
:param env: Dictionary of possible environment variables to passed down to
the process.
:type env: dict[str]
"""
logger.info("Running Convert2RHEL %s", (SCRIPT_TYPE.title()))

command = ["/usr/bin/convert2rhel"]
if IS_ANALYSIS:
command.append("analyze")

command.append("-y")

# This will always be represented as either false/true, since this option
# comes from the input parameters through Insights UI.
els_disabled = json.loads(env.pop("ELS_DISABLED", "false").lower())
if not bool(els_disabled):
command.append("--els")

optional_repositories = env.pop("OPTIONAL_REPOSITORIES", [])
if optional_repositories:
repositories = optional_repositories.split(",")
repositories = [
"--enablerepo=%s" % repository.strip() for repository in repositories
]
command.extend(repositories)

env = prepare_environment_variables(env)
output, returncode = run_subprocess(command, env=env)
return output, returncode


def parse_environment_variables():
"""Read the environment variable from os.environ and return them."""
new_env = {}
for key, value in os.environ.items():
valid_prefix = "RHC_WORKER_"
Expand All @@ -671,14 +724,7 @@ def run_convert2rhel():
new_env[key.replace(valid_prefix, "")] = value
else:
new_env[key] = value

command = ["/usr/bin/convert2rhel"]
if IS_ANALYSIS:
command.append("analyze")

command.append("-y")
output, returncode = run_subprocess(command, env=new_env)
return output, returncode
return new_env


def cleanup(required_files):
Expand Down Expand Up @@ -881,7 +927,8 @@ def main():
YUM_TRANSACTIONS_TO_UNDO.add(transaction_id)

check_convert2rhel_inhibitors_before_run()
stdout, returncode = run_convert2rhel()
new_env = parse_environment_variables()
stdout, returncode = run_convert2rhel(new_env)
execution_successful = returncode == 0

# Returncode other than 0 can happen in three states in analysis mode:
Expand Down
77 changes: 66 additions & 11 deletions playbooks/convert-to-rhel-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
vars:
insights_signature: |
ascii_armored gpg signature
insights_signature_exclude: /vars/insights_signature
insights_signature_exclude: /vars/insights_signature,/vars/content_vars
interpreter: /usr/bin/python2
content: |
import json
Expand Down Expand Up @@ -666,12 +666,65 @@
return False, None
def run_convert2rhel():
def prepare_environment_variables(env):
"""Prepare environment variables to be used in subprocess
This metod will prepare any environment variables before they are sent down
to the subprocess that will convert2rhel. Currently, this is meant to be a
workaround since convert2rhel does not parse the value of the environment
variables, but only check the presence of them in os.environ.
With this function, we are make sure that any variables that have the value
0 are ignored before setting them in the subprocess env context, this will
prevent convert2rhel to wrongly skipping checks because it was pre-defined
in the insights playbook.
:param env: The environment variables before setting them in subprocess.
:type env: dict[str, Any]
"""
for variable, value in env.items():
if variable.startswith("CONVERT2RHEL_") and value == 0:
env.pop(variable)
return env
def run_convert2rhel(env):
"""
Run the convert2rhel tool assigning the correct environment variables.
:param env: Dictionary of possible environment variables to passed down to
the process.
:type env: dict[str]
"""
logger.info("Running Convert2RHEL %s", (SCRIPT_TYPE.title()))
command = ["/usr/bin/convert2rhel"]
if IS_ANALYSIS:
command.append("analyze")
command.append("-y")
# This will always be represented as either false/true, since this option
# comes from the input parameters through Insights UI.
els_disabled = json.loads(env.pop("ELS_DISABLED", "false").lower())
if not bool(els_disabled):
command.append("--els")
optional_repositories = env.pop("OPTIONAL_REPOSITORIES", [])
if optional_repositories:
repositories = optional_repositories.split(",")
repositories = [
"--enablerepo=%s" % repository.strip() for repository in repositories
]
command.extend(repositories)
env = prepare_environment_variables(env)
output, returncode = run_subprocess(command, env=env)
return output, returncode
def parse_environment_variables():
"""Read the environment variable from os.environ and return them."""
new_env = {}
for key, value in os.environ.items():
valid_prefix = "RHC_WORKER_"
Expand All @@ -680,14 +733,7 @@
new_env[key.replace(valid_prefix, "")] = value
else:
new_env[key] = value
command = ["/usr/bin/convert2rhel"]
if IS_ANALYSIS:
command.append("analyze")
command.append("-y")
output, returncode = run_subprocess(command, env=new_env)
return output, returncode
return new_env
def cleanup(required_files):
Expand Down Expand Up @@ -890,7 +936,8 @@
YUM_TRANSACTIONS_TO_UNDO.add(transaction_id)
check_convert2rhel_inhibitors_before_run()
stdout, returncode = run_convert2rhel()
new_env = parse_environment_variables()
stdout, returncode = run_convert2rhel(new_env)
execution_successful = returncode == 0
# Returncode other than 0 can happen in three states in analysis mode:
Expand Down Expand Up @@ -1033,4 +1080,12 @@
if __name__ == "__main__":
main()
content_vars:
CONVERT2RHEL_THROUGH_INSIGHTS: 1
SCRIPT_MODE: ANALYSIS
CONVERT2RHEL_ALLOW_UNAVAILABLE_KMODS: 0
CONVERT2RHEL_UNSUPPORTED_SKIP_KERNEL_CURRENCY_CHECK: 0
CONVERT2RHEL_OUTDATED_PACKAGE_CHECK_SKIP: 0
ELS_DISABLED: false
# Multiple optional repository parameter values will be comma separated, for example:
# rhel-7-server-optional-rpms,rhel-7-server-extras-rpms,rhel-7-server-supplementary-rpms
OPTIONAL_REPOSITORIES: None
76 changes: 65 additions & 11 deletions playbooks/convert-to-rhel-conversion.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
vars:
insights_signature: |
ascii_armored gpg signature
insights_signature_exclude: /vars/insights_signature
insights_signature_exclude: /vars/insights_signature,/vars/content_vars
interpreter: /usr/bin/python2
content: |
import json
Expand Down Expand Up @@ -666,12 +666,65 @@
return False, None
def run_convert2rhel():
def prepare_environment_variables(env):
"""Prepare environment variables to be used in subprocess
This metod will prepare any environment variables before they are sent down
to the subprocess that will convert2rhel. Currently, this is meant to be a
workaround since convert2rhel does not parse the value of the environment
variables, but only check the presence of them in os.environ.
With this function, we are make sure that any variables that have the value
0 are ignored before setting them in the subprocess env context, this will
prevent convert2rhel to wrongly skipping checks because it was pre-defined
in the insights playbook.
:param env: The environment variables before setting them in subprocess.
:type env: dict[str, Any]
"""
for variable, value in env.items():
if variable.startswith("CONVERT2RHEL_") and value == 0:
env.pop(variable)
return env
def run_convert2rhel(env):
"""
Run the convert2rhel tool assigning the correct environment variables.
:param env: Dictionary of possible environment variables to passed down to
the process.
:type env: dict[str]
"""
logger.info("Running Convert2RHEL %s", (SCRIPT_TYPE.title()))
command = ["/usr/bin/convert2rhel"]
if IS_ANALYSIS:
command.append("analyze")
command.append("-y")
# This will always be represented as either false/true, since this option
# comes from the input parameters through Insights UI.
els_disabled = json.loads(env.pop("ELS_DISABLED", "false").lower())
if not bool(els_disabled):
command.append("--els")
optional_repositories = env.pop("OPTIONAL_REPOSITORIES", [])
if optional_repositories:
repositories = optional_repositories.split(",")
repositories = [
"--enablerepo=%s" % repository.strip() for repository in repositories
]
command.extend(repositories)
env = prepare_environment_variables(env)
output, returncode = run_subprocess(command, env=env)
return output, returncode
def parse_environment_variables():
"""Read the environment variable from os.environ and return them."""
new_env = {}
for key, value in os.environ.items():
valid_prefix = "RHC_WORKER_"
Expand All @@ -680,14 +733,7 @@
new_env[key.replace(valid_prefix, "")] = value
else:
new_env[key] = value
command = ["/usr/bin/convert2rhel"]
if IS_ANALYSIS:
command.append("analyze")
command.append("-y")
output, returncode = run_subprocess(command, env=new_env)
return output, returncode
return new_env
def cleanup(required_files):
Expand Down Expand Up @@ -890,7 +936,8 @@
YUM_TRANSACTIONS_TO_UNDO.add(transaction_id)
check_convert2rhel_inhibitors_before_run()
stdout, returncode = run_convert2rhel()
new_env = parse_environment_variables()
stdout, returncode = run_convert2rhel(new_env)
execution_successful = returncode == 0
# Returncode other than 0 can happen in three states in analysis mode:
Expand Down Expand Up @@ -1036,3 +1083,10 @@
CONVERT2RHEL_THROUGH_INSIGHTS: 1
CONVERT2RHEL_CONFIGURE_HOST_METERING: auto
SCRIPT_MODE: CONVERSION
CONVERT2RHEL_ALLOW_UNAVAILABLE_KMODS: 0
CONVERT2RHEL_UNSUPPORTED_SKIP_KERNEL_CURRENCY_CHECK: 0
CONVERT2RHEL_OUTDATED_PACKAGE_CHECK_SKIP: 0
ELS_DISABLED: false
# Multiple optional repository parameter values will be comma separated, for example:
# rhel-7-server-optional-rpms,rhel-7-server-extras-rpms,rhel-7-server-supplementary-rpms
OPTIONAL_REPOSITORIES: None
75 changes: 75 additions & 0 deletions tests/test_environment_variables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import os
import pytest

from convert2rhel_insights_tasks import main


@pytest.mark.parametrize(
("env", "expected"),
(
({"RHC_WORKER_SCRIPT_MODE": "CONVERSION"}, {"SCRIPT_MODE": "CONVERSION"}),
(
{"RHC_WORKER_FOO": "BAR", "RHC_WORKER_BAR": "FOO"},
{"FOO": "BAR", "BAR": "FOO"},
),
(
{"RHC_WORKER_FOO": "BAR", "RHC_BAR": "FOO"},
{"FOO": "BAR", "RHC_BAR": "FOO"},
),
(
{"FOO": "BAR", "BAR": "FOO"},
{"FOO": "BAR", "BAR": "FOO"},
),
),
)
def test_parse_environment_variables(env, expected, monkeypatch):
monkeypatch.setattr(os, "environ", env)
result = main.parse_environment_variables()

assert result == expected


def test_parse_environment_variables_empty(monkeypatch):
monkeypatch.setattr(os, "environ", {})
result = main.parse_environment_variables()
assert not result


@pytest.mark.parametrize(
("env", "expected"),
(
(
{},
{},
),
(
{"CONVERT2RHEL_ALLOW_UNAVAILABLE_KMODS": 0},
{},
),
(
{"CONVERT2RHEL_ALLOW_UNAVAILABLE_KMODS": 1},
{"CONVERT2RHEL_ALLOW_UNAVAILABLE_KMODS": 1},
),
(
{"CONVERT2RHEL_CONFIGURE_HOST_METERING": "auto"},
{"CONVERT2RHEL_CONFIGURE_HOST_METERING": "auto"},
),
(
{"CONVERT2RHEL_ALLOW_UNAVAILABLE_KMODS": 0, "SCRIPT_MODE": "ANALYSIS"},
{"SCRIPT_MODE": "ANALYSIS"},
),
(
{
"CONVERT2RHEL_OUTDATED_PACKAGE_CHECK_SKIP": 1,
"CONVERT2RHEL_ALLOW_UNAVAILABLE_KMODS": 0,
"SCRIPT_MODE": "ANALYSIS",
},
{"CONVERT2RHEL_OUTDATED_PACKAGE_CHECK_SKIP": 1, "SCRIPT_MODE": "ANALYSIS"},
),
),
)
def test_prepare_environment_variables(env, expected, monkeypatch):
monkeypatch.setattr(os, "environ", env)
result = main.prepare_environment_variables(env)

assert result == expected
Loading

0 comments on commit 7c75a7f

Please sign in to comment.