Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add python_path to pyvenv class #686

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,7 @@ The following parameters are available in the `python::pyvenv` defined type:
* [`path`](#-python--pyvenv--path)
* [`environment`](#-python--pyvenv--environment)
* [`prompt`](#-python--pyvenv--prompt)
* [`python_path`](#-python--pyvenv--python_path)
* [`pip_version`](#-python--pyvenv--pip_version)

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

Default value: `undef`

##### <a name="-python--pyvenv--python_path"></a>`python_path`

Data type: `Optional[Stdlib::Absolutepath]`

Optionally specify python path for creation of virtualenv

Default value: `undef`

##### <a name="-python--pyvenv--pip_version"></a>`pip_version`

Data type: `Python::Venv::PipVersion`
Expand Down
14 changes: 11 additions & 3 deletions manifests/pyvenv.pp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# @param path Specifies the PATH variable.
# @param environment Optionally specify environment variables for pyvenv
# @param prompt Optionally specify the virtualenv prompt (python >= 3.6)
# @param python_path Optionally specify python path for creation of virtualenv
#
# @example
# python::pyvenv { '/var/www/project1' :
Expand All @@ -34,6 +35,7 @@
Array $environment = [],
Optional[String[1]] $prompt = undef,
Python::Venv::PipVersion $pip_version = 'latest',
Optional[Stdlib::Absolutepath] $python_path = undef,
) {
include python

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

$local_exec_prefix = $python_path ? {
undef => $python::exec_prefix,
default => $python_path,
}
# pyvenv is deprecated since 3.6 and will be removed in 3.8
if versioncmp($normalized_python_version, '3.6') >=0 {
$virtualenv_cmd = "${python::exec_prefix}python${normalized_python_version} -m venv"
if $python_path != undef {
$virtualenv_cmd = "${python_path} -m venv"
} elsif versioncmp($normalized_python_version, '3.6') >=0 {
$virtualenv_cmd = "${local_exec_prefix}python${normalized_python_version} -m venv"
} else {
$virtualenv_cmd = "${python::exec_prefix}pyvenv-${normalized_python_version}"
$virtualenv_cmd = "${local_exec_prefix}pyvenv-${normalized_python_version}"
}

$_path = $python::provider ? {
Expand Down
49 changes: 49 additions & 0 deletions spec/acceptance/pyvenv_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,53 @@ class { 'python':
its(:stdout) { is_expected.to match %r{agent.* 0\.1\.2} }
end
end

context 'with versioned minimal python::pip and without systempkgs using custom python path' do
it 'works with no errors' do
pp = <<-PUPPET

class { 'python':
dev => 'present',
venv => 'present',
}
file { '/usr/bin/mycustompython':
ensure => link,
target => '/usr/bin/python',
}
user { 'agent':
ensure => 'present',
managehome => true,
home => '/opt/agent',
}
group { 'agent':
ensure => 'present',
system => true,
}
python::pyvenv { '/opt/agent/venv':
ensure => 'present',
systempkgs => false,
owner => 'agent',
group => 'agent',
mode => '0755',
pip_version => '<= 20.3.4',
python_path => '/usr/bin/mycustompython',
}
python::pip { 'agent' :
ensure => '0.1.2',
virtualenv => '/opt/agent/venv',
owner => 'agent',
group => 'agent',
}
PUPPET

# Run it twice and test for idempotency
apply_manifest(pp, catch_failures: true)
apply_manifest(pp, catch_changes: true)
end

describe command('/opt/agent/venv/bin/pip list') do
its(:exit_status) { is_expected.to eq 0 }
its(:stdout) { is_expected.to match %r{agent.* 0\.1\.2} }
end
end
end