#!/bin/bash

function usage(){
    echo -e "usage: $0 [OPTIONS]"
    echo -e "Install OSM"
    echo -e "  OPTIONS"
    echo -e "     -u:  UI/SO (rift) IP address"
    echo -e "     -r:  RO (openmano) IP address"
    echo -e "     -v:  VCA (juju) IP address"
    echo -e "     -h:  show this help"
}

#Check root privileges and non a root user behind
[ "$USER" != "root" ] && echo "Needed root privileges (run with sudo)" >&2 && exit -1

#Install LXD and initialize it
#To be included

#Get default IP address
NET_IF=`route -n |awk '$1~/0.0.0.0/ {print $8}'`
NET_IP=`ip -o -4 a |grep ${NET_IF}|awk '{split($4,a,"/"); print a[1]}'`
UI_IP=$NET_IP
RO_IP=$NET_IP
VCA_IP=$NET_IP

#read input options
while getopts ":u:r:v:h-:" o; do
    case "${o}" in
        u)
            export UI_IP="$OPTARG"
            ;;
        r)
            export RO_IP="$OPTARG"
            ;;
        v)
            export VCA_IP="$OPTARG"
            ;;
        h)
            usage && exit 0
            ;;
        -)
            [ "${OPTARG}" == "help" ] && usage && exit 0
            echo -e "Invalid option: '--$OPTARG'\nTry $0 --help for more information" >&2
            exit 1
            ;;
        \?)
            echo -e "Invalid option: '-$OPTARG'\nTry $0 --help for more information" >&2
            exit 1
            ;;
        :)
            echo -e "Option '-$OPTARG' requires an argument\nTry $0 --help for more information" >&2
            exit 1
            ;;
        *)
            usage >&2
            exit -1
            ;;
    esac
done

#Create containers
echo
echo "*** Creating openmano container ***"
wget -O install-openmano.sh 'https://osm.etsi.org/gitweb/?p=osm/openmano.git;a=blob_plain;f=scripts/install-openmano.sh'
chmod +x install-openmano.sh
lxc launch ubuntu:14.04 openmano
lxc exec openmano -- sh -c 'useradd -m -G sudo osm'
lxc exec openmano -- sh -c 'echo "osm4u\nosm4u" | passwd osm'
lxc file push install-openmano.sh openmano/home/osm/
sleep 5
lxc exec openmano -- su - osm -c 'echo -e "osm4u" | sudo -S ./install-openmano.sh -p osm4u -q'
lxc exec openmano -- su - osm -c 'service-openmano start'
rm install-openmano.sh


echo
echo "*** Creating juju container ***"
lxc launch ubuntu:14.04 jujuserver

#Installing server
lxc exec jujuserver -- sh -c 'useradd -m -G sudo osm'
lxc exec jujuserver -- sh -c 'echo "osm4u\nosm4u" | passwd osm'
sleep 5
lxc exec jujuserver -- sh -c 'apt-get update'
lxc exec jujuserver -- sh -c 'apt-get install -y systemd-services lxc software-properties-common ssh'

#Installing client
lxc exec jujuserver -- sh -c 'apt-get install -y software-properties-common'
lxc exec jujuserver -- sh -c 'add-apt-repository -y ppa:juju/stable'
lxc exec jujuserver -- sh -c 'apt-get update'
lxc exec jujuserver -- sh -c 'apt-get install -y juju juju-local juju-quickstart charm-tools squid-deb-proxy'
lxc exec jujuserver -- sh -c 'echo >> /etc/squid-deb-proxy/mirror-dstdomain.acl << EOF "
# launchpad personal package archives (disabled by default)
ppa.launchpad.net
private-ppa.launchpad.net
"
EOF'
lxc exec jujuserver -- sh -c 'service squid-deb-proxy restart'

lxc exec jujuserver -- su - osm -c 'mkdir .juju'
lxc exec jujuserver -- su - osm -c 'echo >> .juju/environments.yaml << EOF "
default: osm0-manual
environments:
  osm0-manual:
    admin-secret: osm4u
    bootstrap-host: localhost
    bootstrap-user: osm
    default-series: trusty
    type: manual
    enable-os-refresh-update: False
    enable-os-upgrade: False
    apt-http-proxy: http://10.0.3.1:8000
    apt-https-proxy: http://10.0.3.1:8000
"
EOF'

lxc exec jujuserver -- su - osm -c 'ssh-keygen -f /home/osm/.ssh/id_rsa -P ""'
lxc exec jujuserver -- su - osm -c 'ssh-keyscan -t rsa localhost >> .ssh/known_hosts'
lxc exec jujuserver -- su - osm -c 'cp .ssh/id_rsa.pub .ssh/authorized_keys'

#Creating the environment and deploying juju-gui
lxc exec jujuserver -- su - osm -c 'echo "osm4u" | juju bootstrap -v'
lxc exec jujuserver -- su - osm -c 'juju deploy juju-gui --to 0'

echo
echo "*** Creating rift container ***"
lxc launch fedora20 rift -c security.privileged=true
wget -O riftware_install.sh 'https://osm.etsi.org/gitweb/?p=osm/riftware.git;a=blob_plain;f=scripts/util/riftware_install.sh;hb=HEAD'
chmod +x riftware_install.sh
lxc file push riftware_install.sh rift/root/
sleep 5
lxc exec rift -- bash -x riftware_install.sh
rm riftware_install.sh


echo
echo "*** Configuring iptables rules ***"
#Get IP addresses
juju_ip=`lxc list jujuserver -c 4|grep eth0 |awk '{print $2}'`
rift_ip=`lxc list rift -c 4|grep eth0 |awk '{print $2}'`
openmano_ip=`lxc list openmano -c 4|grep eth0 |awk '{print $2}'`

#Port forwarding config
#Cleaning rules
sudo iptables -t nat -F PREROUTING
#Openmano ports
sudo iptables -t nat -A PREROUTING -d $RO_IP -p tcp --dport 9090 -j DNAT --to-destination $openmano_ip
#Riftware ports
sudo iptables -t nat -A PREROUTING -d $UI_IP -p tcp --dport 8000 -j DNAT --to-destination $rift_ip
sudo iptables -t nat -A PREROUTING -d $UI_IP -p tcp --dport 3000 -j DNAT --to-destination $rift_ip
sudo iptables -t nat -A PREROUTING -d $UI_IP -p tcp --dport 4567 -j DNAT --to-destination $rift_ip
sudo iptables -t nat -A PREROUTING -d $UI_IP -p tcp --dport 3500:3519 -j DNAT --to-destination $rift_ip
sudo iptables -t nat -A PREROUTING -d $UI_IP -p tcp --dport 8443 -j DNAT --to-destination $rift_ip
#Juju ports
sudo iptables -t nat -A PREROUTING -d $VCA_IP -p tcp --dport 443 -j DNAT --to-destination $juju_ip

echo
echo
echo "OSM installed"
echo '  Run "lxc exec rift bash" to access to rift container'
echo '  Run "lxc exec jujuserver bash" to access to jujuserver container'
echo '  Run "lxc exec openmano bash" to access to openmano container'
echo
echo "Don't forget to add datacenters in openmano, and configure Rift with the Cloud account and Config account"
echo


