#!/bin/bash

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
. $SCRIPT_DIR/common.sh "$@"


echo "-- SETTING UP MESOS ------------------------------------------------------------"

export AMESOS_VERSION=1.3.0

# Note : I am not creating any user since unfotunately running the mesos-slave as non-root
# user is not optimal, however possible. Onyl mesos-slave run as root can use systemd 
# slices to control the container managed processes.
# Running as non-root user prevents mesos-slave from relying on systemd.
# mesos-master whould perftecly work as whatever user though.

echo " - Simlinking mesos config to /usr/local/etc/mesos"
sudo ln -s /usr/local/lib/mesos-$AMESOS_VERSION/etc/mesos /usr/local/etc/mesos

echo " - Simlinking mesos binaries to /usr/local/bin"
for i in `ls -1 /usr/local/lib/mesos-$AMESOS_VERSION/bin`; do 
	sudo ln -s /usr/local/lib/mesos-$AMESOS_VERSION/bin/$i /usr/local/bin/$i
done

echo " - Simlinking mesos system binaries to /usr/local/sbin"
for i in `ls -1 /usr/local/lib/mesos-$AMESOS_VERSION/sbin`; do 
	sudo ln -s /usr/local/lib/mesos-$AMESOS_VERSION/sbin/$i /usr/local/sbin/$i
done

if [[ $IS_MASTER == 1 ]]; then
    echo " - Creating mesos master working directory in /var/lib/mesos/master"
    sudo mkdir -p /var/lib/mesos/master
    sudo chmod 755 /var/lib/mesos/master
fi

echo " - Creating mesos slave working directory in /var/lib/mesos/slave"
sudo mkdir -p /var/lib/mesos/slave
sudo chmod 755 /var/lib/mesos/slave

echo " - Creating mesos log directory in /var/log/mesos"
sudo mkdir -p /var/log/mesos
sudo chmod 755 /var/log/mesos

echo " - Copying mesos-init-wrapper script"
sudo cp $SCRIPT_DIR/utils/mesos-init-wrapper.sh /usr/local/sbin/
sudo chmod 754 /usr/local/sbin/mesos-init-wrapper.sh

if [[ $IS_MASTER == 1 ]]; then
    echo " - Copying mesos-master systemd file"
    sudo cp $SCRIPT_DIR/utils/mesos-master.service /lib/systemd/system/
    sudo chmod 644 /lib/systemd/system/mesos-master.service
fi

echo " - Copying mesos-slave systemd file"
sudo cp $SCRIPT_DIR/utils/mesos-slave.service /lib/systemd/system/
sudo chmod 644 /lib/systemd/system/mesos-slave.service

echo " - Creating common mesos environment file"
sudo rm -Rf /usr/local/etc/mesos/mesos-env.sh
sudo bash -c "echo -e \"\n#Working configuration \" >> /usr/local/etc/mesos/mesos-env.sh"
sudo bash -c "echo \"export MESOS_log_dir=/var/log/mesos\" >> /usr/local/etc/mesos/mesos-env.sh"

sudo bash -c "echo -e \"\n#Specify a human readable name for the cluster \" >> /usr/local/etc/mesos/mesos-env.sh"
sudo bash -c "echo \"export MESOS_cluster=mes_cluster\" >> /usr/local/etc/mesos/mesos-env.sh"

get_ip_address
sudo bash -c "echo -e \"\n#Avoid issues with systems that have multiple ethernet interfaces when the Master or Slave\"  >> /usr/local/etc/mesos/mesos-env.sh"
sudo bash -c "echo -e \"#registers with a loopback or otherwise undesirable interface.\"  >> /usr/local/etc/mesos/mesos-env.sh"
sudo bash -c "echo \"export MESOS_ip=$IP_ADDRESS\" >> /usr/local/etc/mesos/mesos-env.sh"

sudo bash -c "echo -e \"\n#By default, the Master will use the system hostname which can result in issues in the \"  >> /usr/local/etc/mesos/mesos-env.sh"
sudo bash -c "echo -e \"#event the system name isn’t resolvable via your DNS server.\"  >> /usr/local/etc/mesos/mesos-env.sh"
sudo bash -c "echo \"export MESOS_hostname=$IP_ADDRESS\" >> /usr/local/etc/mesos/mesos-env.sh"

if [[ $IS_MASTER == 1 ]]; then

    echo " - Creating mesos master environment file"
    sudo rm -Rf /usr/local/etc/mesos/mesos-master-env.sh

    sudo bash -c "echo -e \"\n#Path of the master work directory. \"  >> /usr/local/etc/mesos/mesos-master-env.sh"
    sudo bash -c "echo -e \"#This is where the persistent information of the cluster will be stored\"  >> /usr/local/etc/mesos/mesos-master-env.sh"
    sudo bash -c "echo \"export MESOS_work_dir=/var/lib/mesos/master\" >> /usr/local/etc/mesos/mesos-master-env.sh"

    sudo bash -c "echo -e \"\n#Specify the master Zookeeper URL which the Mesos Master will register with\"  >> /usr/local/etc/mesos/mesos-master-env.sh"
    sudo bash -c "echo \"export MESOS_zk=zk://$IP_ADDRESS:2181/mesos\" >> /usr/local/etc/mesos/mesos-master-env.sh"
    # Change quorum for a greater value if one has more than one master
    sudo bash -c "echo \"export MESOS_quorum=1\" >> /usr/local/etc/mesos/mesos-master-env.sh"

    sudo bash -c "echo -e \"\n# file path containing the JSON-formatted ACLs used for authorization.\" >> /usr/local/etc/mesos/mesos-master-env.sh"
    sudo bash -c "echo \"export MESOS_acls=/usr/local/lib/mesos-$AMESOS_VERSION/etc/mesos/mesos-acls.json\" >> /usr/local/etc/mesos/mesos-master-env.sh"

    echo " - Creating mesos ACLs file to forbid root from submitting tasks."
    sudo rm -Rf /usr/local/lib/mesos-$AMESOS_VERSION/etc/mesos/mesos-acls.json
cat > /tmp/mesos-acls.json <<- "EOF"
{
  "run_tasks": [
    { 
      "principals": { "type": "NONE" },
      "users": { "values": ["root"] }
    }
  ]
}
EOF
    sudo mv /tmp/mesos-acls.json /usr/local/lib/mesos-$AMESOS_VERSION/etc/mesos/mesos-acls.json
    sudo chmod 755 /usr/local/lib/mesos-$AMESOS_VERSION/etc/mesos/mesos-acls.json
    sudo chown root.staff /usr/local/lib/mesos-$AMESOS_VERSION/etc/mesos/mesos-acls.json

fi

echo " - Creating mesos slave environment file"
sudo rm -Rf /usr/local/etc/mesos/mesos-slave-env.sh

sudo bash -c "echo -e \"\n#Path of the slave work directory. \"  >> /usr/local/etc/mesos/mesos-slave-env.sh"
sudo bash -c "echo -e \"#This is where executor sandboxes will be placed, as well as the agent's checkpointed state.\"  >> /usr/local/etc/mesos/mesos-slave-env.sh"
sudo bash -c "echo \"export MESOS_work_dir=/var/lib/mesos/slave\" >> /usr/local/etc/mesos/mesos-slave-env.sh"

sudo bash -c "echo -e \"\n#we need the Slave to discover the Master.\"  >> /usr/local/etc/mesos/mesos-slave-env.sh" 
sudo bash -c "echo -e \"#This is accomplished by updating the master argument to the master Zookeeper URL\"  >> /usr/local/etc/mesos/mesos-slave-env.sh"
sudo bash -c "echo \"export MESOS_master=zk://$MASTER_IP:2181/mesos\" >> /usr/local/etc/mesos/mesos-slave-env.sh"

# FIXME No that seems to mess things up more than anything else
#sudo bash -c "echo -e \"\n# file path containing the JSON-formatted Total consumable resources per agent.\" >> /usr/local/etc/mesos/mesos-slave-env.sh"
#sudo bash -c "echo \"export MESOS_resources=file:///usr/local/lib/mesos-$AMESOS_VERSION/etc/mesos/mesos-resources.json\" >> /usr/local/etc/mesos/mesos-slave-env.sh"
#
#echo " - Creating mesos agent resource file to define agent available resources"
#sudo rm -Rf /usr/local/lib/mesos-$AMESOS_VERSION/etc/mesos/mesos-resources.json.json
## IMPORTANT NOTE : In a production environment, one should not use all CPUs and whole RAM !
#cat > /tmp/mesos-resources.json <<- "EOF"
#[
#  {
#    "name": "cpus",
#    "type": "SCALAR",
#    "scalar": {
#      "value": 2
#    }
#  },
#  {
#    "name": "mem",
#    "type": "SCALAR",
#    "scalar": {
#      "value": 1200
#    }
#  }
#]
#EOF
#sudo mv /tmp/mesos-resources.json /usr/local/lib/mesos-$AMESOS_VERSION/etc/mesos/mesos-resources.json
#sudo chmod 755 /usr/local/lib/mesos-$AMESOS_VERSION/etc/mesos/mesos-resources.json
#sudo chown root.staff /usr/local/lib/mesos-$AMESOS_VERSION/etc/mesos/mesos-resources.json



echo " - Checking mesos slave Systemd file"
if [[ `systemctl status mesos-slave | grep 'could not be found'` != "" ]]; then
    echo "mesos-slave systemd file installation failed"
    exit -12
fi
sudo systemctl status mesos-slave > /tmp/mesos_install_log 2>&1
if [[ $? != 0 && $? != 3 ]]; then
    echo "mesos-slave systemd file doesn't work as expected"
    exit -12
fi  

if [[ $IS_MASTER == 1 ]]; then

    echo " - Checking mesos master Systemd file"
    if [[ `systemctl status mesos-master | grep 'could not be found'` != "" ]]; then
        echo "mesos-master systemd file installation failed"
        exit -12
    fi
    sudo systemctl status mesos-master > /tmp/mesos_install_log 2>&1
    if [[ $? != 0 && $? != 3 ]]; then
        echo "mesos-master systemd file doesn't work as expected"
        exit -12
    fi  
fi

if [[ $IS_MASTER == 1 ]]; then

    echo " - Testing systemd startup - starting mesos-master"
    sudo systemctl start mesos-master > /tmp/mesos_install_log 2>&1
    fail_if_error $? "/tmp/mesos_install_log" -1

    echo " - Testing systemd startup - Checking startup of master"
    sleep 8
    sudo systemctl status mesos-master > /tmp/mesos_install_log 2>&1
    fail_if_error $? "/tmp/mesos_install_log" -2

    echo " - Testing systemd startup - stopping mesos-master"
    sudo systemctl stop mesos-master > /tmp/mesos_install_log 2>&1
    fail_if_error $? "/tmp/mesos_install_log" -3

    echo " - Enabling mesos-master on startup"
    sudo systemctl enable mesos-master > /tmp/mesos_install_log 2>&1
    fail_if_error $? "/tmp/mesos_install_log" -4
fi

# Restarting master before playing with slaves
if [[ $IS_MASTER == 1 ]]; then

    echo " - Testing systemd startup - starting mesos-master"
    sudo systemctl start mesos-master > /tmp/mesos_install_log 2>&1
    fail_if_error $? "/tmp/mesos_install_log" -5
    
    sleep 3    
fi

echo " - Testing systemd startup - starting mesos-slave"
sudo systemctl start mesos-slave > /tmp/mesos_install_log 2>&1
fail_if_error $? "/tmp/mesos_install_log" -6

echo " - Testing systemd startup - Checking startup of slave"
sleep 8
sudo systemctl status mesos-slave > /tmp/mesos_install_log 2>&1
fail_if_error $? "/tmp/mesos_install_log" -7

echo " - Testing systemd startup - stopping mesos-slave"
sudo systemctl stop mesos-slave > /tmp/mesos_install_log 2>&1
fail_if_error $? "/tmp/mesos_install_log" -8

echo " - Enabling mesos-slave on startup"
sudo systemctl enable mesos-slave > /tmp/mesos_install_log 2>&1
fail_if_error $? "/tmp/mesos_install_log" -9





