noosfero 3.61 KB
#! /bin/sh
### BEGIN INIT INFO
# Provides:          noosfero
# Required-Start:    $remote_fs
# Required-Stop:     $remote_fs
# Should-Start:      postgresql
# Should-Stop:       postgresql
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Noosfero
# Description:       This file should be used to construct scripts to be
#                    placed in /etc/init.d.
### END INIT INFO

# Sample init.d script for noosfero.
# 
# This script was based on the skeleton init.d script present in a Debian
# GNU/Linux system (sid), on Sat Feb 16 11:12:03 BRT 2008. It must be placed in
# /etc/init.d/ (or whatever place your system uses for startup scripts), and you must create a file /etc/default/noosfero defining the variable
# 
# Author: Antonio Terceiro <terceiro@colivre.coop.br>

# Do NOT "set -e"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Noosfero web platform"
NAME=noosfero
SCRIPTNAME=/etc/init.d/$NAME

# default values
NOOSFERO_DIR=/var/lib/noosfero/current
NOOSFERO_USER=noosfero

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
if [ -e /lib/init/vars.sh ]; then
  . /lib/init/vars.sh
fi

if [ -z "$NOOSFERO_DIR" ] || [ -z "$NOOSFERO_USER" ]; then
  echo "NOOSFERO_DIR or NOOSFERO_USER not defined, noosfero not being started."
  echo "Both variables must be defined in /etc/default/noosfero"
  exit 0
fi

if test -x /usr/sbin/noosfero-check-dbconfig ; then
  if ! /usr/sbin/noosfero-check-dbconfig; then
    echo "Noosfero database access not configured, service disabled."
    exit 0
  fi
fi

######################

main_script() {
  if [ "$NOOSFERO_USER" != "$USER" ]; then
    su $NOOSFERO_USER -l -c "cd $NOOSFERO_DIR && ./script/production $1"
  else
    cd $NOOSFERO_DIR
    ./script/production $1
  fi
}

do_setup() {
  # PID directories
  if [ ! -d /var/run/noosfero ]; then
    mkdir /var/run/noosfero
    chown $NOOSFERO_USER:root /var/run/noosfero
    chmod 755 /var/run/noosfero
  fi

  # Noosfero logs
  if [ ! -d /var/log/noosfero ]; then
    mkdir /var/log/noosfero
    chown $NOOSFERO_USER:root /var/log/noosfero
    chmod 750 /var/log/noosfero
  fi

  # Noosfero tmp directory
  if [ ! -d /var/tmp/noosfero ]; then
    mkdir /var/tmp/noosfero
    chown $NOOSFERO_USER:root /var/tmp/noosfero
    chmod 750 /var/tmp/noosfero
  fi

  # Noosfero cache directory
  if [ ! -d /var/cache/noosfero ]; then
    mkdir /var/cache/noosfero
    chown $NOOSFERO_USER:root /var/cache/noosfero
    chmod 755 /var/cache/noosfero
  fi

  # symlink the directories into Noosfero directory
  if [ ! -e $NOOSFERO_DIR/tmp ]; then
    ln -s /var/tmp/noosfero $NOOSFERO_DIR/tmp
  fi
  if [ ! -e $NOOSFERO_DIR/tmp/pids ]; then
    ln -s /var/run/noosfero $NOOSFERO_DIR/tmp/pids
  fi
  if [ ! -e $NOOSFERO_DIR/log ]; then
    ln -s /var/log/noosfero $NOOSFERO_DIR/log
  fi
  if [ ! -e $NOOSFERO_DIR/cache ]; then
    ln -s /var/cache/noosfero $NOOSFERO_DIR/cache
  fi
}

do_start() {
  if ! running; then
    do_setup
    # actually start the service
    main_script start
  else
    echo 'Noosfero is already running, nothing to do...'
  fi
}

do_stop() {
  if running; then
    main_script stop
  else
    echo 'Noosfero is already stopped, nothing to do...'
  fi
}

do_restart() {
  if running; then
    main_script restart
  else
    do_start
  fi
}

running(){
  main_script running
}

case "$1" in
  start|stop|restart|setup)
    do_$1
    ;;
  force-reload)
    do_restart
    ;;
  *)
    echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload|setup}" >&2
    exit 3
    ;;
esac

: