#!/bin/sh
### BEGIN INIT INFO
# Provides: slapos
# Required-Start: $network $remote_fs
# Required-Stop: $network $remote_fs
# Default-Start: 3 5
# Default-Stop:
# Description: Run SlapOS
### END INIT INFO
##############################################################################
#
# Copyright (c) 2010 Vifib SARL and Contributors. All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly advised to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

case "$1" in
  start)

# clean the system
for service in rpcbind network-remotefs postfix ; do
  chkconfig --del $service
  /etc/init.d/$service stop
done

# set random root password
pwgen -sync 512 1 | passwd --stdin root

SLAP_INSTALL_LOG=/opt/slapos/slapos-install.log

if [ ! -f /opt/slapos/bin/slapformat ] ; then
  while [ ! -f /opt/slapos/bin/slapformat ] ; do
    echo -n "Installing SlapOS, log availble at ${SLAP_INSTALL_LOG}..."
    # software not detected, force forever installation
    mkdir -p /opt/slapos
    (python -S -c \
      'import urllib2;print urllib2.urlopen("http://svn.zope.org/*checkout*/zc.buildout/trunk/bootstrap/bootstrap.py").read()' \
      | python -S - -c /etc/slapos/software.cfg buildout:directory=/opt/slapos \
      >> ${SLAP_INSTALL_LOG} 2>&1 && /opt/slapos/bin/buildout -c \
      /etc/slapos/software.cfg buildout:directory=/opt/slapos >> \
      ${SLAP_INSTALL_LOG} 2>&1 ) && (echo "done.") || (echo \
      "failed, retrying, the last error was:." ; tail -n 20 ${SLAP_INSTALL_LOG} ; rm -f /opt/slapos/bin/slapformat)
  done
fi

# software detected, ready to run
# Set up cron
echo """# BEWARE: This file will be automatically regenerated on each boot
SHELL=/bin/sh
PATH=/usr/bin:/usr/sbin:/sbin:/bin:/usr/lib/news/bin
MAILTO=root
0 0 * * * root /opt/slapos/bin/slapformat --verbose --log_file=/opt/slapos/slapformat.log /etc/slapos/slapos.cfg >> /opt/slapos/slapformat.log 2>&1
*/5 * * * * root /opt/slapos/bin/slapgrid-cp --verbose --logfile=/opt/slapos/slapgrid-cp.log --pidfile=/opt/slapos/slapgrid-cp.pid /etc/slapos/slapos.cfg >> /opt/slapos/slapgrid-cp.log 2>&1
*/5 * * * * root /opt/slapos/bin/slapgrid-sr --verbose --logfile=/opt/slapos/slapgrid-sr.log --pidfile=/opt/slapos/slapgrid-sr.pid /etc/slapos/slapos.cfg >> /opt/slapos/slapgrid-sr.log 2>&1
0 0 * * * root /opt/slapos/bin/slapgrid-ur --verbose --logfile=/opt/slapos/slapgrid-ur.log --pidfile=/opt/slapos/slapgrid-ur.pid /etc/slapos/slapos.cfg >> /opt/slapos/slapgrid-ur.log 2>&1
""" > /etc/cron.d/slapos

# Execute slapformat
echo -n "Running slapformat..."
/opt/slapos/bin/slapformat --verbose --console /etc/slapos/slapos.cfg
echo "done."

# Set kvm up
modprobe kvm_intel
chmod 666 /dev/kvm

# Enable noop scheduler for disk which have SLAPOS labeled partition
disk=`blkid -L SLAPOS | sed -r -e 's/(\/dev\/|[0-9]*$)//g'`
echo noop > /sys/block/$disk/queue/scheduler

  ;;
esac
