remove rt from monitor package.
[monitor.git] / rt3 / monitor-rt3.init
index abd770e..0e24a11 100644 (file)
@@ -13,6 +13,7 @@
 
 # Source function library and configuration
 . /etc/plc.d/functions
+. /etc/plc.d/monitor.functions
 . /etc/planetlab/plc_config
 local_config=/etc/planetlab/configs/site.xml
 
@@ -35,13 +36,10 @@ RT3_DB_NAME="rt3"
 
 WROTE_PG_CONFIG=
 
-if [ -z "$PLC_MONITOR_IP" ] ; then
-       PLC_MONITOR_IP=$( gethostbyname $PLC_MONITOR_HOST )
+if [ -z "$PLC_RT_IP" ] ; then
+       PLC_RT_IP=$( gethostbyname $PLC_RT_HOST )
 fi
 
-if [ "$PLC_MONITOR_ENABLED" != "1" ] ; then
-    exit 0
-fi
 
 # TODO: make values re-configurable...  this may be an issue with RT's db, though.
 function update_config ()
@@ -56,6 +54,11 @@ function check_rt_siteconfig ()
 {
        tmp_siteconfig=$(mktemp)
        tmp_initialdata=$(mktemp)
+
+       # TODO: need a better approach for this.
+       for f in $MONITORPATH/rt3/rtconf.d/*.pl ; do 
+               update_config PLC_RT_HOSTNAME $PLC_RT_HOST $f
+       done
        
        # if the templates are newer than the actual config, then replace them.
        if [ $MONITORPATH/rt3/RT_SiteConfig.pm -nt /etc/rt3/RT_SiteConfig.pm ] ;
@@ -65,25 +68,33 @@ function check_rt_siteconfig ()
                cp -f $MONITORPATH/rt3/initialdata $tmp_initialdata
 
                # setup RT_SiteConfig.pm
-               update_config PLC_NAME $PLC_NAME $tmp_siteconfig
-               update_config PLC_RT_HOSTNAME $PLC_RT_HOSTNAME $tmp_siteconfig
+               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_MONITOR_DBPASSWORD $tmp_siteconfig
 
                # setup initialdata
-               update_config PLC_RT_HOSTNAME $PLC_RT_HOSTNAME $tmp_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 
+               chmod 644 /etc/rt3/RT_SiteConfig.pm 
+               chmod 644 /etc/rt3/initialdata 
 
                rm -f $tmp_siteconfig
                rm -f $tmp_initialdata
        fi
 }
 
+function check_rt_custom ()
+{
+       rsync -qv -az $MONITORPATH/rt3/local/html /usr/share/rt3
+}
+
 function check_rt_pghba ()
 {
        NAME=$RT3_DB_NAME
@@ -91,9 +102,10 @@ function check_rt_pghba ()
        CONF=$PGDATA/pg_hba.conf.d/${NAME}.conf
        PATTERN="host all postgres 127.0.0.1/32 trust"
 
-       if ! grep -q $PATTERN $CONF ; then
+       if ! grep -q "$PATTERN" $CONF ; then
                #### SETUP ACCESS from postgres user to run init for the first time.
-               echo $PATTERN >> $CONF
+               echo "$PATTERN" >> $CONF
+               WROTE_PG_CONFIG="true"
        fi
 
 }
@@ -103,8 +115,9 @@ 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 <<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/"
@@ -120,31 +133,67 @@ function check_rt_init ()
 {
        if [ ! -f /etc/rt3/setup.finished ] ; then
                /usr/sbin/rt-setup-database --action init --dba postgres
+               for f in $MONITORPATH/rt3/rtconf.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)
+               for f in $MONITORPATH/rt3/rtsetup.d/*.{pl,py,sh} ; do 
+                       $f
+               done
+
                touch /etc/rt3/setup.finished
+
        fi
+}
 
-       ###Last DB adjustments
-       #disable queue General, set disabled to 0 in table queues, set defaultduein to 1 for support queue
-#      /bin/su - -c "/bin/echo \"UPDATE queues SET disabled = 1 WHERE name='General';\"|/usr/bin/psql -U postgres -d rt" postgres
-#      /bin/su - -c "/bin/echo \"UPDATE queues SET defaultduein = 1 WHERE name='support';\"|/usr/bin/psql -U postgres -d rt" postgres
-#
-       #alter acl for Everyone be able to create new tickets in support list
-       #Everyone (installation id 3) on support ( installation is 3)
-#      /bin/su - -c "/bin/echo \"INSERT INTO acl (principaltype,principalid,rightname,objecttype,objectid,delegatedby,delegatedfrom) VALUES ('Group',3,'CreateTicket','RT::Queue',3,0,0);\"|/usr/bin/psql -U postgres -d rt" postgres
-#      /bin/su - -c "/bin/echo \"INSERT INTO acl (principaltype,principalid,rightname,objecttype,objectid,delegatedby,delegatedfrom) VALUES ('Group',3,'ReplyToTicket','RT::Queue',3,0,0);\"|/usr/bin/psql -U postgres -d rt" postgres
-#      #Everyone (installation id 3) on monitor ( installation is 2)
-#      /bin/su - -c "/bin/echo \"INSERT INTO acl (principaltype,principalid,rightname,objecttype,objectid,delegatedby,delegatedfrom) VALUES ('Group',3,'CreateTicket','RT::Queue',2,0,0);\"|/usr/bin/psql -U postgres -d rt" postgres
-#      /bin/su - -c "/bin/echo \"INSERT INTO acl (principaltype,principalid,rightname,objecttype,objectid,delegatedby,delegatedfrom) VALUES ('Group',3,'ReplyToTicket','RT::Queue',2,0,0);\"|/usr/bin/psql -U postgres -d rt" postgres
+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
+
+               # 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
+       fi
+
+
 }
 
+if [ "$PLC_RT_ENABLED" != "1" ] ; then
+    exit 0
+fi
+
 case "$1" in
        start)
                MESSAGE=$"Bootstrap RT (please wait...)"
                dialog "$MESSAGE"
 
                check_pg_hba $RT3_DB_NAME $RT3_DB_USER
-               check_user_and_db $RT3_DB_NAME $RT3_DB_USER
+               #check_user_and_db $RT3_DB_NAME $RT3_DB_USER
                check_rt_siteconfig
                check_rt_pghba
                if [ -n "$WROTE_PG_CONFIG" ] ; then
@@ -155,6 +204,8 @@ case "$1" in
                fi
                check_rt_aliases
                check_rt_init
+               check_rt_sendmail
+               check_rt_custom         # todo: restart httpd if needed.
 
                result "$MESSAGE"
        ;;
@@ -164,20 +215,26 @@ case "$1" in
                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
                PATTERN="host all postgres 127.0.0.1/32 trust"
-               sed -i -e "s/$PATTERN//g" $PGDATA/pg_hba.conf.d/${RT3_DB_USER}.conf
+               sed -i -e "s|$PATTERN||g" $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 Monitor"
+               MESSAGE=$"Stopping RT"
                dialog "$MESSAGE"
 
                # TODO: is there anything to stop?