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