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