简体   繁体   English

重构 ensure_packages 以切换已安装包的版本

[英]refactor ensure_packages to switch version of installed packages

I am successfully installing several PHP modules by version with puppet on Debian linux like this:我在 Debian linux 上按版本成功安装了几个 PHP 模块,如下所示:

  $php_version = '7.3'

  ensure_packages([
    "php$php_version-xml",
    "php$php_version-zip",
    "php$php_version-curl",
    "php$php_version-mbstring",
    "libapache2-mod-php$php_version",
  ],
    {
      'ensure' => 'present',
    }
  )

now I want to prepare for an update from PHP 7.3 to 7.4.现在我想准备从 PHP 7.3 到 7.4 的更新。 This basically works, but the 7.3 packages stay installed.这基本上有效,但 7.3 软件包保持安装状态。 I would like to adapt the code to remove the old packages.我想修改代码以删除旧包。 I am looking for a way to reuse the list of packages of modules for uninstalling.我正在寻找一种方法来重用模块包列表进行卸载。

I am thinking of a signature like this我在想这样的签名

class profile::software::apache (
  $php_version    = '7.4',
  $php_remove     = ['7.0‘, ‘7.3'],
  #...
) {

$myPackages = [
    "php$php_version-xml",
    "php$php_version-zip",
    "php$php_version-curl",
    "php$php_version-mbstring",
    "libapache2-mod-php$php_version",
  ]

ensure_packages($myPackages,
    {
      'ensure' => 'present',
    }
  )

  $php_remove.each | String $php_version | { 
    ensure_packages($myPackages,
      {
        'ensure' => 'absent',
      }
    )
   }
}

Is there a way to solve this?有办法解决这个问题吗?

thx谢谢

I was able to solve this by using iteration functions of puppet.我能够通过使用木偶的迭代函数来解决这个问题。

From the two parameters I build a hash with keys of versions to work on and values to either install or remove the given version.根据这两个参数,我构建了一个 hash,其中包含要处理的版本键和用于安装或删除给定版本的值。 Now I can iterate over this hash with each, reusing the structure:现在我可以对每个 hash 进行迭代,重用该结构:

class profile::software::apache (
  $php_version    = '7.4',
  $php_remove     = ['7.0‘, ‘7.3'],
  #...
) {
  # build a hash of PHP Versions with a value of either present or absent
  # and iterate over it with each
  $phpInstallHash = { $php_version => 'present' }
  #notify { "Value of phpRemove: ${php_remove}": }
  if $php_remove {
    # We have the array, use the map function to build remove hash
    $phpRemoveHash = $php_remove.map |$version| {
      { $version => 'absent' }
    }
    $phpFullHash = $phpInstallHash + $phpRemoveHash
  } else {
    $phpFullHash = $phpInstallHash
  }
  #notify { "phpHash to iterate over to install/uninstall: ${phpFullHash}": }

  #iterate over the result installing/uninstalling
  $phpFullHash.each | $php_version, $ensure_value | {
    ensure_packages([
      "php$php_version-xml",
      "php$php_version-zip",
      "php$php_version-curl",
      "php$php_version-mbstring",
      "libapache2-mod-php$php_version",
    ],
      {
        'ensure' => $ensure_value,
        require  => [Class['apt::update'],
          Apt::Source['source_php_sury'],
        ],
        notify   => Class['apache::service'],
      }
    )

  }
}

hth hth

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM