#!/bin/bash
set -eux

install-packages git build-essential python-dev libssl-dev python-netaddr

install -m 0755 -o root -g root -d /opt/stack/boot-stack

# tools
for f in `ls $(dirname $0)/../bin`; do
  install -m 0755 -o root -g root $(dirname $0)/../bin/$f /usr/local/bin/$f
done

# openstack creds
# TODO: generate random creds.
install -m 0755 -o root -g root $(dirname $0)/../stackrc /root/stackrc
echo "source /root/stackrc" >> /root/.bash_profile

# db
install-packages mysql-server python-mysqldb

my_cnf=
if [ -f /etc/mysql/my.cnf ]; then
  my_cnf=/etc/mysql/my.cnf # Ubuntu
elif [ -f /etc/my.cnf ]; then
  my_cnf=/etc/my.cnf # Fedora/RHEL
fi

sed -i 's/127.0.0.1/0.0.0.0/g' $my_cnf

function deps {
  if hash apt-get &> /dev/null; then
    apt-get update
  fi
  install-packages screen ccze
  install-packages git ipmitool python-dev python-pip python-greenlet libxml2-dev libxslt-dev python-zmq
  install-packages openvswitch-common openvswitch-controller openvswitch-switch open-iscsi
  install-packages python-numpy python-lxml
}

function ip_forwarding {
  cat > /etc/sysctl.conf <<eof
net.ipv4.ip_forward=1
eof
}

function enable_services {
  DISTRO=`lsb_release -si` || true

  if [ $DISTRO = "Fedora" ]; then
      systemctl enable mysqld.service
  fi
}

deps
ip_forwarding
enable_services
