# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure(2) do |config|

  if Vagrant.has_plugin?("vagrant-cachier")
    config.cache.scope = :box
  end

  # Handle local proxy settings
  if Vagrant.has_plugin?("vagrant-proxyconf")
    if ENV["http_proxy"]
      config.proxy.http = ENV["http_proxy"]
    end
    if ENV["https_proxy"]
      config.proxy.https = ENV["https_proxy"]
    end
    if ENV["no_proxy"]
      config.proxy.no_proxy = ENV["no_proxy"] + ',192.168.10.6,10.0.2.15,127.0.0.1'
    end

  end

  config.vm.hostname = "devstack"
  config.vm.box = "bento/ubuntu-16.04"
  config.vm.box_check_update = false
  config.vm.network "private_network",ip:"192.168.10.6"
  config.vm.synced_folder "~/", "/vagrant_home"

  config.vm.provider "virtualbox" do |vb|
    vb.gui = false
    vb.memory = "12800"
    vb.cpus = 4
    # vb.customize ["modifyvm", :id, "--cpuexecutioncap", "50"]
  end

  config.vm.provision "shell", privileged: false, inline: <<-SHELL
    # sudo apt-get update
    # sudo apt-get -y upgrade
    sudo apt-get -y install git

    if [ $http_proxy ]; then
      git config --global url.https://git.openstack.org/.insteadOf https://git.openstack.org/
      sudo git config --global url.https://git.openstack.org/.insteadOf https://git.openstack.org/

      protocol=`echo $http_proxy | awk -F: '{print $1}'`
      host=`echo $http_proxy | awk -F/ '{print $3}' | awk -F: '{print $1}'`
      port=`echo $http_proxy | awk -F/ '{print $3}' | awk -F: '{print $2}'`

      echo "<settings>
              <proxies>
                  <proxy>
                      <id>$host</id>
                      <active>true</active>
                      <protocol>$protocol</protocol>
                      <host>$host</host>
                      <port>$port</port>
                  </proxy>
              </proxies>
             </settings>" > ./maven_proxy_settings.xml

      mkdir -p ~/.m2
      cp ./maven_proxy_settings.xml ~/.m2/settings.xml

      sudo mkdir -p /root/.m2
      sudo cp ./maven_proxy_settings.xml /root/.m2/settings.xml
    fi

    git clone https://git.openstack.org/openstack-dev/devstack -b master --depth 1

    # If using vagrant-cachier, restore cached downloads of 3rd-party dependencies
    if [ -d "/tmp/vagrant-cache" ]; then
        if [ -d "/tmp/vagrant-cache/downloads" ]; then
          echo "Restoring downloads"
          cp /tmp/vagrant-cache/downloads/* devstack/files
        fi
        if [ -f "/tmp/vagrant-cache/pip-cache.tar.gz" ]; then
          echo "Restoring ~/.cache"
          tar xzf /tmp/vagrant-cache/pip-cache.tar.gz -C ~
        fi
        if [ -f "/tmp/vagrant-cache/nvm-cache.tar.gz" ]; then
          echo "Restoring ~/.nvm/.cache"
          mkdir -p ~/.nvm
          tar xzf /tmp/vagrant-cache/nvm-cache.tar.gz -C ~/.nvm
        fi
        if [ -f "/tmp/vagrant-cache/npm-pkgs.tar.gz" ]; then
          echo "Restoring ~/.npm"
          tar xzf /tmp/vagrant-cache/npm-pkgs.tar.gz -C ~
        fi
        if [ -f "/tmp/vagrant-cache/root-pip-cache.tar.gz" ]; then
          echo "Restoring ~root/.cache"
          sudo tar xzf /tmp/vagrant-cache/root-pip-cache.tar.gz -C ~root
        fi
        if [ -f "/tmp/vagrant-cache/root-m2-cache.tar.gz" ]; then
          echo "Restoring ~root/.m2"
          sudo tar xzf /tmp/vagrant-cache/root-m2-cache.tar.gz -C ~root
        fi
    fi

    cd devstack
    echo '[[local|localrc]]

GIT_DEPTH=1

SERVICE_HOST=192.168.10.6
HOST_IP=192.168.10.6
HOST_IP_IFACE=eth1

DATABASE_PASSWORD=secretdatabase
RABBIT_PASSWORD=secretrabbit
ADMIN_PASSWORD=secretadmin
SERVICE_PASSWORD=secretservice

LOGFILE=$DEST/logs/stack.sh.log
LOGDIR=$DEST/logs
LOG_COLOR=False

DEST=/opt/stack

disable_all_services
enable_service rabbit key tempest horizon

# Enable more OpenStack services if neccessary:
# https://github.com/openstack-dev/devstack/blob/master/stackrc#L56-L81
# Nova - services to support libvirt based openstack clouds
# enable_service n-api n-cpu n-cond n-sch n-novnc n-cauth n-api-meta
# Placement and Glance services needed for Nova
# enable_service placement-api placement-client
# enable_service g-api g-reg
# Cinder, Neutron
# enable_service cinder c-api c-vol c-sch c-bak
# enable_service neutron q-svc q-agt q-dhcp q-meta q-l3

# Uncomment one of the following lines to use either MySQL or PostgreSQL
# as RDB (relational database) backend for monasca.
enable_service mysql
#enable_service postgresql

# Enable entire storm for the sake of local development mode
enable_service monasca-storm-nimbus
enable_service monasca-storm-supervisor
enable_service monasca-storm-ui
enable_service monasca-storm-logviewer

# Enable/Disable ORM support for mysql/postgresql
# HINT: If postgresql service is enabled, ORM is enforced
MONASCA_DATABASE_USE_ORM=${MONASCA_DATABASE_USE_ORM:-false}

# The following two variables allow switching between Java and Python for the implementations
# of the Monasca API and the Monasca Persister. If these variables are not set, then the
# default is to install the Python implementations of both the Monasca API and the Monasca Persister.

# Uncomment one of the following two lines to choose Java or Python for the Monasca API.
# MONASCA_API_IMPLEMENTATION_LANG=${MONASCA_API_IMPLEMENTATION_LANG:-java}
MONASCA_API_IMPLEMENTATION_LANG=${MONASCA_API_IMPLEMENTATION_LANG:-python}

# Uncomment one of the following two lines to choose Java or Python for the Monasca Pesister.
# MONASCA_PERSISTER_IMPLEMENTATION_LANG=${MONASCA_PERSISTER_IMPLEMENTATION_LANG:-java}
MONASCA_PERSISTER_IMPLEMENTATION_LANG=${MONASCA_PERSISTER_IMPLEMENTATION_LANG:-python}

# Uncomment one of the following three lines to choose either InfluxDB, Vertica or Cassandra.
# MONASCA_METRICS_DB=${MONASCA_METRICS_DB:-vertica}
# MONASCA_METRICS_DB=${MONASCA_METRICS_DB:-cassandra}
MONASCA_METRICS_DB=${MONASCA_METRICS_DB:-influxdb}

MONASCA_API_USE_MOD_WSGI=${MONASCA_API_USE_MOD_WSGI:-True}

# Uncomment one of the following lines and modify accordingly to enable the Monasca DevStack Plugin
enable_plugin monasca-api https://git.openstack.org/openstack/monasca-api
# enable_plugin monasca-api file:///vagrant_home/Documents/repos/openstack/monasca-api.vertica

' > local.conf
    ./stack.sh
    # Cache downloaded files for future runs
    if [ -d "/tmp/vagrant-cache" ]; then
        mkdir -p /tmp/vagrant-cache/downloads
        cp files/*gz files/*.deb /tmp/vagrant-cache/downloads
        tar czf /tmp/vagrant-cache/pip-cache.tar.gz -C ~ .cache
        tar czf /tmp/vagrant-cache/nvm-cache.tar.gz -C ~/.nvm .cache
        tar czf /tmp/vagrant-cache/npm-pkgs.tar.gz -C ~ .npm
        sudo tar czf /tmp/vagrant-cache/root-pip-cache.tar.gz -C ~root .cache
        sudo tar czf /tmp/vagrant-cache/root-m2-cache.tar.gz -C ~root .m2
    fi
  SHELL

end
