require 'vagrant.rb'
Vagrant.configure(2) do |config|

  config.cache.scope = :box if Vagrant.has_plugin?("vagrant-cachier")
  config.timezone.value = :host if Vagrant.has_plugin?('vagrant-timezone')
  if Vagrant.has_plugin?('vagrant-proxyconf')
    config.proxy.http = ENV['http_proxy'] if ENV['http_proxy']
    config.proxy.https = ENV['https_proxy'] if ENV['https_proxy']
    if ENV['no_proxy']
        local_no_proxy = ",192.168.10.6,10.0.2.15"
        config.proxy.no_proxy = ENV['no_proxy'] + local_no_proxy
    end
  end

  config.ssh.forward_agent = true

  config.vm.hostname = "devstack"
  config.vm.box = "bento/ubuntu-18.04"
  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
  end

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

    if [ $http_proxy ]; then
      git config --global url.https://opendev.org/.insteadOf https://opendev.org/
      sudo git config --global url.https://opendev.org/.insteadOf https://opendev.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 ~/.m2
      cp ./maven_proxy_settings.xml ~/.m2/settings.xml

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

    git clone https://opendev.org/openstack/devstack --branch master --depth 1
    cd devstack
    echo '[[local|localrc]]

GIT_DEPTH=1
DEST=/opt/stack
USE_VENV=False

SERVICE_HOST=192.168.10.6
HOST_IP=192.168.10.6
DATABASE_HOST=192.168.10.6
MYSQL_HOST=192.168.10.6
HOST_IP_IFACE=eth1

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

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

disable_all_services
enable_service zookeeper rabbit mysql 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

# 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 two lines to choose either InfluxDB or Vertica.
# MONASCA_METRICS_DB=${MONASCA_METRICS_DB:-vertica}
# MONASCA_METRICS_DB=${MONASCA_METRICS_DB:-cassandra}
MONASCA_METRICS_DB=${MONASCA_METRICS_DB:-influxdb}

# Uncomment following line to deploy monasca-log-api with Apache
# MONASCA_LOG_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://opendev.org/openstack/monasca-api.git
enable_plugin monasca-log-api https://opendev.org/openstack/monasca-log-api.git

' > local.conf
    ./stack.sh
  SHELL

end
