Layman 覆盖和 Puppet

Layman 覆盖和 Puppet

我们开始使用 Puppet 来管理各个服务器的配置。我们有一个 Portage 存储库,里面有一些我们自己的软件包,每台机器都使用 Layman 来管理覆盖层。

它是安装软件包非常简单在 puppet 中,但是我们应该如何确保 Layman 已配置?那里有模块吗?

答案1

我不知道 gentoo、portage 或 layman 的具体细节,而且我没有看到任何现有的模块Puppet 模块锻造但从快速浏览来看一些 gentoo 入门文档看起来使用 Puppet 自己编写它会相当简单:

stage { "first": before => Stage[main] } # Set up first stage, before main

class layman { # "overlays"?
  package { "layman": ensure => present }
  # Then everything else (file, execs, whatever) to configure layman,
  # overlays, etc
  # Looks to me like you need to change /etc/make.conf, /etc/layman/layman.cfg
  # and write some execs that run "layman -a <overlay-name>"
  # depending on output of "layman -i <overlay-name>"
  # or possibly grepping /var/lib/layman/overlays.xmls
}

class{"layman": stage => "first"} # Set layman class to run in the first stage

require => Class[layman]您可以使用所有需要它的语句,而不是使用阶段package。使用 require 更冗长;如果我只需要几件事,或者我需要特定的覆盖,我会使用它。但我认为您通常应该避免使用跨阶段边界的 require,因为它既多余又可能令人不快奇怪的虫子

根据您的需要,Alternate 可避免分阶段,并且仅执行显式要求排序。我使用 RHEL 和 yum repos 执行类似操作:

# In a "layman" module.
class layman {
  [...]
}

define layman::overlay() {
  exec {
    "layman -a $name":
      require => Class[layman],
      creates => "/var/lib/layman/${name}",
  }
}

class layman::overlay::php {
  layman::overlay { "php": }
}

class layman::overlay::apache2 {
  layman::overlay { "apache2": }
}

class apache {
  include layman::overlay::apache2
  package { "apache2":
    ensure => present,
    require => Class[layman::overlay::apache2];
  }
  file { "/etc/apache2/conf.d/whatever.conf":
    source => "...",
    require => Package[apache2],
    notify => Service[apache2];
  }
  service { "apache2":
    ensure => running,
    enable => true,
    require => [ Package[apache2], File["/etc/apache2/conf.d/whatever.conf"] ];
  }

}

# "yoursite" module or "somephpapp" module
class yoursite::somephpapp {
  include apache
  include layman::overlay::php
  package { "somephpapp":
    ensure => present,
    require => [ Class[apache], Class[layman::overlay::php] ];
  }
  file {
    "/path/to/somephpapp.conf":
      source => "...",
      require => Package[somephpapp],
      notify => Service[apache2]; # probably not actually required, example
  }
}

答案2

根据 freiheit 的回答,以下是我最终得到的结论。

class packages-layman {
    Exec { path => '/usr/bin:/bin:/usr/sbin:/sbin', loglevel => 'debug' }

    package { 'app-portage/layman': ensure => 'installed' }

    file { '/etc/eix-sync.conf':
        ensure => present,
        content => '*',
    }

    line { 'layman-make.conf-overlay':
        file => '/etc/make.conf',
        line => 'source /var/lib/layman/make.conf',
    }

    exec { 'layman-list':
        command => 'layman -o "http://dev.mycompany.com" -L',
        require => [
            Package['app-portage/layman'],
            Service['openvpn']
        ],
    }

    exec { 'layman-my-overlay':
        command => 'layman -o "http://dev.mycompany.com" -a myoverlay',
        returns => [0,1],
        require => Exec['layman-list'],
    }

    exec { 'layman-eix-sync':
        command => 'eix-sync',
        require => [
            File['/etc/eix-sync.conf'],
            Line['layman-make.conf-overlay'],
            Exec['layman-my-overlay'],
        ],
    }
}

请注意,'layman-list' exec 是为了解决 Gentoo 上的 layman 版本中似乎存在的一个错误,该错误导致覆盖在它们被列出之前无法工作。

Puppet 可以选择以任意随机顺序运行命令,因此所有条目都会强制执行各种任务的顺序require。要确保任务在此任务之后发生,请使用require如下命令:

package { 'app-misc/my-custom-package':
    ensure => 'installed',
    require => Exec['layman-eix-sync']
}

它需要这个定义 line来自 Puppet wiki 让您编辑更大文件的单行:

define line($file, $line, $ensure = 'present') {
    case $ensure {
        default : { err ( "unknown ensure value ${ensure}" ) }
        present: {
            exec { "/bin/echo '${line}' >> '${file}'":
                unless => "/bin/grep -qFx '${line}' '${file}'"
            }
        }
        absent: {
            exec { "/usr/bin/perl -ni -e 'print unless /^\\Q${line}\\E\$/' '${file}'":
                onlyif => "/bin/grep -qFx '${line}' '${file}'"
            }
        }
    }
}

相关内容