Class: NetVbox::VmSetManager

Inherits:
Object
  • Object
show all
Defined in:
lib/netvbox/vm_set_manager.rb

Constant Summary collapse

DEFAULT_VM_SET_NAME =
'default'
VM_SETS_FOLDER_NAME =
'vm_sets'
CURRENT_VM_SET_FILENAME =
'current_vm_set'
VM_SET_EXTENSION =
'vmset'

Instance Method Summary collapse

Constructor Details

#initialize(netvbox_home) ⇒ VmSetManager

Returns a new instance of VmSetManager.



12
13
14
15
16
17
18
# File 'lib/netvbox/vm_set_manager.rb', line 12

def initialize(netvbox_home)
  @netvbox_home = netvbox_home
  FileUtils.mkdir_p "#{@netvbox_home}/#{VM_SETS_FOLDER_NAME}"
  FileUtils.touch vm_set_config_file(DEFAULT_VM_SET_NAME)
  current_vm_file = "#{@netvbox_home}/#{CURRENT_VM_SET_FILENAME}"
  IO.write(current_vm_file, DEFAULT_VM_SET_NAME) unless File.exists?(current_vm_file)
end

Instance Method Details

#create_set(vm_set_name) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/netvbox/vm_set_manager.rb', line 52

def create_set(vm_set_name)
  raise "Set: #{vm_set_name} already exists" if exists? vm_set_name
  begin
    FileUtils.touch vm_set_config_file(vm_set_name)
  rescue
    raise "Cannot create set with name: #{vm_set_name}"
  end
end

#current_setObject



35
36
37
# File 'lib/netvbox/vm_set_manager.rb', line 35

def current_set
  VmSet.new(VmSetConfig.new(vm_set_config_file(current_set_name)))
end

#current_set_nameObject



30
31
32
33
# File 'lib/netvbox/vm_set_manager.rb', line 30

def current_set_name
  current_vm_file = "#{@netvbox_home}/#{CURRENT_VM_SET_FILENAME}"
  File.readable?(current_vm_file) ? IO.read(current_vm_file) : DEFAULT_VM_SET_NAME
end

#exists?(vm_set_name) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/netvbox/vm_set_manager.rb', line 20

def exists?(vm_set_name)
  File.exists? vm_set_config_file(vm_set_name)
end

#remove_set(vm_set_name) ⇒ Object



61
62
63
64
65
66
# File 'lib/netvbox/vm_set_manager.rb', line 61

def remove_set(vm_set_name)
  raise "Set: #{vm_set_name} does not exist" unless exists? vm_set_name
  raise "Cannot remove default set" if vm_set_name == DEFAULT_VM_SET_NAME
  use_default_set if current_set_name == vm_set_name
  File.delete vm_set_config_file(vm_set_name)
end

#use_default_setObject



39
40
41
42
# File 'lib/netvbox/vm_set_manager.rb', line 39

def use_default_set
  FileUtils.touch vm_set_config_file(DEFAULT_VM_SET_NAME)
  IO.write("#{@netvbox_home}/#{CURRENT_VM_SET_FILENAME}", DEFAULT_VM_SET_NAME)
end

#use_set(vm_set_name) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/netvbox/vm_set_manager.rb', line 44

def use_set(vm_set_name)
  if File.exists? vm_set_config_file(vm_set_name)
    IO.write("#{@netvbox_home}/#{CURRENT_VM_SET_FILENAME}", vm_set_name)
  else
    raise "VM set #{vm_set_name} does not exist"
  end
end

#vm_set_namesObject



24
25
26
27
28
# File 'lib/netvbox/vm_set_manager.rb', line 24

def vm_set_names
  Dir.entries("#{@netvbox_home}/#{VM_SETS_FOLDER_NAME}")
     .select {|e| e.end_with?(VM_SET_EXTENSION)}
     .collect {|e| e[0...(-VM_SET_EXTENSION.length - 1)]}
end