Ops/Deploy
Deploy script - service status check (bash)
mkdir.chandler
2023. 8. 24. 00:00
반응형
Deploy script - service status check (bash)
■ 설명
Autoscaling이 적용되어 있는 EC2 서버의 배포를 위한 script 입니다.
서비스의 현재 상태를 확인하고, 일괄로 서비스를 내리거나 서비스를 시작하기 위한 스크립트입니다.
AWS가 아닌 다른 환경이라면 ip를 얻어오는 방식을 해당 환경에 맞춰서 수정하면 됩니다.
■ 로직
1. ALB에 등록되어 있는 EC2 서버들의 ip를 확인 및 수집 (Traget Group을 이용)
2. 해당 ip를 이용하여 해당 EC2에 접속
3. 서비스 (데몬) 의 상태를 확인 / 중지 / 시작 명령
■ 코드
#! /bin/sh
ARGUMENTS=$*
if [ ! "$ARGUMENTS" ]; then
echo "No commands specified."
exit -1;
fi
region=ap-northeast-2
env=dev
echo "+------------------------------------------------------------------------------------+"
echo "| Execute commands to all server |"
echo "| COMMAND: ${ARGUMENTS} |"
echo "| Region: ${region} |"
echo "| env: ${env} |"
echo "+------------------------------------------------------------------------------------+"
echo ""
SERVICE_CODE=mc
BASE=/home/$USER
ELB_NAME=elb-mkdir-chandler
ec2_private_ips=""
function join_by
{
local IFS="$1";
shift;
echo "$*";
}
function get_ec2ips_from_elbv2
{
local elb_arns=$(aws elbv2 --region ${region} describe-load-balancers --names $ELB_NAME | jq -r '.LoadBalancers[].LoadBalancerArn')
local target_group_arn=$(aws elbv2 --region ${region} describe-target-groups --load-balancer-arn ${elb_arns[0]} | jq -r '.TargetGroups[].TargetGroupArn')
local target_group_arn_string=$(join_by ' ' $target_group_arn)
local ids=$(aws elbv2 --region ${region} describe-target-health --target-group-arn ${target_group_arn_string} | jq -r '.TargetHealthDescriptions[].Target.Id' )
if [ ! "$ids" ];then
echo " >> There is no ec2 in ELB"
clear_end
fi
ec2_private_ips=$(aws ec2 --region ${region} describe-instances --instance-ids $ids | jq -r '.Reservations[].Instances[].NetworkInterfaces[].PrivateIpAddresses[].PrivateIpAddress')
echo $ec2_private_ips
}
function do_command_all
{
if [ $? -ne 0 ]; then
echo " - error ouccered, process is terminated!!!!!!!!!!!!!!!"
echo ""
exit -1
fi
if [ "$ec2_private_ips" == "" ];then
echo " - error ouccered, process is terminated!!!!!!!!!!!!!!!"
echo ""
exit -1
fi
for ip in $ec2_private_ips
do
echo ""
echo "= [ $ip ] ========================================================================="
echo ""
ssh -oStrictHostKeyChecking=no -i $BASE/.key/mkdir-chandler_key.pem ec2-user@$ip "$*"
echo ""
sleep 2
done
clear_end
}
function clear_end
{
echo ""
echo " * Execute command done ********************";
echo ""
exit 0;
}
get_ec2ips_from_elbv2
do_command_all $ARGUMENTS
exit 0;
by mkdir-chandler
728x90
반응형