Skip to content

Commit 05cbf65

Browse files
wmellemaWouter Mellemabastelfreak
authored
Add python_path to pyvenv class (#686)
* Add python_path to pyvenv class Issue #685 - Add: `python_path` variable to `python::pyvenv` * Fixed whitespace issue * Update manifests/pyvenv.pp Co-authored-by: Tim Meusel <tim@bastelfreak.de> * Updated reference.md --------- Co-authored-by: Wouter Mellema <w.mellema@iwink.nl> Co-authored-by: Tim Meusel <tim@bastelfreak.de>
1 parent 8f09514 commit 05cbf65

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

REFERENCE.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,7 @@ The following parameters are available in the `python::pyvenv` defined type:
913913
* [`path`](#-python--pyvenv--path)
914914
* [`environment`](#-python--pyvenv--environment)
915915
* [`prompt`](#-python--pyvenv--prompt)
916+
* [`python_path`](#-python--pyvenv--python_path)
916917
* [`pip_version`](#-python--pyvenv--pip_version)
917918

918919
##### <a name="-python--pyvenv--ensure"></a>`ensure`
@@ -995,6 +996,14 @@ Optionally specify the virtualenv prompt (python >= 3.6)
995996

996997
Default value: `undef`
997998

999+
##### <a name="-python--pyvenv--python_path"></a>`python_path`
1000+
1001+
Data type: `Optional[Stdlib::Absolutepath]`
1002+
1003+
Optionally specify python path for creation of virtualenv
1004+
1005+
Default value: `undef`
1006+
9981007
##### <a name="-python--pyvenv--pip_version"></a>`pip_version`
9991008

10001009
Data type: `Python::Venv::PipVersion`

manifests/pyvenv.pp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# @param path Specifies the PATH variable.
1212
# @param environment Optionally specify environment variables for pyvenv
1313
# @param prompt Optionally specify the virtualenv prompt (python >= 3.6)
14+
# @param python_path Optionally specify python path for creation of virtualenv
1415
#
1516
# @example
1617
# python::pyvenv { '/var/www/project1' :
@@ -34,6 +35,7 @@
3435
Array $environment = [],
3536
Optional[String[1]] $prompt = undef,
3637
Python::Venv::PipVersion $pip_version = 'latest',
38+
Optional[Stdlib::Absolutepath] $python_path = undef,
3739
) {
3840
include python
3941

@@ -46,11 +48,17 @@
4648
$python_version_parts = split($python_version, '[.]')
4749
$normalized_python_version = sprintf('%s.%s', $python_version_parts[0], $python_version_parts[1])
4850

51+
$local_exec_prefix = $python_path ? {
52+
undef => $python::exec_prefix,
53+
default => $python_path,
54+
}
4955
# pyvenv is deprecated since 3.6 and will be removed in 3.8
50-
if versioncmp($normalized_python_version, '3.6') >=0 {
51-
$virtualenv_cmd = "${python::exec_prefix}python${normalized_python_version} -m venv"
56+
if $python_path != undef {
57+
$virtualenv_cmd = "${python_path} -m venv"
58+
} elsif versioncmp($normalized_python_version, '3.6') >=0 {
59+
$virtualenv_cmd = "${local_exec_prefix}python${normalized_python_version} -m venv"
5260
} else {
53-
$virtualenv_cmd = "${python::exec_prefix}pyvenv-${normalized_python_version}"
61+
$virtualenv_cmd = "${local_exec_prefix}pyvenv-${normalized_python_version}"
5462
}
5563

5664
$_path = $python::provider ? {

spec/acceptance/pyvenv_spec.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,53 @@ class { 'python':
192192
its(:stdout) { is_expected.to match %r{agent.* 0\.1\.2} }
193193
end
194194
end
195+
196+
context 'with versioned minimal python::pip and without systempkgs using custom python path' do
197+
it 'works with no errors' do
198+
pp = <<-PUPPET
199+
200+
class { 'python':
201+
dev => 'present',
202+
venv => 'present',
203+
}
204+
file { '/usr/bin/mycustompython':
205+
ensure => link,
206+
target => '/usr/bin/python',
207+
}
208+
user { 'agent':
209+
ensure => 'present',
210+
managehome => true,
211+
home => '/opt/agent',
212+
}
213+
group { 'agent':
214+
ensure => 'present',
215+
system => true,
216+
}
217+
python::pyvenv { '/opt/agent/venv':
218+
ensure => 'present',
219+
systempkgs => false,
220+
owner => 'agent',
221+
group => 'agent',
222+
mode => '0755',
223+
pip_version => '<= 20.3.4',
224+
python_path => '/usr/bin/mycustompython',
225+
}
226+
python::pip { 'agent' :
227+
ensure => '0.1.2',
228+
virtualenv => '/opt/agent/venv',
229+
owner => 'agent',
230+
group => 'agent',
231+
}
232+
PUPPET
233+
234+
# Run it twice and test for idempotency
235+
apply_manifest(pp, catch_failures: true)
236+
apply_manifest(pp, catch_changes: true)
237+
end
238+
239+
describe command('/opt/agent/venv/bin/pip list') do
240+
its(:exit_status) { is_expected.to eq 0 }
241+
its(:stdout) { is_expected.to match %r{agent.* 0\.1\.2} }
242+
end
243+
end
195244
end

0 commit comments

Comments
 (0)