我自己编写的Puppet独立模块,测试执行成功且Agent端多次执行也不会造成多次重启等问题(逻辑依赖及调用顺序均设置好),功能如下:
1)检测系统中是否安装了apache-tomcat-6.0.41.tar.gz包,若没有安装自动安装java依赖及其他依赖包;
2)检测是否存在apache-tomcat-6.0.41.tar.gz安装包文件,若不存在自动分发到Agent端;
3)解压apache-tomcat-6.0.41.tar.gz部署Tomcat6,分发标准的server.xml文件,成功后启动tomcat进程(仅会在安装后启动一次),清理其他临时文件;
4)tomcat的配置发现有更新,自动部署并重启tomcat进程(kill掉tomcat进程重新启动,仅指定的文件更新才会激活重启的动作)。
模块tomcat6目录结构
./files:
apache-tomcat-6.0.41.tar.gz index.html server.xml tomcat_restart.sh
./manifests:
init.pp update.pp
./templates:
1)manifests/init.pp文件内容
class tomcat6::install {
package { "java*":
ensure => installed,
allow_virtual => false,
}
package { "xml-commons-apis":
ensure => installed,
allow_virtual => false,
}
file { "/usr/local/apache-tomcat-6.0.41.tar.gz":
source => "puppet:///modules/tomcat6/apache-tomcat-6.0.41.tar.gz",
require => [Package["java*"],Package["xml-commons-apis"]],
}
Exec {
path => ["/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"],
user => "root",
}
exec { "install tomcat":
cwd => "/usr/local",
creates => "/usr/local/tomcat6",
command => "tar zxf apache-tomcat-6.0.41.tar.gz && mv apache-tomcat-6.0.41 tomcat6 ",
require => File["/usr/local/apache-tomcat-6.0.41.tar.gz"],
}
exec { "clear tmpfile": #本段内容不是必要,因为每次删除apache-tomcat-6.0.41.tar.gz后File资源又会重新分发。这里保留是为了提供格式参考以便做其他清理的动作。
cwd => "/usr/local",
onlyif => "test -f apache-tomcat-6.0.41.tar.gz",
command => "rm -f apache-tomcat-6.0.41.tar.gz",
require => Exec["install tomcat"],
}
}
class tomcat6::config {
file { "/usr/local/tomcat6/conf/server.xml":
owner => "root",
group => "root",
mode => 0600,
source => "puppet:///modules/tomcat6/server.xml",
recurse => "true",
require => Class["tomcat6::install"],
notify => Class["tomcat6::start"],
}
}
class tomcat6::start {
Exec {
cwd => "/usr/local/tomcat6/bin",
user => "root",
}
exec {"first start tomcat":
refreshonly => true,
command => "/usr/local/tomcat6/bin/startup.sh",
require => Class["tomcat6::config"],
}
}
class tomcat6 {
include tomcat6::install,tomcat6::config,tomcat6::start,tomcat6::update,tomcat6::restart
}
保存
2)manifests/update.pp文件内容,用来测试index.html文件更新,若文件有更新自动部署并重启tomcat进程。 class tomcat6::update {
file { "/usr/local/tomcat6/webapps/ROOT/index.html":
owner => "root",
group => "root",
mode => 0644,
source => "puppet://$puppetserver/modules/tomcat6/index.html",
notify => Class["tomcat6::restart"],
}
}
class tomcat6::restart {
file { "/tmp/tomcat_restart.sh":
owner => "root",
group => "root",
mode => 0755,
source => "puppet://$puppetserver/modules/tomcat6/tomcat_restart.sh",
}
Exec {
path => ["/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"],
cwd => "/usr/local/tomcat6/bin",
user => "root",
}
exec {"stop tomcat":
onlyif => "ls /tmp/tomcat_restart.sh",
refreshonly => true,
command => "/tmp/tomcat_restart.sh",
require => File["/tmp/tomcat_restart.sh"],
}
exec {"start tomcat":
command => "/usr/local/tomcat6/bin/startup.sh",
require => Exec["stop tomcat"],
}
}
保存