Class: Nifty::Backends::Utils::Opennebula::TemplateHandler

Inherits:
Handler
  • Object
show all
Defined in:
lib/nifty/backends/utils/opennebula/template_handler.rb

Overview

Handler for OpenNebula TemplatePool

Author:

  • Michal Kimle

Constant Summary

Constants inherited from Handler

Handler::API_POLLING_WAIT, Handler::ATTRIBUTE_APPLIANCE_ID, Handler::LEAVE_AS_IS, Handler::ONEADMIN_ID, Handler::OWNER_OCTET

Instance Attribute Summary

Attributes inherited from Handler

#client, #pool

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Handler

chmod, chown, #reload!

Constructor Details

#initialize(client) ⇒ TemplateHandler

Constructor

See Also:



14
15
16
17
# File 'lib/nifty/backends/utils/opennebula/template_handler.rb', line 14

def initialize(client)
  super(client)
  @pool = OpenNebula::TemplatePool.new(client)
end

Class Method Details

.prepare_template(template_dir, data) ⇒ String

Prepares a template for a template

Parameters:

  • template_dir (String)

    directory with templates

  • data (Hash)

    used while populating a template

Returns:

  • (String)

    final template for a template



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/nifty/backends/utils/opennebula/template_handler.rb', line 83

def self.prepare_template(template_dir, data)
  template_location = File.join(template_dir, "template.erb")
  fail Nifty::Errors::ArgumentError, "Missing file 'template.erb' in template directory '#{template_dir}'" unless File.exist?(template_location)

  template = Tilt::ERBTemplate.new(template_location)
  template_content = template.render(Object.new, data)

  template = Tilt::ERBTemplate.new(File.join(Nifty::GEM_DIR, 'templates', 'template.erb'))
  nifty_template_content = template.render(Object.new, data)

  whole_template_content = template_content + nifty_template_content
  logger.debug "Populated template template:\n#{whole_template_content}"
  whole_template_content
end

Instance Method Details

#delete_template(template) ⇒ Object

Deletes template

Parameters:

  • template (OpenNebula::Template)

Raises:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/nifty/backends/utils/opennebula/template_handler.rb', line 49

def delete_template(template)
  template.info!
  id = template.id

  logger.debug("Deleting template with id #{id.inspect}")
  Nifty::Backends::Utils::Opennebula::Helper.handle_opennebula_error { template.delete }

  Timeout::timeout(Nifty::Backends::Utils::Opennebula::Handler.api_call_timeout) do
    while(template_exist?(id))
      sleep(Nifty::Backends::Utils::Opennebula::Handler::API_POLLING_WAIT)
    end
  end
rescue Timeout::Error
  fail Nifty::Errors::ApiCallTimeoutError, "Template with id #{template.id.inspect} was not deleted within timeout"
end

#register_template(template_template) ⇒ Object

Registers a new template

Parameters:

  • template_template (String)

    template for template



68
69
70
71
72
73
74
75
76
# File 'lib/nifty/backends/utils/opennebula/template_handler.rb', line 68

def register_template(template_template)
  template_alloc = ::OpenNebula::Template.build_xml
  template = ::OpenNebula::Template.new(template_alloc, client)

  logger.debug("Registering template with template\n#{template_template}")
  Nifty::Backends::Utils::Opennebula::Helper.handle_opennebula_error { template.allocate(template_template) }

  template
end

#template_exist?(id) ⇒ OpenNebula::Template

Returns template with given id if exists

Parameters:

  • id (Fixnum)

Returns:

  • (OpenNebula::Template)

    template with given id



39
40
41
42
43
# File 'lib/nifty/backends/utils/opennebula/template_handler.rb', line 39

def template_exist?(id)
  reload!

  pool.find { |template| template.id == id }
end

#templates(appliance_id) ⇒ Array

Returns all templates for given appliance id

Parameters:

  • appliance_id (String)

Returns:

  • (Array)

    array of templates



23
24
25
26
27
# File 'lib/nifty/backends/utils/opennebula/template_handler.rb', line 23

def templates(appliance_id)
  reload!

  pool.find_all { |template| template["TEMPLATE/#{ATTRIBUTE_APPLIANCE_ID}"] == appliance_id }
end

#unmanaged_templates_with_image(image_name) ⇒ Object



29
30
31
32
33
# File 'lib/nifty/backends/utils/opennebula/template_handler.rb', line 29

def unmanaged_templates_with_image(image_name)
  reload!

  pool.find_all { |template| template["TEMPLATE/DISK/IMAGE"] == image_name && template["TEMPLATE/#{ATTRIBUTE_APPLIANCE_ID}"].nil? }
end