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

« back to all changes in this revision

Viewing changes to reactive/ibm-mobilefirst-server.sh

  • Committer: Suchitra Venugopal
  • Date: 2016-09-06 09:48:53 UTC
  • Revision ID: suchvenu@in.ibm.com-20160906094853-1n09myeisek096nm
IBM MobileFirst Server

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/bash
 
2
set -ex
 
3
 
 
4
source charms.reactive.sh
 
5
 
 
6
ARCHITECTURE=`uname -m`
 
7
ARCHIVE_DIR=$CHARM_DIR/files/archives
 
8
AUTH_KEY_FILE=/root/.ssh/authorized_keys
 
9
IM_INSTALL_PATH=/opt/IBM/InstallationManager
 
10
LIBERTY_INSTALL_PATH=/opt/ibm/wlp
 
11
MobileFirstServer_PATH=/opt/ibm/MobileFirst_Platform_Server_liberty
 
12
 
 
13
 
 
14
# Create a workight server.
 
15
create_worklightserver()
 
16
{
 
17
if [ -d $LIBERTY_INSTALL_PATH/bin ]; then
 
18
        juju-log "IBM MFS: Creating worklight server"
 
19
        #Create worklight server
 
20
        if [ -d $LIBERTY_INSTALL_PATH/usr/servers/worklight ]; then
 
21
                juju-log "IBM MFS: server is already there"
 
22
        else
 
23
                $LIBERTY_INSTALL_PATH/bin/server create worklight
 
24
                juju-log "IBM MFS: created server sucessfully"
 
25
                cd $LIBERTY_INSTALL_PATH/usr/servers
 
26
                juju-log "IBM MFS: Editing http port to $httpport and https port to $httpsport in server.xml"
 
27
                sed -i 's/9080/'$1'/' $LIBERTY_INSTALL_PATH/usr/servers/worklight/server.xml
 
28
                sed -i 's/9443/'$2'/' $LIBERTY_INSTALL_PATH/usr/servers/worklight/server.xml
 
29
        fi
 
30
else
 
31
        juju-log "IBM MFS: Websphere Liberty is not installed"
 
32
        exit 0
 
33
fi
 
34
 
 
35
}
 
36
remove_worklightserver()
 
37
{
 
38
        if [ -d $LIBERTY_INSTALL_PATH/usr/servers/worklight ]; then
 
39
                juju-log "IBM MFS: server is already there"
 
40
                sudo rm -rf $LIBERTY_INSTALL_PATH/usr/servers/worklight
 
41
                juju-log "IBM MFS: Worklight Server deleted"
 
42
        fi
 
43
}
 
44
#Clean the old installations if any
 
45
clean_old_installations()
 
46
{
 
47
        juju-log "Clean old installations if any before installations"
 
48
        #Uninstall if already present
 
49
        if [ -d $MobileFirstServer_PATH/WorklightServer ]; then
 
50
                juju-log "Editing uninstall response file with charmdir"
 
51
                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
 
52
                cd $IM_INSTALL_PATH/eclipse/tools
 
53
                sudo ./imcl input $CHARM_DIR/files/archives/uninstall-worklight-responsefile.xml
 
54
                rm -rf $MobileFirstServer_PATH
 
55
                juju-log "IBM MFS: MobileFirst Server software uninstalled"
 
56
 
 
57
        fi
 
58
 
 
59
 
 
60
}
 
61
 
 
62
get_db_values_from_interface()
 
63
{
 
64
        db2_port=$(relation_call --state=mobilefirstserverdb.ready get_db2_port) || true
 
65
        db2_instance=$(relation_call --state=mobilefirstserverdb.ready get_db2_instance_name) || true
 
66
        db2_path=$(relation_call --state=mobilefirstserverdb.ready get_db2_path) || true
 
67
        hostname=$(relation_call --state=mobilefirstserverdb.ready get_db2_hostname) || true
 
68
        dbusername=$(relation_call --state=mobilefirstserverdb.ready get_dbusername) || true
 
69
        dbuserpw=$(relation_call --state=mobilefirstserverdb.ready get_dbuserpw) || true
 
70
        dbs_created=$(relation_call --state=mobilefirstserverdb.ready get_db2_dbnames) || true
 
71
 
 
72
}
 
73
 
 
74
#Edit MobileFirst Server install responsefile with Liberty and db2 values
 
75
edit_responsefile()
 
76
{
 
77
        juju-log "IBM MFS: Editing response file"
 
78
        get_db_values_from_interface
 
79
 
 
80
        PAK_DIR="$CHARM_DIR/../resources/ibm_mobilefirstserver_installer"
 
81
        PAK_NAME="$PAK_DIR/ibm_worklight_installer.zip"
 
82
        sed -i 's|/root/IBM/IBMIMShared|/opt/ibm/IBMIMShared|g' $CHARM_DIR/files/archives/install-MFS-responsefile.xml
 
83
        sed -i 's|/root/Installfiles/MobileFirst_Platform_Server/disk1|'$PAK_DIR'/MobileFirst_Platform_Server/disk1|g' $CHARM_DIR/files/archives/install-MFS-responsefile.xml
 
84
        sed -i 's/APPCNTRA/APPCNTR/' $CHARM_DIR/files/archives/install-MFS-responsefile.xml
 
85
        sed -i 's/50000/'$db2_port'/' $CHARM_DIR/files/archives/install-MFS-responsefile.xml
 
86
        sed -i 's/db2inst1/'$dbusername'/' $CHARM_DIR/files/archives/install-MFS-responsefile.xml
 
87
        sed -i 's/localhost/'$hostname'/' $CHARM_DIR/files/archives/install-MFS-responsefile.xml
 
88
        oldpasswd=`cat $CHARM_DIR/files/archives/install-MFS-responsefile.xml | grep "user.database.db2.appcenter.password" | cut -d"=" -f3 | cut -d"/" -f1`
 
89
        oldpasswd=`echo $oldpasswd | tr -d "'"`
 
90
        sed -i s/$oldpasswd/$dbuserpw/g $CHARM_DIR/files/archives/install-MFS-responsefile.xml
 
91
        
 
92
        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
 
93
        sed -i 's/waserver1/worklight/' $CHARM_DIR/files/archives/install-MFS-responsefile.xml
 
94
        sed -i 's|/root/IBM/WebSphere/wlp|'$LIBERTY_INSTALL_PATH'|g' $CHARM_DIR/files/archives/install-MFS-responsefile.xml
 
95
        juju-log "IBM MFS:Response file edited"
 
96
}
 
97
 
 
98
#Edit configure_liberty_db2_file
 
99
edit_configure_liberty_db2_file()
 
100
{
 
101
        juju-log "IBM MFS: Editing configure_liberty_db2.xml file"
 
102
        get_db_values_from_interface
 
103
 
 
104
        cp $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2-orig.xml $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
 
105
 
 
106
        sed -i '/"database.db2.wladmin.password"/c\<property name="database.db2.wladmin.password" value="'$dbuserpw'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
 
107
        sed -i '/"database.db2.worklight.password"/c\<property name="database.db2.worklight.password" value="'$dbuserpw'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
 
108
        sed -i '/"database.db2.worklightreports.password"/c\<property name="database.db2.worklightreports.password" value="'$dbuserpw'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
 
109
        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
 
110
        sed -i '/"worklight.contextroot"/c\<property name="worklight.contextroot" value="/worklight"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
 
111
        sed -i '/"database.db2.host"/c\<property name="database.db2.host" value="'$hostname'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
 
112
        sed -i '/"database.db2.port"/c\<property name="database.db2.port" value="'$db2_port'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
 
113
        sed -i '/"database.db2.instance"/c\<property name="database.db2.instance" value="'$db2_instance'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
 
114
        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
 
115
        sed -i '/"database.db2.wladmin.dbname"/c\<property name="database.db2.wladmin.dbname" value="WRKLGHT"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
 
116
        sed -i '/"database.db2.wladmin.schema"/c\<property name="database.db2.wladmin.schema" value="WLADMIN"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
 
117
        sed -i '/"database.db2.wladmin.username"/c\<property name="database.db2.wladmin.username" value="'$dbusername'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
 
118
        sed -i '/"database.db2.worklight.dbname"/c\<property name="database.db2.worklight.dbname" value="WRKLGHT"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
 
119
        sed -i '/"database.db2.worklight.schema"/c\<property name="database.db2.worklight.schema" value="WRKLGHT"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
 
120
        sed -i '/"database.db2.worklight.username"/c\<property name="database.db2.worklight.username" value="'$dbusername'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
 
121
        sed -i '/"database.db2.worklightreports.dbname"/c\<property name="database.db2.worklightreports.dbname" value="WRKLGHT"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
 
122
        sed -i '/"database.db2.worklightreports.schema"/c\<property name="database.db2.worklightreports.schema" value="WLREPORT"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
 
123
        sed -i '/"database.db2.worklightreports.username"/c\<property name="database.db2.worklightreports.username" value="'$dbusername'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
 
124
        sed -i '/"appserver.was.installdir"/c\<property name="appserver.was.installdir" value="'$LIBERTY_INSTALL_PATH'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
 
125
        sed -i '/"appserver.was85liberty.serverInstance"/c\<property name="appserver.was85liberty.serverInstance" value="worklight"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml
 
126
        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
 
127
 
 
128
}
 
129
update_usename_serverxml_file()
 
130
{
 
131
        password=`config-get worklight_passwd`
 
132
        username=`config-get worklight_user`
 
133
        juju-log "IBM MFS: Username is $username, Password is $password"
 
134
        
 
135
        juju-log "IBM MFS: Editing server.xml file for updated credentials"
 
136
        cp $LIBERTY_INSTALL_PATH/usr/servers/worklight/server_org.xml $LIBERTY_INSTALL_PATH/usr/servers/worklight/server.xml
 
137
 
 
138
        sed -i '/user name="admin"/c\<user name="'$username'" password="'$password'"/>' $LIBERTY_INSTALL_PATH/usr/servers/worklight/server.xml
 
139
        sed -i '/member name="appcenteradmin"/c\<member name="'$username'"/>' $LIBERTY_INSTALL_PATH/usr/servers/worklight/server.xml
 
140
}
 
141
 
 
142
 
 
143
#Edit create_database_db2_file
 
144
edit_create_database_db2_file()
 
145
{
 
146
        juju-log "IBM MFS: Editing create-database-db2.xml file"
 
147
        get_db_values_from_interface
 
148
 
 
149
 
 
150
        sed -i '/"database.db2.admin.password"/c\<property name="database.db2.admin.password" value="'$dbuserpw'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
 
151
        sed -i '/"database.db2.wladmin.password"/c\<property name="database.db2.wladmin.password" value="'$dbuserpw'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
 
152
        sed -i '/"database.db2.worklight.password"/c\<property name="database.db2.worklight.password" value="'$dbuserpw'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
 
153
        sed -i '/"database.db2.worklightreports.password"/c\<property name="database.db2.worklightreports.password" value="'$dbuserpw'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
 
154
        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
 
155
        sed -i '/"database.db2.host"/c\<property name="database.db2.host" value="'$hostname'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
 
156
        sed -i '/"database.db2.port"/c\<property name="database.db2.port" value="'$db2_port'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
 
157
        sed -i '/"database.db2.instance"/c\<property name="database.db2.instance" value="'$db2_instance'"/>'  $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
 
158
        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
 
159
        sed -i '/"database.db2.admin.username"/c\<property name="database.db2.admin.username" value="'$dbusername'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
 
160
        sed -i '/"database.db2.wladmin.dbname"/c\<property name="database.db2.wladmin.dbname" value="WRKLGHT"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
 
161
        sed -i '/"database.db2.wladmin.schema"/c\<property name="database.db2.wladmin.schema" value="WLADMIN"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
 
162
        sed -i '/"database.db2.wladmin.username"/c\<property name="database.db2.wladmin.username" value="'$dbusername'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
 
163
        sed -i '/"database.db2.worklight.dbname"/c\<property name="database.db2.worklight.dbname" value="WRKLGHT"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
 
164
        sed -i '/"database.db2.worklight.schema"/c\<property name="database.db2.worklight.schema" value="WRKLGHT"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
 
165
        sed -i '/"database.db2.worklight.username"/c\<property name="database.db2.worklight.username" value="'$dbusername'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
 
166
        sed -i '/"database.db2.worklightreports.dbname"/c\<property name="database.db2.worklightreports.dbname" value="WRKLGHT"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
 
167
        sed -i '/"database.db2.worklightreports.schema"/c\<property name="database.db2.worklightreports.schema" value="WLREPORT"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
 
168
        sed -i '/"database.db2.worklightreports.username"/c\<property name="database.db2.worklightreports.username" value="'$dbusername'"/>' $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml
 
169
}
 
170
 
 
171
#Backup the configuration files to reuee it later it required.
 
172
backup_configuration_files()
 
173
{
 
174
        cp $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2.xml $MobileFirstServer_PATH/WorklightServer/configuration-samples/configure-liberty-db2-orig.xml
 
175
        cp $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2.xml $MobileFirstServer_PATH/WorklightServer/configuration-samples/create-database-db2-orig.xml
 
176
}
 
177
 
 
178
 
 
179
#Post install configurations.
 
180
postworklightinstall_configuration()
 
181
{
 
182
 
 
183
        juju-log "IBM MFS: Editing configure-liberty-db2.xml file"
 
184
        edit_configure_liberty_db2_file
 
185
        juju-log "IBM MFS: Editing create-database-db2.xml file"
 
186
        edit_create_database_db2_file
 
187
 
 
188
        juju-log "IBM MFS: Running ant commands"
 
189
        cd $MobileFirstServer_PATH/WorklightServer/configuration-samples
 
190
        #Run create admdatabases command
 
191
        ant -f create-database-db2.xml admdatabases
 
192
        if [ $? -eq 0 ]; then
 
193
                juju-log "IBM MFS: ant create admddatabase command ran successfully"
 
194
        else
 
195
                juju-log "IBM MFS: ant create admddatabase command failed to run"
 
196
                exit 1
 
197
        fi
 
198
 
 
199
        #Run create-database-db2.xml databases command
 
200
        ant -f create-database-db2.xml databases
 
201
        if [ $? -eq 0 ]; then
 
202
                juju-log "IBM MFS: ant create database command ran successfully"
 
203
        else
 
204
                juju-log "IBM MFS: ant create database command failed to run"
 
205
                exit 1
 
206
        fi
 
207
 
 
208
        #Run configure admdatabases command
 
209
        ant -f configure-liberty-db2.xml admdatabases
 
210
        if [ $? -eq 0 ]; then
 
211
                juju-log "IBM MFS: ant configure liberty admddatabase command ran successfully"
 
212
        else
 
213
                juju-log "IBM MFS: ant configure liberty admddatabase command failed to run"
 
214
                exit 1
 
215
        fi
 
216
        #Run configure databases command
 
217
        ant -f configure-liberty-db2.xml databases
 
218
        if [ $? -eq 0 ]; then
 
219
                juju-log "IBM MFS: ant configure liberty database command ran successfully"
 
220
        else
 
221
                juju-log "IBM MFS: ant configure liberty database command failed to run"
 
222
                exit 1
 
223
        fi
 
224
 
 
225
        #Run adminstall command
 
226
        ant -f configure-liberty-db2.xml adminstall
 
227
        if [ $? -eq 0 ]; then
 
228
                juju-log "IBM MFS: ant adminstall command ran successfully"
 
229
        else
 
230
                juju-log "IBM MFS: ant adminstall command failed to run"
 
231
                exit 1
 
232
        fi
 
233
 
 
234
        #Run install command to configure sample runtime environment
 
235
        ant -f configure-liberty-db2.xml install
 
236
        if [ $? -eq 0 ]; then
 
237
                juju-log "IBM MFS: ant configure install command ran successfully"
 
238
        else
 
239
                juju-log "IBM MFS: ant configure install command failed to run"
 
240
                exit 1
 
241
        fi
 
242
 
 
243
}
 
244
 
 
245
#Restart liberty server
 
246
restart_worklightserver()
 
247
{
 
248
        if [ -d $LIBERTY_INSTALL_PATH/usr/servers/worklight ]; then
 
249
                juju-log "IBM MFS: Restarting server"
 
250
                set +e
 
251
                $LIBERTY_INSTALL_PATH/bin/server stop worklight
 
252
                if [ $? == 0 ]; then
 
253
                        juju-log "IBM MFS: Websphere Liberty stopped successfully"
 
254
                else
 
255
                        juju-log "IBM MFS: Server not running"
 
256
                fi
 
257
 
 
258
                $LIBERTY_INSTALL_PATH/bin/server start worklight
 
259
                if [ $? == 0 ]; then
 
260
                        juju-log "IBM MFS: Websphere Liberty started successfully"
 
261
                        set_state 'ibm-mobilefirst-server.website.started'
 
262
                else
 
263
                        juju-log "IBM MFS: Server is running"
 
264
                        set_state 'ibm-mobilefirst-server.website.started'
 
265
                fi
 
266
 
 
267
        fi
 
268
        juju-log "IBM MFS: server restarted successfully"
 
269
 
 
270
}
 
271
 
 
272
@when 'website.available'
 
273
@when_not 'ibm-mobilefirst-server.website.configured'
 
274
function capture_portdetials(){
 
275
        juju-log "IBM MFS: Website is available"
 
276
        #Get the port number from WLP and check whether the port is free and can be used. If not free, use the next one. 
 
277
        hostname=`relation-get hostname`
 
278
        juju-log "IBM MFS: Hostname is $hostname"
 
279
        httpport=`relation-get port`
 
280
        httpsport=`relation-get httpsport`
 
281
        juju-log "IBM MFS: Http post = $httpport , Https port = $httpsport"
 
282
        while [ 1 ]
 
283
        do
 
284
                result=`netstat -an | grep $httpport | cut -d"*" -f1`
 
285
                if [ "$result" != "" ]; then
 
286
                        httpport=$((httpport+1))
 
287
                else
 
288
                        break
 
289
                fi
 
290
        done;
 
291
        while [ 1 ]
 
292
        do
 
293
                result=`netstat -an | grep $httpsport | cut -d"*" -f1`
 
294
                if [ "$result" != "" ]; then
 
295
                        httpsport=$((httpsport+1))
 
296
                else
 
297
                        break
 
298
                fi
 
299
        done;
 
300
        echo "HttpPort:$httpport" > portdetails.txt
 
301
        echo "HttpsPort:$httpsport" >> portdetails.txt
 
302
 
 
303
        set_state 'ibm-mobilefirst-server.website.configured'
 
304
        
 
305
}
 
306
 
 
307
@when 'website.available'
 
308
@when 'ibm-mobilefirst-server.installed'
 
309
@when_not 'ibm-mobilefirst-server.website.started'
 
310
function start_worklight_server(){
 
311
        if [ -d $LIBERTY_INSTALL_PATH/usr/servers/worklight ]; then
 
312
 
 
313
                $LIBERTY_INSTALL_PATH/bin/server start worklight
 
314
                if [ $? == 0 ]; then
 
315
                        juju-log "IBM MFS: Websphere Liberty started successfully"
 
316
                        set_state 'ibm-mobilefirst-server.website.started'
 
317
                fi
 
318
        fi
 
319
}
 
320
 
 
321
 
 
322
 
 
323
@when 'website.available'
 
324
@when 'ibm-mobilefirst-server.website.configured'
 
325
@when 'ibm-mobilefirst-server.mobilefirstserverdb.configured'
 
326
@when_not 'ibm-mobilefirst-server.installed'
 
327
function install() {
 
328
        juju-log "IBM MFS: License accepted"
 
329
        remove_state 'ibm-mobilefirst-server.uninstalled'
 
330
 
 
331
        httpport=`cat portdetails.txt | grep "HttpPort" | cut -d":" -f2`
 
332
        httpsport=`cat portdetails.txt | grep "HttpsPort" | cut -d":" -f2`
 
333
        juju-log "httpport = $httpport $httpsport"
 
334
        # Get the installable resource
 
335
        juju-log "IBM MFS: Fetching the ibm_mobilefirst-server_installer resource"
 
336
        status-set maintenance "fetching the ibm_mobilefirst-server_installer resource"
 
337
        cfg_worklight_pkg_name=`resource-get 'ibm_mobilefirstserver_installer' || echo unavailable`
 
338
        # If we don't have a package, report blocked status; we can't proceed.
 
339
        if [ "$cfg_worklight_pkg_name" = "unavailable" ]; then
 
340
                juju-log "IBM MFS: missing required ibm_mobilefirst-server_installer resource"
 
341
                status-set blocked "missing required ibm_mobilefirst-server_installer resource"
 
342
                exit 0
 
343
        fi
 
344
        # Check for empty packages from Charm store
 
345
        ibm_worklight_empty=`file $cfg_worklight_pkg_name | { grep -q empty && echo "True"; } || echo "False"`
 
346
        if [ "$ibm_worklight_empty" = "True" ]; then
 
347
                juju-log "IBM IM: no ibm_worklight to install"
 
348
                status-set active "ready"
 
349
                exit 0
 
350
        fi
 
351
 
 
352
        juju-log "IBM MFS: using $cfg_worklight_pkg_name as the ibm_mobilefirst-server_installer resource"
 
353
        PKG_DIR=`dirname $cfg_worklight_pkg_name`
 
354
        juju-log "Archive dir is $PKG_DIR"
 
355
 
 
356
        cd $PKG_DIR
 
357
        status-set maintenance "Extracting IBM MobileFirst Server package"
 
358
        rm -rf MobileFirst_Platform_Server*
 
359
        unzip $PKG_DIR/*.zip
 
360
        if [ $? != 0 ]; then
 
361
                juju-log "IBM MFS: Unable to extract the MFS package content. Verify whether the package is corrupt."
 
362
                status-set blocked "IBM MobileFirst Server package is corrupt"
 
363
                #Remove corrupt archive file
 
364
                rm -rf $PKG_DIR/*.zip
 
365
                exit 0
 
366
        fi
 
367
        if [ -d $PKG_DIR/MobileFirst_Platform_Server/disk1 ]; then
 
368
                juju-log "IBM MFS: MobileFirst server Packages are available for installation."
 
369
                #Create worklight server
 
370
                create_worklightserver $httpport $httpsport
 
371
                #Edit response file for installation
 
372
                edit_responsefile
 
373
                #edit_post_install_file
 
374
                juju-log "IBM MFS: Installing MobileFirst Server package."
 
375
                status-set maintenance "Installing IBM MobileFirst Server software"
 
376
                cd $IM_INSTALL_PATH/eclipse/tools
 
377
                ./imcl input $CHARM_DIR/files/archives/install-MFS-responsefile.xml -acceptLicense -showProgress
 
378
                juju-log "IBM MFS: Installation of MobileFirst server complete."
 
379
                status-set active "Installed IBM MobileFirst Server"
 
380
                #Check install successful or not
 
381
                if [ -d $MobileFirstServer_PATH/WorklightServer ]; then
 
382
                        juju-log "IBM MFS: Install Sucessful and moving to post install configuration"
 
383
                        backup_configuration_files
 
384
                        status-set maintenance "Running Post install configuration steps for IBM MobileFirst Server"
 
385
                        postworklightinstall_configuration
 
386
                        juju-log "IBM MFS: Post install configuration is done"
 
387
                        status-set maintenance "Post install configuration is done"
 
388
                        #Restart worklight server
 
389
                        restart_worklightserver
 
390
                        set_state 'ibm-mobilefirst-server.installed'
 
391
                        #Taking a backup of WLP server.xml file
 
392
                        cp $LIBERTY_INSTALL_PATH/usr/servers/worklight/server.xml $LIBERTY_INSTALL_PATH/usr/servers/worklight/server_org.xml
 
393
                        update_usename_serverxml_file
 
394
                        status-set active "IBM MFS Installed and is Ready"
 
395
                        open-port $httpport
 
396
                        open-port $httpsport
 
397
 
 
398
                else
 
399
                        juju-log "IBM MFS: Install failed"
 
400
                        status-set blocked "IBM MobileFirst Server install failed"
 
401
                        exit 1
 
402
                fi
 
403
        else
 
404
                juju-log "IBM MFS: MobileFirst server Packages are missing. Please check README file."
 
405
                status-set blocked "IBM MobileFirst server Packages are missing"
 
406
                exit 1
 
407
        fi
 
408
}
 
409
 
 
410
@when 'mobilefirstserverdb.connected'
 
411
@when_not 'mobilefirstserverdb.ready'
 
412
function send_sshkeys_mobilefirstserverdbs(){
 
413
        SSH_PATH=/root/.ssh
 
414
        if [ ! -f  $SSH_PATH/id_rsa.pub ]; then
 
415
                juju-log "IBM MFS: Setting up SSH keys."
 
416
                ssh-keygen -t rsa -f $SSH_PATH/id_rsa -N ''
 
417
        fi
 
418
        key="`cat $SSH_PATH/id_rsa.pub`"
 
419
        mobilefirstserverdbnames="APPCNTR,WRKLGHT"
 
420
        juju-log "DB names is $mobilefirstserverdbnames"
 
421
        # To pass the SSH key and DB names to DB2 charm
 
422
        relation_call --state=mobilefirstserverdb.connected set_ssh_keys "$key" || true
 
423
        relation_call --state=mobilefirstserverdb.connected set_dbs $mobilefirstserverdbnames || true
 
424
}
 
425
 
 
426
@when 'mobilefirstserverdb.ready'
 
427
@when_not 'ibm-mobilefirst-server.mobilefirstserverdb.configured'
 
428
function configure_mobilefirstserverdb(){
 
429
 
 
430
        get_db_values_from_interface
 
431
 
 
432
        if [ -z "$db2_port" ]; then
 
433
                juju-log "No data sent yet"
 
434
                exit 0
 
435
        fi
 
436
 
 
437
        get_db_values_from_interface
 
438
 
 
439
        juju-log "IBM MFS: Got the db2 values : $db2_path $hostname $dbusername $dbuserpw $dbs_created"
 
440
        juju-log "IBM MFS: Copying files from remote - Directory: $db2_path Host:$hostname"
 
441
        status-set maintenance "Copying mobilefirstserverdb2 jar files from remote DB2 machine"
 
442
        scp  -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null root@$hostname:$db2_path/V10.5/java/db2jcc.jar $CHARM_DIR/files || true
 
443
        scp  -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null root@$hostname:$db2_path/V10.5/java/db2jcc4.jar $CHARM_DIR/files || true
 
444
        scp  -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null root@$hostname:$db2_path/V10.5/java/db2jcc_license_*.jar $CHARM_DIR/files || true
 
445
        juju-log "IBM MFS: Copied the files to $CHARM_DIR/files"
 
446
        #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.
 
447
        if [ -d $MobileFirstServer_PATH/WorklightServer ]; then
 
448
                #Update the password if changed in server.xml file
 
449
                newpassword=`cat $LIBERTY_INSTALL_PATH/usr/servers/worklight/server.xml | grep "WRKLGHT" | grep "WLADMIN" | cut -d"}" -f2 | cut -d" " -f1 | tr -d '"'`
 
450
                oldpassword=`cat $LIBERTY_INSTALL_PATH/usr/servers/worklight/server.xml | grep "APPCNTR" | cut -d"}" -f2 | cut -d" " -f1 | tr -d '"'`
 
451
                sed -i s/$oldpassword/$newpassword/g $LIBERTY_INSTALL_PATH/usr/servers/worklight/server.xml
 
452
                status-set maintenance "Server.xml file updated" 
 
453
        fi
 
454
        set_state 'ibm-mobilefirst-server.mobilefirstserverdb.configured'
 
455
 
 
456
}
 
457
 
 
458
@when 'mobilefirstserverdb.departed'
 
459
function stop_mobilefirstserverdb2(){
 
460
        # 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.
 
461
        # 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.
 
462
        juju-log "IBM MFS: Removing the jar files when relation is broken"
 
463
        rm -rf $CHARM_DIR/files/db2jcc.jar $CHARM_DIR/files/db2jcc4.jar $CHARM_DIR/files/db2jcc_license_*.jar
 
464
        juju-log "IBM MFS: JAR files removed"   
 
465
        if [ -d $LIBERTY_INSTALL_PATH/usr/servers/worklight ]; then
 
466
                server_status=`ps -ef | grep worklight | grep $LIBERTY_INSTALL_PATH/bin/tools/ws-server.jar | awk '{print $(NF-1),$NF}'`
 
467
                if [ "$server_status" == "$LIBERTY_INSTALL_PATH/bin/tools/ws-server.jar worklight" ]; then
 
468
                        juju-log "IBM MFS: stopping worklight server"
 
469
                        $LIBERTY_INSTALL_PATH/bin/server stop worklight
 
470
                        if [ $? == 0 ]; then
 
471
                                juju-log "IBM MFS: worklight server stopped sucessfully"
 
472
                                remove_state 'ibm-mobilefirst-server.website.started'
 
473
                                juju-log "Copying the org server.xml file back in website broken fn"
 
474
                        else
 
475
                                juju-log "IBM MFS: Failed to stop worklight server"
 
476
                                exit 0
 
477
                        fi
 
478
                fi
 
479
                clean_old_installations
 
480
                remove_worklightserver
 
481
                remove_state 'ibm-mobilefirst-server.installed'
 
482
                remove_state 'ibm-mobilefirst-server.mobilefirstserverdb.configured'
 
483
        else
 
484
                juju-log "IBM MFS: Worklight server does not exist"
 
485
        fi
 
486
}
 
487
 
 
488
 
 
489
@when 'website.departed'
 
490
function stop_worklight_server(){
 
491
        #If relation between wlp and worklight is broken( or when MobileFirst Server service is removed) , uninstalling worklight.
 
492
        #Otherwise if we set the relation again or deploy MobileFirst server again, the installation goes wrong with traces of old files present
 
493
        juju-log "IBM MFS: In website departed function"
 
494
        if [ -d $LIBERTY_INSTALL_PATH/usr/servers/worklight ]; then
 
495
                server_status=`ps -ef | grep worklight | grep $LIBERTY_INSTALL_PATH/bin/tools/ws-server.jar | awk '{print $(NF-1),$NF}'`
 
496
                if [ "$server_status" == "$LIBERTY_INSTALL_PATH/bin/tools/ws-server.jar worklight" ]; then
 
497
                        juju-log "IBM MFS: stopping worklight server"
 
498
                        $LIBERTY_INSTALL_PATH/bin/server stop worklight
 
499
                        if [ $? == 0 ]; then
 
500
                                juju-log "IBM MFS: worklight server stopped sucessfully"
 
501
                                remove_state 'ibm-mobilefirst-server.website.started'           
 
502
                                juju-log "Copying the org server.xml file back in website broken fn"
 
503
                        else
 
504
                                juju-log "IBM MFS: Failed to stop worklight server"
 
505
                                exit 0
 
506
                        fi
 
507
                fi
 
508
                clean_old_installations
 
509
                remove_worklightserver
 
510
                remove_state 'ibm-mobilefirst-server.installed'
 
511
                remove_state 'ibm-mobilefirst-server.mobilefirstserverdb.configured'
 
512
        else
 
513
                juju-log "IBM MFS: Worklight server does not exist"
 
514
        fi
 
515
 
 
516
}
 
517
 
 
518
@when_not_all 'config.default.worklight_user' 'config.default.worklight_passwd'
 
519
@when_any 'config.changed.worklight_user' 'config.changed.worklight_passwd'
 
520
function create_new_userpassword(){
 
521
        # Change only if worklight is installed
 
522
        if [ -d $LIBERTY_INSTALL_PATH/usr/servers/worklight ]; then
 
523
                juju-log "Creating new User/Password"
 
524
                status-set active "Updating User Credentials"   
 
525
                update_usename_serverxml_file
 
526
                status-set active "IBM MFS Updated"
 
527
        fi
 
528
        
 
529
 
 
530
}
 
531
reactive_handler_main