51 | | List and explain final hook script here! |
| 51 | #!/bin/bash |
| 52 | # |
| 53 | # Example event hook for the OML server, copying an Sqlite database elsewhere |
| 54 | # when the last client has exited. |
| 55 | # Copyright 2012-2013 National ICT Australia (NICTA), Australia |
| 56 | # |
| 57 | # Permission is hereby granted, free of charge, to any person obtaining a copy |
| 58 | # of this software and associated documentation files (the "Software"), to deal |
| 59 | # in the Software without restriction, including without limitation the rights |
| 60 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 61 | # copies of the Software, and to permit persons to whom the Software is |
| 62 | # furnished to do so, subject to the following conditions: |
| 63 | # |
| 64 | # The above copyright notice and this permission notice shall be included in |
| 65 | # all copies or substantial portions of the Software. |
| 66 | # |
| 67 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 68 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 69 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 70 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 71 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 72 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 73 | # THE SOFTWARE. |
| 74 | # |
| 75 | irodsUserName=rods |
| 76 | irodsHost=emmy9.casa.umass.edu |
| 77 | irodsPort=1247 |
| 78 | irodsZone=geniRenci |
| 79 | HOME=/home/oml2 |
| 80 | export irodsUserName irodsHost irodsPort irodsZone HOME |
| 81 | |
| 82 | LOGFILE=/tmp/oml2-server-hook.log |
| 83 | function log () |
| 84 | { |
| 85 | echo "$@" >&2 |
| 86 | echo "$@" >> ${LOGFILE} |
| 87 | } |
| 88 | |
| 89 | # XXX: You might need to initialise the iRODS password for the UNIX user |
| 90 | # running tho oml2-server by running 'iinit' to create ~/.irods/.irodsA on its |
| 91 | # behalf for iput to work |
| 92 | IPUT=/usr/bin/iput |
| 93 | SQLITE3=sqlite3 |
| 94 | PGDUMP=pg_dump |
| 95 | |
| 96 | echo "OML HOOK READY" |
| 97 | log "OML HOOK READY" |
| 98 | |
| 99 | while read COMMAND ARGUMENTS; do |
| 100 | # One report line must be printed in each control path; |
| 101 | # this first one puts out a timestamp and a dump of the received command, but no newline |
| 102 | log -n "`date`: ${COMMAND} ${ARGUMENTS}: " |
| 103 | case "${COMMAND}" in |
| 104 | "DBCLOSED") |
| 105 | case "${ARGUMENTS}" in |
| 106 | file:*) |
| 107 | DBFILE=${ARGUMENTS/file:/} |
| 108 | log "${IPUT} ${OPTION} ${DBFILE}" |
| 109 | NAME=${DBFILE:14:6}; |
| 110 | FILE=${DBFILE:14}; |
| 111 | LENGTH=${#FILE} |
| 112 | SLICE=${FILE:0:$LENGTH-4} |
| 113 | DATE=`date` |
| 114 | log "b db ${DBFILE} closed, pushing to iRODS..." |
| 115 | ${IPUT} -f ${DBFILE} /geniRenci/home/$NAME/ #$FILE |
| 116 | log "an iRODS operation finished" |
| 117 | ;; |
| 118 | postgresql://*) |
| 119 | # Separate the components of the URI by gradually eating them off the TMP variable |
| 120 | |
| 121 | DOMAIN=${ARGUMENTS//*\//} # cut everything before the final '/' |
| 122 | USERNAME=${DOMAIN/-*/} # get the first part before the '-' |
| 123 | REST=${DOMAIN/$USERNAME-/} # remove the username from the rest |
| 124 | EXPNAME=${REST/-*/} # same as for the username |
| 125 | TIMESTAMP=${REST//*-/} # get the last part after the '-' |
| 126 | |
| 127 | |
| 128 | TMP="${ARGUMENTS/postgresql:\/\//}" |
| 129 | USER=${TMP/@*/} |
| 130 | TMP=${TMP/${USER}@/} |
| 131 | HOST=${TMP/:*/} |
| 132 | TMP=${TMP/${HOST}:/} |
| 133 | PORT=${TMP/\/*/} |
| 134 | TMP=${TMP/${PORT}\//} |
| 135 | DBNAME=${TMP} |
| 136 | DBFILE=${DBNAME}.`date +%Y-%m-%d_%H:%M:%S%z`.pg.sql |
| 137 | log "PostgreSQL DB ${DBNAME} closed, dumping as ${DBFILE} and pushing to iRODS" |
| 138 | log "User ${USER} Host ${HOST} Port ${PORT} DBNAME ${DBNAME} Home ${HOME}" |
| 139 | ${PGDUMP} -U ${USER} -h ${HOST} -p ${PORT} ${DBNAME} > /tmp/${DBFILE} |
| 140 | log "Before IPUT" |
| 141 | log "${IPUT} -f /tmp/${DBFILE} /geniRenci/home/rods/" |
| 142 | log `${IPUT} -V -f /tmp/${DBFILE} /geniRenci/home/rods/ 2>&1` |
| 143 | # ${IPUT} -f /tmp/${DBFILE} /geniRenci/home/rods/ |
| 144 | ${IPUT} -f /tmp/${DBFILE} /geniRenci/home/${USERNAME}/experiments/${EXPNAME}-${TIMESTAMP}/measurements.sql |
| 145 | log "After IPUT" |
| 146 | ;; |
| 147 | *) |
| 148 | log "DB ${ARGUMENTS} closed, but don't know how to handle it" |
| 149 | ;; |
| 150 | esac |
| 151 | ;; |
| 152 | "EXIT") |
| 153 | log "Exiting" |
| 154 | exit 0 |
| 155 | ;; |
| 156 | *) |
| 157 | log "Unknown command" |
| 158 | ;; |
| 159 | esac |
| 160 | done |
| 161 | |