Skip to content

Commit

Permalink
Add python-venv installation
Browse files Browse the repository at this point in the history
  • Loading branch information
crazymind1337 committed Dec 15, 2020
1 parent d9c51a8 commit ed824cf
Show file tree
Hide file tree
Showing 14 changed files with 100 additions and 60 deletions.
48 changes: 35 additions & 13 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@

### Data types

* [`Python::Loglevel`](#pythonloglevel): A version type to match all valid loglevels for python
* [`Python::Package::Ensure`](#pythonpackageensure): A version type to match all valid package ensures for python
* [`Python::Provider`](#pythonprovider): A version type to match all valid provider for python
* [`Python::Umask`](#pythonumask): A version type to match valid umask for python
* [`Python::Version`](#pythonversion): A version type to match all valid versions for python
* [`Python::Loglevel`](#pythonloglevel): Match all valid loglevels for python
* [`Python::Package::Ensure`](#pythonpackageensure): Match all valid package ensures for python
* [`Python::Provider`](#pythonprovider): Match all valid provider for python
* [`Python::Umask`](#pythonumask): Match valid umask for python
* [`Python::Version`](#pythonversion): Match all valid versions for python

## Classes

Expand Down Expand Up @@ -116,6 +116,14 @@ Desired installation state for the virtualenv package

Default value: `$python::params::virtualenv`

##### `venv`

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

Desired installation state for the virtualenv package

Default value: `$python::params::venv`

##### `gunicorn`

Data type: `Python::Package::Ensure`
Expand Down Expand Up @@ -164,27 +172,41 @@ The default umask for invoked exec calls.

Default value: ``undef``

##### `manage_gunicorn`

manage the state for package gunicorn

Default value: `$python::params::manage_gunicorn`

##### `manage_python_package`

Data type: `Boolean`


manage the state for package python

Default value: `$python::params::manage_python_package`

##### `manage_virtualenv_package`

Data type: `Boolean`


manage the state for package virtualenv

Default value: `$python::params::manage_virtualenv_package`

##### `manage_pip_package`
##### `manage_venv_package`

Data type: `Boolean`

manage the state for package venv

Default value: `$python::params::manage_venv_package`

##### `manage_pip_package`

Data type: `Boolean`

manage the state for package pip

Default value: `$python::params::manage_pip_package`

Expand Down Expand Up @@ -1202,31 +1224,31 @@ Default value: ``undef``

### `Python::Loglevel`

A version type to match all valid loglevels for python
Match all valid loglevels for python

Alias of `Enum['debug', 'info', 'warning', 'error', 'critical']`

### `Python::Package::Ensure`

A version type to match all valid package ensures for python
Match all valid package ensures for python

Alias of `Enum['absent', 'present', 'latest']`

### `Python::Provider`

A version type to match all valid provider for python
Match all valid provider for python

Alias of `Enum['pip', 'scl', 'rhscl', 'anaconda', '']`

### `Python::Umask`

A version type to match valid umask for python
Match valid umask for python

Alias of `Pattern[/[0-7]{1,4}/]`

### `Python::Version`

A version type to match all valid versions for python
Match all valid versions for python

Alias of `Pattern[/\A(python)?[0-9](\.?[0-9])*/, /\Apypy\Z/, /\Asystem\Z/, /\Arh-python[0-9]{2}(?:-python)?\Z/]`

8 changes: 8 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@
# @param pip Desired installation state for the python-pip package.
# @param dev Desired installation state for the python-dev package.
# @param virtualenv Desired installation state for the virtualenv package
# @param venv Desired installation state for the virtualenv package
# @param gunicorn Desired installation state for Gunicorn.
# @param manage_gunicorn Allow Installation / Removal of Gunicorn.
# @param provider What provider to use for installation of the packages, except gunicorn and Python itself.
# @param use_epel to determine if the epel class is used.
# @param manage_scl Whether to manage core SCL packages or not.
# @param umask The default umask for invoked exec calls.
# @param manage_gunicorn manage the state for package gunicorn
# @param manage_python_package manage the state for package python
# @param manage_virtualenv_package manage the state for package virtualenv
# @param manage_venv_package manage the state for package venv
# @param manage_pip_package manage the state for package pip
#
# @example install python from system python
# class { 'python':
Expand All @@ -41,10 +47,12 @@
Python::Package::Ensure $pip = $python::params::pip,
Python::Package::Ensure $dev = $python::params::dev,
Python::Package::Ensure $virtualenv = $python::params::virtualenv,
Python::Package::Ensure $venv = $python::params::venv,
Python::Package::Ensure $gunicorn = $python::params::gunicorn,
Boolean $manage_gunicorn = $python::params::manage_gunicorn,
Boolean $manage_python_package = $python::params::manage_python_package,
Boolean $manage_virtualenv_package = $python::params::manage_virtualenv_package,
Boolean $manage_venv_package = $python::params::manage_venv_package,
Boolean $manage_pip_package = $python::params::manage_pip_package,
String[1] $gunicorn_package_name = $python::params::gunicorn_package_name,
Optional[Python::Provider] $provider = $python::params::provider,
Expand Down
44 changes: 20 additions & 24 deletions manifests/install.pp
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,7 @@
'Gentoo' => undef,
}

$pip_ensure = $python::pip ? {
true => 'present',
false => 'absent',
default => $python::pip,
}

$venv_ensure = $python::virtualenv ? {
true => 'present',
false => 'absent',
default => $python::virtualenv,
}

if $venv_ensure == 'present' {
if $python::virtualenv == 'present' {
$dev_ensure = 'present'

unless $python::dev {
Expand All @@ -59,17 +47,25 @@

if $python::manage_virtualenv_package {
package { 'virtualenv':
ensure => $venv_ensure,
ensure => $python::virtualenv,
name => "${python}-virtualenv",
require => Package['python'],
}
}

if $python::manage_venv_package {
package { 'python-venv':
ensure => $python::venv,
name => "${python}-venv",
require => Package['python'],
}
}

case $python::provider {
'pip': {
if $python::manage_pip_package {
package { 'pip':
ensure => $pip_ensure,
ensure => $python::pip,
require => Package['python'],
}
}
Expand All @@ -81,8 +77,8 @@
}
}

# Respect the $pip_ensure setting
unless $pip_ensure == 'absent' {
# Respect the $python::pip setting
unless $python::pip == 'absent' {
# Install pip without pip, see https://pip.pypa.io/en/stable/installing/.
include python::pip::bootstrap

Expand Down Expand Up @@ -129,7 +125,7 @@

Package['scl-utils'] -> Package["${python}-scldevel"]

if $pip_ensure != 'absent' {
if $python::pip != 'absent' {
Package['scl-utils'] -> Exec['python-scl-pip-install']
}
}
Expand All @@ -138,7 +134,7 @@
ensure => $dev_ensure,
}

if $pip_ensure != 'absent' {
if $python::pip != 'absent' {
exec { 'python-scl-pip-install':
command => "${python::exec_prefix}easy_install pip",
path => ['/usr/bin', '/bin'],
Expand Down Expand Up @@ -172,7 +168,7 @@
}

package { "${python}-python-pip":
ensure => $pip_ensure,
ensure => $python::pip,
tag => 'python-pip-package',
}

Expand Down Expand Up @@ -210,7 +206,7 @@
} else {
if $python::manage_pip_package {
package { 'python-pip':
ensure => $pip_ensure,
ensure => $python::pip,
require => Package['python'],
provider => 'yum',
}
Expand All @@ -229,7 +225,7 @@
default: {
if $python::manage_pip_package {
package { 'pip':
ensure => $pip_ensure,
ensure => $python::pip,
require => Package['python'],
}
}
Expand All @@ -246,11 +242,11 @@

case $facts['os']['family'] {
'RedHat': {
if $pip_ensure != 'absent' and $python::use_epel and ($python::manage_pip_package or $python::manage_python_package) {
if $python::pip != 'absent' and $python::use_epel and ($python::manage_pip_package or $python::manage_python_package) {
require epel
}

if $venv_ensure != 'absent' and $facts['os']['release']['full'] =~ /^6/ and $python::use_epel {
if $python::virtualenv != 'absent' and $facts['os']['release']['full'] =~ /^6/ and $python::use_epel {
require epel
}

Expand Down
2 changes: 2 additions & 0 deletions manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
$pip = 'present'
$dev = 'absent'
$virtualenv = 'absent'
$venv = 'absent'
$gunicorn = 'absent'
$manage_gunicorn = true
$manage_python_package = true
$manage_virtualenv_package = true
$manage_venv_package = true
$manage_pip_package = true
$provider = undef
$valid_versions = undef
Expand Down
17 changes: 5 additions & 12 deletions manifests/pyvenv.pp
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@
$python_version_parts = split($python_version, '[.]')
$normalized_python_version = sprintf('%s.%s', $python_version_parts[0], $python_version_parts[1])

# Debian splits the venv module into a seperate package
if ( $facts['os']['family'] == 'Debian') {
$python3_venv_package = "python${normalized_python_version}-venv"
ensure_packages($python3_venv_package)

Package[$python3_venv_package] -> File[$venv_dir]
}

# pyvenv is deprecated since 3.6 and will be removed in 3.8
if versioncmp($normalized_python_version, '3.6') >=0 {
$virtualenv_cmd = "${python::exec_prefix}python${normalized_python_version} -m venv"
Expand All @@ -70,10 +62,11 @@
}

file { $venv_dir:
ensure => directory,
owner => $owner,
group => $group,
mode => $mode,
ensure => directory,
owner => $owner,
group => $group,
mode => $mode,
require => Package['python-venv'],
}

$pip_cmd = "${python::exec_prefix}${venv_dir}/bin/pip"
Expand Down
1 change: 1 addition & 0 deletions spec/acceptance/class_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class { 'python':
pip => 'present',
dev => 'present',
virtualenv => 'present',
venv => 'present',
}
EOS

Expand Down
1 change: 1 addition & 0 deletions spec/acceptance/pyvenv_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class { 'python':
version => '3',
dev => 'present',
venv => 'present',
}
user { 'agent':
ensure => 'present',
Expand Down
23 changes: 22 additions & 1 deletion spec/classes/python_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,42 @@
it { is_expected.to contain_package('python') }
it { is_expected.to contain_package('virtualenv') }
it { is_expected.to contain_package('pip') }
it { is_expected.to contain_package('python-venv') }
end

context 'without managing things' do
let :params do
{
manage_python_package: false,
manage_virtualenv_package: false,
manage_pip_package: false
manage_pip_package: false,
manage_venv_package: false
}
end

it { is_expected.to compile.with_all_deps }
it { is_expected.not_to contain_package('python') }
it { is_expected.not_to contain_package('virtualenv') }
it { is_expected.not_to contain_package('pip') }
it { is_expected.not_to contain_package('python-venv') }
end

context 'with packages present' do
let :params do
{
manage_virtualenv_package: true,
manage_pip_package: true,
manage_venv_package: true,
pip: 'present',
virtualenv: 'present',
venv: 'present'
}
end

it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_package('virtualenv').with(ensure: 'present') }
it { is_expected.to contain_package('pip').with(ensure: 'present') }
it { is_expected.to contain_package('python-venv').with(ensure: 'present') }
end

case facts[:os]['family']
Expand Down
6 changes: 1 addition & 5 deletions spec/defines/pyvenv_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@
end

context 'with default parameters' do
it { is_expected.to contain_file('/opt/env') }
it { is_expected.to contain_file('/opt/env').that_requires('Package[python-venv]') }
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') }

if %w[xenial bionic cosmic disco stretch buster].include?(facts[:lsbdistcodename])
it { is_expected.to contain_package('python3.5-venv').that_comes_before('File[/opt/env]') }
end
end

describe 'when ensure' do
Expand Down
2 changes: 1 addition & 1 deletion types/loglevel.pp
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# @summary A version type to match all valid loglevels for python
# @summary Match all valid loglevels for python
#
type Python::Loglevel = Enum['debug', 'info', 'warning', 'error', 'critical']
2 changes: 1 addition & 1 deletion types/package/ensure.pp
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# @summary A version type to match all valid package ensures for python
# @summary Match all valid package ensures for python
#
type Python::Package::Ensure = Enum['absent', 'present', 'latest']
2 changes: 1 addition & 1 deletion types/provider.pp
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# @summary A version type to match all valid provider for python
# @summary Match all valid provider for python
#
type Python::Provider = Enum['pip', 'scl', 'rhscl', 'anaconda', '']
Loading

0 comments on commit ed824cf

Please sign in to comment.