#!/bin/bash # # priority: 850 # # Manage settings for the RT installtion # # Stephen Soltesz # Copyright (C) 2008 The Trustees of Princeton University # # $Id$ # # Source function library and configuration . /etc/plc.d/functions . /etc/planetlab/plc_config local_config=/etc/planetlab/configs/site.xml PLCRTPATH=/usr/share/plcrt function gethostbyaddr () { python -c 'import socket; import sys; print socket.gethostbyaddr(sys.argv[1])[0]' $1 2>/dev/null } # Be verbose set -x # Default locations PGDATA=/var/lib/pgsql/data postgresql_conf=$PGDATA/postgresql.conf pghba_conf=$PGDATA/pg_hba.conf # Export so that we do not have to specify -p to psql invocations export PGPORT=$PLC_DB_PORT RT3_DB_USER="rt3user" RT3_DB_NAME="rt3" WROTE_PG_CONFIG= if [ -z "$PLC_RT_IP" ] ; then PLC_RT_IP=$( gethostbyname $PLC_RT_HOST ) fi function check_rt_password () { if [[ -z "$PLC_RT_DBPASSWORD" || "$PLC_RT_DBPASSWORD" = "None" ]] ; then # NOTE: this provides a simpler string than uuidgen PLC_RT_DBPASSWORD=$( uuidgen | md5sum - | awk '{print $1}' ) plc-config --category=plc_rt --variable=dbpassword --value="$PLC_RT_DBPASSWORD" --save=$local_config $local_config service plc reload CREATED="true" fi } # NOTE: code duplicated from monitor.functions to allow package to be separate # from it. function check_pg_hba () { NAME=$1 USER=$2 #### SETUP ACCESS to this user and database mkdir -p $PGDATA/pg_hba.conf.d CONF=$PGDATA/pg_hba.conf.d/${NAME}.conf if [ ! -f $CONF ] ; then echo "host $NAME $USER 127.0.0.1/32 password" > $CONF echo "host $NAME $USER $PLC_RT_IP/32 password" >> $CONF WROTE_PG_CONFIG="true" fi } # TODO: make values re-configurable... this may be an issue with RT's db, though. function update_config () { pattern=$1 with=$2 file=$3 sed -i -e "s/$pattern/$with/g" $file } function apply_template () { TEMPLATE=$1 DESTFILE=$2 tmp_file=$(mktemp) cp $TEMPLATE $tmp_file update_config PLC_RT_HOSTNAME $PLC_RT_HOST $tmp_file update_config PLC_RT_CC_ADDRESS $PLC_RT_CC_ADDRESS $tmp_file update_config PLC_NAME "$PLC_NAME" $tmp_file update_config PLC_RT_HOSTNAME $PLC_RT_HOST $tmp_file update_config PLC_WWW_HOSTNAME $PLC_WWW_HOST $tmp_file update_config RT_DB_NAME $RT3_DB_NAME $tmp_file update_config RT_DB_USER $RT3_DB_USER $tmp_file update_config RT_DB_PASSWORD $PLC_RT_DBPASSWORD $tmp_file # setup initialdata update_config PLC_RT_HOSTNAME $PLC_RT_HOST $tmp_file cp $tmp_file $DESTFILE rm -f $tmp_file } function check_rt_templates () { for f in $PLCRTPATH/conf.d/*.pl ; do mkdir -p /etc/rt3/conf.d if [ $f -nt /etc/rt3/conf.d/`basename $f` ] ; then apply_template $f /etc/rt3/conf.d/`basename $f` fi done for f in $PLCRTPATH/setup.d/*.{pl,py,sh} ; do mkdir -p /etc/rt3/setup.d if [ $f -nt /etc/rt3/setup.d/`basename $f` ] ; then apply_template $f /etc/rt3/setup.d/`basename $f` chmod 751 /etc/rt3/setup.d/`basename $f` fi done # TODO: need a better approach for this. if [ -f /etc/httpd/conf.d/rt3.conf ] ; then update_config PLC_RT_HOST $PLC_RT_HOST /etc/httpd/conf.d/rt3.conf fi # if the templates are newer than the actual config, then replace them. if [ $PLCRTPATH/RT_SiteConfig.pm -nt /etc/rt3/RT_SiteConfig.pm ] ; then # copy templates apply_template $PLCRTPATH/RT_SiteConfig.pm /etc/rt3/RT_SiteConfig.pm apply_template $PLCRTPATH/initialdata /etc/rt3/initialdata chmod 644 /etc/rt3/RT_SiteConfig.pm chmod 644 /etc/rt3/initialdata fi } function check_rt_custom () { rsync -qv -az $PLCRTPATH/local/html /usr/share/rt3 } function check_rt_pghba () { NAME=$RT3_DB_NAME USER=$RT3_DB_USER CONF=$PGDATA/pg_hba.conf.d/${NAME}.conf PATTERN="host all postgres 127.0.0.1/32 trust" if ! grep -q "$PATTERN" $CONF ; then #### SETUP ACCESS from postgres user to run init for the first time. echo "$PATTERN" >> $CONF WROTE_PG_CONFIG="true" fi PATTERN="host all postgres $PLC_RT_IP/32 trust" if ! grep -q "$PATTERN" $CONF ; then #### SETUP ACCESS from postgres user to run init for the first time. echo "$PATTERN" >> $CONF WROTE_PG_CONFIG="true" fi } function check_rt_aliases () { if ! grep -q "rt-mailgate --queue support" /etc/aliases ; then sed -i -e "s/^support.*postmaster//g" /etc/aliases sed -i -e "s/^security.*root//g" /etc/aliases cat <> /etc/aliases # added by RT init scripts for default queues. support: "|/usr/sbin/rt-mailgate --queue support --action correspond --url http://$PLC_RT_HOST/rt3/" monitor: "|/usr/sbin/rt-mailgate --queue monitor --action correspond --url http://$PLC_RT_HOST/rt3/" security: "|/usr/sbin/rt-mailgate --queue security --action correspond --url http://$PLC_RT_HOST/rt3/" legal: "|/usr/sbin/rt-mailgate --queue legal --action correspond --url http://$PLC_RT_HOST/rt3/" EOF /usr/bin/newaliases fi } function check_rt_init () { if [ ! -f /etc/rt3/setup.finished ] ; then /usr/sbin/rt-setup-database --action init --dba postgres for f in /etc/rt3/conf.d/*.pl ; do /usr/sbin/rt-setup-database --action insert --dba postgres --datafile $f done # run initial setup scripts (run only once, or for the first time) if [ -d /etc/rt3/setup.d ] ; then for f in /etc/rt3/setup.d/*.{pl,py,sh} ; do if [ -x $f ] ; then $f fi done fi touch /etc/rt3/setup.finished fi } check_rt_sendmail () { tmp_sendmailmc=$(mktemp) # if the templates is newer than the processed config, then update it if grep -q "Addr=127.0.0.1," /etc/mail/sendmail.mc ; then # copy templates cp -f /etc/mail/sendmail.mc $tmp_sendmailmc # setup initialdata update_config "Addr=127.0.0.1," "" $tmp_sendmailmc # copy to live configuration cp -f $tmp_sendmailmc /etc/mail/sendmail.mc rm -f $tmp_sendmailmc fi # edit /etc/mail/access to add local IP if ! grep "$PLC_RT_IP" /etc/mail/access ; then echo "$PLC_RT_IP RELAY" >> /etc/mail/access makemap hash /etc/mail/access.db < /etc/mail/access fi if [ ! -f /etc/smrsh/rt-mailgate ] ; then ln -s /usr/sbin/rt-mailgate /etc/smrsh/rt-mailgate fi # TODO: fix this to only append the hostnaen once, rather than appending if ! grep "$PLC_RT_HOST" /etc/mail/local-host-names ; then # edit /etc/mail/local-host-names echo "$PLC_RT_HOST" >> /etc/mail/local-host-names IP=$( gethostbyname $PLC_RT_HOST ) REVHOST=$( gethostbyaddr $IP ) if [ -n "$REVHOST" ] ; then echo "$REVHOST" >> /etc/mail/local-host-names fi fi m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf service sendmail restart } if [ "$PLC_RT_ENABLED" != "1" ] ; then exit 0 fi case "$1" in start) MESSAGE=$"Bootstrap RT (please wait...)" dialog "$MESSAGE" check_rt_password check_pg_hba $RT3_DB_NAME $RT3_DB_USER #check_user_and_db $RT3_DB_NAME $RT3_DB_USER check_rt_templates # TODO: make this dependent on whether a change was made! service plc restart httpd check_rt_pghba if [ -n "$WROTE_PG_CONFIG" ] ; then # NOTE: restart db to enable access by users granted above. service plc restart postgresql MESSAGE=$"Bootstrap RT 2 (please wait...)" dialog "$MESSAGE" fi check_rt_aliases check_rt_init check_rt_sendmail check_rt_custom # todo: restart httpd if needed. # NOTE: remove external permission # This is needed to allow it to run without localhost # TODO: find a better way to do this. # remove 'trust' entry from .conf file sed -i -e "s/.*trust//g" $PGDATA/pg_hba.conf.d/${RT3_DB_NAME}.conf if [ -n "$WROTE_PG_CONFIG" ] ; then # NOTE: restart db to enable access by users granted above. service plc restart postgresql MESSAGE=$"Bootstrap RT 3 (please wait...)" dialog "$MESSAGE" fi result "$MESSAGE" ;; delete) MESSAGE=$"Deleting databases..." dialog "$MESSAGE" service plc stop httpd dropdb -U postgres $RT3_DB_NAME dropuser -U postgres $RT3_DB_USER rm -f /etc/rt3/RT_SiteConfig.pm rm -f /etc/rt3/initialdata rm -f /etc/rt3/conf.d/*.pl rm -f $PGDATA/pg_hba.conf.d/${RT3_DB_NAME}.conf sed -i -e "s/.*mailgate.*//g" /etc/aliases rm -f /etc/rt3/setup.finished sed -i -e "s/Port=smtp, Name=MTA/Port=smtp,Addr=127.0.0.1, Name=MTA/g" /etc/mail/sendmail.mc service plc start httpd result "$MESSAGE" ;; stop) MESSAGE=$"Stopping RT" dialog "$MESSAGE" # TODO: is there anything to stop? result "$MESSAGE" ;; esac exit $ERRORS