#!/bin/bash
#
# $Id$
#
# Copyright ITEG IT-Engineers GmbH 2016
#

# When called as 
#   $0 start
# this script starts those LXC containers that were running 
# when this scripts was called with
#   $0 stop
# 
# The list of LXC containers to start can be updated at any time with
#   $0 adjust
#
# The objective is to use the last-known state of the CT rather
# than lxc.auto configuration to decide what LXC containers to start.
# This is very convenient if LXC containers are run be teams of servers
# to ensure that exactly the correct LXC containers are started, especially
# if a major problem (power outage) occurs during maintenance work.
#
# LXC containers are NOT started/stopped using 
#   lxc-start
# resp.
#  lxc-stop
# directly but with
#   lcu-start
# resp.
#   lcu-stop

RUNNINGLISTFILE=/etc/lxcutils/running_on_$(hostname -s).txt

case "$1" in
  start)
    echo "lcu-startstop starting ..."
    I=0
    while [ ! -e /etc/lxcutils/conf/CT.conf.template -a "$I" -lt 60 ] ; do
      ((I += 1))
      echo "Waiting for /etc/lxcutils/conf/CT.conf.template to appear ..."
      sleep 1
    done
    if [ ! -e /etc/lxcutils/conf/CT.conf.template ] ; then
      echo 'WARNING: No /etc/lxcutils/environment after a minute!?'
    fi
    if [ -x /etc/lxcutils/environment ] ; then
      . /etc/lxcutils/environment
    else
      echo 'WARNING: No executable /etc/lxcutils/environment!?'
    fi
    # list present?
    if [ ! -f ${RUNNINGLISTFILE} ] ; then
      echo "No ${RUNNINGLISTFILE}, not starting anything."
      exit 0
    fi
    # start the LXC containers running during stop
    test -f ${RUNNINGLISTFILE} || exit 0
    ALLOK="Y"
    #cat ${RUNNINGLISTFILE} |cut -f1 -d' ' |while read CTNAME
    for CTNAME in $(cat ${RUNNINGLISTFILE} |cut -f1 -d' ')
    do
      echo "Calling lcu-start -l ${CTNAME}"
      /usr/sbin/lcu-start -l ${CTNAME} || ALLOK="N"
      echo "Back from lcu-start -l ${CTNAME}"
    done
    touch /run/lcu-startstop.running
    if [ "$ALLOK" = "Y" ]
    then
      exit 0
    else
      exit 1
    fi
    ;;
  stop)
    echo "lcu-startstop stopping ..."
    # good idea but bad for updates:
    #if [ ! -f /run/lcu-startstop.running ] ; then
    #  echo "Not stopping anything because /run/lcu-startstop.running not present."
    #  exit 0;
    #fi
    # better idea ...
    NRCTSRUNNING=$(lxc-ls --running -1 |wc -l |tr -d ' ')
    echo "There are $NRCTSRUNNING containers running."
    if [ "$NRCTSRUNNING" = "0" ] ; then
      echo "No containers running, not overwriting eventual ${RUNNINGLISTFILE}"
      exit 0
    fi
    rm -f ${RUNNINGLISTFILE}
    touch ${RUNNINGLISTFILE}
    for CTNAME in $(lxc-ls --running)
    do
      echo "${CTNAME}" >>${RUNNINGLISTFILE}
    done
    ALLOK="Y"
    #cat ${RUNNINGLISTFILE} |cut -f1 -d' ' |while read CTNAME
    for CTNAME in $(lxc-ls --running)
    do
      echo "Calling lcu-stop -l ${CTNAME}"
      /usr/sbin/lcu-stop -l ${CTNAME} || ALLOK="N"
      echo "Back from lcu-stop -l ${CTNAME}"
    done
    rm -fv /run/lcu-startstop.running
    if [ "$ALLOK" = "Y" ]
    then
      exit 0
    else
      exit 1
    fi
    ;;
  restart|force-reload)
    echo "lcu-startstop restarting ..."
    $0 stop
    $0 start
    ;;
  adjust)
    echo "lcu-startstop adjusting ..."
    # remember and stop the started LXC containers
    rm -f ${RUNNINGLISTFILE}
    touch ${RUNNINGLISTFILE}
    for CTNAME in $(lxc-ls --running)
    do
      echo "${CTNAME}" >>${RUNNINGLISTFILE}
    done
    echo "Adjusted list of LXC containers to auto-start to these:"
    cat ${RUNNINGLISTFILE} 
    exit 0
    ;;
  status)
    echo "lcu-startstop statussing ..."
    lxc-ls --running
    exit 0
    ;;
  *)
    echo "Usage: $0 {adjust|start|stop|status|restart}"
    exit 1
esac

exit $RETVAL
