我正在尝试在替换木偶服务背后的软件时避免竞争条件.
为此,puppet需要停止服务,替换可执行文件,然后启动服务.有没有办法让傀儡这样做?它的首选方式似乎是替换可执行文件,然后检查状态并在必要时再次启动服务.
(这个例子是人为的.真正的竞争条件远不是这么简单......)
这是我用来模拟这个问题的木偶清单:
$O = '1'
$I = '2'
exec { hi :
command => '/bin/echo "$(/bin/date +%s) puppet says hello" >> /tmp/freebird.log' ,
}
file { exe :
name => "/tmp/freebird" ,
ensure => present ,
owner => "root" ,
group => "root" ,
mode => "0555" ,
source => "/root/test-v$I" ,
}
file { init :
name => '/etc/init.d/freebird' ,
ensure => present,
owner => "root",
group => "root",
mode => "0555",
source => "/root/test.init" ,
}
service { freebird :
ensure => running,
enable => true,
hasrestart => true,
hasstatus => true,
require => [ File[init], File[exe] ],
}
Run Code Online (Sandbox Code Playgroud)
这是test-v1文件.该测试-V2文件是相同的,但用v=2.
#!/bin/bash
v=1
while true
do
echo "$(date +%s) $v" >> /tmp/freebird-v.log
sleep 1
done
Run Code Online (Sandbox Code Playgroud)
和init.d脚本:
#!/bin/bash
#
# /etc/rc.d/init.d/freebird
# chkconfig: 2345 90 10
# description: freebird
# Provides: freebird
# Required-Start: $syslog $remote_fs
# Should-Start:
# Required-Stop: $syslog $remote_fs
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: freebird
# Source function library.
. /etc/rc.d/init.d/functions
xme=freebird
export PATH=/sbin:/bin:/usr/sbin:/usr/bin
function L () {
echo "$(date +%s) $*" 1>&2
echo "$(date +%s) $*" >> /tmp/$xme.log
}
case "$1" in
(start) L $1 $xme
( /tmp/$xme &)
;;
(stop) L $1 $xme
fuser -k /tmp/$xme
;;
(status) L $1 $xme
/sbin/fuser /tmp/$xme >/dev/null 2>&1
;;
(restart) L $1 $xme
$0 stop
$0 start
;;
(*)
echo "Usage: $xme {start|stop|status|restart]"
exit 1
;;
esac
Run Code Online (Sandbox Code Playgroud)
我试图在替换木偶服务背后的软件时避免竞争条件。
为此,puppet 需要停止服务,替换可执行文件,然后启动服务。有没有办法让木偶这样做?它的首选处理方式似乎是替换可执行文件,然后检查状态并在必要时再次启动服务。
那么 Puppet 目前正在做的问题是它应该总是在替换某些文件后重新启动服务?
如果是这样,您应该使用通知/订阅关系来始终在文件替换后触发服务重新启动。以您的示例为例service,我们可以将订阅添加到组成它的文件中(与配置的方式相同),如果其中任何一个发生更改,这将触发重新启动。
service { freebird :
ensure => running,
enable => true,
hasrestart => true,
hasstatus => true,
require => [ File[init], File[exe] ],
subscribe => [ File[init], File[exe] ],
}
Run Code Online (Sandbox Code Playgroud)
另一种方法是使用操作系统包管理,Puppet 对此有很好的支持。然后,您可以从包脚本中触发重新启动(或安装前/安装后的停止/启动),让 Puppet 确保服务已配置并运行。查看 Jordan Sissel 的fpm 项目,了解一个可以轻松构建多种包格式的工具。
| 归档时间: |
|
| 查看次数: |
10847 次 |
| 最近记录: |