Defined Type: python::requirements

Defined in:
manifests/requirements.pp

Summary

Installs and manages Python packages from requirements file.

Overview

Examples:

install pip requirements from /var/www/project1/requirements.txt

python::requirements { '/var/www/project1/requirements.txt' :
  virtualenv => '/var/www/project1',
  proxy      => 'http://proxy.domain.com:3128',
  owner      => 'appuser',
  group      => 'apps',
}

Parameters:

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

    Path to the requirements file.

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

    virtualenv to run pip in.

  • pip_provider (Enum['pip', 'pip3']) (defaults to: 'pip')

    version of pip you wish to use.

  • 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.

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

    Proxy server to use for outbound connections.

  • src (Any) (defaults to: false)

    Pip –src parameter to; if the requirements file contains –editable resources, this parameter specifies where they will be installed. See the pip documentation for more.

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

    Additional environment variables required to install the packages.

  • forceupdate (Boolean) (defaults to: false)

    Run a pip install requirements even if we don’t receive an event from the requirements file - Useful for when the requirements file is written as part of a resource other than file (E.g vcsrepo)

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

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

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

    Extra arguments to pass to pip after the requirements file

  • manage_requirements (Boolean) (defaults to: true)

    Create the requirements file if it doesn’t exist.

  • fix_requirements_owner (Boolean) (defaults to: true)

    Change owner and group of requirements file.

  • log_dir (Stdlib::Absolutepath) (defaults to: '/tmp')

    Log directory.

  • timeout (Integer) (defaults to: 1800)

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



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
# File 'manifests/requirements.pp', line 28

define python::requirements (
  Stdlib::Absolutepath                         $requirements           = $name,
  Variant[Enum['system'],Stdlib::Absolutepath] $virtualenv             = 'system',
  Enum['pip', 'pip3']                          $pip_provider           = 'pip',
  String[1]                                    $owner                  = 'root',
  String[1]                                    $group                  = 'root',
  Optional[Stdlib::HTTPUrl]                    $proxy                  = undef,
  Any                                          $src                    = false,
  Array                                        $environment            = [],
  Boolean                                      $forceupdate            = false,
  Optional[Stdlib::Absolutepath]               $cwd                    = undef,
  Optional[String[1]]                          $extra_pip_args         = undef,
  Boolean                                      $manage_requirements    = true,
  Boolean                                      $fix_requirements_owner = true,
  Stdlib::Absolutepath                         $log_dir                = '/tmp',
  Integer                                      $timeout                = 1800,
) {
  include python

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

  if $fix_requirements_owner {
    $owner_real = $owner
    $group_real = $group
  } else {
    $owner_real = undef
    $group_real = undef
  }

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

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

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

  $src_flag = $src ? {
    false   => '',
    default => "--src=${src}",
  }

  # This will ensure multiple python::virtualenv definitions can share the
  # the same requirements file.
  if !defined(File[$requirements]) and $manage_requirements == true {
    file { $requirements:
      ensure  => file,
      mode    => '0644',
      owner   => $owner_real,
      group   => $group_real,
      audit   => content,
      replace => false,
      content => '# Puppet will install and/or update pip packages listed here',
    }

    $local_subscribe = File[$requirements]
  } elsif File[$requirements] and $manage_requirements == true {
    $local_subscribe = File[$requirements]
  } else {
    $local_subscribe = undef
  }

  exec { "python_requirements${name}":
    provider    => shell,
    command     => "${pip_env} --log ${log}/pip.log install ${proxy_flag} ${src_flag} -r ${requirements} ${extra_pip_args}",
    refreshonly => !$forceupdate,
    timeout     => $timeout,
    cwd         => $cwd,
    user        => $owner,
    subscribe   => $local_subscribe,
    environment => $environment,
  }
}