3d6a1b9ec481285591b802886fd56a76729ca364
[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 OTHER=$1
28 shift
29
30 function status {
31   if [ -d "$DIR" ]; then
32      echo "$@" > $DIR/$APP-$HOSTNAME
33   fi
34 }
35
36
37 function check {
38   if [ -d "$DIR" ]; then
39     if [ -z "$(ls $DIR/$OTHER* 2> /dev/null)" ]; then
40       echo "$DIR/$OTHER does not exist"
41     else 
42       echo "$(cat $DIR/$OTHER*)"
43     fi
44   else 
45     echo "$DIR does not exist"
46   fi
47 }
48
49 function wait {
50   n=0
51   while [ $n -lt 40  ]; do 
52      rv="$(check)"
53      echo "$rv"
54      if [ -z "$(echo $rv | grep "ready")" ]; then
55        (( ++n )) 
56        echo "Sleep 10 (iteration $n)"
57        sleep 10
58      else 
59        echo "$OTHER is $rv"
60        n=10000
61      fi
62   done
63 }
64
65 function start {
66   n=0
67   while [ $n -lt 40  ]; do 
68      rv="$(check)"
69      echo "$OTHER is $rv"
70      if [ -z "$(echo $rv | grep "ready")" ]; then
71        (( ++n )) 
72        echo "Sleep 10 (iteration $n)"
73        sleep 10
74      else 
75        # This is critical.  Until status is literally "ready" in the status directory, no processes will start
76        status ready
77        echo "Starting $@"
78        n=10000
79      fi
80   done
81 }
82
83 case "$OTHER" in
84   sleep)
85     echo "Sleeping $1"
86     status "Sleeping $1"
87     sleep $1
88     shift
89     status "ready"
90     echo "Done"
91     ;;
92   stop) 
93     echo "Removing $DIR/$APP-$HOSTNAME"
94     rm $DIR/$APP-$HOSTNAME
95     ;;
96   wait)
97     OTHER="$1"
98     shift    
99     wait
100     ;;
101   *)
102     echo "App $APP is waiting to start until $OTHER is ready"
103     status "waiting for $OTHER"
104   
105     start
106   ;;
107 esac  
108
109 eval "$@"