bug 11266: Add lockfile for scripts in system vm

Otherwise it's easy to trigger the racy issue.

This one just contained fix for reconfigLB.sh
This commit is contained in:
Sheng Yang 2011-08-25 17:54:22 -07:00
parent db8341bb35
commit c7a887a51c
2 changed files with 60 additions and 3 deletions

View File

@ -0,0 +1,44 @@
#!/bin/bash
# getLockFile() parameters
# $1 lock filename
# $2 timeout seconds
getLockFile() {
__locked=0
__LOCKFILE="/tmp/$1.lock"
if [ $2 ]
then
__TIMEOUT=$2
else
__TIMEOUT=10
fi
for i in `seq 1 $__TIMEOUT`
do
if [ ! -e $__LOCKFILE ]
then
touch $__LOCKFILE
__locked=1
break
fi
sleep 1
logger -t cloud "sleep 1 second wait for the lock file " $__LOCKFILE
done
if [ $__locked -ne 1 ]
then
logger -t cloud "fail to acquire the lock file $__LOCKFILE after $__TIMEOUT seconds time out!"
fi
echo $__locked
}
# releaseLockFile() parameters
# $1 lock filename
# $2 locked(1) or not(0)
releaseLockFile() {
__LOCKFILE="/tmp/$1.lock"
__locked=$2
if [ "$__locked" == "1" ]
then
rm $__LOCKFILE
fi
}

View File

@ -18,9 +18,18 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
name="reconfigLB"
source func.sh
locked=$(getLockFile $name)
if [ "$locked" != "1" ]
then
logger -t cloud "Fail to get the lock for " $name
exit 1
fi
ret=0
# save previous state
mv /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.old
mv /var/run/haproxy.pid /var/run/haproxy.pid.old
@ -32,7 +41,7 @@
echo "New haproxy instance successfully loaded, stopping previous one."
kill -KILL $(cat /var/run/haproxy.pid.old)
rm -f /var/run/haproxy.pid.old
exit 0
ret=0
else
echo "New instance failed to start, resuming previous one."
kill -TTIN $(cat /var/run/haproxy.pid.old)
@ -40,5 +49,9 @@
mv /var/run/haproxy.pid.old /var/run/haproxy.pid
mv /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.new
mv /etc/haproxy/haproxy.cfg.old /etc/haproxy/haproxy.cfg
exit 1
ret=1
fi
releaseLockFile $name $locked
exit $ret