Make OOM workable with Dublin
[aaf/authz.git] / auth / sample / bin / pod_wait.sh
1 #!/bin/bash
2 #########
3 #  ============LICENSE_START====================================================
4 #  org.onap.aaf
5 #  ===========================================================================
6 #  Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
7 #  ===========================================================================
8 #  Licensed under the Apache License, Version 2.0 (the "License");
9 #  you may not use this file except in compliance with the License.
10 #  You may obtain a copy of the License at
11 #
12 #       http://www.apache.org/licenses/LICENSE-2.0
13 #
14 #  Unless required by applicable law or agreed to in writing, software
15 #  distributed under the License is distributed on an "AS IS" BASIS,
16 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 #  See the License for the specific language governing permissions and
18 #  limitations under the License.
19 #  ============LICENSE_END====================================================
20 #
21 #
22 # A Script for use in Pods... Check for status files, and validate before moving on.
23 #
24 DIR="/opt/app/aaf/status"
25 APP=$1
26 shift
27
28 function status {
29   if [ -d "$DIR" ]; then
30      echo "$@" > $DIR/$APP-$HOSTNAME
31   fi
32 }
33
34
35 function check {
36   if [ -d "$DIR" ]; then
37     if [ -z "$(ls $DIR/$OTHER* 2> /dev/null)" ]; then
38       echo "$DIR/$OTHER does not exist"
39     else 
40       echo "$(cat $DIR/$OTHER*)"
41     fi
42   else 
43     echo "$DIR does not exist"
44   fi
45 }
46
47 function wait {
48   n=0
49   while [ $n -lt 40  ]; do 
50      rv="$(check)"
51      echo "$rv"
52      if [ -z "$(echo $rv | grep "ready")" ]; then
53        (( ++n )) 
54        echo "Sleep 10 (iteration $n)"
55        sleep 10
56      else 
57        echo "$OTHER is $rv"
58        n=10000
59      fi
60   done
61 }
62
63 function wait_nc {
64   n=0
65   while [ $n -lt 40  ]; do 
66      echo "Waiting for Network Access to $@"
67      status "Waiting for Network Access to $1 $2"
68      rv="$(nc -zvw 5 $1 $2 2>&1 | grep -e "[open|succeed]")"
69      echo $rv
70
71      if [[ "$rv" == *open* ]] || [[ "$rv" == *succeeded* ]]; then
72        status "Network Connectable to $1 $2"
73        n=10000
74      else
75        (( ++n )) 
76        echo "Sleep 10 (iteration $n)"
77        sleep 10
78      fi
79   done
80 }
81
82 function start {
83   n=0
84   while [ $n -lt 40  ]; do 
85      rv="$(check)"
86      echo "$OTHER is $rv"
87      if [ -z "$(echo $rv | grep "ready")" ]; then
88        (( ++n )) 
89        echo "Sleep 10 (iteration $n)"
90        sleep 10
91      else 
92        # This is critical.  Until status is literally "ready" in the status directory, no processes will start
93        status ready
94        echo "Starting $@"
95        n=10000
96      fi
97   done
98 }
99
100 while [ ! -z "$1" ]; do
101   OTHER=$1
102   shift
103   case "$OTHER" in
104     nc) 
105       H=$1
106       shift
107       P=$1
108       shift
109       wait_nc "$H" "$P"
110       if [ -z "$@" ]; then
111         echo "ready"
112         status "ready"
113       fi
114       ;;
115     sleep)
116       echo "Sleeping $1"
117       status "Sleeping $1"
118       sleep $1
119       shift
120       if [ -z "$@" ]; then
121         echo "ready"
122         status "ready"
123       fi
124       echo "Done"
125       ;;
126     remove) 
127       echo "Removing $DIR/$APP-$HOSTNAME"
128       rm -f $DIR/$APP-$HOSTNAME
129       ;;
130     wait)
131       OTHER="$1"
132       shift    
133       wait
134       ;;
135     *)
136       echo "App $APP is waiting to start until $OTHER is ready"
137       status "waiting for $OTHER"
138     
139       start
140       break
141     ;;
142   esac  
143 done