~ibmcharmers/charms/trusty/layer-ibm-platform-lsf-server/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/bin/bash
set -ex

source charms.reactive.sh

ARCHIVE_DIR=$CHARM_DIR/files/archives
lsf_top=/usr/share/lsf
LSF_ADMIN=lsfadmin
hostname_server=`hostname`
ip_server=`unit-get private-address`
ARCHITECTURE=`uname -m`

@when_not 'ibm-platform-lsf-server.nfsclient.installed'
function install_nfsclient(){
         juju-log "IBM Platform LSF: Pre-reqs will be installed."
         # To check for unsupported platforms
         if [ "$ARCHITECTURE" != "x86_64" -a "$ARCHITECTURE" != "ppc64le" ]; then
           juju-log "IBM Platform LSF: Unsupported platform. IBM Platform LSF installed with this Charm supports only the x86_64 platform and POWER LE (ppc64le) platforms."
           exit 1
         fi
         # Install nfs-common using apt
         apt-get install nfs-common -y
         set_state 'ibm-platform-lsf-server.nfsclient.installed'
         remove_state 'ibm-platform-lsf-server.nfsclient.ready'
         remove_state 'ibm-platform-lsf-server.conf.ready'
         juju-log "IBM Platform LSF : Pre-reqs installed successfully"
}

function lsfstart(){
         juju-log "IBM Platform LSF : Starting Platform LSF cluser....."
         lsadmin limstartup
         lsadmin resstartup
         badmin hstartup
         if [ "$?" != "0" ]; then
           juju-log "IBM Platform LSF : Error while starting  LSF Cluster"
         else
          juju-log "IBM Platform LSF : LSF Cluster started successfully"
         fi
}


# Function for adding LSF Server hostname details in /etc/hosts file
function add_hostname(){
         # Block of code to get the IP and hostname of Server Host machine
         juju-log "IBM Platform LSF : Server host Hostname is : $hostname_server"
         juju-log "IBM Platform LSF : Server host IP is : $ip_server"
         # Adding hostname entry in hosts file
         cd /etc
         if grep -w "$ip_server $hostname_server" hosts
         then
             juju-log "IBM Platform LSF : Host file already updated"                                         
         else
             juju-log "IBM Platform LSF : Updating Host file"                                           
             echo "$ip_server $hostname_server" >> /etc/hosts
         fi
}


# Function to find lsf version and distribution type
function find_version_distr(){
         # To find the lsf version
         lsf_version=`grep "LSF_VERSION" $lsf_top/conf/lsf.conf | awk -F'=' '{print $2}'`
         # To determine the distribution type of host node
         ARCHITECTURE=`uname -m`
         cd $lsf_top/$lsf_version
         if [ "$ARCHITECTURE" = "x86_64" ]; then
           distribution_OS_type=`ls -lrt | grep x86_64 | awk -F' ' '{print $9}'`
         elif [ "$ARCHITECTURE" = "ppc64le" ]; then
           distribution_OS_type=`ls -lrt | grep ppc64le | awk -F' ' '{print $9}'`
         fi
}


# Block of code for adding the server host to the LSF storage server and share the LSF installation binaries using NFS.
function nfs_sharelsf(){
         # Using NFS , share the LSF file location installed on storage server and mount it on master host
         ip_storage=$1
         juju-log "IBM Platform LSF : IP storage is : $ip_storage"
         if  [ -d $lsf_top ] ; then
            juju-log "IBM Platform LSF : LSF Directory already exists" 
         else
            mkdir $lsf_top    
         fi
           
         if grep -q "$ip_storage:$lsf_top" /etc/fstab
         then
             juju-log "IBM Platform LSF : Entries already exist in /etc/fstab for mounting"
         else
             echo "$ip_storage:$lsf_top $lsf_top nfs rw 0 0" >> /etc/fstab 
         fi

         mount -a
         if [ "$?" != "0" ]; then
           juju-log "IBM Platform LSF : Error while mounting the LSF Directory, check the /etc/fstab or check whether the Storage Server is ready"
           exit 1
         else
           juju-log "IBM Platform LSF : Successfully mounted the LSF Directory"
         fi
}


# Function for creating lsf user and sudoers file
function createuser_conf(){
         # checking whether lsfadmin already exists or not
         cd /etc
         if grep -q "lsfadmin" passwd 
         then
             juju-log "IBM Platform LSF : User lsfadmin already exists"
         else
             useradd -m -d /home/$LSF_ADMIN $LSF_ADMIN
             if [ $? == 0 ]; then
               juju-log "IBM Platform LSF : LSF administrator lsfadmin user created successfully"
             else
               juju-log "IBM Platform LSF : Error while creating the LSF administrator user "
               exit 0
             fi
         fi
       
         find_version_distr
         cd /etc
         if [[ "`grep "LSF_STARTUP_USERS" lsf.sudoers`" ]] && [[ "`grep "LSF_STARTUP_PATH" lsf.sudoers`" ]]; then
           juju-log "IBM Platform LSF : lsf.sudoers file already updated with values"
         else               
           touch lsf.sudoers
           echo "LSF_STARTUP_PATH=$lsf_top/$lsf_version/$distribution_OS_type/etc" >> lsf.sudoers
           sed -i '1i LSF_STARTUP_USERS="lsfadmin"' lsf.sudoers
           chmod 600 lsf.sudoers
           juju-log "IBM Platform LSF : lsf.sudoers file updated."
         fi   
}


@when_not 'storage.joined' 'ibm-platform-lsf-server.nfsclient.ready'
function notify_user(){
         juju-log "IBM Platform LSF : Please add a relation between LSF Storage and LSF Master charm to have a working LSF Master"
         status-set waiting "Waiting for relation with LSF Storage/Details not sent from Storage Server"
}

# When relation is established between LSF Storage and LSF Server
@when 'storage.available' 
@when_not 'ibm-platform-lsf-server.nfsclient.ready'
function configure_lsfserver(){
        juju-log "IBM Platform LSF : Getting connected to Storage Server and mounting of shared LSF installation files"
        ip=$(relation_call --state=storage.available private_address) || true 
        juju-log "IBM Platform LSF : IP of LSF Storage Server as $ip"
        hostname_storage=$(relation_call --state=storage.available get_hostname) || true
        juju-log "IBM Platform LSF : Hostname of LSF Storage Server as $hostname_storage"
        add_hostname
        nfs_sharelsf $ip
        createuser_conf
        # Set state that now NFS client is ready and files are mounted.
        juju-log "IBM Platform LSF : NFS Client is ready now and LSF Files have been shared."
        status-set maintenance "NFS client is ready and LSF Files shared"
        set_state 'ibm-platform-lsf-server.nfsclient.ready'          
}


@when_not 'master.joined' 
function notify_user(){
         juju-log "IBM Platform LSF : Please add a relation between LSF Server and LSF Master charm to add the LSF Server to the LSF Cluster"
         status-set waiting "Waiting for relation with LSF Master/Details not sent from LSF Master"
}


# When relation is added between LSF Server and Master, share the hostname of LSF Server
@when 'master.joined'
function send_hostname_master(){
         juju-log "IBM Platform LSF : Sending LSF Server Hostname to LSF Master"
         relation_call --state=master.joined set_hostname $hostname_server || true 
}


# When LSF Master is ready and reconfigured LSF Daemons and NFS client is also ready, then start the LSF Server Daemons to make the LSF Server up and running.
@when 'master.MasterReConfigReady' 'ibm-platform-lsf-server.nfsclient.ready'
function configure_lsfserver_master(){
         count=0
         count1=0
         juju-log "IBM Platform LSF : Configuring LSF Server when LSF Master is ready" 
         ip_master=$(relation_call --state=master.MasterReConfigReady private_address) || true 
         juju-log "IBM Platform LSF : IP of LSF master Server as $ip_master"
         hostname_master=$(relation_call --state=master.MasterReConfigReady get_hostname) || true
         juju-log "IBM Platform LSF : Hostname of LSF Master Server as $hostname_master"
         
         # First add the host details of LSF Master in /etc/hosts file before starting the LSF Deamons.
         for master_ip in $ip_master
         do
           for master_hostname in $hostname_master
           do
             if [ $count == $count1 ]; then
               cd /etc
               if grep -w "$master_ip $master_hostname" hosts
               then
                   juju-log "IBM Platform LSF : Host file already updated with host information of the LSF Server"                                         
               else
                   juju-log "IBM Platform LSF : Updating Host file"                                           
                   echo "$master_ip $master_hostname" >> /etc/hosts
               fi
             fi
           count1=$((count1+1))
           done
         count=$((count+1))
         count1=0
         done 
         # Getting the flag value to indicate LSF Master has reconfigured itself or not.
         reconfigFlag_master=$(relation_call --state=master.MasterReConfigReady notify_server) || true 
         for master_flag in $reconfigFlag_master
         do
          if [ "$master_flag" == "MasterReConfigReady" ]; then
            # start the lsf services on the server
            start_lsf   
            set_state 'ibm-platform-lsf-server.conf.ready'   
            status-set active "LSF Server is ready"
          fi
         done
}


# When LSF Server configuration is done and LSF Master is reconfigured, start LSF Services on the server.
function start_lsf(){
         # Check if lsf files are available
         if ! [ -d "$lsf_top/conf" ]; then
            juju-log "IBM Platform LSF : LSF Install Directory does not exist"
            exit 0
         fi
         find_version_distr
         juju-log "IBM Platform LSF : Setting the LSF profile path"
         set +e
         . $lsf_top/conf/profile.lsf
         set -e
         # Starting the LSF cluster
         cd $lsf_top/$lsf_version/$distribution_OS_type/bin
         lim_chk=`ps -ef | grep -w $lsf_top/$lsf_version/$distribution_OS_type/etc/lim | awk '{print $8}' | head -n 1`
         res_chk=`ps -ef | grep -w $lsf_top/$lsf_version/$distribution_OS_type/etc/res | awk '{print $8}' | head -n 1`
         if [ "$lim_chk" == "$lsf_top/$lsf_version/$distribution_OS_type/etc/lim" ] && [ "$res_chk" == "$lsf_top/$lsf_version/$distribution_OS_type/etc/res" ]; then
           juju-log "IBM Platform LSF : LSF Daemons are already running"
           # Restart them
           stop_lsfserver
           lsfstart
         else
           lsfstart
         fi
}


# Stop the LSF Services once service is removed or relation is broken
function stop_lsfserver(){
         # Check if lsf files are available
         if ! [ -d "$lsf_top/conf" ]; then
            juju-log "IBM Platform LSF : LSF Install Directory does not exist"
            exit 0
         fi
         juju-log "IBM Platform LSF : Stopping the LSF Daemons on Server.........."
         find_version_distr
         juju-log "IBM Platform LSF : Setting the LSF profile path"
         set +e
         . $lsf_top/conf/profile.lsf
         set -e
         # check before stopping its already running or not
         juju-log "IBM Platform LSF : Stopping LSF cluster......"
         cd $lsf_top/$lsf_version/$distribution_OS_type/bin
         lim_chk=`ps -ef | grep -w $lsf_top/$lsf_version/$distribution_OS_type/etc/lim | awk '{print $8}' | head -n 1`
         if [ "$lim_chk" != "$lsf_top/$lsf_version/$distribution_OS_type/etc/lim" ] && [ "$res_chk" != "$lsf_top/$lsf_version/$distribution_OS_type/etc/res" ]; then
           juju-log "IBM Platform LSF : LSF Daemons are already stopped"
         else
           badmin hshutdown || true
           lsadmin resshutdown || true
           lsadmin limshutdown || true
           if [ "$?" != "0" ]; then
            juju-log "IBM Platform LSF : Error while stopping LSF Cluster"
           else
            juju-log "IBM Platform LSF : LSF Cluster stopped successfully"
           fi
         fi
}


# When relation between LSF Master and LSF Server is departed, stop the LSF Deamons and remove entry from lsf.cluster.cluster1 file.
@when 'master.departed' 'ibm-platform-lsf-server.conf.ready'
function remove_serverhost(){
         # Stop the LSF Daemons first
         stop_lsfserver
         # Remove entry from LSF.cluster file
         sed -i  '/'$hostname_server'/d' $lsf_top/conf/lsf.cluster.cluster1
         remove_state 'ibm-platform-lsf-server.conf.ready'
         relation_call --state=master.departed dismiss_departed || true 
}

# When relation between LSF Storage Server and LSF Server is departed. If relation with LSF Master exists, first remove that then unmount the LSF Files.
@when_none 'ibm-platform-lsf-server.conf.ready'
@when 'storage.departing' 'ibm-platform-lsf-server.nfsclient.ready'
function nfsstorage_depart(){
         remove_serverhost
         mount_flag=`stat -f -L -c %T $lsf_top` || true

         if  [ "$mount_flag" == "ext2/ext3" ]; then
            juju-log "IBM Platform LSF : /usr/share/lsf directory is not mounted yet, cannot proceed with unmounting"
            exit 0
         fi
         if ! [ -d $lsf_top/conf ]; then
             juju-log "IBM Platform LSF : No LSF folders , nothing to unmount"
         else
             cd /home
             set +e
             umount -f -l $lsf_top 
             set -e
             juju-log " IBM Platform LSF : Unmounted the LSF installation folders, now LSF folders will not be accessible"
             sed -i  "\|$lsf_top|d" /etc/fstab     
             remove_state 'ibm-platform-lsf-server.nfsclient.ready'
         fi
}

reactive_handler_main