Environment documentation
This commit is contained in:
parent
7aded5e214
commit
f6db8e5518
|
@ -2,18 +2,25 @@ require 'pathname'
|
||||||
require 'fileutils'
|
require 'fileutils'
|
||||||
|
|
||||||
module Vagrant
|
module Vagrant
|
||||||
# Represents a single Vagrant environment. This class is responsible
|
# Represents a single Vagrant environment. A "Vagrant environment" is
|
||||||
# for loading all of the Vagrantfile's for the given environment and
|
# defined as basically a folder with a "Vagrantfile." This class allows
|
||||||
# storing references to the various instances.
|
# access to the VMs, CLI, etc. all in the scope of this environment.
|
||||||
class Environment
|
class Environment
|
||||||
ROOTFILE_NAME = "Vagrantfile"
|
ROOTFILE_NAME = "Vagrantfile"
|
||||||
HOME_SUBDIRS = ["tmp", "boxes", "logs"]
|
HOME_SUBDIRS = ["tmp", "boxes", "logs"]
|
||||||
DEFAULT_VM = :default
|
DEFAULT_VM = :default
|
||||||
|
|
||||||
attr_reader :parent # Parent environment (in the case of multi-VMs)
|
# Parent environment (in the case of multi-VMs)
|
||||||
|
attr_reader :parent
|
||||||
|
|
||||||
|
# The `cwd` that this environment represents
|
||||||
attr_reader :cwd
|
attr_reader :cwd
|
||||||
|
|
||||||
|
# The single VM that this environment represents, in the case of
|
||||||
|
# multi-VM.
|
||||||
attr_accessor :vm
|
attr_accessor :vm
|
||||||
|
|
||||||
|
# The {UI} object to communicate with the outside world.
|
||||||
attr_writer :ui
|
attr_writer :ui
|
||||||
|
|
||||||
#---------------------------------------------------------------
|
#---------------------------------------------------------------
|
||||||
|
@ -30,6 +37,11 @@ module Vagrant
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# 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).
|
||||||
def initialize(opts=nil)
|
def initialize(opts=nil)
|
||||||
opts = {
|
opts = {
|
||||||
:parent => nil,
|
:parent => nil,
|
||||||
|
@ -53,27 +65,37 @@ module Vagrant
|
||||||
|
|
||||||
# The path to the `dotfile`, which contains the persisted UUID of
|
# The path to the `dotfile`, which contains the persisted UUID of
|
||||||
# the VM if it exists.
|
# the VM if it exists.
|
||||||
|
#
|
||||||
|
# @return [Pathname]
|
||||||
def dotfile_path
|
def dotfile_path
|
||||||
root_path.join(config.vagrant.dotfile_name) rescue nil
|
root_path.join(config.vagrant.dotfile_name) rescue nil
|
||||||
end
|
end
|
||||||
|
|
||||||
# The path to the home directory, expanded relative to the root path,
|
# The path to the home directory, expanded relative to the root path,
|
||||||
# and converted into a Pathname object.
|
# and converted into a Pathname object.
|
||||||
|
#
|
||||||
|
# @return [Pathname]
|
||||||
def home_path
|
def home_path
|
||||||
@_home_path ||= Pathname.new(File.expand_path(config.vagrant.home, root_path))
|
@_home_path ||= Pathname.new(File.expand_path(config.vagrant.home, root_path))
|
||||||
end
|
end
|
||||||
|
|
||||||
# The path to the Vagrant tmp directory
|
# The path to the Vagrant tmp directory
|
||||||
|
#
|
||||||
|
# @return [Pathname]
|
||||||
def tmp_path
|
def tmp_path
|
||||||
home_path.join("tmp")
|
home_path.join("tmp")
|
||||||
end
|
end
|
||||||
|
|
||||||
# The path to the Vagrant boxes directory
|
# The path to the Vagrant boxes directory
|
||||||
|
#
|
||||||
|
# @return [Pathname]
|
||||||
def boxes_path
|
def boxes_path
|
||||||
home_path.join("boxes")
|
home_path.join("boxes")
|
||||||
end
|
end
|
||||||
|
|
||||||
# Path to the Vagrant logs directory
|
# Path to the Vagrant logs directory
|
||||||
|
#
|
||||||
|
# @return [Pathname]
|
||||||
def log_path
|
def log_path
|
||||||
home_path.join("logs")
|
home_path.join("logs")
|
||||||
end
|
end
|
||||||
|
@ -81,29 +103,41 @@ module Vagrant
|
||||||
# Returns the name of the resource which this environment represents.
|
# Returns the name of the resource which this environment represents.
|
||||||
# The resource is the VM name if there is a VM it represents, otherwise
|
# The resource is the VM name if there is a VM it represents, otherwise
|
||||||
# it defaults to "vagrant"
|
# it defaults to "vagrant"
|
||||||
|
#
|
||||||
|
# @return [String]
|
||||||
def resource
|
def resource
|
||||||
vm.name rescue "vagrant"
|
vm.name rescue "vagrant"
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns the collection of boxes for the environment.
|
# Returns the collection of boxes for the environment.
|
||||||
|
#
|
||||||
|
# @return [BoxCollection]
|
||||||
def boxes
|
def boxes
|
||||||
return parent.boxes if parent
|
return parent.boxes if parent
|
||||||
@_boxes ||= BoxCollection.new(self)
|
@_boxes ||= BoxCollection.new(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns the box that this environment represents.
|
# Returns the box that this environment represents.
|
||||||
|
#
|
||||||
|
# @return [Box]
|
||||||
def box
|
def box
|
||||||
boxes.find(config.vm.box)
|
boxes.find(config.vm.box)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns the VMs associated with this environment.
|
# Returns the VMs associated with this environment.
|
||||||
|
#
|
||||||
|
# @return [Array<VM>]
|
||||||
def vms
|
def vms
|
||||||
return parent.vms if parent
|
return parent.vms if parent
|
||||||
load! if !loaded?
|
load! if !loaded?
|
||||||
@vms ||= load_vms!
|
@vms ||= load_vms!
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns the primray VM associated with this environment
|
# 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.
|
||||||
|
#
|
||||||
|
# @return [VM]
|
||||||
def primary_vm
|
def primary_vm
|
||||||
return vms.values.first if !multivm?
|
return vms.values.first if !multivm?
|
||||||
return parent.primary_vm if parent
|
return parent.primary_vm if parent
|
||||||
|
@ -118,6 +152,8 @@ module Vagrant
|
||||||
# Returns a boolean whether this environment represents a multi-VM
|
# Returns a boolean whether this environment represents a multi-VM
|
||||||
# environment or not. This will work even when called on child
|
# environment or not. This will work even when called on child
|
||||||
# environments.
|
# environments.
|
||||||
|
#
|
||||||
|
# @return [Bool]
|
||||||
def multivm?
|
def multivm?
|
||||||
if parent
|
if parent
|
||||||
parent.multivm?
|
parent.multivm?
|
||||||
|
@ -127,13 +163,18 @@ module Vagrant
|
||||||
end
|
end
|
||||||
|
|
||||||
# Makes a call to the CLI with the given arguments as if they
|
# Makes a call to the CLI with the given arguments as if they
|
||||||
# came from the real command line (sometimes they do!)
|
# came from the real command line (sometimes they do!). An example:
|
||||||
|
#
|
||||||
|
# env.cli("package", "--vagrantfile", "Vagrantfile")
|
||||||
|
#
|
||||||
def cli(*args)
|
def cli(*args)
|
||||||
CLI.start(args.flatten, :env => self)
|
CLI.start(args.flatten, :env => self)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns the {UI} for the environment, which is responsible
|
# Returns the {UI} for the environment, which is responsible
|
||||||
# for talking with the outside world.
|
# for talking with the outside world.
|
||||||
|
#
|
||||||
|
# @return [UI]
|
||||||
def ui
|
def ui
|
||||||
@ui ||= if parent
|
@ui ||= if parent
|
||||||
result = parent.ui.clone
|
result = parent.ui.clone
|
||||||
|
@ -145,12 +186,16 @@ module Vagrant
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns the host object associated with this environment.
|
# Returns the host object associated with this environment.
|
||||||
|
#
|
||||||
|
# @return [Hosts::Base]
|
||||||
def host
|
def host
|
||||||
@host ||= Hosts::Base.load(self, config.vagrant.host)
|
@host ||= Hosts::Base.load(self, config.vagrant.host)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns the {Action} class for this environment which allows actions
|
# Returns the {Action} class for this environment which allows actions
|
||||||
# to be executed (middleware chains) in the context of this environment.
|
# to be executed (middleware chains) in the context of this environment.
|
||||||
|
#
|
||||||
|
# @return [Action]
|
||||||
def actions
|
def actions
|
||||||
@actions ||= Action.new(self)
|
@actions ||= Action.new(self)
|
||||||
end
|
end
|
||||||
|
@ -160,6 +205,8 @@ module Vagrant
|
||||||
# so it can be used to store system-wide information. Note that "system-wide"
|
# 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
|
# typically means "for this user" since the location of the global data
|
||||||
# store is in the home directory.
|
# store is in the home directory.
|
||||||
|
#
|
||||||
|
# @return [DataStore]
|
||||||
def global_data
|
def global_data
|
||||||
return parent.global_data if parent
|
return parent.global_data if parent
|
||||||
@global_data ||= DataStore.new(File.expand_path("global_data.json", home_path))
|
@global_data ||= DataStore.new(File.expand_path("global_data.json", home_path))
|
||||||
|
@ -169,6 +216,8 @@ module Vagrant
|
||||||
# store. This file is always at the root path as the file "~/.vagrant"
|
# 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
|
# and contains a JSON dump of a hash. See {DataStore} for more
|
||||||
# information.
|
# information.
|
||||||
|
#
|
||||||
|
# @return [DataStore]
|
||||||
def local_data
|
def local_data
|
||||||
return parent.local_data if parent
|
return parent.local_data if parent
|
||||||
@local_data ||= DataStore.new(dotfile_path)
|
@local_data ||= DataStore.new(dotfile_path)
|
||||||
|
@ -177,6 +226,8 @@ module Vagrant
|
||||||
# Accesses the logger for Vagrant. This logger is a _detailed_
|
# Accesses the logger for Vagrant. This logger is a _detailed_
|
||||||
# logger which should be used to log internals only. For outward
|
# logger which should be used to log internals only. For outward
|
||||||
# facing information, use {#ui}.
|
# facing information, use {#ui}.
|
||||||
|
#
|
||||||
|
# @return [Logger]
|
||||||
def logger
|
def logger
|
||||||
return parent.logger if parent
|
return parent.logger if parent
|
||||||
@logger ||= Util::ResourceLogger.new(resource, self)
|
@logger ||= Util::ResourceLogger.new(resource, self)
|
||||||
|
@ -185,6 +236,8 @@ module Vagrant
|
||||||
# The root path is the path where the top-most (loaded last)
|
# The root path is the path where the top-most (loaded last)
|
||||||
# Vagrantfile resides. It can be considered the project root for
|
# Vagrantfile resides. It can be considered the project root for
|
||||||
# this environment.
|
# this environment.
|
||||||
|
#
|
||||||
|
# @return [String]
|
||||||
def root_path
|
def root_path
|
||||||
return @root_path if defined?(@root_path)
|
return @root_path if defined?(@root_path)
|
||||||
|
|
||||||
|
@ -204,6 +257,8 @@ module Vagrant
|
||||||
# The configuration object represented by this environment. This
|
# The configuration object represented by this environment. This
|
||||||
# will trigger the environment to load if it hasn't loaded yet (see
|
# will trigger the environment to load if it hasn't loaded yet (see
|
||||||
# {#load!}).
|
# {#load!}).
|
||||||
|
#
|
||||||
|
# @return [Config::Top]
|
||||||
def config
|
def config
|
||||||
load! if !loaded?
|
load! if !loaded?
|
||||||
@config
|
@config
|
||||||
|
@ -215,6 +270,8 @@ module Vagrant
|
||||||
|
|
||||||
# Returns a boolean representing if the environment has been
|
# Returns a boolean representing if the environment has been
|
||||||
# loaded or not.
|
# loaded or not.
|
||||||
|
#
|
||||||
|
# @return [Bool]
|
||||||
def loaded?
|
def loaded?
|
||||||
!!@loaded
|
!!@loaded
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue