From 4c94f78a968e4183c16e29f8a32758cfed3df86d Mon Sep 17 00:00:00 2001 From: kapeller Date: Wed, 21 May 2014 08:33:20 +0000 Subject: [PATCH] inital version --- shellbox_from_db | 85 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100755 shellbox_from_db diff --git a/shellbox_from_db b/shellbox_from_db new file mode 100755 index 0000000..e7296fa --- /dev/null +++ b/shellbox_from_db @@ -0,0 +1,85 @@ +#!/bin/bash +############################################################################### +# Description: +# Get softiocs parameters like port, user, directory, command, needed to create +# a configuration file for the shellbox service. +# Create/update /etc/shellbox.d/.conf files +# +# $Id: shellbox_from_db,v 1.1 2014/05/21 08:33:20 kapeller Exp $ +# $Source: /cvs/G/EPICS/App/scripts/shellbox_from_db,v $ +############################################################################### +# + +PATH="/usr/bin:/bin:/usr/sbin:/sbin" +SHELLBOX_DIR="/etc/shellbox.d" + +# If user is not a real person, send notification to the following email addresses: +DEFAULT_EMAIL="rene.kapeller@psi.ch, renata.krempaska@psi.ch" + +hostname=$(hostname -s) +progname=$(basename $0) +pid=$$ +tmpfile="/tmp/$progname.tmp" +lockfile="/var/tmp/${progname}.lck" + +############################################################################### +### functions + +unlock() { + rm -rf $lockfile + exit 1 +} + +lock() { + if [ -e $lockfile ]; then + sub_perr "found $lockfile" + exit 1 + else + touch $lockfile + trap unlock EXIT TERM INT + fi +} + +############################################################################### +### main + +lock + +if (wget -qO $tmpfile https://inventory.psi.ch/soap/softioc_info.aspx?host=$hostname); then + mkdir -p $SHELLBOX_DIR + rm $SHELLBOX_DIR/.[0-9]*.conf + while IFS=$'\n' read line; do + if [ -n "$line" ]; then + port=$(echo $line | cut -d ' ' -f 1) + user=$(echo $line | cut -d ' ' -f 2) + email=$(ldapsearch -x -H ldap://d.psi.ch -b "ou=Users,ou=PSI,dc=d,dc=psi,dc=ch" "(cn=$user)" mail | grep @ | awk '{print $2}') + if [ -z "$email" ]; then + email=$DEFAULT_EMAIL + fi + new_file="$SHELLBOX_DIR/.${port}.conf" + old_file="$SHELLBOX_DIR/${port}.conf" + echo "#" > $new_file + echo "# This file is generated by $progname." >> $new_file + echo "# Manual changes will get lost!" >> $new_file + echo "#" >> $new_file + echo "$line" >> $new_file + if (! cmp -s $new_file $old_file); then + cp $new_file $old_file + mail -s "$hostname - $progname - $old_file changed" $email < $old_file + fi + fi + done < $tmpfile +fi +rm $tmpfile + +# Delete what is not in the database +cd $SHELLBOX_DIR +for file in [0-9]*.conf; do + if [ -e $file -a ! -e .$file ]; then + rm $file + fi +done + +unlock + +### EOF