Class: Mccloud::Definition
- Inherits:
-
Object
- Object
- Mccloud::Definition
show all
- Defined in:
- lib/mccloud/definition.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(name, env) ⇒ Definition
Returns a new instance of Definition.
9
10
11
12
13
|
# File 'lib/mccloud/definition.rb', line 9
def initialize(name,env)
@name=name
@env=env
@type="vm"
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
20
21
22
23
|
# File 'lib/mccloud/definition.rb', line 20
def method_missing(m, *args, &block)
@raw.send(m,*args)
end
|
Instance Attribute Details
#env ⇒ Object
Returns the value of attribute env.
6
7
8
|
# File 'lib/mccloud/definition.rb', line 6
def env
@env
end
|
#name ⇒ Object
Returns the value of attribute name.
5
6
7
|
# File 'lib/mccloud/definition.rb', line 5
def name
@name
end
|
#provider ⇒ Object
Returns the value of attribute provider.
7
8
9
|
# File 'lib/mccloud/definition.rb', line 7
def provider
@provider
end
|
Instance Method Details
#copy_template(templatename) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/mccloud/definition.rb', line 66
def copy_template(templatename)
raise ::Mccloud::Error, "Definition #{@name} already exists" if self.exists?
raise ::Mccloud::Error, "Template #{templatename} does not exist" unless @env.config.templates[templatename].exists?
begin
t=@env.config.templates[templatename]
@env.logger.info "Copying template #{t.path} to definition #{self.path}"
FileUtils.cp_r(t.path,self.path)
save_mccloud_rb
FileUtils.rm(File.join(self.path,"mccloud.erb"))
rescue Exception => ex
raise ::Mccloud::Error, "Error copying template #{templatename} to definition #{@name}:\n#{ex}"
end
end
|
#definition_path ⇒ Object
37
38
39
|
# File 'lib/mccloud/definition.rb', line 37
def definition_path
File.join(self.path,"mccloud.rb")
end
|
#exists? ⇒ Boolean
29
30
31
|
# File 'lib/mccloud/definition.rb', line 29
def exists?
File.directory?(self.path)
end
|
#load! ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/mccloud/definition.rb', line 41
def load!
self.validate
content=File.read(self.definition_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 definition from file #{definition_file}#{ex}"
end
end
|
#path ⇒ Object
33
34
35
|
# File 'lib/mccloud/definition.rb', line 33
def path
File.join(@env.config.mccloud.definition_path,@name)
end
|
#save_mccloud_rb ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/mccloud/definition.rb', line 82
def save_mccloud_rb
begin
unless File.exists?(self.definition_path)
File.open(self.definition_path,'w'){ |f| f.write(self.to_template(@name))}
else
raise ::Mccloud::Error, "Definition file #{self.definition_path} already exists"
end
rescue Error => ex
raise ::Mccloud::Error, "Error writing mccloud.rb"
end
end
|
#to_template(templatename) ⇒ Object
94
95
96
97
98
99
100
101
102
|
# File 'lib/mccloud/definition.rb', line 94
def to_template(templatename)
result=""
t=@env.config.templates[templatename]
filename=File.join(self.path,'mccloud.erb')
env.logger.info "Opening vm template file #{@file}"
template=File.new(filename).read
result=ERB.new(template).result(binding)
return result
end
|
#to_vm(name) ⇒ Object
55
56
57
58
59
|
# File 'lib/mccloud/definition.rb', line 55
def to_vm(name)
vm=@raw.dup
vm.name=name
return vm
end
|
#valid? ⇒ Boolean
25
26
27
|
# File 'lib/mccloud/definition.rb', line 25
def valid?
File.exists?(self.definition_path)
end
|
#validate ⇒ Object
61
62
63
|
# File 'lib/mccloud/definition.rb', line 61
def validate
raise ::Mccloud::Error, "Definition #{@name} does not yet exist" unless self.exists?
end
|