Fix: AAF timing issues
[aaf/authz.git] / auth / auth-cass / cass_init / cmd.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 # Engage normal Cass Init, then check for data installation
23 #
24 DIR="/opt/app/aaf/status"
25 INSTALLED_VERSION=/var/lib/cassandra/AAF_VERSION
26 AAF_INIT_DATA=/var/lib/cassandra/AAF_INIT_DATA
27
28 if [ ! -e /aaf_cmd ]; then
29   ln -s /opt/app/aaf/cass_init/cmd.sh /aaf_cmd
30   chmod u+x /aaf_cmd
31 fi
32
33 # Always need startup status...
34 if [ ! -e "$DIR" ]; then
35   mkdir -p "$DIR"
36   chmod 777 $DIR
37 fi
38
39 function status {
40      echo "$@"
41      echo "$@" > $DIR/aaf-cass
42 }
43
44 function wait_start {
45     sleep 10
46     status wait for cassandra to start
47     for CNT in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do
48       if [ -z "$(grep 'listening for CQL clients' /var/log/cassandra/system.log)" ]; then
49         echo "Waiting for Cassandra to start... Sleep 10"
50         sleep 10
51       else
52          break
53       fi
54     done
55 }
56
57
58 function wait_cql {
59    status wait for keyspace to be initialized
60    for CNT in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do
61      if [ -n "$(/usr/bin/cqlsh -e 'describe keyspaces' | grep authz)"  ]; then
62         break
63      else
64         echo "Waiting for Keyspaces to be loaded... Sleep 10"
65         sleep 10
66       fi
67     done
68 }
69
70 function wait_ready {
71    status wait for cassandra to be fully ready
72    for CNT in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do
73        STATUS="$(cat $DIR/aaf-cass)"
74        if [ "$STATUS" = "ready" ]; then
75         break
76      else
77         echo "Waiting for Start, $STATUS... Sleep 10"
78         sleep 10
79       fi
80     done
81 }
82
83 function install_cql {
84     wait_start started   
85     # Now, make sure data exists
86     if [ ! -e $INSTALLED_VERSION ] && [ -n "$(/usr/bin/cqlsh -e 'describe keyspaces' | grep authz)" ]; then
87       /usr/bin/cqlsh -e 'DROP KEYSPACE authz' 
88     fi
89     if [ -z "`/usr/bin/cqlsh -e 'describe keyspaces' | grep authz`" ]; then
90         status install 
91         echo "Initializing Cassandra DB" 
92         echo "Docker Installed Basic Cassandra on aaf.cass.  Executing the following "
93         echo "NOTE: This creator provided is only a Single Instance. For more complex Cassandra, create independently"
94         echo ""
95         until /usr/bin/cqlsh -e 'describe keyspaces';
96         do
97           echo "Cassandra DB is not up Yet!! Trying in 10 seconds"
98           sleep 10
99         done
100         echo " cd /opt/app/aaf/cass_init"
101         cd /opt/app/aaf/cass_init
102         echo " cqlsh -f keyspace.cql"
103         /usr/bin/cqlsh --request-timeout=60 -f keyspace.cql
104         status keyspace installed
105         echo " cqlsh -f init.cql"
106         /usr/bin/cqlsh --request-timeout=60 -f init.cql
107         status data initialized
108         echo ""
109         echo "The following will give you a temporary identity with which to start working, or emergency"
110         echo " cqlsh -f temp_identity.cql"
111         echo "casablanca" > $INSTALLED_VERSION
112     else 
113       echo "Cassandra DB already includes 'authz' keyspace"
114     fi
115     status $1
116 }
117
118 function install_onap {
119     echo " cd /opt/app/aaf/cass_init"
120     install_cql initialized
121     if [ -e "$AAF_INIT_DATA" ]; then 
122        echo "AAF Data already initialized on this Cassandra"
123     else 
124       status prep data for bootstrapping
125       cd /opt/app/aaf/cass_init
126       status prep data 
127       bash prep.sh
128       status push data to cassandra
129       bash push.sh
130       cd -
131       echo $(date) > $AAF_INIT_DATA
132     fi
133     status ready
134 }
135
136 case "$1" in
137   start)
138     # start install_cql in background, waiting for process to start
139     install_cql ready &
140
141     # Startup like normal
142     echo "Cassandra Startup"
143     exec /usr/local/bin/docker-entrypoint.sh 
144   ;;
145   wait)
146     # Wait for initialization.  This can be called from Docker only as a check to make sure it is ready
147     wait_ready 
148
149   ;;
150   onap)
151     cd /opt/app/aaf/cass_init
152     # start install_onap (which calls install_cql first) in background, waiting for process to start
153     install_onap &
154
155     # Startup like normal
156     echo "Cassandra Startup"
157     if ! cat /etc/cassandra/cassandra.yaml | grep "write_request_timeout_in_ms: 20000"; then
158       sed -i 's/write_request_timeout_in_ms: 2000/write_request_timeout_in_ms: 20000/' /etc/cassandra/cassandra.yaml
159     fi
160     exec /usr/local/bin/docker-entrypoint.sh 
161   ;;
162 esac
163