Defined Type: python::pyvenv

Defined in:
manifests/pyvenv.pp

Summary

Create a Python3 virtualenv using pyvenv.

Overview

Examples:

python::pyvenv { '/var/www/project1' :
  ensure       => present,
  version      => 'system',
  systempkgs   => true,
  venv_dir     => '/home/appuser/virtualenvs',
  owner        => 'appuser',
  group        => 'apps',
}

Parameters:

  • ensure (Python::Package::Ensure) (defaults to: present)
  • version (Python::Version) (defaults to: 'system')

    Python version to use.

  • systempkgs (Boolean) (defaults to: false)

    Copy system site-packages into virtualenv

  • venv_dir (Stdlib::Absolutepath) (defaults to: $name)

    Directory to install virtualenv to

  • owner (String[1]) (defaults to: 'root')

    The owner of the virtualenv being manipulated

  • group (String[1]) (defaults to: 'root')

    The group relating to the virtualenv being manipulated

  • mode (Stdlib::Filemode) (defaults to: '0755')

    Optionally specify directory mode

  • path (Array[Stdlib::Absolutepath]) (defaults to: ['/bin', '/usr/bin', '/usr/sbin', '/usr/local/bin',])

    Specifies the PATH variable.

  • environment (Array) (defaults to: [])

    Optionally specify environment variables for pyvenv

  • prompt (Optional[String[1]]) (defaults to: undef)

    Optionally specify the virtualenv prompt (python >= 3.6)

  • python_path (Optional[Stdlib::Absolutepath]) (defaults to: undef)

    Optionally specify python path for creation of virtualenv

  • pip_version (Python::Venv::PipVersion) (defaults to: 'latest')


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
109
110
111
112
113
114
115
116
117
# File 'manifests/pyvenv.pp', line 26

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 = [],
  Optional[String[1]]         $prompt      = undef,
  Python::Venv::PipVersion    $pip_version = 'latest',
  Optional[Stdlib::Absolutepath] $python_path = undef,
) {
  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])

    $local_exec_prefix = $python_path ? {
      undef   => $python::exec_prefix,
      default => $python_path,
    }
    # pyvenv is deprecated since 3.6 and will be removed in 3.8
    if $python_path != undef {
      $virtualenv_cmd = "${python_path} -m venv"
    } elsif versioncmp($normalized_python_version, '3.6') >=0 {
      $virtualenv_cmd = "${local_exec_prefix}python${normalized_python_version} -m venv"
    } else {
      $virtualenv_cmd = "${local_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 = ''
    }

    if versioncmp($normalized_python_version, '3.6') >=0 and $prompt {
      $prompt_arg = "--prompt ${shell_escape($prompt)}"
    } else {
      $prompt_arg = ''
    }

    file { $venv_dir:
      ensure  => directory,
      owner   => $owner,
      group   => $group,
      mode    => $mode,
      require => Class['python::install'],
    }

    $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} ${prompt_arg} ${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)")
  }
}