8000 Avoid direct use of os.environ by ssbarnea · Pull Request #71 · ansible/ansible-compat · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Avoid direct use of os.environ #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 5, 2021
Merged

Avoid direct use of os.environ #71

merged 1 commit into from
Sep 5, 2021

Conversation

ssbarnea
Copy link
Member
@ssbarnea ssbarnea commented Sep 5, 2021

From now on, Runtime() will make a copy of os.environ and use that
instead, eventually making required modifications to it. This ensures
that modifications made by our runtime will not affect current
interpreter environment.

This sorts a bug observed during testing where modification to
os.environ started to cross-contaminate between tests.

Another benefit from this change is that it makes easier to adopt use
of containerized/remote ansible runtimes like execution engine. This
change should be considered major because it means that any consumer
of the library must use of Runtime.exec() to execute commands
using the modified environment. If they used subprocess.run or similar
they will need at least to pass Runtime.environ in order to avoid
execution errors as os.environ is no longer altered.

@ssbarnea
Copy link
Member Author
ssbarnea commented Sep 5, 2021

@webknjaz @tadeboro I need to pick your brain on one thing. Both Runtime() and AnsibleConfig() to need access to our environ and both execute code using this environment.

I could try to pass Runtime.environ to AnsibleConfig in the constructor. Still this will not enable me to reuse Runtime.exec() inside config instance.

Another option would be to pass runtime instance when creating AnsibleConfig, but this will mean that those two classes will cross linked. I am not sure how big of a problem is that but at least it allows us to keep execution in one place, which would clearly be a big advantage towards enabling use of ansible-runner.

I already considered keeping environ only inside config, but that is not sorting the execution bit, only making a little bit harder to access via runtime.config.environ instead of the more direct runtime.environ.

WDYT?

@ssbarnea ssbarnea marked this pull request as ready for review September 5, 2021 12:05
@ssbarnea ssbarnea requested a review from webknjaz as a code owner September 5, 2021 12:05
@ssbarnea ssbarnea requested a review from tadeboro September 5, 2021 12:05
From now on, Runtime() will make a copy of os.environ and use that
instead, eventually making required modifications to it. This ensures
that modifications made by our runtime will not affect current
interpreter environment.

This sorts a bug observed during testing where modification to
os.environ started to cross-contaminate between tests.

Another benefit from this change is that it makes easier to adopt use
of containerized/remote ansible runtimes like execution engine. This
change should be considered major because it means that any consumer
of the library must use of `Runtime.exec()` to execute commands
using the modified environment. If they used subprocess.run or similar
they will need at least to pass Runtime.environ in order to avoid
execution errors as os.environ is no longer altered.
@tadeboro
Copy link
tadeboro commented Sep 5, 2021

@webknjaz @tadeboro I need to pick your brain on one thing. Both Runtime() and AnsibleConfig() to need access to our environ and both execute code using this environment.

I could try to pass Runtime.environ to AnsibleConfig in the constructor. Still this will not enable me to reuse Runtime.exec() inside config instance.

Another option would be to pass runtime instance when creating AnsibleConfig, but this will mean that those two classes will cross linked. I am not sure how big of a problem is that but at least it allows us to keep execution in one place, which would clearly be a big advantage towards enabling use of ansible-runner.

I already considered keeping environ only inside config, but that is not sorting the execution bit, only making a little bit harder to access via runtime.config.environ instead of the more direct runtime.environ.

WDYT?

I would go in a slightly different direction and make AnsibleConfig a pure data container with no dynamic parts and move all process-execution parts to the Runtime object. Library consumers would then get their hands on the ansible config through the config property of the Runtime instance.

@ssbarnea ssbarnea merged commit d6cf84f into main Sep 5, 2021
@ssbarnea ssbarnea deleted the fix/api branch September 5, 2021 15:02
@ssbarnea ssbarnea changed the title Avoid use direct use of os.environ Avoid direct use of os.environ Dec 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0