Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -904,6 +904,7 @@ The following parameters are available in the `python::pyvenv` defined type:
* [`path`](#-python--pyvenv--path)
* [`environment`](#-python--pyvenv--environment)
* [`prompt`](#-python--pyvenv--prompt)
* [`index`](#-python--pyvenv--index)
* [`pip_version`](#-python--pyvenv--pip_version)

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

Default value: `undef`

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

Data type: `Optional[String[1]]`

Optionally specify an index location from where pip and setuptools should be installed

Default value: `undef`

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

Data type: `Python::Venv::PipVersion`
Expand Down
9 changes: 8 additions & 1 deletion 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 index Optionally specify an index location from where pip and setuptools should be installed
#
# @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[String[1]] $index = undef,
) {
include python

Expand Down Expand Up @@ -80,13 +82,18 @@

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

$index_config = $index ? {
undef => '',
default => "-i ${index}"
Copy link
Member

@smortex smortex May 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we not shell_escape / batch_escape / powershell_escape this variable?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}

$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} ${prompt_arg} ${venv_dir} && ${pip_cmd} --log ${venv_dir}/pip.log install ${pip_upgrade} && ${pip_cmd} --log ${venv_dir}/pip.log install --upgrade setuptools",
command => "${virtualenv_cmd} --clear ${system_pkgs_flag} ${prompt_arg} ${venv_dir} && ${pip_cmd} --log ${venv_dir}/pip.log install ${index_config} ${pip_upgrade} && ${pip_cmd} --log ${venv_dir}/pip.log install ${index_config} --upgrade setuptools",
user => $owner,
creates => "${venv_dir}/bin/activate",
path => $_path,
Expand Down
4 changes: 2 additions & 2 deletions spec/classes/python_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
it {
expect(subject).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',
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: [
Expand All @@ -175,7 +175,7 @@
it {
expect(subject).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',
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: [
Expand Down
6 changes: 3 additions & 3 deletions spec/defines/pyvenv_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

context 'with default parameters' do
it { is_expected.to contain_file('/opt/env').that_requires('Class[python::install]') }
it { is_expected.to contain_exec('python_virtualenv_/opt/env').with_command('pyvenv-3.5 --clear /opt/env && /opt/env/bin/pip --log /opt/env/pip.log install --upgrade pip && /opt/env/bin/pip --log /opt/env/pip.log install --upgrade setuptools') }
it { is_expected.to contain_exec('python_virtualenv_/opt/env').with_command('pyvenv-3.5 --clear /opt/env && /opt/env/bin/pip --log /opt/env/pip.log install --upgrade pip && /opt/env/bin/pip --log /opt/env/pip.log install --upgrade setuptools') }
end

describe 'when ensure' do
Expand Down Expand Up @@ -57,7 +57,7 @@

it {
is_expected.to contain_file('/opt/env').that_requires('Class[python::install]')
is_expected.to contain_exec('python_virtualenv_/opt/env').with_command('python3.6 -m venv --clear --prompt custom\\ prompt /opt/env && /opt/env/bin/pip --log /opt/env/pip.log install --upgrade pip && /opt/env/bin/pip --log /opt/env/pip.log install --upgrade setuptools')
is_expected.to contain_exec('python_virtualenv_/opt/env').with_command('python3.6 -m venv --clear --prompt custom\\ prompt /opt/env && /opt/env/bin/pip --log /opt/env/pip.log install --upgrade pip && /opt/env/bin/pip --log /opt/env/pip.log install --upgrade setuptools')
}
end
end
Expand All @@ -81,7 +81,7 @@

it {
is_expected.to contain_file('/opt/env').that_requires('Class[python::install]')
is_expected.to contain_exec('python_virtualenv_/opt/env').with_command('pyvenv-3.5 --clear /opt/env && /opt/env/bin/pip --log /opt/env/pip.log install --upgrade pip && /opt/env/bin/pip --log /opt/env/pip.log install --upgrade setuptools')
is_expected.to contain_exec('python_virtualenv_/opt/env').with_command('pyvenv-3.5 --clear /opt/env && /opt/env/bin/pip --log /opt/env/pip.log install --upgrade pip && /opt/env/bin/pip --log /opt/env/pip.log install --upgrade setuptools')
}
end
end
Expand Down