5 # Manage settings for the RT installtion
7 # Stephen Soltesz <soltesz@cs.princeton.edu>
8 # Copyright (C) 2008 The Trustees of Princeton University
13 # Source function library and configuration
14 . /etc/plc.d/functions
15 . /etc/planetlab/plc_config
16 local_config=/etc/planetlab/configs/site.xml
18 PLCRTPATH=/usr/share/plcrt
20 function gethostbyaddr ()
22 python -c 'import socket; import sys; print socket.gethostbyaddr(sys.argv[1])[0]' $1 2>/dev/null
30 PGDATA=/var/lib/pgsql/data
31 postgresql_conf=$PGDATA/postgresql.conf
32 pghba_conf=$PGDATA/pg_hba.conf
34 # Export so that we do not have to specify -p to psql invocations
35 export PGPORT=$PLC_DB_PORT
43 if [ -z "$PLC_RT_IP" ] ; then
44 PLC_RT_IP=$( gethostbyname $PLC_RT_HOST )
47 function check_rt_password ()
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
58 # NOTE: code duplicated from monitor.functions to allow package to be separate
60 function check_pg_hba ()
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
71 WROTE_PG_CONFIG="true"
75 # TODO: make values re-configurable... this may be an issue with RT's db, though.
76 function update_config ()
81 sed -i -e "s/$pattern/$with/g" $file
83 function apply_template ()
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
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
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
102 update_config PLC_RT_HOSTNAME $PLC_RT_HOST $tmp_file
104 cp $tmp_file $DESTFILE
108 function check_rt_templates ()
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`
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`
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
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 ] ;
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
141 function check_rt_custom ()
143 rsync -qv -az $PLCRTPATH/local/html /usr/share/rt3
146 function check_rt_pghba ()
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"
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"
167 function check_rt_aliases ()
170 if ! grep -q "rt-mailgate --queue support" /etc/aliases ;
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/"
186 function check_rt_init ()
188 if [ ! -f /etc/rt3/setup.finished ] ; then
189 /usr/sbin/rt-setup-database --action init --dba postgres
191 for f in /etc/rt3/conf.d/*.pl ; do
192 /usr/sbin/rt-setup-database --action insert --dba postgres --datafile $f
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
204 touch /etc/rt3/setup.finished
211 tmp_sendmailmc=$(mktemp)
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 ;
217 cp -f /etc/mail/sendmail.mc $tmp_sendmailmc
220 update_config "Addr=127.0.0.1," "" $tmp_sendmailmc
222 # copy to live configuration
223 cp -f $tmp_sendmailmc /etc/mail/sendmail.mc
224 rm -f $tmp_sendmailmc
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
233 if [ ! -f /etc/smrsh/rt-mailgate ] ; then
234 ln -s /usr/sbin/rt-mailgate /etc/smrsh/rt-mailgate
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
247 m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
248 service sendmail restart
252 if [ "$PLC_RT_ENABLED" != "1" ] ; then
258 MESSAGE=$"Bootstrap RT (please wait...)"
262 check_pg_hba $RT3_DB_NAME $RT3_DB_USER
263 #check_user_and_db $RT3_DB_NAME $RT3_DB_USER
265 # TODO: make this dependent on whether a change was made!
266 service plc restart httpd
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...)"
278 check_rt_custom # todo: restart httpd if needed.
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.
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...)"
298 MESSAGE=$"Deleting databases..."
301 service plc stop httpd
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
310 sed -i -e "s/.*mailgate.*//g" /etc/aliases
311 rm -f /etc/rt3/setup.finished
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
320 MESSAGE=$"Stopping RT"
323 # TODO: is there anything to stop?