Class: Vagrant::Environment

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/vagrant/environment.rb

Overview

Represents a single Vagrant environment. This class is responsible for loading all of the Vagrantfile’s for the given environment and storing references to the various instances.

Constant Summary collapse

ROOTFILE_NAME =
"Vagrantfile"
HOME_SUBDIRS =
["tmp", "boxes"]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#error_and_exit, included, #logger, #wrap_output

Constructor Details

#initialize(cwd = nil) ⇒ Environment

Returns a new instance of Environment.



48
49
50
# File 'lib/vagrant/environment.rb', line 48

def initialize(cwd=nil)
  @cwd = cwd
end

Instance Attribute Details

#active_listObject (readonly)

Returns the value of attribute active_list.



17
18
19
# File 'lib/vagrant/environment.rb', line 17

def active_list
  @active_list
end

#boxObject (readonly)

Returns the value of attribute box.



14
15
16
# File 'lib/vagrant/environment.rb', line 14

def box
  @box
end

#commandsObject (readonly)

Returns the value of attribute commands.



18
19
20
# File 'lib/vagrant/environment.rb', line 18

def commands
  @commands
end

#configObject (readonly)

Returns the value of attribute config.



13
14
15
# File 'lib/vagrant/environment.rb', line 13

def config
  @config
end

#cwdObject

Specifies the “current working directory” for this environment. This is vital in determining the root path and therefore the dotfile, rootpath vagrantfile, etc. This defaults to the actual cwd (‘Dir.pwd`).



60
61
62
# File 'lib/vagrant/environment.rb', line 60

def cwd
  @cwd
end

#root_pathObject (readonly)

Returns the value of attribute root_path.



12
13
14
# File 'lib/vagrant/environment.rb', line 12

def root_path
  @root_path
end

#sshObject (readonly)

Returns the value of attribute ssh.



16
17
18
# File 'lib/vagrant/environment.rb', line 16

def ssh
  @ssh
end

#vmObject (readonly)

Returns the value of attribute vm.



15
16
17
# File 'lib/vagrant/environment.rb', line 15

def vm
  @vm
end

Class Method Details

.check_virtualbox!Object

Verifies that VirtualBox is installed and that the version of VirtualBox installed is high enough. Also verifies that the configuration path is properly set.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/vagrant/environment.rb', line 34

def check_virtualbox!
  version = VirtualBox::Command.version
  if version.nil?
    error_and_exit(:virtualbox_not_detected)
  elsif version.to_f < 3.1
    error_and_exit(:virtualbox_invalid_version, :version => version.to_s)
  end

  if !VirtualBox::Global.vboxconfig?
    error_and_exit(:virtualbox_xml_not_detected)
  end
end

.load!(cwd = nil) ⇒ Object

Loads and returns an environment given a specific working directory. If a working directory is not given, it will default to the pwd.



27
28
29
# File 'lib/vagrant/environment.rb', line 27

def load!(cwd=nil)
  Environment.new(cwd).load!
end

Instance Method Details

#boxes_pathObject

The path to the Vagrant boxes directory



81
82
83
# File 'lib/vagrant/environment.rb', line 81

def boxes_path
  File.join(home_path, "boxes")
end

#create_vmObject

Sets the VM to a new VM. This is not too useful but is used in Command.up. This will very likely be refactored at a later time.



210
211
212
213
214
# File 'lib/vagrant/environment.rb', line 210

def create_vm
  @vm = VM.new
  @vm.env = self
  @vm
end

#depersist_vmObject

Removes this environment’s VM from the dotfile.



229
230
231
232
233
234
235
# File 'lib/vagrant/environment.rb', line 229

def depersist_vm
  # Delete the dotfile if it exists
  File.delete(dotfile_path) if File.exist?(dotfile_path)

  # Remove from the global store
  active_list.remove(vm)
end

#dotfile_pathObject

The path to the ‘dotfile`, which contains the persisted UUID of the VM if it exists.



66
67
68
# File 'lib/vagrant/environment.rb', line 66

def dotfile_path
  File.join(root_path, config.vagrant.dotfile_name)
end

#home_pathObject

The path to the home directory, which is usually in ‘~/.vagrant/~



71
72
73
# File 'lib/vagrant/environment.rb', line 71

def home_path
  config ? config.vagrant.home : nil
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.



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/vagrant/environment.rb', line 92

def load!
  load_root_path!
  load_config!
  load_home_directory!
  load_box!
  load_config!
  self.class.check_virtualbox!
  load_vm!
  load_ssh!
  load_active_list!
  load_commands!
  self
end

#load_active_list!Object

Loads the activelist for this environment



192
193
194
# File 'lib/vagrant/environment.rb', line 192

def load_active_list!
  @active_list = ActiveList.new(self)
end

#load_box!Object

Loads the specified box for this environment.



168
169
170
171
172
# File 'lib/vagrant/environment.rb', line 168

def load_box!
  return unless root_path

  @box = Box.find(self, config.vm.box) if config.vm.box
end

#load_commands!Object

Loads the instance of Commands for this environment. This allows users of the instance to run commands such as “up” “down” etc. in the context of this environment.



199
200
201
# File 'lib/vagrant/environment.rb', line 199

def load_commands!
  @commands = Commands.new(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.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/vagrant/environment.rb', line 129

def load_config!
  # Prepare load paths for config files
  load_paths = [File.join(PROJECT_ROOT, "config", "default.rb")]
  load_paths << File.join(box.directory, ROOTFILE_NAME) if box
  load_paths << File.join(home_path, ROOTFILE_NAME) if home_path
  load_paths << File.join(root_path, ROOTFILE_NAME) if root_path

  # Clear out the old data
  Config.reset!(self)

  # Load each of the config files in order
  load_paths.each do |path|
    if File.exist?(path)
      logger.info "Loading config from #{path}..."
      load path
    end
  end

  # Execute the configuration stack and store the result
  @config = Config.execute!
end

#load_home_directory!Object

Loads the home directory path and creates the necessary subdirectories within the home directory if they’re not already created.



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/vagrant/environment.rb', line 153

def load_home_directory!
  # Setup the array of necessary home directories
  dirs = HOME_SUBDIRS.collect { |subdir| File.join(home_path, subdir) }
  dirs.unshift(home_path)

  # Go through each required directory, creating it if it doesn't exist
  dirs.each do |dir|
    next if File.directory?(dir)

    logger.info "Creating home directory since it doesn't exist: #{dir}"
    FileUtils.mkdir_p(dir)
  end
end

#load_root_path!(path = nil) ⇒ Object

Loads the root path of this environment, given the starting directory (the “cwd” of this environment for lack of better words). This method allows an environment in ‘/foo` to be detected from `/foo/bar` (similar to how git works in subdirectories)



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/vagrant/environment.rb', line 110

def load_root_path!(path=nil)
  path = Pathname.new(File.expand_path(path || cwd))

  # Stop if we're at the root.
  return false if path.root?

  file = "#{path}/#{ROOTFILE_NAME}"
  if File.exist?(file)
    @root_path = path.to_s
    return true
  end

  load_root_path!(path.parent)
end

#load_ssh!Object

Loads/initializes the SSH object



187
188
189
# File 'lib/vagrant/environment.rb', line 187

def load_ssh!
  @ssh = SSH.new(self)
end

#load_vm!Object

Loads the persisted VM (if it exists) for this environment.



175
176
177
178
179
180
181
182
183
184
# File 'lib/vagrant/environment.rb', line 175

def load_vm!
  return if !root_path || !File.file?(dotfile_path)

  File.open(dotfile_path) do |f|
    @vm = Vagrant::VM.find(f.read)
    @vm.env = self if @vm
  end
rescue Errno::ENOENT
  @vm = nil
end

#persist_vmObject

Persists this environment’s VM to the dotfile so it can be re-loaded at a later time.



218
219
220
221
222
223
224
225
226
# File 'lib/vagrant/environment.rb', line 218

def persist_vm
  # Save to the dotfile for this project
  File.open(dotfile_path, 'w+') do |f|
    f.write(vm.uuid)
  end

  # Also add to the global store
  active_list.add(vm)
end

#require_boxObject



245
246
247
248
249
250
251
252
253
254
255
# File 'lib/vagrant/environment.rb', line 245

def require_box
  require_root_path

  if !box
    if !Vagrant.config.vm.box
      error_and_exit(:box_not_specified)
    else
      error_and_exit(:box_specified_doesnt_exist, :box_name => Vagrant.config.vm.box)
    end
  end
end

#require_persisted_vmObject



257
258
259
260
261
# File 'lib/vagrant/environment.rb', line 257

def require_persisted_vm
  require_root_path

  error_and_exit(:environment_not_created) if !vm
end

#require_root_pathObject


Methods to check for properties and error




241
242
243
# File 'lib/vagrant/environment.rb', line 241

def require_root_path
  error_and_exit(:rootfile_not_found) if !root_path
end

#tmp_pathObject

The path to the Vagrant tmp directory



76
77
78
# File 'lib/vagrant/environment.rb', line 76

def tmp_path
  File.join(home_path, "tmp")
end