4
source charms.reactive.sh
6
ARCHITECTURE=`uname -m`
7
ARCHIVE_DIR=$CHARM_DIR/files/archives
11
DB2_INSTALL_PATH=/opt/ibm/db2/V10.5
12
AUTH_KEY_FILE=/root/.ssh/authorized_keys
16
remoteunit=$cfgusername
20
#Get the cfguser name based on the remote unit name
27
cfgusername=`echo $cfgusername | cut -d"/" -f1`
28
cfgusername=`echo "$cfgusername" | sed -r 's/-//g' | sed -r 's/ibm//g'`
29
cfgusername=`echo "$cfgusername" | awk '{print substr($0,0,5)}'`
30
remoteunit=$cfgusername
31
cfgusername="$cfgusername$usr"
32
cfgpasswd="$cfgusername$passwd"
33
remoteunitdbname="$remoteunit$db"
36
# Check whether DB2 is installed
39
if [ -d $DB2_INSTALL_PATH/bin ]; then
47
# Remove DB2, if installed
50
db2_inst=`is_db2_installed`
51
if [ $db2_inst == True ]; then
52
juju-log "IBM DB2: Removing IBM DB2 software."
53
status-set maintenance "Removing IBM DB2"
55
#finding the DB alias names to be dropped and dropping all Databases in that
56
for queue1 in `su - $DB2_USER -c 'db2ilist'`;
58
juju-log "IBM DB2: Dropping the Databases for the instance $queue1"
59
#if db2 is not running, we need to start it first, otherwise db2 drop command will fail
60
if su - $queue1 -c 'ps -eaf|grep -i "\bdb2sysc 0\b" |grep -i '$queue1;
62
juju-log "IBM DB2: DB2 already started for the user $queue1"
64
su - $queue1 -c 'db2start'
65
juju-log "IBM DB2: DB2 is started"
68
for queue in `su - $queue1 -c 'db2 list db directory | grep "Database alias" | cut -d"=" -f2'`;
70
queue=`echo $queue | xargs`
71
juju-log "IBM DB2: Dropping the DB $queue"
72
su - $queue1 -c 'db2 drop database '$queue
74
juju-log "IBM DB2: Dropped the DB $queue"
76
juju-log "IBM DB2: Dropping the DB $queue failed"
77
status-set blocked "Dropping the DB failed"
81
juju-log "IBM DB2: Stopping the DB for $queue1"
83
if su - $queue1 -c 'ps -eaf|grep -i "\bdb2sysc 0\b" |grep -i '$queue1
85
su - $queue1 -c 'db2 force application all'
86
su - $queue1 -c 'db2stop'
88
juju-log "IBM DB2: Stopped the DB for the instance $queue1"
90
juju-log "IBM DB2: Stopping the DB for $queue1 failed"
91
status-set blocked "Stopping the DB for $queue1 failed"
96
juju-log "IBM DB2: DB2 is already stopped"
101
cd $DB2_INSTALL_PATH/instance
102
juju-log "IBM DB2: Dropping the DAS"
105
juju-log "IBM DB2: Dropped the DAS"
107
juju-log "IBM DB2: Dropping the DAS failed"
108
status-set blocked "Dropping the DAS failed"
112
#delete the instances
113
su - $DB2_USER -c 'db2ilist'
114
for queue1 in `su - $DB2_USER -c 'db2ilist'`;
116
juju-log "IBM DB2: Deleting the instance $queue1"
117
/opt/ibm/db2/V10.5/instance/db2idrop $queue1
119
juju-log "IBM DB2: Deleted the instance $queue1"
121
juju-log "IBM DB2: Deleting the instance $queue1 failed"
122
status-set blocked "Deleting the instance $queue1 failed"
128
#Delete the instance entries from /etc/services file
129
juju-log "IBM DB2: Deleting the entries from /etc/services"
130
sed -i '/db2c_/d' /etc/services
132
juju-log "IBM DB2: Deleted the entries from /etc/services"
134
juju-log "IBM DB2: Deletion of the entries from /etc/services failed"
135
status-set blocked "Deletion of the entries from /etc/services failed"
140
juju-log "IBM DB2: Uninstall "
141
cd /opt/ibm/db2/V10.5/install
145
juju-log "IBM DB2: Uninstall of db2 successful"
147
juju-log "IBM DB2: Uninstall of db2 failed"
148
status-set blocked "IBM DB2 Uninstallation failed"
154
juju-log "IBM DB2: Finished Uninstalling......."
155
status-set blocked "IBM DB2 Uninstalled"
158
juju-log "IBM DB2: DB2 not installed"
163
# Update system configuration after installing DB2
166
juju-log "IBM DB2: Updating system configuration."
167
cd $DB2_INSTALL_PATH/instance
168
su - $DB2_USER -c 'db2set DB2COMM=tcpip'
170
#Start DB for the DB2_user
171
if su - $DB2_USER -c 'ps -eaf|grep -i "\bdb2sysc 0\b" |grep -i '$DB2_USER;
173
juju-log "IBM DB2: DB2 already started for the user $DB2_USER"
175
su - $DB2_USER -c 'db2start'
176
juju-log "IBM DB2: DB2 is started"
179
service_name=`su - $DB2_USER -c 'db2 get dbm cfg|grep -i svce|cut -d"=" -f2'`
180
port_num=`grep $service_name /etc/services | cut -d"/" -f1 | cut -f2`
181
port_num=`echo $port_num | xargs`
182
if su - $DB2_USER -c 'netstat -an | grep '$port_num;
184
juju-log "IBM DB2: DB2 started on port $port_num"
186
juju-log "IBM DB2: DB2 not started on port $port_num"
190
juju-log "IBM DB2: Updated system configuration."
195
configure_dftdbpath()
197
db2_inst=`is_db2_installed`
198
if [ $db2_inst == True ]; then
199
cfgdbpath=`config-get dftdbpath`
200
#If null, assign the default value
201
if [ "cfgdbpath" == "" ]
206
for queue1 in `su - $DB2_USER -c 'db2ilist'`;
208
#if db2 is not running don't do anything
209
if su - $queue1 -c 'ps -eaf|grep -i "\bdb2sysc 0\b" |grep -i '$queue1;
211
su - $queue1 -c 'db2 attach to '$queue1
212
dbpath=`su - $queue1 -c 'db2 get dbm cfg|grep -i DFTDBPATH|cut -d"=" -f2'`
213
cfgdbpath=`echo $cfgdbpath | xargs`
214
dbpath=`echo $dbpath | xargs`
215
if [ "$cfgdbpath" != "$dbpath" ]; then
216
if [ -d "$cfgdbpath" ]; then
217
su - $queue1 -c 'db2 update dbm cfg using DFTDBPATH '$cfgdbpath
219
juju-log "IBM DB2: DFTDBPATH changed to $cfgdbpath"
221
juju-log "IBM DB2: Unable to set DFTDBPATH to $cfgdbpath"
225
juju-log "IBM DB2: Not a valid path. Please check the path and run the command again"
232
juju-log "IBM DB2: DB2 is not installed. Install DB2 to change the dbpath"
236
# Edit the response file for UID
239
rsp_file=db2server_temp.rsp
240
if [ "$ARCHITECTURE" = "x86_64" ]; then
241
orgrsp_file=db2server.rsp
243
orgrsp_file=db2server_ppcle.rsp
245
cp $CHARM_DIR/files/archives/$orgrsp_file $CHARM_DIR/files/archives/$rsp_file
247
db2uid=`id $DB2_USER | cut -d"=" -f2 | cut -d"(" -f1`
248
sed -i s/db2inst1_uid/$db2uid/g $CHARM_DIR/files/archives/$rsp_file
249
sed -i s/db2inst1_gid/$db2uid/g $CHARM_DIR/files/archives/$rsp_file
250
sed -i s/db2inst1_homedir/$DB2_USER/g $CHARM_DIR/files/archives/$rsp_file
251
sed -i s/db2inst1_password/$DB2_USER_PW/g $CHARM_DIR/files/archives/$rsp_file
253
db2fenid=`id db2fenc1 | cut -d"=" -f2 | cut -d"(" -f1`
254
sed -i s/db2fenc1_uid/$db2fenid/g $CHARM_DIR/files/archives/$rsp_file
255
sed -i s/db2fenc1_gid/$db2fenid/g $CHARM_DIR/files/archives/$rsp_file
256
dasuid=`id dasusr1 | cut -d"=" -f2 | cut -d"(" -f1`
257
sed -i s/das_uid/$dasuid/g $CHARM_DIR/files/archives/$rsp_file
258
sed -i s/das_gid/$dasuid/g $CHARM_DIR/files/archives/$rsp_file
267
db2_inst=`is_db2_installed`
268
get_cfgusername $servicename
269
#juju-log "Suchitra: cfg user name in create user is $cfgusername"
270
if [ $db2_inst == True ]; then
271
status-set maintenance "Creating Users"
272
#Find the group id for db2inst1 user which is used to install db2 and find all
273
#users belonging to this user
274
db2inst1grpid=`id $DB2_USER | cut -d" " -f2`
276
#If the user is already available, just change the password.
277
for queue2 in `awk -F':' '{ print $1}' /etc/passwd`;
279
gid=`id $queue2 | cut -d" " -f2`
280
if [ $gid == $db2inst1grpid ]; then
281
if [ $queue2 != "$DB2_USER" ]; then
282
if [ $cfgusername == $queue2 ]; then
283
juju-log "IBM DB2: Changing the password for the user $cfgusername to $cfgpasswd"
284
echo $cfgusername:$cfgpasswd | chpasswd
286
juju-log "IBM DB2: Changed the password for the user $cfgusername to $cfgpasswd"
292
#If not available, add a new user
293
if [ $passwdchanged != 1 ]; then
294
juju-log "IBM DB2: Creating New user"
295
create_newuser $servicename
296
juju-log "IBM DB2: Created New user"
297
juju-log $cfgusername
299
#Creating a new instance for the new user if not existing
301
for queue1 in `su - $DB2_USER -c 'db2ilist'`;
303
if [ $queue1 == $cfgusername ]; then
307
if [ $instance_exists != 1 ]; then
308
juju-log "IBM DB2: Creating new instance for the user"
309
/opt/ibm/db2/V10.5/instance/db2icrt -u $cfgusername $cfgusername
311
juju-log "IBM DB2: Created new instance for the user"
313
juju-log "IBM DB2: Creation of new instance failed"
318
#Do the configuration for the new instance created
322
#Check whether the port number is already in etc/services file, and then add it
324
if grep -q "db2c_$cfgusername" services
326
juju-log "IBM DB2: Services file already updated"
328
while [ $free_port == 0 ]
330
is_free=`netstat -lnp | grep $new_port | cut -d":" -f2 | cut -d" " -f1`
331
if [ "$is_free" != "$new_port" ]; then
334
new_port=$((new_port+1))
337
juju-log "IBM DB2:Updating Services file"
338
echo -e "db2c_$cfgusername $new_port/tcp" >> /etc/services
339
su - $cfgusername -c 'set db2instance=$cfgusername'
340
su - $cfgusername -c 'db2set DB2COMM=tcpip'
341
new_port=`echo $new_port | xargs`
342
#port_num_consumer=$new_port
343
su - $cfgusername -c 'db2 update dbm cfg using svcename '$new_port
344
/opt/ibm/db2/V10.5/instance/db2iupdt $cfgusername
348
#Start DB for the new user
349
if su - $cfgusername -c 'ps -eaf|grep -i "\bdb2sysc 0\b" |grep -i '$cfgusername;
351
juju-log "IBM DB2: Db2 already started for the user $cfgusername"
353
su - $cfgusername -c 'db2start'
354
juju-log "IBM DB2: DB2 is started"
356
#set_state 'db.dbstarted'
359
juju-log "IBM DB2: DB2 is not installed. Install DB2 before creating users"
363
status-set maintenance "Created DB user and Instance"
371
get_cfgusername $servicename
372
#juju-log "Suchitra: In create new user cfgname is $cfgusername"
373
useradd -g db2grp1 -G dasadm1 -m $cfgusername
375
juju-log "IBM DB2: Addition of user $queue2 failed."
376
status-set blocked "Addition of new used failed, Try again"
379
#Set the password for the new user
380
echo $cfgusername:$cfgpasswd | chpasswd
382
juju-log "IBM DB2: Setting password for user $queue2 failed."
383
status-set blocked "Setting the password for user failed, Try again"
386
#Check whether test DB is already existing. If not create it
388
dbname=`su - $DB2_USER -c 'db2 list db directory' | grep "Database name" | grep -i "\bTEST\b" | cut -d"=" -f2`
389
dbname=`echo $dbname | xargs`
390
if [ "$dbname" != "TEST" ]; then
391
juju-log "IBM DB2: Creating DB Test"
392
su - $DB2_USER -c 'db2 create database test'
394
juju-log "IBM DB2: DB creation failed"
395
status-set blocked "DB creation failed, Try again"
398
juju-log "IBM DB2: Created DB Test"
400
#Connect to the DB and grant data access
401
su - $DB2_USER -c 'db2 connect to test;db2 grant dataaccess on database to user '$cfgusername
403
juju-log "IBM DB2: Grant access failed"
404
status-set blocked "Grant dataaccess on database failed, Try again"
407
su - $DB2_USER -c 'db2 connect to test;db2 disconnect test'
409
juju-log "IBM DB2: Disconnect DB failed"
410
status-set blocked "Disconnect DB failed"
420
get_cfgusername $servicename
421
#create the db for $dbnametocreate if it doesn't exist
422
dbname=`su - $cfgusername -c 'db2 list db directory' | grep "Database name" | grep -i "\b$dbtocreate\b" | cut -d"=" -f2`
423
dbname=`echo $dbname | xargs`
424
dbname=`echo "$dbname" | awk '{ print tolower($1) }'`
425
dbtocreate=`echo "$dbtocreate" | awk '{ print tolower($1) }'`
426
#check whether dbname is more than 8 chars
427
juju-log "No ofchars ${#dbtocreate}"
428
if [ ${#dbtocreate} -gt 8 ]; then
429
juju-log "Faield to create DB $dbtocreate. DB name should be <= 8 chars"
430
#status-set blocked "DB creation for $dbtocreate failed as it contains more than 8 chars"
432
if [ "$dbname" != "$dbtocreate" ]; then
433
juju-log "IBM DB2: Creating DB $dbtocreate"
434
su - $cfgusername -c 'db2 create database '$dbtocreate
436
juju-log "IBM DB2: DB creation failed"
437
status-set blocked "DB creation failed, Try again"
440
juju-log "IBM DB2: Created DB $dbtocreate"
448
dbnames=$(relation_call --state=db.ready get_dbnames $servicename) || true
450
#If dbnames is null, create a default DB as per service name. Else create create the DBs requested by the related charm
451
if [ -z "$dbnames" ] ; then
452
juju-log "IBM DB2: Creating a default DB $remoteunitdbname based on remote service name"
453
create_database $remoteunitdbname $servicename
454
elif [ "$dbnames" == "None" ]; then
455
juju-log "IBM DB2: DB name caught as None, Creating default DB $remoteunitdbname based on remote service name"
456
create_database $remoteunitdbname $servicename
458
juju-log "IBM DB2: Creating the DB names as passed by consumer"
459
#parse the $dbnames string to get the names of dbs to be created
460
for queue in `echo $dbnames | cut -d"," -f1`;
462
#create the db for $queue
464
create_database $queue $servicename
465
dbnames=`echo $dbnames | cut -d"," -f$count`
466
if [ `echo $dbnames | grep ","` ]; then
467
juju-log "IBM DB2: Found the next DB to be created"
469
juju-log "IBM DB2: Creating new DB $dbnames"
470
#create DB for last one and then quit
471
create_database $dbnames $servicename
481
@when 'ibm-db2.installed'
482
@when_not 'ibm-base.license.accepted'
483
function uninstall(){
484
juju-log "IBM DB2: Removing IBM DB2 (if installed) as the license agreement is not accepted."
485
status-set maintenance "IBM DB2 is getting uninstalled"
487
remove_state 'ibm-db2.installed'
488
remove_state 'ibm-db2.client.sshconfigured'
489
set_state 'ibm-db2.uninstalled'
490
status-set maintenance "IBM DB2 is uninstalled"
494
#Setting the interface states after uninstall
495
@when 'ibm-db2.uninstalled'
497
function reset_interface_states(){
498
services=$(relation_call --state=db.available services) || true
499
for service in $services; do
500
juju-log "IBM DB2: Resetting states for the service $service"
501
relation_call --state=db.available reset_states $service
504
remove_state 'ibm-db2.uninstalled'
508
@when_not 'ibm-db2.prereqsinstalled'
511
juju-log "IBM DB2: Begin Install."
513
status-set maintenance "Installing pre requisites for IBM DB2"
514
juju-log "IBM DB2: Installing binutils"
515
#apt-get install binutils
516
juju-log "IBM DB2: Installed binutils"
517
if [ "$ARCHITECTURE" != "x86_64" -a "$ARCHITECTURE" != "ppc64le" ]; then
518
juju-log "IBM DB2: Unsupported platform. IBM DB2 installed with this Charm supports only the x86_64 and POWER LE (ppc64le) platforms."
522
if [ "$ARCHITECTURE" == "x86_64" ]; then
523
juju-log "IBM DB2: Installing 32 bit libstdc++.so.6"
525
apt-get install lib32stdc++6 -y
526
juju-log "IBM DB2: Installed 32 bit libstdc++.so.6"
528
if [ "$ARCHITECTURE" == "ppc64le" ]; then
529
juju-log "IBM DB2: Installing libnuma package for POWER LE (ppc64le) platforms."
530
apt-get install libnuma-dev -y
531
#Installing XL compiler runtime packages for Linux on Power as this is one of the prerequisite software/configuration requirements
532
echo "deb http://public.dhe.ibm.com/software/server/POWER/Linux/rte/xlcpp/le/ubuntu $(lsb_release -s -c) main" | tee -a /etc/apt/sources.list.d/ibm-xlcpp-rte.list
534
apt-get install libxlc -y --force-yes
535
juju-log "IBM DB2: Installed libnuma package for POWER LE (ppc64le) platforms."
538
#create the userids required for db2 installation
542
juju-log "IBM DB2: Creating the user ids for db2 installation"
547
useradd -g db2grp1 -G dasadm1 -m $DB2_USER
548
echo $DB2_USER:$DB2_USER_PW | chpasswd
549
useradd -g dasadm1 -G db2grp1 -m dasusr1
550
echo dasusr1:dasusr1 | chpasswd
551
useradd -g db2fgrp1 -m db2fenc1
552
echo db2fenc1:db2fenc1 | chpasswd
554
juju-log "IBM DB2: Created the user ids for db2 installation"
555
status-set maintenance "Installed pre requisites for IBM DB2"
556
set_state 'ibm-db2.prereqsinstalled'
559
@when 'ibm-db2.prereqsinstalled' 'ibm-base.license.accepted' 'ibm-base.curl.resource.fetched'
560
@when_not 'ibm-db2.installed'
562
juju-log "IBM DB2: License accepted"
563
db2_inst=`is_db2_installed`
564
if [[ ! -d $ARCHIVE_DIR ]]; then
567
#Install if download succeeded
568
if [ $db2_inst == False ]; then
569
if [ -f $ARCHIVE_DIR/*.gz ]; then
571
cd $CHARM_DIR/files/archives
572
tar -zxvf $ARCHIVE_DIR/*.gz
574
juju-log "IBM DB2: Unable to extract the DB2 package content. Verify whether the package is corrupt."
575
status-set maintenance "Unable to extract the DB2 package content"
576
#Remove corrupt archive file
577
rm -rf $ARCHIVE_DIR/*.gz
580
if [ "$ARCHITECTURE" = "x86_64" ]; then
581
rsp_file=db2server.rsp
583
rsp_file=db2server_ppcle.rsp
586
cd $CHARM_DIR/files/archives/server*
587
juju-log "IBM DB2: Edited the response file"
589
juju-log "IBM DB2: Checking etc/hosts file."
590
private_address=`unit-get private-address`
591
#juju-log "private_address : $private_address"
593
if grep -q "$private_address" hosts
595
juju-log "IBM DB2: Host file already updated"
597
juju-log "IBM DB2: Updating Host file"
598
echo "$private_address `hostname`" >> /etc/hosts
600
juju-log "IBM DB2: Installing db2 package."
601
status-set maintenance "Installing IBM DB2"
603
./db2setup -r $CHARM_DIR/files/archives/$rsp_file
604
juju-log "IBM DB2: Installation of db2 complete."
607
# Configure system values for Db2
611
set_state 'ibm-db2.installed'
612
remove_state 'ibm-db2.uninstalled'
613
status-set active "IBM DB2 Installed"
616
@when 'config.changed.dftdbpath'
617
function configurevalues(){
621
@when 'ibm-base.license.accepted'
623
function configure_sshkeys(){
624
#juju-log "************************In db available function ***********"
625
services=$(relation_call --state=db.available services)
626
for service in $services; do
627
key=$(relation_call --state=db.available get_sshkeys $service) || true
628
juju-log "IBM DB2: Received the ssh key $key from the service $service"
630
if [ -z "$key" ]; then
631
juju-log "IBM DB2: No data sent yet"
635
# Add the key to the authorized_keys file, if it exists
637
if [ -f $AUTH_KEY_FILE ]; then
638
# Append only if the key is not present
639
juju-log "IBM DB2: Updating the key file"
640
grep "$key" $AUTH_KEY_FILE > /dev/null
642
juju-log "IBM DB2: Updating authorized keys file"
643
echo "$key" >> $AUTH_KEY_FILE
645
juju-log "IBM DB2: Key already present"
648
juju-log "IBM DB2: Creating authorized keys file"
649
echo "$key" > $AUTH_KEY_FILE
653
juju-log "IBM DB2: Completed updating the authorized key"
654
set_state 'ibm-db2.client.sshconfigured'
658
@when 'ibm-db2.installed'
659
@when 'ibm-db2.client.sshconfigured'
660
function create_consumer_dbs(){
661
DB2_INSTALL_PATH=/opt/ibm/db2/V10.5
662
services=$(relation_call --state=db.available services)
663
for service in $services; do
664
#Create the user for the remote unit connected
665
juju-log "IBM DB2: Creating the users for the consumer $service"
668
#Create the DBs after parsing
670
juju-log "IBM DB2: Create DBs completed"
672
get_cfgusername $service
673
service_name=`su - $cfgusername -c 'db2 get dbm cfg|grep -i svce|cut -d"=" -f2'`
674
db2_port=`grep $service_name /etc/services | cut -d"/" -f1 | cut -f2`
675
db2_path="/opt/ibm/db2"
676
hostname=`unit-get private-address`
677
dbusername=$cfgusername
679
db2_instance_name=$cfgusername
681
dbs=$(relation_call --state=db.ready get_dbnames $servicename) || true
683
juju-log "IBM DB2: Sending the DB details to the consumer $service"
684
juju-log "Port number: $db2_port"
685
juju-log "Hostname: $hostname"
686
juju-log "DBusername: $dbusername DBuserpw: $dbuserpw"
687
juju-log "DB Instance name: $db2_instance_name"
688
juju-log "Service: $service_name"
689
juju-log "DBname: $dbs"
691
relation_call --state=db.connected set_db_details $service $db2_path $db2_port $hostname $dbusername $dbuserpw $db2_instance_name || true
697
juju-log "IBM DB2: Stopping the DB when relation is broken"
698
services=$(relation_call --state=db.departed services) || true
699
for service in $services; do
700
juju-log "IBM DB2: Service name is $service"
701
get_cfgusername $service
702
if su - $cfgusername -c 'ps -eaf|grep -i "\bdb2sysc 0\b" |grep -i '$cfgusername
704
su - $cfgusername -c 'db2stop'
705
juju-log "IBM DB2: DB2 stopped for instance $cfgusername"
707
juju-log "IBM DB2: DB2 is already stopped for the instance $cfgusername"
709
#juju-log "Calling dismiss for $service"
710
relation_call --state=db.departed dismiss $service || true
712
remove_state 'ibm-db2.client.sshconfigured'
716
reactive_handler_main