Class: VCenterDriver::VIHelper
- Inherits:
-
Object
- Object
- VCenterDriver::VIHelper
- Defined in:
- lib/vi_helper.rb
Overview
Class VIHelper
Constant Summary collapse
- ETC_LOCATION =
'/etc/one/'
- VCENTER_DRIVER_DEFAULT =
"#{ETC_LOCATION}/vcenter_driver.default"
- VM_PREFIX_DEFAULT =
'one-$i-'
Class Method Summary collapse
- .add_ref_hash(attr, one_object) ⇒ Object
- .check_error(rc, message, exit_condition = false) ⇒ Object
- .check_opts(opts, att_list) ⇒ Object
- .clean_ref_hash ⇒ Object
-
.client ⇒ Object
rubocop:disable Style/GlobalVars rubocop:disable Style/ClassVars.
- .create_ref_hash(attribute, pool, vcenter_uuid = nil) ⇒ Object
- .find_by_name(the_class, name, pool = nil, raise_if_fail = true) ⇒ Object
- .find_by_ref(the_class, attribute, ref, vcenter_uuid, pool = nil) ⇒ Object
- .find_image_by(att, the_class, path, ds_id, pool = nil) ⇒ Object
- .find_persistent_image_by_source(source, pool) ⇒ Object
- .find_vcenter_vm_by_name(one_vm, host, vi_client) ⇒ Object
- .generate_name(opts, nbytes) ⇒ Object
- .get_cluster_id(clusters) ⇒ Object
- .get_default(xpath) ⇒ Object
- .get_location(item) ⇒ Object
- .get_ref_key(element, attribute, vcenter_uuid = nil) ⇒ Object
- .new_one_item(the_class) ⇒ Object
- .one_item(the_class, id, exit_if_fail = true) ⇒ Object
- .one_managed?(object) ⇒ Boolean
- .one_name(the_class, name, key, pool = nil, bytes = 0) ⇒ Object
- .one_pool(the_class, exit_if_fail = true) ⇒ Object
- .remove_ref_hash(attr, one_object) ⇒ Object
-
.return_if_error(rc, item, exit_if_fail) ⇒ Object
rubocop:enable Style/ClassVars.
-
.set_client(options, client = nil) ⇒ Object
rubocop:enable Style/GlobalVars.
Class Method Details
.add_ref_hash(attr, one_object) ⇒ Object
191 192 193 194 195 196 197 198 199 |
# File 'lib/vi_helper.rb', line 191 def self.add_ref_hash(attr, one_object) raise 'cache is empty!' unless @ref_hash refkey = get_ref_key(one_object, attr) return unless @ref_hash[attr] @ref_hash[attr][refkey] = one_object end |
.check_error(rc, message, exit_condition = false) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/vi_helper.rb', line 66 def self.check_error(rc, , exit_condition = false) return unless OpenNebula.is_error?(rc) OpenNebula.("\n Error #{}: #{rc.}\n") exit 1 if exit_condition raise rc. end |
.check_opts(opts, att_list) ⇒ Object
92 93 94 95 96 |
# File 'lib/vi_helper.rb', line 92 def self.check_opts(opts, att_list) att_list.each do |att| raise "#{att} option is mandatory" if opts[att].nil? end end |
.clean_ref_hash ⇒ Object
187 188 189 |
# File 'lib/vi_helper.rb', line 187 def self.clean_ref_hash @ref_hash = {} end |
.client ⇒ Object
rubocop:disable Style/GlobalVars rubocop:disable Style/ClassVars
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/vi_helper.rb', line 33 def self.client if $conf.nil? @@client ||= OpenNebula::Client.new # rubocop:disable Style/ClassVars else @@client ||= OpenNebula::Client.new( nil, $conf[:one_xmlrpc] ) end end |
.create_ref_hash(attribute, pool, vcenter_uuid = nil) ⇒ Object
176 177 178 179 180 181 182 183 184 185 |
# File 'lib/vi_helper.rb', line 176 def self.create_ref_hash(attribute, pool, vcenter_uuid = nil) hash = {} pool.each_element(proc do |e| refkey = get_ref_key(e, attribute, vcenter_uuid) hash[refkey] = e end) hash end |
.find_by_name(the_class, name, pool = nil, raise_if_fail = true) ⇒ Object
121 122 123 124 125 126 127 128 129 |
# File 'lib/vi_helper.rb', line 121 def self.find_by_name(the_class, name, pool = nil, raise_if_fail = true) pool = one_pool(the_class, raise_if_fail) if pool.nil? element = pool.find {|e| e['NAME'] == name.to_s } if element.nil? && raise_if_fail raise "Could not find element '#{name}' in pool '#{the_class}'" end element end |
.find_by_ref(the_class, attribute, ref, vcenter_uuid, pool = nil) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/vi_helper.rb', line 211 def self.find_by_ref( the_class, attribute, ref, vcenter_uuid, pool = nil ) pool = one_pool(the_class, false) if pool.nil? @ref_hash ||= {} if @ref_hash[attribute].nil? || @ref_hash[attribute] == {} @ref_hash[attribute] = create_ref_hash(attribute, pool, vcenter_uuid) end refkey = '' refkey = ref if ref refkey += vcenter_uuid if vcenter_uuid @ref_hash[attribute][refkey] end |
.find_image_by(att, the_class, path, ds_id, pool = nil) ⇒ Object
235 236 237 238 239 240 241 |
# File 'lib/vi_helper.rb', line 235 def self.find_image_by(att, the_class, path, ds_id, pool = nil) pool = one_pool(the_class, false) if pool.nil? pool.find do |e| e[att] == Addressable::URI.escape(path) && e['DATASTORE_ID'] == ds_id end end |
.find_persistent_image_by_source(source, pool) ⇒ Object
243 244 245 246 247 248 |
# File 'lib/vi_helper.rb', line 243 def self.find_persistent_image_by_source(source, pool) pool.find do |e| e['SOURCE'] == source && e['PERSISTENT'] == '1' end end |
.find_vcenter_vm_by_name(one_vm, host, vi_client) ⇒ Object
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/vi_helper.rb', line 250 def self.find_vcenter_vm_by_name(one_vm, host, vi_client) # Let's try to find the VM object only by its name # Let's build the VM name vm_prefix = host['TEMPLATE/VM_PREFIX'] vm_prefix = VM_PREFIX_DEFAULT if vm_prefix.nil? || vm_prefix.empty? vm_prefix.gsub!('$i', one_vm['ID']) vm_name = vm_prefix + one_vm['NAME'] # We have no DEPLOY_ID, the VM has never been deployed # let's use a view to try to find the VM from the root folder view = vi_client .vim .serviceContent .viewManager .CreateContainerView( { :container => vi_client.vim.rootFolder, :type => ['VirtualMachine'], :recursive => true } ) if !view.view.nil? && !view.view.empty? vcenter_vm = view .view .find {|v| v.name == vm_name } end view.DestroyView # Destroy the view vcenter_vm end |
.generate_name(opts, nbytes) ⇒ Object
131 132 133 134 135 136 137 138 139 |
# File 'lib/vi_helper.rb', line 131 def self.generate_name(opts, nbytes) return opts[:name] if nbytes <= 0 @sha256 ||= Digest::SHA256.new chain = opts[:key] hash = @sha256.hexdigest(chain)[0..nbytes-1] "#{opts[:name]}-#{hash}" end |
.get_cluster_id(clusters) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/vi_helper.rb', line 75 def self.get_cluster_id(clusters) clusters.each do |id| return id unless id == -1 end -1 end |
.get_default(xpath) ⇒ Object
283 284 285 286 287 288 289 290 291 |
# File 'lib/vi_helper.rb', line 283 def self.get_default(xpath) begin xml = OpenNebula::XMLElement.new xml.initialize_xml(File.read(VCENTER_DRIVER_DEFAULT), 'VCENTER') xml[xpath] rescue StandardError nil end end |
.get_location(item) ⇒ Object
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/vi_helper.rb', line 293 def self.get_location(item) folders = [] until item.instance_of? RbVmomi::VIM::Datacenter item = item.parent if !item.instance_of?(RbVmomi::VIM::Datacenter) && item.name != 'host' folders << item.name end raise 'Could not find the location' if item.nil? end location = folders.reverse.join('/') location = '/' if location.empty? location end |
.get_ref_key(element, attribute, vcenter_uuid = nil) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/vi_helper.rb', line 157 def self.get_ref_key(element, attribute, vcenter_uuid = nil) key = element[attribute] return if key.nil? tvid = element['TEMPLATE/VCENTER_INSTANCE_ID'] uvid = element['USER_TEMPLATE/VCENTER_INSTANCE_ID'] if tvid key += tvid elsif uvid key += uvid elsif vcenter_uuid key += vcenter_uuid end key end |
.new_one_item(the_class) ⇒ Object
104 105 106 |
# File 'lib/vi_helper.rb', line 104 def self.new_one_item(the_class) the_class.new(the_class.build_xml, client) end |
.one_item(the_class, id, exit_if_fail = true) ⇒ Object
98 99 100 101 102 |
# File 'lib/vi_helper.rb', line 98 def self.one_item(the_class, id, exit_if_fail = true) item = the_class.new_with_id(id, client) rc = item.info return_if_error(rc, item, exit_if_fail) end |
.one_managed?(object) ⇒ Boolean
82 83 84 85 86 87 88 89 90 |
# File 'lib/vi_helper.rb', line 82 def self.one_managed?(object) if object.class.ancestors.include?(OpenNebula::XMLElement) managed = object['TEMPLATE/OPENNEBULA_MANAGED'] || object['USER_TEMPLATE/OPENNEBULA_MANAGED'] return managed != 'NO' end false end |
.one_name(the_class, name, key, pool = nil, bytes = 0) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/vi_helper.rb', line 141 def self.one_name(the_class, name, key, pool = nil, bytes = 0) # Remove \u007F character that comes from vcenter name = name.tr("\u007F", '') pool = one_pool(the_class) if pool.nil? import_name = generate_name({ :name => name, :key => key }, bytes) begin find_by_name(the_class, import_name, pool) rescue StandardError return import_name end one_name(the_class, name, key, pool, bytes+2) end |
.one_pool(the_class, exit_if_fail = true) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/vi_helper.rb', line 108 def self.one_pool(the_class, exit_if_fail = true) item = the_class.new(client) rc = nil begin rc = item.info_all rescue StandardError rc = item.info end return_if_error(rc, item, exit_if_fail) end |
.remove_ref_hash(attr, one_object) ⇒ Object
201 202 203 204 205 206 207 208 209 |
# File 'lib/vi_helper.rb', line 201 def self.remove_ref_hash(attr, one_object) raise 'cache is empty!' unless @ref_hash refkey = get_ref_key(one_object, attr) return unless @ref_hash[attr] @ref_hash[attr].delete(refkey) end |
.return_if_error(rc, item, exit_if_fail) ⇒ Object
rubocop:enable Style/ClassVars
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/vi_helper.rb', line 54 def self.return_if_error(rc, item, exit_if_fail) if OpenNebula.is_error?(rc) raise rc. unless exit_if_fail STDERR.puts rc. exit 1 else item end end |
.set_client(options, client = nil) ⇒ Object
rubocop:enable Style/GlobalVars
45 46 47 48 49 50 51 |
# File 'lib/vi_helper.rb', line 45 def self.set_client(, client = nil) if client.nil? @@client=OpenNebulaHelper::OneHelper.get_client(, true) else @@client = client end end |