~ibmcharmers/charms/xenial/layer-ibm-was-base/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
#!/bin/bash
set -ex

source charms.reactive.sh

reactive_states=`charms.reactive -y get_states`

WAS_BASE_INSTALL_PATH=/root/IBM/WebSphere/AppServer/V85/BASE
IM_INSTALL_PATH=/opt/IBM/InstallationManager
ARCHITECTURE=`uname -m`
profile_name=`config-get profile_name`
was_admin_name=`config-get was_admin_user`
old_user_name=""
old_password=""
old_profile_path=""
was_admin_pw=`config-get was_admin_pw`
profilepath=`config-get profile_path`
profile_template_path="$WAS_BASE_INSTALL_PATH/profileTemplates/default"
ihsadminuser="usr"
ihsadminpw=`pwgen -N 1 15`
ihsadmingrp="grp"

if [ "$ARCHITECTURE" != "x86_64" -a  "$ARCHITECTURE" != "ppc64le" -a "$ARCHITECTURE" != "s390x" ]; then
	juju-log "IBM WAS Base: Unsupported platform. IBM WAS Base installed with this Charm supports only the x86_64 and ppc64le platforms."
	status-set blocked "IBM WAS Base: Unsupported architecture"
	exit 1
fi

	
if [ -f $CHARM_DIR/files/was_users.txt ]; then
	source $CHARM_DIR/files/was_users.txt
	old_user_name=$user_name
	old_password=$password
	old_profile_path=$profile_path
fi

#Get the cfguser name based on the remote unit name
get_cfgusername()
{
	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)}'`
	ihsadminuser="$cfgusername$ihsadminuser"
	ihsadmingrp="$cfgusername$ihsadmingrp"

}

stop_was_base_server() {
	if [ "$1" != "" -a "$1" != $was_admin_name ]; then
		was_admin_name=$1
	fi
	if [ "$2" != "" -a "$2" != $was_admin_pw ]; then
		was_admin_pw=$2
	fi
	if [ "$3" != "" -a "$3" != "$profilepath" ]; then
		profilepath=$3
	fi
	profile_name=`ls $profilepath`
	profile_name=`basename $profile_name`
	juju-log "IBM WAS Base: profile_nmae===="$profile_name"==was_admin_name==="$was_admin_name"====was_admin_pw=="$was_admin_pw"profilepath====$profilepath"
	if [ -d "$profilepath/$profile_name/bin" ]; then
		cd $profilepath/$profile_name/bin/
		if ! ./stopServer.sh server1 -username $was_admin_name -password $was_admin_pw
		then
			status-set blocked "IBM WAS Base:Error while stoping the server"
			exit 0
		else
			status-set active "IBM WAS Base:Server stopped successfully"
			close-port 9060
			close-port 9043
			remove_state 'ibm-was-base.server.started'
		fi
	else
		juju-log "IBM WAS Base: No server to stop, exiting."
		exit 0
	fi
}

install_was_base_fixpack() {
	if $IM_INSTALL_PATH/eclipse/tools/imcl install com.ibm.websphere.BASE.v90 -acceptLicense -repositories $CHARM_DIR/../resources/WAS_BASE/FP -installationDirectory $WAS_BASE_INSTALL_PATH  -showProgress
	then
		set_state 'ibm-was-base.updated'
		status-set active "IBM WAS Base: WAS Base installation upgraded successfully!"
	else
		status-set blocked "IBM WAS Base: Error while upgrading WAS Base!"
		exit 1
	fi

}

install_java_sdk_fixpack() {
	if $IM_INSTALL_PATH/eclipse/tools/imcl install com.ibm.java.jdk.v8 -acceptLicense -repositories $CHARM_DIR/../resources/IBM_SDK/FP -installationDirectory $WAS_BASE_INSTALL_PATH  -showProgress
	then
		set_state 'ibm-was-base.java.updated'
		status-set active "IBM WAS Base: WAS Base installation upgraded successfully!"
	else
		status-set blocked "IBM WAS Base: Error while upgrading WAS Base!"
		exit 1
	fi

}

@when 'ibm-im.installed'
@when_not 'ibm-was-base.installed'
function install_was_base() {
	juju-log "IBM WAS Base: Checking etc/hosts file."
	private_address=`unit-get private-address`
	juju-log "IBM WAS Base:private_address : $private_address"
	cd /etc
	if grep -q "$private_address" hosts
	then
		juju-log "IBM WAS Base: Host file already updated"
	else
		juju-log "IBM WAS Base: Updating Host file"
		echo "$private_address `hostname`" >> /etc/hosts
	fi

	# Get the installable WAS Base resources
	juju-log "IBM WAS Base: fetching the ibm_was_base_installer resource"
	status-set maintenance "fetching the ibm_was_base_installer resource"
	cfg_was_base_pkg_name=`resource-get 'ibm_was_base_installer' || echo unavailable`
	
	juju-log "IBM WAS Base: fetching the ibm_java_sdk resource"
	status-set maintenance "fetching the ibm_java_sdk resource"
	ibm_java_sdk_pkg_name=`resource-get 'ibm_java_sdk_installer' || echo unavailable`

	ibm_was_pkg_isempty=`file $cfg_was_base_pkg_name | { grep -q empty && echo "True"; } || echo "False"`
	ibm_java_sdk_pkg_isempty=`file $ibm_java_sdk_pkg_name | { grep -q empty && echo "True"; } || echo "False"`
		
	# If we don't have a package, report blocked status; we can't proceed.
	if [ "$cfg_was_base_pkg_name" = "unavailable" -o "$ibm_java_sdk_pkg_name" = "unavailable" ]; then
		juju-log "IBM WAS Base: missing required ibm_was_base_installer resource or ibm_java_sdk_installer resource"
		status-set blocked "missing required ibm_was_base_installer resource or ibm_java_sdk_installer resource"
		exit 0
	fi
		
	if [ "$ibm_was_pkg_isempty" = "True" -o "$ibm_java_sdk_pkg_isempty" = "True" ]; then
		juju-log "IBM WAS Base: missing required ibm_was_base_installer resource or ibm_java_sdk_installer resource, found empty packages"
		status-set blocked "IBM WAS Base:empty resource found, Please provide ibm_was_base_installer resource and ibm_java_sdk_installer resources."
		exit 0
	fi
	
	juju-log "IBM WAS Base: using $cfg_was_base_pkg_name $ibm_java_sdk_pkg_name as the resources for this charm."
	was_base_installer="$cfg_was_base_pkg_name" 
	ibm_java_sdk_resources="$ibm_java_sdk_pkg_name"
	
	if [ -f $was_base_installer ]; then
		ARCHIVE_DIR=`dirname $was_base_installer`
		juju-log "IBM WAS Base: extracting the ibm_was_base_installer resource"
		status-set maintenance "extracting the ibm_was_base_installer resource"
		if [ -d $ARCHIVE_DIR/../WAS_BASE -a "$(ls -A $ARCHIVE_DIR/../WAS_BASE)" ]; then
			juju-log "IBM WAS Base: ibm_was_base_installer resources already extracted"
		else
			if [ ! -d $ARCHIVE_DIR/../WAS_BASE ]; then
				mkdir $ARCHIVE_DIR/../WAS_BASE
			fi
			if ! unzip $was_base_installer -d $ARCHIVE_DIR/../WAS_BASE
			then
				juju-log "IBM WAS Base: Unable to extract the WAS Base packages content. Verify whether the package is corrupt."
				# Remove corrupt archive file
				status-set blocked "IBM WAS Base packages are corrupt"
				rm -rf $ARCHIVE_DIR/../WAS_BASE
				exit 0
			else
				juju-log "IBM WAS Base: $was_pkg extracted successfully"
			fi
		fi
	fi

	if [ -f $ibm_java_sdk_resources ]; then
		ARCHIVE_DIR=`dirname $ibm_java_sdk_resources`
		juju-log "IBM WAS Base: extracting the ibm_java_sdk_installer resource"
		status-set maintenance "extracting the ibm_java_sdk_installer resource"
		if [ -d $ARCHIVE_DIR/../IBM_SDK -a "$(ls -A $ARCHIVE_DIR/../IBM_SDK)" ];then
			juju-log "IBM WAS Base: ibm_java_sdk_installer resource already extracted"
		else
			if [ ! -d $ARCHIVE_DIR/../IBM_SDK ]; then
				mkdir $ARCHIVE_DIR/../IBM_SDK
			fi				
			if ! unzip $ibm_java_sdk_resources -d $ARCHIVE_DIR/../IBM_SDK
			then
				juju-log "IBM WAS Base: Unable to extract the IBM Java SDK packages content. Verify whether the package is corrupt."
				# Remove corrupt archive file
				status-set blocked "IBM Java SDK packages are corrupt"
				rm -rf $ARCHIVE_DIR/../IBM_SDK
				exit 0
			else
				juju-log "IBM WAS Base: $sdk_pkg extracted successfully"
			fi
		fi
	fi

	
	juju-log "IBM WAS Base: starting was base installation."
	status-set maintenance "IBM WAS Base: Installing WAS BASE"
	#sh does not work in ubuntu so linking to bash
	juju-log "IBM WAS Base: Unlink /bin/sh"
	unlink /bin/sh
	ln -s /bin/bash /bin/sh
	if $IM_INSTALL_PATH/eclipse/tools/imcl install com.ibm.websphere.BASE.v90, com.ibm.java.jdk.v8 -acceptLicense -repositories $ARCHIVE_DIR/../WAS_BASE,$ARCHIVE_DIR/../IBM_SDK -installationDirectory $WAS_BASE_INSTALL_PATH  -showProgress
	then
		set_state 'ibm-was-base.installed'
		status-set active "IBM WAS Base: WAS Base is installed successfully!"
	else
		status-set blocked "IBM WAS Base: WAS Base Installation failed!"
		exit 1
	fi
}

@when 'ibm-was-base.installed'
@when_not_all 'ibm-was-base.updated' 'ibm-was-base.java.updated'
function update_was_base(){
	# Get the WAS Base fixpack resources
	juju-log "IBM WAS Base: fetching the ibm_was_base_fp resource"
	status-set maintenance "fetching the ibm_was_base_fp resource"
	was_base_fp=`resource-get 'ibm_was_base_fp' || echo unavailable`
		
	juju-log "IBM WAS Base: fetching the ibm_java_sdk_fp resource"
	status-set maintenance "fetching the ibm_java_sdk_fp resource"
	java_sdk_fp=`resource-get 'ibm_java_sdk_fp' || echo unavailable`
	
	#If we don't have a fixpack, just exit successfully; there's nothing to do.
	if [ "$was_base_fp" = "unavailable" -a "$java_sdk_fp" = "unavailable" ]; then
		juju-log "IBM WAS Base: no ibm_was_base_fixpack packages to install"
		status-set active "IBM WAS Base is ready"
		return 0
	fi
	
	#If we detect the fixpack is just a placeholder in charm store, just exit successfully; there's nothing to do.
	ibm_was_fp_isempty=`file $was_base_fp | { grep -q empty && echo "True"; } || echo "False"`
	ibm_java_sdk_fp_isempty=`file $java_sdk_fp | { grep -q empty && echo "True"; } || echo "False"`
	if [ "$ibm_was_fp_isempty" = "True" -a "$ibm_java_sdk_fp_isempty" = "True" ]; then
		juju-log "IBM WAS Base: no ibm_was_base_fixpack packages to install"
		status-set active "IBM WAS Base is ready"
		return 0
	fi
	
	if ! charms.reactive is_state 'ibm-was-base.updated'
	then 
	if [ -f "$was_base_fp"  ]
	then
		was_base_fp_installer="$was_base_fp"
		if [ -f $was_base_fp_installer ]; then
			ARCHIVE_DIR=`dirname $was_base_fp_installer`
			juju-log "IBM WAS Base: extracting the ibm_was_base_fp_installer resource"
			status-set maintenance "extracting the ibm_was_base_fp_installer resource"
			if [ -d $ARCHIVE_DIR/../WAS_BASE/FP -a "$(ls -A $ARCHIVE_DIR/../WAS_BASE/FP)" ]; then
				juju-log "IBM WAS Base: ibm_was_base_fp_installer resources already extracted"
			else
				if [ ! -d $ARCHIVE_DIR/../WAS_BASE/FP ]; then
					mkdir $ARCHIVE_DIR/../WAS_BASE/FP
				fi
				if ! unzip $was_base_fp_installer -d $ARCHIVE_DIR/../WAS_BASE/FP
				then
					juju-log "IBM WAS Base: Unable to extract the WAS Base packages content. Verify whether the package is corrupt."
					# Remove corrupt archive file
					status-set blocked "IBM WAS Base packages are corrupt"
					rm -rf $ARCHIVE_DIR/../WAS_BASE/FP
					exit 0
				else
					juju-log "IBM WAS Base: $was_base_fp extracted successfully"
				fi
			fi
		fi
		if charms.reactive is_state 'ibm-was-base.server.started'; then
			stop_was_base_server $old_user_name $old_password $old_profile_path
		fi		
		install_was_base_fixpack
	fi
	fi
	if ! charms.reactive is_state 'ibm-was-base.java.updated'
	then 
		if [ -f "$java_sdk_fp" ]
		then
		ibm_java_sdk_fp_resource="$java_sdk_fp"
		if [ -f $ibm_java_sdk_fp_resource ]; then
			ARCHIVE_DIR=`dirname $ibm_java_sdk_fp_resource`
			juju-log "IBM WAS Base: extracting the ibm_java_sdk_fp_installer resources"
			status-set maintenance "extracting the ibm_java_sdk_fp_installer resources"
			if [ -d $ARCHIVE_DIR/../IBM_SDK/FP -a "$(ls -A $ARCHIVE_DIR/../IBM_SDK/FP)" ];then
				juju-log "IBM WAS Base: ibm_java_sdk_fp_installer resources already extracted"
			else
				if [ ! -d $ARCHIVE_DIR/../IBM_SDK/FP ]; then
					mkdir $ARCHIVE_DIR/../IBM_SDK/FP
				fi				
				if ! unzip $ibm_java_sdk_fp_resource -d $ARCHIVE_DIR/../IBM_SDK/FP
				then
					juju-log "IBM WAS Base: Unable to extract the IBM Java SDK packages content. Verify whether the package is corrupt."
					# Remove corrupt archive file
					status-set blocked "IBM Java SDK packages are corrupt"
					rm -rf $ARCHIVE_DIR/../IBM_SDK/FP
					exit 0
				else
					juju-log "IBM WAS Base: $sdk_fp_pkg extracted successfully"
				fi
			fi
		fi
		if charms.reactive is_state 'ibm-was-base.server.started'; then
			stop_was_base_server $old_user_name $old_password $old_profile_path
		fi		
		install_java_sdk_fixpack
		fi
	fi
}


@when 'ibm-was-base.installed'
@when_not 'ibm-was-base.profile.available'
function create_profile(){
	$WAS_BASE_INSTALL_PATH/bin/manageprofiles.sh -create -profileName $profile_name -profilePath $profilepath/$profile_name -templatePath $profile_template_path -enableAdminSecurity true -serverName "server1" -adminUserName $was_admin_name -adminPassword $was_admin_pw
	set_state 'ibm-was-base.profile.available'
	if [ ! -d $CHARM_DIR/files ]; then
		mkdir $CHARM_DIR/files
	fi
	if [ -f $CHARM_DIR/files/was_users.txt ]; then
		juju-log "IBM WAS Base: was_users.txt file already exists."
	else
		touch $CHARM_DIR/files/was_users.txt
	fi
	
cat > $CHARM_DIR/files/was_users.txt <<EOF
user_name=$was_admin_name
password=$was_admin_pw
profile_path=$profilepath
EOF
	chmod 600 $CHARM_DIR/files/was_users.txt
}

@when 'ibm-was-base.installed' 'ibm-was-base.profile.available'
@when_not 'ibm-was-base.server.started'
function start_was_base_server() {
	if [ "$old_profile_path" != "" ]; then
		profilepath=$old_profile_path
		profile_name=`ls $old_profile_path`
		profile_name=`basename $profile_name`
	fi
	if [ -d $profilepath/$profile_name/bin ]; then
		cd $profilepath/$profile_name/bin/
		if ! ./startServer.sh server1
		then
			status-set blocked "IBM WAS Base: Error while starting the server"
		else
			status-set active "IBM WAS Base: Server started successfully"
			open-port 9060
			open-port 9043
			set_state 'ibm-was-base.server.started'
		fi
	else
		status-set blocked "IBM WAS Base: No server to start, exiting."
		exit 0
	fi
}

@when_not_all 'config.default.profile_name' 'config.default.was_admin_user' 'config.default.was_admin_pw' 'config.default.profile_path'
@when_any 'config.changed.profile_name' 'config.changed.was_admin_user' 'config.changed.was_admin_pw' 'config.changed.profile_path'
function create_new_profile() {
	
	if  charms.reactive is_state 'was-ihs.available'; then
		status-set blocked "IBM WAS Base: Please remove the relation before creating new profile."
		exit 0
	fi
	if [ "$was_admin_name" =="" -o  "$was_admin_pw" == "" -o "$profilepath" == "" -o "$profile_name" == "" ]; then
		status-set blocked "IBM WAS Base: Please provide the valid values for config options."
		exit 0
	fi
		
	juju-log "IBM WAS Base: old_was_admin_user=$old_user_name, old_was_admin_password=$old_password, old_profile_path=$old_profile_path"
	if [ -d $profilepath -o "$old_profile_path" != "" -a "$old_profile_path" != "$profilepath" ]; then
		if [ "$old_profile_path" != "" -a "$old_profile_path" != "$profilepath" ]; then
			juju-log "IBM WAS Base: old_profile_path===$old_profile_path profilepath=$profilepath"
			old_profile_name=`ls $old_profile_path`
		else
			juju-log "IBM WAS Base: old_profile_path===$old_profile_path profilepath=$profilepath"
			old_profile_name=`ls $profilepath`
		fi
		old_profile_name=`basename $old_profile_name`
		stop_was_base_server $old_user_name $old_password $old_profile_path
		cd /
		$WAS_BASE_INSTALL_PATH/bin/manageprofiles.sh -delete -profileName $old_profile_name
		if [ -d "$old_profile_path" ]; then
			rm -rf $old_profile_path
		else
			juju-log "IBM WAS Base: profilepath to be deleted $profilepath"
			rm -rf $profilepath
		fi
		remove_state 'ibm-was-base.profile.available'
		juju-log "IBM WAS Base: Removed old profile."
	fi
					
}

@hook 'upgrade-charm'
function check_fixpack(){
	# The upgrade-charm hook will fire when a new resource is pushed for this
	# charm. This is a good time to determine if we need to deal with a new
	# fixpack.
	if ! charms.reactive is_state 'ibm-was-base.updated'; then
		# If there is no prior fixpack installed (because ibm-was-base.updated is not
		# set), do nothing since update_was_base_fixpack will handle that case.
		juju-log "IBM WAS Base: no fixpack has been installed; nothing to upgrade."
		return 0
	else
		# If we have a fixpack packges already (because ibm-was-base.updated is set),
		# we should fetch the latest fixpack packages and determine if it is new.
		#  - If it is new, set our states so install_was_base_fixpack is called again
		#  - If it is not new, do nothing
		juju-log "IBM WAS Base: scanning for new fixpacks to install"
		WAS_FP_DIR="$CHARM_DIR/../resources/ibm_was_base_fp"

		CUR_WAS_FP="$WAS_FP_DIR/ibm_was_base_fp.zip"
		if [ -f $CUR_WAS_FP ]; then
			CUR_WAS_FP_MD5=`md5sum "$CUR_WAS_FP" | awk '{print $1}'`
		fi
		
		NEW_WAS_FP=`resource-get 'ibm_was_base_fp' || echo unavailable`
		
		if [ "$NEW_WAS_FP" = "unavailable" ]; then
			juju-log "IBM WAS Base: no fixpacks to install"
		elif [ -f $NEW_WAS_FP ]; then
			NEW_WAS_FP_MD5=`md5sum "$NEW_WAS_FP" | awk '{print $1}'`
		fi
		
		if [ "$CUR_WAS_FP_MD5" != "$NEW_WAS_FP_MD5" ]; then
			juju-log "IBM WAS Base: new fixpack detected ($CUR_WAS_FP with $CUR_WAS_FP_MD5 versus $NEW_WAS_FP with $NEW_WAS_FP_MD5)"
			rm -rf $CHARM_DIR/../resources/WAS_BASE/FP
			remove_state 'ibm-was-base.updated'
		else
			juju-log "IBM WAS Base: no new WAS Base fixpack to install"
		fi
	fi
	
	if ! charms.reactive is_state 'ibm-was-base.java.updated'; then
		# If there is no prior fixpack installed (because ibm-was-base.java.updated is not
		# set), do nothing since update_was_base_fixpack will handle that case.
		juju-log "IBM WAS Base: no Java SDK fixpack has been installed; nothing to upgrade."
		return 0
	else
		# If we have a fixpack packges already (because ibm-was-base.updated is set),
		# we should fetch the latest fixpack packages and determine if it is new.
		#  - If it is new, set our states so install_was_base_fixpack is called again
		#  - If it is not new, do nothing
		juju-log "IBM WAS Base: scanning for new java SDK fixpacks to install"
		IBM_SDK_DIR="$CHARM_DIR/../resources/ibm_java_sdk_fp"

		CUR_JAVA_SDK="$IBM_SDK_PART1_DIR/ibm_java_sdk_fp.zip"
		if [ -f  $CUR_JAVA_SDK ]; then
			CUR_JAVA_SDK_MD5=`md5sum "$CUR_JAVA_SDK" | awk '{print $1}'`
		fi

		NEW_JAVA_SDK=`resource-get 'ibm_java_sdk_fp' || echo unavailable`
		
		if [ "$NEW_JAVA_SDK" = "unavailable" ]; then
			juju-log "IBM WAS Base: no Java SDK fixpacks to install"
		elif [ -f  $NEW_JAVA_SDK ]; then
			NEW_JAVA_SDK_MD5=`md5sum "$NEW_JAVA_SDK" | awk '{print $1}'`
		fi
		
		if [ "$CUR_JAVA_SDK_MD5" != "$NEW_JAVA_SDK_MD5" ]; then
			juju-log "IBM WAS Base: new JAVA SDK fixpack detected ($CUR_JAVA_SDK with $CUR_JAVA_SDK_MD5 versus $NEW_JAVA_SDK with $NEW_JAVA_SDK_MD5)"
			rm -rf $CHARM_DIR/../resources/IBM_SDK/FP
			remove_state 'ibm-was-base.java.updated'
		else
			juju-log "IBM WAS Base: no new Java SDK fixpack to install"
		fi
	fi
}

@when 'ibm-was-base.server.started' 'was-ihs.available'
@when_not 'ibm-was-base.exposed'
function expose_was_base_details(){
	juju-log "IBM WAS Base: ************************In expose_was_base_details function ***********"
	was_path=$WAS_BASE_INSTALL_PATH
	was_port='9060'
	host_name=`unit-get private-address`
	services=$(relation_call --state=was-ihs.available services)
	for service in $services; do
		juju-log "IBM WAS Base: Creating the users for the IHS Admin Server"
		get_cfgusername $service
		juju-log "IBM WAS Base: Created IHS Admin Server Credentials."
		juju-log "IBM WAS Base: Other values are was_path = $was_path hostname = $host_name service = $service ihsadminuser = $ihsadminuser ihsadminpw = $ihsadminpw ihsadmingrp = $ihsadmingrp profilename = $profile_name"
		relation_call --state=was-ihs.available set_was_details $service $was_path $was_port $host_name $ihsadminuser $ihsadmingrp $ihsadminpw $profile_name || true
	done
	set_state 'ibm-was-base.exposed'
}

@when 'was-ihs.available' 'ibm-was-base.server.started'
@when_not 'ibm-was-base.ihs.propagated'
function propagate_ihs(){
	juju-log "IBM WAS Base: ************************In propagate_ihs function ***********"
	services=$(relation_call --state=was-ihs.available services)
	for service in $services; do
		juju-log "IBM WAS Base: Checking IHS Admin Server status."
		ihsserverstatus=$(relation_call --state=was-ihs.available get_isihsstarted $service) || true
		if [ "$ihsadminserverstatus" != "started" ] 
		then
			juju-log "IBM WAS Base: IHS admin server is not yet started."
			continue
		else
			if charms.reactive is_state 'ibm-was-base.server.started'
			then
				stop_was_base_server $was_admin_name $was_admin_pw
				set_state 'ibm-was-base.ihs.propagated'
				juju-log "IBM WAS Base: IHS propagated successfully!"
				status-set active "IHS propagated successfully!"
			else
				juju-log "IBM WAS Base: Error occured while propagating IHS"
				status-set blocked "Error occured while propagating IHS"
			fi
		fi
	done
}

@when 'was-ihs.departed' 
function remove_relation(){
	juju-log "IBM WAS Base:Removing relation from IHS."
	remove_state 'ibm-was-base.exposed'
	stop_was_base_server $was_admin_name $was_admin_pw
	remove_state 'ibm-was-base.ihs.propagated'
	juju-log "IBM WAS Base: Removed relation from IHS"
}

#to pass ssh keys
#TODO:The below functions are written for WAS, DB2 and MQ bundle charm. 
#For WAS Base stand alone deployment, these functions are not required.  
@when 'wasdb.connected'
@when_not 'ibm-was-base.sshconfigured'
function configure_sshkeys_dbs(){
	SSH_PATH=/root/.ssh
	if [ ! -f  $SSH_PATH/id_rsa.pub ]; then
		juju-log "IBM WAS Base: Setting up SSH keys."
		ssh-keygen -t rsa -f $SSH_PATH/id_rsa -N ''
	fi
	key="`cat $SSH_PATH/id_rsa.pub`"
	
	#Pass the DB names to be created to DB2 charm
	dbnames="WASDB"
	juju-log "IBM WAS Base: Key is $key"
	
	juju-log "IBM WAS Base: DB names is $dbnames"
	juju-log "IBM WAS Base: ******* In pass_ssh_keys function in WAS Base charm ***"
	
	# To pass the SSH key and DB names to DB2 charm
	relation_call --state=wasdb.connected set_ssh_keys "$key" || true
	relation_call --state=wasdb.connected set_dbs $dbnames || true
	set_state 'ibm-was-base.sshconfigured'
}

@when 'wasdb.ready'
@when 'ibm-was-base.sshconfigured'
@when_not 'ibm-was-base.db2.available'
function configure_db(){
	juju-log "IBM WAS Base: *******In configure_db reactive function of WAS Base charm*******"
	port=$(relation_call --state=wasdb.connected get_db2_port) || true
	juju-log "IBM WAS Base: I got the port as $port"
	if [ -z "$port" ]; then
		juju-log "IBM WAS Base: No data sent yet"
		return 0
	fi
	db2_instance=$(relation_call --state=wasdb.connected get_db2_instance_name) || true
	juju-log "IBM WAS Base: I got the db2_instance as $db2_instance"
	db2_path=$(relation_call --state=wasdb.available get_db2_path) || true
	hostname=$(relation_call --state=wasdb.available get_db2_hostname) || true
	dbusername=$(relation_call --state=wasdb.available get_dbusername) || true
	dbuserpw=$(relation_call --state=wasdb.available get_dbuserpw) || true
	juju-log "IBM WAS Base: I got the db2 values $db2_path $hostname $dbusername $dbuserpw"
	juju-log "IBM WAS Base: Copying files from remote - Directory: $db2_path Host:$hostname"
	status-set maintenance "Copying db2 jar files from remote DB2 machine"
	scp  -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null root@$hostname:$db2_path/V10.5/java/db2jcc.jar $CHARM_DIR/files || true
	scp  -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null root@$hostname:$db2_path/V10.5/java/db2jcc4.jar $CHARM_DIR/files || true
	scp  -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null root@$hostname:$db2_path/V10.5/java/db2jcc_license_*.jar $CHARM_DIR/files || true
	juju-log "IBM WAS Base: Copied the files to $CHARM_DIR/files"
	status-set maintenance "db2 jar files copied from remote DB2 machine"
	set_state 'ibm-was-base.db2.available'
	
}

@when 'ibm-was-base.db2.available'
@when_not 'wasdb.ready'
function reset_all_states(){
	juju-log "IBM WAS Base: Resetting all states for WAS Base charm"
	remove_state 'ibm-was-base.sshconfigured'
	remove_state 'ibm-was-base.db2.available'
}

@when 'wasdb.departed'
function departed_from_db2(){
	status-set maintenance "Relation broken from db2"
	juju-log "IBM WAS Base: WAS Base cahrm and db2 departed"
}

@when 'wasmessaging.ready'
@when_not 'ibm-was-base.mq.available'
function get_mq_instance_details(){
	juju-log "IBM WAS Base: *******In get_mq_instance_details reactive function of WAS Base charm*******"
	port=$(relation_call --state=wasmessaging.connected get_mq_port) || true
	if [ -z "$port" ]; then
		juju-log "IBM WAS Base: No MQ data sent yet"
		return 0
	fi
	QM_Name=$(relation_call --state=wasmessaging.available get_qm_name) || true
	hostname=$(relation_call --state=wasmessaging.available get_mq_hostname) || true
	Qname=$(relation_call --state=wasmessaging.available get_qname) || true
	status-set active "IBM WAS Base: Got MQ instance details."
	juju-log "IBM WAS Base: values of MQ in WAS Charm port=$port QM_Name=$QM_Name hostname=$hostname Qname=$Qname"
	set_state 'ibm-was-base.mq.available'
}

@when 'ibm-was-base.mq.available'
@when_not 'wasmessaging.ready'
function reset_all_mq_relation_states(){
	juju-log "IBM WAS Base: Resetting all MQ states for WAS Base charm"
	remove_state 'ibm-was-base.mq.available'
}

@when 'wasmessaging.departed'
function departed_from_mq(){
	status-set maintenance "IBM WAS Base: Relation broken from mq"
	juju-log "IBM WAS Base: WAS Base cahrm and mq departed"
}

reactive_handler_main