GIMI-GEC16-Tutorials/GIMI-GEC16-TutorialB/Orchestrate: oml2-server-hook.sh

File oml2-server-hook.sh, 3.8 KB (added by divyashri.bhat@gmail.com, 6 years ago)
Line 
1#!/bin/bash
2#
3# Example event hook for the OML server, copying an Sqlite database elsewhere
4# when the last client has exited.
5# Copyright 2012-2013 National ICT Australia (NICTA), Australia
6#
7# Permission is hereby granted, free of charge, to any person obtaining a copy
8# of this software and associated documentation files (the "Software"), to deal
9# in the Software without restriction, including without limitation the rights
10# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11# copies of the Software, and to permit persons to whom the Software is
12# furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included in
15# all copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23# THE SOFTWARE.
24#
25irodsUserName=rods
26irodsHost=emmy9.casa.umass.edu
27irodsPort=1247
28irodsZone=geniRenci
29HOME=/home/oml2
30export irodsUserName irodsHost irodsPort irodsZone HOME
31
32LOGFILE=/tmp/oml2-server-hook.log
33function log ()
34{
35        echo "$@" >&2
36        echo "$@" >> ${LOGFILE}
37}
38
39# XXX: You might need to initialise the iRODS password for the UNIX user
40# running tho oml2-server by running 'iinit' to create ~/.irods/.irodsA on its
41# behalf for iput to work
42IPUT=/usr/bin/iput
43SQLITE3=sqlite3
44PGDUMP=pg_dump
45
46echo "OML HOOK READY"
47log "OML HOOK READY"
48
49while read COMMAND ARGUMENTS; do
50        # One report line must be printed in each control path;
51        # this first one puts out a timestamp and a dump of the received command, but no newline
52        log -n "`date`: ${COMMAND} ${ARGUMENTS}: "
53        case "${COMMAND}" in
54                "DBCLOSED")
55                        case "${ARGUMENTS}" in
56                                file:*)
57                                        DBFILE=${ARGUMENTS/file:/}
58                                        log "${IPUT} ${OPTION} ${DBFILE}"
59                                        NAME=${DBFILE:14:6};
60                                        FILE=${DBFILE:14};
61                                        LENGTH=${#FILE}
62                                        SLICE=${FILE:0:$LENGTH-4}
63                                        DATE=`date`
64                                        log "b db ${DBFILE} closed, pushing to iRODS..."
65                                        ${IPUT} -f ${DBFILE} /geniRenci/home/$NAME/ #$FILE
66                                        log "an iRODS operation finished"
67                                        ;;
68                                postgresql://*)
69                                        # Separate the components of the URI by gradually eating them off the TMP variable
70                                       
71                                        DOMAIN=${ARGUMENTS//*\//}       # cut everything before the final '/'
72                                        USERNAME=${DOMAIN/-*/}          # get the first part before the '-'
73                                        REST=${DOMAIN/$USERNAME-/}      # remove the username from the rest
74                                        EXPNAME=${REST/-*/}             # same as for the username
75                                        TIMESTAMP=${REST//*-/}          # get the last part after the '-'
76
77                                               
78                                        TMP="${ARGUMENTS/postgresql:\/\//}"
79                                        USER=${TMP/@*/}
80                                        TMP=${TMP/${USER}@/}
81                                        HOST=${TMP/:*/}
82                                        TMP=${TMP/${HOST}:/}
83                                        PORT=${TMP/\/*/}
84                                        TMP=${TMP/${PORT}\//}
85                                        DBNAME=${TMP}
86                                        DBFILE=${DBNAME}.`date +%Y-%m-%d_%H:%M:%S%z`.pg.sql
87                                        log "PostgreSQL DB ${DBNAME} closed, dumping as ${DBFILE} and pushing to iRODS"
88                                        log "User ${USER} Host ${HOST} Port ${PORT} DBNAME ${DBNAME} Home ${HOME}"
89                                        ${PGDUMP} -U ${USER} -h ${HOST} -p ${PORT} ${DBNAME} > /tmp/${DBFILE}
90                                        log "Before IPUT"
91                                        log "${IPUT} -f /tmp/${DBFILE} /geniRenci/home/rods/"
92                                        log `${IPUT} -V -f /tmp/${DBFILE} /geniRenci/home/rods/ 2>&1`
93                                        # ${IPUT} -f /tmp/${DBFILE} /geniRenci/home/rods/
94                                        ${IPUT} -f /tmp/${DBFILE} /geniRenci/home/${USERNAME}/experiments/${EXPNAME}-${TIMESTAMP}/measurements.sql
95                                        log "After IPUT"
96                                        ;;
97                                *)
98                                        log "DB ${ARGUMENTS} closed, but don't know how to handle it"
99                                        ;;
100                        esac
101                        ;;
102                "EXIT")
103                        log "Exiting"
104                        exit 0
105                        ;;
106                *)
107                        log "Unknown command"
108                        ;;
109        esac
110done