try to add the reverse-lookup hostname as well to make the rtdemo work
[plcrt.git] / plcrt.init
1 #!/bin/bash
2 #
3 # priority: 850
4 #
5 # Manage settings for the RT installtion 
6 #
7 # Stephen Soltesz <soltesz@cs.princeton.edu>
8 # Copyright (C) 2008 The Trustees of Princeton University
9 #
10 # $Id$
11 #
12
13 # Source function library and configuration
14 . /etc/plc.d/functions
15 . /etc/planetlab/plc_config
16 local_config=/etc/planetlab/configs/site.xml
17
18 PLCRTPATH=/usr/share/plcrt
19
20 function gethostbyaddr ()
21 {
22     python -c 'import socket; import sys; print socket.gethostbyaddr(sys.argv[1])[0]' $1 2>/dev/null
23 }
24
25
26 # Be verbose
27 set -x
28
29 # Default locations
30 PGDATA=/var/lib/pgsql/data
31 postgresql_conf=$PGDATA/postgresql.conf
32 pghba_conf=$PGDATA/pg_hba.conf
33
34 # Export so that we do not have to specify -p to psql invocations
35 export PGPORT=$PLC_DB_PORT
36
37
38 RT3_DB_USER="rt3user"
39 RT3_DB_NAME="rt3"
40
41 WROTE_PG_CONFIG=
42
43 if [ -z "$PLC_RT_IP" ] ; then
44         PLC_RT_IP=$( gethostbyname $PLC_RT_HOST )
45 fi
46
47 function check_rt_password ()
48 {
49     if [[ -z "$PLC_RT_DBPASSWORD" || "$PLC_RT_DBPASSWORD" = "None" ]] ; then
50         # NOTE: this provides a simpler string than uuidgen
51         PLC_RT_DBPASSWORD=$( uuidgen | md5sum - | awk '{print $1}' )
52         plc-config --category=plc_rt --variable=dbpassword --value="$PLC_RT_DBPASSWORD" --save=$local_config $local_config
53         service plc reload
54                 CREATED="true"
55     fi
56 }
57
58 # NOTE: code duplicated from monitor.functions to allow package to be separate
59 #               from it.
60 function check_pg_hba ()
61 {
62         NAME=$1
63         USER=$2
64         #### SETUP ACCESS to this user and database
65         mkdir -p $PGDATA/pg_hba.conf.d
66         CONF=$PGDATA/pg_hba.conf.d/${NAME}.conf
67         if [ ! -f $CONF ] ; then
68                 echo "host $NAME $USER 127.0.0.1/32 password"   > $CONF
69                 echo "host $NAME $USER $PLC_RT_IP/32 password" >> $CONF
70
71                 WROTE_PG_CONFIG="true"
72         fi
73 }
74
75 # TODO: make values re-configurable...  this may be an issue with RT's db, though.
76 function update_config ()
77 {
78         pattern=$1
79         with=$2
80         file=$3
81         sed -i -e "s/$pattern/$with/g" $file
82 }
83 function apply_template ()
84 {
85         TEMPLATE=$1
86         DESTFILE=$2
87
88         tmp_file=$(mktemp)
89         cp $TEMPLATE $tmp_file
90         update_config PLC_RT_HOSTNAME $PLC_RT_HOST $tmp_file
91         update_config PLC_RT_CC_ADDRESS $PLC_RT_CC_ADDRESS $tmp_file
92
93         update_config PLC_NAME "$PLC_NAME" $tmp_file
94         update_config PLC_RT_HOSTNAME $PLC_RT_HOST $tmp_file
95         update_config PLC_WWW_HOSTNAME $PLC_WWW_HOST $tmp_file
96
97         update_config RT_DB_NAME $RT3_DB_NAME $tmp_file
98         update_config RT_DB_USER $RT3_DB_USER $tmp_file
99         update_config RT_DB_PASSWORD $PLC_RT_DBPASSWORD $tmp_file
100
101         # setup initialdata
102         update_config PLC_RT_HOSTNAME $PLC_RT_HOST $tmp_file
103
104         cp $tmp_file $DESTFILE
105         rm -f $tmp_file
106 }
107
108 function check_rt_templates ()
109 {
110         for f in $PLCRTPATH/conf.d/*.pl ; do 
111                 mkdir -p /etc/rt3/conf.d
112                 if [ $f -nt /etc/rt3/conf.d/`basename $f` ] ; then 
113                         apply_template $f /etc/rt3/conf.d/`basename $f`
114                 fi
115         done
116
117         for f in $PLCRTPATH/setup.d/*.{pl,py,sh} ; do 
118                 mkdir -p /etc/rt3/setup.d
119                 if [ $f -nt /etc/rt3/setup.d/`basename $f` ] ; then 
120                         apply_template $f /etc/rt3/setup.d/`basename $f`
121                         chmod 751 /etc/rt3/setup.d/`basename $f`
122                 fi
123         done
124
125         # TODO: need a better approach for this.
126         if [ -f /etc/httpd/conf.d/rt3.conf ] ; then
127                 update_config PLC_RT_HOST $PLC_RT_HOST /etc/httpd/conf.d/rt3.conf
128         fi
129         
130         # if the templates are newer than the actual config, then replace them.
131         if [ $PLCRTPATH/RT_SiteConfig.pm -nt /etc/rt3/RT_SiteConfig.pm ] ;
132         then
133                 # copy templates
134                 apply_template $PLCRTPATH/RT_SiteConfig.pm /etc/rt3/RT_SiteConfig.pm
135                 apply_template $PLCRTPATH/initialdata /etc/rt3/initialdata
136                 chmod 644 /etc/rt3/RT_SiteConfig.pm 
137                 chmod 644 /etc/rt3/initialdata 
138         fi
139 }
140
141 function check_rt_custom ()
142 {
143         rsync -qv -az $PLCRTPATH/local/html /usr/share/rt3
144 }
145
146 function check_rt_pghba ()
147 {
148         NAME=$RT3_DB_NAME
149         USER=$RT3_DB_USER
150         CONF=$PGDATA/pg_hba.conf.d/${NAME}.conf
151         PATTERN="host all postgres 127.0.0.1/32 trust"
152         if ! grep -q "$PATTERN" $CONF ; then
153                 #### SETUP ACCESS from postgres user to run init for the first time.
154                 echo "$PATTERN" >> $CONF
155                 WROTE_PG_CONFIG="true"
156         fi
157
158         PATTERN="host all postgres $PLC_RT_IP/32 trust"
159         if ! grep -q "$PATTERN" $CONF ; then
160                 #### SETUP ACCESS from postgres user to run init for the first time.
161                 echo "$PATTERN" >> $CONF
162                 WROTE_PG_CONFIG="true"
163         fi
164
165 }
166
167 function check_rt_aliases ()
168 {
169
170         if ! grep -q "rt-mailgate --queue support" /etc/aliases ; 
171         then 
172                 sed -i -e "s/^support.*postmaster//g" /etc/aliases
173                 sed -i -e "s/^security.*root//g" /etc/aliases
174         cat <<EOF >> /etc/aliases
175 # added by RT init scripts for default queues.
176 support: "|/usr/sbin/rt-mailgate --queue support --action correspond --url http://$PLC_RT_HOST/rt3/"
177 monitor: "|/usr/sbin/rt-mailgate --queue monitor --action correspond --url http://$PLC_RT_HOST/rt3/"
178 security: "|/usr/sbin/rt-mailgate --queue security --action correspond --url http://$PLC_RT_HOST/rt3/"
179 legal: "|/usr/sbin/rt-mailgate --queue legal --action correspond --url http://$PLC_RT_HOST/rt3/"
180 EOF
181                 /usr/bin/newaliases
182         fi
183
184 }
185
186 function check_rt_init ()
187 {
188         if [ ! -f /etc/rt3/setup.finished ] ; then
189                 /usr/sbin/rt-setup-database --action init --dba postgres
190
191                 for f in /etc/rt3/conf.d/*.pl ; do 
192                         /usr/sbin/rt-setup-database --action insert --dba postgres --datafile $f
193                 done
194
195                 # run initial setup scripts (run only once, or for the first time)
196                 if [ -d /etc/rt3/setup.d ] ; then 
197                         for f in /etc/rt3/setup.d/*.{pl,py,sh} ; do 
198                                 if [ -x $f ] ; then 
199                                         $f
200                                 fi
201                         done
202                 fi
203
204                 touch /etc/rt3/setup.finished
205
206         fi
207 }
208
209 check_rt_sendmail ()
210 {
211         tmp_sendmailmc=$(mktemp)
212         
213         # if the templates is newer than the processed config, then update it
214         if grep -q "Addr=127.0.0.1," /etc/mail/sendmail.mc  ; 
215         then
216                 # copy templates
217                 cp -f /etc/mail/sendmail.mc $tmp_sendmailmc
218
219                 # setup initialdata
220                 update_config "Addr=127.0.0.1," "" $tmp_sendmailmc
221
222                 # copy to live configuration
223                 cp -f $tmp_sendmailmc /etc/mail/sendmail.mc
224                 rm -f $tmp_sendmailmc
225
226                 # edit /etc/mail/access to add local IP
227                 if ! grep "$PLC_RT_IP" /etc/mail/access ; then
228                         echo "$PLC_RT_IP                RELAY" >> /etc/mail/access
229                         makemap hash /etc/mail/access.db < /etc/mail/access
230                 fi
231                 if [ !  -f /etc/smrsh/rt-mailgate ] ; then
232                         ln -s /usr/sbin/rt-mailgate /etc/smrsh/rt-mailgate
233                 fi
234                 if ! grep "$PLC_RT_HOST" /etc/mail/local-host-names ; then
235                         # edit /etc/mail/local-host-names
236                         echo "$PLC_RT_HOST" >> /etc/mail/local-host-names
237                         IP=$( gethostbyname $PLC_RT_HOST )
238                         REVHOST=$( gethostbyaddr $IP )
239                         if [ -n "$REVHOST" ] ; then 
240                             echo "$REVHOST" >> /etc/mail/local-host-names
241                         fi
242                 fi
243                 m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
244                 service sendmail restart
245         fi
246
247
248 }
249
250 if [ "$PLC_RT_ENABLED" != "1" ] ; then
251     exit 0
252 fi
253
254 case "$1" in
255         start)
256                 MESSAGE=$"Bootstrap RT (please wait...)"
257                 dialog "$MESSAGE"
258
259                 check_rt_password
260                 check_pg_hba $RT3_DB_NAME $RT3_DB_USER
261                 #check_user_and_db $RT3_DB_NAME $RT3_DB_USER
262                 check_rt_templates
263                 # TODO: make this dependent on whether a change was made!
264                 service plc restart httpd
265
266                 check_rt_pghba
267                 if [ -n "$WROTE_PG_CONFIG" ] ; then
268                         # NOTE: restart db to enable access by users granted above.
269                         service plc restart postgresql
270                         MESSAGE=$"Bootstrap RT 2 (please wait...)"
271                         dialog "$MESSAGE"
272                 fi
273                 check_rt_aliases
274                 check_rt_init
275                 check_rt_sendmail
276                 check_rt_custom         # todo: restart httpd if needed.
277
278                 # NOTE: remove external permission
279                 # This is needed to allow it to run without localhost
280                 # TODO: find a better way to do this.
281
282                 # remove 'trust' entry from .conf file
283                 sed -i -e "s/.*trust//g" $PGDATA/pg_hba.conf.d/${RT3_DB_NAME}.conf
284                 if [ -n "$WROTE_PG_CONFIG" ] ; then
285                         # NOTE: restart db to enable access by users granted above.
286                         service plc restart postgresql
287                         MESSAGE=$"Bootstrap RT 3 (please wait...)"
288                         dialog "$MESSAGE"
289                 fi
290
291                 result "$MESSAGE"
292         ;;
293
294
295         delete)
296                 MESSAGE=$"Deleting databases..."
297                 dialog "$MESSAGE"
298
299                 service plc stop httpd
300
301                 dropdb -U postgres $RT3_DB_NAME
302                 dropuser -U postgres $RT3_DB_USER
303                 rm -f /etc/rt3/RT_SiteConfig.pm
304                 rm -f /etc/rt3/initialdata
305                 rm -f /etc/rt3/conf.d/*.pl
306                 rm -f $PGDATA/pg_hba.conf.d/${RT3_DB_NAME}.conf
307
308                 sed -i -e "s/.*mailgate.*//g" /etc/aliases
309                 rm -f /etc/rt3/setup.finished
310
311                 sed -i -e "s/Port=smtp, Name=MTA/Port=smtp,Addr=127.0.0.1, Name=MTA/g" /etc/mail/sendmail.mc
312                 service plc start httpd
313
314                 result "$MESSAGE"
315         ;;
316
317         stop)
318                 MESSAGE=$"Stopping RT"
319                 dialog "$MESSAGE"
320
321                 # TODO: is there anything to stop?
322
323                 result "$MESSAGE"
324         ;;
325 esac
326
327 exit $ERRORS