use python2 explicitly for f31
[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         fi
226
227         # edit /etc/mail/access to add local IP
228         if ! grep "$PLC_RT_IP" /etc/mail/access ; then
229                 echo "$PLC_RT_IP                RELAY" >> /etc/mail/access
230                 makemap hash /etc/mail/access.db < /etc/mail/access
231         fi
232
233         if [ !  -f /etc/smrsh/rt-mailgate ] ; then
234                 ln -s /usr/sbin/rt-mailgate /etc/smrsh/rt-mailgate
235         fi
236
237         # TODO: fix this to only append the hostnaen once, rather than appending
238         if ! grep "$PLC_RT_HOST" /etc/mail/local-host-names ; then
239                 # edit /etc/mail/local-host-names
240                 echo "$PLC_RT_HOST" >> /etc/mail/local-host-names
241                 IP=$( gethostbyname $PLC_RT_HOST )
242                 REVHOST=$( gethostbyaddr $IP )
243                 if [ -n "$REVHOST" ] ; then 
244                     echo "$REVHOST" >> /etc/mail/local-host-names
245                 fi
246         fi
247         m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
248         service sendmail restart
249
250 }
251
252 if [ "$PLC_RT_ENABLED" != "1" ] ; then
253     exit 0
254 fi
255
256 case "$1" in
257         start)
258                 MESSAGE=$"Bootstrap RT (please wait...)"
259                 dialog "$MESSAGE"
260
261                 check_rt_password
262                 check_pg_hba $RT3_DB_NAME $RT3_DB_USER
263                 #check_user_and_db $RT3_DB_NAME $RT3_DB_USER
264                 check_rt_templates
265                 # TODO: make this dependent on whether a change was made!
266                 service plc restart httpd
267
268                 check_rt_pghba
269                 if [ -n "$WROTE_PG_CONFIG" ] ; then
270                         # NOTE: restart db to enable access by users granted above.
271                         service plc restart postgresql
272                         MESSAGE=$"Bootstrap RT 2 (please wait...)"
273                         dialog "$MESSAGE"
274                 fi
275                 check_rt_aliases
276                 check_rt_init
277                 check_rt_sendmail
278                 check_rt_custom         # todo: restart httpd if needed.
279
280                 # NOTE: remove external permission
281                 # This is needed to allow it to run without localhost
282                 # TODO: find a better way to do this.
283
284                 # remove 'trust' entry from .conf file
285                 sed -i -e "s/.*trust//g" $PGDATA/pg_hba.conf.d/${RT3_DB_NAME}.conf
286                 if [ -n "$WROTE_PG_CONFIG" ] ; then
287                         # NOTE: restart db to enable access by users granted above.
288                         service plc restart postgresql
289                         MESSAGE=$"Bootstrap RT 3 (please wait...)"
290                         dialog "$MESSAGE"
291                 fi
292
293                 result "$MESSAGE"
294         ;;
295
296
297         delete)
298                 MESSAGE=$"Deleting databases..."
299                 dialog "$MESSAGE"
300
301                 service plc stop httpd
302
303                 dropdb -U postgres $RT3_DB_NAME
304                 dropuser -U postgres $RT3_DB_USER
305                 rm -f /etc/rt3/RT_SiteConfig.pm
306                 rm -f /etc/rt3/initialdata
307                 rm -f /etc/rt3/conf.d/*.pl
308                 rm -f $PGDATA/pg_hba.conf.d/${RT3_DB_NAME}.conf
309
310                 sed -i -e "s/.*mailgate.*//g" /etc/aliases
311                 rm -f /etc/rt3/setup.finished
312
313                 sed -i -e "s/Port=smtp, Name=MTA/Port=smtp,Addr=127.0.0.1, Name=MTA/g" /etc/mail/sendmail.mc
314                 service plc start httpd
315
316                 result "$MESSAGE"
317         ;;
318
319         stop)
320                 MESSAGE=$"Stopping RT"
321                 dialog "$MESSAGE"
322
323                 # TODO: is there anything to stop?
324
325                 result "$MESSAGE"
326         ;;
327 esac
328
329 exit $ERRORS