initial import of PLCRT; package separately from monitor-rt
[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 # NOTE: code duplicated from monitor.functions to allow package to be separate
42 #               from it.
43 function check_pg_hba ()
44 {
45         NAME=$1
46         USER=$2
47         #### SETUP ACCESS to this user and database
48         mkdir -p $PGDATA/pg_hba.conf.d
49         CONF=$PGDATA/pg_hba.conf.d/${NAME}.conf
50         if [ ! -f $CONF ] ; then
51                 echo "host $NAME $USER 127.0.0.1/32 password"   > $CONF
52                 echo "host $NAME $USER $PLC_MONITOR_IP/32 password" >> $CONF
53
54                 WROTE_PG_CONFIG="true"
55         fi
56 }
57
58 # TODO: make values re-configurable...  this may be an issue with RT's db, though.
59 function update_config ()
60 {
61         pattern=$1
62         with=$2
63         file=$3
64         sed -i -e "s/$pattern/$with/g" $file
65 }
66
67 function check_rt_siteconfig ()
68 {
69         tmp_siteconfig=$(mktemp)
70         tmp_initialdata=$(mktemp)
71
72         # TODO: need a better approach for this.
73         for f in $PLCRTPATH/conf.d/*.pl ; do 
74                 update_config PLC_RT_HOSTNAME $PLC_RT_HOST $f
75         done
76         
77         # if the templates are newer than the actual config, then replace them.
78         if [ $PLCRTPATH/RT_SiteConfig.pm -nt /etc/rt3/RT_SiteConfig.pm ] ;
79         then
80                 # copy templates
81                 cp -f $PLCRTPATH/RT_SiteConfig.pm $tmp_siteconfig
82                 cp -f $PLCRTPATH/initialdata $tmp_initialdata
83
84                 # setup RT_SiteConfig.pm
85                 update_config PLC_NAME "$PLC_NAME" $tmp_siteconfig
86                 update_config PLC_RT_HOSTNAME $PLC_RT_HOST $tmp_siteconfig
87                 update_config PLC_WWW_HOSTNAME $PLC_WWW_HOST $tmp_siteconfig
88
89                 update_config RT_DB_NAME $RT3_DB_NAME $tmp_siteconfig
90                 update_config RT_DB_USER $RT3_DB_USER $tmp_siteconfig
91                 update_config RT_DB_PASSWORD $PLC_MONITOR_DBPASSWORD $tmp_siteconfig
92
93                 # setup initialdata
94                 update_config PLC_RT_HOSTNAME $PLC_RT_HOST $tmp_initialdata
95
96                 # copy to live configuration
97                 cp -f $tmp_siteconfig /etc/rt3/RT_SiteConfig.pm 
98                 cp -f $tmp_initialdata /etc/rt3/initialdata 
99                 chmod 644 /etc/rt3/RT_SiteConfig.pm 
100                 chmod 644 /etc/rt3/initialdata 
101
102                 rm -f $tmp_siteconfig
103                 rm -f $tmp_initialdata
104         fi
105 }
106
107 function check_rt_custom ()
108 {
109         rsync -qv -az $PLCRTPATH/local/html /usr/share/rt3
110 }
111
112 function check_rt_pghba ()
113 {
114         NAME=$RT3_DB_NAME
115         USER=$RT3_DB_USER
116         CONF=$PGDATA/pg_hba.conf.d/${NAME}.conf
117         PATTERN="host all postgres 127.0.0.1/32 trust"
118
119         if ! grep -q "$PATTERN" $CONF ; then
120                 #### SETUP ACCESS from postgres user to run init for the first time.
121                 echo "$PATTERN" >> $CONF
122                 WROTE_PG_CONFIG="true"
123         fi
124
125 }
126
127 function check_rt_aliases ()
128 {
129
130         if ! grep -q "rt-mailgate --queue support" /etc/aliases ; 
131         then 
132                 sed -i -e "s/^support.*postmaster//g" /etc/aliases
133                 sed -i -e "s/^security.*root//g" /etc/aliases
134         cat <<EOF >> /etc/aliases
135 # added by RT init scripts for default queues.
136 support: "|/usr/sbin/rt-mailgate --queue support --action correspond --url http://localhost/rt3/"
137 monitor: "|/usr/sbin/rt-mailgate --queue monitor --action correspond --url http://localhost/rt3/"
138 security: "|/usr/sbin/rt-mailgate --queue security --action correspond --url http://localhost/rt3/"
139 legal: "|/usr/sbin/rt-mailgate --queue legal --action correspond --url http://localhost/rt3/"
140 EOF
141                 /usr/bin/newaliases
142         fi
143
144 }
145
146 function check_rt_init ()
147 {
148         if [ ! -f /etc/rt3/setup.finished ] ; then
149                 /usr/sbin/rt-setup-database --action init --dba postgres
150                 for f in $PLCRTPATH/conf.d/*.pl ; do 
151                         /usr/sbin/rt-setup-database --action insert --dba postgres --datafile $f
152                 done
153
154                 # run initial setup scripts (run only once, or for the first time)
155                 if [ -d $PLCRTPATH/setup.d ] ; then 
156                         for f in $PLCRTPATH/setup.d/*.{pl,py,sh} ; do 
157                                 $f
158                         done
159                 fi
160
161                 touch /etc/rt3/setup.finished
162
163         fi
164 }
165
166 check_rt_sendmail ()
167 {
168         tmp_sendmailmc=$(mktemp)
169         
170         # if the templates is newer than the processed config, then update it
171         if grep -q "Addr=127.0.0.1," /etc/mail/sendmail.mc  ; 
172         then
173                 # copy templates
174                 cp -f /etc/mail/sendmail.mc $tmp_sendmailmc
175
176                 # setup initialdata
177                 update_config "Addr=127.0.0.1," "" $tmp_sendmailmc
178
179                 # copy to live configuration
180                 cp -f $tmp_sendmailmc /etc/mail/sendmail.mc
181                 rm -f $tmp_sendmailmc
182
183                 # edit /etc/mail/access to add local IP
184                 if ! grep "$PLC_RT_IP" /etc/mail/access ; then
185                         echo "$PLC_RT_IP                RELAY" >> /etc/mail/access
186                         makemap hash /etc/mail/access.db < /etc/mail/access
187                 fi
188                 if [ !  -f /etc/smrsh/rt-mailgate ] ; then
189                         ln -s /usr/sbin/rt-mailgate /etc/smrsh/rt-mailgate
190                 fi
191                 if ! grep "$PLC_RT_HOST" /etc/mail/local-host-names ; then
192                         # edit /etc/mail/local-host-names
193                         echo "$PLC_RT_HOST" >> /etc/mail/local-host-names
194                 fi
195                 m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
196                 service sendmail restart
197         fi
198
199
200 }
201
202 if [ "$PLC_RT_ENABLED" != "1" ] ; then
203     exit 0
204 fi
205
206 case "$1" in
207         start)
208                 MESSAGE=$"Bootstrap RT (please wait...)"
209                 dialog "$MESSAGE"
210
211                 check_pg_hba $RT3_DB_NAME $RT3_DB_USER
212                 #check_user_and_db $RT3_DB_NAME $RT3_DB_USER
213                 check_rt_siteconfig
214                 check_rt_pghba
215                 if [ -n "$WROTE_PG_CONFIG" ] ; then
216                         # NOTE: restart db to enable access by users granted above.
217                         service plc restart postgresql
218                         MESSAGE=$"Bootstrap RT 2 (please wait...)"
219                         dialog "$MESSAGE"
220                 fi
221                 check_rt_aliases
222                 check_rt_init
223                 check_rt_sendmail
224                 check_rt_custom         # todo: restart httpd if needed.
225
226                 result "$MESSAGE"
227         ;;
228
229
230         delete)
231                 MESSAGE=$"Deleting databases..."
232                 dialog "$MESSAGE"
233
234                 service plc stop httpd
235
236                 dropdb -U postgres $RT3_DB_NAME
237                 dropuser -U postgres $RT3_DB_USER
238                 rm -f /etc/rt3/RT_SiteConfig.pm
239                 rm -f /etc/rt3/initialdata
240                 PATTERN="host all postgres 127.0.0.1/32 trust"
241                 sed -i -e "s|$PATTERN||g" $PGDATA/pg_hba.conf.d/${RT3_DB_NAME}.conf
242
243                 sed -i -e "s/.*mailgate.*//g" /etc/aliases
244                 rm -f /etc/rt3/setup.finished
245
246                 sed -i -e "s/Port=smtp, Name=MTA/Port=smtp,Addr=127.0.0.1, Name=MTA/g" /etc/mail/sendmail.mc
247                 service plc start httpd
248
249                 result "$MESSAGE"
250         ;;
251
252         stop)
253                 MESSAGE=$"Stopping RT"
254                 dialog "$MESSAGE"
255
256                 # TODO: is there anything to stop?
257
258                 result "$MESSAGE"
259         ;;
260 esac
261
262 exit $ERRORS