Class: Vagrant::ActiveList

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/active_list.rb

Overview

This class represents the active list of vagrant virtual machines.

Constant Summary collapse

FILENAME =
"active.json"
@@list =
nil

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env = nil) ⇒ ActiveList

Creates the instance of the ActiveList, with the given environment if specified



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

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

Instance Attribute Details

#envObject

The environment this active list belongs to



10
11
12
# File 'lib/vagrant/active_list.rb', line 10

def env
  @env
end

Instance Method Details

#add(vm) ⇒ Object

Adds a virtual environment to the list of active virtual machines



47
48
49
50
51
# File 'lib/vagrant/active_list.rb', line 47

def add(vm)
  list << vm.uuid
  list.uniq!
  save
end

#filtered_listObject

Returns an array of UUIDs filtered so each is verified to exist.



42
43
44
# File 'lib/vagrant/active_list.rb', line 42

def filtered_list
  vms.collect { |vm| vm.uuid }
end

#list(reload = false) ⇒ Array<String>

Parses and returns the list of UUIDs from the active VM JSON file. This will cache the result, which can be reloaded by setting the ‘reload` parameter to true.

Returns:

  • (Array<String>)


23
24
25
26
27
28
29
30
31
32
33
# File 'lib/vagrant/active_list.rb', line 23

def list(reload=false)
  return @list unless @list.nil? || reload

  @list ||= []
  return @list unless File.file?(path)
  File.open(path, "r") do |f|
    @list = JSON.parse(f.read)
  end

  @list
end

#pathObject

Returns the path to the JSON file which holds the UUIDs of the active virtual machines managed by Vagrant.



69
70
71
# File 'lib/vagrant/active_list.rb', line 69

def path
  File.join(env.home_path, FILENAME)
end

#remove(vm) ⇒ Object

Remove a virtual environment from the list of active virtual machines



54
55
56
57
58
# File 'lib/vagrant/active_list.rb', line 54

def remove(vm)
  vm = vm.uuid if vm.is_a?(Vagrant::VM)
  list.delete(vm)
  save
end

#saveObject

Persists the list down to the JSON file.



61
62
63
64
65
# File 'lib/vagrant/active_list.rb', line 61

def save
  File.open(path, "w+") do |f|
    f.write(filtered_list.to_json)
  end
end

#vmsObject

Returns an array of VM objects which are currently active.



37
38
39
# File 'lib/vagrant/active_list.rb', line 37

def vms
  list.collect { |uuid| Vagrant::VM.find(uuid) }.compact
end