Skip to content

Commit

Permalink
Add the possibility to specify the pip version in virtual envs
Browse files Browse the repository at this point in the history
  • Loading branch information
SaschaDoering committed Feb 8, 2021
1 parent 911daa0 commit 605c2ba
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 26 deletions.
8 changes: 7 additions & 1 deletion manifests/pyvenv.pp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
Stdlib::Filemode $mode = '0755',
Array[Stdlib::Absolutepath] $path = ['/bin', '/usr/bin', '/usr/sbin', '/usr/local/bin',],
Array $environment = [],
Python::Venv::PipVersion $pip_version = 'latest',
) {
include python

Expand Down Expand Up @@ -78,8 +79,13 @@

$pip_cmd = "${python::exec_prefix}${venv_dir}/bin/pip"

$pip_upgrade = ($pip_version != 'latest') ? {
true => "--upgrade 'pip ${pip_version}'",
false => '--upgrade pip',
}

exec { "python_virtualenv_${venv_dir}":
command => "${virtualenv_cmd} --clear ${system_pkgs_flag} ${venv_dir} && ${pip_cmd} --log ${venv_dir}/pip.log install --upgrade pip && ${pip_cmd} --log ${venv_dir}/pip.log install --upgrade setuptools",
command => "${virtualenv_cmd} --clear ${system_pkgs_flag} ${venv_dir} && ${pip_cmd} --log ${venv_dir}/pip.log install ${pip_upgrade} && ${pip_cmd} --log ${venv_dir}/pip.log install --upgrade setuptools",
user => $owner,
creates => "${venv_dir}/bin/activate",
path => $_path,
Expand Down
55 changes: 30 additions & 25 deletions spec/acceptance/pyvenv_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ class { 'python':
system => true,
}
python::pyvenv { '/opt/agent/venv':
ensure => 'present',
systempkgs => true,
owner => 'agent',
group => 'agent',
mode => '0755',
ensure => 'present',
systempkgs => true,
owner => 'agent',
group => 'agent',
mode => '0755',
pip_version => '<= 20.3.4',
}
PUPPET

Expand All @@ -50,11 +51,12 @@ class { 'python':
system => true,
}
python::pyvenv { '/opt/agent/venv':
ensure => 'present',
systempkgs => true,
owner => 'agent',
group => 'agent',
mode => '0755',
ensure => 'present',
systempkgs => true,
owner => 'agent',
group => 'agent',
mode => '0755',
pip_version => '<= 20.3.4',
}
python::pip { 'agent' :
ensure => 'latest',
Expand Down Expand Up @@ -89,11 +91,12 @@ class { 'python':
system => true,
}
python::pyvenv { '/opt/agent/venv':
ensure => 'present',
systempkgs => true,
owner => 'agent',
group => 'agent',
mode => '0755',
ensure => 'present',
systempkgs => true,
owner => 'agent',
group => 'agent',
mode => '0755',
pip_version => '<= 20.3.4',
}
python::pip { 'agent' :
virtualenv => '/opt/agent/venv',
Expand Down Expand Up @@ -125,11 +128,12 @@ class { 'python':
system => true,
}
python::pyvenv { '/opt/agent/venv':
ensure => 'present',
systempkgs => false,
owner => 'agent',
group => 'agent',
mode => '0755',
ensure => 'present',
systempkgs => false,
owner => 'agent',
group => 'agent',
mode => '0755',
pip_version => '<= 20.3.4',
}
python::pip { 'agent' :
virtualenv => '/opt/agent/venv',
Expand Down Expand Up @@ -161,11 +165,12 @@ class { 'python':
system => true,
}
python::pyvenv { '/opt/agent/venv':
ensure => 'present',
systempkgs => false,
owner => 'agent',
group => 'agent',
mode => '0755',
ensure => 'present',
systempkgs => false,
owner => 'agent',
group => 'agent',
mode => '0755',
pip_version => '<= 20.3.4',
}
python::pip { 'agent' :
ensure => '0.1.2',
Expand Down
62 changes: 62 additions & 0 deletions spec/classes/python_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,68 @@
end
end

describe 'with python::python_pyvenvs and pip version defined' do
context 'with two pyenvs' do
let(:params) do
{
python_pyvenvs: {
'/opt/env1' => {
version: '3.8',
pip_version: 'latest'
},
'/opt/env2' => {
version: '3.8',
pip_version: '<= 20.3.4'
}
}
}
end

it { is_expected.to compile }

it { is_expected.to contain_python__pyvenv('/opt/env1').with_ensure('present') }
it { is_expected.to contain_python__pyvenv('/opt/env2').with_ensure('present') }
it { is_expected.to contain_exec('python_virtualenv_/opt/env1')
.with(
command: 'python3.8 -m venv --clear /opt/env1 && /opt/env1/bin/pip --log /opt/env1/pip.log install --upgrade pip && /opt/env1/bin/pip --log /opt/env1/pip.log install --upgrade setuptools',
user: 'root',
creates: '/opt/env1/bin/activate',
path: [
'/bin',
'/usr/bin',
'/usr/sbin',
'/usr/local/bin'
],
cwd: '/tmp',
environment: [],
timeout: 600,
unless: %r{^grep '\^\[\\t \]\*VIRTUAL_ENV=\[\\\\'\\\"\]\*/opt/env1\[\\\"\\\\'\]\[\\t \]\*\$' /opt/env1/bin/activate$}
)
.that_requires('File[/opt/env1]')
}
it { is_expected.to contain_exec('python_virtualenv_/opt/env2')
.with(
command: 'python3.8 -m venv --clear /opt/env2 && /opt/env2/bin/pip --log /opt/env2/pip.log install --upgrade \'pip <= 20.3.4\' && /opt/env2/bin/pip --log /opt/env2/pip.log install --upgrade setuptools',
user: 'root',
creates: '/opt/env2/bin/activate',
path: [
'/bin',
'/usr/bin',
'/usr/sbin',
'/usr/local/bin'
],
cwd: '/tmp',
environment: [],
timeout: 600,
unless: %r{^grep '\^\[\\t \]\*VIRTUAL_ENV=\[\\\\'\\\"\]\*/opt/env2\[\\\"\\\\'\]\[\\t \]\*\$' /opt/env2/bin/activate$}
)
.that_requires('File[/opt/env2]')
}
it { is_expected.to contain_file('/opt/env1') }
it { is_expected.to contain_file('/opt/env2') }
end
end

describe 'with manage_gunicorn' do
context 'true' do
let(:params) { { manage_gunicorn: true } }
Expand Down
58 changes: 58 additions & 0 deletions spec/type_aliases/venv/pipversion_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require 'spec_helper'

describe 'Python::Venv::PipVersion' do
describe 'valid values' do
[
'< 1',
'< 0.1',
'< 1.2.3',
'< 1.2.3.40',
'<= 1',
'<= 0.1',
'<= 1.2.3',
'<= 1.2.3.40',
'> 1',
'> 0.1',
'> 1.2.3',
'> 1.2.3.40',
'>= 1',
'>= 0.1',
'>= 1.2.3',
'>= 1.2.3.40',
'== 1',
'== 0.1',
'== 1.2.3',
'== 1.2.3.40',
].each do |value|
describe value.inspect do
it {
is_expected.to allow_value(value)
}
end
end
end

describe 'invalid values' do
[
'+ 1',
'- 0.1',
'< -1',
'< 1.+2.3.40',
'<=',
'<= 0.-1',
'<= 1.f.3',
'1.2.3.0',
'pip > 1',
'all',
-1,
65_536,
:undef,
].each do |value|
describe value.inspect do
it {
is_expected.not_to allow_value(value)
}
end
end
end
end
6 changes: 6 additions & 0 deletions types/venv/pipversion.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# @summary A version type to ensure a specific Pip version in a virtual env.
#
type Python::Venv::PipVersion = Pattern[
/^(<|>|<=|>=|==) [0-9]*(\.[0-9]+)*$/,
/\Alatest\Z/
]

0 comments on commit 605c2ba

Please sign in to comment.