Running command in activated virtualenv

1,620 views
Skip to first unread message

James O'Neill

unread,
Aug 29, 2013, 8:42:19 AM8/29/13
to salt-...@googlegroups.com
I have the following code in a state file (.sls):

related_fields_patch:
  cmd.run:
    - user: administrator
    - cwd: /home/administrator/www/treeio
    - name: 'DJANGO_DIR=`python -c "import django; import os; print os.path.dirname(django.__file__)"` && python related_fields_patch.py $DJANGO_DIR'
    - require:
      - virtualenv: venv
      - git: tree-io-clone


But it doesn't appear to activate the virtualenv I need it to run within:

----------
    State: - cmd
    Name:      DJANGO_DIR=`python -c "import django; import os; print os.path.dirname(django.__file__)"` && python related_fields_patch.py $DJANGO_DIR
    Function:  run
        Result:    False
        Comment:   Command "DJANGO_DIR=`python -c "import django; import os; print os.path.dirname(django.__file__)"` && python related_fields_patch.py $DJANGO_DIR" run
        Changes:   pid: 2607
                   retcode: 1
                   stderr: Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named django
                   stdout:


I'm running Salt version 0.16.3 on the master and minion. This issue appears to affect all states that require an activated virtualenv, including pip installations; they have been installed as system packages.

How do I get states to apply within an activated virtualenv? Have I misunderstood how to use virtualenv with states?

Thanks,

James

Nick Davis

unread,
Aug 29, 2013, 9:46:31 AM8/29/13
to salt-...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


I've always used my states to create the virtualenv and add python
packages to it. It looks something like this:

First, create virtualenv:

venv:
virtualenv.managed:
- name: /path/to/virtualenv
- distribute: True

Then add packages in requirements file to virtual env:

pip-pkgs:
pip.installed:
- requirements: /path/to/PACKAGES.pip
- bin_env: /path/to/virtualenv
- require:
- virtualenv: venv

And/or add single package to virtual env:

celery:
pip.installed:
- bin_env: /path/to/virtualenv
- upgrade: True
- require:
- virtualenv: venv


- From there, I run a script to execute code that has to run within the
virtualenv.

However, to use a salt cmd state to run python code within a virtualenv,
you might get by with providing the FULL path to the python binary when
you call your command. Try something like this:

related_fields_patch:
cmd.run:
- user: administrator
- cwd: /home/administrator/www/treeio
- name: /path/to/virtualenv/bin/python related_fields_patch.py
`/path/to/virtualenv/bin/python -c "import ....."`
- require:
- virtualenv: venv
- git: tree-io-clone

A bash tip: Instead of doing cmd && cmd, you can place the first part
where the $DJANGO_DIR variable is as I demonstrated above. That will
make it one command.


I hope that helps.

Nick
> --
> You received this message because you are subscribed to the Google
> Groups "Salt-users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to salt-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iEYEARECAAYFAlIfULcACgkQYoNGId/piAuuvQCfeM3SJrQ0DQ8jeCJW9F5au01F
pAEAniUqp0c+efD01nmzD1zwMwAXHXd6
=hDzu
-----END PGP SIGNATURE-----

James O'Neill

unread,
Aug 30, 2013, 8:19:20 AM8/30/13
to salt-...@googlegroups.com
Thank you, Nick.

Adding bin_env to the pip states, and using the full path to the python executable in the virtualenv got everything working. Here's how it looks now:

related_fields_patch:
  cmd.run:
    - user: administrator
    - cwd: /home/administrator/www/treeio
    - name: 'PATH="/home/administrator/www/venv/bin/:$PATH"; python related_fields_patch.py `python -c "import django; import os; print os.path.dirname(django.__file__)"`'

    - require:
      - virtualenv: venv
      - git: tree-io-clone

gunicorn:
  pip.installed:
    - bin_env: /home/administrator/www/venv

    - require:
      - virtualenv: venv


Next step is to move stuff into Pillars.

Nick Davis

unread,
Aug 30, 2013, 9:39:09 AM8/30/13
to salt-...@googlegroups.com
Fantastic. 
Reply all
Reply to author
Forward
0 new messages