Provisioning CoreOS with Ansible pip error - python

I am trying to provision a coreOS box using Ansible. First a bootstapped the box using https://github.com/defunctzombie/ansible-coreos-bootstrap
This seems to work ad all but pip (located in /home/core/bin) is not added to the path. In a next step I am trying to run a task that installs docker-py:
- name: Install docker-py
pip: name=docker-py
As pip's folder is not in path I did it using ansible:
environment:
PATH: /home/core/bin:$PATH
If I am trying to execute this task I get the following error:
fatal: [192.168.0.160]: FAILED! => {"changed": false, "cmd": "/home/core/bin/pip install docker-py", "failed": true, "msg": "\n:stderr: /home/core/bin/pip: line 2: basename: command not found\n/home/core/bin/pip: line 2: /root/pypy/bin/: No such file or directory\n"}
what I ask is where does /root/pypy/bin/ come from it seems this is the problem. Any idea?

You can't use shell-style variable expansion when setting Ansible variables. In this statement...
environment:
PATH: /home/core/bin:$PATH
...you are setting your PATH environment variable to the literal value /home/core/bin:$PATH. In other words, you are blowing away any existing value of $PATH, which is why you're getting "command not found" errors for basic things like basename.
Consider installing pip somewhere in your existing $PATH, modifying $PATH before calling ansible, or calling pip from a shells cript:
- name: install something with pip
shell: |
PATH="/home/core/bin:$PATH"
pip install some_module

The problem lies in /home/core/bin/pip script which is literally:
#!/bin/bash
LD_LIBRARY_PATH=$HOME/pypy/lib:$LD_LIBRARY_PATH $HOME/pypy/bin/$(basename $0) $#
when run under root by ansible the $HOME variable is substituted with /root and not with /home/core.
Change $HOME with /home/core and it should work.

Related

Show Python virtualenv name and a short path in Powershell

I want to simplify my powershell prompt when using Python virtual environment: I want to see the name of the current environment, and the current directory only (not the full path).
For example, I want to convert this
(venv) PS C:User\me\Desktop\project\subfolder>
to this:
(venv) PS subfolder>
I tried many Powershell profile.ps configuration, such as
function prompt {
$p = Split-Path -leaf -path (Get-Location)
"$p> "
}
But this command hides the current virtual environment.
Does someone already achieved to do what I want ?

zsh always show Python virtual env

I currently have this script to show my GitHub branch and virtual env:
setopt PROMPT_SUBST
autoload -Uz vcs_info
precmd() { vcs_info }
zstyle ':vcs_info:git:*' formats '(%b)'
MYPS1=''
MYPS1+='%F{green}'
MYPS1+='${${(%):-%n}:0:1}'
MYPS1+='#'
MYPS1+='${${(%):-%m}:(-4)}' # Get last 4 chars of var machine name
MYPS1+=':'
MYPS1+='%F{yellow}'
MYPS1+='%1~' # Show only the name of the working directory or ~ if it is the home directory
MYPS1+='%F{magenta}'
MYPS1+='${vcs_info_msg_0_}' # Show git branch if any
MYPS1+='%f'
MYPS1+='%# '
PS1=$MYPS1
Sometimes I need to update my .zshrc so I run:
source ~/.zshrc
The problem is, whenever I reload my shell, I cannot see my Python virtual environment anymore even though it's still active.
# After activating virtual env
(my-ve-3.7.13) u#m1:repo-name(github-branch)%
# After reloading my zsh
u#m1:repo-name(github-branch)%
I use pyenv and virtualenvs.
How can I keep the virtual env name in my prompt?
Following #chepner's comment, I figured it out:
Use env to see the list of all env vars. pyenv uses PYENV_VERSION.
Add it to the prompt, use () to have the same look as pyenv does.
...
MYPS1=''
MYPS1+='($PYENV_VERSION) '
MYPS1+='%F{green}'
...

Why can I not create virtualenv using ansible?

I'm trying to create a virtualenv for nodepool user using ansible but it is failing as outlined below. I want to become nodepool user as it uses python3.5 whereas all others use the server default, 2.7.5. It seems that it cannot source the 3.5 version.
The play is:
- name: Create nodepool venv
become: true
become_user: nodepool
become_method: su
command: virtualenv-3.5 /var/lib/nodepool/npvenv
The error is:
fatal: [ca-o3lscizuul]: FAILED! => {"changed": false, "cmd": "virtualenv-3.5 /var/lib/nodepool/npvenv", "failed": true, "msg": "[Errno 2] No such file or directory", "rc": 2}
It works from shell.
[root#host ~]# su nodepool
[nodepool#host root]$ virtualenv-3.5 /var/lib/nodepool/npvenv
Using base prefix '/opt/rh/rh-python35/root/usr'
New python executable in /var/lib/nodepool/npvenv/bin/python3
Also creating executable in /var/lib/nodepool/npvenv/bin/python
Installing setuptools, pip, wheel...done.
Worked around the issue as follows.
shell: source /var/lib/nodepool/.bashrc && virtualenv-3.5 /var/lib/nodepool/npvenv creates="/var/lib/nodepool/npvenv"
It is not as I'd like to do it but it will do. If anyone knows how I might do like originally posted, please advise. Perhaps it's not possible as it doesn't pickup paths etc.
I threw in the creates option as it prevents redoing if it exists.

DistutilsOptionError: must supply either home or prefix/exec-prefix -- not both

I've been usually installed python packages through pip.
For Google App Engine, I need to install packages to another target directory.
I've tried:
pip install -I flask-restful --target ./lib
but it fails with:
must supply either home or prefix/exec-prefix -- not both
How can I get this to work?
Are you using OS X and Homebrew? The Homebrew python page https://github.com/Homebrew/brew/blob/master/docs/Homebrew-and-Python.md calls out a known issue with pip and a work around.
Worked for me.
You can make this "empty prefix" the default by adding a
~/.pydistutils.cfg file with the following contents:
[install]
prefix=
Edit: The Homebrew page was later changed to recommend passing --prefix on the command line, as discussed in the comments below. Here is the last version which contained that text. Unfortunately this only works for sdists, not wheels.
The issue was reported to pip, which later fixed it for --user. That's probably why the section has now been removed from the Homebrew page. However, the problem still occurs when using --target as in the question above.
I believe there is a simpler solution to this problem (Homebrew's Python on macOS) that won't break your normal pip operations.
All you have to do is to create a setup.cfg file at the root directory of your project, usually where your main __init__.py or executable py file is. So if the root folder of your project is: /path/to/my/project/, create a setup.cfg file in there and put the magic words inside:
[install]
prefix=
OK, now you sould be able to run pip's commands for that folder:
pip install package -t /path/to/my/project/
This command will run gracefully for that folder only. Just copy setup.cfg to whatever other projects you might have. No need to write a .pydistutils.cfg on your home directory.
After you are done installing the modules, you may remove setup.cfg.
On OSX(mac), assuming a project folder called /var/myproject
cd /var/myproject
Create a file called setup.cfg and add
[install]
prefix=
Run pip install <packagename> -t .
Another solution* for Homebrew users is simply to use a virtualenv.
Of course, that may remove the need for the target directory anyway - but even if it doesn't, I've found --target works by default (as in, without creating/modifying a config file) when in a virtual environment.
*I say solution; perhaps it's just another motivation to meticulously use venvs...
I hit errors with the other recommendations around --install-option="--prefix=lib". The only thing I found that worked is using PYTHONUSERBASE as described here.
export PYTHONUSERBASE=lib
pip install -I flask-restful --user
this is not exactly the same as --target, but it does the trick for me in any case.
As other mentioned, this is known bug with pip & python installed with homebrew.
If you create ~/.pydistutils.cfg file with "empty prefix" instruction it will fix this problem but it will break normal pip operations.
Until this bug is officially addressed, one of the options would be to create your own bash script that would handle this case:
#!/bin/bash
name=''
target=''
while getopts 'n:t:' flag; do
case "${flag}" in
n) name="${OPTARG}" ;;
t) target="${OPTARG}" ;;
esac
done
if [ -z "$target" ];
then
echo "Target parameter must be provided"
exit 1
fi
if [ -z "$name" ];
then
echo "Name parameter must be provided"
exit 1
fi
# current workaround for homebrew bug
file=$HOME'/.pydistutils.cfg'
touch $file
/bin/cat <<EOM >$file
[install]
prefix=
EOM
# end of current workaround for homebrew bug
pip install -I $name --target $target
# current workaround for homebrew bug
rm -rf $file
# end of current workaround for homebrew bug
This script wraps your command and:
accepts name and target parameters
checks if those parameters are empty
creates ~/.pydistutils.cfg file with "empty prefix" instruction in it
executes your pip command with provided parameters
removes ~/.pydistutils.cfg file
This script can be changed and adapted to address your needs but you get idea. And it allows you to run your command without braking pip. Hope it helps :)
If you're using virtualenv*, it might be a good idea to double check which pip you're using.
If you see something like /usr/local/bin/pip you've broken out of your environment. Reactivating your virtualenv will fix this:
VirtualEnv: $ source bin/activate
VirtualFish: $ vf activate [environ]
*: I use virtualfish, but I assume this tip is relevant to both.
I have a similar issue.
I use the --system flag to avoid the error as I decribe here on other thread where I explain the specific case of my situation.
I post this here expecting that can help anyone facing the same problem.

I am unable to switch virtualenv with virtualenvwrapper in Fabric

I am using virtualenvwrapper to use virtualenv for my Django deployement.
Following is my Fabric task:
proj_path = '/path/to/proj'
def setup_code():
sudo('pip install virtualenvwrapper')
run('export WORKON_HOME=$HOME/.virtualenvs')
run('source /usr/local/bin/virtualenvwrapper.sh && mkvirtualenv myenv')
run('source /usr/local/bin/virtualenvwrapper.sh && workon myenv')
cd(proj_path)
req_file = os.path.join(proj_path, 'requirements.txt')
run('pip install -r %s' % req_file)
I executed the above fab task, but it's behaving strangely. pip starts retrieving all the packages, and then starts to execute the setup file for them. While executing setup file it crashes saying Permission denied.
But why? It's working inside ~ and virtualenv.
What am I doing wrong?
Figured out the problem :
For Fabric :
cd('dir') # doesn't works.
Following works:
with cd('dir'):
print('pwd') # Directory change reflects here.
Similarly, other environmental things like :
run('export WORKON_HOME=$HOME/.virtualenvs')
run('source /usr/local/bin/virtualenvwrapper.sh && mkvirtualenv myenv')
run('source /usr/local/bin/virtualenvwrapper.sh && workon myenv')
But changed to :
with prefix('WORKON_HOME=$HOME/.virtualenvs'):
with prefix('source /usr/local/bin/virtualenvwrapper.sh'):
with prefix('workon myenv'): # Assuming there is a env called `myenv`
run('pip install -r requirements.txt') # Works in virtualenv
Figured it out from the official documentation : http://docs.fabfile.org/en/stable/api/core/context_managers.html
I think thats dont works because then you active virtualenv its do some magic with your existing environment, but as I know fabric doesnt has its own shell with environment. You can try like this:
run('/home/your_folder/virtualenv/bin/pip install -r %s' % req_file)
If you don't want to use your .bashrc, then here's a solution that'll work with latest Fabric (1.10) + virtualenvwrapper (1.11.4):
with shell_env(WORKON_HOME=run('printf $HOME/.virtualenvs'),
prefix('source /usr/share/virtualenvwrapper/virtualenvwrapper.sh'):
run('mkvirtualenv foo')
with prefix('workon foo'):
run('which python')

Categories

Resources