From 37863d09c8e325e000d4ad6965676735acc395ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Fr=C3=B6hner?= Date: Tue, 17 Nov 2020 08:23:04 +0100 Subject: [PATCH] fix puppet code style/lint/indent --- manifests/config.pp | 2 +- manifests/dotfile.pp | 2 +- manifests/gunicorn.pp | 1 + manifests/init.pp | 10 ++---- manifests/install.pp | 57 +++++++++++++++++------------- manifests/params.pp | 53 ++++++++++++++-------------- manifests/pip.pp | 71 +++++++++++++++++++++----------------- manifests/pip/bootstrap.pp | 8 +++-- manifests/pyvenv.pp | 17 ++++----- manifests/requirements.pp | 5 +-- manifests/virtualenv.pp | 33 +++++++++--------- 11 files changed, 138 insertions(+), 121 deletions(-) diff --git a/manifests/config.pp b/manifests/config.pp index cbb466f1..dfff33b6 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -12,7 +12,7 @@ Python::Virtualenv <| |> -> Python::Pip <| |> if $python::manage_gunicorn { - if $python::gunicorn != 'absent' { + unless $python::gunicorn == 'absent' { Class['python::install'] -> Python::Gunicorn <| |> Python::Gunicorn <| |> ~> Service['gunicorn'] diff --git a/manifests/dotfile.pp b/manifests/dotfile.pp index 7fc0e7c3..c33bfa35 100644 --- a/manifests/dotfile.pp +++ b/manifests/dotfile.pp @@ -34,7 +34,7 @@ exec { "create ${title}'s parent dir": command => "install -o ${owner} -g ${group} -d ${parent_dir}", - path => ['/usr/bin', '/bin', '/usr/local/bin',], + path => [ '/usr/bin', '/bin', '/usr/local/bin', ], creates => $parent_dir, } diff --git a/manifests/gunicorn.pp b/manifests/gunicorn.pp index 4a817163..1b2954be 100644 --- a/manifests/gunicorn.pp +++ b/manifests/gunicorn.pp @@ -62,6 +62,7 @@ owner => 'root', group => 'root', } + file { "${config_dir}/${name}": ensure => $ensure, mode => '0644', diff --git a/manifests/init.pp b/manifests/init.pp index 7642eae5..b39f4fd5 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -66,24 +66,18 @@ default => '', } - # Module compatibility check - $compatible = ['Debian', 'RedHat', 'Suse', 'Gentoo', 'AIX'] - if ! ($facts['os']['family'] in $compatible) { - fail("Module is not compatible with ${facts['os']['name']}") - } - contain python::install contain python::config Class['python::install'] -> Class['python::config'] - # Set default umask. + ## Set default umask. if $umask != undef { Exec { umask => $umask } } - # Allow hiera configuration of python resources + ## Allow hiera configuration of python resources create_resources('python::pip', $python_pips) create_resources('python::pyvenv', $python_pyvenvs) create_resources('python::virtualenv', $python_virtualenvs) diff --git a/manifests/install.pp b/manifests/install.pp index fd063cdf..6641532b 100644 --- a/manifests/install.pp +++ b/manifests/install.pp @@ -36,9 +36,10 @@ if $venv_ensure == 'present' { $dev_ensure = 'present' + unless $python::dev { - # Error: python2-devel is needed by (installed) python-virtualenv-15.1.0-2.el7.noarch - # Python dev is required for virtual environment, but python environment is not required for python dev. + ## Error: python2-devel is needed by (installed) python-virtualenv-15.1.0-2.el7.noarch + ## Python dev is required for virtual environment, but python environment is not required for python dev. notify { 'Python virtual environment is dependent on python dev': } } } else { @@ -80,10 +81,10 @@ } } - # Respect the $pip_ensure setting + ## Respect the $pip_ensure setting unless $pip_ensure == 'absent' { - # Install pip without pip, see https://pip.pypa.io/en/stable/installing/. - include 'python::pip::bootstrap' + ## Install pip without pip, see https://pip.pypa.io/en/stable/installing/. + include python::pip::bootstrap Exec['bootstrap pip'] -> File['pip-python'] -> Package <| provider == pip |> @@ -91,6 +92,7 @@ name => 'pip', provider => 'pip', } + if $pythondev { Package <| title == 'virtualenv' |> { name => 'virtualenv', @@ -106,9 +108,9 @@ } } 'scl': { - # SCL is only valid in the RedHat family. If RHEL, package must be - # enabled using the subscription manager outside of puppet. If CentOS, - # the centos-release-SCL will install the repository. + ## SCL is only valid in the RedHat family. If RHEL, package must be + ## enabled using the subscription manager outside of puppet. If CentOS, + ## the centos-release-SCL will install the repository. if $python::manage_scl { $install_scl_repo_package = $facts['os']['name'] ? { 'CentOS' => 'present', @@ -119,6 +121,7 @@ ensure => $install_scl_repo_package, before => Package['scl-utils'], } + package { 'scl-utils': ensure => 'present', before => Package['python'], @@ -131,14 +134,10 @@ } } - # This gets installed as a dependency anyway - # package { "${python::version}-python-virtualenv": - # ensure => $venv_ensure, - # require => Package['scl-utils'], - # } package { "${python}-scldevel": ensure => $dev_ensure, } + if $pip_ensure != 'absent' { exec { 'python-scl-pip-install': command => "${python::exec_prefix}easy_install pip", @@ -148,9 +147,10 @@ } } 'rhscl': { - # rhscl is RedHat SCLs from softwarecollections.org + ## rhscl is RedHat SCLs from softwarecollections.org if $python::rhscl_use_public_repository { $scl_package = "rhscl-${python::version}-epel-${facts['os']['release']['major']}-${facts['os']['architecture']}" + package { $scl_package: source => "https://www.softwarecollections.org/en/scls/rhscl/${python::version}/epel-${facts['os']['release']['major']}-${facts['os']['architecture']}/download/${scl_package}.noarch.rpm", provider => 'rpm', @@ -181,8 +181,7 @@ -> Package <| tag == 'python-scl-package' |> } - Package <| tag == 'python-scl-package' |> - -> Package <| tag == 'python-pip-package' |> + Package <| tag == 'python-scl-package' |> -> Package <| tag == 'python-pip-package' |> } 'anaconda': { $installer_path = '/var/tmp/anaconda_installer.sh' @@ -217,6 +216,7 @@ } } } + if $pythondev { package { 'python-dev': ensure => $dev_ensure, @@ -233,6 +233,7 @@ require => Package['python'], } } + if $pythondev { package { 'python-dev': ensure => $dev_ensure, @@ -247,14 +248,22 @@ 'RedHat': { if $pip_ensure != 'absent' { if $python::use_epel == true { - include 'epel' - if $python::manage_pip_package { Class['epel'] -> Package['pip'] } - if $python::manage_python_package { Class['epel'] -> Package['python'] } + include epel + + if $python::manage_pip_package { + Class['epel'] -> Package['pip'] + } + + if $python::manage_python_package { + Class['epel'] -> Package['python'] + } } } + if ($venv_ensure != 'absent') and ($facts['os']['release']['full'] =~ /^6/) { if $python::use_epel == true { - include 'epel' + include epel + Class['epel'] -> Package['virtualenv'] } } @@ -278,19 +287,19 @@ if String($python::version) =~ /^python3/ { $pip_category = undef - $pip_package = "${python}-pip" + $pip_package = "${python}-pip" $pip_provider = $python.regsubst(/^.*python3\.?/,'pip3.').regsubst(/\.$/,'') } elsif ($facts['os']['family'] == 'RedHat') and (versioncmp($facts['os']['release']['major'], '7') >= 0) { $pip_category = undef - $pip_package = 'python2-pip' + $pip_package = 'python2-pip' $pip_provider = pip2 } elsif $facts['os']['family'] == 'Gentoo' { $pip_category = 'dev-python' - $pip_package = 'pip' + $pip_package = 'pip' $pip_provider = 'pip' } else { $pip_category = undef - $pip_package = 'python-pip' + $pip_package = 'python-pip' $pip_provider = 'pip' } diff --git a/manifests/params.pp b/manifests/params.pp index 8ff6a669..57a24dfc 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -4,28 +4,32 @@ # The python Module default configuration settings. # class python::params { - $ensure = 'present' - $version = 'system' - $pip = 'present' - $dev = 'absent' - $virtualenv = 'absent' - $gunicorn = 'absent' - $manage_gunicorn = true - $manage_python_package = true - $manage_virtualenv_package = true - $manage_pip_package = true - $provider = undef - $valid_versions = undef - $manage_scl = true + ## Module compatibility check + unless $facts['os']['family'] in [ 'Debian', 'RedHat', 'Suse', 'Gentoo', 'AIX', ] { + fail("Module is not compatible with ${facts['os']['name']}") + } + + $ensure = 'present' + $version = 'system' + $pip = 'present' + $dev = 'absent' + $virtualenv = 'absent' + $gunicorn = 'absent' + $manage_gunicorn = true + $manage_python_package = true + $manage_virtualenv_package = true + $manage_pip_package = true + $provider = undef + $valid_versions = undef + $manage_scl = true + $rhscl_use_public_repository = true + $anaconda_installer_url = 'https://repo.anaconda.com/archive/Anaconda3-5.2.0-Linux-x86_64.sh' + $anaconda_install_path = '/opt/python' - if $facts['os']['family'] == 'RedHat' { - if $facts['os']['name'] != 'Fedora' { - $use_epel = true - } else { - $use_epel = false - } + if $facts['os']['family'] == 'RedHat' and $facts['os']['name'] != 'Fedora' { + $use_epel = true } else { - $use_epel = false + $use_epel = false } $group = $facts['os']['family'] ? { @@ -34,17 +38,12 @@ } $pip_lookup_path = $facts['os']['family'] ? { - 'AIX' => ['/bin', '/usr/bin', '/usr/local/bin', '/opt/freeware/bin/'], - default => ['/bin', '/usr/bin', '/usr/local/bin'] + 'AIX' => [ '/bin', '/usr/bin', '/usr/local/bin', '/opt/freeware/bin/', ], + default => [ '/bin', '/usr/bin', '/usr/local/bin', ] } $gunicorn_package_name = $facts['os']['family'] ? { 'RedHat' => 'python-gunicorn', default => 'gunicorn', } - - $rhscl_use_public_repository = true - - $anaconda_installer_url = 'https://repo.anaconda.com/archive/Anaconda3-5.2.0-Linux-x86_64.sh' - $anaconda_install_path = '/opt/python' } diff --git a/manifests/pip.pp b/manifests/pip.pp index f5112b92..96624ee5 100644 --- a/manifests/pip.pp +++ b/manifests/pip.pp @@ -77,8 +77,8 @@ Python::Virtualenv <| |> -> Python::Pip[$name] } - # Get SCL exec prefix - # NB: this will not work if you are running puppet from scl enabled shell + ## 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} -- ", @@ -90,7 +90,7 @@ default => $path, } - # Parameter validation + ## Parameter validation if $virtualenv == 'system' and $owner != 'root' { fail('python::pip: root user must be used when virtualenv is system') } @@ -127,7 +127,7 @@ $install_editable = '' } - #TODO: Do more robust argument checking, but below is a start + ## 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') } @@ -137,18 +137,19 @@ } if $pkgname =~ /==/ { - $parts = split($pkgname, '==') + $parts = split($pkgname, '==') $real_pkgname = $parts[0] + $_ensure = $ensure ? { 'absent' => 'absent', default => $parts[1], } } else { $real_pkgname = $pkgname - $_ensure = $ensure + $_ensure = $ensure } - # Check if searching by explicit version. + ## Check if searching by explicit version. if $_ensure =~ /^((19|20)[0-9][0-9]-(0[1-9]|1[1-2])-([0-2][1-9]|3[0-1])|[0-9]+\.\w+\+?\w*(\.\w+)*)$/ { $grep_regex = "^${real_pkgname}[[:space:]]\\+(\\?${_ensure}\\()$\\|$\\|, \\|[[:space:]]\\)" } else { @@ -166,61 +167,69 @@ } $source = $url ? { - false => "${real_pkgname}${extras_string}", - /^(\/|[a-zA-Z]\:)/ => "'${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}'", + default => "'${url}#egg=${egg_name}'", } - $pip_install = "${pip_env} --log ${log}/pip.log install" + $pip_install = "${pip_env} --log ${log}/pip.log install" $pip_common_args = "${pypi_index} ${proxy_flag} ${install_args} ${install_editable} ${source}" - # Explicit version out of VCS when PIP supported URL is provided + ## 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}" + $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}" + $command = "${pip_install} ${install_args} ${pip_common_args}" $unless_command = "${pip_env} list | grep -i -e '${grep_regex}'" } } else { case $_ensure { /^((19|20)[0-9][0-9]-(0[1-9]|1[1-2])-([0-2][1-9]|3[0-1])|[0-9]+\.\w+\+?\w*(\.\w+)*)$/: { - # Version formats as per http://guide.python-distribute.org/specification.html#standard-versioning-schemes - # Explicit version. - $command = "${pip_install} ${install_args} ${pip_common_args}==${_ensure}" + ## Version formats as per http://guide.python-distribute.org/specification.html#standard-versioning-schemes + ## Explicit 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} ${pip_common_args}" + ## Whatever version is available. + $command = "${pip_install} ${pip_common_args}" $unless_command = "${pip_env} list | grep -i -e '${grep_regex}'" } 'latest': { - # 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} ${pypi_index} ${proxy_flag} ${install_args} ${install_editable} ${real_pkgname}==notreallyaversion 2>&1", + ## 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} ${pypi_index} ${proxy_flag} ${install_args} ${install_editable} ${real_pkgname}==notreallyaversion 2>&1", ' | grep -oP "\(from versions: .*\)" | sed -E "s/\(from versions: (.*?, )*(.*)\)/\2/g"', - ' | tr -d "[:space:]"']) + ' | 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') + ## 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", + $installed_version = join( + [ + "${pip_env} freeze --all", " | grep -i -e ${grep_regex_pkgname_with_dashes} | cut -d= -f3", - " | tr -d '[:space:]'"]) + " | tr -d '[:space:]'", + ] + ) - $command = "${pip_install} --upgrade ${pip_common_args}" + $command = "${pip_install} --upgrade ${pip_common_args}" $unless_command = "[ \$(${latest_version}) = \$(${installed_version}) ]" } default: { - # Anti-action, uninstall. - $command = "echo y | ${pip_env} uninstall ${uninstall_args} ${proxy_flag} ${name}" + ## Anti-action, uninstall. + $command = "echo y | ${pip_env} uninstall ${uninstall_args} ${proxy_flag} ${name}" $unless_command = "! ${pip_env} list | grep -i -e '${grep_regex}'" } } diff --git a/manifests/pip/bootstrap.pp b/manifests/pip/bootstrap.pp index 19848e63..720b560d 100644 --- a/manifests/pip/bootstrap.pp +++ b/manifests/pip/bootstrap.pp @@ -1,4 +1,3 @@ -# # @summary allow to bootstrap pip when python is managed from other module # # @param version should be pip or pip3 @@ -9,6 +8,7 @@ # class { 'python::pip::bootstrap': # version => 'pip', # } +# class python::pip::bootstrap ( Enum['pip', 'pip3'] $version = 'pip', Variant[Boolean, String] $manage_python = false, @@ -40,7 +40,8 @@ require => Package['python3'], provider => $exec_provider, } - # puppet is opinionated about the pip command name + + ## puppet is opinionated about the pip command name file { 'pip3-python': ensure => link, path => '/usr/bin/pip3', @@ -56,7 +57,8 @@ require => Package['python'], provider => $exec_provider, } - # puppet is opinionated about the pip command name + + ## puppet is opinionated about the pip command name file { 'pip-python': ensure => link, path => '/usr/bin/pip', diff --git a/manifests/pyvenv.pp b/manifests/pyvenv.pp index 06148e20..9e1a363f 100644 --- a/manifests/pyvenv.pp +++ b/manifests/pyvenv.pp @@ -40,14 +40,15 @@ default => $version, } - $python_version_parts = split($python_version, '[.]') + $python_version_parts = split($python_version, '[.]') $normalized_python_version = sprintf('%s.%s', $python_version_parts[0], $python_version_parts[1]) - # Debian splits the venv module into a seperate package + ## Debian splits the venv module into a seperate package if ( $facts['os']['family'] == 'Debian') { - $python3_venv_package="python${normalized_python_version}-venv" + $python3_venv_package = "python${normalized_python_version}-venv" + case $facts['os']['distro']['codename'] { - 'xenial','bionic','cosmic','disco','stretch','buster': { + 'xenial', 'bionic', 'cosmic', 'disco', 'stretch', 'buster': { ensure_packages ($python3_venv_package) Package[$python3_venv_package] -> File[$venv_dir] } @@ -55,8 +56,8 @@ } } - # pyvenv is deprecated since 3.6 and will be removed in 3.8 - if (versioncmp($normalized_python_version, '3.6') >=0) { + ## pyvenv is deprecated since 3.6 and will be removed in 3.8 + if versioncmp($normalized_python_version, '3.6') >=0 { $virtualenv_cmd = "${python::exec_prefix}python${normalized_python_version} -m venv" } else { $virtualenv_cmd = "${python::exec_prefix}pyvenv-${normalized_python_version}" @@ -67,7 +68,7 @@ default => $path, } - if ( $systempkgs == true ) { + if $systempkgs == true { $system_pkgs_flag = '--system-site-packages' } else { $system_pkgs_flag = '' @@ -80,7 +81,7 @@ mode => $mode, } - $pip_cmd = "${python::exec_prefix}${venv_dir}/bin/pip" + $pip_cmd = "${python::exec_prefix}${venv_dir}/bin/pip" exec { "python_virtualenv_${venv_dir}": command => "${virtualenv_cmd} --clear ${system_pkgs_flag} ${venv_dir} && ${pip_cmd} --log ${venv_dir}/pip.log install --upgrade pip && ${pip_cmd} --log ${venv_dir}/pip.log install --upgrade setuptools", diff --git a/manifests/requirements.pp b/manifests/requirements.pp index e448f983..4ddd5241 100644 --- a/manifests/requirements.pp +++ b/manifests/requirements.pp @@ -76,8 +76,8 @@ default => "--src=${src}", } - # This will ensure multiple python::virtualenv definitions can share the - # the same requirements file. + ## 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, @@ -88,6 +88,7 @@ replace => false, content => '# Puppet will install and/or update pip packages listed here', } + $local_subscribe = File[$requirements] } else { $local_subscribe = undef diff --git a/manifests/virtualenv.pp b/manifests/virtualenv.pp index e098b366..4c908fba 100644 --- a/manifests/virtualenv.pp +++ b/manifests/virtualenv.pp @@ -52,8 +52,9 @@ $virtualenv = undef, ) { include python + $python_provider = getparam(Class['python'], 'provider') - $anaconda_path = getparam(Class['python'], 'anaconda_install_path') + $anaconda_path = getparam(Class['python'], 'anaconda_install_path') if $ensure == 'present' { $python = $version ? { @@ -86,17 +87,18 @@ } } - # 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 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 )) { + + 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 )) { + } elsif versioncmp($_virtualenv_version,'1.7') < 0 and $systempkgs == false { $system_pkgs_flag = '--no-site-packages' } else { $system_pkgs_flag = $systempkgs ? { @@ -115,12 +117,12 @@ 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 + ## 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: @@ -132,9 +134,8 @@ } $virtualenv_cmd = "${python::exec_prefix}${used_virtualenv}" - - $pip_cmd = "${python::exec_prefix}${venv_dir}/bin/pip" - $pip_flags = "${pypi_index} ${proxy_flag} ${pip_args}" + $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}",