Class: Bfire::Template
- Inherits:
-
Object
- Object
- Bfire::Template
- Defined in:
- lib/bfire/template.rb
Instance Attribute Summary collapse
-
#context(opts = {}) ⇒ Object
readonly
Returns the value of attribute context.
-
#disks ⇒ Object
readonly
Return the list of disks defined.
-
#errors ⇒ Object
readonly
Return an Array of error messages in case this template is not valid.
-
#group ⇒ Object
readonly
Return the group this template belongs to.
-
#instances ⇒ Object
readonly
Returns the value of attribute instances.
-
#metrics ⇒ Object
readonly
Return the list of metrics defined.
-
#name ⇒ Object
readonly
Return the template name (i.e. the location name).
-
#nics ⇒ Object
readonly
Return the list of nics defined.
-
#properties ⇒ Object
readonly
Return the properties defined for this template (instance_type, etc.).
Instance Method Summary collapse
- #connect_to(network, options = {}) ⇒ Object
-
#deploy(storage, options = {}) ⇒ Object
Define the image to deploy on the compute resources.
-
#initialize(group, location_name = nil) ⇒ Template
constructor
A new instance of Template.
-
#instance_type(instance_type) ⇒ Object
Define the instance type to use.
- #location ⇒ Object
-
#merge_defaults!(template) ⇒ Object
Merge this template with another one.
-
#register(metric_name, options = {}) ⇒ Object
Register a metric on the compute resources.
-
#resolve! ⇒ Object
Resolve the networks and storages required for the template to be valid.
-
#to_h ⇒ Object
Exports the template to a ruby Hash, which conforms to what is expected by Restfully to submit a resource.
-
#valid? ⇒ Boolean
Returns true if valid, false otherwise.
Constructor Details
#initialize(group, location_name = nil) ⇒ Template
Returns a new instance of Template.
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/bfire/template.rb', line 21 def initialize(group, location_name = nil) @group = group @location_name = location_name @name = location_name @nics = [] @disks = [] @errors = [] @metrics = [] @properties = {} @context = {} @instances = [] end |
Instance Attribute Details
#context(opts = {}) ⇒ Object (readonly)
Returns the value of attribute context.
13 14 15 |
# File 'lib/bfire/template.rb', line 13 def context @context end |
#disks ⇒ Object (readonly)
Return the list of disks defined.
6 7 8 |
# File 'lib/bfire/template.rb', line 6 def disks @disks end |
#errors ⇒ Object (readonly)
Return an Array of error messages in case this template is not valid.
17 18 19 |
# File 'lib/bfire/template.rb', line 17 def errors @errors end |
#group ⇒ Object (readonly)
Return the group this template belongs to.
19 20 21 |
# File 'lib/bfire/template.rb', line 19 def group @group end |
#instances ⇒ Object (readonly)
Returns the value of attribute instances.
14 15 16 |
# File 'lib/bfire/template.rb', line 14 def instances @instances end |
#metrics ⇒ Object (readonly)
Return the list of metrics defined.
12 13 14 |
# File 'lib/bfire/template.rb', line 12 def metrics @metrics end |
#name ⇒ Object (readonly)
Return the template name (i.e. the location name).
8 9 10 |
# File 'lib/bfire/template.rb', line 8 def name @name end |
#nics ⇒ Object (readonly)
Return the list of nics defined.
4 5 6 |
# File 'lib/bfire/template.rb', line 4 def nics @nics end |
#properties ⇒ Object (readonly)
Return the properties defined for this template (instance_type, etc.).
10 11 12 |
# File 'lib/bfire/template.rb', line 10 def properties @properties end |
Instance Method Details
#connect_to(network, options = {}) ⇒ Object
67 68 69 70 71 |
# File 'lib/bfire/template.rb', line 67 def connect_to(network, = {}) @nics.push .merge( :network => network ) end |
#deploy(storage, options = {}) ⇒ Object
Define the image to deploy on the compute resources.
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/bfire/template.rb', line 56 def deploy(storage, = {}) props = .merge( :storage => storage ) if @disks.empty? @disks.push props else @disks[0] = props end end |
#instance_type(instance_type) ⇒ Object
Define the instance type to use.
51 52 53 |
# File 'lib/bfire/template.rb', line 51 def instance_type(instance_type) @properties[:instance_type] = instance_type.to_s end |
#location ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/bfire/template.rb', line 34 def location @location ||= if @location_name == :default # noop else group.engine.fetch_location(@location_name) end end |
#merge_defaults!(template) ⇒ Object
Merge this template with another one. nics, disks, and metrics will be added, while other properties will be merged.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/bfire/template.rb', line 76 def merge_defaults!(template) @properties = template.properties.merge(@properties) @context = template.context.merge(@context) template.nics.each do |nic| @nics.unshift nic.clone end template.disks.each do |disk| @disks.unshift disk.clone end template.metrics.each do |metric| @metrics.unshift metric.clone end self end |
#register(metric_name, options = {}) ⇒ Object
Register a metric on the compute resources.
118 119 120 |
# File 'lib/bfire/template.rb', line 118 def register(metric_name, = {}) @metrics.push .merge(:name => metric_name) end |
#resolve! ⇒ Object
Resolve the networks and storages required for the template to be valid.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/bfire/template.rb', line 101 def resolve! nics.each{|nic| nic[:network] = group.engine.fetch_network( nic[:network], location ) || raise(Error, "Can't find network #{nic[:network].inspect} at #{location["name"].inspect}") } disks.each{|disk| disk[:storage] = group.engine.fetch_storage( disk[:storage], location ) || raise(Error, "Can't find storage #{disk[:storage].inspect} at #{location["name"].inspect}") } self end |
#to_h ⇒ Object
Exports the template to a ruby Hash, which conforms to what is expected by Restfully to submit a resource.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/bfire/template.rb', line 124 def to_h h = {} h.merge!(@properties) h['name'] = "#{group.name}--#{name}--#{SecureRandom.hex(4)}" h['name'] << "-#{group.tag}" if group.tag h['nic'] = nics h['disk'] = disks h['location'] = location h['context'] = @context h['context']['metrics'] = XML::Node.new_cdata(metrics.map{|m| "<metric>"+[m[:name], m[:command]].join(",")+"</metric>" }.join("")) unless metrics.empty? group.dependencies.each{|gname,block| h['context'].merge!(block.call(group.engine.group(gname))) } h end |
#valid? ⇒ Boolean
Returns true if valid, false otherwise
92 93 94 95 96 97 98 |
# File 'lib/bfire/template.rb', line 92 def valid? @errors = [] @errors.push("You must specify an instance_type") unless properties[:instance_type] @errors.push("You must specify at least one disk image") if @disks.empty? @errors.push("You must specify at least one network attachment") if @nics.empty? @errors.empty? end |