Skip to content

Commit 1ee9a64

Browse files
authored
Merge pull request #716 from voxpupuli/fix-pyvenv-bootstraping
Fix bootstrapping `python::pyvenv` when Python is not installed
2 parents 16043ea + 9ad7d67 commit 1ee9a64

File tree

16 files changed

+112
-58
lines changed

16 files changed

+112
-58
lines changed

REFERENCE.md

+7
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class { 'python' :
6767

6868
The following parameters are available in the `python` class:
6969

70+
* [`default_system_version`](#-python--default_system_version)
7071
* [`ensure`](#-python--ensure)
7172
* [`version`](#-python--version)
7273
* [`pip`](#-python--pip)
@@ -92,6 +93,12 @@ The following parameters are available in the `python` class:
9293
* [`anaconda_installer_url`](#-python--anaconda_installer_url)
9394
* [`anaconda_install_path`](#-python--anaconda_install_path)
9495

96+
##### <a name="-python--default_system_version"></a>`default_system_version`
97+
98+
Data type: `Python::Version`
99+
100+
The default version of Python provided by the operating system. Only used as a fallback if Python is not installed yet to determine how to handle some actions that vary depending on the Python version used.
101+
95102
##### <a name="-python--ensure"></a>`ensure`
96103

97104
Data type: `Python::Package::Ensure`

data/common.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python::default_system_version: "3.11"

data/os/Archlinux.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python::default_system_version: "3.13"

data/os/Debian/11.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python::default_system_version: "3.9"

data/os/Debian/12.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python::default_system_version: "3.11"

data/os/FreeBSD.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python::default_system_version: "3.11"

data/os/Gentoo.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python::default_system_version: "3.12"

data/os/RedHat/8.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python::default_system_version: "3.6"

data/os/RedHat/9.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python::default_system_version: "3.9"

data/os/Ubuntu/20.04.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python::default_system_version: "3.8"

data/os/Ubuntu/22.04.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python::default_system_version: "3.10"

data/os/Ubuntu/24.04.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python::default_system_version: "3.12"

hiera.yaml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
version: 5
3+
4+
defaults: # Used for any hierarchy level that omits these keys.
5+
datadir: data # This path is relative to hiera.yaml's directory.
6+
data_hash: yaml_data # Use the built-in YAML backend.
7+
8+
hierarchy:
9+
- name: "archicture"
10+
paths:
11+
- "architecture/%{facts.os.architecture}.yaml"
12+
- "architecture/common.yaml"
13+
- name: "osfamily/major release"
14+
paths:
15+
- "os/%{facts.os.name}/%{facts.os.release.major}.yaml" # Used to distinguish between Debian and Ubuntu
16+
- "os/%{facts.os.family}/%{facts.os.release.major}.yaml" #
17+
- "os/%{facts.os.family}/%{facts.kernelrelease}.yaml" # Used for Solaris
18+
- name: "osfamily"
19+
paths:
20+
- "os/%{facts.os.name}.yaml"
21+
- "os/%{facts.os.family}.yaml"
22+
- name: 'common'
23+
path: 'common.yaml'

manifests/init.pp

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# @summary Installs and manages python, python-dev and gunicorn.
22
#
3+
# @param default_system_version The default version of Python provided by the operating system. Only used as a fallback if Python is not installed yet to determine how to handle some actions that vary depending on the Python version used.
34
# @param ensure Desired installation state for the Python package.
45
# @param version Python version to install. Beware that valid values for this differ a) by the provider you choose and b) by the osfamily/operatingsystem you are using.
56
# Allowed values:
@@ -38,6 +39,7 @@
3839
# }
3940
#
4041
class python (
42+
Python::Version $default_system_version,
4143
Python::Package::Ensure $ensure = 'present',
4244
Python::Version $version = $facts['os']['family'] ? { 'Archlinux' => 'system', default => '3' },
4345
Python::Package::Ensure $pip = 'present',

manifests/pyvenv.pp

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141

4242
if $ensure == 'present' {
4343
$python_version = $version ? {
44-
'system' => $facts['python3_version'],
44+
'system' => $facts['python3_version'].lest || { $python::default_system_version },
4545
default => $version,
4646
}
4747

spec/defines/pyvenv_spec.rb

+68-57
Original file line numberDiff line numberDiff line change
@@ -6,83 +6,94 @@
66
on_supported_os.each do |os, facts|
77
next if os == 'gentoo-3-x86_64'
88

9+
let :title do
10+
'/opt/env'
11+
end
12+
913
context "on #{os}" do
10-
let :facts do
11-
# python3 is required to use pyvenv
12-
facts.merge(
13-
python3_version: '3.5.1'
14-
)
15-
end
16-
let :title do
17-
'/opt/env'
14+
context 'with default parameters' do
15+
let :facts do
16+
facts
17+
end
18+
19+
it { is_expected.to compile }
1820
end
1921

20-
context 'with default parameters' do
21-
it { is_expected.to contain_file('/opt/env').that_requires('Class[python::install]') }
22-
it { is_expected.to contain_exec('python_virtualenv_/opt/env').with_command('pyvenv-3.5 --clear /opt/env && /opt/env/bin/pip --log /opt/env/pip.log install --upgrade pip && /opt/env/bin/pip --log /opt/env/pip.log install --upgrade setuptools') }
22+
context 'with a specific python3 version' do
23+
let :facts do
24+
# python3 is required to use pyvenv
25+
facts.merge(
26+
python3_version: '3.5.1'
27+
)
28+
end
29+
30+
context 'with default parameters' do
31+
it { is_expected.to contain_file('/opt/env').that_requires('Class[python::install]') }
32+
it { is_expected.to contain_exec('python_virtualenv_/opt/env').with_command('pyvenv-3.5 --clear /opt/env && /opt/env/bin/pip --log /opt/env/pip.log install --upgrade pip && /opt/env/bin/pip --log /opt/env/pip.log install --upgrade setuptools') }
33+
end
34+
35+
describe 'when ensure' do
36+
context 'is absent' do
37+
let :params do
38+
{
39+
ensure: 'absent'
40+
}
41+
end
42+
43+
it {
44+
expect(subject).to contain_file('/opt/env').with_ensure('absent').with_purge(true)
45+
}
46+
end
47+
end
2348
end
2449

25-
describe 'when ensure' do
26-
context 'is absent' do
50+
context "prompt on #{os} with python 3.6" do
51+
let :facts do
52+
# python 3.6 is required for venv and prompt
53+
facts.merge(
54+
python3_version: '3.6.1'
55+
)
56+
end
57+
let :title do
58+
'/opt/env'
59+
end
60+
61+
context 'with prompt' do
2762
let :params do
2863
{
29-
ensure: 'absent'
64+
prompt: 'custom prompt',
3065
}
3166
end
3267

3368
it {
34-
expect(subject).to contain_file('/opt/env').with_ensure('absent').with_purge(true)
69+
is_expected.to contain_file('/opt/env').that_requires('Class[python::install]')
70+
is_expected.to contain_exec('python_virtualenv_/opt/env').with_command('python3.6 -m venv --clear --prompt custom\\ prompt /opt/env && /opt/env/bin/pip --log /opt/env/pip.log install --upgrade pip && /opt/env/bin/pip --log /opt/env/pip.log install --upgrade setuptools')
3571
}
3672
end
3773
end
38-
end
3974

40-
context "prompt on #{os} with python 3.6" do
41-
let :facts do
42-
# python 3.6 is required for venv and prompt
43-
facts.merge(
44-
python3_version: '3.6.1'
45-
)
46-
end
47-
let :title do
48-
'/opt/env'
49-
end
50-
51-
context 'with prompt' do
52-
let :params do
53-
{
54-
prompt: 'custom prompt',
55-
}
75+
context "prompt on #{os} with python 3.5" do
76+
let :facts do
77+
facts.merge(
78+
python3_version: '3.5.1'
79+
)
80+
end
81+
let :title do
82+
'/opt/env'
5683
end
5784

58-
it {
59-
is_expected.to contain_file('/opt/env').that_requires('Class[python::install]')
60-
is_expected.to contain_exec('python_virtualenv_/opt/env').with_command('python3.6 -m venv --clear --prompt custom\\ prompt /opt/env && /opt/env/bin/pip --log /opt/env/pip.log install --upgrade pip && /opt/env/bin/pip --log /opt/env/pip.log install --upgrade setuptools')
61-
}
62-
end
63-
end
64-
65-
context "prompt on #{os} with python 3.5" do
66-
let :facts do
67-
facts.merge(
68-
python3_version: '3.5.1'
69-
)
70-
end
71-
let :title do
72-
'/opt/env'
73-
end
85+
context 'with prompt' do
86+
let :params do
87+
{
88+
prompt: 'custom prompt',
89+
}
90+
end
7491

75-
context 'with prompt' do
76-
let :params do
77-
{
78-
prompt: 'custom prompt',
92+
it {
93+
is_expected.to contain_file('/opt/env').that_requires('Class[python::install]')
94+
is_expected.to contain_exec('python_virtualenv_/opt/env').with_command('pyvenv-3.5 --clear /opt/env && /opt/env/bin/pip --log /opt/env/pip.log install --upgrade pip && /opt/env/bin/pip --log /opt/env/pip.log install --upgrade setuptools')
7995
}
8096
end
81-
82-
it {
83-
is_expected.to contain_file('/opt/env').that_requires('Class[python::install]')
84-
is_expected.to contain_exec('python_virtualenv_/opt/env').with_command('pyvenv-3.5 --clear /opt/env && /opt/env/bin/pip --log /opt/env/pip.log install --upgrade pip && /opt/env/bin/pip --log /opt/env/pip.log install --upgrade setuptools')
85-
}
8697
end
8798
end
8899
end

0 commit comments

Comments
 (0)