Skip to content

Commit 1aaef84

Browse files
committed
Made system virtualenv more robust and ensured that pip logging would occur in an appropriate directory upon failure to avoid errors
1 parent f6fd9f4 commit 1aaef84

3 files changed

Lines changed: 27 additions & 10 deletions

File tree

manifests/pip.pp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
# Sergey Stankevich
2929
#
3030
define python::pip (
31-
$virtualenv,
31+
$virtualenv = 'system',
3232
$ensure = present,
3333
$url = false,
3434
$owner = 'root',
@@ -42,6 +42,20 @@
4242
fail('python::pip: virtualenv parameter must not be empty')
4343
}
4444

45+
if $virtualenv == 'system' and ($owner != 'root' or $group != 'root') {
46+
fail('python::pip: root user must be used when virtualenv is system')
47+
}
48+
49+
$cwd = $virtualenv ? {
50+
'system' => '/',
51+
default => "${virtualenv}",
52+
}
53+
54+
$pip_env = $virtualenv ? {
55+
'system' => '`which pip`',
56+
default => "${virtualenv}/bin/pip",
57+
}
58+
4559
$proxy_flag = $proxy ? {
4660
false => '',
4761
default => "--proxy=${proxy}",
@@ -60,17 +74,17 @@
6074
case $ensure {
6175
present: {
6276
exec { "pip_install_${name}":
63-
command => "${virtualenv}/bin/pip install ${proxy_flag} ${source}",
64-
unless => "${virtualenv}/bin/pip freeze | grep -i -e ${grep_regex}",
77+
command => "$pip_env --log-file ${cwd}/pip.log install ${proxy_flag} ${source}",
78+
unless => "$pip_env freeze | grep -i -e ${grep_regex}",
6579
user => $owner,
6680
environment => $environment,
6781
}
6882
}
6983

7084
default: {
7185
exec { "pip_uninstall_${name}":
72-
command => "echo y | ${virtualenv}/bin/pip uninstall ${proxy_flag} ${name}",
73-
onlyif => "${virtualenv}/bin/pip freeze | grep -i -e ${grep_regex}",
86+
command => "echo y | $pip_env uninstall ${proxy_flag} ${name}",
87+
onlyif => "$pip_env freeze | grep -i -e ${grep_regex}",
7488
user => $owner,
7589
environment => $environment,
7690
}

manifests/requirements.pp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@
3333
$group = 'root'
3434
) {
3535

36+
if $virtualenv == 'system' and ($owner != 'root' or $group != 'root') {
37+
fail('python::pip: root user must be used when virtualenv is system')
38+
}
39+
3640
$cwd = $virtualenv ? {
3741
'system' => '/',
38-
default => "${virtualenv}/bin/",
42+
default => "${virtualenv}",
3943
}
4044

4145
$pip_env = $virtualenv ? {
@@ -64,8 +68,7 @@
6468

6569
exec { "python_requirements${name}":
6670
provider => shell,
67-
command => "${pip_env} install ${proxy_flag} -r ${requirements}",
68-
cwd => $cwd,
71+
command => "${pip_env} --log-file ${cwd}/pip.log install ${proxy_flag} -r ${requirements}",
6972
refreshonly => true,
7073
timeout => 1800,
7174
user => $owner,

manifests/virtualenv.pp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@
8585
}
8686

8787
exec { "python_virtualenv_${venv_dir}":
88-
command => "mkdir -p ${venv_dir} ${proxy_command} && virtualenv -p `which ${python}` ${system_pkgs_flag} ${venv_dir} && ${venv_dir}/bin/pip install ${pypi_index} ${proxy_flag} --upgrade ${distribute_pkg} pip",
88+
command => "mkdir -p ${venv_dir} ${proxy_command} && virtualenv -p `which ${python}` ${system_pkgs_flag} ${venv_dir} && ${venv_dir}/bin/pip --log-file ${venv_dir}/pip.log install ${pypi_index} ${proxy_flag} --upgrade ${distribute_pkg} pip",
8989
user => $owner,
9090
creates => "${venv_dir}/bin/activate",
9191
path => [ '/bin', '/usr/bin', '/usr/sbin' ],
9292
}
9393

9494
if $requirements {
9595
exec { "python_requirements_initial_install_${requirements}_${venv_dir}":
96-
command => "${venv_dir}/bin/pip install ${pypi_index} ${proxy_flag} --requirement ${requirements}",
96+
command => "${venv_dir}/bin/pip --log-file ${venv_dir}/pip.log install ${pypi_index} ${proxy_flag} --requirement ${requirements}",
9797
refreshonly => true,
9898
timeout => 1800,
9999
user => $owner,

0 commit comments

Comments
 (0)