Defined Type: python::virtualenv

Defined in:
manifests/virtualenv.pp

Summary

Creates Python virtualenv.

Overview

Examples:

install a virtual env at /var/www/project1

python::virtualenv { '/var/www/project1':
  ensure       => present,
  version      => 'system',
  requirements => '/var/www/project1/requirements.txt',
  proxy        => 'http://proxy.domain.com:3128',
  systempkgs   => true,
  index        => 'http://www.example.com/simple/',
}

Parameters:

  • ensure (Any) (defaults to: 'present')
  • version (Any) (defaults to: 'system')

    Python version to use.

  • requirements (Any) (defaults to: false)

    Path to pip requirements.txt file

  • systempkgs (Any) (defaults to: false)

    Copy system site-packages into virtualenv.

  • venv_dir (Any) (defaults to: $name)

    Directory to install virtualenv to

  • ensure_venv_dir (Any) (defaults to: true)

    Create $venv_dir

  • distribute (Any) (defaults to: true)

    Include distribute in the virtualenv

  • index (Any) (defaults to: false)

    Base URL of Python package index

  • owner (Any) (defaults to: 'root')

    The owner of the virtualenv being manipulated

  • group (Any) (defaults to: 'root')

    The group relating to the virtualenv being manipulated

  • mode (Any) (defaults to: '0755')

    Optionally specify directory mode

  • proxy (Optional[Stdlib::HTTPUrl]) (defaults to: undef)

    Proxy server to use for outbound connections

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

    Additional environment variables required to install the packages

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

    Specifies the PATH variable

  • cwd (Any) (defaults to: undef)

    The directory from which to run the “pip install” command

  • timeout (Any) (defaults to: 1800)

    The maximum time in seconds the “pip install” command should take

  • pip_args (Any) (defaults to: '')

    Arguments to pass to pip during initialization

  • extra_pip_args (Any) (defaults to: '')

    Extra arguments to pass to pip after requirements file

  • virtualenv (Any) (defaults to: undef)


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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'manifests/virtualenv.pp', line 33

define python::virtualenv (
  $ensure                          = 'present',
  $version                         = 'system',
  $requirements                    = false,
  $systempkgs                      = false,
  $venv_dir                        = $name,
  $ensure_venv_dir                 = true,
  $distribute                      = true,
  $index                           = false,
  $owner                           = 'root',
  $group                           = 'root',
  $mode                            = '0755',
  Optional[Stdlib::HTTPUrl] $proxy = undef,
  $environment                     = [],
  $path                            = ['/bin', '/usr/bin', '/usr/sbin', '/usr/local/bin'],
  $cwd                             = undef,
  $timeout                         = 1800,
  $pip_args                        = '',
  $extra_pip_args                  = '',
  $virtualenv                      = undef,
) {
  include python
  $python_provider = getparam(Class['python'], 'provider')
  $anaconda_path = getparam(Class['python'], 'anaconda_install_path')

  if $ensure == 'present' {
    $python = $version ? {
      'system' => 'python',
      'pypy'   => 'pypy',
      default  => "python${version}",
    }

    $_path = $python_provider ? {
      'anaconda' => concat(["${anaconda_path}/bin"], $path),
      default    => $path,
    }

    if $virtualenv == undef {
      $used_virtualenv = 'virtualenv'
    } else {
      $used_virtualenv = $virtualenv
    }

    $proxy_flag = $proxy ? {
      false    => '',
      default  => "--proxy=${proxy}",
    }

    $proxy_hash = $proxy ? {
      undef   => {},
      default => $facts['os']['family'] ? {
        'AIX'   => { 'http_proxy' => $proxy, 'https_proxy' => $proxy },
        default => { 'HTTP_PROXY' => $proxy, 'HTTPS_PROXY' => $proxy },
      }
    }

    # Virtualenv versions prior to 1.7 do not support the
    # --system-site-packages flag, default off for prior versions
    # Prior to version 1.7 the default was equal to --system-site-packages
    # and the flag --no-site-packages had to be passed to do the opposite
    $_virtualenv_version = getvar('virtualenv_version') ? {
      /.*/ => getvar('virtualenv_version'),
      default => '',
    }
    if (( versioncmp($_virtualenv_version,'1.7') > 0 ) and ( $systempkgs == true )) {
      $system_pkgs_flag = '--system-site-packages'
    } elsif (( versioncmp($_virtualenv_version,'1.7') < 0 ) and ( $systempkgs == false )) {
      $system_pkgs_flag = '--no-site-packages'
    } else {
      $system_pkgs_flag = $systempkgs ? {
        true    => '--system-site-packages',
        false   => '--no-site-packages',
        default => fail('Invalid value for systempkgs. Boolean value is expected')
      }
    }

    $distribute_pkg = $distribute ? {
      true     => 'distribute',
      default  => 'setuptools',
    }
    $pypi_index = $index ? {
      false   => '',
      default => "-i ${index}",
    }

    # Python 2.6 and older does not support setuptools/distribute > 0.8 which
    # is required for pip wheel support, pip therefor requires --no-use-wheel flag
    # if the # pip version is more recent than 1.4.1 but using an old python or
    # setuputils/distribute version
    # To check for this we test for wheel parameter using help and then using
    # version, this makes sure we only use wheels if they are supported

    if $ensure_venv_dir {
      file { $venv_dir:
        ensure => directory,
        owner  => $owner,
        group  => $group,
        mode   => $mode,
      }
    }

    $virtualenv_cmd = "${python::exec_prefix}${used_virtualenv}"

    $pip_cmd   = "${python::exec_prefix}${venv_dir}/bin/pip"
    $pip_flags = "${pypi_index} ${proxy_flag} ${pip_args}"

    exec { "python_virtualenv_${venv_dir}":
      command     => "${virtualenv_cmd} ${system_pkgs_flag} -p ${python} ${venv_dir} && ${pip_cmd} --log ${venv_dir}/pip.log install ${pip_flags} --upgrade pip && ${pip_cmd} install ${pip_flags} --upgrade ${distribute_pkg}",
      user        => $owner,
      creates     => "${venv_dir}/bin/activate",
      path        => $_path,
      cwd         => '/tmp',
      environment => (Hash($environment.map |$val| { $val.split(/=|$/) }) + $proxy_hash).map|$key, $val| { "${key}=${val}" },
      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],
    }

    if $requirements {
      exec { "python_requirements_initial_install_${requirements}_${venv_dir}":
        command     => "${pip_cmd} --log ${venv_dir}/pip.log install ${pypi_index} ${proxy_flag} --no-binary :all: -r ${requirements} ${extra_pip_args}",
        refreshonly => true,
        timeout     => $timeout,
        user        => $owner,
        subscribe   => Exec["python_virtualenv_${venv_dir}"],
        environment => $environment,
        cwd         => $cwd,
      }

      python::requirements { "${requirements}_${venv_dir}":
        requirements   => $requirements,
        virtualenv     => $venv_dir,
        proxy          => $proxy,
        owner          => $owner,
        group          => $group,
        cwd            => $cwd,
        require        => Exec["python_virtualenv_${venv_dir}"],
        extra_pip_args => $extra_pip_args,
      }
    }
  } elsif $ensure == 'absent' {
    file { $venv_dir:
      ensure  => absent,
      force   => true,
      recurse => true,
      purge   => true,
    }
  }
}