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