diff --git a/REFERENCE.md b/REFERENCE.md index 2aac08b3..14e73eba 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -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) ##### `ensure` @@ -986,6 +987,14 @@ Optionally specify the virtualenv prompt (python >= 3.6) Default value: `undef` +##### `index` + +Data type: `Optional[String[1]]` + +Optionally specify an index location from where pip and setuptools should be installed + +Default value: `undef` + ##### `pip_version` Data type: `Python::Venv::PipVersion` diff --git a/manifests/pyvenv.pp b/manifests/pyvenv.pp index 14dcb9fb..08f8db39 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 index Optionally specify an index location from where pip and setuptools should be installed # # @example # python::pyvenv { '/var/www/project1' : @@ -34,6 +35,7 @@ Array $environment = [], Optional[String[1]] $prompt = undef, Python::Venv::PipVersion $pip_version = 'latest', + Optional[String[1]] $index = undef, ) { include python @@ -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, diff --git a/spec/classes/python_spec.rb b/spec/classes/python_spec.rb index cca3ef65..cabd6d51 100644 --- a/spec/classes/python_spec.rb +++ b/spec/classes/python_spec.rb @@ -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: [ @@ -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: [ diff --git a/spec/defines/pyvenv_spec.rb b/spec/defines/pyvenv_spec.rb index ba49fb37..4e4665a9 100644 --- a/spec/defines/pyvenv_spec.rb +++ b/spec/defines/pyvenv_spec.rb @@ -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 @@ -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 @@ -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 @@ -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