add plc_rt_dbpassword
[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
78 function check_rt_siteconfig ()
79 {
80         tmp_siteconfig=$(mktemp)
81         tmp_initialdata=$(mktemp)
82
83         # TODO: need a better approach for this.
84         for f in $PLCRTPATH/conf.d/*.pl ; do 
85                 mkdir -p /etc/rt3/conf.d
86                 if [ $f -nt /etc/rt3/conf.d/`basename $f` ] ; then 
87                         tmp_file=$(mktemp)
88                         cp $f $tmp_file
89                         update_config PLC_RT_HOSTNAME $PLC_RT_HOST $tmp_file
90
91                         cp $tmp_file /etc/rt3/conf.d/`basename $f`
92                         rm -f $tmp_file
93                 fi
94         done
95
96         # TODO: need a better approach for this.
97         if [ -f /etc/httpd/conf.d/rt3.conf ] ; then
98                 update_config PLC_RT_HOST $PLC_RT_HOST /etc/httpd/conf.d/rt3.conf
99         fi
100         
101         # if the templates are newer than the actual config, then replace them.
102         if [ $PLCRTPATH/RT_SiteConfig.pm -nt /etc/rt3/RT_SiteConfig.pm ] ;
103         then
104                 # copy templates
105                 cp -f $PLCRTPATH/RT_SiteConfig.pm $tmp_siteconfig
106                 cp -f $PLCRTPATH/initialdata $tmp_initialdata
107
108                 # setup RT_SiteConfig.pm
109                 update_config PLC_NAME "$PLC_NAME" $tmp_siteconfig
110                 update_config PLC_RT_HOSTNAME $PLC_RT_HOST $tmp_siteconfig
111                 update_config PLC_WWW_HOSTNAME $PLC_WWW_HOST $tmp_siteconfig
112
113                 update_config RT_DB_NAME $RT3_DB_NAME $tmp_siteconfig
114                 update_config RT_DB_USER $RT3_DB_USER $tmp_siteconfig
115                 update_config RT_DB_PASSWORD $PLC_RT_DBPASSWORD $tmp_siteconfig
116
117                 # setup initialdata
118                 update_config PLC_RT_HOSTNAME $PLC_RT_HOST $tmp_initialdata
119
120                 # copy to live configuration
121                 cp -f $tmp_siteconfig /etc/rt3/RT_SiteConfig.pm 
122                 cp -f $tmp_initialdata /etc/rt3/initialdata 
123                 chmod 644 /etc/rt3/RT_SiteConfig.pm 
124                 chmod 644 /etc/rt3/initialdata 
125
126                 rm -f $tmp_siteconfig
127                 rm -f $tmp_initialdata
128         fi
129 }
130
131 function check_rt_custom ()
132 {
133         rsync -qv -az $PLCRTPATH/local/html /usr/share/rt3
134 }
135
136 function check_rt_pghba ()
137 {
138         NAME=$RT3_DB_NAME
139         USER=$RT3_DB_USER
140         CONF=$PGDATA/pg_hba.conf.d/${NAME}.conf
141         PATTERN="host all postgres 127.0.0.1/32 trust"
142         if ! grep -q "$PATTERN" $CONF ; then
143                 #### SETUP ACCESS from postgres user to run init for the first time.
144                 echo "$PATTERN" >> $CONF
145                 WROTE_PG_CONFIG="true"
146         fi
147
148         PATTERN="host all postgres $PLC_RT_IP/32 trust"
149         if ! grep -q "$PATTERN" $CONF ; then
150                 #### SETUP ACCESS from postgres user to run init for the first time.
151                 echo "$PATTERN" >> $CONF
152                 WROTE_PG_CONFIG="true"
153         fi
154
155 }
156
157 function check_rt_aliases ()
158 {
159
160         if ! grep -q "rt-mailgate --queue support" /etc/aliases ; 
161         then 
162                 sed -i -e "s/^support.*postmaster//g" /etc/aliases
163                 sed -i -e "s/^security.*root//g" /etc/aliases
164         cat <<EOF >> /etc/aliases
165 # added by RT init scripts for default queues.
166 support: "|/usr/sbin/rt-mailgate --queue support --action correspond --url http://localhost/rt3/"
167 monitor: "|/usr/sbin/rt-mailgate --queue monitor --action correspond --url http://localhost/rt3/"
168 security: "|/usr/sbin/rt-mailgate --queue security --action correspond --url http://localhost/rt3/"
169 legal: "|/usr/sbin/rt-mailgate --queue legal --action correspond --url http://localhost/rt3/"
170 EOF
171                 /usr/bin/newaliases
172         fi
173
174 }
175
176 function check_rt_init ()
177 {
178         if [ ! -f /etc/rt3/setup.finished ] ; then
179                 /usr/sbin/rt-setup-database --action init --dba postgres
180                 for f in /etc/rt3/conf.d/*.pl ; do 
181                         /usr/sbin/rt-setup-database --action insert --dba postgres --datafile $f
182                 done
183
184                 # run initial setup scripts (run only once, or for the first time)
185                 if [ -d $PLCRTPATH/setup.d ] ; then 
186                         for f in $PLCRTPATH/setup.d/*.{pl,py,sh} ; do 
187                                 $f
188                         done
189                 fi
190
191                 touch /etc/rt3/setup.finished
192
193         fi
194 }
195
196 check_rt_sendmail ()
197 {
198         tmp_sendmailmc=$(mktemp)
199         
200         # if the templates is newer than the processed config, then update it
201         if grep -q "Addr=127.0.0.1," /etc/mail/sendmail.mc  ; 
202         then
203                 # copy templates
204                 cp -f /etc/mail/sendmail.mc $tmp_sendmailmc
205
206                 # setup initialdata
207                 update_config "Addr=127.0.0.1," "" $tmp_sendmailmc
208
209                 # copy to live configuration
210                 cp -f $tmp_sendmailmc /etc/mail/sendmail.mc
211                 rm -f $tmp_sendmailmc
212
213                 # edit /etc/mail/access to add local IP
214                 if ! grep "$PLC_RT_IP" /etc/mail/access ; then
215                         echo "$PLC_RT_IP                RELAY" >> /etc/mail/access
216                         makemap hash /etc/mail/access.db < /etc/mail/access
217                 fi
218                 if [ !  -f /etc/smrsh/rt-mailgate ] ; then
219                         ln -s /usr/sbin/rt-mailgate /etc/smrsh/rt-mailgate
220                 fi
221                 if ! grep "$PLC_RT_HOST" /etc/mail/local-host-names ; then
222                         # edit /etc/mail/local-host-names
223                         echo "$PLC_RT_HOST" >> /etc/mail/local-host-names
224                 fi
225                 m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
226                 service sendmail restart
227         fi
228
229
230 }
231
232 if [ "$PLC_RT_ENABLED" != "1" ] ; then
233     exit 0
234 fi
235
236 case "$1" in
237         start)
238                 MESSAGE=$"Bootstrap RT (please wait...)"
239                 dialog "$MESSAGE"
240
241                 check_rt_password
242                 check_pg_hba $RT3_DB_NAME $RT3_DB_USER
243                 #check_user_and_db $RT3_DB_NAME $RT3_DB_USER
244                 check_rt_siteconfig
245                 # TODO: make this dependent on whether a change was made!
246                 service plc restart httpd
247
248                 check_rt_pghba
249                 if [ -n "$WROTE_PG_CONFIG" ] ; then
250                         # NOTE: restart db to enable access by users granted above.
251                         service plc restart postgresql
252                         MESSAGE=$"Bootstrap RT 2 (please wait...)"
253                         dialog "$MESSAGE"
254                 fi
255                 check_rt_aliases
256                 check_rt_init
257                 check_rt_sendmail
258                 check_rt_custom         # todo: restart httpd if needed.
259
260                 # NOTE: remove external permission
261                 # This is needed to allow it to run without localhost
262                 # TODO: find a better way to do this.
263
264                 # remove 'trust' entry from .conf file
265                 sed -i -e "s/.*trust//g" $PGDATA/pg_hba.conf.d/${RT3_DB_NAME}.conf
266                 if [ -n "$WROTE_PG_CONFIG" ] ; then
267                         # NOTE: restart db to enable access by users granted above.
268                         service plc restart postgresql
269                         MESSAGE=$"Bootstrap RT 3 (please wait...)"
270                         dialog "$MESSAGE"
271                 fi
272
273                 result "$MESSAGE"
274         ;;
275
276
277         delete)
278                 MESSAGE=$"Deleting databases..."
279                 dialog "$MESSAGE"
280
281                 service plc stop httpd
282
283                 dropdb -U postgres $RT3_DB_NAME
284                 dropuser -U postgres $RT3_DB_USER
285                 rm -f /etc/rt3/RT_SiteConfig.pm
286                 rm -f /etc/rt3/initialdata
287                 rm -f /etc/rt3/conf.d/*.pl
288                 rm -f $PGDATA/pg_hba.conf.d/${RT3_DB_NAME}.conf
289
290                 sed -i -e "s/.*mailgate.*//g" /etc/aliases
291                 rm -f /etc/rt3/setup.finished
292
293                 sed -i -e "s/Port=smtp, Name=MTA/Port=smtp,Addr=127.0.0.1, Name=MTA/g" /etc/mail/sendmail.mc
294                 service plc start httpd
295
296                 result "$MESSAGE"
297         ;;
298
299         stop)
300                 MESSAGE=$"Stopping RT"
301                 dialog "$MESSAGE"
302
303                 # TODO: is there anything to stop?
304
305                 result "$MESSAGE"
306         ;;
307 esac
308
309 exit $ERRORS