11#! /bin/bash
2- version=" 0.1.8 "
2+ version=" 0.1.9 "
33echo " Starting server....(start_server.sh v$version )"
44if [ " $1 " == " -h" ]; then
5- echo " Usage: ` basename $0 ` application_url"
6- echo ' Param 1: (optional) URL for SNS Callback'
7- echo ' Will use public-host-name if not passed'
8- echo ' e.g. https://myrg.example.com'
9- echo ' Param 2: (optional) Target Group ARN to register with'
10- exit 0
5+ echo " Usage: $( basename $0 ) < application_url> <target_group_arn> "
6+ echo ' Param 1: (optional) URL for SNS Callback'
7+ echo ' Will use public-host-name if not passed'
8+ echo ' e.g. https://myrg.example.com'
9+ echo ' Param 2: (optional) Target Group ARN to register with'
10+ exit 0
1111fi
1212
13- [ -z $RG_HOME ] && RG_HOME=' /opt/deploy/sp2'
13+ [ -z " $RG_HOME " ] && RG_HOME=' /opt/deploy/sp2'
1414echo " RG_HOME=$RG_HOME "
1515
1616myurl=$1
@@ -27,24 +27,23 @@ docker pull $(cat "$RG_HOME/docker-compose.yml" | grep -i image | grep -i nginx
2727echo ' Pulling docker image for notificationsink'
2828docker pull $( cat " $RG_HOME /docker-compose.yml" | grep -i image | grep -i notificationsink | awk ' {print $2}' | uniq | tr -d ' \r' )
2929
30-
3130echo ' Modifying HttpResponseHopLimit'
3231ec2instanceid=$( curl -s http://169.254.169.254/latest/meta-data/instance-id)
3332aws ec2 modify-instance-metadata-options --instance-id " $ec2instanceid " --http-put-response-hop-limit 2 --http-endpoint enabled
3433
3534if [ -z $myurl ]; then
36- public_host_name=" $( wget -q -O - http://169.254.169.254/latest/meta-data/public-hostname) "
37- baseurl=" $public_host_name "
35+ public_host_name=" $( wget -q -O - http://169.254.169.254/latest/meta-data/public-hostname) "
36+ baseurl=" $public_host_name "
3837else
39- baseurl=" $myurl "
38+ baseurl=" $myurl "
4039fi
4140echo " BaseURL=$baseurl "
4241echo " TGARN=$tgarn "
4342
4443if [ ! -z $tgarn ]; then
45- ec2instanceid=$( curl -s http://169.254.169.254/latest/meta-data/instance-id)
46- echo " Registering instance $ec2instanceid with Target group: $tgarn "
47- aws elbv2 register-targets --targets " Id=$ec2instanceid ,Port=$port " --target-group-arn " $tgarn "
44+ ec2instanceid=$( curl -s http://169.254.169.254/latest/meta-data/instance-id)
45+ echo " Registering instance $ec2instanceid with Target group: $tgarn "
46+ aws elbv2 register-targets --targets " Id=$ec2instanceid ,Port=$port " --target-group-arn " $tgarn "
4847fi
4948echo " Calling swarm init will respond with error if this node is already part of a swarm"
5049docker swarm init
@@ -60,68 +59,66 @@ sleep 5
6059
6160# Check if stack is deployed
6261echo " Checking if stack is deployed"
63- service_count=` docker service ls -q | wc -l`
64- if [ " ${service_count} " -gt 0 ]; then
65- echo " Service count deployed is $service_count "
62+ service_count=$( docker service ls -q | wc -l)
63+ if [ " ${service_count} " -gt 0 ]; then
64+ echo " Service count deployed is $service_count "
6665else
67- echo " Service not deployed successfully"
68- exit 1
66+ echo " Service not deployed successfully"
67+ exit 1
6968fi
7069
7170sleep 30
7271
73- function check_stack_status(){
74- # Check if all the services are up
75- state_replicated=0
76- for i in ` docker service ls -q` ; do
77- echo " $i "
78- replicated=` docker service inspect --pretty $i | grep -i " Service Mode" | awk ' {print $3}' `
79- echo " Replicated service status: $replicated "
80- if [ " ${replicated} " == " Replicated" ]; then
81- replicas=` docker service inspect --pretty $i | grep -i replicas | awk ' {print $2}' `
82- echo " Replicas count : $replicas "
83- running_replicas=` docker service ps $i | grep -i running | wc -l`
84- echo " Running replicas count : $running_replicas "
85- if [ ${replicas} -ne " ${running_replicas} " ]; then
86- let " state_replicated=state_replicated+1"
87- fi
88- fi
89- done
90- echo " Stack replication : $state_replicated "
91- if [ " ${state_replicated} " -gt 0 ]; then
92- echo " CRITICAL - Not all services are replicated"
93- else
94- echo " OK - All services are replicated"
95- fi
96- return $state_replicated
72+ function check_stack_status() {
73+ # Check if all the services are up
74+ state_replicated=0
75+ for i in $( docker service ls -q) ; do
76+ echo " $i "
77+ replicated=$( docker service inspect --pretty $i | grep -i " Service Mode" | awk ' {print $3}' )
78+ echo " Replicated service status: $replicated "
79+ if [ " ${replicated} " == " Replicated" ]; then
80+ replicas=$( docker service inspect --pretty $i | grep -i replicas | awk ' {print $2}' )
81+ echo " Replicas count : $replicas "
82+ running_replicas=$( docker service ps $i | grep -i running | wc -l)
83+ echo " Running replicas count : $running_replicas "
84+ if [ ${replicas} -ne " ${running_replicas} " ]; then
85+ let " state_replicated=state_replicated+1"
86+ fi
87+ fi
88+ done
89+ echo " Stack replication : $state_replicated "
90+ if [ " ${state_replicated} " -gt 0 ]; then
91+ echo " CRITICAL - Not all services are replicated"
92+ else
93+ echo " OK - All services are replicated"
94+ fi
95+ return $state_replicated
9796}
9897
99- for i in {0..3}
100- do
101- echo " Checking stack status $i "
102- check_stack_status
103- res=$?
104- echo " $res "
105- if [ " ${res} " == 0 ]; then
106- echo " All services are up"
107- break
108- else
109- sleep 30
110- fi
111- done
112-
98+ for i in {0..3}; do
99+ echo " Checking stack status $i "
100+ check_stack_status
101+ res=$?
102+ echo " $res "
103+ if [ " ${res} " == 0 ]; then
104+ echo " All services are up"
105+ break
106+ else
107+ sleep 30
108+ fi
109+ done
110+
113111# Check if web application is up and running
114- for i in {0..3}
115- do
116- sleep 10
117- echo " Checking if web application is up and running"
118- status_code=$( curl -sL -w " %{http_code}\n" " $baseurl " -o /dev/null)
119- if [ " $status_code " -ne 200 ]; then
120- echo " Application is not up, responded with status $status_code "
121- else
122- echo " Application is up and running, status code response is $status_code "
123- break
124- fi
125- done
126-
127- echo " Done"
112+ for i in {0..3}; do
113+ sleep 10
114+ echo " Checking if web application is up and running"
115+ status_code=$( curl -sL -w " %{http_code}\n" " $baseurl " -o /dev/null)
116+ if [ " $status_code " -ne 200 ]; then
117+ echo " Application is not up, responded with status $status_code "
118+ else
119+ echo " Application is up and running, status code response is $status_code "
120+ break
121+ fi
122+ done
123+
124+ echo " Done"
0 commit comments