Class: Mccloud::Vm

Inherits:
Object
  • Object
show all
Defined in:
lib/mccloud/vm.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, env) ⇒ Vm

Returns a new instance of Vm.



9
10
11
12
# File 'lib/mccloud/vm.rb', line 9

def initialize(name,env)
  @name=name
  @env=env
end

Instance Attribute Details

#definitionObject

Returns the value of attribute definition.



7
8
9
# File 'lib/mccloud/vm.rb', line 7

def definition
  @definition
end

#envObject (readonly)

Returns the value of attribute env.



6
7
8
# File 'lib/mccloud/vm.rb', line 6

def env
  @env
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/mccloud/vm.rb', line 5

def name
  @name
end

Instance Method Details

#createObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mccloud/vm.rb', line 36

def create
  begin
    unless self.exists?
      File.open(self.path,'w'){ |f| f.write(self.to_template)}
    else
      raise ::Mccloud::Error, "VM file #{self.path} already exists"
    end
  rescue Error => ex
    raise ::Mccloud::Error, "Error writing vm file"
  end
end

#exists?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/mccloud/vm.rb', line 14

def exists?
  File.exists?(self.path)
end

#load!Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mccloud/vm.rb', line 22

def load!
  self.validate

  content=File.read(self.path)
  mccloud_configurator=env.config
  content.gsub!("Mccloud::Config.run","mccloud_configurator.define")

  begin
    env.config.instance_eval(content)
  rescue Error => ex
    raise ::Mccloud::Error, "Error reading vm from file #{definition_file}#{ex}"
  end
end

#pathObject



18
19
20
# File 'lib/mccloud/vm.rb', line 18

def path
  File.join(@env.config.mccloud.vm_path,@name+".rb")
end

#to_templateObject



48
49
50
51
52
53
54
55
# File 'lib/mccloud/vm.rb', line 48

def to_template
  result=""
  filename=File.expand_path(File.join(File.dirname(__FILE__),'templates','vm.erb'))
  env.logger.info "Opening vm template file #{@file}"
  template=File.new(filename).read
  result=ERB.new(template).result(binding)
  return result
end

#validateObject

Raises:



57
58
59
# File 'lib/mccloud/vm.rb', line 57

def validate
  raise ::Mccloud::Error, "Vm #{@name} does not yet exist" unless self.exists?
end