github.com/wasbazi/deis@v1.7.1-0.20150609203025-5765871615de/Vagrantfile (about)

     1  # -*- mode: ruby -*-
     2  # # vi: set ft=ruby :
     3  
     4  require 'fileutils'
     5  
     6  Vagrant.require_version ">= 1.6.5"
     7  
     8  unless Vagrant.has_plugin?("vagrant-triggers")
     9    raise Vagrant::Errors::VagrantError.new, "Please install the vagrant-triggers plugin running 'vagrant plugin install vagrant-triggers'"
    10  end
    11  
    12  CLOUD_CONFIG_PATH = File.join(File.dirname(__FILE__), "contrib", "coreos", "user-data")
    13  CONFIG = File.join(File.dirname(__FILE__), "config.rb")
    14  
    15  # Defaults for config options defined in CONFIG
    16  $num_instances = 1
    17  $instance_name_prefix = "deis"
    18  $update_channel = ENV["COREOS_CHANNEL"] || "stable"
    19  $enable_serial_logging = false
    20  $share_home = false
    21  $vm_gui = false
    22  $vm_memory = 2048
    23  $vm_cpus = 1
    24  $shared_folders = {}
    25  $forwarded_ports = {}
    26  
    27  # Attempt to apply the deprecated environment variable NUM_INSTANCES to
    28  # $num_instances while allowing config.rb to override it
    29  if ENV["NUM_INSTANCES"].to_i > 0 && ENV["NUM_INSTANCES"]
    30    $num_instances = ENV["NUM_INSTANCES"].to_i
    31  elsif ENV["DEIS_NUM_INSTANCES"].to_i > 0 && ENV["DEIS_NUM_INSTANCES"]
    32    $num_instances = ENV["DEIS_NUM_INSTANCES"].to_i
    33  else
    34    $num_instances = 3
    35  end
    36  
    37  if File.exist?(CONFIG)
    38    require CONFIG
    39  end
    40  
    41  # Use old vb_xxx config variables when set
    42  def vm_gui
    43    $vb_gui.nil? ? $vm_gui : $vb_gui
    44  end
    45  
    46  def vm_memory
    47    $vb_memory.nil? ? $vm_memory : $vb_memory
    48  end
    49  
    50  def vm_cpus
    51    $vb_cpus.nil? ? $vm_cpus : $vb_cpus
    52  end
    53  
    54  Vagrant.configure("2") do |config|
    55    # always use Vagrants insecure key
    56    config.ssh.insert_key = false
    57  
    58    config.vm.box = "coreos-%s" % $update_channel
    59    config.vm.box_version = ">= 647.2.0"
    60    config.vm.box_url = "http://%s.release.core-os.net/amd64-usr/current/coreos_production_vagrant.json" % $update_channel
    61  
    62    ["vmware_fusion", "vmware_workstation"].each do |vmware|
    63      config.vm.provider vmware do |v, override|
    64        override.vm.box_url = "http://%s.release.core-os.net/amd64-usr/current/coreos_production_vagrant_vmware_fusion.json" % $update_channel
    65      end
    66    end
    67  
    68    config.vm.provider :virtualbox do |v|
    69      # On VirtualBox, we don't have guest additions or a functional vboxsf
    70      # in CoreOS, so tell Vagrant that so it can be smarter.
    71      v.check_guest_additions = false
    72      v.functional_vboxsf     = false
    73    end
    74  
    75    # plugin conflict
    76    if Vagrant.has_plugin?("vagrant-vbguest") then
    77      config.vbguest.auto_update = false
    78    end
    79  
    80    config.trigger.before :up do
    81      if !File.exists?(CLOUD_CONFIG_PATH) || File.readlines(CLOUD_CONFIG_PATH).grep(/#\s*discovery:/).any?
    82        raise Vagrant::Errors::VagrantError.new, "Run 'make discovery-url' first to create user-data."
    83      end
    84    end
    85  
    86    (1..$num_instances).each do |i|
    87      config.vm.define vm_name = "%s-%02d" % [$instance_name_prefix, i] do |config|
    88        config.vm.hostname = vm_name
    89  
    90        if $enable_serial_logging
    91          logdir = File.join(File.dirname(__FILE__), "log")
    92          FileUtils.mkdir_p(logdir)
    93  
    94          serialFile = File.join(logdir, "%s-serial.txt" % vm_name)
    95          FileUtils.touch(serialFile)
    96  
    97          ["vmware_fusion", "vmware_workstation"].each do |vmware|
    98            config.vm.provider vmware do |v, override|
    99              v.vmx["serial0.present"] = "TRUE"
   100              v.vmx["serial0.fileType"] = "file"
   101              v.vmx["serial0.fileName"] = serialFile
   102              v.vmx["serial0.tryNoRxLoss"] = "FALSE"
   103            end
   104          end
   105  
   106          config.vm.provider :virtualbox do |vb, override|
   107            vb.customize ["modifyvm", :id, "--uart1", "0x3F8", "4"]
   108            vb.customize ["modifyvm", :id, "--uartmode1", serialFile]
   109          end
   110        end
   111  
   112        if $expose_docker_tcp
   113          config.vm.network "forwarded_port", guest: 2375, host: ($expose_docker_tcp + i - 1), auto_correct: true
   114        end
   115  
   116        $forwarded_ports.each do |guest, host|
   117          config.vm.network "forwarded_port", guest: guest, host: host, auto_correct: true
   118        end
   119  
   120        ["vmware_fusion", "vmware_workstation"].each do |vmware|
   121          config.vm.provider vmware do |v|
   122            v.gui = vm_gui
   123            v.vmx['memsize'] = vm_memory
   124            v.vmx['numvcpus'] = vm_cpus
   125          end
   126        end
   127  
   128        config.vm.provider :virtualbox do |vb|
   129          vb.gui = vm_gui
   130          vb.memory = vm_memory
   131          vb.cpus = vm_cpus
   132        end
   133  
   134        ip = "172.17.8.#{i+99}"
   135        config.vm.network :private_network, ip: ip
   136  
   137        # Uncomment below to enable NFS for sharing the host machine into the coreos-vagrant VM.
   138        #config.vm.synced_folder ".", "/home/core/share", id: "core", :nfs => true, :mount_options => ['nolock,vers=3,udp']
   139        $shared_folders.each_with_index do |(host_folder, guest_folder), index|
   140          config.vm.synced_folder host_folder.to_s, guest_folder.to_s, id: "core-share%02d" % index, nfs: true, mount_options: ['nolock,vers=3,udp']
   141        end
   142  
   143        if $share_home
   144          config.vm.synced_folder ENV['HOME'], ENV['HOME'], id: "home", :nfs => true, :mount_options => ['nolock,vers=3,udp']
   145        end
   146  
   147        if File.exist?(CLOUD_CONFIG_PATH)
   148          config.vm.provision :file, :source => "#{CLOUD_CONFIG_PATH}", :destination => "/tmp/vagrantfile-user-data"
   149          # check that the CoreOS user-data file is valid
   150          config.vm.provision :shell do |s|
   151            s.path = File.join(File.dirname(__FILE__), "contrib", "util", "check-user-data.sh")
   152            s.args = ["/tmp/vagrantfile-user-data", $num_instances]
   153          end
   154          config.vm.provision :shell, :inline => "mv /tmp/vagrantfile-user-data /var/lib/coreos-vagrant/", :privileged => true
   155        else
   156          config.vm.provision :shell do |s|
   157            s.inline = "echo \"File not found: #{CLOUD_CONFIG_PATH}\" &&" +
   158              "echo \"Run 'make discovery-url' first to create user-data.\" && exit 1"
   159          end
   160        end
   161  
   162      end
   163    end
   164  end