improve sendmail check to retry certain operations more than once.
[plcrt.git] / plcrt.init
index 23f118d..e6db40d 100644 (file)
@@ -17,6 +17,12 @@ 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
 
@@ -74,22 +80,45 @@ function update_config ()
        file=$3
        sed -i -e "s/$pattern/$with/g" $file
 }
-
-function check_rt_siteconfig ()
+function apply_template ()
 {
-       tmp_siteconfig=$(mktemp)
-       tmp_initialdata=$(mktemp)
+       TEMPLATE=$1
+       DESTFILE=$2
 
-       # TODO: need a better approach for this.
+       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 
-                       tmp_file=$(mktemp)
-                       cp $f $tmp_file
-                       update_config PLC_RT_HOSTNAME $PLC_RT_HOST $tmp_file
+                       apply_template $f /etc/rt3/conf.d/`basename $f`
+               fi
+       done
 
-                       cp $tmp_file /etc/rt3/conf.d/`basename $f`
-                       rm -f $tmp_file
+       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
 
@@ -102,29 +131,10 @@ function check_rt_siteconfig ()
        if [ $PLCRTPATH/RT_SiteConfig.pm -nt /etc/rt3/RT_SiteConfig.pm ] ;
        then
                # copy templates
-               cp -f $PLCRTPATH/RT_SiteConfig.pm $tmp_siteconfig
-               cp -f $PLCRTPATH/initialdata $tmp_initialdata
-
-               # setup RT_SiteConfig.pm
-               update_config PLC_NAME "$PLC_NAME" $tmp_siteconfig
-               update_config PLC_RT_HOSTNAME $PLC_RT_HOST $tmp_siteconfig
-               update_config PLC_WWW_HOSTNAME $PLC_WWW_HOST $tmp_siteconfig
-
-               update_config RT_DB_NAME $RT3_DB_NAME $tmp_siteconfig
-               update_config RT_DB_USER $RT3_DB_USER $tmp_siteconfig
-               update_config RT_DB_PASSWORD $PLC_RT_DBPASSWORD $tmp_siteconfig
-
-               # setup initialdata
-               update_config PLC_RT_HOSTNAME $PLC_RT_HOST $tmp_initialdata
-
-               # copy to live configuration
-               cp -f $tmp_siteconfig /etc/rt3/RT_SiteConfig.pm 
-               cp -f $tmp_initialdata /etc/rt3/initialdata 
+               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 
-
-               rm -f $tmp_siteconfig
-               rm -f $tmp_initialdata
        fi
 }
 
@@ -163,10 +173,10 @@ function check_rt_aliases ()
                sed -i -e "s/^security.*root//g" /etc/aliases
         cat <<EOF >> /etc/aliases
 # added by RT init scripts for default queues.
-support: "|/usr/sbin/rt-mailgate --queue support --action correspond --url http://localhost/rt3/"
-monitor: "|/usr/sbin/rt-mailgate --queue monitor --action correspond --url http://localhost/rt3/"
-security: "|/usr/sbin/rt-mailgate --queue security --action correspond --url http://localhost/rt3/"
-legal: "|/usr/sbin/rt-mailgate --queue legal --action correspond --url http://localhost/rt3/"
+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
@@ -177,14 +187,17 @@ 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 $PLCRTPATH/setup.d ] ; then 
-                       for f in $PLCRTPATH/setup.d/*.{pl,py,sh} ; do 
-                               $f
+               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
 
@@ -209,23 +222,30 @@ check_rt_sendmail ()
                # 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
-               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
-               fi
-               m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
-               service sendmail restart
+       # 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
 
 }
 
@@ -241,7 +261,7 @@ case "$1" in
                check_rt_password
                check_pg_hba $RT3_DB_NAME $RT3_DB_USER
                #check_user_and_db $RT3_DB_NAME $RT3_DB_USER
-               check_rt_siteconfig
+               check_rt_templates
                # TODO: make this dependent on whether a change was made!
                service plc restart httpd