Commits

Nicola Paolucci committed c1064ee

Squashed 'modules/python/' content from commit dd6d873

git-subtree-dir: modules/python
git-subtree-split: dd6d87326503e5f9e67651d66f0b7ab96ab91d7b

Comments (0)

Files changed (17)

+Copyright © 2012 Sergey Stankevich
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    [http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0)
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+name         'puppet-python'
+version      '1.1.4'
+
+author       'Sergey Stankevich'
+license      'Apache License, Version 2.0'
+project_page 'https://github.com/stankevich/puppet-python'
+source       'git://github.com/stankevich/puppet-python'
+summary      'Puppet module for Python'
+description  'Installs and manages Python, pip, virtualenv, Gunicorn virtual hosts.'
+[puppet-python](https://github.com/stankevich/puppet-python)
+======
+
+Puppet module for installing and managing python, pip, virtualenvs and Gunicorn virtual hosts.
+
+**Version 1.1.x Notes**
+
+Version 1.1.x makes several fundamental changes to the core of this module, adding some additional features, improving performance and making operations more robust in general.
+
+Please note that everal changes have been made in v1.1.x which make manifests incompatible with the previous version.  However, modifying your manifests to suit is trivial.  Please see the notes below.
+
+Currently, the changes you need to make are as follows:
+
+* All pip definitions MUST include the owner field which specifies which user owns the virtualenv that packages will be installed in.  Adding this greatly improves performance and efficiency of this module.
+* You must explicitly specify pip => true in the python class if you want pip installed.  As such, the pip package is now independent of the dev package and so one can exist without the other.
+
+## Installation
+
+``` bash
+cd /etc/puppet/modules
+git clone git://github.com/stankevich/puppet-python.git python
+```
+
+## Usage
+
+### python
+
+Installs and manages python, python-dev, python-virtualenv and Gunicorn.
+
+**version** - Python version to install. Default: system default
+
+**pip** - Install python-pip. Default: false
+
+**dev** - Install python-dev. Default: false
+
+**virtualenv** - Install python-virtualenv. Default: false
+
+**gunicorn** - Install Gunicorn. Default: false
+
+	class { 'python':
+	  version    => 'system',
+	  dev        => true,
+	  virtualenv => true,
+	  gunicorn   => true,
+	}
+
+### python::pip
+
+Installs and manages packages from pip.
+
+**ensure** - present/absent. Default: present
+
+**virtualenv** - virtualenv to run pip in. Default: system (no virtualenv)
+
+**url** - URL to install from. Default: none
+
+**owner** - The owner of the virtualenv to ensure that packages are installed with the correct permissions (must be specified). Default: root
+
+**proxy** - Proxy server to use for outbound connections. Default: none
+
+**environment** - Additional environment variables required to install the packages. Default: none
+
+	python::pip { 'cx_Oracle':
+	  virtualenv  => '/var/www/project1',
+	  owner       => 'appuser',
+	  proxy       => 'http://proxy.domain.com:3128',
+	  environment => 'ORACLE_HOME=/usr/lib/oracle/11.2/client64',
+	}
+
+### python::requirements
+
+Installs and manages Python packages from requirements file.
+
+**virtualenv** - virtualenv to run pip in. Default: system-wide
+
+**proxy** - Proxy server to use for outbound connections. Default: none
+
+**owner** - The owner of the virtualenv to ensure that packages are installed with the correct permissions (must be specified). Default: root
+
+**group** - The group that was used to create the virtualenv.  This is used to create the requirements file with correct permissions if it's not present already.
+
+	python::requirements { '/var/www/project1/requirements.txt':
+	  virtualenv => '/var/www/project1',
+	  proxy      => 'http://proxy.domain.com:3128',
+	  owner      => 'appuser',
+	  group      => 'apps',
+	}
+
+### python::virtualenv
+
+Creates Python virtualenv.
+
+**ensure** - present/absent. Default: present
+
+**version** - Python version to use. Default: system default
+
+**requirements** - Path to pip requirements.txt file. Default: none
+
+**proxy** - Proxy server to use for outbound connections. Default: none
+
+**systempkgs** - Copy system site-packages into virtualenv. Default: don't
+
+**distribute** - Include distribute in the virtualenv. Default: true
+
+**owner** - Specify the owner of this virtualenv
+
+**group** - Specify the group for this virtualenv
+
+**index** - Base URL of Python package index. Default: none
+
+	python::virtualenv { '/var/www/project1':
+	  ensure       => present,
+	  version      => 'system',
+	  requirements => '/var/www/project1/requirements.txt',
+	  proxy        => 'http://proxy.domain.com:3128',
+	  systempkgs   => true,
+	  distribute   => false,
+	  owner        => 'appuser',
+	  group        => 'apps',
+	}
+
+### python::gunicorn
+
+Manages Gunicorn virtual hosts.
+
+**ensure** - present/absent. Default: present
+
+**virtualenv** - Run in virtualenv, specify directory. Default: disabled
+
+**mode** - Gunicorn mode. wsgi/django. Default: wsgi
+
+**dir** - Application directory.
+
+**bind** - Bind on: 'HOST', 'HOST:PORT', 'unix:PATH'. Default: unix:/tmp/gunicorn-$name.socket or unix:${virtualenv}/${name}.socket
+
+**environment** - Set ENVIRONMENT variable. Default: none
+
+**template** - Which ERB template to use. Default: python/gunicorn.erb
+
+	python::gunicorn { 'vhost':
+	  ensure      => present,
+	  virtualenv  => '/var/www/project1',
+	  mode        => 'wsgi',
+	  dir         => '/var/www/project1/current',
+	  bind        => 'unix:/tmp/gunicorn.socket',
+	  environment => 'prod',
+	  template    => 'python/gunicorn.erb',
+	}
+
+## Authors
+
+[Sergey Stankevich](https://github.com/stankevich)
+[Ashley Penney](https://github.com/apenney)
+[Marc Fournier](https://github.com/mfournier)
+[Fotis Gimian](https://github.com/fgimian)
+# Rakefile for puppet-lint (https://github.com/rodjek/puppet-lint)
+# Run: rake lint
+
+require 'puppet-lint/tasks/puppet-lint'
+PuppetLint.configuration.with_filename = true
+PuppetLint.configuration.send('disable_documentation')
+PuppetLint.configuration.send('disable_class_parameter_defaults')
+PuppetLint.configuration.send('disable_80chars')

manifests/config.pp

+class python::config {
+
+  Class['python::install'] -> Python::Pip <| |>
+  Class['python::install'] -> Python::Requirements <| |>
+  Class['python::install'] -> Python::Virtualenv <| |>
+
+  Python::Virtualenv <| |> -> Python::Pip <| |>
+
+  if $python::gunicorn {
+    Class['python::install'] -> Python::Gunicorn <| |>
+
+    Python::Gunicorn <| |> ~> Service['gunicorn']
+
+    service { 'gunicorn':
+      ensure     => running,
+      enable     => true,
+      hasrestart => true,
+      hasstatus  => false,
+      pattern    => '/usr/bin/gunicorn',
+    }
+  }
+
+}

manifests/gunicorn.pp

+# == Define: python::gunicorn
+#
+# Manages Gunicorn virtual hosts.
+#
+# === Parameters
+#
+# [*ensure*]
+#  present|absent. Default: present
+#
+# [*virtualenv*]
+#  Run in virtualenv, specify directory. Default: disabled
+#
+# [*mode*]
+#  Gunicorn mode.
+#  wsgi|django. Default: wsgi
+#
+# [*dir*]
+#  Application directory.
+#
+# [*bind*]
+#  Bind on: 'HOST', 'HOST:PORT', 'unix:PATH'.
+#  Default: system-wide: unix:/tmp/gunicorn-$name.socket
+#           virtualenv:  unix:${virtualenv}/${name}.socket
+#
+# [*environment*]
+#  Set ENVIRONMENT variable. Default: none
+#
+# [*template*]
+#  Which ERB template to use. Default: python/gunicorn.erb
+#
+# === Examples
+#
+# python::gunicorn { 'vhost':
+#   ensure      => present,
+#   virtualenv  => '/var/www/project1',
+#   mode        => 'wsgi',
+#   dir         => '/var/www/project1/current',
+#   bind        => 'unix:/tmp/gunicorn.socket',
+#   environment => 'prod',
+#   template    => 'python/gunicorn.erb',
+# }
+#
+# === Authors
+#
+# Sergey Stankevich
+# Ashley Penney
+# Marc Fournier
+#
+define python::gunicorn (
+  $ensure        = present,
+  $virtualenv    = false,
+  $mode          = 'wsgi',
+  $dir           = false,
+  $bind          = false,
+  $environment   = false,
+  $template      = 'python/gunicorn.erb',
+) {
+
+  # Parameter validation
+  if ! $dir {
+    fail('python::gunicorn: dir parameter must not be empty')
+  }
+
+  file { "/etc/gunicorn.d/${name}":
+    ensure  => $ensure,
+    mode    => '0644',
+    owner   => 'root',
+    group   => 'root',
+    content => template($template),
+  }
+
+}

manifests/init.pp

+# == Class: python
+#
+# Installs and manages python, python-dev, python-virtualenv and Gunicorn.
+#
+# === Parameters
+#
+# [*version*]
+#  Python version to install. Default: system default
+#
+# [*pip*]
+#  Install python-pip. Default: false
+#
+# [*dev*]
+#  Install python-dev. Default: false
+#
+# [*virtualenv*]
+#  Install python-virtualenv. Default: false
+#
+# [*gunicorn*]
+#  Install Gunicorn. Default: false
+#
+# === Examples
+#
+# class { 'python':
+#   version    => 'system',
+#   pip        => true,
+#   dev        => true,
+#   virtualenv => true,
+#   gunicorn   => true,
+# }
+#
+# === Authors
+#
+# Sergey Stankevich
+#
+class python (
+  $version    = 'system',
+  $pip        = false,
+  $dev        = false,
+  $virtualenv = false,
+  $gunicorn   = false
+) {
+
+  # Module compatibility check
+  $compatible = [ 'Debian', 'Ubuntu', 'CentOS', 'RedHat' ]
+  if ! ($::operatingsystem in $compatible) {
+    fail("Module is not compatible with ${::operatingsystem}")
+  }
+
+  Class['python::install'] -> Class['python::config']
+
+  include python::install
+  include python::config
+
+}

manifests/install.pp

+class python::install {
+
+  $python = $python::version ? {
+    'system' => 'python',
+    default  => "python${python::version}",
+  }
+
+  $pythondev = $::operatingsystem ? {
+    /(?i:RedHat|CentOS|Fedora)/ => "${python}-devel",
+    /(?i:Debian|Ubuntu)/        => "${python}-dev"
+  }
+
+  package { $python: ensure => present }
+
+  $dev_ensure = $python::dev ? {
+    true    => present,
+    default => absent,
+  }
+
+  $pip_ensure = $python::pip ? {
+    true    => present,
+    default => absent,
+  }
+
+  package { $pythondev: ensure => $dev_ensure }
+  package { 'python-pip': ensure => $pip_ensure }
+
+  $venv_ensure = $python::virtualenv ? {
+    true    => present,
+    default => absent,
+  }
+
+  package { 'python-virtualenv': ensure => $venv_ensure }
+
+  $gunicorn_ensure = $python::gunicorn ? {
+    true    => present,
+    default => absent,
+  }
+
+  package { 'gunicorn': ensure => $gunicorn_ensure }
+
+}
+# == Define: python::pip
+#
+# Installs and manages packages from pip.
+#
+# === Parameters
+#
+# [*ensure*]
+#  present|absent. Default: present
+#
+# [*virtualenv*]
+#  virtualenv to run pip in.
+#
+# [*url*]
+#  URL to install from. Default: none
+#
+# [*owner*]
+#  The owner of the virtualenv being manipulated. Default: root
+#
+# [*proxy*]
+#  Proxy server to use for outbound connections. Default: none
+#
+# [*environment*]
+#  Additional environment variables required to install the packages. Default: none
+#
+# === Examples
+#
+# python::pip { 'flask':
+#   virtualenv => '/var/www/project1',
+#   proxy      => 'http://proxy.domain.com:3128',
+# }
+#
+# === Authors
+#
+# Sergey Stankevich
+# Fotis Gimian
+#
+define python::pip (
+  $ensure      = present,
+  $virtualenv  = 'system',
+  $url         = false,
+  $owner       = 'root',
+  $proxy       = false,
+  $environment = []
+) {
+
+  # Parameter validation
+  if ! $virtualenv {
+    fail('python::pip: virtualenv parameter must not be empty')
+  }
+
+  if $virtualenv == 'system' and $owner != 'root' {
+    fail('python::pip: root user must be used when virtualenv is system')
+  }
+
+  $cwd = $virtualenv ? {
+    'system' => '/',
+    default  => "${virtualenv}",
+  }
+
+  $pip_env = $virtualenv ? {
+    'system' => 'pip',
+    default  => "${virtualenv}/bin/pip",
+  }
+
+  $proxy_flag = $proxy ? {
+    false    => '',
+    default  => "--proxy=${proxy}",
+  }
+
+  $grep_regex = $name ? {
+    /==/    => "^${name}\$",
+    default => "^${name}==",
+  }
+
+  $source = $url ? {
+    false   => $name,
+    default => "${url}#egg=${name}",
+  }
+
+  case $ensure {
+    present: {
+      exec { "pip_install_${name}":
+        command     => "$pip_env --log-file ${cwd}/pip.log install ${proxy_flag} ${source}",
+        unless      => "$pip_env freeze | grep -i -e ${grep_regex}",
+        user        => $owner,
+        environment => $environment,
+      }
+    }
+
+    default: {
+      exec { "pip_uninstall_${name}":
+        command     => "echo y | $pip_env uninstall ${proxy_flag} ${name}",
+        onlyif      => "$pip_env freeze | grep -i -e ${grep_regex}",
+        user        => $owner,
+        environment => $environment,
+      }
+    }
+  }
+
+}

manifests/requirements.pp

+# == Define: python::requirements
+#
+# Installs and manages Python packages from requirements file.
+#
+# === Parameters
+#
+# [*requirements*]
+#  Path to the requirements file. Defaults to the resource name
+#
+# [*virtualenv*]
+#  virtualenv to run pip in. Default: system-wide
+#
+# [*owner*]
+#  The owner of the virtualenv being manipulated. Default: root
+#
+# [*group*]
+#  The group relating to the virtualenv being manipulated. Default: root
+#
+# [*proxy*]
+#  Proxy server to use for outbound connections. Default: none
+#
+# [*environment*]
+#  Additional environment variables required to install the packages. Default: none
+#
+# === Examples
+#
+# python::requirements { '/var/www/project1/requirements.txt':
+#   virtualenv => '/var/www/project1',
+#   proxy      => 'http://proxy.domain.com:3128',
+# }
+#
+# === Authors
+#
+# Sergey Stankevich
+# Ashley Penney
+# Fotis Gimian
+#
+define python::requirements (
+  $requirements = $name,
+  $virtualenv   = 'system',
+  $owner        = 'root',
+  $group        = 'root',
+  $proxy        = false,
+  $environment = []
+) {
+
+  if $virtualenv == 'system' and ($owner != 'root' or $group != 'root') {
+    fail('python::pip: root user must be used when virtualenv is system')
+  }
+
+  $cwd = $virtualenv ? {
+    'system' => '/',
+    default  => "${virtualenv}",
+  }
+
+  $pip_env = $virtualenv ? {
+    'system' => 'pip',
+    default  => "${virtualenv}/bin/pip",
+  }
+
+  $proxy_flag = $proxy ? {
+    false    => '',
+    default  => "--proxy=${proxy}",
+  }
+
+  # This will ensure multiple python::virtualenv definitions can share the
+  # the same requirements file.
+  if !defined(File[$requirements]) {
+    file { $requirements:
+      ensure  => present,
+      mode    => '0644',
+      owner   => $owner,
+      group   => $group,
+      audit   => content,
+      replace => false,
+      content => '# Puppet will install and/or update pip packages listed here',
+    }
+  }
+
+  exec { "python_requirements${name}":
+    provider    => shell,
+    command     => "${pip_env} --log-file ${cwd}/pip.log install ${proxy_flag} -r ${requirements}",
+    refreshonly => true,
+    timeout     => 1800,
+    user        => $owner,
+    subscribe   => File[$requirements],
+    environment => $environment,
+  }
+
+}

manifests/virtualenv.pp

+# == Define: python::virtualenv
+#
+# Creates Python virtualenv.
+#
+# === Parameters
+#
+# [*ensure*]
+#  present|absent. Default: present
+#
+# [*version*]
+#  Python version to use. Default: system default
+#
+# [*requirements*]
+#  Path to pip requirements.txt file. Default: none
+#
+# [*systempkgs*]
+#  Copy system site-packages into virtualenv. Default: don't
+#
+# [*distribute*]
+#  Include distribute in the virtualenv. Default: true
+#
+# [*index*]
+#  Base URL of Python package index. Default: none (http://pypi.python.org/simple/)
+#
+# [*owner*]
+#  The owner of the virtualenv being manipulated. Default: root
+#
+# [*group*]
+#  The group relating to the virtualenv being manipulated. Default: root
+#
+# [*proxy*]
+#  Proxy server to use for outbound connections. Default: none
+#
+# [*environment*]
+#  Additional environment variables required to install the packages. Default: none
+#
+# === Examples
+#
+# 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/'
+# }
+#
+# === Authors
+#
+# Sergey Stankevich
+# Ashley Penney
+# Marc Fournier
+# Fotis Gimian
+#
+define python::virtualenv (
+  $ensure       = present,
+  $version      = 'system',
+  $requirements = false,
+  $systempkgs   = false,
+  $distribute   = true,
+  $index        = false,
+  $owner        = 'root',
+  $group        = 'root',
+  $proxy        = false,
+  $environment = []
+) {
+
+  $venv_dir = $name
+
+  if $ensure == 'present' {
+
+    $python = $version ? {
+      'system' => 'python',
+      default  => "python${version}",
+    }
+
+    $proxy_flag = $proxy ? {
+      false    => '',
+      default  => "--proxy=${proxy}",
+    }
+
+    $proxy_command = $proxy ? {
+      false   => '',
+      default => "&& export http_proxy=${proxy}",
+    }
+
+    $system_pkgs_flag = $systempkgs ? {
+      false    => '',
+      default  => '--system-site-packages',
+    }
+
+    $distribute_pkg = $distribute ? {
+      true     => 'distribute',
+      default  => '',
+    }
+    $pypi_index = $index ? {
+        false   => '',
+        default => "-i ${index}",
+    }
+
+    exec { "python_virtualenv_${venv_dir}":
+      command => "mkdir -p ${venv_dir} ${proxy_command} && virtualenv ${system_pkgs_flag} ${venv_dir} && ${venv_dir}/bin/pip --log-file ${venv_dir}/pip.log install ${pypi_index} ${proxy_flag} --upgrade ${distribute_pkg} pip",
+      user    => $owner,
+      creates => "${venv_dir}/bin/activate",
+      path    => [ '/bin', '/usr/bin', '/usr/sbin' ],
+      cwd     => "/tmp",
+      environment => $environment,
+    }
+
+    if $requirements {
+      exec { "python_requirements_initial_install_${requirements}_${venv_dir}":
+        command     => "${venv_dir}/bin/pip --log-file ${venv_dir}/pip.log install ${pypi_index} ${proxy_flag} -r ${requirements}",
+        refreshonly => true,
+        timeout     => 1800,
+        user        => $owner,
+        subscribe   => Exec["python_virtualenv_${venv_dir}"],
+        environment => $environment,
+      }
+
+      python::requirements { "${requirements}_${venv_dir}":
+        requirements => $requirements,
+        virtualenv   => $venv_dir,
+        proxy        => $proxy,
+        owner        => $owner,
+        group        => $group,
+        require      => Exec["python_virtualenv_${venv_dir}"],
+      }
+    }
+
+  } elsif $ensure == 'absent' {
+
+    file { $venv_dir:
+      ensure  => absent,
+      force   => true,
+      recurse => true,
+      purge   => true,
+    }
+
+  }
+
+}

templates/gunicorn.erb

+CONFIG = {
+<% if mode == 'django' -%>
+  'mode': 'django',
+<% else -%>
+  'mode': 'wsgi',
+<% end -%>
+<% if virtualenv -%>
+  'environment': {
+<% if environment -%>
+    'ENVIRONMENT': '<%= environment %>',
+<% end -%>
+    'PYTHONPATH': '<%= virtualenv %>'
+  },
+<% end -%>
+  'working_dir': '<%= dir %>',
+  'user': 'www-data',
+  'group': 'www-data',
+<% if virtualenv -%>
+  'python': '<%= virtualenv %>/bin/python',
+<% else -%>
+  'python': '/usr/bin/python',
+<% end -%>
+  'args': (
+<% if !virtualenv and !bind -%>
+    '--bind=unix:/tmp/gunicorn-<%= name %>.socket',
+<% elsif virtualenv and !bind -%>
+    '--bind=unix:<%= virtualenv %>/<%= name %>.socket',
+<% else -%>
+    '--bind=<%= bind %>',
+<% end -%>
+    '--workers=<%= @processorcount.to_i*2 %>',
+    '--timeout=30',
+<% if mode != 'django' -%>
+    'app:app',
+<% end -%>
+  ),
+}

tests/gunicorn.pp

+class { 'python':
+  version    => 'system',
+  dev        => true,
+  virtualenv => true,
+}
+
+python::gunicorn { 'vhost':
+  ensure      => present,
+  virtualenv  => '/var/www/project1',
+  mode        => 'wsgi',
+  dir         => '/var/www/project1/current',
+  bind        => 'unix:/tmp/gunicorn.socket',
+  environment => 'prod',
+  template    => 'python/gunicorn.erb',
+}
+class { 'python':
+  version    => 'system',
+  dev        => true,
+  virtualenv => true,
+}
+class { 'python':
+  version    => 'system',
+  dev        => true,
+  virtualenv => true,
+}
+
+python::pip { 'flask':
+  virtualenv => '/var/www/project1',
+  proxy      => 'http://proxy.domain.com:3128',
+}

tests/requirements.pp

+class { 'python':
+  version    => 'system',
+  dev        => true,
+  virtualenv => true,
+}
+
+python::requirements { '/var/www/project1/requirements.txt':
+  virtualenv => '/var/www/project1',
+  proxy      => 'http://proxy.domain.com:3128',
+}
+
+python::requirements { '/var/www/project1/requirements.txt':
+  virtualenv => 'system',
+  proxy      => 'http://proxy.domain.com:3128',
+}

tests/virtualenv.pp

+class { 'python':
+  version    => 'system',
+  dev        => true,
+  virtualenv => true,
+}
+
+python::virtualenv { '/var/www/project1':
+  ensure       => present,
+  version      => 'system',
+  requirements => '/var/www/project1/requirements.txt',
+  proxy        => 'http://proxy.domain.com:3128',
+  systempkgs   => true,
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.