Class: Nifty::Backends::Opennebula

Inherits:
Nifty::Backend show all
Defined in:
lib/nifty/backends/opennebula.rb

Overview

OpenNebula backend

Constant Summary collapse

VMCATCHER_APPLIANCE_ID =
'VMCATCHER_EVENT_DC_IDENTIFIER'
VMCATCHER_APPLIANCE_VERSION =
'VMCATCHER_EVENT_HV_VERSION'
VMCATCHER_APPLIANCE_TITLE =
'VMCATCHER_EVENT_DC_TITLE'
VMCATCHER_APPLIANCE_DESCRIPTION =
'VMCATCHER_EVENT_DC_DESCRIPTION'
VMCATCHER_APPLIANCE_OS_ARCH =
'VMCATCHER_EVENT_SL_ARCH'
VMCATCHER_APPLIANCE_OS_DISTRIBUTION =
'VMCATCHER_EVENT_SL_OS'
VMCATCHER_APPLIANCE_OS_VERSION =
'VMCATCHER_EVENT_SL_OSVERSION'
VMCATCHER_PREFIX =
'VMCATCHER_'

Class Method Summary collapse

Methods inherited from Nifty::Backend

post

Class Method Details

.backend?Boolean

Returns:

  • (Boolean)

See Also:

  • Nifty::Backend#backend?


18
19
20
# File 'lib/nifty/backends/opennebula.rb', line 18

def backend?
  true
end

.create_event(event_class, appliance, transfer_method, parameters) ⇒ Object

Parameters:

  • parameters (Hash)

Options Hash (parameters):

  • secret (String)

    OpenNebula’s secret

  • endpoint (String)

    OpenNebula’s endpoint

  • datastores (Array)

    array of datastore names

  • template-dir (String)

    path to dir with image and template templates

See Also:

  • Nifty::Backend#create_event


112
113
114
115
116
117
118
# File 'lib/nifty/backends/opennebula.rb', line 112

def create_event(event_class, appliance, transfer_method, parameters)
  client = Nifty::Backends::Utils::Opennebula::Helper.client(parameters[:secret], parameters[:endpoint])
  datastores = Nifty::Backends::Utils::Opennebula::DatastoreHandler.new(client).datastores(parameters[:datastores])
  event_class.new(appliance, transfer_method, client, datastores, parameters)
rescue Nifty::Errors::Backends::OpennebulaError => ex
  fail Nifty::Errors::BackendError, ex
end

.descriptionObject

See Also:

  • Nifty::Backend#description


23
24
25
# File 'lib/nifty/backends/opennebula.rb', line 23

def description
  "OpenNebula backend"
end

.migrate(parameters) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/nifty/backends/opennebula.rb', line 120

def migrate(parameters)
  client = Nifty::Backends::Utils::Opennebula::Helper.client(parameters[:secret], parameters[:endpoint])
  image_handler = Nifty::Backends::Utils::Opennebula::ImageHandler.new(client)
  template_handler = Nifty::Backends::Utils::Opennebula::TemplateHandler.new(client)

  image_handler.unmanaged_images.each do |image|
    image_name = image.name
    logger.debug "Processing image #{image_name}"

    os = Cloud::Appliance::Descriptor::Os.new
    os.arch = image["TEMPLATE/#{VMCATCHER_APPLIANCE_OS_ARCH}"]
    os.version = image["TEMPLATE/#{VMCATCHER_APPLIANCE_OS_VERSION}"]
    os.distribution = image["TEMPLATE/#{VMCATCHER_APPLIANCE_OS_DISTRIBUTION}"]

    appliance = Cloud::Appliance::Descriptor::Appliance.new os: os
    appliance.identifier = image["TEMPLATE/#{VMCATCHER_APPLIANCE_ID}"]
    appliance.version = image["TEMPLATE/#{VMCATCHER_APPLIANCE_VERSION}"]
    appliance.title = image["TEMPLATE/#{VMCATCHER_APPLIANCE_TITLE}"]
    appliance.description = image["TEMPLATE/#{VMCATCHER_APPLIANCE_DESCRIPTION}"]

    vmcatcher_attributes = {}
    image.to_hash['IMAGE']['TEMPLATE'].each_pair { |k,v| vmcatcher_attributes[k] = v if k.start_with? VMCATCHER_PREFIX }

    image_template = Tilt::ERBTemplate.new(File.join(Nifty::GEM_DIR, 'templates', 'image.erb'))
    image_template_update = image_template.render(Object.new, {name: image_name, appliance: appliance})
    logger.debug("Updating image template for image #{image_name}")
    Nifty::Backends::Utils::Opennebula::Helper.handle_opennebula_error { image.update(image_template_update, true) }

    appliance.attributes = vmcatcher_attributes

    logger.debug("Looking for templates with image #{image_name}...")
    template_handler.unmanaged_templates_with_image(image_name).each do |template|
      template_name = template.name
      logger.debug "Processing template #{template_name}"
      template_template = Tilt::ERBTemplate.new(File.join(Nifty::GEM_DIR, 'templates', 'template.erb'))
      template_template_update = template_template.render(Object.new, {name: template_name, appliance: appliance})
      logger.debug("Updating template template for template #{template_name}")
      Nifty::Backends::Utils::Opennebula::Helper.handle_opennebula_error { template.update(template_template_update, true) }
    end
  end

  Nifty::ExitCodes::NO_ERROR_EXIT_CODE
rescue Nifty::Errors::Backends::OpennebulaError => ex
  logger.error "Migration error: #{ex.message}"
  Nifty::ExitCodes::MIGRATION_ERROR_EXIT_CODE
end

.migrate_optionsObject

See Also:

  • Nifty::Backend#migrate_options


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

def migrate_options
  { :secret => {
      :type => :string,
      :desc => 'Pair of username and password in form of \'username:password\' for accessing OpenNebula'
    },
    :endpoint => {
      :type => :string,
      :desc => 'OpenNebula\'s XML RPC endpoint'
    },
    :"api-call-timeout" => {
      :required => true,
      :type => :string,
      :desc => 'How long will NIFTY wait for image/template operations to finish in OpenNebula'
    }
  }
end

.optionsObject

See Also:

  • Nifty::Backend#options


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/nifty/backends/opennebula.rb', line 28

def options
  { :secret => {
      :type => :string,
      :desc => 'Pair of username and password in form of \'username:password\' for accessing OpenNebula'
    },
    :endpoint => {
      :type => :string,
      :desc => 'OpenNebula\'s XML RPC endpoint'
    },
    :"expiration-interval" => {
      :required => true,
      :type => :string,
      :desc => 'How long should expired images be kept in OpenNebula befor removal, 0 means don\'t remove'
    },
    :"api-call-timeout" => {
      :required => true,
      :type => :string,
      :desc => 'How long will NIFTY wait for image/template operations to finish in OpenNebula'
    },
    :datastores => {
      :required => true,
      :type => :array,
      :aliases => '-d',
      :desc => 'Names of OpenNebula datastores images will be uploaded to'
    },
    :"template-dir" => {
      :default => File.join(Nifty::GEM_DIR, 'config', 'templates'),
      :type => :string,
      :desc => 'If set, templates within this directory are used to construct images and templates in OpenNebula'
    },
    :"description-naming" => {
      :type => :boolean,
      :desc => 'If true, uses identifier and version from appliance description as template/image name instead of generated uuids'
    },
    :"disk-expiration" => {
      :type => :boolean,
      :desc => 'Will expire old disks before the new one is registered'
    },
    :permissions => {
      :required => true,
      :type => :string,
      :desc => 'UNIX-like image and template permissions in OpenNebula'
    },
    :groups => {
      :type => :array,
      :desc => 'Names of groups appliances will be registred to in OpenNebula'
    },
    :users => {
      :type => :array,
      :desc => 'Handle only images/templates of specified users'
    }
  }
end

.pre(parameters) ⇒ Object

See Also:

  • Nifty::Backend#pre


101
102
103
104
# File 'lib/nifty/backends/opennebula.rb', line 101

def pre(parameters)
  Nifty::Backends::Utils::Opennebula::Handler.api_call_timeout = ChronicDuration.parse(parameters[:'api-call-timeout'], keep_zero: true)
  expiration_check(parameters)
end