nifty 0.2.5 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5cdc153b695c7aff6d7f455493d363fff9e25b86
4
- data.tar.gz: 391b28158089f86dadd7d65afd043f5ccd688401
3
+ metadata.gz: 8cf414409a82b584fa64445d7ab380746e8047b9
4
+ data.tar.gz: 8f3786014089406349e47db7be0fa96156279044
5
5
  SHA512:
6
- metadata.gz: 7975505c83737b902cb1c1aba634c842e19c96fa2c3eb998c7a2807ec3160ed2e90a0fc844db911ac7828f5331ff7af7490d3cda6393d9b49c09c555260fd776
7
- data.tar.gz: ea370f4d5d5d872423eb84a111601e7614d3724ba4aa26f34c03244b93349b4c2f79fed015d171271535d8714c8cf67519ece310dc6a70d2c71f1414f9a650d2
6
+ metadata.gz: 01d3d5df7891b4d48cabf47e1f0210f7f474062ae2fe03e4329e94eb6ce210834c5c11260f76cade2391c4d106d0f6a453297764004b413851f7da7e9c1364d4
7
+ data.tar.gz: a033fda42899d9a49e4bbeb0294f0bdae0e02343affc74ffcd82a9769fb93f880e0c23e6e351d4dedec7b6bc44e068e01779e8f929d0a5e7ca7b442b5e507c6c
data/lib/nifty/backend.rb CHANGED
@@ -27,6 +27,14 @@ class Nifty::Backend
27
27
  nil
28
28
  end
29
29
 
30
+ # Returns hash from which new CLI command for backend is constructed
31
+ #
32
+ # @abstract
33
+ # @return [Hash, nil] hash from which new CLI command for backend is constructed
34
+ def migrate_options
35
+ nil
36
+ end
37
+
30
38
  # Routine run before any events are processed
31
39
  #
32
40
  # @abstract
@@ -41,6 +49,13 @@ class Nifty::Backend
41
49
  def post(parameters)
42
50
  end
43
51
 
52
+
53
+ # Prepares backend for integration with NIFTY
54
+ #
55
+ # @param [Hash] parameters to help the migration
56
+ def migrate(parameters)
57
+ end
58
+
44
59
  # Creates specific event for the backend
45
60
  #
46
61
  # @abstract
@@ -4,6 +4,14 @@ require 'chronic_duration'
4
4
 
5
5
  # OpenNebula backend
6
6
  class Nifty::Backends::Opennebula < Nifty::Backend
7
+ VMCATCHER_APPLIANCE_ID = 'VMCATCHER_EVENT_DC_IDENTIFIER'
8
+ VMCATCHER_APPLIANCE_VERSION = 'VMCATCHER_EVENT_HV_VERSION'
9
+ VMCATCHER_APPLIANCE_TITLE = 'VMCATCHER_EVENT_DC_TITLE'
10
+ VMCATCHER_APPLIANCE_DESCRIPTION = 'VMCATCHER_EVENT_DC_DESCRIPTION'
11
+ VMCATCHER_APPLIANCE_OS_ARCH = 'VMCATCHER_EVENT_SL_ARCH'
12
+ VMCATCHER_APPLIANCE_OS_DISTRIBUTION = 'VMCATCHER_EVENT_SL_OS'
13
+ VMCATCHER_APPLIANCE_OS_VERSION = 'VMCATCHER_EVENT_SL_OSVERSION'
14
+
7
15
  class << self
8
16
  # @see Nifty::Backend#backend?
9
17
  def backend?
@@ -66,6 +74,24 @@ class Nifty::Backends::Opennebula < Nifty::Backend
66
74
  }
67
75
  end
68
76
 
77
+ # @see Nifty::Backend#migrate_options
78
+ def migrate_options
79
+ { :secret => {
80
+ :type => :string,
81
+ :desc => 'Pair of username and password in form of \'username:password\' for accessing OpenNebula'
82
+ },
83
+ :endpoint => {
84
+ :type => :string,
85
+ :desc => 'OpenNebula\'s XML RPC endpoint'
86
+ },
87
+ :"api-call-timeout" => {
88
+ :required => true,
89
+ :type => :string,
90
+ :desc => 'How long will NIFTY wait for image/template operations to finish in OpenNebula'
91
+ }
92
+ }
93
+ end
94
+
69
95
  # @see Nifty::Backend#pre
70
96
  def pre(parameters)
71
97
  Nifty::Backends::Utils::Opennebula::Handler.api_call_timeout = ChronicDuration.parse(parameters[:'api-call-timeout'], keep_zero: true)
@@ -86,6 +112,48 @@ class Nifty::Backends::Opennebula < Nifty::Backend
86
112
  fail Nifty::Errors::BackendError, ex
87
113
  end
88
114
 
115
+ def migrate(parameters)
116
+ client = Nifty::Backends::Utils::Opennebula::Helper.client(parameters[:secret], parameters[:endpoint])
117
+ image_handler = Nifty::Backends::Utils::Opennebula::ImageHandler.new(client)
118
+ template_handler = Nifty::Backends::Utils::Opennebula::TemplateHandler.new(client)
119
+
120
+ image_handler.unmanaged_images.each do |image|
121
+ image_name = image.name
122
+ logger.debug "Processing image #{image_name}"
123
+
124
+ os = Cloud::Appliance::Descriptor::Os.new
125
+ os.arch = image["TEMPLATE/#{VMCATCHER_APPLIANCE_OS_ARCH}"]
126
+ os.version = image["TEMPLATE/#{VMCATCHER_APPLIANCE_OS_VERSION}"]
127
+ os.distribution = image["TEMPLATE/#{VMCATCHER_APPLIANCE_OS_DISTRIBUTION}"]
128
+
129
+ appliance = Cloud::Appliance::Descriptor::Appliance.new os: os
130
+ appliance.identifier = image["TEMPLATE/#{VMCATCHER_APPLIANCE_ID}"]
131
+ appliance.version = image["TEMPLATE/#{VMCATCHER_APPLIANCE_VERSION}"]
132
+ appliance.title = image["TEMPLATE/#{VMCATCHER_APPLIANCE_TITLE}"]
133
+ appliance.description = image["TEMPLATE/#{VMCATCHER_APPLIANCE_DESCRIPTION}"]
134
+
135
+ image_template = Tilt::ERBTemplate.new(File.join(Nifty::GEM_DIR, 'templates', 'image.erb'))
136
+ image_template_update = image_template.render(Object.new, {name: image_name, appliance: appliance})
137
+ logger.debug("Updating image template for image #{image_name}")
138
+ Nifty::Backends::Utils::Opennebula::Helper.handle_opennebula_error { image.update(image_template_update, true) }
139
+
140
+ logger.debug("Looking for templates with image #{image_name}...")
141
+ template_handler.unmanaged_templates_with_image(image_name).each do |template|
142
+ template_name = template.name
143
+ logger.debug "Processing template #{template_name}"
144
+ template_template = Tilt::ERBTemplate.new(File.join(Nifty::GEM_DIR, 'templates', 'template.erb'))
145
+ template_template_update = template_template.render(Object.new, {name: template_name, appliance: appliance})
146
+ logger.debug("Updating template template for template #{template_name}")
147
+ Nifty::Backends::Utils::Opennebula::Helper.handle_opennebula_error { template.update(template_template_update, true) }
148
+ end
149
+ end
150
+
151
+ Nifty::ExitCodes::NO_ERROR_EXIT_CODE
152
+ rescue Nifty::Errors::Backends::OpennebulaError => ex
153
+ logger.error "Migration error: #{ex.message}"
154
+ Nifty::ExitCodes::MIGRATION_ERROR_EXIT_CODE
155
+ end
156
+
89
157
  private
90
158
 
91
159
  # Checks whether there are expired images that should be removed
@@ -43,6 +43,12 @@ class Nifty::Backends::Utils::Opennebula::ImageHandler < Nifty::Backends::Utils:
43
43
  pool.find_all { |image| image["TEMPLATE/#{ATTRIBUTE_EXPIRATION}"] }
44
44
  end
45
45
 
46
+ def unmanaged_images()
47
+ reload!
48
+
49
+ pool.find_all { |image| image["TEMPLATE/#{ATTRIBUTE_APPLIANCE_ID}"].nil? && image["TEMPLATE/#{Nifty::Backends::Opennebula::VMCATCHER_APPLIANCE_ID}"] }
50
+ end
51
+
46
52
  def images_by_version(version)
47
53
  reload!
48
54
 
@@ -23,7 +23,13 @@ class Nifty::Backends::Utils::Opennebula::TemplateHandler < Nifty::Backends::Uti
23
23
  def templates(appliance_id)
24
24
  reload!
25
25
 
26
- pool.find_all{ |template| template["TEMPLATE/#{ATTRIBUTE_APPLIANCE_ID}"] == appliance_id }
26
+ pool.find_all { |template| template["TEMPLATE/#{ATTRIBUTE_APPLIANCE_ID}"] == appliance_id }
27
+ end
28
+
29
+ def unmanaged_templates_with_image(image_name)
30
+ reload!
31
+
32
+ pool.find_all { |template| template["TEMPLATE/DISK/IMAGE"] == image_name && template["TEMPLATE/#{ATTRIBUTE_APPLIANCE_ID}"].nil? }
27
33
  end
28
34
 
29
35
  # Returns template with given id if exists
@@ -114,6 +114,21 @@ def #{backend}_transfer_methods
114
114
  end
115
115
  end
116
116
  ^
117
+
118
+ if backend_clazz.respond_to? 'migrate'
119
+ options = backend_clazz.migrate_options
120
+ options.each do |name, parameters|
121
+ parameters[:default] = Nifty::Settings[backend][name.to_s] unless parameters[:default]
122
+ method_option name, parameters
123
+ end
124
+
125
+ class_eval %Q^
126
+ desc '#{backend}-migrate', 'Prepares #{backend.inspect} for integration with NIFTY'
127
+ def #{backend}_migrate
128
+ migrate('#{backend}', options)
129
+ end
130
+ ^
131
+ end
117
132
  end
118
133
 
119
134
  private
@@ -138,6 +153,16 @@ end
138
153
  exit processor.process_events
139
154
  end
140
155
 
156
+ def migrate(backend, options)
157
+ parameters = options.to_hash.deep_symbolize_keys
158
+ init_log parameters
159
+
160
+ backend = Nifty::Backends.const_get(backend.camelize)
161
+ logger.debug "Selected backend '#{backend.inspect}'"
162
+
163
+ exit backend.migrate(parameters)
164
+ end
165
+
141
166
  # Inits logging according to the settings
142
167
  #
143
168
  # @param [Hash] parameters
@@ -5,4 +5,5 @@ class Nifty::ExitCodes
5
5
  EVENT_PREPARATION_ERROR_EXIT_CODE = 3
6
6
  NO_TRANSFER_METHOD_ERROR_EXIT_CODE = 4
7
7
  TRANSFER_METHOD_ERROR_EXIT_CODE = 5
8
+ MIGRATION_ERROR_EXIT_CODE = 6
8
9
  end
data/lib/nifty/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Nifty
2
- VERSION = "0.2.5"
2
+ VERSION = "0.2.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nifty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michal Kimle
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-02 00:00:00.000000000 Z
11
+ date: 2016-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler