~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
#!/bin/bash
set -ex

source charms.reactive.sh

ARCHITECTURE=`uname -m`
ARCHIVE_DIR=$CHARM_DIR/files/archives
AUTH_KEY_FILE=/root/.ssh/authorized_keys
IM_INSTALL_PATH=/opt/IBM/InstallationManager
LIBERTY_INSTALL_PATH=/opt/ibm/wlp
MobileFirstServer_PATH=/opt/ibm/MobileFirst_Platform_Server_liberty


# Create a workight server.
create_worklightserver()
{
if [ -d $LIBERTY_INSTALL_PATH/bin ]; then
	juju-log "IBM MFS: Creating worklight server"
	#Create worklight server
	if [ -d $LIBERTY_INSTALL_PATH/usr/servers/worklight ]; then
		juju-log "IBM MFS: server is already there"
	else
		$LIBERTY_INSTALL_PATH/bin/server create worklight
		juju-log "IBM MFS: created server sucessfully"
		cd $LIBERTY_INSTALL_PATH/usr/servers
		juju-log "IBM MFS: Editing http port to $httpport and https port to $httpsport in server.xml"
		sed -i 's/9080/'$1'/' $LIBERTY_INSTALL_PATH/usr/servers/worklight/server.xml
		sed -i 's/9443/'$2'/' $LIBERTY_INSTALL_PATH/usr/servers/worklight/server.xml
	fi
	return 0
else
	juju-log "IBM MFS: Websphere Liberty is not installed"
	return 1
fi

}
remove_worklightserver()
{
	if [ -d $LIBERTY_INSTALL_PATH/usr/servers/worklight ]; then
		juju-log "IBM MFS: server is already there"
		sudo rm -rf $LIBERTY_INSTALL_PATH/usr/servers/worklight
		juju-log "IBM MFS: Worklight Server deleted"
	fi
}
#Clean the old installations if any
clean_old_installations()
{
	juju-log "Clean old installations if any before installations"
	#Uninstall if already present
	if [ -d $MobileFirstServer_PATH/WorklightServer ]; then
		juju-log "Editing uninstall response file with charmdir"
		sed -i 's|/root/Installfiles/MobileFirst_Platform_Server/disk1|'$CHARM_DIR'/files/archives/MobileFirst_Platform_Server/disk1|g' $CHARM_DIR/files/archives/uninstall-worklight-responsefile.xml
		cd $IM_INSTALL_PATH/eclipse/tools
		sudo ./imcl input $CHARM_DIR/files/archives/uninstall-worklight-responsefile.xml
		rm -rf $MobileFirstServer_PATH
		juju-log "IBM MFS: MobileFirst Server software uninstalled"

	fi


}

get_db_values_from_interface()
{
	db2_port=$(relation_call --state=mobilefirstserverdb.ready get_db2_port) || true
	db2_instance=$(relation_call --state=mobilefirstserverdb.ready get_db2_instance_name) || true
	db2_path=$(relation_call --state=mobilefirstserverdb.ready get_db2_path) || true
	hostname=$(relation_call --state=mobilefirstserverdb.ready get_db2_hostname) || true
	dbusername=$(relation_call --state=mobilefirstserverdb.ready get_dbusername) || true
	dbuserpw=$(relation_call --state=mobilefirstserverdb.ready get_dbuserpw) || true
	dbs_created=$(relation_call --state=mobilefirstserverdb.ready get_db2_dbnames) || true

}

#Edit MobileFirst Server install responsefile with Liberty and db2 values
edit_responsefile()
{
	juju-log "IBM MFS: Editing response file"
	get_db_values_from_interface

	PAK_DIR="$CHARM_DIR/../resources/ibm_mobilefirstserver_installer"
	PAK_NAME="$PAK_DIR/ibm_worklight_installer.zip"
	sed -i 's|/root/IBM/IBMIMShared|/opt/ibm/IBMIMShared|g' $CHARM_DIR/files/archives/install-MFS-responsefile.xml
	sed -i 's|/root/Installfiles/MobileFirst_Platform_Server/disk1|'$PAK_DIR'/MobileFirst_Platform_Server/disk1|g' $CHARM_DIR/files/archives/install-MFS-responsefile.xml
	sed -i 's/APPCNTRA/APPCNTR/' $CHARM_DIR/files/archives/install-MFS-responsefile.xml
	sed -i 's/50000/'$db2_port'/' $CHARM_DIR/files/archives/install-MFS-responsefile.xml
	sed -i 's/db2inst1/'$dbusername'/' $CHARM_DIR/files/archives/install-MFS-responsefile.xml
	sed -i 's/localhost/'$hostname'/' $CHARM_DIR/files/archives/install-MFS-responsefile.xml
	oldpasswd=`cat $CHARM_DIR/files/archives/install-MFS-responsefile.xml | grep "user.database.db2.appcenter.password" | cut -d"=" -f3 | cut -d"/" -f1`
	oldpasswd=`echo $oldpasswd | tr -d "'"`
	sed -i s/$oldpasswd/$dbuserpw/g $CHARM_DIR/files/archives/install-MFS-responsefile.xml
	
	sed -i 's|/opt/ibm/db2/V10.5/java/db2jcc.jar|'$CHARM_DIR'/files/db2jcc4.jar|g' $CHARM_DIR/files/archives/install-MFS-responsefile.xml
	sed -i 's/waserver1/worklight/' $CHARM_DIR/files/archives/install-MFS-responsefile.xml
	sed -i 's|/root/IBM/WebSphere/wlp|'$LIBERTY_INSTALL_PATH'|g' $CHARM_DIR/files/archives/install-MFS-responsefile.xml
	juju-log "IBM MFS:Response file edited"
}

#Edit configure_liberty_db2_file
edit_configure_liberty_db2_file()
{
	juju-log "IBM MFS: Editing configure_liberty_db2.xml file"
	get_db_values_from_interface

	cp $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2-orig.xml $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml

	sed -i '/"database.db2.wladmin.password"/c\<property name="database.db2.wladmin.password" value="'$dbuserpw'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
	sed -i '/"database.db2.worklight.password"/c\<property name="database.db2.worklight.password" value="'$dbuserpw'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
	sed -i '/"database.db2.worklightreports.password"/c\<property name="database.db2.worklightreports.password" value="'$dbuserpw'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
	sed -i '/"worklight.server.install.dir"/c\<property name="worklight.server.install.dir" value="'$MobileFirstServer_PATH'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
	sed -i '/"worklight.contextroot"/c\<property name="worklight.contextroot" value="/worklight"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
	sed -i '/"database.db2.host"/c\<property name="database.db2.host" value="'$hostname'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
	sed -i '/"database.db2.port"/c\<property name="database.db2.port" value="'$db2_port'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
	sed -i '/"database.db2.instance"/c\<property name="database.db2.instance" value="'$db2_instance'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
	sed -i '/"database.db2.driver.dir"/c\<property name="database.db2.driver.dir" value="'$CHARM_DIR'/files"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
	sed -i '/"database.db2.wladmin.dbname"/c\<property name="database.db2.wladmin.dbname" value="WRKLGHT"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
	sed -i '/"database.db2.wladmin.schema"/c\<property name="database.db2.wladmin.schema" value="WLADMIN"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
	sed -i '/"database.db2.wladmin.username"/c\<property name="database.db2.wladmin.username" value="'$dbusername'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
	sed -i '/"database.db2.worklight.dbname"/c\<property name="database.db2.worklight.dbname" value="WRKLGHT"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
	sed -i '/"database.db2.worklight.schema"/c\<property name="database.db2.worklight.schema" value="WRKLGHT"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
	sed -i '/"database.db2.worklight.username"/c\<property name="database.db2.worklight.username" value="'$dbusername'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
	sed -i '/"database.db2.worklightreports.dbname"/c\<property name="database.db2.worklightreports.dbname" value="WRKLGHT"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
	sed -i '/"database.db2.worklightreports.schema"/c\<property name="database.db2.worklightreports.schema" value="WLREPORT"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
	sed -i '/"database.db2.worklightreports.username"/c\<property name="database.db2.worklightreports.username" value="'$dbusername'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
	sed -i '/"appserver.was.installdir"/c\<property name="appserver.was.installdir" value="'$LIBERTY_INSTALL_PATH'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
	sed -i '/"appserver.was85liberty.serverInstance"/c\<property name="appserver.was85liberty.serverInstance" value="worklight"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
	sed -i '/"worklight.project.war.file"/c\<property name="worklight.project.war.file" value="'$CHARM_DIR'/files/archives/test.war"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml

}
update_usename_serverxml_file()
{
	password=`config-get worklight_passwd`
	username=`config-get worklight_user`
	juju-log "IBM MFS: Username is $username, Password is $password"
	
	juju-log "IBM MFS: Editing server.xml file for updated credentials"
	cp $LIBERTY_INSTALL_PATH/usr/servers/worklight/server_org.xml $LIBERTY_INSTALL_PATH/usr/servers/worklight/server.xml

	sed -i '/user name="admin"/c\<user name="'$username'" password="'$password'"/>' $LIBERTY_INSTALL_PATH/usr/servers/worklight/server.xml
	sed -i '/member name="appcenteradmin"/c\<member name="'$username'"/>' $LIBERTY_INSTALL_PATH/usr/servers/worklight/server.xml
}


#Edit create_database_db2_file
edit_create_database_db2_file()
{
	juju-log "IBM MFS: Editing create-database-db2.xml file"
	get_db_values_from_interface


	sed -i '/"database.db2.admin.password"/c\<property name="database.db2.admin.password" value="'$dbuserpw'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
	sed -i '/"database.db2.wladmin.password"/c\<property name="database.db2.wladmin.password" value="'$dbuserpw'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
	sed -i '/"database.db2.worklight.password"/c\<property name="database.db2.worklight.password" value="'$dbuserpw'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
	sed -i '/"database.db2.worklightreports.password"/c\<property name="database.db2.worklightreports.password" value="'$dbuserpw'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
	sed -i '/"worklight.server.install.dir"/c\<property name="worklight.server.install.dir" value="'$MobileFirstServer_PATH'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
	sed -i '/"database.db2.host"/c\<property name="database.db2.host" value="'$hostname'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
	sed -i '/"database.db2.port"/c\<property name="database.db2.port" value="'$db2_port'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
	sed -i '/"database.db2.instance"/c\<property name="database.db2.instance" value="'$db2_instance'"/>'  $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
	sed -i '/"database.db2.driver.dir"/c\<property name="database.db2.driver.dir" value="'$CHARM_DIR'/files"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
	sed -i '/"database.db2.admin.username"/c\<property name="database.db2.admin.username" value="'$dbusername'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
	sed -i '/"database.db2.wladmin.dbname"/c\<property name="database.db2.wladmin.dbname" value="WRKLGHT"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
	sed -i '/"database.db2.wladmin.schema"/c\<property name="database.db2.wladmin.schema" value="WLADMIN"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
	sed -i '/"database.db2.wladmin.username"/c\<property name="database.db2.wladmin.username" value="'$dbusername'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
	sed -i '/"database.db2.worklight.dbname"/c\<property name="database.db2.worklight.dbname" value="WRKLGHT"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
	sed -i '/"database.db2.worklight.schema"/c\<property name="database.db2.worklight.schema" value="WRKLGHT"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
	sed -i '/"database.db2.worklight.username"/c\<property name="database.db2.worklight.username" value="'$dbusername'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
	sed -i '/"database.db2.worklightreports.dbname"/c\<property name="database.db2.worklightreports.dbname" value="WRKLGHT"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
	sed -i '/"database.db2.worklightreports.schema"/c\<property name="database.db2.worklightreports.schema" value="WLREPORT"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
	sed -i '/"database.db2.worklightreports.username"/c\<property name="database.db2.worklightreports.username" value="'$dbusername'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
}

#Backup the configuration files to reuee it later it required.
backup_configuration_files()
{
	cp $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2-orig.xml
	cp $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2-orig.xml
}


#Post install configurations.
postworklightinstall_configuration()
{

	juju-log "IBM MFS: Editing configure-liberty-db2.xml file"
	edit_configure_liberty_db2_file
	juju-log "IBM MFS: Editing create-database-db2.xml file"
	edit_create_database_db2_file

	juju-log "IBM MFS: Running ant commands"
	cd $MobileFirstServer_PATH/WorklightServer/configuration-samples
	#Run create admdatabases command
	ant -f create-database-db2.xml admdatabases
	if [ $? -eq 0 ]; then
		juju-log "IBM MFS: ant create admddatabase command ran successfully"
	else
		juju-log "IBM MFS: ant create admddatabase command failed to run"
		return 1
	fi

	#Run create-database-db2.xml databases command
	ant -f create-database-db2.xml databases
	if [ $? -eq 0 ]; then
		juju-log "IBM MFS: ant create database command ran successfully"
	else
		juju-log "IBM MFS: ant create database command failed to run"
		return 1
	fi

	#Run configure admdatabases command
	ant -f configure-liberty-db2.xml admdatabases
	if [ $? -eq 0 ]; then
		juju-log "IBM MFS: ant configure liberty admddatabase command ran successfully"
	else
		juju-log "IBM MFS: ant configure liberty admddatabase command failed to run"
		return 1
	fi
	#Run configure databases command
	ant -f configure-liberty-db2.xml databases
	if [ $? -eq 0 ]; then
		juju-log "IBM MFS: ant configure liberty database command ran successfully"
	else
		juju-log "IBM MFS: ant configure liberty database command failed to run"
		return 1
	fi

	#Run adminstall command
	ant -f configure-liberty-db2.xml adminstall
	if [ $? -eq 0 ]; then
		juju-log "IBM MFS: ant adminstall command ran successfully"
	else
		juju-log "IBM MFS: ant adminstall command failed to run"
		return 1
	fi

	#Run install command to configure sample runtime environment
	ant -f configure-liberty-db2.xml install
	if [ $? -eq 0 ]; then
		juju-log "IBM MFS: ant configure install command ran successfully"
	else
		juju-log "IBM MFS: ant configure install command failed to run"
		return 1
	fi

}

#Restart liberty server
restart_worklightserver()
{
	if [ -d $LIBERTY_INSTALL_PATH/usr/servers/worklight ]; then
		juju-log "IBM MFS: Restarting server"
		set +e
		$LIBERTY_INSTALL_PATH/bin/server stop worklight
		if [ $? == 0 ]; then
			juju-log "IBM MFS: Websphere Liberty stopped successfully"
		else
			juju-log "IBM MFS: Server not running"
		fi

		$LIBERTY_INSTALL_PATH/bin/server start worklight
		if [ $? == 0 ]; then
			juju-log "IBM MFS: Websphere Liberty started successfully"
			set_state 'ibm-mobilefirst-server.website.started'
		else
			juju-log "IBM MFS: Server is running"
			set_state 'ibm-mobilefirst-server.website.started'
		fi

	fi
	juju-log "IBM MFS: server restarted successfully"

}

@when 'website.available'
@when_not 'ibm-mobilefirst-server.website.configured'
function capture_portdetials(){
	juju-log "IBM MFS: Website is available"
	#Get the port number from WLP and check whether the port is free and can be used. If not free, use the next one. 
	hostname=`relation-get hostname`
	juju-log "IBM MFS: Hostname is $hostname"
	httpport=`relation-get port`
	httpsport=`relation-get httpsport`
	juju-log "IBM MFS: Http post = $httpport , Https port = $httpsport"
	while [ 1 ]
	do
		result=`netstat -an | grep $httpport | cut -d"*" -f1`
		if [ "$result" != "" ]; then
			httpport=$((httpport+1))
		else
			break
		fi
	done;
	while [ 1 ]
	do
		result=`netstat -an | grep $httpsport | cut -d"*" -f1`
		if [ "$result" != "" ]; then
			httpsport=$((httpsport+1))
		else
			break
		fi
	done;
	echo "HttpPort:$httpport" > portdetails.txt
	echo "HttpsPort:$httpsport" >> portdetails.txt

	set_state 'ibm-mobilefirst-server.website.configured'
	
}

@when 'website.available'
@when 'ibm-mobilefirst-server.installed'
@when_not 'ibm-mobilefirst-server.website.started'
function start_worklight_server(){
	if [ -d $LIBERTY_INSTALL_PATH/usr/servers/worklight ]; then

		$LIBERTY_INSTALL_PATH/bin/server start worklight
		if [ $? == 0 ]; then
			juju-log "IBM MFS: Websphere Liberty started successfully"
			set_state 'ibm-mobilefirst-server.website.started'
		fi
	fi
}



@when 'website.available'
@when 'ibm-mobilefirst-server.website.configured'
@when 'ibm-mobilefirst-server.mobilefirstserverdb.configured'
@when_not 'ibm-mobilefirst-server.installed'
function install() {
	juju-log "IBM MFS: License accepted"
	remove_state 'ibm-mobilefirst-server.uninstalled'

	httpport=`cat portdetails.txt | grep "HttpPort" | cut -d":" -f2`
	httpsport=`cat portdetails.txt | grep "HttpsPort" | cut -d":" -f2`
	juju-log "httpport = $httpport $httpsport"
	# Get the installable resource
	juju-log "IBM MFS: Fetching the ibm_mobilefirst-server_installer resource"
	status-set maintenance "fetching the ibm_mobilefirst-server_installer resource"
	cfg_worklight_pkg_name=`resource-get 'ibm_mobilefirstserver_installer' || echo unavailable`
	# If we don't have a package, report blocked status; we can't proceed.
	if [ "$cfg_worklight_pkg_name" = "unavailable" ]; then
		juju-log "IBM MFS: missing required ibm_mobilefirst-server_installer resource"
		status-set blocked "missing required ibm_mobilefirst-server_installer resource"
		exit 0
	fi
	# Check for empty packages from Charm store
	ibm_worklight_empty=`file $cfg_worklight_pkg_name | { grep -q empty && echo "True"; } || echo "False"`
	if [ "$ibm_worklight_empty" = "True" ]; then
		juju-log "IBM MFS: No ibm_worklight package available to install"
		#status-set active "ready"
		status-set blocked "No ibm_worklight package available to install"
		exit 0
	fi

	juju-log "IBM MFS: using $cfg_worklight_pkg_name as the ibm_mobilefirst-server_installer resource"
	PKG_DIR=`dirname $cfg_worklight_pkg_name`
	juju-log "Archive dir is $PKG_DIR"

	cd $PKG_DIR
	status-set maintenance "Extracting IBM MobileFirst Server package"
	rm -rf MobileFirst_Platform_Server*
	unzip $PKG_DIR/*.zip
	if [ $? != 0 ]; then
		juju-log "IBM MFS: Unable to extract the MFS package content. Verify whether the package is corrupt."
		status-set blocked "IBM MobileFirst Server package is corrupt"
		#Remove corrupt archive file
		rm -rf $PKG_DIR/*.zip
		return
	fi
	if [ -d $PKG_DIR/MobileFirst_Platform_Server/disk1 ]; then
		juju-log "IBM MFS: MobileFirst server Packages are available for installation."
		#Create worklight server
		create_worklightserver $httpport $httpsport
		if [ $? -eq 1 ] ; then
			juju-log "IBM MFS: Websphere Liberty is not installed"
			status-set blocked "Websphere Liberty is not installed"
			return 0
		fi
		#Edit response file for installation
		edit_responsefile
		#edit_post_install_file
		juju-log "IBM MFS: Installing MobileFirst Server package."
		status-set maintenance "Installing IBM MobileFirst Server software"
		cd $IM_INSTALL_PATH/eclipse/tools
		./imcl input $CHARM_DIR/files/archives/install-MFS-responsefile.xml -acceptLicense -showProgress
		juju-log "IBM MFS: Installation of MobileFirst server complete."
		status-set active "Installed IBM MobileFirst Server"
		#Check install successful or not
		if [ -d $MobileFirstServer_PATH/WorklightServer ]; then
			juju-log "IBM MFS: Install Sucessful and moving to post install configuration"
			backup_configuration_files
			status-set maintenance "Running Post install configuration steps for IBM MobileFirst Server"
			postworklightinstall_configuration
			if [ $? -eq 1 ]; then
				juju-log "IBM MFS: Post Install Configuration script failed" 
				return 1
			fi
			juju-log "IBM MFS: Post install configuration is done"
			status-set maintenance "Post install configuration is done"
			#Restart worklight server
			restart_worklightserver
			set_state 'ibm-mobilefirst-server.installed'
			#Taking a backup of WLP server.xml file
			cp $LIBERTY_INSTALL_PATH/usr/servers/worklight/server.xml $LIBERTY_INSTALL_PATH/usr/servers/worklight/server_org.xml
			update_usename_serverxml_file
			status-set active "IBM MFS Installed and is Ready"
			open-port $httpport
			open-port $httpsport

		else
			juju-log "IBM MFS: Install failed"
			status-set blocked "IBM MobileFirst Server install failed"
			exit 1
		fi
	else
		juju-log "IBM MFS: MobileFirst server Packages are missing. Please check README file."
		status-set blocked "IBM MobileFirst server Packages are missing"
		return 
	fi
}

@when 'mobilefirstserverdb.connected'
@when_not 'mobilefirstserverdb.ready'
function send_sshkeys_mobilefirstserverdbs(){
	SSH_PATH=/root/.ssh
	if [ ! -f  $SSH_PATH/id_rsa.pub ]; then
		juju-log "IBM MFS: Setting up SSH keys."
		ssh-keygen -t rsa -f $SSH_PATH/id_rsa -N ''
	fi
	key="`cat $SSH_PATH/id_rsa.pub`"
	mobilefirstserverdbnames="APPCNTR,WRKLGHT"
	juju-log "DB names is $mobilefirstserverdbnames"
	# To pass the SSH key and DB names to DB2 charm
	relation_call --state=mobilefirstserverdb.connected set_ssh_keys "$key" || true
	relation_call --state=mobilefirstserverdb.connected set_dbs $mobilefirstserverdbnames || true
}

@when 'mobilefirstserverdb.ready'
@when_not 'ibm-mobilefirst-server.mobilefirstserverdb.configured'
function configure_mobilefirstserverdb(){

	get_db_values_from_interface

	if [ -z "$db2_port" ]; then
		juju-log "No data sent yet"
		return 0
	fi

	#get_db_values_from_interface

	juju-log "IBM MFS: Got the db2 values : $db2_path $hostname $dbusername $dbuserpw $dbs_created"
	juju-log "IBM MFS: Copying files from remote - Directory: $db2_path Host:$hostname"
	status-set maintenance "Copying mobilefirstserverdb2 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 MFS: Copied the files to $CHARM_DIR/files"
	#Updating the passwords for db connection in the required wlp files if its changed and restarting the worklight server. Don't do this if MFS is not installed.
	if [ -d $MobileFirstServer_PATH/WorklightServer ]; then
		#Update the password if changed in server.xml file
		newpassword=`cat $LIBERTY_INSTALL_PATH/usr/servers/worklight/server.xml | grep "WRKLGHT" | grep "WLADMIN" | cut -d"}" -f2 | cut -d" " -f1 | tr -d '"'`
		oldpassword=`cat $LIBERTY_INSTALL_PATH/usr/servers/worklight/server.xml | grep "APPCNTR" | cut -d"}" -f2 | cut -d" " -f1 | tr -d '"'`
		sed -i s/$oldpassword/$newpassword/g $LIBERTY_INSTALL_PATH/usr/servers/worklight/server.xml
		status-set maintenance "Server.xml file updated" 
	fi
	set_state 'ibm-mobilefirst-server.mobilefirstserverdb.configured'

}

@when 'mobilefirstserverdb.departed'
function stop_mobilefirstserverdb2(){
	# If relation with db2 is broken as the DBs related to worklight will be deleted and the login credentilas for the consoles will be lost.
	# Without having the login info from the DB,the user cannot login. The user needs to remove the Mobile First server service and deploy it again.
	juju-log "IBM MFS: Removing the jar files when relation is broken"
	rm -rf $CHARM_DIR/files/db2jcc.jar $CHARM_DIR/files/db2jcc4.jar $CHARM_DIR/files/db2jcc_license_*.jar
	juju-log "IBM MFS: JAR files removed"	
	if [ -d $LIBERTY_INSTALL_PATH/usr/servers/worklight ]; then
		server_status=`ps -ef | grep worklight | grep $LIBERTY_INSTALL_PATH/bin/tools/ws-server.jar | awk '{print $(NF-1),$NF}'`
		if [ "$server_status" == "$LIBERTY_INSTALL_PATH/bin/tools/ws-server.jar worklight" ]; then
			juju-log "IBM MFS: stopping worklight server"
			$LIBERTY_INSTALL_PATH/bin/server stop worklight
			if [ $? == 0 ]; then
				juju-log "IBM MFS: worklight server stopped sucessfully"
				remove_state 'ibm-mobilefirst-server.website.started'
				juju-log "Copying the org server.xml file back in website broken fn"
			else
				juju-log "IBM MFS: Failed to stop worklight server"
				return 0
			fi
		fi
		clean_old_installations
		remove_worklightserver
		remove_state 'ibm-mobilefirst-server.installed'
		remove_state 'ibm-mobilefirst-server.mobilefirstserverdb.configured'
	else
		juju-log "IBM MFS: Worklight server does not exist"
	fi
}


@when 'website.departed'
function stop_worklight_server(){
	#If relation between wlp and worklight is broken( or when MobileFirst Server service is removed) , uninstalling worklight.
	#Otherwise if we set the relation again or deploy MobileFirst server again, the installation goes wrong with traces of old files present
	juju-log "IBM MFS: In website departed function"
	if [ -d $LIBERTY_INSTALL_PATH/usr/servers/worklight ]; then
		server_status=`ps -ef | grep worklight | grep $LIBERTY_INSTALL_PATH/bin/tools/ws-server.jar | awk '{print $(NF-1),$NF}'`
		if [ "$server_status" == "$LIBERTY_INSTALL_PATH/bin/tools/ws-server.jar worklight" ]; then
			juju-log "IBM MFS: stopping worklight server"
			$LIBERTY_INSTALL_PATH/bin/server stop worklight
			if [ $? == 0 ]; then
				juju-log "IBM MFS: worklight server stopped sucessfully"
				remove_state 'ibm-mobilefirst-server.website.started'		
				juju-log "Copying the org server.xml file back in website broken fn"
			else
				juju-log "IBM MFS: Failed to stop worklight server"
				return 0
			fi
		fi
		clean_old_installations
		remove_worklightserver
		remove_state 'ibm-mobilefirst-server.installed'
		remove_state 'ibm-mobilefirst-server.mobilefirstserverdb.configured'
	else
		juju-log "IBM MFS: Worklight server does not exist"
	fi

}

@when_not_all 'config.default.worklight_user' 'config.default.worklight_passwd'
@when_any 'config.changed.worklight_user' 'config.changed.worklight_passwd'
function create_new_userpassword(){
	# Change only if worklight is installed
	if [ -d $LIBERTY_INSTALL_PATH/usr/servers/worklight ]; then
		juju-log "Creating new User/Password"
		status-set active "Updating User Credentials"	
		update_usename_serverxml_file
		status-set active "IBM MFS Updated"
	fi
	

}
reactive_handler_main