From 05cbf65a03eb2c79a204bfd466933f6052026113 Mon Sep 17 00:00:00 2001 From: Wouter Mellema Date: Thu, 8 Feb 2024 16:50:59 +0100 Subject: [PATCH] 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 * Updated reference.md --------- Co-authored-by: Wouter Mellema Co-authored-by: Tim Meusel --- REFERENCE.md | 9 +++++++ manifests/pyvenv.pp | 14 +++++++--- spec/acceptance/pyvenv_spec.rb | 49 ++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 3 deletions(-) diff --git a/REFERENCE.md b/REFERENCE.md index 3c51151b..f3a382e5 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -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) ##### `ensure` @@ -995,6 +996,14 @@ Optionally specify the virtualenv prompt (python >= 3.6) Default value: `undef` +##### `python_path` + +Data type: `Optional[Stdlib::Absolutepath]` + +Optionally specify python path for creation of virtualenv + +Default value: `undef` + ##### `pip_version` Data type: `Python::Venv::PipVersion` diff --git a/manifests/pyvenv.pp b/manifests/pyvenv.pp index 14dcb9fb..5bfc2103 100644 --- a/manifests/pyvenv.pp +++ b/manifests/pyvenv.pp @@ -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' : @@ -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 @@ -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 ? { diff --git a/spec/acceptance/pyvenv_spec.rb b/spec/acceptance/pyvenv_spec.rb index 318c2f73..0b20e27f 100644 --- a/spec/acceptance/pyvenv_spec.rb +++ b/spec/acceptance/pyvenv_spec.rb @@ -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