aboutsummaryrefslogtreecommitdiffstats
path: root/sdc-os-chef/sdc-cassandra/chef-repo/cookbooks/cassandra-actions/templates/default/create_cassandra_user.sh.erb
blob: 6b972244c253954b1fc0e124cb656c658489a2a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/bash

CASSANDRA_USER=asdc_user
CASSANDRA_PASS=Aa1234%^!
CASSANDRA_IP=<%= @cassandra_ip %>

pass_changed=99
retry_num=1
is_up=0
while [ $is_up -eq 0 -a $retry_num -le 100 ]; do
   echo "exit" | cqlsh -u cassandra -p cassandra $CASSANDRA_IP  > /dev/null 2>&1
   res1=$?
   echo "exit" | cqlsh -u cassandra -p $CASSANDRA_PASS $CASSANDRA_IP  > /dev/null 2>&1
   res2=$?

   if [ $res1 -eq 0 -o $res2 -eq 0 ]; then
      echo "`date` --- cqlsh is enabled to connect."
      is_up=1
      [ $res1 -eq 0 ] && pass_changed=0
      [ $res2 -eq 0 ] && pass_changed=1
   else
      echo "`date` --- cqlsh is NOT enabled to connect yet. sleep 5"
      sleep 5
   fi
   let "retry_num++"
done


echo "pass_changed=[$pass_changed]"
case $pass_changed in
   0)
        cassandra_user_exist=`echo "list users;" | cqlsh -u cassandra -p cassandra $CASSANDRA_IP  |grep -c $CASSANDRA_USER`
        if [ $cassandra_user_exist -eq 1 ] ; then
                echo "cassandra user $CASSANDRA_USER already exist"
                                echo "alter user $CASSANDRA_USER with password '$CASSANDRA_PASS' nosuperuser;" | cqlsh -u cassandra -p cassandra $CASSANDRA_IP 
        else
                echo "Going to create $CASSANDRA_USER"
                echo "create user $CASSANDRA_USER with password '$CASSANDRA_PASS' nosuperuser;" | cqlsh -u cassandra -p cassandra $CASSANDRA_IP 
        fi
        echo "Modify cassandra password"
        echo "ALTER USER cassandra WITH PASSWORD '$CASSANDRA_PASS';" | cqlsh -u cassandra -p cassandra $CASSANDRA_IP 
   ;;
   1)
        cassandra_user_exist=`echo "list users;" | cqlsh -u cassandra -p $CASSANDRA_PASS $CASSANDRA_IP  |grep -c $CASSANDRA_USER`
        if [ $cassandra_user_exist -eq 1 ] ; then
                echo "cassandra user $CASSANDRA_USER already exist"
                                echo "alter user $CASSANDRA_USER with password '$CASSANDRA_PASS' nosuperuser;" | cqlsh -u cassandra -p $CASSANDRA_PASS $CASSANDRA_IP 
        else
                echo "Going to create $CASSANDRA_USER"
                echo "create user $CASSANDRA_USER with password '$CASSANDRA_PASS' nosuperuser;" | cqlsh -u cassandra -p $CASSANDRA_PASS $CASSANDRA_IP 
        fi
    ;;
    *)
       echo "pass_changed doen't have value"
    ;;
esac