Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mitogen
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
mitogen
Commits
cd1683b9
Commit
cd1683b9
authored
Mar 27, 2018
by
David Wilson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ansible: split environment editing into a separate class.
parent
fccca540
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
25 additions
and
18 deletions
+25
-18
ansible_mitogen/helpers.py
ansible_mitogen/helpers.py
+25
-18
No files found.
ansible_mitogen/helpers.py
View file @
cd1683b9
...
...
@@ -112,6 +112,17 @@ def module_fixups(mod):
mod
.
YumRepo
.
repofile
=
mod
.
configparser
.
RawConfigParser
()
class
TemporaryEnvironment
(
object
):
def
__init__
(
self
,
env
=
None
):
self
.
original
=
os
.
environ
.
copy
()
self
.
env
=
env
or
{}
os
.
environ
.
update
((
k
,
str
(
v
))
for
k
,
v
in
self
.
env
.
iteritems
())
def
revert
(
self
):
os
.
environ
.
clear
()
os
.
environ
.
update
(
self
.
original
)
def
run_module
(
module
,
raw_params
=
None
,
args
=
None
,
env
=
None
):
"""
Set up the process environment in preparation for running an Ansible
...
...
@@ -130,25 +141,21 @@ def run_module(module, raw_params=None, args=None, env=None):
'ANSIBLE_MODULE_ARGS'
:
args
})
if
env
:
original_env
=
os
.
environ
.
copy
()
os
.
environ
.
update
((
k
,
str
(
v
))
for
k
,
v
in
env
.
iteritems
())
temp_env
=
TemporaryEnvironment
(
env
)
try
:
mod
=
__import__
(
module
,
{},
{},
[
''
])
module_fixups
(
mod
)
# Ansible modules begin execution on import. Thus the above __import__
# will cause either Exit or ModuleError to be raised. If we reach the
# line below, the module did not execute and must already have been
# imported for a previous invocation, so we need to invoke main
# explicitly.
mod
.
main
()
except
(
Exit
,
ModuleError
),
e
:
result
=
json
.
dumps
(
e
.
dct
)
if
env
:
os
.
environ
.
clear
()
os
.
environ
.
update
(
original_env
)
try
:
mod
=
__import__
(
module
,
{},
{},
[
''
])
module_fixups
(
mod
)
# Ansible modules begin execution on import. Thus the above __import__
# will cause either Exit or ModuleError to be raised. If we reach the
# line below, the module did not execute and must already have been
# imported for a previous invocation, so we need to invoke main
# explicitly.
mod
.
main
()
except
(
Exit
,
ModuleError
),
e
:
result
=
json
.
dumps
(
e
.
dct
)
finally
:
temp_env
.
revert
()
return
result
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment