From 7464d013c3e1448b1083b11c5e48959833388ea8 Mon Sep 17 00:00:00 2001 From: Joshua Spence Date: Mon, 21 Sep 2015 09:51:56 +1000 Subject: [PATCH] Allow setting a custom index for `python::pip` --- manifests/pip.pp | 64 ++++++++++++++++++++++------------------ manifests/virtualenv.pp | 2 +- spec/defines/pip_spec.rb | 21 +++++++++++-- 3 files changed, 55 insertions(+), 32 deletions(-) diff --git a/manifests/pip.pp b/manifests/pip.pp index 2847c8f2..bfc55309 100644 --- a/manifests/pip.pp +++ b/manifests/pip.pp @@ -25,6 +25,9 @@ # [*group*] # The group of the virtualenv being manipulated. Default: root # +# [*index*] +# Base URL of Python package index. Default: none (http://pypi.python.org/simple/) +# # [*proxy*] # Proxy server to use for outbound connections. Default: none # @@ -53,6 +56,7 @@ # python::pip { 'flask': # virtualenv => '/var/www/project1', # proxy => 'http://proxy.domain.com:3128', +# index => 'http://www.example.com/simple/', # } # # === Authors @@ -67,6 +71,7 @@ $url = false, $owner = 'root', $group = 'root', + $index = false, $proxy = false, $egg = false, $editable = false, @@ -103,6 +108,11 @@ default => "${virtualenv}/bin/pip", } + $pypi_index = $index ? { + false => '', + default => "--index-url=${index}", + } + $proxy_flag = $proxy ? { false => '', default => "--proxy=${proxy}", @@ -162,38 +172,36 @@ # Explicit version out of VCS when PIP supported URL is provided if $source =~ /^(git\+|hg\+|bzr\+|svn\+)(http|https|ssh|svn|sftp|ftp|lp)(:\/\/).+$/ { - if $ensure != present and $ensure != latest { - exec { "pip_install_${name}": - command => "${pip_env} wheel --help > /dev/null 2>&1 && { ${pip_env} wheel --version > /dev/null 2>&1 || wheel_support_flag='--no-use-wheel'; } ; { ${pip_env} --log ${log}/pip.log install ${install_args} \$wheel_support_flag ${proxy_flag} ${install_args} ${install_editable} ${source}@${ensure}#egg=${egg_name} || ${pip_env} --log ${log}/pip.log install ${install_args} ${proxy_flag} ${install_args} ${install_editable} ${source}@${ensure}#egg=${egg_name} ;}", - unless => "${pip_env} freeze | grep -i -e ${grep_regex}", - user => $owner, - group => $group, - cwd => $cwd, - environment => $environment, - path => ['/usr/local/bin','/usr/bin','/bin', '/usr/sbin'], - timeout => $timeout, - } - } - else { - exec { "pip_install_${name}": - command => "${pip_env} wheel --help > /dev/null 2>&1 && { ${pip_env} wheel --version > /dev/null 2>&1 || wheel_support_flag='--no-use-wheel'; } ; { ${pip_env} --log ${log}/pip.log install ${install_args} \$wheel_support_flag ${proxy_flag} ${install_args} ${install_editable} ${source} || ${pip_env} --log ${log}/pip.log install ${install_args} ${proxy_flag} ${install_args} ${install_editable} ${source} ;}", - unless => "${pip_env} freeze | grep -i -e ${grep_regex}", - user => $owner, - group => $group, - cwd => $cwd, - environment => $environment, - path => ['/usr/local/bin','/usr/bin','/bin', '/usr/sbin'], - timeout => $timeout, - } + if $ensure != present and $ensure != latest { + exec { "pip_install_${name}": + command => "${pip_env} wheel --help > /dev/null 2>&1 && { ${pip_env} wheel --version > /dev/null 2>&1 || wheel_support_flag='--no-use-wheel'; } ; { ${pip_env} --log ${log}/pip.log install ${install_args} \$wheel_support_flag ${pypi_index} ${proxy_flag} ${install_args} ${install_editable} ${source}@${ensure}#egg=${egg_name} || ${pip_env} --log ${log}/pip.log install ${install_args} ${pypi_index} ${proxy_flag} ${install_args} ${install_editable} ${source}@${ensure}#egg=${egg_name} ;}", + unless => "${pip_env} freeze | grep -i -e ${grep_regex}", + user => $owner, + group => $group, + cwd => $cwd, + environment => $environment, + path => ['/usr/local/bin','/usr/bin','/bin', '/usr/sbin'], + timeout => $timeout, + } + } else { + exec { "pip_install_${name}": + command => "${pip_env} wheel --help > /dev/null 2>&1 && { ${pip_env} wheel --version > /dev/null 2>&1 || wheel_support_flag='--no-use-wheel'; } ; { ${pip_env} --log ${log}/pip.log install ${install_args} \$wheel_support_flag ${pypi_index} ${proxy_flag} ${install_args} ${install_editable} ${source} || ${pip_env} --log ${log}/pip.log install ${install_args} ${pypi_index} ${proxy_flag} ${install_args} ${install_editable} ${source} ;}", + unless => "${pip_env} freeze | grep -i -e ${grep_regex}", + user => $owner, + group => $group, + cwd => $cwd, + environment => $environment, + path => ['/usr/local/bin','/usr/bin','/bin', '/usr/sbin'], + timeout => $timeout, + } } - } - else { + } else { case $ensure { /^((19|20)[0-9][0-9]-(0[1-9]|1[1-2])-([0-2][1-9]|3[0-1])|[0-9]+\.[0-9]+(\.[0-9]+)?)$/: { # Version formats as per http://guide.python-distribute.org/specification.html#standard-versioning-schemes # Explicit version. exec { "pip_install_${name}": - command => "${pip_env} wheel --help > /dev/null 2>&1 && { ${pip_env} wheel --version > /dev/null 2>&1 || wheel_support_flag='--no-use-wheel'; } ; { ${pip_env} --log ${log}/pip.log install ${install_args} \$wheel_support_flag ${proxy_flag} ${install_args} ${install_editable} ${source}==${ensure} || ${pip_env} --log ${log}/pip.log install ${install_args} ${proxy_flag} ${install_args} ${install_editable} ${source}==${ensure} ;}", + command => "${pip_env} wheel --help > /dev/null 2>&1 && { ${pip_env} wheel --version > /dev/null 2>&1 || wheel_support_flag='--no-use-wheel'; } ; { ${pip_env} --log ${log}/pip.log install ${install_args} \$wheel_support_flag ${pypi_index} ${proxy_flag} ${install_args} ${install_editable} ${source}==${ensure} || ${pip_env} --log ${log}/pip.log install ${install_args} ${pypi_index} ${proxy_flag} ${install_args} ${install_editable} ${source}==${ensure} ;}", unless => "${pip_env} freeze | grep -i -e ${grep_regex}", user => $owner, group => $group, @@ -207,7 +215,7 @@ present: { # Whatever version is available. exec { "pip_install_${name}": - command => "${pip_env} wheel --help > /dev/null 2>&1 && { ${pip_env} wheel --version > /dev/null 2>&1 || wheel_support_flag='--no-use-wheel'; } ; { ${pip_env} --log ${log}/pip.log install \$wheel_support_flag ${proxy_flag} ${install_args} ${install_editable} ${source} || ${pip_env} --log ${log}/pip.log install ${proxy_flag} ${install_args} ${install_editable} ${source} ;}", + command => "${pip_env} wheel --help > /dev/null 2>&1 && { ${pip_env} wheel --version > /dev/null 2>&1 || wheel_support_flag='--no-use-wheel'; } ; { ${pip_env} --log ${log}/pip.log install \$wheel_support_flag ${pypi_index} ${proxy_flag} ${install_args} ${install_editable} ${source} || ${pip_env} --log ${log}/pip.log install ${pypi_index} ${proxy_flag} ${install_args} ${install_editable} ${source} ;}", unless => "${pip_env} freeze | grep -i -e ${grep_regex}", user => $owner, group => $group, @@ -221,7 +229,7 @@ latest: { # Latest version. exec { "pip_install_${name}": - command => "${pip_env} wheel --help > /dev/null 2>&1 && { ${pip_env} wheel --version > /dev/null 2>&1 || wheel_support_flag='--no-use-wheel'; } ; { ${pip_env} --log ${log}/pip.log install --upgrade \$wheel_support_flag ${proxy_flag} ${install_args} ${install_editable} ${source} || ${pip_env} --log ${log}/pip.log install --upgrade ${proxy_flag} ${install_args} ${install_editable} ${source} ;}", + command => "${pip_env} wheel --help > /dev/null 2>&1 && { ${pip_env} wheel --version > /dev/null 2>&1 || wheel_support_flag='--no-use-wheel'; } ; { ${pip_env} --log ${log}/pip.log install --upgrade \$wheel_support_flag ${pypi_index} ${proxy_flag} ${install_args} ${install_editable} ${source} || ${pip_env} --log ${log}/pip.log install --upgrade ${pypi_index} ${proxy_flag} ${install_args} ${install_editable} ${source} ;}", unless => "${pip_env} search ${proxy_flag} ${source} | grep -i INSTALLED | grep -i latest", user => $owner, group => $group, diff --git a/manifests/virtualenv.pp b/manifests/virtualenv.pp index 78ba19b2..60d10af1 100644 --- a/manifests/virtualenv.pp +++ b/manifests/virtualenv.pp @@ -60,7 +60,7 @@ # requirements => '/var/www/project1/requirements.txt', # proxy => 'http://proxy.domain.com:3128', # systempkgs => true, -# index => 'http://www.example.com/simple/' +# index => 'http://www.example.com/simple/', # } # # === Authors diff --git a/spec/defines/pip_spec.rb b/spec/defines/pip_spec.rb index b4170104..d603edc8 100644 --- a/spec/defines/pip_spec.rb +++ b/spec/defines/pip_spec.rb @@ -43,14 +43,29 @@ end context "adds proxy to install command if proxy set" do let (:params) {{ :proxy => "http://my.proxy:3128" }} - it { is_expected.to contain_exec("pip_install_rpyc").with_command("pip wheel --help > /dev/null 2>&1 && { pip wheel --version > /dev/null 2>&1 || wheel_support_flag='--no-use-wheel'; } ; { pip --log /tmp/pip.log install $wheel_support_flag --proxy=http://my.proxy:3128 rpyc || pip --log /tmp/pip.log install --proxy=http://my.proxy:3128 rpyc ;}") } + it { is_expected.to contain_exec("pip_install_rpyc").with_command("pip wheel --help > /dev/null 2>&1 && { pip wheel --version > /dev/null 2>&1 || wheel_support_flag='--no-use-wheel'; } ; { pip --log /tmp/pip.log install $wheel_support_flag --proxy=http://my.proxy:3128 rpyc || pip --log /tmp/pip.log install --proxy=http://my.proxy:3128 rpyc ;}") } end context "adds proxy to search command if set to latest" do let (:params) {{ :proxy => "http://my.proxy:3128", :ensure => 'latest' }} - it { is_expected.to contain_exec("pip_install_rpyc").with_command("pip wheel --help > /dev/null 2>&1 && { pip wheel --version > /dev/null 2>&1 || wheel_support_flag='--no-use-wheel'; } ; { pip --log /tmp/pip.log install --upgrade $wheel_support_flag --proxy=http://my.proxy:3128 rpyc || pip --log /tmp/pip.log install --upgrade --proxy=http://my.proxy:3128 rpyc ;}") } + it { is_expected.to contain_exec("pip_install_rpyc").with_command("pip wheel --help > /dev/null 2>&1 && { pip wheel --version > /dev/null 2>&1 || wheel_support_flag='--no-use-wheel'; } ; { pip --log /tmp/pip.log install --upgrade $wheel_support_flag --proxy=http://my.proxy:3128 rpyc || pip --log /tmp/pip.log install --upgrade --proxy=http://my.proxy:3128 rpyc ;}") } it { is_expected.to contain_exec("pip_install_rpyc").with_unless('pip search --proxy=http://my.proxy:3128 rpyc | grep -i INSTALLED | grep -i latest') } end end + describe 'index as' do + context 'defaults to empty' do + let (:params) {{ }} + it { is_expected.to contain_exec('pip_install_rpyc').without_command(/--index-url/) } + end + context 'adds index to install command if index set' do + let (:params) {{ :index => 'http://www.example.com/simple/' }} + it { is_expected.to contain_exec('pip_install_rpyc').with_command("pip wheel --help > /dev/null 2>&1 && { pip wheel --version > /dev/null 2>&1 || wheel_support_flag='--no-use-wheel'; } ; { pip --log /tmp/pip.log install $wheel_support_flag --index-url=http://www.example.com/simple/ rpyc || pip --log /tmp/pip.log install --index-url=http://www.example.com/simple/ rpyc ;}") } + end + context 'adds index to search command if set to latest' do + let (:params) {{ :index => 'http://www.example.com/simple/', :ensure => 'latest' }} + it { is_expected.to contain_exec('pip_install_rpyc').with_command("pip wheel --help > /dev/null 2>&1 && { pip wheel --version > /dev/null 2>&1 || wheel_support_flag='--no-use-wheel'; } ; { pip --log /tmp/pip.log install --upgrade $wheel_support_flag --index-url=http://www.example.com/simple/ rpyc || pip --log /tmp/pip.log install --upgrade --index-url=http://www.example.com/simple/ rpyc ;}") } + end + end + end -end \ No newline at end of file +end