From 5030a16f8494f2c17c2df690270ccde6ea41fb9a Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 5 Feb 2014 15:35:37 -0800 Subject: [PATCH] kernel/v2: default all configs properly, remove config/default.rb --- config/default.rb | 34 ---------- lib/vagrant/environment.rb | 9 ++- plugins/commands/box/command/repackage.rb | 3 +- plugins/kernel_v2/config/package.rb | 8 +++ plugins/kernel_v2/config/ssh.rb | 7 +- plugins/kernel_v2/config/vagrant.rb | 2 +- plugins/kernel_v2/config/vm.rb | 29 +++++++- plugins/synced_folders/nfs/config.rb | 4 +- .../plugins/kernel_v2/config/package_test.rb | 14 ++++ .../unit/plugins/kernel_v2/config/ssh_test.rb | 14 ++++ .../plugins/kernel_v2/config/vagrant_test.rb | 20 ++++++ test/unit/plugins/kernel_v2/config/vm_test.rb | 66 +++++++++++++++++++ .../plugins/synced_folders/nfs/config_test.rb | 21 ++++++ 13 files changed, 182 insertions(+), 49 deletions(-) delete mode 100644 config/default.rb create mode 100644 test/unit/plugins/kernel_v2/config/package_test.rb create mode 100644 test/unit/plugins/kernel_v2/config/ssh_test.rb create mode 100644 test/unit/plugins/kernel_v2/config/vagrant_test.rb create mode 100644 test/unit/plugins/synced_folders/nfs/config_test.rb diff --git a/config/default.rb b/config/default.rb deleted file mode 100644 index 06d07d30e..000000000 --- a/config/default.rb +++ /dev/null @@ -1,34 +0,0 @@ -Vagrant.configure("2") do |config| - config.vagrant.host = :detect - - config.ssh.forward_agent = false - config.ssh.forward_x11 = false - config.ssh.guest_port = 22 - config.ssh.keep_alive = true - config.ssh.shell = "bash -l" - - config.ssh.default.username = "vagrant" - - config.vm.usable_port_range = (2200..2250) - config.vm.box_url = nil - config.vm.base_mac = nil - config.vm.boot_timeout = 300 - config.vm.graceful_halt_timeout = 60 - - # Share SSH locally by default - config.vm.network :forwarded_port, - guest: 22, - host: 2222, - host_ip: "127.0.0.1", - id: "ssh", - auto_correct: true - - # Share the root folder. This can then be overridden by - # other Vagrantfiles, if they wish. - config.vm.synced_folder ".", "/vagrant" - - config.nfs.map_uid = :auto - config.nfs.map_gid = :auto - - config.package.name = 'package.box' -end diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index fcba1e292..29e805659 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -252,14 +252,13 @@ module Vagrant # managing. Then, the actual individual configuration is loaded for # each {#machine} call. @config_loader = Config::Loader.new(Config::VERSIONS, Config::VERSIONS_ORDER) - @config_loader.set(:default, File.expand_path("config/default.rb", Vagrant.source_root)) @config_loader.set(:home, home_vagrantfile) if home_vagrantfile @config_loader.set(:root, root_vagrantfile) if root_vagrantfile # Make the initial call to get the "global" config. This is mostly # only useful to get the list of machines that we are managing. # Because of this, we ignore any warnings or errors. - @config_global, _ = @config_loader.load([:default, :home, :root]) + @config_global, _ = @config_loader.load([:home, :root]) # Return the config @config_global @@ -329,7 +328,7 @@ module Vagrant vm_config_key = "vm_#{name}".to_sym @config_loader.set(vm_config_key, sub_vm.config_procs) config, config_warnings, config_errors = \ - @config_loader.load([:default, :home, :root, vm_config_key]) + @config_loader.load([:home, :root, vm_config_key]) # Determine the possible box formats for any boxes and find the box box_formats = provider_options[:box_format] || provider @@ -365,7 +364,7 @@ module Vagrant box_config_key = "box_#{box.name}_#{box.provider}".to_sym @config_loader.set(box_config_key, box_vagrantfile) config, config_warnings, config_errors = \ - @config_loader.load([:default, box_config_key, :home, :root, vm_config_key]) + @config_loader.load([box_config_key, :home, :root, vm_config_key]) end end @@ -377,7 +376,7 @@ module Vagrant provider_override_key = "vm_#{name}_#{config.vm.box}_#{provider}".to_sym @config_loader.set(provider_override_key, provider_overrides) config, config_warnings, config_errors = \ - @config_loader.load([:default, box_config_key, :home, :root, vm_config_key, provider_override_key]) + @config_loader.load([box_config_key, :home, :root, vm_config_key, provider_override_key]) end if config.vm.box && original_box != config.vm.box diff --git a/plugins/commands/box/command/repackage.rb b/plugins/commands/box/command/repackage.rb index 5c408accb..103c0da92 100644 --- a/plugins/commands/box/command/repackage.rb +++ b/plugins/commands/box/command/repackage.rb @@ -31,7 +31,8 @@ module VagrantPlugins raise Vagrant::Errors::BoxNotFound, :name => box_name, :provider => box_provider if !box # Repackage the box - output_path = Pathname.new(File.expand_path(@env.config_global.package.name, FileUtils.pwd)) + output_name = @env.config_global.package.name || "package.box" + output_path = Pathname.new(File.expand_path(output_name, FileUtils.pwd)) box.repackage(output_path) # Success, exit status 0 diff --git a/plugins/kernel_v2/config/package.rb b/plugins/kernel_v2/config/package.rb index c8b3b8c56..2719d89f2 100644 --- a/plugins/kernel_v2/config/package.rb +++ b/plugins/kernel_v2/config/package.rb @@ -5,6 +5,14 @@ module VagrantPlugins class PackageConfig < Vagrant.plugin("2", :config) attr_accessor :name + def initialize + @name = UNSET_VALUE + end + + def finalize! + @name = nil if @name == UNSET_VALUE + end + def to_s "Package" end diff --git a/plugins/kernel_v2/config/ssh.rb b/plugins/kernel_v2/config/ssh.rb index e39ec65ce..b8153a55f 100644 --- a/plugins/kernel_v2/config/ssh.rb +++ b/plugins/kernel_v2/config/ssh.rb @@ -41,12 +41,13 @@ module VagrantPlugins @forward_agent = false if @forward_agent == UNSET_VALUE @forward_x11 = false if @forward_x11 == UNSET_VALUE - @guest_port = nil if @guest_port == UNSET_VALUE - @keep_alive = false if @keep_alive == UNSET_VALUE + @guest_port = 22 if @guest_port == UNSET_VALUE + @keep_alive = true if @keep_alive == UNSET_VALUE @proxy_command = nil if @proxy_command == UNSET_VALUE @pty = false if @pty == UNSET_VALUE - @shell = nil if @shell == UNSET_VALUE + @shell = "bash -l" if @shell == UNSET_VALUE + @default.username = "vagrant" if @default.username == UNSET_VALUE @default.finalize! end diff --git a/plugins/kernel_v2/config/vagrant.rb b/plugins/kernel_v2/config/vagrant.rb index 0cad409cb..344fd2486 100644 --- a/plugins/kernel_v2/config/vagrant.rb +++ b/plugins/kernel_v2/config/vagrant.rb @@ -10,7 +10,7 @@ module VagrantPlugins end def finalize! - @host = nil if @host == UNSET_VALUE + @host = :detect if @host == UNSET_VALUE @host = @host.to_sym if @host end diff --git a/plugins/kernel_v2/config/vm.rb b/plugins/kernel_v2/config/vm.rb index 9a64eed94..1e92f68f3 100644 --- a/plugins/kernel_v2/config/vm.rb +++ b/plugins/kernel_v2/config/vm.rb @@ -30,6 +30,7 @@ module VagrantPlugins attr_reader :provisioners def initialize + @base_mac = UNSET_VALUE @boot_timeout = UNSET_VALUE @box_download_ca_cert = UNSET_VALUE @box_download_checksum = UNSET_VALUE @@ -41,6 +42,7 @@ module VagrantPlugins @guest = UNSET_VALUE @hostname = UNSET_VALUE @provisioners = [] + @usable_port_range = UNSET_VALUE # Internal state @__compiled_provider_configs = {} @@ -299,6 +301,7 @@ module VagrantPlugins def finalize! # Defaults + @base_mac = nil if @base_mac == UNSET_VALUE @boot_timeout = 300 if @boot_timeout == UNSET_VALUE @box_download_ca_cert = nil if @box_download_ca_cert == UNSET_VALUE @box_download_checksum = nil if @box_download_checksum == UNSET_VALUE @@ -306,11 +309,15 @@ module VagrantPlugins @box_download_client_cert = nil if @box_download_client_cert == UNSET_VALUE @box_download_insecure = false if @box_download_insecure == UNSET_VALUE @box_url = nil if @box_url == UNSET_VALUE - @graceful_halt_timeout = 300 if @graceful_halt_timeout == UNSET_VALUE + @graceful_halt_timeout = 60 if @graceful_halt_timeout == UNSET_VALUE @guest = nil if @guest == UNSET_VALUE @hostname = nil if @hostname == UNSET_VALUE @hostname = @hostname.to_s if @hostname + if @usable_port_range == UNSET_VALUE + @usable_port_range = (2200..2250) + end + # Make sure that the download checksum is a string and that # the type is a symbol @box_download_checksum = "" if !@box_download_checksum @@ -330,8 +337,18 @@ module VagrantPlugins # default VM which just inherits the rest of the configuration. define(DEFAULT_VM_NAME) if defined_vm_keys.empty? + # Make sure the SSH forwarding is added if it doesn't exist + if !@__networks["ssh"] + network :forwarded_port, + guest: 22, + host: 2222, + host_ip: "127.0.0.1", + id: "ssh", + auto_correct: true + end + # Clean up some network configurations - @__networks.each do |type, opts| + @__networks.values.each do |type, opts| if type == :forwarded_port opts[:guest] = opts[:guest].to_i if opts[:guest] opts[:host] = opts[:host].to_i if opts[:host] @@ -360,11 +377,17 @@ module VagrantPlugins @__compiled_provider_configs[name] = config end - # Finaliez all the provisioners + # Finalize all the provisioners @provisioners.each do |p| p.config.finalize! if !p.invalid? end + # If we didn't share our current directory, then do it + # manually. + if !@__synced_folders["/vagrant"] + synced_folder(".", "/vagrant") + end + @__synced_folders.each do |id, options| if options[:nfs] options[:type] = :nfs diff --git a/plugins/synced_folders/nfs/config.rb b/plugins/synced_folders/nfs/config.rb index d842f78b1..21ff5274d 100644 --- a/plugins/synced_folders/nfs/config.rb +++ b/plugins/synced_folders/nfs/config.rb @@ -14,8 +14,8 @@ module VagrantPlugins end def finalize! - @map_uid = nil if @map_uid == UNSET_VALUE - @map_gid = nil if @map_gid == UNSET_VALUE + @map_uid = :auto if @map_uid == UNSET_VALUE + @map_gid = :auto if @map_gid == UNSET_VALUE end def to_s diff --git a/test/unit/plugins/kernel_v2/config/package_test.rb b/test/unit/plugins/kernel_v2/config/package_test.rb new file mode 100644 index 000000000..c20910a56 --- /dev/null +++ b/test/unit/plugins/kernel_v2/config/package_test.rb @@ -0,0 +1,14 @@ +require File.expand_path("../../../../base", __FILE__) + +require Vagrant.source_root.join("plugins/kernel_v2/config/package") + +describe VagrantPlugins::Kernel_V2::PackageConfig do + subject { described_class.new } + + describe "#name" do + it "defaults to nil" do + subject.finalize! + expect(subject.name).to be_nil + end + end +end diff --git a/test/unit/plugins/kernel_v2/config/ssh_test.rb b/test/unit/plugins/kernel_v2/config/ssh_test.rb new file mode 100644 index 000000000..61365518b --- /dev/null +++ b/test/unit/plugins/kernel_v2/config/ssh_test.rb @@ -0,0 +1,14 @@ +require File.expand_path("../../../../base", __FILE__) + +require Vagrant.source_root.join("plugins/kernel_v2/config/ssh") + +describe VagrantPlugins::Kernel_V2::SSHConfig do + subject { described_class.new } + + describe "#default" do + it "defaults to vagrant username" do + subject.finalize! + expect(subject.default.username).to eq("vagrant") + end + end +end diff --git a/test/unit/plugins/kernel_v2/config/vagrant_test.rb b/test/unit/plugins/kernel_v2/config/vagrant_test.rb new file mode 100644 index 000000000..d3b876942 --- /dev/null +++ b/test/unit/plugins/kernel_v2/config/vagrant_test.rb @@ -0,0 +1,20 @@ +require File.expand_path("../../../../base", __FILE__) + +require Vagrant.source_root.join("plugins/kernel_v2/config/vagrant") + +describe VagrantPlugins::Kernel_V2::VagrantConfig do + subject { described_class.new } + + describe "#host" do + it "defaults to :detect" do + subject.finalize! + expect(subject.host).to eq(:detect) + end + + it "symbolizes" do + subject.host = "foo" + subject.finalize! + expect(subject.host).to eq(:foo) + end + end +end diff --git a/test/unit/plugins/kernel_v2/config/vm_test.rb b/test/unit/plugins/kernel_v2/config/vm_test.rb index b24f3a363..99afefdab 100644 --- a/test/unit/plugins/kernel_v2/config/vm_test.rb +++ b/test/unit/plugins/kernel_v2/config/vm_test.rb @@ -5,6 +5,45 @@ require Vagrant.source_root.join("plugins/kernel_v2/config/vm") describe VagrantPlugins::Kernel_V2::VMConfig do subject { described_class.new } + describe "#base_mac" do + it "defaults properly" do + subject.finalize! + expect(subject.base_mac).to be_nil + end + end + + describe "#box_url" do + it "defaults properly" do + subject.finalize! + expect(subject.box_url).to be_nil + end + end + + describe "#network(s)" do + it "defaults to forwarding SSH" do + subject.finalize! + n = subject.networks + expect(n.length).to eq(1) + expect(n[0][0]).to eq(:forwarded_port) + expect(n[0][1][:guest]).to eq(22) + expect(n[0][1][:host]).to eq(2222) + expect(n[0][1][:host_ip]).to eq("127.0.0.1") + expect(n[0][1][:id]).to eq("ssh") + end + + it "turns all forwarded port ports to ints" do + subject.network "forwarded_port", + guest: "45", host: "4545", id: "test" + subject.finalize! + n = subject.networks.find do |type, data| + type == :forwarded_port && data[:id] == "test" + end + expect(n).to_not be_nil + expect(n[1][:guest]).to eq(45) + expect(n[1][:host]).to eq(4545) + end + end + describe "#provision" do it "stores the provisioners" do subject.provision("shell", inline: "foo") @@ -100,4 +139,31 @@ describe VagrantPlugins::Kernel_V2::VMConfig do end end end + + describe "#synced_folder(s)" do + it "defaults to sharing the current directory" do + subject.finalize! + sf = subject.synced_folders + expect(sf.length).to eq(1) + expect(sf).to have_key("/vagrant") + expect(sf["/vagrant"][:disabled]).to_not be + end + + it "allows overriding settings on the /vagrant sf" do + subject.synced_folder(".", "/vagrant", disabled: true) + subject.finalize! + sf = subject.synced_folders + expect(sf.length).to eq(1) + expect(sf).to have_key("/vagrant") + expect(sf["/vagrant"][:disabled]).to be_true + end + end + + describe "#usable_port_range" do + it "defaults properly" do + subject.finalize! + expect(subject.usable_port_range).to eq( + Range.new(2200, 2250)) + end + end end diff --git a/test/unit/plugins/synced_folders/nfs/config_test.rb b/test/unit/plugins/synced_folders/nfs/config_test.rb new file mode 100644 index 000000000..5fcca1618 --- /dev/null +++ b/test/unit/plugins/synced_folders/nfs/config_test.rb @@ -0,0 +1,21 @@ +require File.expand_path("../../../../base", __FILE__) + +require Vagrant.source_root.join("plugins/synced_folders/nfs/config") + +describe VagrantPlugins::SyncedFolderNFS::Config do + subject { described_class.new } + + describe "#map_gid" do + it "defaults to :auto" do + subject.finalize! + expect(subject.map_gid).to eq(:auto) + end + end + + describe "#map_uid" do + it "defaults to nil" do + subject.finalize! + expect(subject.map_uid).to eq(:auto) + end + end +end