Skip to content

Commit 7de58f4

Browse files
author
Robert Vincent
committed
Allow HTTP_PROXY on bootstrap and also fix voxpupuli#473
1 parent fbc2fda commit 7de58f4

6 files changed

Lines changed: 108 additions & 54 deletions

File tree

REFERENCE.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,14 @@ if python module will manage deps
269269

270270
Default value: `false`
271271

272+
##### `http_proxy`
273+
274+
Data type: `Optional[Stdlib::HTTPUrl]`
275+
276+
Proxy server to use for outbound connections.
277+
278+
Default value: `undef`
279+
272280
## Defined types
273281

274282
### python::dotfile
@@ -652,11 +660,11 @@ Default value: `false`
652660

653661
##### `proxy`
654662

655-
Data type: `Variant[Boolean, String]`
663+
Data type: `Optional[Stdlib::HTTPUrl]`
656664

657665
Proxy server to use for outbound connections.
658666

659-
Default value: `false`
667+
Default value: `undef`
660668

661669
##### `editable`
662670

@@ -896,11 +904,11 @@ Default value: 'root'
896904

897905
##### `proxy`
898906

899-
Data type: `Any`
907+
Data type: `Optional[Stdlib::HTTPUrl]`
900908

901909
Proxy server to use for outbound connections.
902910

903-
Default value: `false`
911+
Default value: `undef`
904912

905913
##### `src`
906914

@@ -1003,7 +1011,7 @@ Data type: `Any`
10031011

10041012

10051013

1006-
Default value: present
1014+
Default value: 'present'
10071015

10081016
##### `version`
10091017

@@ -1087,11 +1095,11 @@ Default value: '0755'
10871095

10881096
##### `proxy`
10891097

1090-
Data type: `Any`
1098+
Data type: `Optional[Stdlib::HTTPUrl]`
10911099

10921100
Proxy server to use for outbound connections
10931101

1094-
Default value: `false`
1102+
Default value: `undef`
10951103

10961104
##### `environment`
10971105

manifests/pip.pp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
$group = getvar('python::params::group'),
5858
$umask = undef,
5959
$index = false,
60-
Variant[Boolean, String] $proxy = false,
60+
Optional[Stdlib::HTTPUrl] $proxy = undef,
6161
$egg = false,
6262
Boolean $editable = false,
6363
$environment = [],
@@ -115,7 +115,7 @@
115115
}
116116

117117
$proxy_flag = $proxy ? {
118-
false => '',
118+
undef => '',
119119
default => "--proxy=${proxy}",
120120
}
121121

manifests/pip/bootstrap.pp

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#
44
# @param version should be pip or pip3
55
# @param manage_python if python module will manage deps
6+
# @param http_proxy Proxy server to use for outbound connections.
67
#
78
# @example
89
# class { 'python::pip::bootstrap':
@@ -11,6 +12,7 @@
1112
class python::pip::bootstrap (
1213
Enum['pip', 'pip3'] $version = 'pip',
1314
Variant[Boolean, String] $manage_python = false,
15+
Optional[Stdlib::HTTPUrl] $http_proxy = undef,
1416
) inherits ::python::params {
1517
if $manage_python {
1618
include python
@@ -19,34 +21,45 @@
1921
'AIX' => '/opt/freeware/bin',
2022
default => '/usr/bin'
2123
}
24+
25+
$environ = $http_proxy ? {
26+
undef => [],
27+
default => $facts['os']['family'] ? {
28+
'AIX' => [ "http_proxy=${http_proxy}", "https_proxy=${http_proxy}" ],
29+
default => [ "HTTP_PROXY=${http_proxy}", "HTTPS_PROXY=${http_proxy}" ],
30+
}
31+
}
32+
2233
if $version == 'pip3' {
2334
exec { 'bootstrap pip3':
24-
command => '/usr/bin/curl https://bootstrap.pypa.io/get-pip.py | python3',
25-
unless => 'which pip3',
26-
path => $python::params::pip_lookup_path,
27-
require => Package['python3'],
35+
command => '/usr/bin/curl https://bootstrap.pypa.io/get-pip.py | python3',
36+
environment => $environ,
37+
unless => 'which pip3',
38+
path => $python::params::pip_lookup_path,
39+
require => Package['python3'],
2840
}
2941
# puppet is opinionated about the pip command name
3042
file { 'pip3-python':
3143
ensure => link,
3244
path => '/usr/bin/pip3',
33-
target => "${target_src_pip_path}/pip${::facts['python3_release']}",
45+
target => "${target_src_pip_path}/pip${facts['python3_release']}",
3446
require => Exec['bootstrap pip3'],
3547
}
3648
} else {
37-
exec { 'bootstrap pip':
38-
command => '/usr/bin/curl https://bootstrap.pypa.io/get-pip.py | python',
39-
unless => 'which pip',
40-
path => $python::params::pip_lookup_path,
41-
require => Package['python'],
42-
}
43-
# puppet is opinionated about the pip command name
44-
file { 'pip-python':
45-
ensure => link,
46-
path => '/usr/bin/pip',
47-
target => "${target_src_pip_path}/pip${::facts['python2_release']}",
48-
require => Exec['bootstrap pip'],
49-
}
49+
exec { 'bootstrap pip':
50+
command => '/usr/bin/curl https://bootstrap.pypa.io/get-pip.py | python',
51+
environment => $environ,
52+
unless => 'which pip',
53+
path => $python::params::pip_lookup_path,
54+
require => Package['python'],
55+
}
56+
# puppet is opinionated about the pip command name
57+
file { 'pip-python':
58+
ensure => link,
59+
path => '/usr/bin/pip',
60+
target => "${target_src_pip_path}/pip${facts['python2_release']}",
61+
require => Exec['bootstrap pip'],
62+
}
5063
}
5164
}
5265
}

manifests/requirements.pp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
Enum['pip', 'pip3'] $pip_provider = 'pip',
3232
$owner = 'root',
3333
$group = 'root',
34-
$proxy = false,
34+
Optional[Stdlib::HTTPUrl] $proxy = undef,
3535
$src = false,
3636
$environment = [],
3737
$forceupdate = false,
@@ -68,8 +68,8 @@
6868
}
6969

7070
$proxy_flag = $proxy ? {
71-
false => '',
72-
default => "--proxy=${proxy}",
71+
undef => '',
72+
default => "--proxy=${proxy}",
7373
}
7474

7575
$src_flag = $src ? {

manifests/virtualenv.pp

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,25 @@
3131
# }
3232
#
3333
define python::virtualenv (
34-
$ensure = present,
35-
$version = 'system',
36-
$requirements = false,
37-
$systempkgs = false,
38-
$venv_dir = $name,
39-
$ensure_venv_dir = true,
40-
$distribute = true,
41-
$index = false,
42-
$owner = 'root',
43-
$group = 'root',
44-
$mode = '0755',
45-
$proxy = false,
46-
$environment = [],
47-
$path = [ '/bin', '/usr/bin', '/usr/sbin', '/usr/local/bin' ],
48-
$cwd = undef,
49-
$timeout = 1800,
50-
$pip_args = '',
51-
$extra_pip_args = '',
52-
$virtualenv = undef
34+
$ensure = 'present',
35+
$version = 'system',
36+
$requirements = false,
37+
$systempkgs = false,
38+
$venv_dir = $name,
39+
$ensure_venv_dir = true,
40+
$distribute = true,
41+
$index = false,
42+
$owner = 'root',
43+
$group = 'root',
44+
$mode = '0755',
45+
Optional[Stdlib::HTTPUrl] $proxy = undef,
46+
$environment = [],
47+
$path = [ '/bin', '/usr/bin', '/usr/sbin', '/usr/local/bin' ],
48+
$cwd = undef,
49+
$timeout = 1800,
50+
$pip_args = '',
51+
$extra_pip_args = '',
52+
$virtualenv = undef,
5353
) {
5454
include python
5555
$python_provider = getparam(Class['python'], 'provider')
@@ -78,9 +78,12 @@
7878
default => "--proxy=${proxy}",
7979
}
8080

81-
$proxy_command = $proxy ? {
82-
false => '',
83-
default => "&& export http_proxy=${proxy}",
81+
$proxy_hash = $proxy ? {
82+
undef => {},
83+
default => $facts['os']['family'] ? {
84+
'AIX' => { 'http_proxy' => $proxy, 'https_proxy' => $proxy },
85+
default => { 'HTTP_PROXY' => $proxy, 'HTTPS_PROXY' => $proxy },
86+
}
8487
}
8588

8689
# Virtualenv versions prior to 1.7 do not support the
@@ -134,12 +137,12 @@
134137
$pip_flags = "${pypi_index} ${proxy_flag} ${pip_args}"
135138

136139
exec { "python_virtualenv_${venv_dir}":
137-
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}",
140+
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}",
138141
user => $owner,
139142
creates => "${venv_dir}/bin/activate",
140143
path => $_path,
141144
cwd => '/tmp',
142-
environment => $environment,
145+
environment => (Hash($environment.map |$val| { $val.split(/=|$/) }) + $proxy_hash).map|$key, $val| { "${key}=${val}" },
143146
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
144147
require => File[$venv_dir],
145148
}

spec/classes/python_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,36 @@
4242
end
4343
end
4444

45+
describe 'with python::python_virtualenvs' do
46+
context 'when `proxy` set' do
47+
let(:params) do
48+
{
49+
python_virtualenvs: {
50+
'/opt/env1' => {
51+
proxy: 'http://example.com:3128'
52+
}
53+
}
54+
}
55+
end
56+
57+
it { is_expected.to contain_exec('python_virtualenv_/opt/env1').with_environment(['HTTP_PROXY=http://example.com:3128', 'HTTPS_PROXY=http://example.com:3128']) }
58+
end
59+
context 'when `proxy` and `environment` have conflicting parameters' do
60+
let(:params) do
61+
{
62+
python_virtualenvs: {
63+
'/opt/env1' => {
64+
proxy: 'http://example.com:3128',
65+
environment: ['HTTP_PROXY=http://example.com:8080']
66+
}
67+
}
68+
}
69+
end
70+
71+
it { is_expected.to contain_exec('python_virtualenv_/opt/env1').with_environment(['HTTP_PROXY=http://example.com:3128', 'HTTPS_PROXY=http://example.com:3128']) }
72+
end
73+
end
74+
4575
describe 'with manage_gunicorn' do
4676
context 'true' do
4777
let(:params) { { manage_gunicorn: true } }

0 commit comments

Comments
 (0)