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