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

« back to all changes in this revision

Viewing changes to reactive/ibm-db2.sh

  • Committer: Suchitra Venugopal
  • Date: 2016-05-04 09:59:42 UTC
  • Revision ID: suchvenu@in.ibm.com-20160504095942-f3hp9a4iydhbnw3i
DB2 layered charm

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