Puppet 简明教程
Puppet - Type and Provider
Puppet 类型用于单个配置管理。Puppet 有不同的类型,如服务类型、程序包类型、提供程序类型等。其中,每一种类型都有提供程序。提供程序处理不同平台或工具上的配置。例如,package 类型具有 aptitude、yum、rpm 和 DGM 提供程序。有许多类型,Puppet 涵盖了需要管理的良好频谱配置管理项。
Puppet types are used for individual configuration management. Puppet has different types like a service type, package type, provider type, etc. Wherein each type has providers. The provider handles the configuration on different platforms or tools. For example, the package type has aptitude, yum, rpm, and DGM providers. There are a lot of types and Puppet covers a good spectrum configuration management item that needs to be managed.
Puppet 使用 Ruby 作为其基本语言。所有现有的 Puppet 类型和提供程序都是使用 Ruby 语言编写的。因为它遵循标准编码格式,所以可以简单地创建它们,如仓库中管理存储库的示例所示。在这里,我们将创建类型 repo 和提供程序 svn 和 git。repo 类型的第一个部分是类型本身。这些类型通常存储在 lib/puppet/type 中。为此,我们将创建一个名为 repo.rb 文件。
Puppet uses Ruby as its base language. All Puppet types and providers present are written in Ruby language. As it follows the standard encoding format, one can simply create them as shown in the example for repo which manages repositories. Here, we will create type repo and providers’ svn and git. The first part of the repo type is type itself. The types are usually stored in lib/puppet/type. For this, we will create a file called repo.rb.
$ touch repo.rb
在文件中添加以下内容。
Add the following content in the file.
Puppet::Type.newtype(:repo) do
@doc = "Manage repos"
Ensurable
newparam(:source) do
desc "The repo source"
validate do |value|
if value =~ /^git/
resource[:provider] = :git
else
resource[:provider] = :svn
end
end
isnamevar
end
newparam(:path) do
desc "Destination path"
validate do |value|
unless value =~ /^\/[a-z0-9]+/
raise ArgumentError , "%s is not a valid file path" % value
end
end
end
end
在上面的脚本中,我们创建了一个块“ Puppet::Type.newtype(:repo) do ”,它创建了一个名称为 repo 的新类型。然后,我们有 @doc,它有助于添加想要添加的任何级别的详细信息。下一个语句是 Ensurable;它创建了一个基本的 ensure 属性。Puppet 类型使用 ensure 属性来确定配置项的状态。
In the above script, we have created a block "Puppet::Type.newtype(:repo) do" which creates a new type with the name repo. Then, we have @doc which helps in adding whatever level of details one wants to add. The next statement is Ensurable; it creates a basic ensure property. Puppet type uses ensure property to determine the state of configuration item.
Example
service { "sshd":
ensure => present,
}
ensure 语句告诉 Puppet 除了在提供程序中创建、销毁和存在。这些方法提供以下功能 −
The ensure statement tells Puppet to except three method: create, destroy, and exist in the provider. These methods provide the following features −
-
A command to create a resource
-
A command to delete a resource
-
A command to check the existence of a resource
我们随后需要做的,就是指定这些方法及其内容。Puppet 会在其周围创建支持性的基础设施。
All we then need to do is specify these methods and their contents. Puppet creates the supporting infrastructure around them.
接下来,我们定义一个名为源(source)的新参数。
Next, we define a new parameter called source.
newparam(:source) do
desc "The repo source"
validate do |value|
if value =~ /^git/
resource[:provider] = :git
else
resource[:provider] = :svn
end
end
isnamevar
end
源将告诉存储库类型从何处检索/克隆/检出源存储库。在此,我们还使用了名为验证(validate)的挂钩。在提供程序部分中,我们已经定义了 git 和 svn,它们用于检查我们已经定义的存储库的有效性。
The source will tell the repo type where to retrieve/clone/checkout the source repository. In this, we are also using a hook called validate. In the provider section, we have defined git and svn which check for the validity of the repository we have defined.
最后,在代码中我们已经定义了另一个称为路径(path)的参数。
Finally, in the code we have defined one more parameter called path.
newparam(:path) do
desc "Destination path"
validate do |value|
unless value =~ /^\/[a-z0-9]+/
raise ArgumentError , "%s is not a valid file path" % value
end
这是值类型,它指定将检索到的新代码放置在何处。在这里,再次使用验证(validate)挂钩来创建一个检查适用性的值块。
This is the value type which specifies where to put the new code that is retrieved. Here, again use the validate hook to create a block that checks the value of appropriateness.
Subversion Provider Use Case
我们使用上面创建的类型从 subversion 提供程序开始。
Let’s start with the subversion provider using the above created type.
require 'fileutils'
Puppet::Type.type(:repo).provide(:svn) do
desc "SVN Support"
commands :svncmd => "svn"
commands :svnadmin => "svnadmin"
def create
svncmd "checkout", resource[:name], resource[:path]
end
def destroy
FileUtils.rm_rf resource[:path]
end
def exists?
File.directory? resource[:path]
end
end
在上面的代码中,我们已经预先定义了我们需要库 fileutils ,需要方法 'fileutils' 。
In the above code, we have upfront defined that we need fileutils library, require 'fileutils' which we are going to use method from.
接下来,我们已经将提供程序定义为块 Puppet::Type.type(:repo).provide(:svn) do,它告诉 Puppet 这是称为 repo 的类型的提供程序。
Next, we have defined the provider as block Puppet::Type.type(:repo).provide(:svn) do which tells Puppet that this is the provider for type called repo.
然后,我们已经添加了 desc ,它允许向提供程序添加一些文档。我们还已经定义了该提供程序将使用哪些命令。在下一行中,我们正在检查资源的功能,例如创建、删除和是否存在。
Then, we have added desc which allows to add some documentation to the provider. We have also defined the command that this provider will use. In the next line, we are checking the features of resource like create, delete, and exist.
Creating a Resource
一旦完成了上述所有操作,我们将创建一个将在我们的类和清单文件中(如下面的代码所示)使用的资源。
Once all the above is done, we will create a resource which will be used in our classes and manifest files as shown in the following code.
repo { "wp":
source => "http://g01063908.git.brcl.org/trunk/",
path => "/var/www/wp",
ensure => present,
}