Defined Type: python::pip

Defined in:
manifests/pip.pp

Summary

Installs and manages packages from pip.

Overview

Examples:

Install Flask to /var/www/project1 using a proxy

python::pip { 'flask':
  virtualenv => '/var/www/project1',
  proxy      => 'http://proxy.domain.com:3128',
  index      => 'http://www.example.com/simple/',
}

Install cx_Oracle with pip

python::pip { 'cx_Oracle' :
  pkgname       => 'cx_Oracle',
  ensure        => '5.1.2',
  virtualenv    => '/var/www/project1',
  owner         => 'appuser',
  proxy         => 'http://proxy.domain.com:3128',
  environment   => ['ORACLE_HOME=/usr/lib/oracle/11.2/client64'],
  install_args  => '-e',
  timeout       => 1800,
}

Install Requests with pip3

python::pip { 'requests' :
  ensure        => 'present',
  pkgname       => 'requests',
  pip_provider  => 'pip3',
  virtualenv    => '/var/www/project1',
  owner         => 'root',
  timeout       => 1800
}

Parameters:

  • name

    must be unique

  • pkgname (String[1]) (defaults to: $name)

    the name of the package.

  • ensure (Variant[Enum[present, absent, latest], String[1]]) (defaults to: present)

    Require pip to be available.

  • virtualenv (Variant[Enum['system'], Stdlib::Absolutepath]) (defaults to: 'system')

    virtualenv to run pip in.

  • pip_provider (String[1]) (defaults to: 'pip')

    version of pip you wish to use.

  • url (Variant[Boolean, String]) (defaults to: false)

    URL to install from.

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

    The owner of the virtualenv being manipulated.

  • group (Optional[String[1]]) (defaults to: getvar('python::params::group'))

    The group of the virtualenv being manipulated.

  • index (Variant[Boolean,String[1]]) (defaults to: false)

    Base URL of Python package index.

  • extra_index (Variant[Boolean,String[1]]) (defaults to: false)

    Base URL of extra Python package index.

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

    Proxy server to use for outbound connections.

  • editable (Boolean) (defaults to: false)

    If true the package is installed as an editable resource.

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

    Additional environment variables required to install the packages.

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

    Extra features provided by the package which should be installed.

  • timeout (Numeric) (defaults to: 1800)

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

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

    Any additional installation arguments that will be supplied when running pip install.

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

    Any additional arguments that will be supplied when running pip uninstall.

  • log_dir (String[1]) (defaults to: '/tmp')

    Log directory

  • egg (Any) (defaults to: false)

    The egg name to use

  • umask (Optional[Python::Umask]) (defaults to: undef)
  • path (Array[String]) (defaults to: ['/usr/local/bin','/usr/bin','/bin', '/usr/sbin'])
  • exec_provider (String[1]) (defaults to: 'shell')


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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'manifests/pip.pp', line 51

define python::pip (
  String[1]                                         $pkgname        = $name,
  Variant[Enum[present, absent, latest], String[1]] $ensure         = present,
  Variant[Enum['system'], Stdlib::Absolutepath]     $virtualenv     = 'system',
  String[1]                                         $pip_provider   = 'pip',
  Variant[Boolean, String]                          $url            = false,
  String[1]                                         $owner          = 'root',
  Optional[String[1]]                               $group          = getvar('python::params::group'),
  Optional[Python::Umask]                           $umask          = undef,
  Variant[Boolean,String[1]]                        $index          = false,
  Variant[Boolean,String[1]]                        $extra_index    = false,
  Optional[Stdlib::HTTPUrl]                         $proxy          = undef,
  Any                                               $egg            = false,
  Boolean                                           $editable       = false,
  Array                                             $environment    = [],
  Array                                             $extras         = [],
  Optional[String[1]]                               $install_args   = undef,
  Optional[String[1]]                               $uninstall_args = undef,
  Numeric                                           $timeout        = 1800,
  String[1]                                         $log_dir        = '/tmp',
  Array[String]                                     $path           = ['/usr/local/bin','/usr/bin','/bin', '/usr/sbin'],
  String[1]                                         $exec_provider  = 'shell',
) {
  $python_provider = getparam(Class['python'], 'provider')
  $python_version  = getparam(Class['python'], 'version')

  if $virtualenv != 'system' {
    Python::Pyvenv <| |> -> Python::Pip[$name]
  }

  # Get SCL exec prefix
  # NB: this will not work if you are running puppet from scl enabled shell
  $exec_prefix = $python_provider ? {
    'scl'   => "scl enable ${python_version} -- ",
    'rhscl' => "scl enable ${python_version} -- ",
    default => '',
  }

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

  # Parameter validation
  if $virtualenv == 'system' and $owner != 'root' {
    fail('python::pip: root user must be used when virtualenv is system')
  }

  $cwd = $virtualenv ? {
    'system' => '/',
    default  => $virtualenv,
  }

  $log = $virtualenv ? {
    'system' => $log_dir,
    default  => $virtualenv,
  }

  $pip_env = $virtualenv ? {
    'system' => "${exec_prefix}${pip_provider}",
    default  => "${exec_prefix}${virtualenv}/bin/${pip_provider}",
  }

  $pypi_index = $index ? {
    false   => '',
    default => "--index-url=${index}",
  }

  $pypi_extra_index = $extra_index ? {
    false   => '',
    default => "--extra-index-url=${extra_index}",
  }

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

  if $editable == true {
    $install_editable = ' -e '
  }
  else {
    $install_editable = ''
  }

  # TODO: Do more robust argument checking, but below is a start
  if ($ensure == absent) and $install_args {
    fail('python::pip cannot provide install_args with ensure => absent')
  }

  if ($ensure == present) and $uninstall_args {
    fail('python::pip cannot provide uninstall_args with ensure => present')
  }

  if $pkgname =~ /==/ {
    $parts        = split($pkgname, '==')
    $real_pkgname = $parts[0]

    $_ensure = $ensure ? {
      'absent' => 'absent',
      default => $parts[1],
    }
  } else {
    $real_pkgname = $pkgname
    $_ensure      = $ensure
  }

  # We do not try to mimic a version scheme validation which is already implemented by the package manager.
  # If it starts with a number it is probably a version.
  # If it wasn't or if there is any error, the package manager will trigger a failure.
  $grep_regex = $_ensure ? {
    /^(present|absent|latest)$/ => "^${real_pkgname}[[:space:]].*$",
    /^[0-9].*$/                 => "^${real_pkgname}[[:space:]]\\+(\\?${_ensure}\\()$\\|$\\|, \\|[[:space:]]\\)",
    default                     => fail('ensure can be a version number or one of: present, absent, latest')
  }

  $extras_string = empty($extras) ? {
    true    => '',
    default => sprintf('[%s]',join($extras,',')),
  }

  $egg_name = $egg ? {
    false   => "${real_pkgname}${extras_string}",
    default => $egg
  }

  $source = $url ? {
    false                                                                     => "${real_pkgname}${extras_string}",
    /^(\/|[a-zA-Z]\:)/                                                        => "'${url}'",
    /^(git\+|hg\+|bzr\+|svn\+)(http|https|ssh|svn|sftp|ftp|lp|git)(:\/\/).+$/ => "'${url}'",
    default                                                                   => "'${url}#egg=${egg_name}'",
  }

  $pip_install     = "${pip_env} --log ${log}/pip.log install"
  $pip_common_args = "${pypi_index} ${pypi_extra_index} ${proxy_flag} ${install_editable} ${source}"

  # Explicit version out of VCS when PIP supported URL is provided
  if $source =~ /^'(git\+|hg\+|bzr\+|svn\+)(http|https|ssh|svn|sftp|ftp|lp|git)(:\/\/).+'$/ {
    if $_ensure != present and $_ensure != latest {
      $command        = "${pip_install} ${install_args} ${pip_common_args}@${_ensure}#egg=${egg_name}"
      $unless_command = "${pip_env} list | grep -i -e '${grep_regex}'"
    } else {
      $command        = "${pip_install} ${install_args} ${pip_common_args}"
      $unless_command = "${pip_env} list | grep -i -e '${grep_regex}'"
    }
  } else {
    case $_ensure {
      /^[0-9].*$/: {
        # Specific version
        $command        = "${pip_install} ${install_args} ${pip_common_args}==${_ensure}"
        $unless_command = "${pip_env} list | grep -i -e '${grep_regex}'"
      }

      'present': {
        # Whatever version is available.
        $command        = "${pip_install} ${install_args} ${pip_common_args}"
        $unless_command = "${pip_env} list | grep -i -e '${grep_regex}'"
      }

      'latest': {
        $pip_version = $facts['pip_version']
        if $pip_version and versioncmp($pip_version, '21.1') == -1 and versioncmp($pip_version, '20.2.4') == 1 {
          $legacy_resolver = '--use-deprecated=legacy-resolver'
        } else {
          $legacy_resolver = ''
        }

        # Unfortunately this is the smartest way of getting the latest available package version with pip as of now
        # Note: we DO need to repeat ourselves with "from version" in both grep and sed as on some systems pip returns
        # more than one line with paretheses.
        $latest_version = join([
            "${pip_install} ${legacy_resolver} ${pypi_index} ${pypi_extra_index} ${proxy_flag}",
            " ${install_args} ${install_editable} '${real_pkgname}==9!0dev0+x' 2>&1",
            " | sed -nE 's/.*\\(from versions: (.*, )*(.*)\\)/\\2/p'",
            ' | tr -d "[:space:]"',
        ])

        # Packages with underscores in their names are listed with dashes in their place in `pip freeze` output
        $pkgname_with_dashes            = regsubst($real_pkgname, '_', '-', 'G')
        $grep_regex_pkgname_with_dashes = "^${pkgname_with_dashes}=="
        $installed_version              = join(["${pip_env} freeze --all", " | grep -i -e ${grep_regex_pkgname_with_dashes} | cut -d= -f3", " | tr -d '[:space:]'",])

        $command        = "${pip_install} --upgrade ${install_args} ${pip_common_args}"
        $unless_command = "[ \$(${latest_version}) = \$(${installed_version}) ]"
      }

      default: {
        # Anti-action, uninstall.
        $command        = "echo y | ${pip_env} uninstall ${uninstall_args} ${proxy_flag} ${real_pkgname}"
        $unless_command = "! ${pip_env} list | grep -i -e '${grep_regex}'"
      }
    }
  }

  $pip_installer = ($ensure == 'absent') ? {
    true  => "pip_uninstall_${name}",
    false => "pip_install_${name}",
  }

  exec { $pip_installer:
    command     => $command,
    unless      => $unless_command,
    user        => $owner,
    group       => $group,
    umask       => $umask,
    cwd         => $cwd,
    environment => $environment,
    timeout     => $timeout,
    path        => $_path,
    provider    => $exec_provider,
  }
}