This repository has been archived by the owner on Jan 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Multi-jenkins to eliminate jenkins down time
Change-Id: I462c7db115f9728901f3576bb97820cbcbf50f68
- Loading branch information
1 parent
3b1b430
commit 3732803
Showing
7 changed files
with
157 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= properties.jenkins.rsa_keys.public %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#!/bin/bash | ||
RUN_DIR=/var/vcap/sys/run/jenkins | ||
DATA_DIR=/var/vcap/store/jenkins | ||
LOG_DIR=/var/vcap/store/log | ||
BACKUP_LOG=jenkins_backup.log | ||
TERMINATE_BACKUP=/tmp/terminate_backup | ||
MONIT_FILE=/var/vcap/jobs/jenkins/jenkins.monitrc | ||
|
||
log() { | ||
timenow=`date "+%Y-%m-%d %H:%M:%S"` | ||
find $LOG_DIR -maxdepth 1 -ctime +5 -name $BACKUP_LOG | grep "$BACKUP_LOG" | ||
[ $? == 0 ] && mv $LOG_DIR/$BACKUP_LOG $LOG_DIR/${BACKUP_LOG}.1 | ||
echo "$timenow $1" >> $LOG_DIR/$BACKUP_LOG | ||
} | ||
|
||
jenkins_master_id=1 | ||
jenkins_address=<%= properties.jenkins.address %> | ||
/sbin/ifconfig | grep $jenkins_address | ||
[ $? == 0 ] || jenkins_master_id=2 | ||
|
||
jenkins_backup_address=<%= properties.jenkins.backup_address %> | ||
[ -z "$jenkins_backup_address" ] && exit 1 | ||
|
||
if [ $jenkins_master_id == 2 ]; then | ||
cat $MONIT_FILE | grep "$jenkins_backup_address" | ||
if [ $? != 0 ]; then | ||
cat $MONIT_FILE | sed -e "s/$jenkins_address/$jenkins_backup_address/g" > /tmp/jenkins.monitrc | ||
sudo mv /tmp/jenkins.monitrc $MONIT_FILE | ||
sudo /var/vcap/bosh/bin/monit reload | ||
sleep 20 | ||
sudo /var/vcap/bosh/bin/monit restart all | ||
fi | ||
[ -f $TERMINATE_BACKUP ] && rm $TERMINATE_BACKUP | ||
exit 0 | ||
fi | ||
|
||
LAST_BAK_FILE=$DATA_DIR/last_backup_time | ||
LAST_RESTART_FILE=$DATA_DIR/last_restart_time | ||
backup_interval=<%= properties.jenkins.backup_interval || 20 %> #unit: min | ||
restart_interval=<%= properties.jenkins.restart_interval || 12 %> #unit: hour | ||
backup_interval_secs=$((backup_interval * 60)) | ||
restart_interval_secs=$((restart_interval * 60 * 60)) | ||
|
||
[ -f $TERMINATE_BACKUP ] && rm $TERMINATE_BACKUP | ||
while true | ||
do | ||
sleep 20 | ||
[ -f $TERMINATE_BACKUP ] && exit 0 | ||
|
||
last_backup=0 | ||
[ -f $LAST_BAK_FILE ] && last_backup=`head -1 $LAST_BAK_FILE` | ||
|
||
last_restart=0 | ||
[ -f $LAST_RESTART_FILE ] && last_restart=`head -1 $LAST_RESTART_FILE` | ||
|
||
now=`date +%s` | ||
ps $JENKINS_PID | ||
if [ $? == 0 ] && (( $now - $last_backup > $backup_interval_secs )); then | ||
log "Prepare for starting sync to $jenkins_backup_address:$DATA_DIR/" | ||
rsync -avz $DATA_DIR/jobs vcap@$jenkins_backup_address:$DATA_DIR/ | grep "jobs" >> $LOG_DIR/$BACKUP_LOG | ||
echo $now > $LAST_BAK_FILE | ||
fi | ||
|
||
now=`date +%s` | ||
ps $JENKINS_PID | ||
if [ $? == 0 ] && (( $now - $last_restart > $restart_interval_secs )); then | ||
log "Retart remote jenkins $jenkins_backup_address" | ||
ssh vcap@$jenkins_backup_address "sudo /var/vcap/bosh/bin/monit restart jenkins" | ||
echo $now > $LAST_RESTART_FILE | ||
fi | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/bin/bash | ||
LOG_DIR=/var/vcap/store/log/ | ||
TERMINATE_BACKUP=/tmp/terminate_backup | ||
STANDBY_LOG=jenkins_standby.log | ||
|
||
log() { | ||
timenow=`date "+%Y-%m-%d %H:%M:%S"` | ||
find $LOG_DIR -maxdepth 1 -ctime +5 -name $STANDBY_LOG | grep "$STANDBY_LOG" | ||
[ $? == 0 ] && mv $LOG_DIR/$STANDBY_LOG $LOG_DIR/${STANDBY_LOG}.1 | ||
echo "$timenow $1" >> $LOG_DIR/$STANDBY_LOG | ||
} | ||
|
||
jenkins_address=<%= properties.jenkins.address %> | ||
/sbin/ifconfig | grep $jenkins_address | ||
[ $? == 0 ] && exit 0 | ||
|
||
[ -f $TERMINATE_BACKUP ] && rm $TERMINATE_BACKUP | ||
while true | ||
do | ||
sleep 10 | ||
[ -f $TERMINATE_BACKUP ] && exit 0 | ||
|
||
status=`curl -o /dev/null -s -w %{http_code} http://<%= properties.jenkins.address %>:<%= properties.jenkins.http_port %>` | ||
if (( $status == 200 )); then | ||
log "The jenkins $jenkins_address is back, cut connection to gerrit <%= properties.gerrit.address %>" | ||
iptables -L | grep <%= properties.gerrit.address %> | ||
[ $? == 0 ] || iptables -A OUTPUT -p ALL -d <%= properties.gerrit.address %> -j DROP | ||
else | ||
log "The jenkins $jenkins_address is error due to $status, open connection to gerrit <%= properties.gerrit.address %>" | ||
iptables -L | grep <%= properties.gerrit.address %> | ||
[ $? == 0 ] && iptables -D OUTPUT 1 | ||
fi | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters