Skip to content

Commit

Permalink
Merge pull request #451 from emetriq/allow_custom_versions
Browse files Browse the repository at this point in the history
Allow custom python versions and environments
  • Loading branch information
bastelfreak authored Feb 19, 2019
2 parents 995ca4e + 5ea5621 commit af0cfe1
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 17 deletions.
6 changes: 3 additions & 3 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@
default => '',
}

$allowed_versions = concat(['system', 'pypy'], $valid_versions)
unless $version =~ Enum[$allowed_versions] {
fail("version needs to be within${allowed_versions}")
unless $version =~ Pattern[/\A(python)?[0-9](\.[0-9])+/,
/\Apypy\Z/, /\Asystem\Z/] {
fail("version needs to be pypy, system or a version string like '3.5' or 'python3.5)")
}

# Module compatibility check
Expand Down
3 changes: 2 additions & 1 deletion manifests/install.pp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
$python = $python_version ? {
'system' => 'python',
'pypy' => 'pypy',
default => "${python_version}", # lint:ignore:only_variable_string
/\A(python)?([0-9](\.?[0-9])+)/ => "python${1}",
default => "python${python::version}",
}

$pythondev = $facts['os']['family'] ? {
Expand Down
8 changes: 1 addition & 7 deletions manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@
$gunicorn = 'absent'
$manage_gunicorn = true
$provider = undef
$valid_versions = $::osfamily ? {
'RedHat' => ['3','27','33'],
'Debian' => ['3', '3.3', '2.7'],
'Suse' => [],
'AIX' => ['python3'],
'Gentoo' => ['2.7', '3.3', '3.4', '3.5']
}
$valid_versions = undef

if $::osfamily == 'RedHat' {
if $::operatingsystem != 'Fedora' {
Expand Down
5 changes: 5 additions & 0 deletions manifests/pip.pp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@
$python_provider = getparam(Class['python'], 'provider')
$python_version = getparam(Class['python'], 'version')

if $virtualenv != 'system' {
Python::Pyvenv <| |> -> Python::Pip[$name]
Python::Virtualenv <| |> -> Python::Pip[$name]
}

# Get SCL exec prefix
# NB: this will not work if you are running puppet from scl enabled shell
$exec_prefix = $python_provider ? {
Expand Down
26 changes: 23 additions & 3 deletions manifests/pyvenv.pp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,30 @@
include ::python

if $ensure == 'present' {
$python_version = $version ? {
'system' => $facts['python3_version'],
default => $version,
}

# Debian splits the venv module into a seperate package
if ( $facts['os']['family'] == 'Debian'){
$python3_venv_package="python${python_version}-venv"
case $facts['lsbdistcodename'] {
'xenial','bionic','cosmic','disco',
'jessie','stretch','buster': {
ensure_packages ($python3_venv_package, {
before => File[$venv_dir],
})
}
default: {}
}
}

$virtualenv_cmd = $version ? {
'system' => "${python::exec_prefix}pyvenv",
default => "${python::exec_prefix}pyvenv-${version}",
# pyvenv is deprecated since 3.6 and will be removed in 3.8
if (versioncmp($facts['python3_version'], '3.6') >=0) {
$virtualenv_cmd = "${python::exec_prefix}python${python_version} -m venv"
} else {
$virtualenv_cmd = "${python::exec_prefix}pyvenv-${python_version}"
}

$_path = $::python::provider ? {
Expand Down
3 changes: 3 additions & 0 deletions spec/default_module_facts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
python_version: "2.7"
python3_version: ~
9 changes: 6 additions & 3 deletions spec/defines/pyvenv_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
on_supported_os.each do |os, facts|
context("on #{os} ") do
let :facts do
facts
# python3 is required to use pyvenv
facts.merge(
python3_version: '3.4'
)
end
let :title do
'/opt/env'
end

it {
is_expected.to contain_file('/opt/env')
is_expected.to contain_exec('python_virtualenv_/opt/env').with_command('pyvenv --clear /opt/env')
is_expected.to contain_exec('python_virtualenv_/opt/env').with_command('pyvenv-3.4 --clear /opt/env')
}

describe 'when ensure' do
Expand All @@ -28,6 +31,6 @@
}
end
end
end
end # context
end
end

0 comments on commit af0cfe1

Please sign in to comment.