~ibmcharmers/charms/trusty/layer-ibm-mobilefirst-server/devel

« back to all changes in this revision

Viewing changes to reactive/ibm-db2.sh

  • Committer: Suchitra Venugopal
  • Date: 2016-05-06 08:43:10 UTC
  • Revision ID: suchvenu@in.ibm.com-20160506084310-8gifuiy16r1slo8i
DB2 Layered charm

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
cfgpasswd=""
18
18
dbs_created=""
19
19
 
 
20
isdbcreated=0
20
21
#Get the cfguser name based on the remote unit name
21
22
get_cfgusername()
22
23
{
266
267
        instance_exists=0
267
268
        db2_inst=`is_db2_installed`
268
269
        get_cfgusername $servicename 
269
 
        #juju-log "Suchitra: cfg user name in create user is $cfgusername"
270
270
        if  [ $db2_inst == True ]; then
271
271
                status-set maintenance "Creating Users"
272
272
                #Find the group id for db2inst1 user which is used to install db2 and find all
348
348
                #Start DB for the new user
349
349
                if su - $cfgusername -c 'ps -eaf|grep -i "\bdb2sysc 0\b" |grep -i '$cfgusername;
350
350
                then
351
 
                        juju-log "IBM DB2: Db2 already started for the user $cfgusername"
 
351
                        juju-log "IBM DB2: DB2 already started for the user $cfgusername"
352
352
                else
353
353
                        su - $cfgusername -c 'db2start'
354
354
                        juju-log "IBM DB2: DB2 is started"
369
369
        dbcreated="False"
370
370
        #Add the new user
371
371
        get_cfgusername $servicename
372
 
        #juju-log "Suchitra: In create new user cfgname is $cfgusername"
373
372
        useradd -g db2grp1 -G dasadm1 -m $cfgusername
374
373
        if [ $? != 0 ]; then
375
374
                juju-log "IBM DB2: Addition of user $queue2 failed."
383
382
                status-set blocked "Setting the password for user failed, Try again"
384
383
                exit 0
385
384
        fi
386
 
        #Check whether test DB is already existing. If not create it
 
385
        #Check whether test DB already exists. If not create it
387
386
 
388
387
        dbname=`su - $DB2_USER -c 'db2 list db directory' | grep "Database name" | grep -i "\bTEST\b" | cut -d"=" -f2`
389
388
        dbname=`echo $dbname | xargs`
415
414
 
416
415
create_database()
417
416
{
 
417
        isdbcreated=0
418
418
        dbtocreate=$1
419
419
        servicename=$2
420
420
        get_cfgusername $servicename
424
424
        dbname=`echo "$dbname" | awk '{ print tolower($1) }'`
425
425
        dbtocreate=`echo "$dbtocreate" | awk '{ print tolower($1) }'`
426
426
        #check whether dbname is more than 8 chars
427
 
        juju-log "No ofchars ${#dbtocreate}"
428
427
        if [ ${#dbtocreate} -gt 8 ]; then
429
 
                juju-log "Faield to create DB $dbtocreate. DB name should be <= 8 chars"
 
428
                juju-log "IBM DB2: Failed to create DB $dbtocreate. DB name should be <= 8 chars"
430
429
                #status-set blocked "DB creation for $dbtocreate failed as it contains more than 8 chars"
431
430
        else
432
431
                if [ "$dbname" != "$dbtocreate" ]; then
439
438
                        fi
440
439
                        juju-log "IBM DB2: Created DB $dbtocreate"
441
440
                fi
 
441
                isdbcreated=1
442
442
        fi
443
443
}
444
444
 
448
448
servicename=$1
449
449
dbnames=$(relation_call --state=db.ready get_dbnames $servicename) || true
450
450
 
451
 
#If dbnames is null, create a default DB as per service name. Else create create the DBs requested by the related charm
 
451
#If dbnames is null, create a default DB as per service name. Else create the DBs requested by the related charm
452
452
if [ -z "$dbnames" ] ; then
453
 
        juju-log "IBM DB2: Creating a default DB $remoteunitdbname based on remote service name"
454
 
        create_database $remoteunitdbname $servicename
455
 
        dbs_created+=$remoteunitdbname
456
 
        dbs_created+="."
 
453
        juju-log "IBM DB2: Creating a default DB $remoteunitdbname based on remote service name"
 
454
        create_database $remoteunitdbname $servicename
 
455
        if [ $isdbcreated == 1 ] ; then
 
456
                dbs_created+=$remoteunitdbname
 
457
                dbs_created+="."
 
458
        fi
457
459
elif [ "$dbnames" == "None" ]; then
458
 
        juju-log "IBM DB2: DB name caught as None, Creating default DB $remoteunitdbname based on remote service name"
459
 
        create_database $remoteunitdbname $servicename
460
 
        dbs_created+=$remoteunitdbname
461
 
        dbs_created+="."
 
460
        juju-log "IBM DB2: DB name caught as None, Creating default DB $remoteunitdbname based on remote service name"
 
461
        create_database $remoteunitdbname $servicename
 
462
        if [ $isdbcreated == 1 ] ; then
 
463
                dbs_created+=$remoteunitdbname
 
464
                dbs_created+="."
 
465
        fi
462
466
else
463
 
        juju-log "IBM DB2: Creating the DB names as passed by consumer"
464
 
        #parse the $dbnames string to get the names of dbs to be created
 
467
        juju-log "IBM DB2: Creating the DB names as passed by consumer"
 
468
        #parse the $dbnames string to get the names of dbs to be created
465
469
        count=1
466
470
        db_to_create="test"
467
471
        while [ "$db_to_create" != "" ]
468
472
        do
469
 
                #echo "Count is $count"
470
 
                db_to_create=`echo $dbnames | cut -d"," -f$count`
471
 
                juju-log "DB to be cretaed is $db_to_create"
 
473
                db_to_create=`echo $dbnames | cut -d"," -f$count`
472
474
                if [ "$db_to_create" != "" ] ; then
473
475
                        create_database $db_to_create $servicename
474
 
                        dbs_created+=$db_to_create
475
 
                        dbs_created+="."
476
 
                        juju-log "DB concatenated $dbs_created"
 
476
                        if [ $isdbcreated == 1 ] ; then
 
477
                                dbs_created+=$db_to_create
 
478
                                dbs_created+="."
 
479
                        fi
477
480
                fi
478
 
                ((count=count+1))
 
481
                ((count=count+1))
479
482
        done
480
483
 
481
484
fi
 
485
isdbcreated=0
482
486
}
483
487
 
484
488
 
592
596
                        
593
597
                        juju-log "IBM DB2: Checking etc/hosts file."
594
598
                        private_address=`unit-get private-address`
595
 
                        #juju-log "private_address : $private_address"
596
599
                        cd /etc
597
600
                        if grep -q "$private_address" hosts
598
601
                        then
625
628
@when 'ibm-base.license.accepted'
626
629
@when 'db.ready'
627
630
function configure_sshkeys(){
628
 
        #juju-log "************************In db available function ***********"
629
631
        services=$(relation_call --state=db.available services)
630
 
        for service in $services; do
 
632
        for service in $services; do
631
633
                key=$(relation_call --state=db.available get_sshkeys $service) || true
632
634
                juju-log "IBM DB2: Received the ssh key $key from the service $service"
633
635
                
710
712
                else
711
713
                        juju-log "IBM DB2: DB2 is already stopped for the instance $cfgusername"
712
714
                fi
713
 
                #juju-log "Calling dismiss for $service"
714
715
                relation_call --state=db.departed dismiss $service || true
715
716
        done
716
717
        remove_state 'ibm-db2.client.sshconfigured'