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

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
#!/bin/bash
set -ex

source charms.reactive.sh

ARCHITECTURE=`uname -m`
ARCHIVE_DIR=$CHARM_DIR/files/archives
DB2_ARCHIVE_DIR=`pwd`
DB2_USER=db2inst1
DB2_USER_PW=db2inst1
DB2_INSTALL_PATH=/opt/ibm/db2/V10.5
AUTH_KEY_FILE=/root/.ssh/authorized_keys

cfgusername=""

remoteunit=$cfgusername
cfgpasswd=""


#Get the cfguser name based on the remote unit name
get_cfgusername()
{
	usr="usr"
	passwd="123"
	db="db"
	cfgusername=$1
	cfgusername=`echo $cfgusername | cut -d"/" -f1`
	cfgusername=`echo "$cfgusername" | sed -r 's/-//g' | sed -r 's/ibm//g'`
	cfgusername=`echo "$cfgusername" | awk '{print substr($0,0,5)}'`
	remoteunit=$cfgusername
	cfgusername="$cfgusername$usr"
	cfgpasswd="$cfgusername$passwd"
	remoteunitdbname="$remoteunit$db"

}
# Check whether DB2 is installed
is_db2_installed()
{
	if [ -d $DB2_INSTALL_PATH/bin ]; then
		echo "True"
	else
		echo "False"
	fi
}


# Remove DB2, if installed
remove_software()
{
	db2_inst=`is_db2_installed`
	if  [ $db2_inst == True ]; then
		juju-log "IBM DB2: Removing IBM DB2 software."
		status-set maintenance "Removing IBM DB2"

		#finding the DB alias names to be dropped and dropping all Databases in that
		for queue1 in `su - $DB2_USER -c 'db2ilist'`;
		do
			juju-log "IBM DB2: Dropping the Databases for the instance $queue1"
			#if db2 is not running, we need to start it first, otherwise db2 drop command will fail
			if su - $queue1 -c 'ps -eaf|grep -i "\bdb2sysc 0\b" |grep -i '$queue1;
			then
				juju-log "IBM DB2: DB2 already started for the user $queue1"
			else
				su - $queue1 -c 'db2start'
				juju-log "IBM DB2: DB2 is started"
			fi

			for queue in `su - $queue1 -c 'db2 list db directory | grep "Database alias" | cut -d"=" -f2'`;
			do
				queue=`echo $queue | xargs`                     
				juju-log "IBM DB2: Dropping the DB $queue"
				su - $queue1 -c 'db2 drop database '$queue
				if [ $? == 0 ]; then
					juju-log "IBM DB2: Dropped the DB $queue"
				else
					juju-log "IBM DB2: Dropping the DB $queue failed"
					status-set blocked "Dropping the DB failed"
					exit 0
				fi
			done
			juju-log "IBM DB2: Stopping the DB for $queue1"

			if su - $queue1 -c 'ps -eaf|grep -i "\bdb2sysc 0\b" |grep -i '$queue1
			then
				su - $queue1 -c 'db2 force application all'
				su - $queue1 -c 'db2stop'
				if [ $? == 0 ]; then
					juju-log "IBM DB2: Stopped the DB for the instance $queue1"
				else
					juju-log "IBM DB2: Stopping the DB for $queue1 failed"
					status-set blocked "Stopping the DB for $queue1 failed"
					exit 0
				fi

			else
				juju-log "IBM DB2: DB2 is already stopped"
			fi
		done

		#drop the das
		cd $DB2_INSTALL_PATH/instance
		juju-log "IBM DB2: Dropping the DAS"
		./dasdrop
		if [ $? == 0 ]; then
			juju-log "IBM DB2: Dropped the DAS"
		else
			juju-log "IBM DB2: Dropping the DAS failed"
			status-set blocked "Dropping the DAS failed"
			exit 0
		fi

		#delete the instances
		su - $DB2_USER -c 'db2ilist'
		for queue1 in `su - $DB2_USER -c 'db2ilist'`;
		do
			juju-log "IBM DB2: Deleting the instance $queue1"
			/opt/ibm/db2/V10.5/instance/db2idrop $queue1
			if [ $? == 0 ]; then
				juju-log "IBM DB2: Deleted  the instance $queue1"
			else
				juju-log "IBM DB2: Deleting the instance $queue1 failed"
				status-set blocked "Deleting the instance $queue1 failed"
				exit 0
			fi

		done

		#Delete the instance entries from /etc/services file
		juju-log "IBM DB2: Deleting the entries from /etc/services"
		sed -i '/db2c_/d' /etc/services
		if [ $? == 0 ]; then
			juju-log "IBM DB2: Deleted the entries from /etc/services"
		else
			juju-log "IBM DB2: Deletion of the entries from /etc/services failed"
			status-set blocked "Deletion of the entries from /etc/services failed"
			exit 0
		fi

		#uninstall
		juju-log "IBM DB2: Uninstall "
		cd /opt/ibm/db2/V10.5/install
		set +e
		./db2_deinstall -a
		if [ $? == 0 ]; then
			juju-log "IBM DB2: Uninstall of db2 successful"
		else
			juju-log "IBM DB2: Uninstall of db2 failed"
			status-set blocked "IBM DB2 Uninstallation failed"
			exit 0
		fi
		set -e
		cd /opt/ibm
		rm -rf db2
		juju-log "IBM DB2: Finished Uninstalling......."
		status-set blocked "IBM DB2 Uninstalled"

	else
		juju-log "IBM DB2: DB2 not installed"
	fi

}

# Update system configuration after installing DB2
configure_system()
{
	juju-log "IBM DB2: Updating system configuration."
	cd $DB2_INSTALL_PATH/instance
	su - $DB2_USER -c 'db2set DB2COMM=tcpip'

	#Start DB for the DB2_user
	if su - $DB2_USER -c 'ps -eaf|grep -i "\bdb2sysc 0\b" |grep -i '$DB2_USER;
	then
		juju-log "IBM DB2: DB2 already started for the user $DB2_USER"
	else
		su - $DB2_USER -c 'db2start'
		juju-log "IBM DB2: DB2 is started"
	fi

	service_name=`su - $DB2_USER -c 'db2 get dbm cfg|grep -i svce|cut -d"=" -f2'`
	port_num=`grep $service_name /etc/services | cut -d"/" -f1 | cut -f2`
	port_num=`echo $port_num | xargs`
	if su - $DB2_USER -c 'netstat -an | grep '$port_num;
	then
		juju-log "IBM DB2: DB2 started on port $port_num"
	else
		juju-log "IBM DB2: DB2 not started on port $port_num"
		exit 1
	fi

	juju-log "IBM DB2: Updated system configuration."

}

#Configure dftdbpath
configure_dftdbpath()
{
	db2_inst=`is_db2_installed`
	if  [ $db2_inst == True ]; then	
		cfgdbpath=`config-get dftdbpath`
		#If null, assign the default value
		if [ "cfgdbpath" == "" ]
		then	
			cfgdbpath="/tmp"
		fi
		
		for queue1 in `su - $DB2_USER -c 'db2ilist'`;	
		do
			#if db2 is not running don't do anything
			if su - $queue1 -c 'ps -eaf|grep -i "\bdb2sysc 0\b" |grep -i '$queue1;
			then
				su - $queue1 -c 'db2 attach to '$queue1
				dbpath=`su - $queue1 -c 'db2 get dbm cfg|grep -i DFTDBPATH|cut -d"=" -f2'`
				cfgdbpath=`echo $cfgdbpath | xargs`
				dbpath=`echo $dbpath | xargs`
				if  [ "$cfgdbpath" != "$dbpath" ]; then
					if [ -d "$cfgdbpath" ]; then
						su - $queue1 -c 'db2 update dbm cfg using DFTDBPATH '$cfgdbpath
						if [ $? == 0 ]; then
							juju-log "IBM DB2: DFTDBPATH changed to $cfgdbpath"
						else
							juju-log "IBM DB2: Unable to set DFTDBPATH to $cfgdbpath"
							exit 0
						fi
					else
						juju-log "IBM DB2: Not a valid path. Please check the path and run the command again"
						exit 0
					fi
				fi
			fi
		done
	else
		juju-log "IBM DB2: DB2 is not installed. Install DB2 to change the dbpath"
		exit 0	
	fi
}
# Edit the response file for UID
edit_responsefile()
{
	rsp_file=db2server_temp.rsp
	if [ "$ARCHITECTURE" = "x86_64" ]; then
		orgrsp_file=db2server.rsp
	else
		orgrsp_file=db2server_ppcle.rsp
	fi
	cp $CHARM_DIR/files/archives/$orgrsp_file $CHARM_DIR/files/archives/$rsp_file

	db2uid=`id $DB2_USER | cut -d"=" -f2 | cut -d"(" -f1`
	sed -i s/db2inst1_uid/$db2uid/g  $CHARM_DIR/files/archives/$rsp_file
	sed -i s/db2inst1_gid/$db2uid/g  $CHARM_DIR/files/archives/$rsp_file
	sed -i s/db2inst1_homedir/$DB2_USER/g $CHARM_DIR/files/archives/$rsp_file
	sed -i s/db2inst1_password/$DB2_USER_PW/g $CHARM_DIR/files/archives/$rsp_file

	db2fenid=`id db2fenc1 | cut -d"=" -f2 | cut -d"(" -f1`
	sed -i s/db2fenc1_uid/$db2fenid/g  $CHARM_DIR/files/archives/$rsp_file	
	sed -i s/db2fenc1_gid/$db2fenid/g  $CHARM_DIR/files/archives/$rsp_file
	dasuid=`id dasusr1 | cut -d"=" -f2 | cut -d"(" -f1`
	sed -i s/das_uid/$dasuid/g $CHARM_DIR/files/archives/$rsp_file
	sed -i s/das_gid/$dasuid/g $CHARM_DIR/files/archives/$rsp_file

}

create_user()
{
	servicename=$1
	passwdchanged=0
	instance_exists=0
	db2_inst=`is_db2_installed`
	get_cfgusername $servicename 
	#juju-log "Suchitra: cfg user name in create user is $cfgusername"
	if  [ $db2_inst == True ]; then
		status-set maintenance "Creating Users"
		#Find the group id for db2inst1 user which is used to install db2 and find all
		#users belonging to this user
		db2inst1grpid=`id $DB2_USER |  cut -d" " -f2`

		#If the user is already available, just change the password.
		for queue2 in `awk -F':' '{ print $1}' /etc/passwd`;
		do
			gid=`id $queue2 |  cut -d" " -f2`
			if [ $gid == $db2inst1grpid ]; then
				if [ $queue2 != "$DB2_USER" ]; then
					if [ $cfgusername == $queue2  ]; then
						juju-log "IBM DB2: Changing the password for the user $cfgusername to $cfgpasswd"
						echo $cfgusername:$cfgpasswd | chpasswd
						passwdchanged=1
						juju-log "IBM DB2: Changed the password for the user $cfgusername to $cfgpasswd"
					fi

				fi
			fi
		done
		#If not available, add a new user
		if [ $passwdchanged != 1 ]; then
			juju-log "IBM DB2: Creating New user"
			create_newuser $servicename
			juju-log "IBM DB2: Created New user"
			juju-log $cfgusername
		fi
		#Creating a new instance for the new user if not existing

		for queue1 in `su - $DB2_USER -c 'db2ilist'`;
		do
			if [ $queue1 == $cfgusername ]; then
				instance_exists=1
			fi
		done
		if [ $instance_exists != 1 ]; then
			juju-log "IBM DB2: Creating new instance for the user"
			/opt/ibm/db2/V10.5/instance/db2icrt -u $cfgusername $cfgusername
			if [ $? = 0 ]; then
				juju-log "IBM DB2: Created new instance for the user"
			else
				juju-log "IBM DB2: Creation of new instance failed"
				exit 0
			fi
		fi

		#Do the configuration for the new instance created
		new_port=50001
		free_port=0
		is_free=0
		#Check whether the port number is already in etc/services file, and then add it
		cd /etc
		if grep -q "db2c_$cfgusername" services
		then
			juju-log "IBM DB2: Services file already updated"
		else
			while [ $free_port == 0 ]
			do
				is_free=`netstat -lnp | grep $new_port | cut -d":" -f2 | cut -d" " -f1`
				if [ "$is_free" != "$new_port" ]; then
					free_port=1
				else
					new_port=$((new_port+1))
				fi
			done
			juju-log "IBM DB2:Updating Services file"
			echo -e "db2c_$cfgusername	$new_port/tcp" >> /etc/services
			su - $cfgusername -c 'set db2instance=$cfgusername'
			su - $cfgusername -c 'db2set DB2COMM=tcpip'
			new_port=`echo $new_port | xargs`
				#port_num_consumer=$new_port
			su - $cfgusername -c 'db2 update dbm cfg using svcename '$new_port
			/opt/ibm/db2/V10.5/instance/db2iupdt $cfgusername
		fi
		cd -

		#Start DB for the new user
		if su - $cfgusername -c 'ps -eaf|grep -i "\bdb2sysc 0\b" |grep -i '$cfgusername;
		then
			juju-log "IBM DB2: Db2 already started for the user $cfgusername"
		else
			su - $cfgusername -c 'db2start'
			juju-log "IBM DB2: DB2 is started"
		fi
		#set_state 'db.dbstarted'

	else
		juju-log "IBM DB2: DB2 is not installed. Install DB2 before creating users"
		exit 0

	fi
	status-set maintenance "Created DB user and Instance"
}

create_newuser()
{
	servicename=$1
	dbcreated="False"
	#Add the new user
	get_cfgusername $servicename
	#juju-log "Suchitra: In create new user cfgname is $cfgusername"
	useradd -g db2grp1 -G dasadm1 -m $cfgusername
	if [ $? != 0 ]; then
		juju-log "IBM DB2: Addition of user $queue2 failed."
		status-set blocked "Addition of new used failed, Try again"
		exit 0
	fi
	#Set the password for the new user
	echo $cfgusername:$cfgpasswd | chpasswd
	if [ $? != 0 ]; then
		juju-log "IBM DB2: Setting password for user $queue2 failed."
		status-set blocked "Setting the password for user failed, Try again"
		exit 0
	fi
	#Check whether test DB is already existing. If not create it

	dbname=`su - $DB2_USER -c 'db2 list db directory' | grep "Database name" | grep -i "\bTEST\b" | cut -d"=" -f2`
	dbname=`echo $dbname | xargs`
	if [ "$dbname" != "TEST" ]; then
		juju-log "IBM DB2: Creating DB Test"
		su - $DB2_USER -c 'db2 create database test'
		if [ $? != 0 ]; then
			juju-log "IBM DB2: DB creation failed"
			status-set blocked "DB creation failed, Try again"
			exit 0
		fi
		juju-log "IBM DB2: Created DB Test"
	fi
	#Connect to the DB and grant data access
	su - $DB2_USER -c 'db2 connect to test;db2 grant dataaccess on database to user '$cfgusername
	if [ $? != 0 ]; then
		juju-log "IBM DB2: Grant access failed"
		status-set blocked "Grant dataaccess on database failed, Try again"
		exit 0
	fi
	su - $DB2_USER -c 'db2 connect to test;db2 disconnect test'
	if [ $? != 0 ]; then
		juju-log "IBM DB2: Disconnect DB failed"
		status-set blocked "Disconnect DB failed"
		exit 0
	fi

}

create_database()
{
	dbtocreate=$1
	servicename=$2
	get_cfgusername $servicename
	#create the db for $dbnametocreate if it doesn't exist
	dbname=`su - $cfgusername -c 'db2 list db directory' | grep "Database name" | grep -i "\b$dbtocreate\b" | cut -d"=" -f2`
	dbname=`echo $dbname | xargs`
	dbname=`echo "$dbname" | awk '{ print tolower($1) }'`
	dbtocreate=`echo "$dbtocreate" | awk '{ print tolower($1) }'`
	#check whether dbname is more than 8 chars
	juju-log "No ofchars ${#dbtocreate}"
	if [ ${#dbtocreate} -gt 8 ]; then
		juju-log "Faield to create DB $dbtocreate. DB name should be <= 8 chars"
		#status-set blocked "DB creation for $dbtocreate failed as it contains more than 8 chars"
	else
		if [ "$dbname" != "$dbtocreate" ]; then
			juju-log "IBM DB2: Creating DB $dbtocreate"
			su - $cfgusername -c 'db2 create database '$dbtocreate
			if [ $? != 0 ]; then
				juju-log "IBM DB2: DB creation failed"
				status-set blocked "DB creation failed, Try again"
				exit 0
			fi
			juju-log "IBM DB2: Created DB $dbtocreate"
		fi
	fi

}
create_dbs()
{
servicename=$1
dbnames=$(relation_call --state=db.ready get_dbnames $servicename) || true

#If dbnames is null, create a default DB as per service name. Else create create the DBs requested by the related charm
if [ -z "$dbnames" ] ; then
	juju-log "IBM DB2: Creating a default DB $remoteunitdbname based on remote service name"
	create_database $remoteunitdbname $servicename
elif [ "$dbnames" == "None" ]; then
	juju-log "IBM DB2: DB name caught as None, Creating default DB $remoteunitdbname based on remote service name"
	create_database $remoteunitdbname $servicename
else
	juju-log "IBM DB2: Creating the DB names as passed by consumer"
	#parse the $dbnames string to get the names of dbs to be created
	for queue in `echo $dbnames | cut -d"," -f1`;
	do
		#create the db for $queue
		count=2
		create_database $queue $servicename
		dbnames=`echo $dbnames | cut -d"," -f$count`
		if [ `echo $dbnames | grep ","` ]; then
			juju-log "IBM DB2: Found the next DB to be created"
		else
			juju-log "IBM DB2: Creating new DB $dbnames"
			#create DB for last one and then quit
			create_database $dbnames $servicename
			#break
		fi
		count=count+1
	done

fi
}


@when 'ibm-db2.installed'
@when_not 'ibm-base.license.accepted'
function uninstall(){
	juju-log "IBM DB2: Removing IBM DB2 (if installed) as the license agreement is not accepted."
	status-set maintenance "IBM DB2 is getting uninstalled"
	remove_software
	remove_state 'ibm-db2.installed'
	remove_state 'ibm-db2.client.sshconfigured'
	set_state 'ibm-db2.uninstalled'
	status-set maintenance "IBM DB2 is uninstalled"
	
}

#Setting the interface states after uninstall
@when 'ibm-db2.uninstalled'
@when 'db.available'
function reset_interface_states(){
	services=$(relation_call --state=db.available services) || true
	for service in $services; do
		juju-log "IBM DB2: Resetting states for the service $service"
		relation_call --state=db.available reset_states $service
	done

	remove_state 'ibm-db2.uninstalled'
}


@when_not 'ibm-db2.prereqsinstalled'
function init() {
	set -e
	juju-log "IBM DB2: Begin Install."
	apt-get update
	status-set maintenance "Installing pre requisites for IBM DB2"
	juju-log "IBM DB2: Installing binutils"
	#apt-get install binutils
	juju-log "IBM DB2: Installed binutils"
	if [ "$ARCHITECTURE" != "x86_64" -a "$ARCHITECTURE" != "ppc64le" ]; then
		juju-log "IBM DB2: Unsupported platform. IBM DB2 installed with this Charm supports only the x86_64 and POWER LE (ppc64le) platforms."
		exit 1
	fi

	if [ "$ARCHITECTURE" == "x86_64" ]; then
		juju-log "IBM DB2: Installing 32 bit libstdc++.so.6"
		apt-get update
		apt-get install lib32stdc++6 -y
		juju-log "IBM DB2: Installed 32 bit libstdc++.so.6"
	fi
	if [ "$ARCHITECTURE" == "ppc64le" ]; then
		juju-log "IBM DB2:  Installing libnuma package for POWER LE (ppc64le) platforms."
		apt-get install libnuma-dev -y
		#Installing XL compiler runtime packages for Linux on Power as this is one of the prerequisite software/configuration requirements
		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
		apt-get update
		apt-get install libxlc -y --force-yes
		juju-log "IBM DB2:  Installed libnuma package for POWER LE (ppc64le) platforms."
	fi

	#create the userids required for db2 installation
	DB2_USER=db2inst1
	DB2_USER_PW=db2inst1

	juju-log "IBM DB2: Creating the user ids for db2 installation"
	groupadd db2grp1
	groupadd dasadm1
	groupadd db2fgrp1

	useradd -g db2grp1 -G dasadm1 -m $DB2_USER
	echo $DB2_USER:$DB2_USER_PW | chpasswd
	useradd -g dasadm1 -G db2grp1 -m dasusr1
	echo dasusr1:dasusr1 | chpasswd
	useradd -g db2fgrp1 -m db2fenc1
	echo db2fenc1:db2fenc1 | chpasswd

	juju-log "IBM DB2: Created the user ids for db2 installation"
	status-set maintenance "Installed pre requisites for IBM DB2"
	set_state 'ibm-db2.prereqsinstalled'

}
@when 'ibm-db2.prereqsinstalled' 'ibm-base.license.accepted' 'ibm-base.curl.resource.fetched'
@when_not 'ibm-db2.installed'
function install() {
	juju-log "IBM DB2: License accepted"
	db2_inst=`is_db2_installed`
	if [[ ! -d $ARCHIVE_DIR ]]; then
		mkdir $ARCHIVE_DIR
	fi
	#Install if download succeeded
	if  [ $db2_inst == False ]; then
		if [ -f  $ARCHIVE_DIR/*.gz ]; then

			cd $CHARM_DIR/files/archives
			tar -zxvf $ARCHIVE_DIR/*.gz
			if [ $? != 0 ]; then
				juju-log "IBM DB2: Unable to extract the DB2 package content. Verify whether the package is corrupt."
				status-set maintenance "Unable to extract the DB2 package content"
				#Remove corrupt archive file
				rm -rf $ARCHIVE_DIR/*.gz
				exit 0
			fi
			if [ "$ARCHITECTURE" = "x86_64" ]; then
				rsp_file=db2server.rsp
			else
				rsp_file=db2server_ppcle.rsp
			fi
			edit_responsefile
			cd $CHARM_DIR/files/archives/server*
			juju-log "IBM DB2: Edited the response file"
			
			juju-log "IBM DB2: Checking etc/hosts file."
			private_address=`unit-get private-address`
			#juju-log "private_address : $private_address"
			cd /etc
			if grep -q "$private_address" hosts
			then
				juju-log "IBM DB2: Host file already updated"
			else
				juju-log "IBM DB2: Updating Host file"
				echo "$private_address `hostname`" >> /etc/hosts
			fi
			juju-log "IBM DB2: Installing db2 package."
			status-set maintenance "Installing IBM DB2"
			cd -
			./db2setup -r $CHARM_DIR/files/archives/$rsp_file
			juju-log "IBM DB2: Installation of db2 complete."
		fi
	fi
	# Configure system values  for Db2
	configure_system
	#Configure DFTDBPATH
	configure_dftdbpath
	set_state 'ibm-db2.installed'
	remove_state 'ibm-db2.uninstalled'
	status-set active "IBM DB2 Installed"
}

@when 'config.changed.dftdbpath'
function configurevalues(){
	configure_dftdbpath
}

@when 'ibm-base.license.accepted'
@when 'db.ready'
function configure_sshkeys(){
	#juju-log "************************In db available function ***********"
	services=$(relation_call --state=db.available services)
        for service in $services; do
		key=$(relation_call --state=db.available get_sshkeys $service) || true
		juju-log "IBM DB2: Received the ssh key $key from the service $service"
		
		if [ -z "$key" ]; then
			juju-log "IBM DB2: No data sent yet"
			continue
		fi

		# Add the key to the authorized_keys file, if it exists
		set +e
		if [ -f $AUTH_KEY_FILE ]; then
			# Append only if the key is not present
			juju-log "IBM DB2: Updating the key file"
			grep "$key" $AUTH_KEY_FILE > /dev/null 
			if [ $? != 0 ]; then
				juju-log "IBM DB2: Updating authorized keys file"
				echo "$key" >> $AUTH_KEY_FILE
			else
				juju-log "IBM DB2: Key already present"
			fi
		else
			juju-log "IBM DB2: Creating authorized keys file"
			echo "$key" > $AUTH_KEY_FILE
		fi
		set -e
	done
	juju-log "IBM DB2: Completed updating the authorized key"
	set_state 'ibm-db2.client.sshconfigured'
}

@when 'db.ready'
@when 'ibm-db2.installed'
@when 'ibm-db2.client.sshconfigured'
function create_consumer_dbs(){
	DB2_INSTALL_PATH=/opt/ibm/db2/V10.5
	services=$(relation_call --state=db.available services)
	for service in $services; do
		#Create the user for the remote unit connected
		juju-log "IBM DB2: Creating the users for the consumer $service"
		create_user $service

		#Create the DBs after parsing
		create_dbs $service
		juju-log "IBM DB2: Create DBs completed"	
	
		get_cfgusername $service
		service_name=`su - $cfgusername -c 'db2 get dbm cfg|grep -i svce|cut -d"=" -f2'`
		db2_port=`grep $service_name /etc/services | cut -d"/" -f1 | cut -f2`
		db2_path="/opt/ibm/db2"
		hostname=`unit-get private-address`
		dbusername=$cfgusername
		dbuserpw=$cfgpasswd
		db2_instance_name=$cfgusername

		dbs=$(relation_call --state=db.ready get_dbnames $servicename) || true

		juju-log "IBM DB2: Sending the DB details to the consumer $service"
		juju-log "Port number: $db2_port"
		juju-log "Hostname: $hostname"
		juju-log "DBusername: $dbusername DBuserpw: $dbuserpw"
		juju-log "DB Instance name: $db2_instance_name"
		juju-log "Service: $service_name"
		juju-log "DBname: $dbs"
		
		relation_call --state=db.connected set_db_details $service $db2_path $db2_port $hostname $dbusername $dbuserpw $db2_instance_name || true
	done
}

@when 'db.departed'
function stop_db2(){
	juju-log "IBM DB2: Stopping the DB when relation is broken"
	services=$(relation_call --state=db.departed services) || true
	for service in $services; do
		juju-log "IBM DB2: Service name is $service"
		get_cfgusername $service
		if su - $cfgusername -c 'ps -eaf|grep -i "\bdb2sysc 0\b" |grep -i '$cfgusername
		then
			su - $cfgusername -c 'db2stop'
			juju-log "IBM DB2: DB2 stopped for instance $cfgusername"
		else
			juju-log "IBM DB2: DB2 is already stopped for the instance $cfgusername"
		fi
		#juju-log "Calling dismiss for $service"
		relation_call --state=db.departed dismiss $service || true
	done
	remove_state 'ibm-db2.client.sshconfigured'
}


reactive_handler_main