Skip to content

Commit a96f038

Browse files
committed
Add the possibility to specify the pip version in virtual envs
1 parent 911daa0 commit a96f038

File tree

5 files changed

+163
-26
lines changed

5 files changed

+163
-26
lines changed

Diff for: manifests/pyvenv.pp

+7-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
Stdlib::Filemode $mode = '0755',
3232
Array[Stdlib::Absolutepath] $path = ['/bin', '/usr/bin', '/usr/sbin', '/usr/local/bin',],
3333
Array $environment = [],
34+
Python::Venv::PipVersion $pip_version = 'latest',
3435
) {
3536
include python
3637

@@ -78,8 +79,13 @@
7879

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

82+
$pip_upgrade = ($pip_version != 'latest') ? {
83+
true => "--upgrade 'pip ${pip_version}'",
84+
false => '--upgrade pip',
85+
}
86+
8187
exec { "python_virtualenv_${venv_dir}":
82-
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",
88+
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",
8389
user => $owner,
8490
creates => "${venv_dir}/bin/activate",
8591
path => $_path,

Diff for: spec/acceptance/pyvenv_spec.rb

+30-25
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ class { 'python':
1919
system => true,
2020
}
2121
python::pyvenv { '/opt/agent/venv':
22-
ensure => 'present',
23-
systempkgs => true,
24-
owner => 'agent',
25-
group => 'agent',
26-
mode => '0755',
22+
ensure => 'present',
23+
systempkgs => true,
24+
owner => 'agent',
25+
group => 'agent',
26+
mode => '0755',
27+
pip_version => '<= 20.3.4',
2728
}
2829
PUPPET
2930

@@ -50,11 +51,12 @@ class { 'python':
5051
system => true,
5152
}
5253
python::pyvenv { '/opt/agent/venv':
53-
ensure => 'present',
54-
systempkgs => true,
55-
owner => 'agent',
56-
group => 'agent',
57-
mode => '0755',
54+
ensure => 'present',
55+
systempkgs => true,
56+
owner => 'agent',
57+
group => 'agent',
58+
mode => '0755',
59+
pip_version => '<= 20.3.4',
5860
}
5961
python::pip { 'agent' :
6062
ensure => 'latest',
@@ -89,11 +91,12 @@ class { 'python':
8991
system => true,
9092
}
9193
python::pyvenv { '/opt/agent/venv':
92-
ensure => 'present',
93-
systempkgs => true,
94-
owner => 'agent',
95-
group => 'agent',
96-
mode => '0755',
94+
ensure => 'present',
95+
systempkgs => true,
96+
owner => 'agent',
97+
group => 'agent',
98+
mode => '0755',
99+
pip_version => '<= 20.3.4',
97100
}
98101
python::pip { 'agent' :
99102
virtualenv => '/opt/agent/venv',
@@ -125,11 +128,12 @@ class { 'python':
125128
system => true,
126129
}
127130
python::pyvenv { '/opt/agent/venv':
128-
ensure => 'present',
129-
systempkgs => false,
130-
owner => 'agent',
131-
group => 'agent',
132-
mode => '0755',
131+
ensure => 'present',
132+
systempkgs => false,
133+
owner => 'agent',
134+
group => 'agent',
135+
mode => '0755',
136+
pip_version => '<= 20.3.4',
133137
}
134138
python::pip { 'agent' :
135139
virtualenv => '/opt/agent/venv',
@@ -161,11 +165,12 @@ class { 'python':
161165
system => true,
162166
}
163167
python::pyvenv { '/opt/agent/venv':
164-
ensure => 'present',
165-
systempkgs => false,
166-
owner => 'agent',
167-
group => 'agent',
168-
mode => '0755',
168+
ensure => 'present',
169+
systempkgs => false,
170+
owner => 'agent',
171+
group => 'agent',
172+
mode => '0755',
173+
pip_version => '<= 20.3.4',
169174
}
170175
python::pip { 'agent' :
171176
ensure => '0.1.2',

Diff for: spec/classes/python_spec.rb

+62
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,68 @@
9797
end
9898
end
9999

100+
describe 'with python::python_pyvenvs and pip version defined' do
101+
context 'with two pyenvs' do
102+
let(:params) do
103+
{
104+
python_pyvenvs: {
105+
'/opt/env1' => {
106+
version: '3.8',
107+
pip_version: 'latest'
108+
},
109+
'/opt/env2' => {
110+
version: '3.8',
111+
pip_version: '<= 20.3.4'
112+
}
113+
}
114+
}
115+
end
116+
117+
it { is_expected.to compile }
118+
119+
it { is_expected.to contain_python__pyvenv('/opt/env1').with_ensure('present') }
120+
it { is_expected.to contain_python__pyvenv('/opt/env2').with_ensure('present') }
121+
it { is_expected.to contain_exec('python_virtualenv_/opt/env1')
122+
.with(
123+
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',
124+
user: 'root',
125+
creates: '/opt/env1/bin/activate',
126+
path: [
127+
'/bin',
128+
'/usr/bin',
129+
'/usr/sbin',
130+
'/usr/local/bin'
131+
],
132+
cwd: '/tmp',
133+
environment: [],
134+
timeout: 600,
135+
unless: %r{^grep '\^\[\\t \]\*VIRTUAL_ENV=\[\\\\'\\\"\]\*/opt/env1\[\\\"\\\\'\]\[\\t \]\*\$' /opt/env1/bin/activate$}
136+
)
137+
.that_requires('File[/opt/env1]')
138+
}
139+
it { is_expected.to contain_exec('python_virtualenv_/opt/env2')
140+
.with(
141+
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',
142+
user: 'root',
143+
creates: '/opt/env2/bin/activate',
144+
path: [
145+
'/bin',
146+
'/usr/bin',
147+
'/usr/sbin',
148+
'/usr/local/bin'
149+
],
150+
cwd: '/tmp',
151+
environment: [],
152+
timeout: 600,
153+
unless: %r{^grep '\^\[\\t \]\*VIRTUAL_ENV=\[\\\\'\\\"\]\*/opt/env2\[\\\"\\\\'\]\[\\t \]\*\$' /opt/env2/bin/activate$}
154+
)
155+
.that_requires('File[/opt/env2]')
156+
}
157+
it { is_expected.to contain_file('/opt/env1') }
158+
it { is_expected.to contain_file('/opt/env2') }
159+
end
160+
end
161+
100162
describe 'with manage_gunicorn' do
101163
context 'true' do
102164
let(:params) { { manage_gunicorn: true } }

Diff for: spec/type_aliases/venv/pipversion_spec.rb

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
require 'spec_helper'
2+
3+
describe 'Python::Venv::PipVersion' do
4+
describe 'valid values' do
5+
[
6+
'< 1',
7+
'< 0.1',
8+
'< 1.2.3',
9+
'< 1.2.3.40',
10+
'<= 1',
11+
'<= 0.1',
12+
'<= 1.2.3',
13+
'<= 1.2.3.40',
14+
'> 1',
15+
'> 0.1',
16+
'> 1.2.3',
17+
'> 1.2.3.40',
18+
'>= 1',
19+
'>= 0.1',
20+
'>= 1.2.3',
21+
'>= 1.2.3.40',
22+
'== 1',
23+
'== 0.1',
24+
'== 1.2.3',
25+
'== 1.2.3.40',
26+
].each do |value|
27+
describe value.inspect do
28+
it {
29+
is_expected.to allow_value(value)
30+
}
31+
end
32+
end
33+
end
34+
35+
describe 'invalid values' do
36+
[
37+
'+ 1',
38+
'- 0.1',
39+
'< -1',
40+
'< 1.+2.3.40',
41+
'<=',
42+
'<= 0.-1',
43+
'<= 1.f.3',
44+
'1.2.3.0',
45+
'pip > 1',
46+
'all',
47+
-1,
48+
65_536,
49+
:undef,
50+
].each do |value|
51+
describe value.inspect do
52+
it {
53+
is_expected.not_to allow_value(value)
54+
}
55+
end
56+
end
57+
end
58+
end

Diff for: types/venv/pipversion.pp

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# @summary A version type to ensure a specific Pip version in a virtual env.
2+
#
3+
type Python::Venv::PipVersion = Pattern[
4+
/^(<|>|<=|>=|==) [0-9]*(\.[0-9]+)*$/,
5+
/\Alatest\Z/
6+
]

0 commit comments

Comments
 (0)