Merge "Refactored docker watchdog code"
[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 ### Functions related to checking.
28
29 function is_deploying() {
30     xml=$(curl -m10 "https://build.opnfv.org/ci/job/${1}-automate-master/lastBuild/api/xml?depth=1")
31     building=$(grep -oPm1 "(?<=<building>)[^<]+" <<< "$xml")
32     if [[ $building == "false" ]]
33     then
34         return 0
35     else
36         return 1
37     fi
38 }
39
40 function get_docker_status() {
41     status=$(service docker status | sed -n 3p | cut -d ' ' -f5)
42     echo -e "Docker status: $status"
43     if [ $status = "active" ]
44     then
45         return 1
46     else
47         return 0
48     fi
49 }
50
51 function check_connectivity() {
52     echo "Checking $1 connection : $2"
53     cmd=`curl --head -m10 --request GET ${2} | grep '200 OK' > /dev/null`
54     rc=$?
55     if [[ $rc == 0 ]]; then
56         return 0
57     else
58         return 1
59     fi
60 }
61
62 function check_modules() {
63     echo -e "Checking modules"
64     failed_modules=()
65     for module in "${modules[@]}"
66     do
67         if is_deploying $module; then
68             continue
69         fi
70         if ! check_connectivity $module "${urls[$module]}"; then
71             echo -e "$module failed"
72             failed_modules+=($module)
73         fi
74     done
75     if [ ! -z "$failed_modules" ]; then
76         echo -e "Failed Modules: $failed_modules"
77         return 1
78     else
79         echo -e "All modules working good"
80         exit 0
81     fi
82 }
83
84 ### Functions related fixes.
85
86 function restart_docker_fix() {
87     echo -e "Running restart_docker_fix"
88     service docker restart
89     start_containers_fix "${modules[@]}"
90 }
91
92 function docker_proxy_fix() {
93     echo -e "Running docker_proxy_fix"
94     fix_modules=("${@}")
95     for module in "${fix_modules[@]}"
96     do
97         echo -e "Kill docker proxy and restart containers"
98         pid=$(netstat -nlp | grep :${ports[$module]} | awk '{print $7}' | cut -d'/' -f1)
99         echo $pid
100         if [ ! -z "$pid" ]; then
101             kill $pid
102             start_container_fix $module
103         fi
104     done
105 }
106
107 function start_containers_fix() {
108     start_modules=("${@}")
109     for module in "${start_modules[@]}"
110     do
111         start_container_fix $module
112     done
113 }
114
115 function start_container_fix() {
116     echo -e "Starting a container $module"
117     sudo docker stop $module
118     sudo docker start $module
119     sleep 5
120     if ! check_connectivity $module "${urls[$module]}"; then
121         echo -e "Starting an old container $module_old"
122         sudo docker stop $module
123         sudo docker start $module"_old"
124         sleep 5
125     fi
126 }
127
128 ### Main Flow
129
130 echo -e
131 echo -e "WatchDog Started"
132 echo -e
133 echo -e `date "+%Y-%m-%d %H:%M:%S.%N"`
134 echo -e
135
136 ## If the problem is related to docker daemon
137
138 if get_docker_status; then
139     restart_docker_fix
140     if ! check_modules; then
141         echo -e "Watchdog failed while restart_docker_fix"
142     fi
143     exit
144 fi
145
146 ## If the problem is related to docker proxy
147
148 if ! check_modules; then
149     docker_proxy_fix "${failed_modules[@]}"
150 fi
151
152 ## If any other problem : restart docker
153
154 if ! check_modules; then
155     restart_docker_fix
156 fi
157
158 ## If nothing works out
159
160 if ! check_modules; then
161     echo -e "Watchdog failed"
162 fi
163
164 sudo docker ps
165 sudo docker images