在替换 puppet 服务后面的软件时,我试图避免竞争条件。
为此,puppet 需要停止服务,替换可执行文件,然后启动服务。有没有办法说服 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] ],
}
这是test-v1文件。test-v2文件相同,但带有v=2
.
#!/bin/bash
v=1
while true
do
echo "$(date +%s) $v" >> /tmp/freebird-v.log
sleep 1
done
和 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