saltstack / salt

Software to automate the management and configuration of any infrastructure or application at scale. Get access to the Salt software package repository here:
https://repo.saltproject.io/
Apache License 2.0
14.13k stars 5.47k forks source link

pip.install's `log` param should permit a new file to be created #44722

Open corywright opened 6 years ago

corywright commented 6 years ago

Description of Issue/Question

When using the pip.installed state and specifying a log parameter the state will fail if the log file does not yet exist. If the log file does not exist and the parent directory is writable then pip.installed should create the file.

The issue is in the underlying pip.install execution module function.

Setup / Steps to Reproduce Issue

I'm trying to install Gnocchi via pip:

gnocchi:
  pip.installed:
    - name: gnocchi[keystone,postgresql,redis]=={{ gnocchi_version }}
    - bin_env: /opt/gnocchi/gnocchi-{{ gnocchi_version }}/bin/pip
    - log: /opt/gnocchi/pip-install-gnocchi-{{ gnocchi_version }}.log

I want to be able to dynamically create a new log file during each pip install. The above should create a /opt/gnocchi/pip-install-gnocchi-{{ gnocchi_version }}.log log file during a highstate, but it does not, and fails with the following error:

----------
      ID: gnocchi
Function: pip.installed
    Name: gnocchi[keystone,postgresql,redis]==4.1.1
  Result: False
 Comment: An exception occurred in this state: Traceback (most recent call last):
            File "/usr/lib/python2.7/dist-packages/salt/state.py", line 1750, in call
              **cdata['kwargs'])
            File "/usr/lib/python2.7/dist-packages/salt/loader.py", line 1705, in wrapper
              return f(*args, **kwargs)
            File "/usr/lib/python2.7/dist-packages/salt/states/pip_state.py", line 708, in installed
              no_cache_dir=no_cache_dir
            File "/usr/lib/python2.7/dist-packages/salt/modules/pip.py", line 602, in install
              raise IOError('\'{0}\' is not writeable'.format(log))
          IOError: '/opt/gnocchi/pip-install-gnocchi-4.1.1.log' is not writeable
 Started: 21:17:32.850211
Duration: 672.218 ms
 Changes:   
----------

If I simply touch the log file before highstating then it succeeds:

# touch /opt/gnocchi/pip-install-gnocchi-4.1.1.log

The problem seems to have been introduced with PR #29470, which added permission checks to pip.install. The check verifies write access to an existing file, but does not check if the file exists or not. There should be an additional check to see if the file does not exist and then verify that the parent directory is writable.

if log:
    if os.path.isdir(log):
        raise IOError(
            '\'{0}\' is a directory. Use --log path_to_file'.format(log))
    elif not os.access(log, os.W_OK):
        raise IOError('\'{0}\' is not writeable'.format(log))

Versions Report

I'm on 2016.11 but the issue exists in develop as well.

Salt Version:
           Salt: 2016.11.7

Dependency Versions:
           cffi: Not Installed
       cherrypy: 3.2.2
       dateutil: 1.5
      docker-py: Not Installed
          gitdb: 0.5.4
      gitpython: 0.3.2 RC1
          ioflo: Not Installed
         Jinja2: 2.7.2
        libgit2: Not Installed
        libnacl: Not Installed
       M2Crypto: Not Installed
           Mako: 0.9.1
   msgpack-pure: Not Installed
 msgpack-python: 0.4.6
   mysql-python: 1.2.3
      pycparser: Not Installed
       pycrypto: 2.6.1
   pycryptodome: Not Installed
         pygit2: Not Installed
         Python: 2.7.6 (default, Jun 22 2015, 17:58:13)
   python-gnupg: Not Installed
         PyYAML: 3.10
          PyZMQ: 14.0.1
           RAET: Not Installed
          smmap: 0.8.2
        timelib: Not Installed
        Tornado: 4.2.1
            ZMQ: 4.0.5

System Versions:
           dist: Ubuntu 14.04 trusty
        machine: x86_64
        release: 3.16.0-77-generic
         system: Linux
        version: Ubuntu 14.04 trusty
gtmanfred commented 6 years ago

Yikes.

That seems like a totally reasonable path to make this work.

Would you mind submitting a PR to fix this?

Thanks, Daniel

corywright commented 6 years ago

@gtmanfred Sure, I need to finish the project I'm working on and then I'll get a PR submitted.

gtmanfred commented 6 years ago

Awesome!

Much appreciated!

Daniel

stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

If this issue is closed prematurely, please leave a comment and we will gladly reopen the issue.

corywright commented 5 years ago

Commenting to keep this issue open.

stale[bot] commented 5 years ago

Thank you for updating this issue. It is no longer marked as stale.

sushantkumr commented 4 years ago

Hey ! Is there any progress on this issue? This seems to be affecting us as well.