github.com/deis/deis@v1.13.5-0.20170519182049-1d9e59fbdbfc/Vagrantfile (about)

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