#!/bin/bash
#/////////////////////////////////////////////////////////////////////////
#//
#// (c) University of Southampton IT Innovation Centre, 2017
#//
#// Copyright in this software belongs to University of Southampton
#// IT Innovation Centre of Gamma House, Enterprise Road,
#// Chilworth Science Park, Southampton, SO16 7NS, UK.
#//
#// This software may not be used, sold, licensed, transferred, copied
#// or reproduced in whole or in part in any manner or form or in or
#// on any media by any person other than in accordance with the terms
#// of the Licence Agreement supplied with the software, or otherwise
#// without the prior written consent of the copyright owners.
#//
#// This software is distributed WITHOUT ANY WARRANTY, without even the
#// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
#// PURPOSE, except where stated in the Licence Agreement supplied with
#// the software.
#//
#//      Created By :            Michael Boniface
#//      Created Date :          13/12/2017
#//      Created for Project :   FLAME
#//
#/////////////////////////////////////////////////////////////////////////

# Force fail on command fail (off for now as virtualenvwrapper install fails)
# set -euo pipefail


echo "Configuring CLMC service"

# Get command line parameters
if [ "$#" -ne 3 ]; then
    echo "Error: illegal number of arguments: "$#
    echo "Usage: install.sh INFLUX_URL DATABASE_NAME REPORT_PERIOD"
    exit 1 
fi

INFLUX_URL=$1
DATABASE_NAME=$2
REPORT_PERIOD=$3

# Define tickstack software versions
INFLUX_VERSION=1.5.2
INFLUX_CHECKSUM=42fede7b497bdf30d4eb5138db218d1add986fca4fce4a8bcd9c7d6dabaf572a

KAPACITOR_VERSION=1.4.1
KAPACITOR_CHECKSUM=eea9b215f241906570eafe3857e1d4c5

CHRONOGRAF_VERSION=1.4.4.2
CHRONOGRAF_CHECKSUM=eea6915aa6db8f134fcd3b095e863b773bfb3a16a26e346dd65904a07df97963

apt-get update

# install influx
wget https://dl.influxdata.com/influxdb/releases/influxdb_${INFLUX_VERSION}_amd64.deb 2> /dev/null
sha256sum influxdb_${INFLUX_VERSION}_amd64.deb | grep $INFLUX_CHECKSUM > /dev/null
if [ $? == 1 ]; then
	echo "influx download failed checksum"
	exit 1
fi
dpkg -i influxdb_${INFLUX_VERSION}_amd64.deb

# install kapacitor
wget https://dl.influxdata.com/kapacitor/releases/kapacitor_${KAPACITOR_VERSION}_amd64.deb 2> /dev/null
md5sum kapacitor_${KAPACITOR_VERSION}_amd64.deb | grep $KAPACITOR_CHECKSUM > /dev/null
if [ $? == 1 ]; then
	echo "Kapacitor download failed checksum"
	exit 1
fi
dpkg -i kapacitor_${KAPACITOR_VERSION}_amd64.deb

# install Chronograf
wget https://dl.influxdata.com/chronograf/releases/chronograf_${CHRONOGRAF_VERSION}_amd64.deb 2> /dev/null
sha256sum chronograf_${CHRONOGRAF_VERSION}_amd64.deb | grep $CHRONOGRAF_CHECKSUM > /dev/null
if [ $? == 1 ]; then
	echo "Chronograf download failed checksum"
	exit 1
fi
dpkg -i chronograf_${CHRONOGRAF_VERSION}_amd64.deb

systemctl start influxdb
systemctl start kapacitor
systemctl start chronograf

## CLMC-SERVICE
## ----------------------------------------------------------------------------------

# install virtualenvwrapper to manage python environments - and check
echo "----> Installing Python3 and Pip3"
apt-get install -y python3 python3-pip
update-alternatives --install /usr/bin/python python /usr/bin/python3 10

echo "----> Installing virtualenv and wrapper"
apt-get install -y python3-virtualenv virtualenvwrapper
pip3 install virtualenv
pip3 install virtualenvwrapper

echo "----> Configuring virtualenvwrapper"
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

# check the mkvirtualenv with a return value of 1 if version comes back correctly
mkvirtualenv --version
if [ $? -ne 1 ] ; then
        echo "Failed: installing virtualenvwrapper"
		exit 1
fi

# create CLMC virtual environment - and check
echo "----> Making CLMC Python environment"
mkvirtualenv CLMC
if [ $? -ne 0 ] ; then
        echo "Failed: creating CLMC python environment"
		exit 1
fi

# switch the CLMC environment - and check
echo "----> Switching to use CLMC python environment"
workon CLMC
if [ $? -ne 0 ] ; then
        echo "Failed: switching to CLMC python environment"
		exit 1
fi

# install tox - and check
echo "----> Installing TOX"
pip3 install tox
tox --version
if [ $? -ne 0 ] ; then
        echo "Failed: installing tox"
		exit 1
fi

# navigate to the clmc-webservice - and check
echo "----> Moving to CLMC webservice"
cd /vagrant/src/clmcwebservice
if [ $? -ne 0 ] ; then
        echo "Failed: could not find clmc-webservice"
		exit 1
fi

# install the service
echo "----> Installing CLMC web service"
pip3 install  .
if [ $? -ne 0 ] ; then
        echo "Failed: installing clmc-webservice"
		exit 1
fi

# start the service
echo "----> Starting CLMC web service"
nohup pserve production.ini > /dev/null 2>&1 &
if [ $? -ne 0 ] ; then
        echo "Failed: starting clmc-webservice"
		exit 1
else
	echo "CLMC service started."
fi

while ! nc -z localhost 9080
do
  echo "Waiting for clmc service port 9080 to be ready on localhost..."
  sleep 5
done

JSON="{\"aggregator_report_period\": ${REPORT_PERIOD}, \"aggregator_database_name\": \"${DATABASE_NAME}\", \"aggregator_database_url\": \"${INFLUX_URL}\"}"
curl -H 'Content-Type: application/json' -X PUT -d "${JSON}" http://localhost:9080/aggregator/config

JSON="{\"action\": \"start\"}"
curl -H 'Content-Type: application/json' -X PUT -d "${JSON}" http://localhost:9080/aggregator/control

