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

require 'yaml'
require "./vagrant/config_software"

# load configuration from init.yml
@cmd = YAML.load_file('conf/init.yml')
puts @cmd.inspect

Total = @cmd['MachineNumber']
Provider = @cmd['Provider']

def validate_provider(provider)
  current_provider = nil
  if ARGV[0] and ARGV[0] == "up"
    if ARGV[1] and \
      (ARGV[1].split('=')[0] == "--provider" or ARGV[2])
      current_provider = (ARGV[1].split('=')[1] || ARGV[2])
    else
      current_provider = (ENV['VAGRANT_DEFAULT_PROVIDER'] || :virtualbox).to_s
    end

    if ( provider == current_provider )
      return
    end

    if ("virtualbox" == current_provider and provider == "vb")
      return
    end

    raise "\nMISMATCH FOUND\nProvider in init.yml is #{provider}." +
          "\nBut vagrant provider is #{current_provider}."
  end
end

validate_provider(Provider)

# load software configuration
Zookeeper = Zookeeper.new('conf/zookeeper.yml')
Mesos = Mesos.new('conf/mesos.yml')
Alluxio = Alluxio.new('conf/alluxio.yml')
Spark = Spark.new('conf/spark.yml')
Ufs = Ufs.new('conf/ufs.yml', Provider)

require "./vagrant/config_#{Provider}"

# Vagrantfile API/syntax version.
VAGRANTFILE_API_VERSION = "2"
Vagrant.require_version ">= 1.6.5"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  (1..Total).each do |i|
    # multi vm config
    if i <= Alluxio.masters
      if i == 1
        name = "AlluxioMaster"
      else
        name = "AlluxioMaster#{i}"
      end
    else
      name = "AlluxioWorker#{i - Alluxio.masters}"
    end
    config.vm.define "#{name}" do |n|
      n.vm.hostname = "#{name}"

      # Start parallel provision when it is the last machine
      if i == Total
        n.vm.provision :ansible do |ans|
          ans.playbook = "ansible/playbook.yml"
          ans.limit = "all" # to let "- hosts: all" work in playbooks
          mesos_repo, mesos_version = Mesos.repo_version
          alluxio_repo, alluxio_version = Alluxio.repo_version
          spark_repo, spark_version = Spark.repo_version
          is_vb = Provider == "vb"
          alluxio_dist = Ufs.alluxio_dist(alluxio_version)
          puts "Using Alluxio distribution #{alluxio_dist}"
          alluxio_platform = "standalone"
          if Mesos.type != "None"
            alluxio_platform = "mesos"
          end
          ans.extra_vars = {
              zookeeper_type: Zookeeper.type,
              zookeeper_version: Zookeeper.version,

              mesos_type: Mesos.type,
              mesos_repo: mesos_repo,
              mesos_dist: Mesos.dist,
              mesos_version: mesos_version,

              alluxio_type:    Alluxio.type,
              alluxio_repo:    alluxio_repo,
              alluxio_dist:    alluxio_dist,
              alluxio_memory:  Alluxio.memory,
              alluxio_version: alluxio_version,
              alluxio_masters: Alluxio.masters,
              alluxio_platform: alluxio_platform,

              spark_type:    Spark.type,
              spark_repo:    spark_repo,
              spark_dist:    Spark.dist,
              spark_profile: Ufs.hadoop.spark_profile,
              spark_version: spark_version,

              ufs: Ufs.type,

              hadoop_version:     Ufs.hadoop.version,
              hadoop_tarball_url: Ufs.hadoop.tarball_url,

              s3_id:     Ufs.s3.id,
              s3_key:    Ufs.s3.key,
              s3_bucket: Ufs.s3.bucket,

              gcs_id:     Ufs.gcs.id,
              gcs_key:    Ufs.gcs.key,
              gcs_bucket: Ufs.gcs.bucket,
              
              swift_container: Ufs.swift.container,
              swift_user: Ufs.swift.user,
              swift_tenant: Ufs.swift.tenant,
              swift_password: Ufs.swift.password,
              swift_auth_url: Ufs.swift.auth_url,
              swift_use_public_url: Ufs.swift.use_public_url,
              swift_auth_method: Ufs.swift.auth_method,

              provider: Provider,
          }
          # ans.verbose = "vvvv"
          ans.raw_ssh_args = ['-o ControlPersist=30m']
        end
      end

      # Provider specific init
      if Provider == "vb"
        config_vb(n, i, Total, name, Alluxio.type == "Local")
      end

      if Provider == "aws"
        if (defined?(@ec2)).nil?
          @ec2 = YAML.load_file('conf/ec2.yml')
          puts @ec2.inspect
          KEYPAIR=@ec2['Keypair']
          KEY_PATH=@ec2['Key_Path']
          AMI=@ec2['AMI']
          REGION=@ec2['Region']
          SECURITY_GROUP=@ec2['Security_Group']
          INSTANCE_TYPE = @ec2['Instance_Type']
          AVAILABILITY_ZONE  = @ec2['Availability_Zone']
          BLOCK_DEVICE_MAPPING = @ec2['Block_Device_Mapping']
          TAG = @ec2['Tag']
          SUBNET = @ec2['Subnet']
        end
        config_aws(n, i, Total, name)
      end

      if Provider == "google"
        if (defined?(@gce)).nil?
          @gce = YAML.load_file('conf/gce.yml')
          puts @gce.inspect
          SSH_USERNAME = @gce['SSH_Username']
          KEY_PATH = @gce['Key_Path']
          GOOGLE_CLOUD_PROJECT_ID = @gce['Google_Cloud_Project_ID']
          JSON_KEY_LOCATION = @gce['Json_Key_Location']
          IMAGE_FAMILY = @gce['Image_Family']
          IMAGE_PROJECT_ID = @gce['Image_Project_ID']	
          MACHINE_TYPE = @gce['Machine_Type']
          PREEMPTIBLE = @gce['Preemptible']
          INSTANCE_GROUP = @gce['Instance_Group']
          SCOPES = @gce['Scopes']
          DISK_SIZE = @gce['Disk_Size']
          PREFIX = @gce['Prefix']
          NETWORK = @gce['Network']
        end
        config_google(n, i, Total, name)
      end
    end
  end
end
