Class: Vmit::Workspace
- Inherits:
-
Object
- Object
- Vmit::Workspace
- Defined in:
- lib/vmit/workspace.rb
Constant Summary collapse
- VM_GLOBAL_DEFAULTS =
{ :memory => '1G', :kernel_cmdline => [], :virtio => true }
- SWITCH =
'br0'- DISK_INIT_DEFAULTS =
{:disk_size => '10G'}
- BINDIR =
File.join(File.dirname(__FILE__), '../../bin')
Instance Attribute Summary collapse
-
#work_dir ⇒ Object
Returns the value of attribute work_dir.
Class Method Summary collapse
Instance Method Summary collapse
-
#[](key) ⇒ Object
Accessor to current options.
-
#config ⇒ Hash
Config of the virtual machine This is all options plus the defaults.
- #config_file ⇒ Object
- #current_image ⇒ Object
- #disk_image_init!(opts = {}) ⇒ Object
-
#disk_image_shift!(opts = {}) ⇒ Object
Shifts an image, adding a new one using the previous newest one as backing file.
-
#disk_images ⇒ Array, <String>
Sorted list of snapshots.
-
#disk_rollback! ⇒ Object
Rolls back to the previous snapshot.
-
#disk_snapshot! ⇒ Object
Takes a disk snapshot.
-
#initialize(work_dir) ⇒ Workspace
constructor
A new instance of Workspace.
- #name ⇒ Object
- #options ⇒ Object
-
#relevant_config ⇒ Hash
Config that differs from default and therefore relevant to be persisted in config.yml.
-
#save_config! ⇒ Object
Saves the configuration in config.yml.
- #to_s ⇒ Object
Constructor Details
#initialize(work_dir) ⇒ Workspace
Returns a new instance of Workspace.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/vmit/workspace.rb', line 63 def initialize(work_dir) @config = Confstruct::Configuration.new(VM_GLOBAL_DEFAULTS.merge({ :uuid => File.read("/proc/sys/kernel/random/uuid").strip, :mac_address => Vmit::Utils.random_mac_address })) @work_dir = work_dir if File.exist?(config_file) @config.configure(YAML::load(File.open(config_file))) end @network = config.lookup!('network', 'default') Vmit.logger.info "Network: #{@network}" end |
Instance Attribute Details
#work_dir ⇒ Object
Returns the value of attribute work_dir.
37 38 39 |
# File 'lib/vmit/workspace.rb', line 37 def work_dir @work_dir end |
Class Method Details
.from_pwd ⇒ Object
46 47 48 |
# File 'lib/vmit/workspace.rb', line 46 def self.from_pwd Vmit::Workspace.new(File.(Dir.pwd)) end |
Instance Method Details
#[](key) ⇒ Object
Accessor to current options
55 56 57 |
# File 'lib/vmit/workspace.rb', line 55 def [](key) @config[key] end |
#config ⇒ Hash
Returns Config of the virtual machine This is all options plus the defaults.
153 154 155 |
# File 'lib/vmit/workspace.rb', line 153 def config @config end |
#config_file ⇒ Object
59 60 61 |
# File 'lib/vmit/workspace.rb', line 59 def config_file File.join(work_dir, 'config.yml') end |
#current_image ⇒ Object
141 142 143 144 145 |
# File 'lib/vmit/workspace.rb', line 141 def current_image curr = disk_images.last raise "No hard disk image available" if curr.nil? curr end |
#disk_image_init!(opts = {}) ⇒ Object
90 91 92 |
# File 'lib/vmit/workspace.rb', line 90 def disk_image_init!(opts={}) disk_image_shift!(opts) end |
#disk_image_shift!(opts = {}) ⇒ Object
Shifts an image, adding a new one using the previous newest one as backing file
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/vmit/workspace.rb', line 101 def disk_image_shift!(opts={}) disk_config = Confstruct::Configuration.new(DISK_INIT_DEFAULTS) file_name = File.join(work_dir, "sda-#{Time.now.to_i}.qcow2") images = disk_images file_name = File.join(work_dir, 'base.qcow2') if images.size == 0 args = ['/usr/bin/qemu-img', 'create', '-f', "qcow2"] if not images.empty? args << '-b' args << images.last end args << file_name if images.empty? args << disk_config.disk_size end Vmit.logger.info "Shifted image. Current is '#{file_name}'." Cheetah.run(*args) end |
#disk_images ⇒ Array, <String>
Returns sorted list of snapshots.
79 80 81 82 83 |
# File 'lib/vmit/workspace.rb', line 79 def disk_images Dir.glob(File.join(work_dir, '*.qcow2')).sort do |a,b| File.ctime(a) <=> File.ctime(b) end end |
#disk_rollback! ⇒ Object
Rolls back to the previous snapshot
127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/vmit/workspace.rb', line 127 def disk_rollback! images = disk_images return if images.empty? if images.size == 1 Vmit.logger.fatal "Only the base snapshot left!" return end Vmit.logger.info "Removing #{images.last}" FileUtils.rm(images.last) end |
#disk_snapshot! ⇒ Object
Takes a disk snapshot
86 87 88 |
# File 'lib/vmit/workspace.rb', line 86 def disk_snapshot! disk_image_shift! end |
#name ⇒ Object
50 51 52 |
# File 'lib/vmit/workspace.rb', line 50 def name work_dir.downcase.gsub(/[^a-z\s]/, '_') end |
#options ⇒ Object
147 148 149 |
# File 'lib/vmit/workspace.rb', line 147 def raise 'Workspace#options is deprecated.' end |
#relevant_config ⇒ Hash
Returns config that differs from default and therefore relevant to be persisted in config.yml.
159 160 161 |
# File 'lib/vmit/workspace.rb', line 159 def relevant_config config.diff(config.default_values) end |
#save_config! ⇒ Object
Saves the configuration in config.yml
164 165 166 167 168 169 170 171 |
# File 'lib/vmit/workspace.rb', line 164 def save_config! if not relevant_config.empty? Vmit.logger.info "Writing config.yml..." File.open(config_file, 'w') do |f| f.write(relevant_config.to_yaml) end end end |
#to_s ⇒ Object
173 174 175 |
# File 'lib/vmit/workspace.rb', line 173 def to_s config.to_s end |