Add umask option to pip module

On systems with restrictive umasks, the pip module won't allow you to
install pip packages that are usable by everyone on the system. This
commit adds a umask option to optionally override the umask on a
per-package basis.
This commit is contained in:
Joel Thompson 2016-01-05 00:01:22 -05:00 committed by Matt Clay
parent e5decc3e51
commit d8d8d7da64

View file

@ -22,6 +22,7 @@
import tempfile import tempfile
import re import re
import os import os
import sys
DOCUMENTATION = ''' DOCUMENTATION = '''
--- ---
@ -114,6 +115,17 @@ options:
version_added: "1.3" version_added: "1.3"
required: false required: false
default: null default: null
umask:
description:
- The system umask to apply before installing the pip package. This is
useful, for example, when installing on systems that have a very
restrictive umask by default (e.g., 0077) and you want to pip install
packages which are to be used by all users. Note that this requires you
to specify desired umask mode in octal, with a leading 0 (e.g., 0077).
version_added: "2.1"
required: false
default: null
notes: notes:
- Please note that virtualenv (U(http://www.virtualenv.org/)) must be installed on the remote host if the virtualenv parameter is specified and the virtualenv needs to be initialized. - Please note that virtualenv (U(http://www.virtualenv.org/)) must be installed on the remote host if the virtualenv parameter is specified and the virtualenv needs to be initialized.
requirements: [ "virtualenv", "pip" ] requirements: [ "virtualenv", "pip" ]
@ -159,6 +171,10 @@ EXAMPLES = '''
# Install (Bottle), forcing reinstallation if it's already installed # Install (Bottle), forcing reinstallation if it's already installed
- pip: name=bottle state=forcereinstall - pip: name=bottle state=forcereinstall
# Install (Bottle) while ensuring the umask is 0022 (to ensure other users can use it)
- pip: name=bottle umask=0022
become: True
''' '''
def _get_cmd_options(module, cmd): def _get_cmd_options(module, cmd):
@ -258,6 +274,7 @@ def main():
editable=dict(default='yes', type='bool', required=False), editable=dict(default='yes', type='bool', required=False),
chdir=dict(default=None, required=False, type='path'), chdir=dict(default=None, required=False, type='path'),
executable=dict(default=None, required=False), executable=dict(default=None, required=False),
umask=dict(reqiured=False,default=None),
), ),
required_one_of=[['name', 'requirements']], required_one_of=[['name', 'requirements']],
mutually_exclusive=[['name', 'requirements']], mutually_exclusive=[['name', 'requirements']],
@ -271,7 +288,20 @@ def main():
extra_args = module.params['extra_args'] extra_args = module.params['extra_args']
virtualenv_python = module.params['virtualenv_python'] virtualenv_python = module.params['virtualenv_python']
chdir = module.params['chdir'] chdir = module.params['chdir']
umask = module.params['umask']
if umask and not isinstance(umask, int):
try:
umask = int(umask, 8)
except Exception:
module.fail_json(msg="umask must be an octal integer",
details=str(sys.exc_info()[1]))
old_umask = None
if umask != None:
old_umask = os.umask(umask)
try:
if state == 'latest' and version is not None: if state == 'latest' and version is not None:
module.fail_json(msg='version is incompatible with state=latest') module.fail_json(msg='version is incompatible with state=latest')
@ -364,7 +394,7 @@ def main():
is_present = _is_present(name, version, out.split()) is_present = _is_present(name, version, out.split())
changed = (state == 'present' and not is_present) or (state == 'absent' and is_present) or (state == 'forcereinstall' and is_present) changed = (state == 'present' and not is_present) or (state == 'absent' and is_present)
module.exit_json(changed=changed, cmd=freeze_cmd, stdout=out, stderr=err) module.exit_json(changed=changed, cmd=freeze_cmd, stdout=out, stderr=err)
if requirements or has_vcs: if requirements or has_vcs:
@ -384,6 +414,9 @@ def main():
if state == 'absent': if state == 'absent':
changed = 'Successfully uninstalled' in out_pip changed = 'Successfully uninstalled' in out_pip
else:
if out_freeze_before is None:
changed = 'Successfully installed' in out_pip
else: else:
if out_freeze_before is None: if out_freeze_before is None:
changed = 'Successfully installed' in out_pip changed = 'Successfully installed' in out_pip
@ -394,6 +427,9 @@ def main():
module.exit_json(changed=changed, cmd=cmd, name=name, version=version, module.exit_json(changed=changed, cmd=cmd, name=name, version=version,
state=state, requirements=requirements, virtualenv=env, state=state, requirements=requirements, virtualenv=env,
stdout=out, stderr=err) stdout=out, stderr=err)
finally:
if old_umask != None:
os.umask(old_umask)
# import module snippets # import module snippets
from ansible.module_utils.basic import * from ansible.module_utils.basic import *