Merge "cloudify_ims reporting fixes"
[releng.git] / utils / test / opts / watchdog.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/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-docker-deploy-master/lastBuild/api/xml?depth=1")
31     building=$(grep -oPm1 "(?<=<building>)[^<]+" <<< "$xml")
32     if [[ $building == "false" ]]
33     then
34         false
35     else
36         true
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         true
46     else
47         false
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         true
57     else
58         false
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 ! check_connectivity $module "${urls[$module]}"; then
68             if ! is_deploying $module; then
69                 echo -e "$module failed"
70                 failed_modules+=($module)
71             fi
72         fi
73     done
74     if [ ! -z "$failed_modules" ]; then
75         echo -e "Failed Modules: $failed_modules"
76         false
77     else
78         echo -e "All modules working good"
79         exit 0
80     fi
81 }
82
83 ### Functions related fixes.
84
85 function restart_docker_fix() {
86     echo -e "Running restart_docker_fix"
87     service docker restart
88     start_containers_fix "${modules[@]}"
89 }
90
91 function docker_proxy_fix() {
92     echo -e "Running docker_proxy_fix"
93     fix_modules=("${@}")
94     for module in "${fix_modules[@]}"
95     do
96         echo -e "Kill docker proxy and restart containers"
97         pid=$(netstat -nlp | grep :${ports[$module]} | awk '{print $7}' | cut -d'/' -f1)
98         echo $pid
99         if [ ! -z "$pid" ]; then
100             kill $pid
101             start_container_fix $module
102         fi
103     done
104 }
105
106 function start_containers_fix() {
107     start_modules=("${@}")
108     for module in "${start_modules[@]}"
109     do
110         start_container_fix $module
111     done
112 }
113
114 function start_container_fix() {
115     echo -e "Starting a container $module"
116     sudo docker restart $module
117     sleep 5
118     if ! check_connectivity $module "${urls[$module]}"; then
119         echo -e "Starting an old container $module_old"
120         sudo docker restart $module"_old"
121         sleep 5
122     fi
123 }
124
125 ### Main Flow
126
127 echo -e
128 echo -e "WatchDog Started"
129 echo -e
130 echo -e `date "+%Y-%m-%d %H:%M:%S.%N"`
131 echo -e
132
133 ## If the problem is related to docker daemon
134
135 if ! get_docker_status; then
136     restart_docker_fix
137     if ! check_modules; then
138         echo -e "Watchdog failed while restart_docker_fix"
139     fi
140     exit
141 fi
142
143 ## If the problem is related to docker proxy
144
145 if ! check_modules; then
146     docker_proxy_fix "${failed_modules[@]}"
147 fi
148
149 ## If any other problem : restart docker
150
151 if ! check_modules; then
152     restart_docker_fix
153 fi
154
155 ## If nothing works out
156
157 if ! check_modules; then
158     echo -e "Watchdog failed"
159 fi
160
161 sudo docker ps
162 sudo docker images