github.com/rochacon/deis@v1.0.2-0.20150903015341-6839b592a1ff/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 Vagrant's insecure key
    56    config.ssh.insert_key = false
    57    config.vm.box = "coreos-%s" % $update_channel
    58  
    59    # HACK to bypass the limitation of the CoreOS "current" metadata for boxes -
    60    # it only specifies the latest release, so we cannot use it for older releases.
    61    # TODO(carmstrong) we can remove this once we're back to using "specific release or more recent"
    62    if $update_channel == "stable"
    63      config.vm.box_version = "= 647.2.0"
    64      config.vm.box_url = "http://stable.release.core-os.net/amd64-usr/647.2.0/coreos_production_vagrant.json"
    65    else
    66      config.vm.box_version = ">= 681.2.0"
    67      config.vm.box_url = "http://%s.release.core-os.net/amd64-usr/current/coreos_production_vagrant.json" % $update_channel
    68    end
    69  
    70    ["vmware_fusion", "vmware_workstation"].each do |vmware|
    71      config.vm.provider vmware do |v, override|
    72        if $update_channel == "stable"
    73          override.vm.box_url = "http://stable.release.core-os.net/amd64-usr/647.2.0/coreos_production_vagrant_vmware_fusion.json" % $update_channel
    74        else
    75          override.vm.box_url = "http://%s.release.core-os.net/amd64-usr/current/coreos_production_vagrant_vmware_fusion.json" % $update_channel
    76        end
    77      end
    78    end
    79  
    80    config.vm.provider :virtualbox do |v|
    81      # On VirtualBox, we don't have guest additions or a functional vboxsf
    82      # in CoreOS, so tell Vagrant that so it can be smarter.
    83      v.check_guest_additions = false
    84      v.functional_vboxsf     = false
    85    end
    86  
    87    # plugin conflict
    88    if Vagrant.has_plugin?("vagrant-vbguest") then
    89      config.vbguest.auto_update = false
    90    end
    91  
    92    config.trigger.before :up do
    93      if File.exists?(CLOUD_CONFIG_PATH) && !File.readlines(CLOUD_CONFIG_PATH).grep(/\s*discovery #DISCOVERY_URL/).any?
    94        # Vagrant binds the VMs IP in VirtualBox's bridge network to the eth1 interface instead of eth0.
    95        # This necessitates the substitution below, which is not required anywhere except in Vagrant.
    96        user_data = File.read(CLOUD_CONFIG_PATH)
    97        new_userdata = user_data.gsub("/opt/bin/flanneld --ip-masq=true", "/opt/bin/flanneld --iface=eth1 --ip-masq=true")
    98        File.open(CLOUD_CONFIG_PATH, "w") {|file| file.puts new_userdata }
    99      else
   100        raise Vagrant::Errors::VagrantError.new, "Run 'make discovery-url' first to create user-data."
   101      end
   102    end
   103  
   104    (1..$num_instances).each do |i|
   105      config.vm.define vm_name = "%s-%02d" % [$instance_name_prefix, i] do |config|
   106        config.vm.hostname = vm_name
   107  
   108        if $enable_serial_logging
   109          logdir = File.join(File.dirname(__FILE__), "log")
   110          FileUtils.mkdir_p(logdir)
   111  
   112          serialFile = File.join(logdir, "%s-serial.txt" % vm_name)
   113          FileUtils.touch(serialFile)
   114  
   115          ["vmware_fusion", "vmware_workstation"].each do |vmware|
   116            config.vm.provider vmware do |v, override|
   117              v.vmx["serial0.present"] = "TRUE"
   118              v.vmx["serial0.fileType"] = "file"
   119              v.vmx["serial0.fileName"] = serialFile
   120              v.vmx["serial0.tryNoRxLoss"] = "FALSE"
   121            end
   122          end
   123  
   124          config.vm.provider :virtualbox do |vb, override|
   125            vb.customize ["modifyvm", :id, "--uart1", "0x3F8", "4"]
   126            vb.customize ["modifyvm", :id, "--uartmode1", serialFile]
   127          end
   128        end
   129  
   130        if $expose_docker_tcp
   131          config.vm.network "forwarded_port", guest: 2375, host: ($expose_docker_tcp + i - 1), auto_correct: true
   132        end
   133  
   134        $forwarded_ports.each do |guest, host|
   135          config.vm.network "forwarded_port", guest: guest, host: host, auto_correct: true
   136        end
   137  
   138        ["vmware_fusion", "vmware_workstation"].each do |vmware|
   139          config.vm.provider vmware do |v|
   140            v.gui = vm_gui
   141            v.vmx['memsize'] = vm_memory
   142            v.vmx['numvcpus'] = vm_cpus
   143          end
   144        end
   145  
   146        config.vm.provider :virtualbox do |vb|
   147          vb.gui = vm_gui
   148          vb.memory = vm_memory
   149          vb.cpus = vm_cpus
   150        end
   151  
   152        ip = "172.17.8.#{i+99}"
   153        config.vm.network :private_network, ip: ip
   154  
   155        # Use the same nameserver as the host machine in order to avoid the "too many redirects" problem.
   156        config.vm.provider :virtualbox do |vb|
   157          vb.customize ["modifyvm", :id, "--natdnshostresolver1", "off"]
   158          vb.customize ["modifyvm", :id, "--natdnsproxy1", "off"]
   159        end
   160  
   161        # Uncomment below to enable NFS for sharing the host machine into the coreos-vagrant VM.
   162        #config.vm.synced_folder ".", "/home/core/share", id: "core", :nfs => true, :mount_options => ['nolock,vers=3,udp']
   163        $shared_folders.each_with_index do |(host_folder, guest_folder), index|
   164          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']
   165        end
   166  
   167        if $share_home
   168          config.vm.synced_folder ENV['HOME'], ENV['HOME'], id: "home", :nfs => true, :mount_options => ['nolock,vers=3,udp']
   169        end
   170  
   171        if File.exist?(CLOUD_CONFIG_PATH)
   172          config.vm.provision :file, :source => "#{CLOUD_CONFIG_PATH}", :destination => "/tmp/vagrantfile-user-data"
   173          # check that the CoreOS user-data file is valid
   174          config.vm.provision :shell do |s|
   175            s.path = File.join(File.dirname(__FILE__), "contrib", "util", "check-user-data.sh")
   176            s.args = ["/tmp/vagrantfile-user-data", $num_instances]
   177          end
   178          config.vm.provision :shell, :inline => "mv /tmp/vagrantfile-user-data /var/lib/coreos-vagrant/", :privileged => true
   179        else
   180          config.vm.provision :shell do |s|
   181            s.inline = "echo \"File not found: #{CLOUD_CONFIG_PATH}\" &&" +
   182              "echo \"Run 'make discovery-url' first to create user-data.\" && exit 1"
   183          end
   184        end
   185  
   186      end
   187    end
   188  end