24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
# File 'manifests/pyvenv.pp', line 24
define python::pyvenv (
Python::Package::Ensure $ensure = present,
Python::Version $version = 'system',
Boolean $systempkgs = false,
Stdlib::Absolutepath $venv_dir = $name,
String[1] $owner = 'root',
String[1] $group = 'root',
Stdlib::Filemode $mode = '0755',
Array[Stdlib::Absolutepath] $path = ['/bin', '/usr/bin', '/usr/sbin', '/usr/local/bin',],
Array $environment = [],
Python::Venv::PipVersion $pip_version = 'latest',
) {
include python
if $ensure == 'present' {
$python_version = $version ? {
'system' => $facts['python3_version'],
default => $version,
}
$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"
} else {
$virtualenv_cmd = "${python::exec_prefix}pyvenv-${normalized_python_version}"
}
$_path = $python::provider ? {
'anaconda' => concat(["${python::anaconda_install_path}/bin"], $path),
default => $path,
}
if $systempkgs == true {
$system_pkgs_flag = '--system-site-packages'
} else {
$system_pkgs_flag = ''
}
file { $venv_dir:
ensure => directory,
owner => $owner,
group => $group,
mode => $mode,
}
$pip_cmd = "${python::exec_prefix}${venv_dir}/bin/pip"
$pip_upgrade = ($pip_version != 'latest') ? {
true => "--upgrade 'pip ${pip_version}'",
false => '--upgrade pip',
}
exec { "python_virtualenv_${venv_dir}":
command => "${virtualenv_cmd} --clear ${system_pkgs_flag} ${venv_dir} && ${pip_cmd} --log ${venv_dir}/pip.log install ${pip_upgrade} && ${pip_cmd} --log ${venv_dir}/pip.log install --upgrade setuptools",
user => $owner,
creates => "${venv_dir}/bin/activate",
path => $_path,
cwd => '/tmp',
environment => $environment,
timeout => 600,
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
require => File[$venv_dir],
}
} elsif $ensure == 'absent' {
file { $venv_dir:
ensure => absent,
force => true,
recurse => true,
purge => true,
}
} else {
fail( "Illegal ensure value: ${ensure}. Expected (present or absent)")
}
}
|