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

« back to all changes in this revision

Viewing changes to reactive/ibm-db2.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
 
DB2_ARCHIVE_DIR=`pwd`
9
 
DB2_USER=db2inst1
10
 
DB2_USER_PW=db2inst1
11
 
DB2_INSTALL_PATH=/opt/ibm/db2/V10.5
12
 
AUTH_KEY_FILE=/root/.ssh/authorized_keys
13
 
TEMP_KEY_FILE=key.txt
14
 
 
15
 
cfgusername=""
16
 
 
17
 
remoteunit=$cfgusername
18
 
cfgpasswd=""
19
 
dbs_created=""
20
 
 
21
 
isdbcreated=0
22
 
#Get the cfguser name based on the remote unit name
23
 
get_cfgusername()
24
 
{
25
 
        usr="usr"
26
 
        passwd="123"
27
 
        db="db"
28
 
        cfgusername=$1
29
 
        cfgusername=`echo $cfgusername | cut -d"/" -f1`
30
 
        cfgusername=`echo "$cfgusername" | sed -r 's/-//g' | sed -r 's/ibm//g'`
31
 
        cfgusername=`echo "$cfgusername" | awk '{print substr($0,0,5)}'`
32
 
        remoteunit=$cfgusername
33
 
        cfgusername="$cfgusername$usr"
34
 
        cfgpasswd="$cfgusername$passwd"
35
 
        remoteunitdbname="$remoteunit$db"
36
 
 
37
 
}
38
 
# Check whether DB2 is installed
39
 
is_db2_installed()
40
 
{
41
 
        if [ -d $DB2_INSTALL_PATH/bin ]; then
42
 
                echo "True"
43
 
        else
44
 
                echo "False"
45
 
        fi
46
 
}
47
 
 
48
 
 
49
 
# Remove DB2, if installed
50
 
remove_software()
51
 
{
52
 
        db2_inst=`is_db2_installed`
53
 
        if  [ $db2_inst == True ]; then
54
 
                juju-log "IBM DB2: Removing IBM DB2 software."
55
 
                status-set maintenance "Removing IBM DB2"
56
 
 
57
 
                #finding the DB alias names to be dropped and dropping all Databases in that
58
 
                for queue1 in `su - $DB2_USER -c 'db2ilist'`;
59
 
                do
60
 
                        juju-log "IBM DB2: Dropping the Databases for the instance $queue1"
61
 
                        #if db2 is not running, we need to start it first, otherwise db2 drop command will fail
62
 
                        if su - $queue1 -c 'ps -eaf|grep -i "\bdb2sysc 0\b" |grep -i '$queue1;
63
 
                        then
64
 
                                juju-log "IBM DB2: DB2 already started for the user $queue1"
65
 
                        else
66
 
                                su - $queue1 -c 'db2start'
67
 
                                juju-log "IBM DB2: DB2 is started"
68
 
                        fi
69
 
 
70
 
                        for queue in `su - $queue1 -c 'db2 list db directory | grep "Database alias" | cut -d"=" -f2'`;
71
 
                        do
72
 
                                queue=`echo $queue | xargs`
73
 
                                juju-log "IBM DB2: Dropping the DB $queue"
74
 
                                su - $queue1 -c 'db2 drop database '$queue
75
 
                                if [ $? == 0 ]; then
76
 
                                        juju-log "IBM DB2: Dropped the DB $queue"
77
 
                                else
78
 
                                        juju-log "IBM DB2: Dropping the DB $queue failed"
79
 
                                        status-set blocked "Dropping the DB failed"
80
 
                                        exit 0
81
 
                                fi
82
 
                        done
83
 
                        juju-log "IBM DB2: Stopping the DB for $queue1"
84
 
 
85
 
                        if su - $queue1 -c 'ps -eaf|grep -i "\bdb2sysc 0\b" |grep -i '$queue1
86
 
                        then
87
 
                                su - $queue1 -c 'db2 force application all'
88
 
                                su - $queue1 -c 'db2stop'
89
 
                                if [ $? == 0 ]; then
90
 
                                        juju-log "IBM DB2: Stopped the DB for the instance $queue1"
91
 
                                else
92
 
                                        juju-log "IBM DB2: Stopping the DB for $queue1 failed"
93
 
                                        status-set blocked "Stopping the DB for $queue1 failed"
94
 
                                        exit 0
95
 
                                fi
96
 
 
97
 
                        else
98
 
                                juju-log "IBM DB2: DB2 is already stopped"
99
 
                        fi
100
 
                done
101
 
 
102
 
                #drop the das
103
 
                cd $DB2_INSTALL_PATH/instance
104
 
                juju-log "IBM DB2: Dropping the DAS"
105
 
                ./dasdrop
106
 
                if [ $? == 0 ]; then
107
 
                        juju-log "IBM DB2: Dropped the DAS"
108
 
                else
109
 
                        juju-log "IBM DB2: Dropping the DAS failed"
110
 
                        status-set blocked "Dropping the DAS failed"
111
 
                        exit 0
112
 
                fi
113
 
 
114
 
                #delete the instances
115
 
                su - $DB2_USER -c 'db2ilist'
116
 
                for queue1 in `su - $DB2_USER -c 'db2ilist'`;
117
 
                do
118
 
                        juju-log "IBM DB2: Deleting the instance $queue1"
119
 
                        /opt/ibm/db2/V10.5/instance/db2idrop $queue1
120
 
                        if [ $? == 0 ]; then
121
 
                                juju-log "IBM DB2: Deleted  the instance $queue1"
122
 
                        else
123
 
                                juju-log "IBM DB2: Deleting the instance $queue1 failed"
124
 
                                status-set blocked "Deleting the instance $queue1 failed"
125
 
                                exit 0
126
 
                        fi
127
 
 
128
 
                done
129
 
 
130
 
                #Delete the instance entries from /etc/services file
131
 
                juju-log "IBM DB2: Deleting the entries from /etc/services"
132
 
                sed -i '/db2c_/d' /etc/services
133
 
                if [ $? == 0 ]; then
134
 
                        juju-log "IBM DB2: Deleted the entries from /etc/services"
135
 
                else
136
 
                        juju-log "IBM DB2: Deletion of the entries from /etc/services failed"
137
 
                        status-set blocked "Deletion of the entries from /etc/services failed"
138
 
                        exit 0
139
 
                fi
140
 
 
141
 
                #uninstall
142
 
                juju-log "IBM DB2: Uninstall "
143
 
                cd /opt/ibm/db2/V10.5/install
144
 
                set +e
145
 
                ./db2_deinstall -a
146
 
                if [ $? == 0 ]; then
147
 
                        juju-log "IBM DB2: Uninstall of db2 successful"
148
 
                else
149
 
                        juju-log "IBM DB2: Uninstall of db2 failed"
150
 
                        status-set blocked "IBM DB2 Uninstallation failed"
151
 
                        exit 0
152
 
                fi
153
 
                set -e
154
 
                cd /opt/ibm
155
 
                rm -rf db2
156
 
                juju-log "IBM DB2: Finished Uninstalling......."
157
 
                status-set blocked "IBM DB2 Uninstalled"
158
 
 
159
 
        else
160
 
                juju-log "IBM DB2: DB2 not installed"
161
 
        fi
162
 
 
163
 
}
164
 
 
165
 
# Update system configuration after installing DB2
166
 
configure_system()
167
 
{
168
 
        juju-log "IBM DB2: Updating system configuration."
169
 
        cd $DB2_INSTALL_PATH/instance
170
 
        su - $DB2_USER -c 'db2set DB2COMM=tcpip'
171
 
 
172
 
        #Start DB for the DB2_user
173
 
        if su - $DB2_USER -c 'ps -eaf|grep -i "\bdb2sysc 0\b" |grep -i '$DB2_USER;
174
 
        then
175
 
                juju-log "IBM DB2: DB2 already started for the user $DB2_USER"
176
 
        else
177
 
                su - $DB2_USER -c 'db2start'
178
 
                juju-log "IBM DB2: DB2 is started"
179
 
        fi
180
 
 
181
 
        service_name=`su - $DB2_USER -c 'db2 get dbm cfg|grep -i svce|cut -d"=" -f2'`
182
 
        port_num=`grep $service_name /etc/services | cut -d"/" -f1 | cut -f2`
183
 
        port_num=`echo $port_num | xargs`
184
 
        if su - $DB2_USER -c 'netstat -an | grep '$port_num;
185
 
        then
186
 
                juju-log "IBM DB2: DB2 started on port $port_num"
187
 
        else
188
 
                juju-log "IBM DB2: DB2 not started on port $port_num"
189
 
                exit 1
190
 
        fi
191
 
 
192
 
        juju-log "IBM DB2: Updated system configuration."
193
 
 
194
 
}
195
 
 
196
 
#Configure dftdbpath
197
 
configure_dftdbpath()
198
 
{
199
 
        db2_inst=`is_db2_installed`
200
 
        if  [ $db2_inst == True ]; then
201
 
                cfgdbpath=`config-get dftdbpath`
202
 
                #If null, assign the default value
203
 
                if [ "cfgdbpath" == "" ]
204
 
                then
205
 
                        cfgdbpath="/tmp"
206
 
                fi
207
 
 
208
 
                for queue1 in `su - $DB2_USER -c 'db2ilist'`;
209
 
                do
210
 
                        #if db2 is not running don't do anything
211
 
                        if su - $queue1 -c 'ps -eaf|grep -i "\bdb2sysc 0\b" |grep -i '$queue1;
212
 
                        then
213
 
                                su - $queue1 -c 'db2 attach to '$queue1
214
 
                                dbpath=`su - $queue1 -c 'db2 get dbm cfg|grep -i DFTDBPATH|cut -d"=" -f2'`
215
 
                                cfgdbpath=`echo $cfgdbpath | xargs`
216
 
                                dbpath=`echo $dbpath | xargs`
217
 
                                if  [ "$cfgdbpath" != "$dbpath" ]; then
218
 
                                        if [ -d "$cfgdbpath" ]; then
219
 
                                                su - $queue1 -c 'db2 update dbm cfg using DFTDBPATH '$cfgdbpath
220
 
                                                if [ $? == 0 ]; then
221
 
                                                        juju-log "IBM DB2: DFTDBPATH changed to $cfgdbpath"
222
 
                                                else
223
 
                                                        juju-log "IBM DB2: Unable to set DFTDBPATH to $cfgdbpath"
224
 
                                                        exit 0
225
 
                                                fi
226
 
                                        else
227
 
                                                juju-log "IBM DB2: Not a valid path. Please check the path and run the command again"
228
 
                                                exit 0
229
 
                                        fi
230
 
                                fi
231
 
                        fi
232
 
                done
233
 
        else
234
 
                juju-log "IBM DB2: DB2 is not installed. Install DB2 to change the dbpath"
235
 
                exit 0
236
 
        fi
237
 
}
238
 
# Edit the response file for UID
239
 
edit_responsefile()
240
 
{
241
 
        rsp_file=db2server_temp.rsp
242
 
        if [ "$ARCHITECTURE" = "x86_64" ]; then
243
 
                orgrsp_file=db2server.rsp
244
 
        else
245
 
                orgrsp_file=db2server_ppcle.rsp
246
 
        fi
247
 
        cp $CHARM_DIR/files/archives/$orgrsp_file $CHARM_DIR/files/archives/$rsp_file
248
 
 
249
 
        db2uid=`id $DB2_USER | cut -d"=" -f2 | cut -d"(" -f1`
250
 
        sed -i s/db2inst1_uid/$db2uid/g  $CHARM_DIR/files/archives/$rsp_file
251
 
        sed -i s/db2inst1_gid/$db2uid/g  $CHARM_DIR/files/archives/$rsp_file
252
 
        sed -i s/db2inst1_homedir/$DB2_USER/g $CHARM_DIR/files/archives/$rsp_file
253
 
        sed -i s/db2inst1_password/$DB2_USER_PW/g $CHARM_DIR/files/archives/$rsp_file
254
 
 
255
 
        db2fenid=`id db2fenc1 | cut -d"=" -f2 | cut -d"(" -f1`
256
 
        sed -i s/db2fenc1_uid/$db2fenid/g  $CHARM_DIR/files/archives/$rsp_file
257
 
        sed -i s/db2fenc1_gid/$db2fenid/g  $CHARM_DIR/files/archives/$rsp_file
258
 
        dasuid=`id dasusr1 | cut -d"=" -f2 | cut -d"(" -f1`
259
 
        sed -i s/das_uid/$dasuid/g $CHARM_DIR/files/archives/$rsp_file
260
 
        sed -i s/das_gid/$dasuid/g $CHARM_DIR/files/archives/$rsp_file
261
 
 
262
 
}
263
 
 
264
 
create_user()
265
 
{
266
 
        servicename=$1
267
 
        passwdchanged=0
268
 
        instance_exists=0
269
 
        db2_inst=`is_db2_installed`
270
 
        get_cfgusername $servicename
271
 
        if  [ $db2_inst == True ]; then
272
 
                status-set maintenance "Creating Users"
273
 
                #Find the group id for db2inst1 user which is used to install db2 and find all
274
 
                #users belonging to this user
275
 
                db2inst1grpid=`id $DB2_USER |  cut -d" " -f2`
276
 
 
277
 
                #If the user is already available, just change the password.
278
 
                for queue2 in `awk -F':' '{ print $1}' /etc/passwd`;
279
 
                do
280
 
                        gid=`id $queue2 |  cut -d" " -f2`
281
 
                        if [ $gid == $db2inst1grpid ]; then
282
 
                                if [ $queue2 != "$DB2_USER" ]; then
283
 
                                        if [ $cfgusername == $queue2  ]; then
284
 
                                                juju-log "IBM DB2: Changing the password for the user $cfgusername to $cfgpasswd"
285
 
                                                echo $cfgusername:$cfgpasswd | chpasswd
286
 
                                                passwdchanged=1
287
 
                                                juju-log "IBM DB2: Changed the password for the user $cfgusername to $cfgpasswd"
288
 
                                        fi
289
 
 
290
 
                                fi
291
 
                        fi
292
 
                done
293
 
                #If not available, add a new user
294
 
                if [ $passwdchanged != 1 ]; then
295
 
                        juju-log "IBM DB2: Creating New user"
296
 
                        create_newuser $servicename
297
 
                        juju-log "IBM DB2: Created New user"
298
 
                        juju-log $cfgusername
299
 
                fi
300
 
                #Creating a new instance for the new user if not existing
301
 
 
302
 
                for queue1 in `su - $DB2_USER -c 'db2ilist'`;
303
 
                do
304
 
                        if [ $queue1 == $cfgusername ]; then
305
 
                                instance_exists=1
306
 
                        fi
307
 
                done
308
 
                if [ $instance_exists != 1 ]; then
309
 
                        juju-log "IBM DB2: Creating new instance for the user"
310
 
                        /opt/ibm/db2/V10.5/instance/db2icrt -u $cfgusername $cfgusername
311
 
                        if [ $? = 0 ]; then
312
 
                                juju-log "IBM DB2: Created new instance for the user"
313
 
                        else
314
 
                                juju-log "IBM DB2: Creation of new instance failed"
315
 
                                exit 0
316
 
                        fi
317
 
                fi
318
 
 
319
 
                #Do the configuration for the new instance created
320
 
                new_port=50001
321
 
                free_port=0
322
 
                is_free=0
323
 
                #Check whether the port number is already in etc/services file, and then add it
324
 
                cd /etc
325
 
                if grep -q "db2c_$cfgusername" services
326
 
                then
327
 
                        juju-log "IBM DB2: Services file already updated"
328
 
                else
329
 
                        while [ $free_port == 0 ]
330
 
                        do
331
 
                                is_free=`netstat -lnp | grep $new_port | cut -d":" -f2 | cut -d" " -f1`
332
 
                                if [ "$is_free" != "$new_port" ]; then
333
 
                                        free_port=1
334
 
                                else
335
 
                                        new_port=$((new_port+1))
336
 
                                fi
337
 
                        done
338
 
                        juju-log "IBM DB2:Updating Services file"
339
 
                        echo -e "db2c_$cfgusername      $new_port/tcp" >> /etc/services
340
 
                        su - $cfgusername -c 'set db2instance=$cfgusername'
341
 
                        su - $cfgusername -c 'db2set DB2COMM=tcpip'
342
 
                        new_port=`echo $new_port | xargs`
343
 
                                #port_num_consumer=$new_port
344
 
                        su - $cfgusername -c 'db2 update dbm cfg using svcename '$new_port
345
 
                        /opt/ibm/db2/V10.5/instance/db2iupdt $cfgusername
346
 
                fi
347
 
                cd -
348
 
 
349
 
                #Start DB for the new user
350
 
                if su - $cfgusername -c 'ps -eaf|grep -i "\bdb2sysc 0\b" |grep -i '$cfgusername;
351
 
                then
352
 
                        juju-log "IBM DB2: DB2 already started for the user $cfgusername"
353
 
                else
354
 
                        su - $cfgusername -c 'db2start'
355
 
                        juju-log "IBM DB2: DB2 is started"
356
 
                fi
357
 
                #set_state 'db.dbstarted'
358
 
 
359
 
        else
360
 
                juju-log "IBM DB2: DB2 is not installed. Install DB2 before creating users"
361
 
                exit 0
362
 
 
363
 
        fi
364
 
        status-set maintenance "Created DB user and Instance"
365
 
}
366
 
 
367
 
create_newuser()
368
 
{
369
 
        servicename=$1
370
 
        dbcreated="False"
371
 
        #Add the new user
372
 
        get_cfgusername $servicename
373
 
        useradd -g db2grp1 -G dasadm1 -m $cfgusername
374
 
        if [ $? != 0 ]; then
375
 
                juju-log "IBM DB2: Addition of user $queue2 failed."
376
 
                status-set blocked "Addition of new used failed, Try again"
377
 
                exit 0
378
 
        fi
379
 
        #Set the password for the new user
380
 
        echo $cfgusername:$cfgpasswd | chpasswd
381
 
        if [ $? != 0 ]; then
382
 
                juju-log "IBM DB2: Setting password for user $queue2 failed."
383
 
                status-set blocked "Setting the password for user failed, Try again"
384
 
                exit 0
385
 
        fi
386
 
        #Check whether test DB already exists. If not create it
387
 
 
388
 
        dbname=`su - $DB2_USER -c 'db2 list db directory' | grep "Database name" | grep -i "\bTEST\b" | cut -d"=" -f2`
389
 
        dbname=`echo $dbname | xargs`
390
 
        if [ "$dbname" != "TEST" ]; then
391
 
                juju-log "IBM DB2: Creating DB Test"
392
 
                su - $DB2_USER -c 'db2 create database test'
393
 
                if [ $? != 0 ]; then
394
 
                        juju-log "IBM DB2: DB creation failed"
395
 
                        status-set blocked "DB creation failed, Try again"
396
 
                        exit 0
397
 
                fi
398
 
                juju-log "IBM DB2: Created DB Test"
399
 
        fi
400
 
        #Connect to the DB and grant data access
401
 
        su - $DB2_USER -c 'db2 connect to test;db2 grant dataaccess on database to user '$cfgusername
402
 
        if [ $? != 0 ]; then
403
 
                juju-log "IBM DB2: Grant access failed"
404
 
                status-set blocked "Grant dataaccess on database failed, Try again"
405
 
                exit 0
406
 
        fi
407
 
        su - $DB2_USER -c 'db2 connect to test;db2 disconnect test'
408
 
        if [ $? != 0 ]; then
409
 
                juju-log "IBM DB2: Disconnect DB failed"
410
 
                status-set blocked "Disconnect DB failed"
411
 
                exit 0
412
 
        fi
413
 
 
414
 
}
415
 
 
416
 
create_database()
417
 
{
418
 
        isdbcreated=0
419
 
        dbtocreate=$1
420
 
        servicename=$2
421
 
        get_cfgusername $servicename
422
 
        #create the db for $dbnametocreate if it doesn't exist
423
 
        dbname=`su - $cfgusername -c 'db2 list db directory' | grep "Database name" | grep -i "\b$dbtocreate\b" | cut -d"=" -f2`
424
 
        dbname=`echo $dbname | xargs`
425
 
        dbname=`echo "$dbname" | awk '{ print tolower($1) }'`
426
 
        dbtocreate=`echo "$dbtocreate" | awk '{ print tolower($1) }'`
427
 
        #check whether dbname is more than 8 chars
428
 
        if [ ${#dbtocreate} -gt 8 ]; then
429
 
                juju-log "IBM DB2: Failed to create DB $dbtocreate. DB name should be <= 8 chars"
430
 
                #status-set blocked "DB creation for $dbtocreate failed as it contains more than 8 chars"
431
 
        else
432
 
                if [ "$dbname" != "$dbtocreate" ]; then
433
 
                        juju-log "IBM DB2: Creating DB $dbtocreate"
434
 
                        su - $cfgusername -c 'db2 create database '$dbtocreate
435
 
                        if [ $? != 0 ]; then
436
 
                                juju-log "IBM DB2: DB creation failed"
437
 
                                status-set blocked "DB creation failed, Try again"
438
 
                                exit 0
439
 
                        fi
440
 
                        juju-log "IBM DB2: Created DB $dbtocreate"
441
 
                fi
442
 
                isdbcreated=1
443
 
        fi
444
 
}
445
 
 
446
 
create_dbs()
447
 
{
448
 
dbs_created=""
449
 
servicename=$1
450
 
dbnames=$(relation_call --state=db.connected get_dbnames $servicename) || true
451
 
 
452
 
#If dbnames is null, create a default DB as per service name. Else create the DBs requested by the related charm
453
 
if [ -z "$dbnames" ] ; then
454
 
        juju-log "IBM DB2: Creating a default DB $remoteunitdbname based on remote service name"
455
 
        create_database $remoteunitdbname $servicename
456
 
        if [ $isdbcreated == 1 ] ; then
457
 
                dbs_created+=$remoteunitdbname
458
 
                dbs_created+="."
459
 
        fi
460
 
elif [ "$dbnames" == "None" ]; then
461
 
        juju-log "IBM DB2: DB name caught as None, Creating default DB $remoteunitdbname based on remote service name"
462
 
        create_database $remoteunitdbname $servicename
463
 
        if [ $isdbcreated == 1 ] ; then
464
 
                dbs_created+=$remoteunitdbname
465
 
                dbs_created+="."
466
 
        fi
467
 
else
468
 
        juju-log "IBM DB2: Creating the DB names as passed by consumer"
469
 
        if [ `echo $dbnames | grep -c "," ` -gt 0 ]
470
 
        then
471
 
                #parse the $dbnames string to get the names of dbs to be created
472
 
                count=1
473
 
                db_to_create="test"
474
 
                while [ "$db_to_create" != "" ]
475
 
                do
476
 
                        db_to_create=`echo $dbnames | cut -d"," -f$count`
477
 
                        juju-log "IBM DB2:db_to_create is $db_to_create"
478
 
                        if [ "$db_to_create" != "" ] ; then
479
 
                                create_database $db_to_create $servicename
480
 
                                juju-log "Created DB $db_to_create"
481
 
                                if [ $isdbcreated == 1 ] ; then
482
 
                                        dbs_created+=$db_to_create
483
 
                                        dbs_created+="."
484
 
                                fi
485
 
                        fi
486
 
                        ((count=count+1))
487
 
                done
488
 
                juju-log "Suchitra - outside loop"
489
 
 
490
 
        else
491
 
                create_database $dbnames $servicename
492
 
                juju-log "Created DB $dbnames"
493
 
                dbs_created+=$dbnames
494
 
                dbs_created+="."
495
 
 
496
 
        fi
497
 
 
498
 
fi
499
 
isdbcreated=0
500
 
}
501
 
 
502
 
remove_sshkeys()
503
 
{
504
 
        servicename=$1
505
 
        #Read the key from the key.txt file and see whether it exists
506
 
        if grep -q "$servicename" $TEMP_KEY_FILE
507
 
        then
508
 
        key=`cat $TEMP_KEY_FILE | grep $servicename | cut -d":" -f2`
509
 
        grep -v "$key" $AUTH_KEY_FILE > tempfile.txt
510
 
        mv tempfile.txt $AUTH_KEY_FILE
511
 
        if grep -q "$key" $AUTH_KEY_FILE
512
 
        then
513
 
                juju-log "IBM DB2: SSH Key not deleted from $AUTH_KEY_FILE"
514
 
        else
515
 
                juju-log "IBM DB2: SSH Key deleted from $AUTH_KEY_FILE"
516
 
        fi
517
 
        #Remove from the temporary file as well
518
 
        grep -v "$servicename:$key" $TEMP_KEY_FILE > tempfile1.txt
519
 
        mv tempfile1.txt $TEMP_KEY_FILE
520
 
        if grep -q "$key" $TEMP_KEY_FILE
521
 
        then
522
 
                juju-log "IBM DB2: SSH Key not deleted from $TEMP_KEY_FILE"
523
 
        else
524
 
                juju-log "IBM DB2: SSH Key deleted from $TEMP_KEY_FILE"
525
 
        fi
526
 
 
527
 
fi
528
 
 
529
 
}
530
 
 
531
 
 
532
 
@when 'ibm-db2.installed'
533
 
@when_not 'ibm-base.license.accepted'
534
 
function uninstall(){
535
 
        juju-log "IBM DB2: Removing IBM DB2 (if installed) as the license agreement is not accepted."
536
 
        status-set maintenance "IBM DB2 is getting uninstalled"
537
 
        remove_software
538
 
        remove_state 'ibm-db2.installed'
539
 
        #remove_state 'ibm-db2.client.sshconfigured'
540
 
        set_state 'ibm-db2.uninstalled'
541
 
        status-set maintenance "IBM DB2 is uninstalled"
542
 
        rm -rf $TEMP_KEY_FILE
543
 
 
544
 
}
545
 
 
546
 
#Setting the interface states after uninstall
547
 
@when 'ibm-db2.uninstalled'
548
 
@when 'db.connected'
549
 
function reset_interface_states(){
550
 
        services=$(relation_call --state=db.connected services) || true
551
 
        for service in $services; do
552
 
                juju-log "IBM DB2: Resetting states for the service $service"
553
 
                relation_call --state=db.connected dismiss_sshconfigured $service || true
554
 
                relation_call --state=db.connected reset_states $service || true
555
 
        done
556
 
 
557
 
        remove_state 'ibm-db2.uninstalled'
558
 
}
559
 
 
560
 
 
561
 
@when_not 'ibm-db2.prereqsinstalled'
562
 
function init() {
563
 
        set -e
564
 
        juju-log "IBM DB2: Begin Install."
565
 
        apt-get update
566
 
        status-set maintenance "Installing pre requisites for IBM DB2"
567
 
        juju-log "IBM DB2: Installing binutils"
568
 
        juju-log "IBM DB2: Installed binutils"
569
 
        if [ "$ARCHITECTURE" != "x86_64" -a "$ARCHITECTURE" != "ppc64le" ]; then
570
 
                juju-log "IBM DB2: Unsupported platform. IBM DB2 installed with this Charm supports only the x86_64 and POWER LE (ppc64le) platforms."
571
 
                exit 1
572
 
        fi
573
 
 
574
 
        if [ "$ARCHITECTURE" == "x86_64" ]; then
575
 
                juju-log "IBM DB2: Installing 32 bit libstdc++.so.6"
576
 
                apt-get update
577
 
                apt-get install lib32stdc++6 -y
578
 
                juju-log "IBM DB2: Installed 32 bit libstdc++.so.6"
579
 
        fi
580
 
        if [ "$ARCHITECTURE" == "ppc64le" ]; then
581
 
                juju-log "IBM DB2:  Installing libnuma package for POWER LE (ppc64le) platforms."
582
 
                apt-get install libnuma-dev -y
583
 
                #Installing XL compiler runtime packages for Linux on Power as this is one of the prerequisite software/configuration requirements
584
 
                echo "deb http://public.dhe.ibm.com/software/server/POWER/Linux/rte/xlcpp/le/ubuntu $(lsb_release -s -c) main" | tee -a /etc/apt/sources.list.d/ibm-xlcpp-rte.list
585
 
                apt-get update
586
 
                apt-get install libxlc -y --force-yes
587
 
                juju-log "IBM DB2:  Installed libnuma package for POWER LE (ppc64le) platforms."
588
 
        fi
589
 
 
590
 
        #create the userids required for db2 installation
591
 
        DB2_USER=db2inst1
592
 
        DB2_USER_PW=db2inst1
593
 
 
594
 
        juju-log "IBM DB2: Creating the user ids for db2 installation"
595
 
        groupadd db2grp1
596
 
        groupadd dasadm1
597
 
        groupadd db2fgrp1
598
 
 
599
 
        useradd -g db2grp1 -G dasadm1 -m $DB2_USER
600
 
        echo $DB2_USER:$DB2_USER_PW | chpasswd
601
 
        useradd -g dasadm1 -G db2grp1 -m dasusr1
602
 
        echo dasusr1:dasusr1 | chpasswd
603
 
        useradd -g db2fgrp1 -m db2fenc1
604
 
        echo db2fenc1:db2fenc1 | chpasswd
605
 
 
606
 
        juju-log "IBM DB2: Created the user ids for db2 installation"
607
 
        status-set maintenance "Installed pre requisites for IBM DB2"
608
 
        set_state 'ibm-db2.prereqsinstalled'
609
 
 
610
 
}
611
 
@when 'ibm-db2.prereqsinstalled' 'ibm-base.license.accepted' 'ibm-base.curl.resource.fetched'
612
 
@when_not 'ibm-db2.installed'
613
 
function install() {
614
 
        juju-log "IBM DB2: License accepted"
615
 
        db2_inst=`is_db2_installed`
616
 
        if [[ ! -d $ARCHIVE_DIR ]]; then
617
 
                mkdir $ARCHIVE_DIR
618
 
        fi
619
 
        #Install if download succeeded
620
 
        if  [ $db2_inst == False ]; then
621
 
                if [ -f  $ARCHIVE_DIR/*.gz ]; then
622
 
 
623
 
                        cd $CHARM_DIR/files/archives
624
 
                        tar -zxvf $ARCHIVE_DIR/*.gz
625
 
                        if [ $? != 0 ]; then
626
 
                                juju-log "IBM DB2: Unable to extract the DB2 package content. Verify whether the package is corrupt."
627
 
                                status-set maintenance "Unable to extract the DB2 package content"
628
 
                                #Remove corrupt archive file
629
 
                                rm -rf $ARCHIVE_DIR/*.gz
630
 
                                exit 0
631
 
                        fi
632
 
                        if [ "$ARCHITECTURE" = "x86_64" ]; then
633
 
                                rsp_file=db2server.rsp
634
 
                        else
635
 
                                rsp_file=db2server_ppcle.rsp
636
 
                        fi
637
 
                        edit_responsefile
638
 
                        cd $CHARM_DIR/files/archives/server*
639
 
                        juju-log "IBM DB2: Edited the response file"
640
 
 
641
 
                        juju-log "IBM DB2: Checking etc/hosts file."
642
 
                        private_address=`unit-get private-address`
643
 
                        cd /etc
644
 
                        if grep -q "$private_address" hosts
645
 
                        then
646
 
                                juju-log "IBM DB2: Host file already updated"
647
 
                        else
648
 
                                juju-log "IBM DB2: Updating Host file"
649
 
                                echo "$private_address `hostname`" >> /etc/hosts
650
 
                        fi
651
 
                        juju-log "IBM DB2: Installing db2 package."
652
 
                        status-set maintenance "Installing IBM DB2"
653
 
                        cd -
654
 
                        ./db2setup -r $CHARM_DIR/files/archives/$rsp_file
655
 
                        juju-log "IBM DB2: Installation of db2 complete."
656
 
                fi
657
 
        fi
658
 
        # Configure system values  for Db2
659
 
        configure_system
660
 
        #Configure DFTDBPATH
661
 
        configure_dftdbpath
662
 
        echo "Tempory Key File" > $TEMP_KEY_FILE
663
 
        set_state 'ibm-db2.installed'
664
 
        remove_state 'ibm-db2.uninstalled'
665
 
        status-set active "IBM DB2 Installed"
666
 
}
667
 
 
668
 
@when 'config.changed.dftdbpath'
669
 
function configurevalues(){
670
 
        configure_dftdbpath
671
 
}
672
 
 
673
 
@when 'ibm-base.license.accepted'
674
 
@when 'db.connected'
675
 
function configure_sshkeys(){
676
 
        services=$(relation_call --state=db.connected services)
677
 
        for service in $services; do
678
 
                key=$(relation_call --state=db.connected get_sshkeys $service) || true
679
 
                juju-log "IBM DB2: Received the ssh key $key from the service $service"
680
 
 
681
 
                if [ "$key" == "None" ]; then
682
 
                        juju-log "IBM DB2: No data sent yet"
683
 
                        continue
684
 
                fi
685
 
 
686
 
                # Add the key to the authorized_keys file, if it exists
687
 
                set +e
688
 
                #Create a temp key file if it doesn't exist
689
 
                if [ -f $TEMP_KEY_FILE ]; then
690
 
                        juju-log "IBM DB2: Temp file already exists"
691
 
                else
692
 
                        echo "Temp Key File" > $TEMP_KEY_FILE
693
 
                fi
694
 
                if [ -f $AUTH_KEY_FILE ]; then
695
 
                        # Append only if the key is not present
696
 
                        juju-log "IBM DB2: Updating the key file"
697
 
                        grep "$key" $AUTH_KEY_FILE > /dev/null
698
 
                        if [ $? != 0 ]; then
699
 
                                juju-log "IBM DB2: Updating authorized keys file"
700
 
                                echo "$key" >> $AUTH_KEY_FILE
701
 
                                echo "$service:$key" >> $TEMP_KEY_FILE
702
 
                        else
703
 
                                juju-log "IBM DB2: Key already present"
704
 
                        fi
705
 
                else
706
 
                        juju-log "IBM DB2: Creating authorized keys file"
707
 
                        echo "$key" > $AUTH_KEY_FILE
708
 
                        echo "$service:$key" > $TEMP_KEY_FILE
709
 
                fi
710
 
                relation_call --state=db.connected set_sshconfigured $service || true
711
 
                juju-log "IBM DB2: Completed updating the authorized key"
712
 
                set -e
713
 
        done
714
 
}
715
 
 
716
 
@when 'db.ready'
717
 
@when 'ibm-db2.installed'
718
 
@when 'db.sshconfigured'
719
 
function create_consumer_dbs(){
720
 
        DB2_INSTALL_PATH=/opt/ibm/db2/V10.5
721
 
        services=$(relation_call --state=db.connected services)
722
 
        for service in $services; do
723
 
 
724
 
                key=$(relation_call --state=db.connected get_sshkeys $service) || true
725
 
                juju-log "IBM DB2: In create_consumer_dbs function, Received the ssh key $key from the service $service"
726
 
 
727
 
                if [ "$key" == "None" ]; then
728
 
                        juju-log "IBM DB2: No data sent yet"
729
 
                        continue
730
 
                fi
731
 
 
732
 
                
733
 
                #Create the user for the remote unit connected
734
 
                juju-log "IBM DB2: Creating the users for the consumer $service"
735
 
                create_user $service
736
 
 
737
 
                #Create the DBs after parsing
738
 
                create_dbs $service
739
 
                juju-log "IBM DB2: Create DBs completed"
740
 
 
741
 
                get_cfgusername $service
742
 
                service_name=`su - $cfgusername -c 'db2 get dbm cfg|grep -i svce|cut -d"=" -f2'`
743
 
                db2_port=`grep $service_name /etc/services | cut -d"/" -f1 | grep "db2c_$cfgusername" | cut -f2`
744
 
                db2_path="/opt/ibm/db2"
745
 
                hostname=`unit-get private-address`
746
 
                dbusername=$cfgusername
747
 
                dbuserpw=$cfgpasswd
748
 
                db2_instance_name=$cfgusername
749
 
 
750
 
 
751
 
                juju-log "IBM DB2: Sending the DB details to the consumer $service"
752
 
                juju-log "Port number: $db2_port"
753
 
                juju-log "Hostname: $hostname"
754
 
                juju-log "DBusername: $dbusername DBuserpw: $dbuserpw"
755
 
                juju-log "DB Instance name: $db2_instance_name"
756
 
                juju-log "Service: $service_name"
757
 
                juju-log "DBname: $dbs_created"
758
 
 
759
 
                relation_call --state=db.connected set_db_details $service $db2_path $db2_port $hostname $dbusername $dbuserpw $db2_instance_name $dbs_created || true
760
 
        done
761
 
}
762
 
 
763
 
@when 'db.departed'
764
 
function stop_db2(){
765
 
        juju-log "IBM DB2: Stopping the DB when relation is broken"
766
 
        services=$(relation_call --state=db.departed services) || true
767
 
        for service in $services; do
768
 
                juju-log "IBM DB2: Service name is $service"
769
 
                get_cfgusername $service
770
 
                if su - $cfgusername -c 'ps -eaf|grep -i "\bdb2sysc 0\b" |grep -i '$cfgusername
771
 
                then
772
 
                        su - $cfgusername -c 'db2stop'
773
 
                        juju-log "IBM DB2: DB2 stopped for instance $cfgusername"
774
 
                else
775
 
                        juju-log "IBM DB2: DB2 is already stopped for the instance $cfgusername"
776
 
                fi
777
 
                #Remove the ssh key entry from ssh keys file
778
 
                remove_sshkeys $service
779
 
                relation_call --state=db.departed dismiss_sshconfigured $service || true
780
 
                relation_call --state=db.departed dismiss $service || true
781
 
        done
782
 
}
783
 
 
784
 
 
785
 
reactive_handler_main