diff --git a/REFERENCE.md b/REFERENCE.md index e526dc66..69e58da4 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -269,6 +269,14 @@ if python module will manage deps Default value: `false` +##### `http_proxy` + +Data type: `Optional[Stdlib::HTTPUrl]` + +Proxy server to use for outbound connections. + +Default value: `undef` + ## Defined types ### python::dotfile @@ -652,11 +660,11 @@ Default value: `false` ##### `proxy` -Data type: `Variant[Boolean, String]` +Data type: `Optional[Stdlib::HTTPUrl]` Proxy server to use for outbound connections. -Default value: `false` +Default value: `undef` ##### `editable` @@ -896,11 +904,11 @@ Default value: 'root' ##### `proxy` -Data type: `Any` +Data type: `Optional[Stdlib::HTTPUrl]` Proxy server to use for outbound connections. -Default value: `false` +Default value: `undef` ##### `src` @@ -1003,7 +1011,7 @@ Data type: `Any` -Default value: present +Default value: 'present' ##### `version` @@ -1087,11 +1095,11 @@ Default value: '0755' ##### `proxy` -Data type: `Any` +Data type: `Optional[Stdlib::HTTPUrl]` Proxy server to use for outbound connections -Default value: `false` +Default value: `undef` ##### `environment` diff --git a/manifests/pip.pp b/manifests/pip.pp index b3f49a8a..569d62f9 100644 --- a/manifests/pip.pp +++ b/manifests/pip.pp @@ -57,7 +57,7 @@ $group = getvar('python::params::group'), $umask = undef, $index = false, - Variant[Boolean, String] $proxy = false, + Optional[Stdlib::HTTPUrl] $proxy = undef, $egg = false, Boolean $editable = false, $environment = [], @@ -115,7 +115,7 @@ } $proxy_flag = $proxy ? { - false => '', + undef => '', default => "--proxy=${proxy}", } diff --git a/manifests/pip/bootstrap.pp b/manifests/pip/bootstrap.pp index 10b3c348..f5b18c02 100644 --- a/manifests/pip/bootstrap.pp +++ b/manifests/pip/bootstrap.pp @@ -3,6 +3,7 @@ # # @param version should be pip or pip3 # @param manage_python if python module will manage deps +# @param http_proxy Proxy server to use for outbound connections. # # @example # class { 'python::pip::bootstrap': @@ -11,6 +12,7 @@ class python::pip::bootstrap ( Enum['pip', 'pip3'] $version = 'pip', Variant[Boolean, String] $manage_python = false, + Optional[Stdlib::HTTPUrl] $http_proxy = undef, ) inherits ::python::params { if $manage_python { include python @@ -19,34 +21,45 @@ 'AIX' => '/opt/freeware/bin', default => '/usr/bin' } + + $environ = $http_proxy ? { + undef => [], + default => $facts['os']['family'] ? { + 'AIX' => [ "http_proxy=${http_proxy}", "https_proxy=${http_proxy}" ], + default => [ "HTTP_PROXY=${http_proxy}", "HTTPS_PROXY=${http_proxy}" ], + } + } + if $version == 'pip3' { exec { 'bootstrap pip3': - command => '/usr/bin/curl https://bootstrap.pypa.io/get-pip.py | python3', - unless => 'which pip3', - path => $python::params::pip_lookup_path, - require => Package['python3'], + command => '/usr/bin/curl https://bootstrap.pypa.io/get-pip.py | python3', + environment => $environ, + unless => 'which pip3', + path => $python::params::pip_lookup_path, + require => Package['python3'], } # puppet is opinionated about the pip command name file { 'pip3-python': ensure => link, path => '/usr/bin/pip3', - target => "${target_src_pip_path}/pip${::facts['python3_release']}", + target => "${target_src_pip_path}/pip${facts['python3_release']}", require => Exec['bootstrap pip3'], } } else { - exec { 'bootstrap pip': - command => '/usr/bin/curl https://bootstrap.pypa.io/get-pip.py | python', - unless => 'which pip', - path => $python::params::pip_lookup_path, - require => Package['python'], - } - # puppet is opinionated about the pip command name - file { 'pip-python': - ensure => link, - path => '/usr/bin/pip', - target => "${target_src_pip_path}/pip${::facts['python2_release']}", - require => Exec['bootstrap pip'], - } + exec { 'bootstrap pip': + command => '/usr/bin/curl https://bootstrap.pypa.io/get-pip.py | python', + environment => $environ, + unless => 'which pip', + path => $python::params::pip_lookup_path, + require => Package['python'], + } + # puppet is opinionated about the pip command name + file { 'pip-python': + ensure => link, + path => '/usr/bin/pip', + target => "${target_src_pip_path}/pip${facts['python2_release']}", + require => Exec['bootstrap pip'], + } } } } diff --git a/manifests/requirements.pp b/manifests/requirements.pp index 7d2a54a9..8afe8568 100644 --- a/manifests/requirements.pp +++ b/manifests/requirements.pp @@ -31,7 +31,7 @@ Enum['pip', 'pip3'] $pip_provider = 'pip', $owner = 'root', $group = 'root', - $proxy = false, + Optional[Stdlib::HTTPUrl] $proxy = undef, $src = false, $environment = [], $forceupdate = false, @@ -68,8 +68,8 @@ } $proxy_flag = $proxy ? { - false => '', - default => "--proxy=${proxy}", + undef => '', + default => "--proxy=${proxy}", } $src_flag = $src ? { diff --git a/manifests/virtualenv.pp b/manifests/virtualenv.pp index 48885c56..ebc22498 100644 --- a/manifests/virtualenv.pp +++ b/manifests/virtualenv.pp @@ -31,25 +31,25 @@ # } # define python::virtualenv ( - $ensure = present, - $version = 'system', - $requirements = false, - $systempkgs = false, - $venv_dir = $name, - $ensure_venv_dir = true, - $distribute = true, - $index = false, - $owner = 'root', - $group = 'root', - $mode = '0755', - $proxy = false, - $environment = [], - $path = [ '/bin', '/usr/bin', '/usr/sbin', '/usr/local/bin' ], - $cwd = undef, - $timeout = 1800, - $pip_args = '', - $extra_pip_args = '', - $virtualenv = undef + $ensure = 'present', + $version = 'system', + $requirements = false, + $systempkgs = false, + $venv_dir = $name, + $ensure_venv_dir = true, + $distribute = true, + $index = false, + $owner = 'root', + $group = 'root', + $mode = '0755', + Optional[Stdlib::HTTPUrl] $proxy = undef, + $environment = [], + $path = [ '/bin', '/usr/bin', '/usr/sbin', '/usr/local/bin' ], + $cwd = undef, + $timeout = 1800, + $pip_args = '', + $extra_pip_args = '', + $virtualenv = undef, ) { include python $python_provider = getparam(Class['python'], 'provider') @@ -78,9 +78,12 @@ default => "--proxy=${proxy}", } - $proxy_command = $proxy ? { - false => '', - default => "&& export http_proxy=${proxy}", + $proxy_hash = $proxy ? { + undef => {}, + default => $facts['os']['family'] ? { + 'AIX' => { 'http_proxy' => $proxy, 'https_proxy' => $proxy }, + default => { 'HTTP_PROXY' => $proxy, 'HTTPS_PROXY' => $proxy }, + } } # Virtualenv versions prior to 1.7 do not support the @@ -134,12 +137,12 @@ $pip_flags = "${pypi_index} ${proxy_flag} ${pip_args}" exec { "python_virtualenv_${venv_dir}": - command => "true ${proxy_command} && ${virtualenv_cmd} ${system_pkgs_flag} -p ${python} ${venv_dir} && ${pip_cmd} --log ${venv_dir}/pip.log install ${pip_flags} --upgrade pip && ${pip_cmd} install ${pip_flags} --upgrade ${distribute_pkg}", + command => "${virtualenv_cmd} ${system_pkgs_flag} -p ${python} ${venv_dir} && ${pip_cmd} --log ${venv_dir}/pip.log install ${pip_flags} --upgrade pip && ${pip_cmd} install ${pip_flags} --upgrade ${distribute_pkg}", user => $owner, creates => "${venv_dir}/bin/activate", path => $_path, cwd => '/tmp', - environment => $environment, + environment => (Hash($environment.map |$val| { $val.split(/=|$/) }) + $proxy_hash).map|$key, $val| { "${key}=${val}" }, unless => "grep '^[\\t ]*VIRTUAL_ENV=[\\\\'\\\"]*${venv_dir}[\\\"\\\\'][\\t ]*$' ${venv_dir}/bin/activate", #Unless activate exists and VIRTUAL_ENV is correct we re-create the virtualenv require => File[$venv_dir], } diff --git a/spec/classes/python_spec.rb b/spec/classes/python_spec.rb index 208462ca..76d62c41 100644 --- a/spec/classes/python_spec.rb +++ b/spec/classes/python_spec.rb @@ -42,6 +42,36 @@ end end + describe 'with python::python_virtualenvs' do + context 'when `proxy` set' do + let(:params) do + { + python_virtualenvs: { + '/opt/env1' => { + proxy: 'http://example.com:3128' + } + } + } + end + + it { is_expected.to contain_exec('python_virtualenv_/opt/env1').with_environment(['HTTP_PROXY=http://example.com:3128', 'HTTPS_PROXY=http://example.com:3128']) } + end + context 'when `proxy` and `environment` have conflicting parameters' do + let(:params) do + { + python_virtualenvs: { + '/opt/env1' => { + proxy: 'http://example.com:3128', + environment: ['HTTP_PROXY=http://example.com:8080'] + } + } + } + end + + it { is_expected.to contain_exec('python_virtualenv_/opt/env1').with_environment(['HTTP_PROXY=http://example.com:3128', 'HTTPS_PROXY=http://example.com:3128']) } + end + end + describe 'with manage_gunicorn' do context 'true' do let(:params) { { manage_gunicorn: true } }