fixed-vagrant 0.7.4.dev

Sign up to get free protection for your applications and to get access to all the features.
Files changed (242) hide show
  1. data/.gitignore +15 -0
  2. data/.yardopts +1 -0
  3. data/CHANGELOG.md +272 -0
  4. data/Gemfile +21 -0
  5. data/LICENSE +21 -0
  6. data/README.md +75 -0
  7. data/Rakefile +22 -0
  8. data/bin/vagrant +22 -0
  9. data/config/default.rb +39 -0
  10. data/contrib/README.md +12 -0
  11. data/contrib/emacs/vagrant.el +8 -0
  12. data/contrib/vim/vagrantfile.vim +9 -0
  13. data/keys/README.md +17 -0
  14. data/keys/vagrant +27 -0
  15. data/keys/vagrant.ppk +26 -0
  16. data/keys/vagrant.pub +1 -0
  17. data/lib/vagrant.rb +42 -0
  18. data/lib/vagrant/action.rb +134 -0
  19. data/lib/vagrant/action/box.rb +11 -0
  20. data/lib/vagrant/action/box/destroy.rb +21 -0
  21. data/lib/vagrant/action/box/download.rb +72 -0
  22. data/lib/vagrant/action/box/package.rb +19 -0
  23. data/lib/vagrant/action/box/unpackage.rb +55 -0
  24. data/lib/vagrant/action/box/verify.rb +23 -0
  25. data/lib/vagrant/action/builder.rb +124 -0
  26. data/lib/vagrant/action/builtin.rb +104 -0
  27. data/lib/vagrant/action/env.rb +7 -0
  28. data/lib/vagrant/action/env/set.rb +18 -0
  29. data/lib/vagrant/action/environment.rb +50 -0
  30. data/lib/vagrant/action/general.rb +8 -0
  31. data/lib/vagrant/action/general/package.rb +109 -0
  32. data/lib/vagrant/action/general/validate.rb +19 -0
  33. data/lib/vagrant/action/vm.rb +31 -0
  34. data/lib/vagrant/action/vm/boot.rb +48 -0
  35. data/lib/vagrant/action/vm/check_box.rb +28 -0
  36. data/lib/vagrant/action/vm/check_guest_additions.rb +30 -0
  37. data/lib/vagrant/action/vm/clean_machine_folder.rb +43 -0
  38. data/lib/vagrant/action/vm/clear_forwarded_ports.rb +39 -0
  39. data/lib/vagrant/action/vm/clear_nfs_exports.rb +20 -0
  40. data/lib/vagrant/action/vm/clear_shared_folders.rb +32 -0
  41. data/lib/vagrant/action/vm/customize.rb +21 -0
  42. data/lib/vagrant/action/vm/destroy.rb +19 -0
  43. data/lib/vagrant/action/vm/destroy_unused_network_interfaces.rb +30 -0
  44. data/lib/vagrant/action/vm/discard_state.rb +22 -0
  45. data/lib/vagrant/action/vm/export.rb +52 -0
  46. data/lib/vagrant/action/vm/forward_ports.rb +134 -0
  47. data/lib/vagrant/action/vm/forward_ports_helpers.rb +28 -0
  48. data/lib/vagrant/action/vm/halt.rb +29 -0
  49. data/lib/vagrant/action/vm/host_name.rb +21 -0
  50. data/lib/vagrant/action/vm/import.rb +35 -0
  51. data/lib/vagrant/action/vm/match_mac_address.rb +21 -0
  52. data/lib/vagrant/action/vm/network.rb +139 -0
  53. data/lib/vagrant/action/vm/nfs.rb +159 -0
  54. data/lib/vagrant/action/vm/nfs_helpers.rb +11 -0
  55. data/lib/vagrant/action/vm/package.rb +23 -0
  56. data/lib/vagrant/action/vm/package_vagrantfile.rb +33 -0
  57. data/lib/vagrant/action/vm/provision.rb +41 -0
  58. data/lib/vagrant/action/vm/resume.rb +20 -0
  59. data/lib/vagrant/action/vm/share_folders.rb +70 -0
  60. data/lib/vagrant/action/vm/suspend.rb +20 -0
  61. data/lib/vagrant/action/warden.rb +79 -0
  62. data/lib/vagrant/box.rb +90 -0
  63. data/lib/vagrant/box_collection.rb +53 -0
  64. data/lib/vagrant/cli.rb +55 -0
  65. data/lib/vagrant/command.rb +25 -0
  66. data/lib/vagrant/command/base.rb +106 -0
  67. data/lib/vagrant/command/box.rb +33 -0
  68. data/lib/vagrant/command/destroy.rb +17 -0
  69. data/lib/vagrant/command/group_base.rb +107 -0
  70. data/lib/vagrant/command/halt.rb +18 -0
  71. data/lib/vagrant/command/helpers.rb +33 -0
  72. data/lib/vagrant/command/init.rb +14 -0
  73. data/lib/vagrant/command/named_base.rb +14 -0
  74. data/lib/vagrant/command/package.rb +41 -0
  75. data/lib/vagrant/command/provision.rb +17 -0
  76. data/lib/vagrant/command/reload.rb +17 -0
  77. data/lib/vagrant/command/resume.rb +17 -0
  78. data/lib/vagrant/command/ssh.rb +42 -0
  79. data/lib/vagrant/command/ssh_config.rb +26 -0
  80. data/lib/vagrant/command/status.rb +22 -0
  81. data/lib/vagrant/command/suspend.rb +17 -0
  82. data/lib/vagrant/command/up.rb +19 -0
  83. data/lib/vagrant/command/upgrade_to_060.rb +45 -0
  84. data/lib/vagrant/command/version.rb +13 -0
  85. data/lib/vagrant/config.rb +123 -0
  86. data/lib/vagrant/config/base.rb +85 -0
  87. data/lib/vagrant/config/error_recorder.rb +19 -0
  88. data/lib/vagrant/config/nfs.rb +10 -0
  89. data/lib/vagrant/config/package.rb +9 -0
  90. data/lib/vagrant/config/ssh.rb +33 -0
  91. data/lib/vagrant/config/top.rb +61 -0
  92. data/lib/vagrant/config/vagrant.rb +16 -0
  93. data/lib/vagrant/config/vm.rb +137 -0
  94. data/lib/vagrant/config/vm/provisioner.rb +56 -0
  95. data/lib/vagrant/config/vm/sub_vm.rb +17 -0
  96. data/lib/vagrant/data_store.rb +70 -0
  97. data/lib/vagrant/downloaders.rb +7 -0
  98. data/lib/vagrant/downloaders/base.rb +23 -0
  99. data/lib/vagrant/downloaders/file.rb +22 -0
  100. data/lib/vagrant/downloaders/http.rb +64 -0
  101. data/lib/vagrant/environment.rb +392 -0
  102. data/lib/vagrant/errors.rb +332 -0
  103. data/lib/vagrant/hosts.rb +8 -0
  104. data/lib/vagrant/hosts/arch.rb +27 -0
  105. data/lib/vagrant/hosts/base.rb +78 -0
  106. data/lib/vagrant/hosts/bsd.rb +52 -0
  107. data/lib/vagrant/hosts/linux.rb +47 -0
  108. data/lib/vagrant/plugin.rb +57 -0
  109. data/lib/vagrant/provisioners.rb +9 -0
  110. data/lib/vagrant/provisioners/base.rb +63 -0
  111. data/lib/vagrant/provisioners/chef.rb +130 -0
  112. data/lib/vagrant/provisioners/chef_server.rb +103 -0
  113. data/lib/vagrant/provisioners/chef_solo.rb +142 -0
  114. data/lib/vagrant/provisioners/puppet.rb +137 -0
  115. data/lib/vagrant/provisioners/puppet_server.rb +55 -0
  116. data/lib/vagrant/provisioners/shell.rb +52 -0
  117. data/lib/vagrant/ssh.rb +173 -0
  118. data/lib/vagrant/ssh/session.rb +125 -0
  119. data/lib/vagrant/systems.rb +11 -0
  120. data/lib/vagrant/systems/base.rb +87 -0
  121. data/lib/vagrant/systems/debian.rb +36 -0
  122. data/lib/vagrant/systems/freebsd.rb +84 -0
  123. data/lib/vagrant/systems/gentoo.rb +27 -0
  124. data/lib/vagrant/systems/linux.rb +83 -0
  125. data/lib/vagrant/systems/linux/config.rb +21 -0
  126. data/lib/vagrant/systems/linux/error.rb +9 -0
  127. data/lib/vagrant/systems/redhat.rb +39 -0
  128. data/lib/vagrant/systems/solaris.rb +62 -0
  129. data/lib/vagrant/systems/ubuntu.rb +17 -0
  130. data/lib/vagrant/test_helpers.rb +128 -0
  131. data/lib/vagrant/ui.rb +69 -0
  132. data/lib/vagrant/util.rb +13 -0
  133. data/lib/vagrant/util/busy.rb +59 -0
  134. data/lib/vagrant/util/hash_with_indifferent_access.rb +63 -0
  135. data/lib/vagrant/util/plain_logger.rb +25 -0
  136. data/lib/vagrant/util/platform.rb +65 -0
  137. data/lib/vagrant/util/resource_logger.rb +63 -0
  138. data/lib/vagrant/util/retryable.rb +25 -0
  139. data/lib/vagrant/util/stacked_proc_runner.rb +35 -0
  140. data/lib/vagrant/util/template_renderer.rb +83 -0
  141. data/lib/vagrant/version.rb +6 -0
  142. data/lib/vagrant/vm.rb +177 -0
  143. data/templates/chef_server_client.erb +18 -0
  144. data/templates/chef_solo_solo.erb +19 -0
  145. data/templates/commands/init/Vagrantfile.erb +82 -0
  146. data/templates/config/validation_failed.erb +7 -0
  147. data/templates/locales/en.yml +535 -0
  148. data/templates/network_entry_debian.erb +8 -0
  149. data/templates/network_entry_gentoo.erb +5 -0
  150. data/templates/network_entry_redhat.erb +8 -0
  151. data/templates/nfs/exports.erb +5 -0
  152. data/templates/nfs/exports_linux.erb +5 -0
  153. data/templates/package_Vagrantfile.erb +11 -0
  154. data/templates/ssh_config.erb +9 -0
  155. data/test/locales/en.yml +8 -0
  156. data/test/test_helper.rb +26 -0
  157. data/test/vagrant/action/box/destroy_test.rb +18 -0
  158. data/test/vagrant/action/box/download_test.rb +125 -0
  159. data/test/vagrant/action/box/package_test.rb +25 -0
  160. data/test/vagrant/action/box/unpackage_test.rb +84 -0
  161. data/test/vagrant/action/box/verify_test.rb +30 -0
  162. data/test/vagrant/action/builder_test.rb +203 -0
  163. data/test/vagrant/action/env/set_test.rb +24 -0
  164. data/test/vagrant/action/environment_test.rb +27 -0
  165. data/test/vagrant/action/general/package_test.rb +268 -0
  166. data/test/vagrant/action/general/validate_test.rb +31 -0
  167. data/test/vagrant/action/vm/boot_test.rb +66 -0
  168. data/test/vagrant/action/vm/check_box_test.rb +56 -0
  169. data/test/vagrant/action/vm/check_guest_additions_test.rb +9 -0
  170. data/test/vagrant/action/vm/clean_machine_folder_test.rb +84 -0
  171. data/test/vagrant/action/vm/clear_forwarded_ports_test.rb +72 -0
  172. data/test/vagrant/action/vm/clear_nfs_exports_test.rb +22 -0
  173. data/test/vagrant/action/vm/clear_shared_folders_test.rb +49 -0
  174. data/test/vagrant/action/vm/customize_test.rb +30 -0
  175. data/test/vagrant/action/vm/destroy_test.rb +25 -0
  176. data/test/vagrant/action/vm/destroy_unused_network_interfaces_test.rb +49 -0
  177. data/test/vagrant/action/vm/discard_state_test.rb +45 -0
  178. data/test/vagrant/action/vm/export_test.rb +107 -0
  179. data/test/vagrant/action/vm/forward_ports_helpers_test.rb +70 -0
  180. data/test/vagrant/action/vm/forward_ports_test.rb +194 -0
  181. data/test/vagrant/action/vm/halt_test.rb +79 -0
  182. data/test/vagrant/action/vm/host_name_test.rb +36 -0
  183. data/test/vagrant/action/vm/import_test.rb +66 -0
  184. data/test/vagrant/action/vm/match_mac_address_test.rb +36 -0
  185. data/test/vagrant/action/vm/network_test.rb +286 -0
  186. data/test/vagrant/action/vm/nfs_helpers_test.rb +26 -0
  187. data/test/vagrant/action/vm/nfs_test.rb +260 -0
  188. data/test/vagrant/action/vm/package_test.rb +25 -0
  189. data/test/vagrant/action/vm/package_vagrantfile_test.rb +46 -0
  190. data/test/vagrant/action/vm/provision_test.rb +90 -0
  191. data/test/vagrant/action/vm/resume_test.rb +35 -0
  192. data/test/vagrant/action/vm/share_folders_test.rb +139 -0
  193. data/test/vagrant/action/vm/suspend_test.rb +35 -0
  194. data/test/vagrant/action/warden_test.rb +119 -0
  195. data/test/vagrant/action_test.rb +89 -0
  196. data/test/vagrant/box_collection_test.rb +45 -0
  197. data/test/vagrant/box_test.rb +74 -0
  198. data/test/vagrant/cli_test.rb +35 -0
  199. data/test/vagrant/command/base_test.rb +23 -0
  200. data/test/vagrant/command/group_base_test.rb +15 -0
  201. data/test/vagrant/command/helpers_test.rb +88 -0
  202. data/test/vagrant/command/package_test.rb +27 -0
  203. data/test/vagrant/config/base_test.rb +52 -0
  204. data/test/vagrant/config/error_recorder_test.rb +18 -0
  205. data/test/vagrant/config/ssh_test.rb +12 -0
  206. data/test/vagrant/config/vagrant_test.rb +35 -0
  207. data/test/vagrant/config/vm/provisioner_test.rb +92 -0
  208. data/test/vagrant/config/vm_test.rb +86 -0
  209. data/test/vagrant/config_test.rb +162 -0
  210. data/test/vagrant/data_store_test.rb +77 -0
  211. data/test/vagrant/downloaders/base_test.rb +28 -0
  212. data/test/vagrant/downloaders/file_test.rb +48 -0
  213. data/test/vagrant/downloaders/http_test.rb +80 -0
  214. data/test/vagrant/environment_test.rb +508 -0
  215. data/test/vagrant/errors_test.rb +42 -0
  216. data/test/vagrant/hosts/base_test.rb +46 -0
  217. data/test/vagrant/hosts/bsd_test.rb +53 -0
  218. data/test/vagrant/hosts/linux_test.rb +54 -0
  219. data/test/vagrant/plugin_test.rb +9 -0
  220. data/test/vagrant/provisioners/base_test.rb +63 -0
  221. data/test/vagrant/provisioners/chef_server_test.rb +188 -0
  222. data/test/vagrant/provisioners/chef_solo_test.rb +219 -0
  223. data/test/vagrant/provisioners/chef_test.rb +179 -0
  224. data/test/vagrant/provisioners/puppet_server_test.rb +68 -0
  225. data/test/vagrant/provisioners/puppet_test.rb +182 -0
  226. data/test/vagrant/provisioners/shell_test.rb +68 -0
  227. data/test/vagrant/ssh/session_test.rb +40 -0
  228. data/test/vagrant/ssh_test.rb +342 -0
  229. data/test/vagrant/systems/base_test.rb +18 -0
  230. data/test/vagrant/systems/linux_test.rb +114 -0
  231. data/test/vagrant/ui_test.rb +29 -0
  232. data/test/vagrant/util/busy_test.rb +106 -0
  233. data/test/vagrant/util/hash_with_indifferent_access_test.rb +39 -0
  234. data/test/vagrant/util/plain_logger_test.rb +17 -0
  235. data/test/vagrant/util/platform_test.rb +18 -0
  236. data/test/vagrant/util/resource_logger_test.rb +78 -0
  237. data/test/vagrant/util/retryable_test.rb +50 -0
  238. data/test/vagrant/util/stacked_proc_runner_test.rb +43 -0
  239. data/test/vagrant/util/template_renderer_test.rb +145 -0
  240. data/test/vagrant/vm_test.rb +291 -0
  241. data/vagrant.gemspec +36 -0
  242. metadata +514 -0
@@ -0,0 +1,17 @@
1
+ module Vagrant
2
+ module Command
3
+ class ProvisionCommand < NamedBase
4
+ register "provision", "Rerun the provisioning scripts on a running VM"
5
+
6
+ def execute
7
+ target_vms.each do |vm|
8
+ if vm.created? && vm.vm.running?
9
+ vm.provision
10
+ else
11
+ vm.env.ui.info I18n.t("vagrant.commands.common.vm_not_created")
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ module Vagrant
2
+ module Command
3
+ class ReloadCommand < NamedBase
4
+ register "reload", "Reload the environment, halting it then restarting it."
5
+
6
+ def execute
7
+ target_vms.each do |vm|
8
+ if vm.created?
9
+ vm.reload
10
+ else
11
+ vm.env.ui.info I18n.t("vagrant.commands.common.vm_not_created")
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ module Vagrant
2
+ module Command
3
+ class ResumeCommand < NamedBase
4
+ register "resume", "Resume a suspended Vagrant environment."
5
+
6
+ def execute
7
+ target_vms.each do |vm|
8
+ if vm.created?
9
+ vm.resume
10
+ else
11
+ vm.env.ui.info I18n.t("vagrant.commands.common.vm_not_created")
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,42 @@
1
+ module Vagrant
2
+ module Command
3
+ class SSHCommand < NamedBase
4
+ class_option :execute, :type => :string, :default => false, :aliases => "-e"
5
+ register "ssh", "SSH into the currently running Vagrant environment."
6
+
7
+ def execute
8
+ if options[:execute]
9
+ ssh_execute
10
+ else
11
+ ssh_connect
12
+ end
13
+ end
14
+
15
+ protected
16
+
17
+ def ssh_execute
18
+ ssh_vm.ssh.execute do |ssh|
19
+ ssh_vm.env.ui.info I18n.t("vagrant.commands.ssh.execute", :command => options[:execute])
20
+ ssh.exec!(options[:execute]) do |channel, type, data|
21
+ ssh_vm.env.ui.info "#{data}"
22
+ end
23
+ end
24
+ end
25
+
26
+ def ssh_connect
27
+ raise Errors::VMNotCreatedError if !ssh_vm.created?
28
+ raise Errors::VMNotRunningError if !ssh_vm.vm.running?
29
+ ssh_vm.ssh.connect
30
+ end
31
+
32
+ def ssh_vm
33
+ @ssh_vm ||= begin
34
+ vm = self.name.nil? && env.multivm? ? env.primary_vm : nil
35
+ raise Errors::MultiVMTargetRequired, :command => "ssh" if !vm && target_vms.length > 1
36
+ vm = target_vms.first if !vm
37
+ vm
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,26 @@
1
+ module Vagrant
2
+ module Command
3
+ class SSHConfigCommand < NamedBase
4
+ class_option :host, :type => :string, :default => nil, :aliases => "-h"
5
+ register "ssh_config", "outputs .ssh/config valid syntax for connecting to this environment via ssh"
6
+
7
+ def execute
8
+ raise Errors::MultiVMTargetRequired, :command => "ssh_config" if target_vms.length > 1
9
+ vm = target_vms.first
10
+ raise Errors::VMNotCreatedError if !vm.created?
11
+
12
+ # We need to fix the file permissions of the key if they aren't set
13
+ # properly, otherwise if the user attempts to SSH in, it won't work!
14
+ vm.ssh.check_key_permissions(vm.env.config.ssh.private_key_path)
15
+
16
+ $stdout.puts(Util::TemplateRenderer.render("ssh_config", {
17
+ :host_key => options[:host] || "vagrant",
18
+ :ssh_host => vm.env.config.ssh.host,
19
+ :ssh_user => vm.env.config.ssh.username,
20
+ :ssh_port => vm.ssh.port,
21
+ :private_key_path => vm.env.config.ssh.private_key_path
22
+ }))
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,22 @@
1
+ module Vagrant
2
+ module Command
3
+ class StatusCommand < NamedBase
4
+ register "status", "Shows the status of the current Vagrant environment."
5
+
6
+ def route
7
+ state = nil
8
+ results = target_vms.collect do |vm|
9
+ state = vm.created? ? vm.vm.state.to_s : "not_created"
10
+ "#{vm.name.to_s.ljust(25)}#{state.gsub("_", " ")}"
11
+ end
12
+
13
+ state = target_vms.length == 1 ? state : "listing"
14
+
15
+ env.ui.info(I18n.t("vagrant.commands.status.output",
16
+ :states => results.join("\n"),
17
+ :message => I18n.t("vagrant.commands.status.#{state}")),
18
+ :prefix => false)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,17 @@
1
+ module Vagrant
2
+ module Command
3
+ class SuspendCommand < NamedBase
4
+ register "suspend", "Suspend a running Vagrant environment."
5
+
6
+ def execute
7
+ target_vms.each do |vm|
8
+ if vm.created?
9
+ vm.suspend
10
+ else
11
+ vm.env.ui.info I18n.t("vagrant.commands.common.vm_not_created")
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ module Vagrant
2
+ module Command
3
+ class UpCommand < NamedBase
4
+ class_option :provision, :type => :boolean, :default => true
5
+ register "up", "Creates the Vagrant environment"
6
+
7
+ def execute
8
+ target_vms.each do |vm|
9
+ if vm.created?
10
+ vm.env.ui.info I18n.t("vagrant.commands.up.vm_created")
11
+ vm.start("provision.enabled" => options[:provision])
12
+ else
13
+ vm.up("provision.enabled" => options[:provision])
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,45 @@
1
+ require 'fileutils'
2
+
3
+ module Vagrant
4
+ module Command
5
+ class UpgradeTo060Command < Base
6
+ register "upgrade_to_060", "Upgrade pre-0.6.0 environment to 0.6.0", :hide => true
7
+
8
+ def execute
9
+ @env.ui.warn I18n.t("vagrant.commands.upgrade_to_060.info"), :prefix => false
10
+ @env.ui.warn "", :prefix => false
11
+ if !@env.ui.yes? I18n.t("vagrant.commands.upgrade_to_060.ask"), :prefix => false, :color => :yellow
12
+ @env.ui.info I18n.t("vagrant.commands.upgrade_to_060.quit"), :prefix => false
13
+ return
14
+ end
15
+
16
+ local_data = @env.local_data
17
+ if !local_data.empty?
18
+ if local_data[:active]
19
+ @env.ui.confirm I18n.t("vagrant.commands.upgrade_to_060.already_done"), :prefix => false
20
+ return
21
+ end
22
+
23
+ # Backup the previous file
24
+ @env.ui.info I18n.t("vagrant.commands.upgrade_to_060.backing_up"), :prefix => false
25
+ FileUtils.cp(local_data.file_path, "#{local_data.file_path}.bak-#{Time.now.to_i}")
26
+
27
+ # Gather the previously set virtual machines into a single
28
+ # active hash
29
+ active = local_data.inject({}) do |acc, data|
30
+ key, uuid = data
31
+ acc[key.to_sym] = uuid
32
+ acc
33
+ end
34
+
35
+ # Set the active hash to the active list and save it
36
+ local_data.clear
37
+ local_data[:active] = active
38
+ local_data.commit
39
+ end
40
+
41
+ @env.ui.confirm I18n.t("vagrant.commands.upgrade_to_060.complete"), :prefix => false
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,13 @@
1
+ module Vagrant
2
+ module Command
3
+ class VersionCommand < Base
4
+ register "version", "Prints the Vagrant version information", :alias => %w(-v --version)
5
+
6
+ def version
7
+ env.ui.info(I18n.t("vagrant.commands.version.output",
8
+ :version => Vagrant::VERSION),
9
+ :prefix => false)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,123 @@
1
+ require 'vagrant/config/base'
2
+ require 'vagrant/config/error_recorder'
3
+ require 'vagrant/config/top'
4
+
5
+ # The built-in configuration classes
6
+ require 'vagrant/config/vagrant'
7
+ require 'vagrant/config/ssh'
8
+ require 'vagrant/config/nfs'
9
+ require 'vagrant/config/vm'
10
+ require 'vagrant/config/package'
11
+
12
+ module Vagrant
13
+ # The config class is responsible for loading Vagrant configurations, which
14
+ # are usually found in Vagrantfiles but may also be procs. The loading is done
15
+ # by specifying a queue of files or procs that are for configuration, and then
16
+ # executing them. The config loader will run each item in the queue, so that
17
+ # configuration from later items overwrite that from earlier items. This is how
18
+ # Vagrant "scoping" of Vagranfiles is implemented.
19
+ #
20
+ # If you're looking to create your own configuration classes, see {Base}.
21
+ #
22
+ # # Loading Configuration Files
23
+ #
24
+ # If you are in fact looking to load configuration files, then this is the
25
+ # class you are looking for. Loading configuration is quite easy. The following
26
+ # example assumes `env` is already a loaded instance of {Environment}:
27
+ #
28
+ # config = Vagrant::Config.new
29
+ # config.set(:first, "/path/to/some/Vagrantfile")
30
+ # config.set(:second, "/path/to/another/Vagrantfile")
31
+ # config.load_order = [:first, :second]
32
+ # result = config.load(env)
33
+ #
34
+ # p "Your box is: #{result.vm.box}"
35
+ #
36
+ # The load order determines what order the config files specified are loaded.
37
+ # If a key is not mentioned (for example if above the load order was set to
38
+ # `[:first]`, therefore `:second` was not mentioned), then that config file
39
+ # won't be loaded.
40
+ class Config
41
+ # An array of symbols specifying the load order for the procs.
42
+ attr_accessor :load_order
43
+ attr_reader :procs
44
+
45
+ # This is the method which is called by all Vagrantfiles to configure Vagrant.
46
+ # This method expects a block which accepts a single argument representing
47
+ # an instance of the {Config::Top} class.
48
+ #
49
+ # Note that the block is not run immediately. Instead, it's proc is stored
50
+ # away for execution later.
51
+ def self.run(&block)
52
+ # Store it for later
53
+ @last_procs ||= []
54
+ @last_procs << block
55
+ end
56
+
57
+ # Returns the last proc which was activated for the class via {run}. This
58
+ # also sets the last proc to `nil` so that calling this method multiple times
59
+ # will not return duplicates.
60
+ #
61
+ # @return [Proc]
62
+ def self.last_proc
63
+ value = @last_procs
64
+ @last_procs = nil
65
+ value
66
+ end
67
+
68
+ def initialize(parent=nil)
69
+ @procs = {}
70
+ @load_order = []
71
+
72
+ if parent
73
+ # Shallow copy the procs and load order from parent if given
74
+ @procs = parent.procs.dup
75
+ @load_order = parent.load_order.dup
76
+ end
77
+ end
78
+
79
+ # Adds a Vagrantfile to be loaded to the queue of config procs. Note
80
+ # that this causes the Vagrantfile file to be loaded at this point,
81
+ # and it will never be loaded again.
82
+ def set(key, path)
83
+ return if @procs.has_key?(key)
84
+ @procs[key] = [path].flatten.map(&method(:proc_for)).flatten
85
+ end
86
+
87
+ # Loads the added procs using the set `load_order` attribute and returns
88
+ # the {Config::Top} object result. The configuration is loaded for the
89
+ # given {Environment} object.
90
+ #
91
+ # @param [Environment] env
92
+ def load(env)
93
+ config = Top.new(env)
94
+
95
+ # Only run the procs specified in the load order, in the order
96
+ # specified.
97
+ load_order.each do |key|
98
+ if @procs[key]
99
+ @procs[key].each do |proc|
100
+ proc.call(config) if proc
101
+ end
102
+ end
103
+ end
104
+
105
+ config
106
+ end
107
+
108
+ protected
109
+
110
+ def proc_for(path)
111
+ return nil if !path
112
+ return path if path.is_a?(Proc)
113
+
114
+ begin
115
+ Kernel.load path if File.exist?(path)
116
+ return self.class.last_proc
117
+ rescue SyntaxError => e
118
+ # Report syntax errors in a nice way for Vagrantfiles
119
+ raise Errors::VagrantfileSyntaxError, :file => e.message
120
+ end
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,85 @@
1
+ module Vagrant
2
+ class Config
3
+ # The base class for all configuration classes. This implements
4
+ # basic things such as the environment instance variable which all
5
+ # config classes need as well as a basic `to_json` implementation.
6
+ class Base
7
+ # {Top} of this configuration stack
8
+ attr_accessor :top
9
+
10
+ # Registers a subclass with the Vagrant configuration system so
11
+ # that it can then be used in Vagrantfiles.
12
+ #
13
+ # @param [Symbol] accessor The accessor on the main config object
14
+ # that is used to access the configuration class.
15
+ #
16
+ def self.configures(accessor, klass=self)
17
+ Top.configures(accessor, klass)
18
+ end
19
+
20
+ # Loads configuration values from JSON back into the proper
21
+ # configuration classes. By default, this is done by simply
22
+ # iterating over all values in the JSON hash and assigning them
23
+ # to instance variables on the class.
24
+ def self.json_create(data)
25
+ data.inject(new) do |result, data|
26
+ key, value = data
27
+ result.instance_variable_set("@#{key}".to_sym, value) if key != "json_class"
28
+ result
29
+ end
30
+ end
31
+
32
+ # A helper to access the environment that this configuration is for.
33
+ # This is obtained by getting the env from the {Top}.
34
+ #
35
+ # @return [Vagrant::Envrionment]
36
+ def env
37
+ top.env
38
+ end
39
+
40
+ # Allows setting options from a hash. By default this simply calls
41
+ # the `#{key}=` method on the config class with the value, which is
42
+ # the expected behavior most of the time.
43
+ def set_options(options)
44
+ options.each do |key, value|
45
+ send("#{key}=", value)
46
+ end
47
+ end
48
+
49
+ # Called by {Top} after the configuration is loaded to validate
50
+ # the configuaration objects. Subclasses should implement this
51
+ # method and add any errors to the `errors` object given.
52
+ #
53
+ # @param [ErrorRecorder] errors
54
+ def validate(errors); end
55
+
56
+ # Converts the configuration to a raw hash by calling `#to_hash`
57
+ # on all instance variables (if it can) and putting them into
58
+ # a hash.
59
+ def to_hash
60
+ instance_variables_hash.inject({}) do |acc, data|
61
+ k,v = data
62
+ v = v.to_hash if v.respond_to?(:to_hash)
63
+ acc[k] = v
64
+ acc
65
+ end
66
+ end
67
+
68
+ # Converts to JSON, with the `json_class` field set so that when
69
+ # the JSON is parsed back, it can be loaded back into the proper class.
70
+ # See {json_create}.
71
+ def to_json(*a)
72
+ result = { 'json_class' => self.class.name }
73
+ result.merge(instance_variables_hash).to_json(*a)
74
+ end
75
+
76
+ # Returns the instance variables as a hash of key-value pairs.
77
+ def instance_variables_hash
78
+ instance_variables.inject({}) do |acc, iv|
79
+ acc[iv.to_s[1..-1]] = instance_variable_get(iv) unless [:@env, :@top].include?(iv.to_sym)
80
+ acc
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end