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

allow setting an index URL when creating the venv #660

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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 @@ -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 ${shell_escape($index)}"
}

$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
16 changes: 13 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 All @@ -35,6 +35,16 @@
}
end
end

context 'custom index is provided' do
let :params do
{
index: 'https://site.tld',
}
end

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 -i https://site.tld --upgrade pip && /opt/env/bin/pip --log /opt/env/pip.log install -i https://site.tld --upgrade setuptools') }
end
end

context "prompt on #{os} with python 3.6" do
Expand All @@ -57,7 +67,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 +91,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