#!/bin/bash

function timer
{
    name=${1:-}
    seconds=$(date +%s)
    if [ -n "$name" ]
    then
        echo "${name}: $((seconds - start_time))" >> ~/timer-results
    else
        start_time=$seconds
    fi
}

set -ex

timer
# When not running my local cloud we don't want these set
if [ ${LOCAL:-1} -eq 1 ]
then
    export http_proxy=http://roxy:3128
    curl -O http://openstack/CentOS-7-x86_64-GenericCloud-1802.qcow2

    export DIB_LOCAL_IMAGE=CentOS-7-x86_64-GenericCloud-1802.qcow2
    export DIB_DISTRIBUTION_MIRROR=http://mirror.centos.org/centos
    export no_proxy=192.168.24.1,192.168.24.2,192.168.24.3
fi

# Set up the undercloud in preparation for running the deployment
sudo yum install -y git wget
rm -rf git-tripleo-ci
git clone https://git.openstack.org/openstack-infra/tripleo-ci git-tripleo-ci
echo '#!/bin/bash' > tripleo.sh
echo 'git-tripleo-ci/scripts/tripleo.sh $@' >> tripleo.sh
chmod +x tripleo.sh
if [ ! -d overcloud-templates ]
then
    git clone https://github.com/cybertron/openstack-virtual-baremetal
    cp -r openstack-virtual-baremetal/overcloud-templates .
fi

export OVERCLOUD_PINGTEST_OLD_HEATCLIENT=0
export TRIPLEOSH=/home/centos/tripleo.sh

# Do the tripleo deployment
# Repo setup
wget -r --no-parent -nd -e robots=off -l 1 -A 'python2-tripleo-repos-*' https://trunk.rdoproject.org/centos7/current/
sudo yum install -y python2-tripleo-repos-*
sudo tripleo-repos current-tripleo-dev --rdo-mirror http://mirror.regionone.rdo-cloud.rdoproject.org:8080/rdo --mirror http://mirror.regionone.rdo-cloud.rdoproject.org

timer 'system setup'
timer

# Undercloud install
cat << EOF > undercloud.conf
[DEFAULT]
undercloud_hostname=undercloud.localdomain
enable_telemetry = false
enable_legacy_ceilometer_api = false
enable_ui = false
enable_validations = false
enable_tempest = false
local_mtu = 1450
[ctlplane-subnet]
masquerade = true
EOF

sudo yum install -y python-tripleoclient
openstack undercloud install
. stackrc

# Undercloud networking
cat >> /tmp/eth2.cfg <<EOF_CAT
network_config:
    - type: interface
      name: eth2
      use_dhcp: false
      mtu: 1450
      addresses:
        - ip_netmask: 10.0.0.1/24
        - ip_netmask: 2001:db8:fd00:1000::1/64
EOF_CAT
sudo os-net-config -c /tmp/eth2.cfg -v
sudo iptables -A POSTROUTING -s 10.0.0.0/24 ! -d 10.0.0.0/24 -j MASQUERADE -t nat
sudo iptables -I FORWARD -s 10.0.0.0/24 -j ACCEPT
sudo iptables -I FORWARD -d 10.0.0.0/24 -j ACCEPT

timer 'undercloud install'
timer

# Image creation
export DIB_YUM_REPO_CONF="/etc/yum.repos.d/delorean*"
openstack overcloud image build
openstack overcloud image upload --update-existing

timer 'image build'
timer

# Node registration and introspection
openstack overcloud node import --introspect --provide instackenv.json
timer 'node introspection'

sleep 60

timer

# Overcloud deploy
export OVERCLOUD_DEPLOY_ARGS="--libvirt-type qemu -e /usr/share/openstack-tripleo-heat-templates/environments/disable-telemetry.yaml"
if [ ${VERSION:-1} -eq 2 ]
then
    OVERCLOUD_DEPLOY_ARGS="$OVERCLOUD_DEPLOY_ARGS -e /home/centos/overcloud-templates/network-templates-v2/network-isolation-absolute.yaml -e /home/centos/overcloud-templates/network-templates-v2/network-environment.yaml"
fi
openstack overcloud deploy --templates $OVERCLOUD_DEPLOY_ARGS

timer 'overcloud deploy'
timer

# Overcloud validation
if [ ${VERSION:-1} -eq 2 ]
then
    export FLOATING_IP_CIDR=10.0.0.0/24
    export FLOATING_IP_START=10.0.0.50
    export FLOATING_IP_END=10.0.0.70
    export EXTERNAL_NETWORK_GATEWAY=10.0.0.1
fi
$TRIPLEOSH --overcloud-pingtest --skip-pingtest-cleanup

timer 'ping test'

cat ~/timer-results
