Change FQDN Register
[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
7 if [ ! -e /aaf_cmd ]; then
8   ln -s /opt/app/aaf/cass_init/cmd.sh /aaf_cmd
9   chmod u+x /aaf_cmd
10 fi
11
12 function status {
13   if [ -d "$DIR" ]; then
14      echo "$@"
15      echo "$@" > $DIR/aaf_cass
16   fi
17 }
18
19 function wait_start {
20     sleep 10
21     status wait for cassandra to start
22     for CNT in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do
23       if [ -z "$(grep 'listening for CQL clients' /var/log/cassandra/system.log)" ]; then
24         echo "Waiting for Cassandra to start... Sleep 10"
25         sleep 10
26       else
27          break
28       fi
29     done
30 }
31
32 function wait_cql {
33    status wait for keyspace to be initialized
34    for CNT in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do
35      if [ "`/usr/bin/cqlsh -e 'describe keyspaces' | grep authz`" == "" ]; then
36         break
37      else
38         echo "Waiting for Keyspaces to be loaded... Sleep 10"
39         sleep 10
40       fi
41     done
42 }
43
44 function install_cql {
45     wait_start started   
46     # Now, make sure data exists
47     if [ "$(/usr/bin/cqlsh -e 'describe keyspaces' | grep authz)" = "" ]; then
48       status install 
49       echo "Initializing Cassandra DB" 
50       if [ "`/usr/bin/cqlsh -e 'describe keyspaces' | grep authz`" == "" ]; then
51         echo "Docker Installed Basic Cassandra on aaf_cass.  Executing the following "
52         echo "NOTE: This creator provided is only a Single Instance. For more complex Cassandra, create independently"
53         echo ""
54         echo " cd /opt/app/aaf/cass_init"
55         cd /opt/app/aaf/cass_init
56         echo " cqlsh -f keyspace.cql"
57         /usr/bin/cqlsh -f keyspace.cql
58         status keyspace installed
59         echo " cqlsh -f init.cql"
60         /usr/bin/cqlsh -f init.cql
61         status data initialized
62         echo ""
63         echo "The following will give you a temporary identity with which to start working, or emergency"
64         echo " cqlsh -f temp_identity.cql"
65       fi
66     fi
67     status $1
68 }
69
70 function install_onap {
71         install_cql initialized
72
73         # Change date expiring dat files to more recent
74         status Creating ONAP Identities
75         ID_FILE=/opt/app/aaf/cass_init/sample.identities.dat    
76         if [ -e $ID_FILE ]; then
77             DATE=$(date "+%Y-%m-%d %H:%M:%S.000+0000" -d "+6 months")
78             echo $DATE
79             CRED="/opt/app/aaf/cass_init/dats/cred.dat"
80             # Enter for People
81             echo "Default Passwords for Apps"
82             for ID in $(grep '|a|' $ID_FILE | sed -e "s/|.*//"); do
83                if [ "$ID" = "aaf" ]; then
84                   DOMAIN="aaf.osaaf.org";
85                else
86                   DOMAIN="$ID.onap.org";
87                fi
88                unset FIRST
89                for D in ${DOMAIN//./ }; do
90                   if [ -z "$FIRST" ]; then
91                     NS="$D"
92                     FIRST="N"
93                   else
94                     NS="$D.$NS"
95                   fi
96                done
97                echo "$ID@$DOMAIN|2|${DATE}|0xd993c5617486296f1b99d04de31633332b8ba1a550038e23860f9dbf0b2fcf95|Initial ID|$NS|53344|" >> $CRED
98             done
99             
100             # Enter for People
101             for ID in $(grep '|e|' $ID_FILE | sed -e "s/|.*//"); do
102                echo "$ID@people.osaaf.org|2|${DATE}|0xd993c5617486296f1b99d04de31633332b8ba1a550038e23860f9dbf0b2fcf95|Initial ID|org.osaaf.people|53344|" >> $CRED
103             done
104
105             # Change UserRole
106             status Setting up User Roles
107             mv dats/user_role.dat tmp
108             sed "s/\(^.*|\)\(.*|\)\(.*|\)\(.*\)/\1${DATE}|\3\4/" tmp > dats/user_role.dat
109
110             # Remove ID File, which is marker for initializing Creds
111             rm $ID_FILE
112         fi
113       status Pushing data to cassandra
114       bash push.sh
115     status ready
116 }
117
118 case "$1" in
119   start)
120     # start install_cql in background, waiting for process to start
121     install_cql ready &
122
123     # Startup like normal
124     echo "Cassandra Startup"
125     /usr/local/bin/docker-entrypoint.sh 
126   ;;
127   wait)
128     # Wait for initialization.  This can be called from Docker only as a check to make sure it is ready
129     wait_start started 
130
131     # Make sure Keyspace is loaded
132     wait_cql 
133     status ready
134   ;;
135   onap)
136     # start install_onap (which calls install_cql first) in background, waiting for process to start
137     install_onap &
138
139     # Startup like normal
140     echo "Cassandra Startup"
141     /usr/local/bin/docker-entrypoint.sh 
142   ;;
143 esac
144