Merge "JUnit test case coverage for Condition.java, JsonLock.java, MusicLockState...
[music.git] / distribution / cassandra_job / runcql.sh
1 #! /bin/bash
2 #
3 # ============LICENSE_START==========================================
4 # org.onap.music
5 # ===================================================================
6 #  Copyright (c) 2019 AT&T Intellectual Property
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 #
20 # ============LICENSE_END=============================================
21 # ====================================================================
22
23 if [ -z "$TIMEOUT" ]; then
24     TIMEOUT=10;
25 fi
26 if [ -z "$DELAY" ]; then
27     DELAY=60;
28 fi
29 TO="--request-timeout=$TIMEOUT"
30
31 if [ $CASS_HOSTNAME ]; then
32     echo "Sleeping for $DELAY seconds before running cql";
33     sleep $DELAY;
34     >&2 echo "#############################################"
35     >&2 echo "############## Let run cql's ################"
36     >&2 echo "#############################################"
37     >&2 echo "Current Variables in play"
38     >&2 echo "Default User"
39     >&2 echo "DEF_USER="$DEF_USER
40     >&2 echo "DEF_PASS=***********"
41     >&2 echo "New User"
42     >&2 echo "USERNAME="$USERNAME
43     >&2 echo "PASSWORD=***********"
44     >&2 echo "TIMEOUT="$TIMEOUT
45     >&2 echo "Running cqlsh $TO -u cassandra -p cassandra -e \"describe keyspaces;\" ${CASS_HOSTNAME} ${PORT};"
46     if cqlsh $TO -u cassandra -p cassandra -e "describe keyspaces;" ${CASS_HOSTNAME} ${PORT};
47     then
48         >&2 echo "Cassandra user still avalable, will continue as usual";
49     else
50         CASSFAIL=true
51         >&2 echo "$DEF_USER failed, trying with $USERNAME"
52         if cqlsh $TO -u $USERNAME -p $PASSWORD -e "describe keyspaces;" ${CASS_HOSTNAME} ${PORT};
53         then
54             >&2 echo "Password $USERNAME in play, update Variables"
55             DEF_USER=$USERNAME
56             DEF_PASS=$PASSWORD
57             >&2 echo "DEF_USER="$DEF_USER
58             >&2 echo "DEF_PASS=***********"
59             if cqlsh $TO -u $USERNAME -p $PASSWORD -e "describe keyspaces;" ${CASS_HOSTNAME} ${PORT} | grep admin;
60             then
61                 >&2 echo "Admin table exists, everything looks good"
62                 exit 0;
63             else
64                 >&2 echo "Admin does not exists but password has changed. Continue as usual with proper username set"
65                 >&2 echo "DEF_USER=" $DEF_USER
66             fi
67         else
68             if [ $CASSFAIL ]; then
69                 >&2 echo "$DEF_USER and $USERNAME fail. DB might need to be initialized again. This shouldn't have happend."
70                 exit 1;
71             else
72                 >&2 echo "Continue and as usual"
73             fi
74         fi
75     fi
76     >&2 echo "Running admin.cql file:"
77     >&2 echo "Running cqlsh -u $DEF_USER -p $DEF_PASS -f /cql/admin.cql ${CASS_HOSTNAME} ${PORT}"
78     sleep 1;
79     if cqlsh $TO -u $DEF_USER -p $DEF_PASS -f /cql/admin.cql ${CASS_HOSTNAME} ${PORT};
80     then
81         >&2 echo "Success - admin.cql - Admin keyspace created";
82     else
83         >&2 echo "Failure - admin.cql";
84         exit 0;
85     fi
86     >&2 echo "Running admin_pw.cql file:"
87     >&2 echo "Running cqlsh -u $DEF_USER -p $DEF_PASS -f /cql/admin_pw.cql ${CASS_HOSTNAME} ${PORT}"
88     sleep 1;
89     if cqlsh $TO -u $DEF_USER -p $DEF_PASS -f /cql/admin_pw.cql ${CASS_HOSTNAME} ${PORT};
90     then
91         >&2 echo "Success - admin_pw.cql - Password Changed";
92     else
93         >&2 echo "Failure - admin_pw.cql";
94         exit 0;
95     fi
96
97     >&2 echo "Running Test - look for admin keyspace:"
98     >&2 echo "Running cqlsh -u $USERNAME -p $PASSWORD -e "select * from system_auth.roles;" ${CASS_HOSTNAME} ${PORT}"
99     sleep 1;
100     if cqlsh $TO -u $USERNAME -p $PASSWORD -e "select * from system_auth.roles;" ${CASS_HOSTNAME} ${PORT}
101     then
102         >&2 echo "Success - running test";
103     else
104         >&2 echo "Failure - running test";
105         exit 0;
106     fi
107
108 else
109     >&2 echo "Missing CASS_HOSTNAME";
110     exit 0;
111 fi
112