Merge "leverage LFID as Authentication"
[releng.git] / utils / test / testapi / tools / watchdog / docker_watch.sh
1 #                                                               *
2 #    http://www.apache.org/licenses/LICENSE-2.0                 *
3 #                                                               *
4 #  Unless required by applicable law or agreed to in writing,   *
5 #  software distributed under the License is distributed on an  *
6 #  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
7 #  KIND, either express or implied.  See the License for the    *
8 #  specific language governing permissions and limitations      *
9 #  under the License.                                           *
10
11 # This script checks if deployments are working or and then
12 # starts the specified containers in case one of the containers
13 # crash. The only solution is restarting docker as of now.
14
15 #!/bin/bash
16
17 ## List of modules
18 modules=(testapi reporting)
19
20 ## Ports of the modules
21 declare -A ports=( ["testapi"]="8082" ["reporting"]="8084")
22
23 ## Urls to check if the modules are deployed or not ?
24 #declare -A urls=( ["testapi"]="http://testresults.opnfv.org/test/" \
25 #    ["reporting"]="http://testresults.opnfv.org/reporting2/reporting/index.html")
26
27 declare -A urls=( ["testapi"]="http://localhost:8082/" \
28     ["reporting"]="http://testresults.opnfv.org/reporting2/reporting/index.html")
29
30
31 ### Functions related to checking.
32
33 function is_deploying() {
34     echo -e "Checking job statuses"
35     for module in "${modules[@]}"
36     do
37         if get_status $module; then
38             exit 0
39         fi
40     done
41 }
42
43 function get_status() {
44     xml=$(curl -m10 "https://build.opnfv.org/ci/job/${1}-automate-master/lastBuild/api/xml?depth=1")
45     building=$(grep -oPm1 "(?<=<building>)[^<]+" <<< "$xml")
46     if [[ $building == "false" ]]
47     then
48         return 1
49     else
50         return 0
51     fi
52 }
53
54 function get_docker_status() {
55     status=$(service docker status | sed -n 3p | cut -d ' ' -f5)
56     echo -e "Docker status: $status"
57     if [ $status = "active" ]
58     then
59         return 1
60     else
61         return 0
62     fi
63 }
64
65 function check_connectivity() {
66     echo "Checking $1 connection : $2"
67     cmd=`curl --head -m10 --request GET ${2} | grep '200 OK' > /dev/null`
68     rc=$?
69     if [[ $rc == 0 ]]; then
70         return 0
71     else
72         return 1
73     fi
74 }
75
76 function check_modules() {
77     echo -e "Checking modules"
78     failed_modules=()
79     for module in "${modules[@]}"
80     do
81         if ! check_connectivity $module "${urls[$module]}"; then
82             echo -e "$module failed"
83             failed_modules+=($module)
84         fi
85     done
86     if [ ! -z "$failed_modules" ]; then
87         echo -e "Failed Modules: $failed_modules"
88         return 1
89     else
90         echo -e "All modules working good"
91         exit 0
92     fi
93 }
94
95 ### Functions related fixes.
96
97 function restart_docker_fix() {
98     echo -e "Running restart_docker_fix"
99     service docker restart
100     start_containers_fix "${modules[@]}"
101 }
102
103 function docker_proxy_fix() {
104     echo -e "Running docker_proxy_fix"
105     fix_modules=("${@}")
106     for module in "${fix_modules[@]}"
107     do
108         echo -e "Kill docker proxy and restart containers"
109         pid=$(netstat -nlp | grep :${ports[$module]} | awk '{print $7}' | cut -d'/' -f1)
110         echo $pid
111         if [ ! -z "$pid" ]; then
112             kill $pid
113             start_containers_fix $module
114         fi
115     done
116 }
117
118 function start_containers_fix() {
119     echo "Runnning start_containers_fix"
120     start_modules=("${@}")
121     for module in "${start_modules[@]}"
122     do
123         echo -e "Starting a container $module"
124         sudo docker stop $module
125         sudo docker start $module
126         sleep 5
127         if ! check_connectivity $module "${urls[$module]}"; then
128             echo -e "Starting an old container $module_old"
129             sudo docker stop $module
130             sudo docker start $module"_old"
131             sleep 5
132         fi
133     done
134 }
135
136 ### Main Flow
137
138 echo -e
139 echo -e "WatchDog Started"
140 echo -e
141 echo -e `date "+%Y-%m-%d %H:%M:%S.%N"`
142 echo -e
143
144 if ! is_deploying; then
145     echo -e "Jenkins Jobs running"
146     exit
147 fi
148
149 ## If the problem is related to docker daemon
150
151 if get_docker_status; then
152     restart_docker_fix
153     if ! check_modules; then
154         echo -e "Watchdog failed while restart_docker_fix"
155     fi
156     exit
157 fi
158
159 ## If the problem is related to docker containers
160
161 if ! check_modules; then
162     start_containers_fix "${failed_modules[@]}"
163 fi
164
165 ## If the problem is related to docker proxy
166
167 if ! check_modules; then
168     docker_proxy_fix "${failed_modules[@]}"
169 fi
170
171 ## If nothing works out
172
173 if ! check_modules; then
174     echo -e "Watchdog failed"
175 fi
176
177 sudo docker ps
178 sudo docker images