fixed-vagrant 0.7.4.dev

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,23 @@
1
+ module Vagrant
2
+ module Downloaders
3
+ # Represents a base class for a downloader. A downloader handles
4
+ # downloading a box file to a temporary file.
5
+ class Base
6
+ include Vagrant::Util
7
+
8
+ # The environment which this downloader is operating.
9
+ attr_reader :env
10
+
11
+ def initialize(env)
12
+ @env = env
13
+ end
14
+
15
+ # Called prior to execution so any error checks can be done
16
+ def prepare(source_url); end
17
+
18
+ # Downloads the source file to the destination file. It is up to
19
+ # implementors of this class to handle the logic.
20
+ def download!(source_url, destination_file); end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,22 @@
1
+ require 'fileutils'
2
+
3
+ module Vagrant
4
+ module Downloaders
5
+ # "Downloads" a file to a temporary file. Basically, this downloader
6
+ # simply does a file copy.
7
+ class File < Base
8
+ def self.match?(uri)
9
+ ::File.exists?(uri)
10
+ end
11
+
12
+ def prepare(source_url)
13
+ raise Errors::DownloaderFileDoesntExist if !::File.file?(source_url)
14
+ end
15
+
16
+ def download!(source_url, destination_file)
17
+ env.ui.info I18n.t("vagrant.downloaders.file.download")
18
+ FileUtils.cp(source_url, destination_file.path)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,64 @@
1
+ require 'net/http'
2
+ require 'net/https'
3
+ require 'open-uri'
4
+ require 'uri'
5
+
6
+ module Vagrant
7
+ module Downloaders
8
+ # Downloads a file from an HTTP URL to a temporary file. This
9
+ # downloader reports its progress to stdout while downloading.
10
+ class HTTP < Base
11
+ def self.match?(uri)
12
+ # URI.parse barfs on '<drive letter>:\\files \on\ windows'
13
+ extracted = URI.extract(uri).first
14
+ extracted && extracted.include?(uri)
15
+ end
16
+
17
+ def download!(source_url, destination_file)
18
+ proxy_uri = URI.parse(ENV["http_proxy"] || "")
19
+ uri = URI.parse(source_url)
20
+ http = Net::HTTP.new(uri.host, uri.port, proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
21
+
22
+ if uri.scheme == "https"
23
+ http.use_ssl = true
24
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
25
+ end
26
+
27
+ http.start do |h|
28
+ env.ui.info I18n.t("vagrant.downloaders.http.download", :url => source_url)
29
+
30
+ h.request_get(uri.request_uri) do |response|
31
+ if response.is_a?(Net::HTTPRedirection)
32
+ # Follow the HTTP redirect.
33
+ # TODO: Error on some redirect limit
34
+ download!(response["Location"], destination_file)
35
+ return
36
+ end
37
+
38
+ total = response.content_length
39
+ progress = 0
40
+ segment_count = 0
41
+
42
+ response.read_body do |segment|
43
+ # Report the progress out
44
+ progress += segment.length
45
+ segment_count += 1
46
+
47
+ # Progress reporting is limited to every 25 segments just so
48
+ # we're not constantly updating
49
+ if segment_count % 25 == 0
50
+ env.ui.report_progress(progress, total)
51
+ segment_count = 0
52
+ end
53
+
54
+ # Store the segment
55
+ destination_file.write(segment)
56
+ end
57
+ end
58
+ end
59
+ rescue SocketError
60
+ raise Errors::DownloaderHTTPSocketError
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,392 @@
1
+ require 'pathname'
2
+ require 'fileutils'
3
+
4
+ module Vagrant
5
+ # Represents a single Vagrant environment. A "Vagrant environment" is
6
+ # defined as basically a folder with a "Vagrantfile." This class allows
7
+ # access to the VMs, CLI, etc. all in the scope of this environment.
8
+ class Environment
9
+ ROOTFILE_NAME = "Vagrantfile"
10
+ HOME_SUBDIRS = ["tmp", "boxes", "logs"]
11
+ DEFAULT_VM = :default
12
+ DEFAULT_HOME = "~/.vagrant"
13
+
14
+ # Parent environment (in the case of multi-VMs)
15
+ attr_reader :parent
16
+
17
+ # The `cwd` that this environment represents
18
+ attr_reader :cwd
19
+
20
+ # The single VM that this environment represents, in the case of
21
+ # multi-VM.
22
+ attr_accessor :vm
23
+
24
+ # The {UI} object to communicate with the outside world.
25
+ attr_writer :ui
26
+
27
+ # The {Config} object representing the Vagrantfile loader
28
+ attr_reader :config_loader
29
+
30
+ #---------------------------------------------------------------
31
+ # Class Methods
32
+ #---------------------------------------------------------------
33
+ class << self
34
+ # Verifies that VirtualBox is installed and that the version of
35
+ # VirtualBox installed is high enough.
36
+ def check_virtualbox!
37
+ version = VirtualBox.version
38
+ raise Errors::VirtualBoxNotDetected if version.nil?
39
+ raise Errors::VirtualBoxInvalidVersion, :version => version.to_s if version.to_f < 4.0
40
+ rescue Errors::VirtualBoxNotDetected
41
+ # On 64-bit Windows, show a special error. This error is a subclass
42
+ # of VirtualBoxNotDetected, so libraries which use Vagrant can just
43
+ # rescue VirtualBoxNotDetected.
44
+ raise Errors::VirtualBoxNotDetected_Win64 if Util::Platform.windows? && Util::Platform.bit64?
45
+
46
+ # Otherwise, reraise the old error
47
+ raise
48
+ end
49
+ end
50
+
51
+ # Initializes a new environment with the given options. The options
52
+ # is a hash where the main available key is `cwd`, which defines where
53
+ # the environment represents. There are other options available but
54
+ # they shouldn't be used in general. If `cwd` is nil, then it defaults
55
+ # to the `Dir.pwd` (which is the cwd of the executing process).
56
+ def initialize(opts=nil)
57
+ opts = {
58
+ :parent => nil,
59
+ :vm => nil,
60
+ :cwd => nil,
61
+ }.merge(opts || {})
62
+
63
+ opts[:cwd] ||= Dir.pwd
64
+ opts[:cwd] = Pathname.new(opts[:cwd])
65
+
66
+ opts.each do |key, value|
67
+ instance_variable_set("@#{key}".to_sym, opts[key])
68
+ end
69
+
70
+ @loaded = false
71
+ end
72
+
73
+ #---------------------------------------------------------------
74
+ # Helpers
75
+ #---------------------------------------------------------------
76
+
77
+ # The path to the `dotfile`, which contains the persisted UUID of
78
+ # the VM if it exists.
79
+ #
80
+ # @return [Pathname]
81
+ def dotfile_path
82
+ root_path.join(config.vagrant.dotfile_name) rescue nil
83
+ end
84
+
85
+ # The path to the home directory and converted into a Pathname object.
86
+ #
87
+ # @return [Pathname]
88
+ def home_path
89
+ @_home_path ||= Pathname.new(File.expand_path(ENV["VAGRANT_HOME"] || DEFAULT_HOME))
90
+ end
91
+
92
+ # The path to the Vagrant tmp directory
93
+ #
94
+ # @return [Pathname]
95
+ def tmp_path
96
+ home_path.join("tmp")
97
+ end
98
+
99
+ # The path to the Vagrant boxes directory
100
+ #
101
+ # @return [Pathname]
102
+ def boxes_path
103
+ home_path.join("boxes")
104
+ end
105
+
106
+ # Path to the Vagrant logs directory
107
+ #
108
+ # @return [Pathname]
109
+ def log_path
110
+ home_path.join("logs")
111
+ end
112
+
113
+ # Returns the name of the resource which this environment represents.
114
+ # The resource is the VM name if there is a VM it represents, otherwise
115
+ # it defaults to "vagrant"
116
+ #
117
+ # @return [String]
118
+ def resource
119
+ result = vm.name rescue nil
120
+ result || "vagrant"
121
+ end
122
+
123
+ # Returns the collection of boxes for the environment.
124
+ #
125
+ # @return [BoxCollection]
126
+ def boxes
127
+ return parent.boxes if parent
128
+ @_boxes ||= BoxCollection.new(self)
129
+ end
130
+
131
+ # Returns the box that this environment represents.
132
+ #
133
+ # @return [Box]
134
+ def box
135
+ boxes.find(config.vm.box)
136
+ end
137
+
138
+ # Returns the VMs associated with this environment.
139
+ #
140
+ # @return [Hash<Symbol,VM>]
141
+ def vms
142
+ return parent.vms if parent
143
+ load! if !loaded?
144
+ @vms ||= load_vms!
145
+ end
146
+
147
+ # Returns the VMs associated with this environment, in the order
148
+ # that they were defined.
149
+ #
150
+ # @return [Array<VM>]
151
+ def vms_ordered
152
+ @vms_enum ||= config.vm.defined_vm_keys.map { |name| @vms[name] }
153
+ end
154
+
155
+ # Returns the primary VM associated with this environment. This
156
+ # method is only applicable for multi-VM environments. This can
157
+ # potentially be nil if no primary VM is specified.
158
+ #
159
+ # @return [VM]
160
+ def primary_vm
161
+ return vms.values.first if !multivm?
162
+ return parent.primary_vm if parent
163
+
164
+ config.vm.defined_vms.each do |name, subvm|
165
+ return vms[name] if subvm.options[:primary]
166
+ end
167
+
168
+ nil
169
+ end
170
+
171
+ # Returns a boolean whether this environment represents a multi-VM
172
+ # environment or not. This will work even when called on child
173
+ # environments.
174
+ #
175
+ # @return [Bool]
176
+ def multivm?
177
+ if parent
178
+ parent.multivm?
179
+ else
180
+ vms.length > 1 || vms.keys.first != DEFAULT_VM
181
+ end
182
+ end
183
+
184
+ # Makes a call to the CLI with the given arguments as if they
185
+ # came from the real command line (sometimes they do!). An example:
186
+ #
187
+ # env.cli("package", "--vagrantfile", "Vagrantfile")
188
+ #
189
+ def cli(*args)
190
+ CLI.start(args.flatten, :env => self)
191
+ end
192
+
193
+ # Returns the {UI} for the environment, which is responsible
194
+ # for talking with the outside world.
195
+ #
196
+ # @return [UI]
197
+ def ui
198
+ @ui ||= if parent
199
+ result = parent.ui.clone
200
+ result.env = self
201
+ result
202
+ else
203
+ UI.new(self)
204
+ end
205
+ end
206
+
207
+ # Returns the host object associated with this environment.
208
+ #
209
+ # @return [Hosts::Base]
210
+ def host
211
+ @host ||= Hosts::Base.load(self, config.vagrant.host)
212
+ end
213
+
214
+ # Returns the {Action} class for this environment which allows actions
215
+ # to be executed (middleware chains) in the context of this environment.
216
+ #
217
+ # @return [Action]
218
+ def actions
219
+ @actions ||= Action.new(self)
220
+ end
221
+
222
+ # Loads on initial access and reads data from the global data store.
223
+ # The global data store is global to Vagrant everywhere (in every environment),
224
+ # so it can be used to store system-wide information. Note that "system-wide"
225
+ # typically means "for this user" since the location of the global data
226
+ # store is in the home directory.
227
+ #
228
+ # @return [DataStore]
229
+ def global_data
230
+ return parent.global_data if parent
231
+ @global_data ||= DataStore.new(File.expand_path("global_data.json", home_path))
232
+ end
233
+
234
+ # Loads (on initial access) and reads data from the local data
235
+ # store. This file is always at the root path as the file "~/.vagrant"
236
+ # and contains a JSON dump of a hash. See {DataStore} for more
237
+ # information.
238
+ #
239
+ # @return [DataStore]
240
+ def local_data
241
+ return parent.local_data if parent
242
+ @local_data ||= DataStore.new(File.expand_path("local_data.json", home_path))
243
+ end
244
+
245
+ # Accesses the logger for Vagrant. This logger is a _detailed_
246
+ # logger which should be used to log internals only. For outward
247
+ # facing information, use {#ui}.
248
+ #
249
+ # @return [Logger]
250
+ def logger
251
+ return parent.logger if parent
252
+ @logger ||= Util::ResourceLogger.new(resource, self)
253
+ end
254
+
255
+ # The root path is the path where the top-most (loaded last)
256
+ # Vagrantfile resides. It can be considered the project root for
257
+ # this environment.
258
+ #
259
+ # @return [String]
260
+ def root_path
261
+ return @root_path if defined?(@root_path)
262
+
263
+ root_finder = lambda do |path|
264
+ return path if File.exist?(File.join(path.to_s, ROOTFILE_NAME))
265
+ return nil if path.root? || !File.exist?(path)
266
+ root_finder.call(path.parent)
267
+ end
268
+
269
+ @root_path = root_finder.call(cwd)
270
+ end
271
+
272
+ #---------------------------------------------------------------
273
+ # Config Methods
274
+ #---------------------------------------------------------------
275
+
276
+ # The configuration object represented by this environment. This
277
+ # will trigger the environment to load if it hasn't loaded yet (see
278
+ # {#load!}).
279
+ #
280
+ # @return [Config::Top]
281
+ def config
282
+ load! if !loaded?
283
+ @config
284
+ end
285
+
286
+ #---------------------------------------------------------------
287
+ # Load Methods
288
+ #---------------------------------------------------------------
289
+
290
+ # Returns a boolean representing if the environment has been
291
+ # loaded or not.
292
+ #
293
+ # @return [Bool]
294
+ def loaded?
295
+ !!@loaded
296
+ end
297
+
298
+ # Loads this entire environment, setting up the instance variables
299
+ # such as `vm`, `config`, etc. on this environment. The order this
300
+ # method calls its other methods is very particular.
301
+ def load!
302
+ if !loaded?
303
+ @loaded = true
304
+ self.class.check_virtualbox!
305
+ load_config!
306
+ end
307
+
308
+ self
309
+ end
310
+
311
+ # Reloads the configuration of this environment.
312
+ def reload_config!
313
+ @config = nil
314
+ @config_loader = nil
315
+ load_config!
316
+ self
317
+ end
318
+
319
+ # Loads this environment's configuration and stores it in the {#config}
320
+ # variable. The configuration loaded by this method is specified to
321
+ # this environment, meaning that it will use the given root directory
322
+ # to load the Vagrantfile into that context.
323
+ def load_config!
324
+ first_run = @config.nil?
325
+
326
+ # First load the initial, non config-dependent Vagrantfiles
327
+ @config_loader ||= Config.new(parent ? parent.config_loader : nil)
328
+ @config_loader.load_order = [:default, :box, :home, :root, :sub_vm]
329
+ @config_loader.set(:default, File.expand_path("config/default.rb", Vagrant.source_root))
330
+ @config_loader.set(:box, File.join(box.directory, ROOTFILE_NAME)) if !first_run && vm && box
331
+ @config_loader.set(:home, File.join(home_path, ROOTFILE_NAME)) if !first_run && home_path
332
+ @config_loader.set(:root, File.join(root_path, ROOTFILE_NAME)) if root_path
333
+
334
+ # If this environment is representing a sub-VM, then we push that
335
+ # proc on as the last configuration.
336
+ if vm
337
+ subvm = parent.config.vm.defined_vms[vm.name]
338
+ @config_loader.set(:sub_vm, subvm.proc_stack) if subvm
339
+ end
340
+
341
+ # Execute the configuration stack and store the result as the final
342
+ # value in the config ivar.
343
+ @config = @config_loader.load(self)
344
+
345
+ # (re)load the logger
346
+ @logger = nil
347
+
348
+ if first_run
349
+ # After the first run we want to load the configuration again since
350
+ # it can change due to box Vagrantfiles and home directory Vagrantfiles
351
+ load_home_directory!
352
+ load_config!
353
+ end
354
+ end
355
+
356
+ # Loads the home directory path and creates the necessary subdirectories
357
+ # within the home directory if they're not already created.
358
+ def load_home_directory!
359
+ # Setup the array of necessary home directories
360
+ dirs = [home_path]
361
+ dirs += HOME_SUBDIRS.collect { |subdir| home_path.join(subdir) }
362
+
363
+ # Go through each required directory, creating it if it doesn't exist
364
+ dirs.each do |dir|
365
+ next if File.directory?(dir)
366
+
367
+ ui.info I18n.t("vagrant.general.creating_home_dir", :directory => dir)
368
+ FileUtils.mkdir_p(dir)
369
+ end
370
+ end
371
+
372
+ # Loads the persisted VM (if it exists) for this environment.
373
+ def load_vms!
374
+ result = {}
375
+
376
+ # Load the VM UUIDs from the local data store
377
+ (local_data[:active] || {}).each do |name, uuid|
378
+ result[name.to_sym] = Vagrant::VM.find(uuid, self, name.to_sym)
379
+ end
380
+
381
+ # For any VMs which aren't created, create a blank VM instance for
382
+ # them
383
+ all_keys = config.vm.defined_vm_keys
384
+ all_keys = [DEFAULT_VM] if all_keys.empty?
385
+ all_keys.each do |name|
386
+ result[name] = Vagrant::VM.new(:name => name, :env => self) if !result.has_key?(name)
387
+ end
388
+
389
+ result
390
+ end
391
+ end
392
+ end