blob: 651e9db2368abfe5fdd103aa84d56d3724a07604 [file] [log] [blame]
ramonsalguer917ce8c2020-07-16 14:42:04 +02001#!/usr/bin/env bash
Mike Marchetti9d9192b2018-09-21 12:03:05 -04002
ramonsalguer917ce8c2020-07-16 14:42:04 +02003# Copyright 2020 Telefónica Investigación y Desarrollo S.A.U.
vijaynag8339ed22019-07-25 17:10:58 +05304#
ramonsalguer917ce8c2020-07-16 14:42:04 +02005# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
vijaynag8339ed22019-07-25 17:10:58 +05308#
ramonsalguer917ce8c2020-07-16 14:42:04 +02009# http://www.apache.org/licenses/LICENSE-2.0
vijaynag8339ed22019-07-25 17:10:58 +053010#
ramonsalguer917ce8c2020-07-16 14:42:04 +020011# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
vijaynag8339ed22019-07-25 17:10:58 +053016
ramonsalguer917ce8c2020-07-16 14:42:04 +020017# Default values
18sampling_period=5 # seconds
19time_for_readiness=2 # minutes ready
garciadeblas6e2e0072021-09-07 18:58:35 +020020time_for_failure=7 # minutes broken
ramonsalguer917ce8c2020-07-16 14:42:04 +020021KUBERNETES= # By default, assumes Docker Swarm installation
22STACK_NAME=osm # By default, "osm"
Mike Marchetti9d9192b2018-09-21 12:03:05 -040023
ramonsalguer917ce8c2020-07-16 14:42:04 +020024while getopts "p:r:f:s:k" o; do
Mike Marchetti9d9192b2018-09-21 12:03:05 -040025 case "${o}" in
ramonsalguer917ce8c2020-07-16 14:42:04 +020026 p)
27 sampling_period=${OPTARG}
28 ;;
29 r)
30 time_for_readiness=${OPTARG}
31 ;;
32 f)
33 time_for_failure=${OPTARG}
Mike Marchetti9d9192b2018-09-21 12:03:05 -040034 ;;
35 s)
36 STACK_NAME=${OPTARG}
37 ;;
vijaynag8339ed22019-07-25 17:10:58 +053038 k)
39 KUBERNETES="y"
40 ;;
Mike Marchetti9d9192b2018-09-21 12:03:05 -040041 esac
42done
43
ramonsalguer917ce8c2020-07-16 14:42:04 +020044oks_threshold=$((time_for_readiness*60/${sampling_period})) # No. ok samples to declare the system ready
45failures_threshold=$((time_for_failure*60/${sampling_period})) # No. nok samples to declare the system broken
46failures_in_a_row=0
47oks_in_a_row=0
Mike Marchetti9d9192b2018-09-21 12:03:05 -040048
ramonsalguer917ce8c2020-07-16 14:42:04 +020049
garciadeblas8080e4b2023-04-14 09:57:17 +020050# Show status of the OSM services deployed with helm
51echo "helm -n ${STACK_NAME} list"
52helm -n ${STACK_NAME} list
53echo "helm -n ${STACK_NAME} status ${STACK_NAME}"
54helm -n ${STACK_NAME} status ${STACK_NAME}
55
ramonsalguer917ce8c2020-07-16 14:42:04 +020056####################################################################################
57# Loop to check system readiness
58####################################################################################
59while [[ (${failures_in_a_row} -lt ${failures_threshold}) && (${oks_in_a_row} -lt ${oks_threshold}) ]]
60do
61
62 #------------ CHECKS FOR KUBERNETES INSTALLATION
63 if [ -n "$KUBERNETES" ]
64 then
65
66 # State of Deployments
67 DEPLOYMENTS_STATE=$(kubectl get deployment -n ${STACK_NAME} --no-headers 2>&1)
68 DEPLOYMENTS_READY=$(echo "${DEPLOYMENTS_STATE}" | awk '$2=="1/1" && $4=="1" {printf ("%20s\t%s\t%s\n", $1, $2, $4)}')
69 DEPLOYMENTS_NOT_READY=$(echo "${DEPLOYMENTS_STATE}" | awk '$2!="1/1" || $4!="1" {printf ("%20s\t%s\t%s\n", $1, $2, $4)}')
70 COUNT_DEPLOYMENTS_READY=$(echo "${DEPLOYMENTS_READY}"| grep -v -e '^$' | wc -l)
71 COUNT_DEPLOYMENTS_NOT_READY=$(echo "${DEPLOYMENTS_NOT_READY}" | grep -v -e '^$' | wc -l)
72
73 # State of Statefulsets
74 STS_STATE=$(kubectl get statefulset -n ${STACK_NAME} --no-headers 2>&1)
almagiaf976ab82023-12-07 15:19:15 +010075 STS_READY=$(echo "${STS_STATE}" | awk '$2=="1/1" || $2=="2/2" || $2=="3/3" {printf ("%20s\t%s\t%s\n", $1, $2, $4)}')
76 STS_NOT_READY=$(echo "${STS_STATE}" | awk '$2!="1/1" && $2!="2/2" && $2!="3/3" {printf ("%20s\t%s\t%s\n", $1, $2, $4)}')
ramonsalguer917ce8c2020-07-16 14:42:04 +020077 COUNT_STS_READY=$(echo "${STS_READY}" | grep -v -e '^$' | wc -l)
78 COUNT_STS_NOT_READY=$(echo "${STS_NOT_READY}" | grep -v -e '^$' | wc -l)
79
80 # OK sample
81 if [[ $((${COUNT_DEPLOYMENTS_NOT_READY}+${COUNT_STS_NOT_READY})) -eq 0 ]]
82 then
83 ((++oks_in_a_row))
84 failures_in_a_row=0
85 echo -ne ===\> Successful checks: "${oks_in_a_row}"/${oks_threshold}\\r
86 # NOK sample
87 else
88 ((++failures_in_a_row))
89 oks_in_a_row=0
90 echo
91 echo Bootstraping... "${failures_in_a_row}" attempts of ${failures_threshold}
92
93 # Reports failed deployments
94 if [[ "${COUNT_DEPLOYMENTS_NOT_READY}" -ne 0 ]]
95 then
96 echo ${COUNT_DEPLOYMENTS_NOT_READY} of $((${COUNT_DEPLOYMENTS_NOT_READY}+${COUNT_DEPLOYMENTS_READY})) deployments starting:
97 echo "${DEPLOYMENTS_NOT_READY}"
98 echo
99 fi
100
101 # Reports failed statefulsets
102 if [[ "${COUNT_STS_NOT_READY}" -ne 0 ]]
103 then
104 echo ${COUNT_STS_NOT_READY} of $((${COUNT_STS_NOT_READY}+${COUNT_STS_READY})) statefulsets starting:
105 echo "${STS_NOT_READY}"
106 echo
107 fi
vijaynag8339ed22019-07-25 17:10:58 +0530108 fi
ramonsalguer917ce8c2020-07-16 14:42:04 +0200109
110 #------------ CHECKS FOR DOCKER SWARM INSTALLATION
vijaynag8339ed22019-07-25 17:10:58 +0530111 else
ramonsalguer917ce8c2020-07-16 14:42:04 +0200112 # State of Docker Services
113 SERVICES_STATE=$(sg docker -c "docker service ls" 2>&1 | grep " ${STACK_NAME}_")
114 SERVICES_READY=$(echo "${SERVICES_STATE}" | awk '$3=="replicated" && $4=="1/1" {printf ("%20s\t%s\n", $2, $4)}')
115 SERVICES_NOT_READY=$(echo "${SERVICES_STATE}" | awk '$3=="replicated" && $4!="1/1" {printf ("%20s\t%s\n", $2, $4)}')
116 COUNT_SERVICES_READY=$(echo "${SERVICES_READY}" | grep -v -e '^$' | wc -l)
117 COUNT_SERVICES_NOT_READY=$(echo "${SERVICES_NOT_READY}" | grep -v -e '^$' | wc -l)
118
119 # OK sample
120 if [[ ${COUNT_SERVICES_NOT_READY} -eq 0 ]]
121 then
122 ((++oks_in_a_row))
123 failures_in_a_row=0
124 echo -ne ===\> Successful checks: "${oks_in_a_row}"/${oks_threshold}\\r
125 # NOK sample
126 else
127 ((++failures_in_a_row))
128 oks_in_a_row=0
129 echo
130 echo Bootstraping... "${failures_in_a_row}" attempts of ${failures_threshold}
131 echo ${COUNT_SERVICES_NOT_READY} of $((${COUNT_SERVICES_NOT_READY}+${COUNT_SERVICES_READY})) services starting:
132 echo "${SERVICES_NOT_READY}"
vijaynag8339ed22019-07-25 17:10:58 +0530133 fi
Mike Marchetti9d9192b2018-09-21 12:03:05 -0400134 fi
135
ramonsalguer917ce8c2020-07-16 14:42:04 +0200136 #------------ NEXT SAMPLE
137 sleep ${sampling_period}
138
Mike Marchetti9d9192b2018-09-21 12:03:05 -0400139done
Mike Marchetti37c3f512018-09-24 10:27:00 -0400140
tiernobc983ec2018-10-11 15:03:06 +0200141
ramonsalguer917ce8c2020-07-16 14:42:04 +0200142####################################################################################
143# OUTCOME
144####################################################################################
145if [[ (${failures_in_a_row} -ge ${failures_threshold}) ]]
146then
147 echo
148 echo SYSTEM IS BROKEN
149 exit 1
150else
151 echo
152 echo SYSTEM IS READY
153fi