如何处理人偶模块中的代码重复? [英] How to deal with code duplication in puppet modules?

查看:113
本文介绍了如何处理人偶模块中的代码重复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了两个人偶模块来设置和配置两个组件-A& B. modules文件夹的目录结构如下:

modules
     modulea
          manifests
               init.pp
               install.pp
               config.pp
               service.pp
          templates
               1.properties.erb (This one is also needed by moduleb)
               2.properties.erb (This one is also needed by moduleb)
     moduleb
          manifests          
               init.pp
               install.pp
               config.pp
               service.pp 
          templates
               1.properties.erb (same as in modulea)
               2.properties.erb (same as in modulea) 
               3.properties.erb
               4.properties.erb
               5.properties.erb

两个模块中install.pp和service.pp中的代码相同. modulea的config.pp中的代码处理2个属性文件,而moduleb的config.pp中的代码处理5个属性文件.在这5个属性文件中,有2个属性文件与modulea完全相同,而3个属性文件特定于moduleb.

下面粘贴的是install.pp和service.pp代码:

**init.pp of modulea**
class modulea::install inherits modulea {
  package { $package_name: ensure => present }
}

**init.pp of moduleb**
class moduleb::install inherits moduleb {
  package { $package_name: ensure => present }
}

**service.pp of modulea**
class modulea::service inherits modulea {
  service { $service_name: ensure => running, }
}

**service.pp of moduleb**
class moduleb::service inherits moduleb {
  service { $service_name: ensure => running, }
}

我必须采取哪些措施来减少代码重复并提高代码的可维护性?我有什么选择,以便将来可以轻松添加一个具有相同模式的模块?

解决方案

为避免人偶代码重复,我通常创建公共类". 我还建议使用hiera参数化普通类. 有关hiera的更多信息: https://docs.puppetlabs.com/hiera/1/

例如: 类common :: installer:

modules
    common
        installer.pp

包含代码:

class common::installer {
   $packages = hiera("common::installer::packages")
   package { $packages:
     ensure => latest,
   }
}

接下来在所有模块中添加

require common::installer #or include common::installer, with this you will avoid "already defined" errors in puppet. 

在hiera中,根据计算机配置,您可以定义common :: installer :: packages的适当值

例如 在machineA.yaml中

 common::installer::packages:
   - packageA
   - packageB
   - packageC

在machineB.yaml

 common::installer::packages:
   - packageA
   - packageB
   - packageD
   - packageE

这也使您有机会在一台计算机上安装模块A和B,而不会发生任何冲突.

只需在hiera中使用创建正确的配置machineAandB.yaml

 common::installer::packages:
   - packageA
   - packageB
   - packageC
   - packageD
   - packageE

您也可以考虑使用sure_package,以避免由于重复定义包而引起的烦人的木偶错误,但是上面的代码不会发生.

您可以使用类似的代码来确保所需的服务正在运行.

I have implemented two puppet modules to setup and configure two components - A & B. The directory structure of the modules folder looks like below:

modules
     modulea
          manifests
               init.pp
               install.pp
               config.pp
               service.pp
          templates
               1.properties.erb (This one is also needed by moduleb)
               2.properties.erb (This one is also needed by moduleb)
     moduleb
          manifests          
               init.pp
               install.pp
               config.pp
               service.pp 
          templates
               1.properties.erb (same as in modulea)
               2.properties.erb (same as in modulea) 
               3.properties.erb
               4.properties.erb
               5.properties.erb

The code in install.pp and service.pp is identical in both the modules. The code in config.pp of modulea deals with 2 property files, while code in config.pp of moduleb deals with 5 property files. Out of these 5 property files, 2 property files are exactly same as modulea, while 3 property files are specific to moduleb.

Pasted below is install.pp and service.pp code:

**init.pp of modulea**
class modulea::install inherits modulea {
  package { $package_name: ensure => present }
}

**init.pp of moduleb**
class moduleb::install inherits moduleb {
  package { $package_name: ensure => present }
}

**service.pp of modulea**
class modulea::service inherits modulea {
  service { $service_name: ensure => running, }
}

**service.pp of moduleb**
class moduleb::service inherits moduleb {
  service { $service_name: ensure => running, }
}

What options do I have to reduce code duplication and improve the maintainability of code? What options do I have so that I can easily add one more module in future that has same pattern?

解决方案

To avoid puppet code duplication, I usually create "common class". I also recommend using hiera to parametrize common class. More about hiera: https://docs.puppetlabs.com/hiera/1/

As an example: class common::installer:

modules
    common
        installer.pp

with code:

class common::installer {
   $packages = hiera("common::installer::packages")
   package { $packages:
     ensure => latest,
   }
}

Next in all modules add

require common::installer #or include common::installer, with this you will avoid "already defined" errors in puppet. 

In hiera, depend on machine configuration you define proper value of common::installer::packages

e.g in machineA.yaml

 common::installer::packages:
   - packageA
   - packageB
   - packageC

in machineB.yaml

 common::installer::packages:
   - packageA
   - packageB
   - packageD
   - packageE

This also gives you opportunity to install both modules A and B on one machine without any conflicts.

Just in hiera create proper config machineAandB.yaml with

 common::installer::packages:
   - packageA
   - packageB
   - packageC
   - packageD
   - packageE

You can also consider using ensure_package, to avoid annoying puppet errors with duplicated definition of packages, but with the code above i will not happen.

Similar code you can use to ensure that required services are running.

这篇关于如何处理人偶模块中的代码重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆