Тема: Stop/Start Postgres-XL cluster на AWS
Есть небольшой кластер Postgres-XL (если интересно то напишу побольше про него, интересная штука...) и для экономии понадобилась его останавливать на ночь, так как это дев и он никому не нужен. А по скольку у AWS очень хороший подсчет использования по времени, то экономия нормальная.
Для этого в gitlab создали проект которой стягивает Jenkins и по крону делает stop/start
Start: запустить сервера, запустить кластер
Stop: остановить кластер, остановить сервера
Скрипты которые запускают и останавливают кластер
pgxc_start.sh
#!/bin/bash
/usr/local/pgsql/bin/pgxc_ctl monitor all
sleep 2
/usr/local/pgsql/bin/pgxc_ctl start all
sleep 2
/usr/local/pgsql/bin/pgxc_ctl monitor all
sleep 2
CHKGTM=`/usr/local/pgsql/bin/pgxc_ctl monitor gtm 2>&1 | grep -q '^Not running' && echo 1 || echo 0`
CHKCRD=`/usr/local/pgsql/bin/pgxc_ctl monitor coordinator 2>&1 | grep -q '^Not running' && echo 1 || echo 0`
CHKDN1=`/usr/local/pgsql/bin/pgxc_ctl monitor datanode dn01 2>&1 | grep -q '^Not running' && echo 1 || echo 0`
CHKDN2=`/usr/local/pgsql/bin/pgxc_ctl monitor datanode dn02 2>&1 | grep -q '^Not running' && echo 1 || echo 0`
RET=0
if [ $CHKGTM -ne 0 ]; then
RET=$CHKGTM
fi
if [ $CHKCRD -ne 0 ]; then
RET=$CHKCRD
fi
if [ $CHKDN1 -ne 0 ]; then
RET=$CHKDN1
fi
if [ $CHKDN2 -ne 0 ]; then
RET=$CHKDN2
fi
echo "$RET"
exit $RET
pgxc_stop.sh
#!/bin/bash
/usr/local/pgsql/bin/pgxc_ctl monitor all
sleep 2
/usr/local/pgsql/bin/pgxc_ctl stop all
sleep 2
/usr/local/pgsql/bin/pgxc_ctl monitor all
sleep 2
CHKGTM=`/usr/local/pgsql/bin/pgxc_ctl monitor gtm 2>&1 | grep -q '^Not running' && echo 0 || echo 1`
CHKCRD=`/usr/local/pgsql/bin/pgxc_ctl monitor coordinator 2>&1 | grep -q '^Not running' && echo 0 || echo 1`
CHKDN1=`/usr/local/pgsql/bin/pgxc_ctl monitor datanode dn01 2>&1 | grep -q '^Not running' && echo 0 || echo 1`
CHKDN2=`/usr/local/pgsql/bin/pgxc_ctl monitor datanode dn02 2>&1 | grep -q '^Not running' && echo 0 || echo 1`
RET=0
if [ $CHKGTM -ne 0 ]; then
RET=$CHKGTM
fi
if [ $CHKCRD -ne 0 ]; then
RET=$CHKCRD
fi
if [ $CHKDN1 -ne 0 ]; then
RET=$CHKDN1
fi
if [ $CHKDN2 -ne 0 ]; then
RET=$CHKDN2
fi
echo "$RET"
exit $RET
Скрипты которые выполняет Jenkins
DEV-PostgresXL-Start.groovy
def nodeLabel = 'DEV'
def region = 'eu-west-1'
def createVirtualEnv(name) {
sh "virtualenv ${name} -p /usr/bin/python2.7"
}
def executeIn(environment, scriptName) {
echo "Running command ${scriptName}"
sh """#!/bin/bash -xe
source ./${environment}/bin/activate && ${scriptName}
"""
}
node(nodeLabel) {
try {
stage('Repository checkout and virtual environment creation') {
git branch: 'master', credentialsId: '111-111-111-111-111', url: 'git@gitlab:jenkins/environment-shutdown.git'
sh(returnStdout: true, script: 'find ./scripts -type f | xargs chmod +x')
createVirtualEnv 'env'
executeIn 'env', 'pip install boto3 && pip install jinja2'
}
stage('Start all instances') {
sh """
aws ec2 start-instances --region ${region} --instance-ids \
i-111 \
i-222 \
i-333 \
i-444 \
i-555 \
i-666
"""
}
stage('Wait for all became "running"') {
sh """
aws ec2 wait instance-running --region ${region} --instance-ids \
i-111 \
i-222 \
i-333 \
i-444 \
i-555 \
i-666
echo \$?
"""
}
stage('Wait for all became "ok"') {
sh """
aws ec2 wait instance-status-ok --region ${region} --instance-ids \
i-111 \
i-222 \
i-333 \
i-444 \
i-555 \
i-666
echo \$?
"""
}
stage('Start PG Cluster') {
sshagent(['2222-2222-2222-2222-2222']) {
executeIn 'env', 'ssh -o StrictHostKeyChecking=no -o BatchMode=yes [email protected] "bash -s" < ./scripts/PostgresXL/pgxc_start.sh'
}
}
} catch(e) {
executeIn 'env', "echo \"${JOB_NAME} ${nodeLabel}\"; exit 1"
}
}
DEV-PostgresXL-Stop.groovy
def nodeLabel = 'DEV'
def region = 'eu-west-1'
def createVirtualEnv(name) {
sh "virtualenv ${name} -p /usr/bin/python2.7"
}
def executeIn(environment, scriptName) {
echo "Running command ${scriptName}"
sh """#!/bin/bash -xe
source ./${environment}/bin/activate && ${scriptName}
"""
}
node(nodeLabel) {
try {
stage('Repository checkout and virtual environment creation') {
git branch: 'master', credentialsId: '1111-1111-1111-1111-1111', url: 'git@gitlab:jenkins/environment-shutdown.git'
sh(returnStdout: true, script: 'find ./scripts -type f | xargs chmod +x')
createVirtualEnv 'env'
executeIn 'env', 'pip install boto3 && pip install jinja2'
}
stage('Stop PG Cluster') {
sshagent(['2222-2222-2222-2222-2222']) {
executeIn 'env', 'ssh -o StrictHostKeyChecking=no -o BatchMode=yes [email protected] "bash -s" < ./scripts/PostgresXL/pgxc_stop.sh'
}
}
stage('Stop all instances') {
sh """
aws ec2 stop-instances --region ${region} --instance-ids \
i-111 \
i-222 \
i-333 \
i-444 \
i-555 \
i-666
"""
}
stage('Wait for all became "stopped"') {
sh """
aws ec2 wait instance-stopped --region ${region} --instance-ids \
i-111 \
i-222 \
i-333 \
i-444 \
i-555 \
i-666
echo \$?
"""
}
} catch(e) {
executeIn 'env', "echo \"${JOB_NAME} ${nodeLabel}\"; exit 1"
}
}