#!/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