1
0
mirror of https://git.FreeBSD.org/ports.git synced 2025-01-29 10:18:30 +00:00
freebsd-ports/net-mgmt/nav/scripts/myautoconf
Anders Nordby b2afface3b Update to 3.0 beta 10.
Sponsored by:	University of Tromsø & University of Bergen
2005-02-15 15:00:59 +00:00

155 lines
4.8 KiB
Bash

#! /bin/sh
# auto-configure nav, anders@FreeBSD.org 2004-11-14
#
# Variabels that needs to be set: LOCALBASE
navuser=navcron
navgroup=nav
if [ -z "$LOCALBASE" ]
then
echo "LOCALBASE variable not set. Aborting."
exit 1
fi
if [ -z "$PORTSDIR" ]
then
echo "PORTSDIR variable not set. Aborting."
exit 1
fi
if [ -z "`grep \"^if \[ -f $LOCALBASE/nav/bin/navenv\.sh \]; then \. $LOCALBASE/nav/bin/navenv\.sh; fi\" /etc/profile`" ]
then
echo "Add sourcing of environment in /etc/profile."
echo "if [ -f $LOCALBASE/nav/bin/navenv.sh ]; then . $LOCALBASE/nav/bin/navenv.sh; fi" >>/etc/profile
fi
if [ ! -f $LOCALBASE/etc/cricket-conf.pl ]
then
echo "Default cricket config missing, copying default one to"
echo "$LOCALBASE/etc/cricket-conf.pl."
install -g $navgroup -o $navuser -m 0755 $LOCALBASE/etc/cricket-conf.pl.dist $LOCALBASE/etc/cricket-conf.pl
fi
setdbcvar() {
# $1: variable $2: password $3: config
sed "s/^$1=.*/$1=$2/" $3 >$3.tmp
cp $3.tmp $3
rm -f $3.tmp
}
ifyes() {
read line
if [ "`echo $line | cut -c1-1 | tr 'A-Z' 'a-z'`" != "y" ]
then
return 1
fi
}
apgck() {
if [ ! -x $LOCALBASE/bin/apg ]
then
printf "Password-generation program ports/security/apg is missing. Do you want me to\n"
printf "try to install it for you? (y/n) "
if (ifyes)
then
cd $PORTSDIR/security/apg
(make clean && make install && make clean) >/dev/null 2>&1
fi
if [ ! -x $LOCALBASE/bin/apg ]
then
echo "Please install ports/security/apg. Need it to generate random passwords"
echo "for PostgreSQL. Then re-run this script/make auto-config."
exit 1
fi
fi
}
if (id pgsql >/dev/null 2>&1)
then
printf "Do you want to auto-configure PostgreSQL? (y/n) "
if (ifyes)
then
apgck
pdir=$LOCALBASE/pgsql/data
pconf=$pdir/postgresql.conf
phbaconf=$pdir/pg_hba.conf
pgsqlp=`$LOCALBASE/bin/apg -M NCL -n 1`
navreadp=`$LOCALBASE/bin/apg -M NCL -n 1`
navwritep=`$LOCALBASE/bin/apg -M NCL -n 1`
navdbconf=$LOCALBASE/nav/etc/db.conf
navdbbkconf=$LOCALBASE/nav/etc/pg_backup.conf
if [ ! -d $pdir ]
then
echo "Running PostgreSQL initdb."
su -l pgsql -c "$LOCALBASE/bin/initdb"
fi
if [ -f $pconf ]
then
echo "Setting up TCP/IP socket for PostgreSQL."
sed "s/.*tcpip_socket.*/tcpip_socket = true/" \
$pconf >$pconf.tmp
cp $pconf.tmp $pconf
rm -f $pconf.tmp
fi
printf "To continue, we need to start the database engine. Proceed? (y/n) "
if (ifyes)
then
echo "Starting PostgreSQL database."
su -l pgsql -c "$LOCALBASE/bin/pg_ctl start -s -w"
echo "Waiting 5 seconds."
sleep 5
echo "Creating users & databases, importing data."
usedb=template1
# firstdb=`su -l pgsql -c "$LOCALBASE/bin/psql -l -t -A" | head -1 | cut -d'|' -f1`
PGUSER=pgsql
export PGUSER
$LOCALBASE/bin/createdb --encoding UTF-8 manage
$LOCALBASE/bin/createdb --encoding UTF-8 navprofiles
$LOCALBASE/bin/createdb --encoding UTF-8 logger
$LOCALBASE/bin/createlang plpgsql manage
$LOCALBASE/bin/createlang plpgsql navprofiles
$LOCALBASE/bin/createlang plpgsql logger
$LOCALBASE/bin/createuser -A -D navread
$LOCALBASE/bin/createuser -A -D navwrite
$LOCALBASE/bin/psql -f $LOCALBASE/nav/doc/sql/manage.sql manage
$LOCALBASE/bin/psql -f $LOCALBASE/nav/doc/sql/navprofiles.sql navprofiles
$LOCALBASE/bin/psql -f $LOCALBASE/nav/doc/sql/logger.sql logger
$LOCALBASE/bin/psql -f $LOCALBASE/nav/doc/sql/snmpoid.sql manage
echo "Setting passwords."
echo "alter user pgsql with password '$pgsqlp';" | $LOCALBASE/bin/psql $usedb
echo "alter user navread with password '$navreadp';" | $LOCALBASE/bin/psql $usedb
echo "alter user navwrite with password '$navwritep';" | $LOCALBASE/bin/psql $usedb
echo
echo "Password for database user navread: $navreadp"
echo "Password for database user navwrite: $navwritep"
echo "Password for database user pgsql: $pgsqlp"
echo "Make a mental note of it!"
echo
echo "Setting database passwords in NAV db.conf and pg_backup.conf."
setdbcvar userpw_navread $navreadp $navdbconf
setdbcvar userpw_navwrite $navwritep $navdbconf
setdbcvar username pgsql $navdbbkconf
setdbcvar password $pgsqlp $navdbbkconf
echo "Configure database to only allow authentcated connections."
cat $phbaconf | sed -E "s/(^(local|host).*)trust/\1md5/" >$phbaconf.tmp
cp $phbaconf.tmp $phbaconf
rm -f $phbaconf.tmp
echo "Reloading database."
su -l pgsql -c "$LOCALBASE/bin/pg_ctl reload"
else
echo "Fine, you have an empty database set up. Now create users, set passwords,"
echo "and import default data. Do not forget to configure pg_hba.conf."
fi
else
echo "Fine, see if I care."
fi
else
echo
echo "Consider installing a matching ports/databases/postgresqlXX-serverfor your"
echo "PostgreSQL client installation if you want a local NAV database. This target"
echo "(auto-config) can set up the database for you then, just run make auto-config"
echo "again."
fi