Class: Vagrant::ActiveList
- Inherits:
-
Object
- Object
- Vagrant::ActiveList
- 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
-
#env ⇒ Object
The environment this active list belongs to.
Instance Method Summary collapse
-
#add(vm) ⇒ Object
Adds a virtual environment to the list of active virtual machines.
-
#filtered_list ⇒ Object
Returns an array of UUIDs filtered so each is verified to exist.
-
#initialize(env = nil) ⇒ ActiveList
constructor
Creates the instance of the ActiveList, with the given environment if specified.
-
#list(reload = false) ⇒ Array<String>
Parses and returns the list of UUIDs from the active VM JSON file.
-
#path ⇒ Object
Returns the path to the JSON file which holds the UUIDs of the active virtual machines managed by Vagrant.
-
#remove(vm) ⇒ Object
Remove a virtual environment from the list of active virtual machines.
-
#save ⇒ Object
Persists the list down to the JSON file.
-
#vms ⇒ Object
Returns an array of VM objects which are currently active.
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
#env ⇒ Object
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_list ⇒ Object
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.
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 |
#path ⇒ Object
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 |
#save ⇒ Object
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 |