Class: Vagrant::Environment
- Inherits:
-
Object
- Object
- Vagrant::Environment
- Defined in:
- lib/vagrant/environment.rb
Overview
Represents a single Vagrant environment. A "Vagrant environment" is defined as basically a folder with a "Vagrantfile." This class allows access to the VMs, CLI, etc. all in the scope of this environment.
Constant Summary collapse
- HOME_SUBDIRS =
["tmp", "boxes", "gems"]
- DEFAULT_VM =
:default
- DEFAULT_HOME =
"~/.vagrant.d"
Instance Attribute Summary collapse
-
#boxes_path ⇒ Object
readonly
The directory where boxes are stored.
-
#cwd ⇒ Object
readonly
The
cwd
that this environment represents. -
#default_private_key_path ⇒ Object
readonly
The path to the default private key.
-
#gems_path ⇒ Object
readonly
The path where the plugins are stored (gems).
-
#home_path ⇒ Object
readonly
The directory to the "home" folder that Vagrant will use to store global state.
-
#tmp_path ⇒ Object
readonly
The directory where temporary files for Vagrant go.
-
#ui ⇒ Object
readonly
The UI object to communicate with the outside world.
-
#vagrantfile_name ⇒ Object
readonly
The valid name for a Vagrantfile for this environment.
Instance Method Summary collapse
-
#action_registry ⇒ Registry
Action registry for registering new actions with this environment.
-
#action_runner ⇒ Action::Runner
Action runner for executing actions in the context of this environment.
-
#boxes ⇒ BoxCollection
Returns the collection of boxes for the environment.
-
#cli(*args) ⇒ Object
Makes a call to the CLI with the given arguments as if they came from the real command line (sometimes they do!).
-
#config ⇒ Config::Container
The configuration object represented by this environment.
-
#dotfile_path ⇒ Pathname
The path to the
dotfile
, which contains the persisted UUID of the VM if it exists. -
#global_data ⇒ DataStore
Loads on initial access and reads data from the global data store.
-
#host ⇒ Hosts::Base
Returns the host object associated with this environment.
-
#initialize(opts = nil) ⇒ Environment
constructor
Initializes a new environment with the given options.
-
#load! ⇒ Object
Loads this entire environment, setting up the instance variables such as
vm
,config
, etc. -
#load_config! ⇒ Object
Loads this environment's configuration and stores it in the #config variable.
-
#load_vms! ⇒ Object
Loads the persisted VM (if it exists) for this environment.
-
#loaded? ⇒ Bool
Returns a boolean representing if the environment has been loaded or not.
-
#local_data ⇒ DataStore
Loads (on initial access) and reads data from the local data store.
-
#lock ⇒ Object
This locks Vagrant for the duration of the block passed to this method.
-
#lock_path ⇒ Object
This returns the path which Vagrant uses to determine the location of the file lock.
-
#multivm? ⇒ Bool
Returns a boolean whether this environment represents a multi-VM environment or not.
-
#primary_vm ⇒ VM
Returns the primary VM associated with this environment.
-
#reload! ⇒ Object
Reloads the configuration of this environment.
-
#root_path ⇒ String
The root path is the path where the top-most (loaded last) Vagrantfile resides.
-
#setup_home_path ⇒ Pathname
This sets the
@home_path
variable properly. -
#vms ⇒ Hash<Symbol,VM>
Returns the VMs associated with this environment.
-
#vms_ordered ⇒ Array<VM>
Returns the VMs associated with this environment, in the order that they were defined.
Constructor Details
#initialize(opts = nil) ⇒ Environment
Initializes a new environment with the given options. The options
is a hash where the main available key is cwd
, which defines where
the environment represents. There are other options available but
they shouldn't be used in general. If cwd
is nil, then it defaults
to the Dir.pwd
(which is the cwd of the executing process).
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/vagrant/environment.rb', line 48 def initialize(opts=nil) opts = { :cwd => nil, :vagrantfile_name => nil, :lock_path => nil, :ui_class => nil, :home_path => nil }.merge(opts || {}) # Set the default working directory to look for the vagrantfile opts[:cwd] ||= Dir.pwd opts[:cwd] = Pathname.new(opts[:cwd]) # Set the default vagrantfile name, which can be either Vagrantfile # or vagrantfile (capital for backwards compatibility) opts[:vagrantfile_name] ||= ["Vagrantfile", "vagrantfile"] opts[:vagrantfile_name] = [opts[:vagrantfile_name]] if !opts[:vagrantfile_name].is_a?(Array) # Set instance variables for all the configuration parameters. @cwd = opts[:cwd] @vagrantfile_name = opts[:vagrantfile_name] @lock_path = opts[:lock_path] @home_path = opts[:home_path] ui_class = opts[:ui_class] || UI::Silent @ui = ui_class.new("vagrant") @loaded = false @lock_acquired = false @logger = Log4r::Logger.new("vagrant::environment") @logger.info("Environment initialized (#{self})") @logger.info(" - cwd: #{cwd}") # Setup the home directory setup_home_path @tmp_path = @home_path.join("tmp") @boxes_path = @home_path.join("boxes") @gems_path = @home_path.join("gems") # Setup the default private key @default_private_key_path = @home_path.join("insecure_private_key") copy_insecure_private_key # Load the plugins load_plugins end |
Instance Attribute Details
#boxes_path ⇒ Object (readonly)
The directory where boxes are stored.
35 36 37 |
# File 'lib/vagrant/environment.rb', line 35 def boxes_path @boxes_path end |
#cwd ⇒ Object (readonly)
The cwd
that this environment represents
19 20 21 |
# File 'lib/vagrant/environment.rb', line 19 def cwd @cwd end |
#default_private_key_path ⇒ Object (readonly)
The path to the default private key
41 42 43 |
# File 'lib/vagrant/environment.rb', line 41 def default_private_key_path @default_private_key_path end |
#gems_path ⇒ Object (readonly)
The path where the plugins are stored (gems)
38 39 40 |
# File 'lib/vagrant/environment.rb', line 38 def gems_path @gems_path end |
#home_path ⇒ Object (readonly)
The directory to the "home" folder that Vagrant will use to store global state.
29 30 31 |
# File 'lib/vagrant/environment.rb', line 29 def home_path @home_path end |
#tmp_path ⇒ Object (readonly)
The directory where temporary files for Vagrant go.
32 33 34 |
# File 'lib/vagrant/environment.rb', line 32 def tmp_path @tmp_path end |
#ui ⇒ Object (readonly)
The UI object to communicate with the outside world.
25 26 27 |
# File 'lib/vagrant/environment.rb', line 25 def ui @ui end |
#vagrantfile_name ⇒ Object (readonly)
The valid name for a Vagrantfile for this environment.
22 23 24 |
# File 'lib/vagrant/environment.rb', line 22 def vagrantfile_name @vagrantfile_name end |
Instance Method Details
#action_registry ⇒ Registry
Action registry for registering new actions with this environment.
206 207 208 209 210 211 |
# File 'lib/vagrant/environment.rb', line 206 def action_registry # For now we return the global built-in actions registry. In the future # we may want to create an isolated registry that inherits from this # global one, but for now there isn't a use case that calls for it. Vagrant.actions end |
#action_runner ⇒ Action::Runner
Action runner for executing actions in the context of this environment.
189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/vagrant/environment.rb', line 189 def action_runner @action_runner ||= Action::Runner.new(action_registry) do { :action_runner => action_runner, :box_collection => boxes, :global_config => config.global, :host => host, :root_path => root_path, :tmp_path => tmp_path, :ui => @ui } end end |
#boxes ⇒ BoxCollection
Returns the collection of boxes for the environment.
112 113 114 |
# File 'lib/vagrant/environment.rb', line 112 def boxes @_boxes ||= BoxCollection.new(boxes_path, action_runner) end |
#cli(*args) ⇒ Object
Makes a call to the CLI with the given arguments as if they came from the real command line (sometimes they do!). An example:
env.cli("package", "--vagrantfile", "Vagrantfile")
162 163 164 |
# File 'lib/vagrant/environment.rb', line 162 def cli(*args) CLI.new(args.flatten, self).execute end |
#config ⇒ Config::Container
The configuration object represented by this environment. This will trigger the environment to load if it hasn't loaded yet (see #load!).
299 300 301 302 |
# File 'lib/vagrant/environment.rb', line 299 def config load! if !loaded? @config end |
#dotfile_path ⇒ Pathname
The path to the dotfile
, which contains the persisted UUID of
the VM if it exists.
104 105 106 107 |
# File 'lib/vagrant/environment.rb', line 104 def dotfile_path return nil if !root_path root_path.join(config.global.vagrant.dotfile_name) end |
#global_data ⇒ DataStore
Loads on initial access and reads data from the global data store. The global data store is global to Vagrant everywhere (in every environment), so it can be used to store system-wide information. Note that "system-wide" typically means "for this user" since the location of the global data store is in the home directory.
220 221 222 |
# File 'lib/vagrant/environment.rb', line 220 def global_data @global_data ||= DataStore.new(File.("global_data.json", home_path)) end |
#host ⇒ Hosts::Base
Returns the host object associated with this environment.
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/vagrant/environment.rb', line 169 def host return @host if defined?(@host) # Attempt to figure out the host class. Note that the order # matters here, so please don't touch. Specifically: The symbol # check is done after the detect check because the symbol check # will return nil, and we don't want to trigger a detect load. host_klass = config.global.vagrant.host host_klass = Hosts.detect(Vagrant.hosts) if host_klass.nil? || host_klass == :detect host_klass = Vagrant.hosts.get(host_klass) if host_klass.is_a?(Symbol) # If no host class is detected, we use the base class. host_klass ||= Hosts::Base @host ||= host_klass.new(@ui) end |
#load! ⇒ Object
Loads this entire environment, setting up the instance variables
such as vm
, config
, etc. on this environment. The order this
method calls its other methods is very particular.
319 320 321 322 323 324 325 326 327 |
# File 'lib/vagrant/environment.rb', line 319 def load! if !loaded? @loaded = true @logger.info("Loading configuration...") load_config! end self end |
#load_config! ⇒ Object
Loads this environment's configuration and stores it in the #config variable. The configuration loaded by this method is specified to this environment, meaning that it will use the given root directory to load the Vagrantfile into that context.
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
# File 'lib/vagrant/environment.rb', line 342 def load_config! # Initialize the config loader config_loader = Config::Loader.new config_loader.load_order = [:default, :box, :home, :root, :vm] inner_load = lambda do |*args| # This is for Ruby 1.8.7 compatibility. Ruby 1.8.7 doesn't allow # default arguments for lambdas, so we get around by doing a *args # and setting the args here. subvm = args[0] box = args[1] # Default Vagrantfile first. This is the Vagrantfile that ships # with Vagrant. config_loader.set(:default, File.("config/default.rb", Vagrant.source_root)) if box # We load the box Vagrantfile box_vagrantfile = find_vagrantfile(box.directory) config_loader.set(:box, box_vagrantfile) if box_vagrantfile end if home_path # Load the home Vagrantfile home_vagrantfile = find_vagrantfile(home_path) config_loader.set(:home, home_vagrantfile) if home_vagrantfile end if root_path # Load the Vagrantfile in this directory root_vagrantfile = find_vagrantfile(root_path) config_loader.set(:root, root_vagrantfile) if root_vagrantfile end if subvm # We have subvm configuration, so set that up as well. config_loader.set(:vm, subvm.proc_stack) end # Execute the configuration stack and store the result as the final # value in the config ivar. config_loader.load end # For the global configuration, we only need to load the configuration # in a single pass, since nothing is conditional on the configuration. global = inner_load.call # For each virtual machine represented by this environment, we have # to load the configuration in two-passes. We do this because the # first pass is used to determine the box for the VM. The second pass # is used to also load the box Vagrantfile. defined_vm_keys = global.vm.defined_vm_keys.dup defined_vms = global.vm.defined_vms.dup # If this isn't a multi-VM environment, then setup the default VM # to simply be our configuration. if defined_vm_keys.empty? defined_vm_keys << DEFAULT_VM defined_vms[DEFAULT_VM] = Config::VMConfig::SubVM.new end vm_configs = defined_vm_keys.map do |vm_name| @logger.debug("Loading configuration for VM: #{vm_name}") subvm = defined_vms[vm_name] # First pass, first run. config = inner_load[subvm] # Second pass, with the box config = inner_load[subvm, boxes.find(config.vm.box)] config.vm.name = vm_name # Return the final configuration for this VM config end # Finally, we have our configuration. Set it and forget it. @config = Config::Container.new(global, vm_configs) end |
#load_vms! ⇒ Object
Loads the persisted VM (if it exists) for this environment.
425 426 427 428 429 430 431 432 433 434 |
# File 'lib/vagrant/environment.rb', line 425 def load_vms! result = {} # Load all the virtual machine instances. config.vms.each do |name| result[name] = Vagrant::VM.new(name, self, config.for_vm(name)) end result end |
#loaded? ⇒ Bool
Returns a boolean representing if the environment has been loaded or not.
312 313 314 |
# File 'lib/vagrant/environment.rb', line 312 def loaded? !!@loaded end |
#local_data ⇒ DataStore
Loads (on initial access) and reads data from the local data store. This file is always at the root path as the file "~/.vagrant" and contains a JSON dump of a hash. See DataStore for more information.
230 231 232 |
# File 'lib/vagrant/environment.rb', line 230 def local_data @local_data ||= DataStore.new(dotfile_path) end |
#lock ⇒ Object
This locks Vagrant for the duration of the block passed to this method. During this time, any other environment which attempts to lock which points to the same lock file will fail.
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/vagrant/environment.rb', line 266 def lock # This allows multiple locks in the same process to be nested return yield if @lock_acquired File.open(lock_path, "w+") do |f| # The file locking fails only if it returns "false." If it # succeeds it returns a 0, so we must explicitly check for # the proper error case. raise Errors::EnvironmentLockedError if f.flock(File::LOCK_EX | File::LOCK_NB) === false begin # Mark that we have a lock @lock_acquired = true yield ensure # We need to make sure that no matter what this is always # reset to false so we don't think we have a lock when we # actually don't. @lock_acquired = false end end end |
#lock_path ⇒ Object
This returns the path which Vagrant uses to determine the location of the file lock. This is specific to each operating system.
259 260 261 |
# File 'lib/vagrant/environment.rb', line 259 def lock_path @lock_path || tmp_path.join("vagrant.lock") end |
#multivm? ⇒ Bool
Returns a boolean whether this environment represents a multi-VM environment or not. This will work even when called on child environments.
153 154 155 |
# File 'lib/vagrant/environment.rb', line 153 def multivm? vms.length > 1 || vms.keys.first != DEFAULT_VM end |
#primary_vm ⇒ VM
Returns the primary VM associated with this environment. This method is only applicable for multi-VM environments. This can potentially be nil if no primary VM is specified.
138 139 140 141 142 143 144 145 146 |
# File 'lib/vagrant/environment.rb', line 138 def primary_vm return vms.values.first if !multivm? config.global.vm.defined_vms.each do |name, subvm| return vms[name] if subvm.[:primary] end nil end |
#reload! ⇒ Object
Reloads the configuration of this environment.
330 331 332 333 334 335 336 |
# File 'lib/vagrant/environment.rb', line 330 def reload! # Reload the configuration load_config! # Clear the VMs because this can now be diferent due to configuration @vms = nil end |
#root_path ⇒ String
The root path is the path where the top-most (loaded last) Vagrantfile resides. It can be considered the project root for this environment.
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/vagrant/environment.rb', line 239 def root_path return @root_path if defined?(@root_path) root_finder = lambda do |path| # Note: To remain compatible with Ruby 1.8, we have to use # a `find` here instead of an `each`. found = vagrantfile_name.find do |rootfile| File.exist?(File.join(path.to_s, rootfile)) end return path if found return nil if path.root? || !File.exist?(path) root_finder.call(path.parent) end @root_path = root_finder.call(cwd) end |
#setup_home_path ⇒ Pathname
This sets the @home_path
variable properly.
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 |
# File 'lib/vagrant/environment.rb', line 439 def setup_home_path @home_path = Pathname.new(File.(@home_path || ENV["VAGRANT_HOME"] || DEFAULT_HOME)) @logger.info("Home path: #{@home_path}") # Setup the array of necessary home directories dirs = [@home_path] dirs += HOME_SUBDIRS.collect { |subdir| @home_path.join(subdir) } # Go through each required directory, creating it if it doesn't exist dirs.each do |dir| next if File.directory?(dir) begin @logger.info("Creating: #{dir}") FileUtils.mkdir_p(dir) rescue Errno::EACCES raise Errors::HomeDirectoryNotAccessible, :home_path => @home_path.to_s end end end |
#vms ⇒ Hash<Symbol,VM>
Returns the VMs associated with this environment.
119 120 121 122 |
# File 'lib/vagrant/environment.rb', line 119 def vms load! if !loaded? @vms ||= load_vms! end |
#vms_ordered ⇒ Array<VM>
Returns the VMs associated with this environment, in the order that they were defined.
128 129 130 131 |
# File 'lib/vagrant/environment.rb', line 128 def vms_ordered return @vms.values if !multivm? @vms_enum ||= config.global.vm.defined_vm_keys.map { |name| @vms[name] } end |