Skip to content

Commit

Permalink
Allow HTTP_PROXY on bootstrap and also fix voxpupuli#473
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Vincent authored and krissik committed Jul 30, 2020
1 parent 4044c02 commit 959a8dc
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 54 deletions.
22 changes: 15 additions & 7 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`

Expand Down Expand Up @@ -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`

Expand Down Expand Up @@ -1003,7 +1011,7 @@ Data type: `Any`



Default value: present
Default value: 'present'

##### `version`

Expand Down Expand Up @@ -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`

Expand Down
4 changes: 2 additions & 2 deletions manifests/pip.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [],
Expand Down Expand Up @@ -115,7 +115,7 @@
}

$proxy_flag = $proxy ? {
false => '',
undef => '',
default => "--proxy=${proxy}",
}

Expand Down
49 changes: 31 additions & 18 deletions manifests/pip/bootstrap.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand All @@ -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
Expand All @@ -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'],
}
}
}
}
6 changes: 3 additions & 3 deletions manifests/requirements.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -68,8 +68,8 @@
}

$proxy_flag = $proxy ? {
false => '',
default => "--proxy=${proxy}",
undef => '',
default => "--proxy=${proxy}",
}

$src_flag = $src ? {
Expand Down
51 changes: 27 additions & 24 deletions manifests/virtualenv.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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],
}
Expand Down
30 changes: 30 additions & 0 deletions spec/classes/python_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 } }
Expand Down

0 comments on commit 959a8dc

Please sign in to comment.