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