#!/bin/bash -ex
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

####################################################################################################
# NOTE: When making changes to this file (especially if you are installing new packages), consider
#       instead making changes to the base vagrant box (see build-support/packer/README.md).
####################################################################################################

readonly IP_ADDRESS=192.168.33.7

function prepare_extras() {
  pushd aurora
    # Include build script in default PATH.
    ln -sf /home/vagrant/aurora/examples/vagrant/aurorabuild.sh /usr/local/bin/aurorabuild
  popd
}

function install_cluster_config {
  mkdir -p /etc/aurora
  ln -sf /home/vagrant/aurora/examples/vagrant/clusters.json /etc/aurora/clusters.json
}

function install_ssh_config {
  cat >> /etc/ssh/ssh_config <<EOF

# Allow local ssh w/out strict host checking
Host *
    StrictHostKeyChecking no
    UserKnownHostsFile /dev/null
EOF
}

function install_rsyslog_config {
  cat >> /etc/rsyslog.d/10-aurora.conf <<EOF
# Send scheduler logs to /var/log/aurora/scheduler.log
:syslogtag, contains, "aurora-scheduler" /var/log/aurora/scheduler.log

# Send observer logs to /var/log/thermos/observer.log
:syslogtag, contains, "thermos-observer" /var/log/thermos/observer.log

EOF
  systemctl restart rsyslog
}

function configure_netrc {
  cat > /home/vagrant/.netrc <<EOF
machine $(hostname -f)
login aurora
password secret
EOF
  chown vagrant:vagrant /home/vagrant/.netrc
}

function docker_setup {
  gpasswd -a vagrant docker
  echo 'DOCKER_OPTS="--storage-driver=aufs"' | sudo tee --append /etc/default/docker
  systemctl restart docker
}

function start_services {
  # Executing true on failure to please bash -e in case services are already running
  systemctl start zookeeper    || true
}

function prepare_sources {
  apt-get install
  # Assign mesos command line arguments.
  cp /vagrant/examples/vagrant/mesos_config/etc_mesos-slave/* /etc/mesos-slave
  cp /vagrant/examples/vagrant/mesos_config/etc_mesos-master/* /etc/mesos-master
  systemctl stop mesos-master || true
  systemctl stop mesos-slave || true
  # Remove slave metadata to ensure slave start does not pick up old state.
  rm -rf /var/lib/mesos/meta/slaves/latest
  systemctl start mesos-master
  systemctl start mesos-slave

  sudo cp /vagrant/examples/vagrant/systemd/{aurora-scheduler,thermos}.service /etc/systemd/system
  cat > /usr/local/bin/update-sources <<EOF
#!/bin/bash
rsync -urzvhl /vagrant/ /home/vagrant/aurora \
    --filter=':- /vagrant/.gitignore' \
    --exclude=.git \
    --exclude=/third_party \
    --delete
# Install/update the upstart configurations.
sudo cp /vagrant/examples/vagrant/systemd/{aurora-scheduler,thermos}.service /etc/systemd/system
EOF
  chmod +x /usr/local/bin/update-sources
  update-sources > /dev/null
  chown -R vagrant:vagrant /home/vagrant
}

prepare_sources
prepare_extras
install_cluster_config
install_ssh_config
install_rsyslog_config
start_services
configure_netrc
docker_setup
su vagrant -c "aurorabuild all"
