forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ext_management_system.rb
704 lines (568 loc) · 22.1 KB
/
ext_management_system.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
class ExtManagementSystem < ApplicationRecord
include CustomActionsMixin
def self.types
leaf_subclasses.collect(&:ems_type)
end
def self.supported_types
supported_subclasses.collect(&:ems_type)
end
def self.leaf_subclasses
descendants.select { |d| d.subclasses.empty? }
end
def self.supported_subclasses
subclasses.flat_map do |s|
s.subclasses.empty? ? s : s.supported_subclasses
end
end
def self.supported_types_and_descriptions_hash
supported_subclasses.each_with_object({}) do |klass, hash|
if Vmdb::PermissionStores.instance.supported_ems_type?(klass.ems_type)
hash[klass.ems_type] = klass.description
end
end
end
belongs_to :provider
has_many :child_managers, :class_name => 'ExtManagementSystem', :foreign_key => 'parent_ems_id'
include CustomAttributeMixin
belongs_to :tenant
has_many :container_deployments, :foreign_key => :deployed_on_ems_id, :inverse_of => :deployed_on_ems
has_many :endpoints, :as => :resource, :dependent => :destroy, :autosave => true
has_many :hosts, :foreign_key => "ems_id", :dependent => :nullify, :inverse_of => :ext_management_system
has_many :non_clustered_hosts, -> { non_clustered }, :class_name => "Host", :foreign_key => "ems_id"
has_many :clustered_hosts, -> { clustered }, :class_name => "Host", :foreign_key => "ems_id"
has_many :vms_and_templates, :foreign_key => "ems_id", :dependent => :nullify,
:class_name => "VmOrTemplate", :inverse_of => :ext_management_system
has_many :miq_templates, :foreign_key => :ems_id, :inverse_of => :ext_management_system
has_many :vms, :foreign_key => :ems_id, :inverse_of => :ext_management_system
has_many :operating_systems, :through => :vms_and_templates
has_many :hardwares, :through => :vms_and_templates
has_many :networks, :through => :hardwares
has_many :disks, :through => :hardwares
has_many :storages, -> { distinct }, :through => :hosts
has_many :ems_events, -> { order("timestamp") }, :class_name => "EmsEvent", :foreign_key => "ems_id",
:inverse_of => :ext_management_system
has_many :generated_events, -> { order("timestamp") }, :class_name => "EmsEvent", :foreign_key => "generating_ems_id",
:inverse_of => :generating_ems
has_many :policy_events, -> { order("timestamp") }, :class_name => "PolicyEvent", :foreign_key => "ems_id"
has_many :blacklisted_events, :foreign_key => "ems_id", :dependent => :destroy, :inverse_of => :ext_management_system
has_many :miq_alert_statuses, :foreign_key => "ems_id", :dependent => :destroy
has_many :ems_folders, :foreign_key => "ems_id", :dependent => :destroy, :inverse_of => :ext_management_system
has_many :ems_clusters, :foreign_key => "ems_id", :dependent => :destroy, :inverse_of => :ext_management_system
has_many :resource_pools, :foreign_key => "ems_id", :dependent => :destroy, :inverse_of => :ext_management_system
has_many :customization_specs, :foreign_key => "ems_id", :dependent => :destroy, :inverse_of => :ext_management_system
has_many :storage_profiles, :foreign_key => "ems_id", :dependent => :destroy, :inverse_of => :ext_management_system
has_many :physical_servers, :foreign_key => "ems_id", :dependent => :destroy, :inverse_of => :ext_management_system
has_many :customization_scripts, :foreign_key => "manager_id", :dependent => :destroy, :inverse_of => :ext_management_system
has_one :iso_datastore, :foreign_key => "ems_id", :dependent => :destroy, :inverse_of => :ext_management_system
belongs_to :zone
has_many :metrics, :as => :resource # Destroy will be handled by purger
has_many :metric_rollups, :as => :resource # Destroy will be handled by purger
has_many :vim_performance_states, :as => :resource # Destroy will be handled by purger
has_many :miq_events, :as => :target, :dependent => :destroy
has_many :cloud_subnets, :foreign_key => :ems_id, :dependent => :destroy
validates :name, :presence => true, :uniqueness => {:scope => [:tenant_id]}
validates :hostname, :presence => true, :if => :hostname_required?
validate :hostname_uniqueness_valid?, :if => :hostname_required?
scope :with_eligible_manager_types, ->(eligible_types) { where(:type => eligible_types) }
serialize :options
def hostname_uniqueness_valid?
return unless hostname_required?
return unless hostname.present? # Presence is checked elsewhere
# check uniqueness per provider type
existing_hostnames = (self.class.all - [self]).map(&:hostname).compact.map(&:downcase)
errors.add(:hostname, N_("has to be unique per provider type")) if existing_hostnames.include?(hostname.downcase)
end
include NewWithTypeStiMixin
include UuidMixin
include EmsRefresh::Manager
include TenancyMixin
include AvailabilityMixin
include SupportsFeatureMixin
include ComplianceMixin
include CustomAttributeMixin
after_destroy { |record| $log.info("MIQ(ExtManagementSystem.after_destroy) Removed EMS [#{record.name}] id [#{record.id}]") }
acts_as_miq_taggable
include FilterableMixin
include EventMixin
include MiqPolicyMixin
include RelationshipMixin
self.default_relationship_type = "ems_metadata"
include AggregationMixin
include AuthenticationMixin
include Metric::CiMixin
include AsyncDeleteMixin
delegate :ipaddress,
:ipaddress=,
:hostname,
:hostname=,
:port,
:port=,
:security_protocol,
:security_protocol=,
:certificate_authority,
:certificate_authority=,
:to => :default_endpoint,
:allow_nil => true
alias_method :address, :hostname # TODO: Remove all callers of address
virtual_column :ipaddress, :type => :string, :uses => :endpoints
virtual_column :hostname, :type => :string, :uses => :endpoints
virtual_column :port, :type => :integer, :uses => :endpoints
virtual_column :security_protocol, :type => :string, :uses => :endpoints
virtual_column :emstype, :type => :string
virtual_column :emstype_description, :type => :string
virtual_column :last_refresh_status, :type => :string
virtual_total :total_vms_and_templates, :vms_and_templates
virtual_total :total_vms, :vms
virtual_total :total_miq_templates, :miq_templates
virtual_total :total_hosts, :hosts
virtual_total :total_storages, :storages
virtual_total :total_clusters, :clusters
virtual_column :zone_name, :type => :string, :uses => :zone
virtual_column :total_vms_on, :type => :integer
virtual_column :total_vms_off, :type => :integer
virtual_column :total_vms_unknown, :type => :integer
virtual_column :total_vms_never, :type => :integer
virtual_column :total_vms_suspended, :type => :integer
virtual_total :total_subnets, :cloud_subnets
virtual_column :supports_block_storage, :type => :boolean
virtual_column :supports_cloud_object_store_container_create, :type => :boolean
virtual_aggregate :total_vcpus, :hosts, :sum, :total_vcpus
virtual_aggregate :total_memory, :hosts, :sum, :ram_size
virtual_aggregate :total_cloud_vcpus, :vms, :sum, :cpu_total_cores
virtual_aggregate :total_cloud_memory, :vms, :sum, :ram_size
alias_method :clusters, :ems_clusters # Used by web-services to return clusters as the property name
alias_attribute :to_s, :name
default_value_for :enabled, true
def self.with_ipaddress(ipaddress)
joins(:endpoints).where(:endpoints => {:ipaddress => ipaddress})
end
def self.with_hostname(hostname)
joins(:endpoints).where(:endpoints => {:hostname => hostname})
end
def self.with_role(role)
joins(:endpoints).where(:endpoints => {:role => role})
end
def self.with_port(port)
joins(:endpoints).where(:endpoints => {:port => port})
end
def self.create_discovered_ems(ost)
ip = ost.ipaddr
unless with_ipaddress(ip).exists?
hostname = Socket.getaddrinfo(ip, nil)[0][2]
ems_klass, ems_name = if ost.hypervisor.include?(:scvmm)
[ManageIQ::Providers::Microsoft::InfraManager, 'SCVMM']
elsif ost.hypervisor.include?(:rhevm)
[ManageIQ::Providers::Redhat::InfraManager, 'RHEV-M']
else
[ManageIQ::Providers::Vmware::InfraManager, 'Virtual Center']
end
ems = ems_klass.create(
:ipaddress => ip,
:name => "#{ems_name} (#{ip})",
:hostname => hostname,
:zone_id => MiqServer.my_server.zone.id
)
_log.info("Provider #{ems.name} created")
AuditEvent.success(
:event => "ems_created",
:target_id => ems.id,
:target_class => "ExtManagementSystem",
:message => "%{provider_type} %{provider_name} created" % {
:provider_type => Dictionary.gettext("ext_management_systems",
:type => :table,
:notfound => :titleize,
:plural => false,
:translate => false),
:provider_name => ems.name})
end
end
def self.model_name_from_emstype(emstype)
model_from_emstype(emstype).try(:name)
end
def self.model_from_emstype(emstype)
emstype = emstype.downcase
ExtManagementSystem.leaf_subclasses.detect { |k| k.ems_type == emstype }
end
def self.short_token
if self == ManageIQ::Providers::BaseManager
nil
elsif parent == ManageIQ::Providers
# "Infra"
name.demodulize.sub(/Manager$/, '')
elsif parent != Object
# "Vmware"
parent.name.demodulize
end
end
def self.short_name
if (t = short_token)
"Ems#{t}"
else
name
end
end
def self.base_manager
(ancestors.select { |klass| klass < ::ExtManagementSystem } - [::ManageIQ::Providers::BaseManager]).last
end
def self.db_name
base_manager.short_name
end
def self.provision_class(_via)
self::Provision
end
def self.provision_workflow_class
self::ProvisionWorkflow
end
# UI methods for determining availability of fields
def supports_port?
false
end
def supports_api_version?
false
end
def supports_security_protocol?
false
end
def supports_provider_id?
false
end
def supports_authentication?(authtype)
authtype.to_s == "default"
end
# UI method for determining which icon to show for a particular EMS
def image_name
emstype.downcase
end
def default_endpoint
default = endpoints.detect { |e| e.role == "default" }
default || endpoints.build(:role => "default")
end
# Takes multiple connection data
# endpoints, and authentications
def connection_configurations=(options)
options.each do |option|
add_connection_configuration_by_role(option)
end
delete_unused_connection_configurations(options)
end
def delete_unused_connection_configurations(options)
chosen_endpoints = options.map { |x| x.deep_symbolize_keys.fetch_path(:endpoint, :role).try(:to_sym) }.compact.uniq
existing_endpoints = endpoints.pluck(:role).map(&:to_sym)
# Delete endpoint that were not picked
roles_for_deletion = existing_endpoints - chosen_endpoints
endpoints.select { |x| x.role && roles_for_deletion.include?(x.role.to_sym) }.each(&:mark_for_destruction)
authentications.select { |x| x.authtype && roles_for_deletion.include?(x.authtype.to_sym) }.each(&:mark_for_destruction)
end
def connection_configurations
roles = endpoints.map(&:role)
options = {}
roles.each do |role|
conn = connection_configuration_by_role(role)
options[role] = conn
end
connections = OpenStruct.new(options)
connections.roles = roles
connections
end
# Takes a hash of connection data
# hostname, port, and authentication
# if no role is passed in assume is default role
def add_connection_configuration_by_role(connection)
connection.deep_symbolize_keys!
unless connection[:endpoint].key?(:role)
connection[:endpoint][:role] ||= "default"
end
if connection[:authentication].blank?
connection.delete(:authentication)
else
unless connection[:authentication].key?(:role)
endpoint_role = connection[:endpoint][:role]
authentication_role = endpoint_role == "default" ? default_authentication_type.to_s : endpoint_role
connection[:authentication][:role] ||= authentication_role
end
end
build_connection(connection)
end
def connection_configuration_by_role(role = "default")
endpoint = endpoints.detect { |e| e.role == role }
if endpoint
authtype = endpoint.role == "default" ? default_authentication_type.to_s : endpoint.role
auth = authentications.detect { |a| a.authtype == authtype }
options = {:endpoint => endpoint, :authentication => auth}
OpenStruct.new(options)
end
end
def hostnames
hostnames ||= endpoints.map(&:hostname)
hostnames
end
def authentication_check_role
'ems_operations'
end
def self.hostname_required?
true
end
delegate :hostname_required?, :to => :class
def my_zone
zone.try(:name).presence || MiqServer.my_zone
end
alias_method :zone_name, :my_zone
def emstype_description
self.class.description || emstype.titleize
end
def with_provider_connection(options = {})
raise _("no block given") unless block_given?
_log.info("Connecting through #{self.class.name}: [#{name}]")
yield connect(options)
end
def self.refresh_all_ems_timer
ems_ids = where(:zone_id => MiqServer.my_server.zone.id).pluck(:id)
refresh_ems(ems_ids, true) unless ems_ids.empty?
end
def self.refresh_ems(ems_ids, reload = false)
ems_ids = [ems_ids] unless ems_ids.kind_of?(Array)
ExtManagementSystem.where(:id => ems_ids).each { |ems| ems.reset_vim_cache_queue if ems.respond_to?(:reset_vim_cache_queue) } if reload
ems_ids = ems_ids.collect { |id| [ExtManagementSystem, id] }
EmsRefresh.queue_refresh(ems_ids)
end
def last_refresh_status
if last_refresh_date
last_refresh_error ? "error" : "success"
else
"never"
end
end
def refresh_ems(opts = {})
if missing_credentials?
raise _("no Provider credentials defined")
end
unless authentication_status_ok?
raise _("Provider failed last authentication check")
end
EmsRefresh.queue_refresh(self, nil, opts)
end
def self.ems_infra_discovery_types
@ems_infra_discovery_types ||= %w(virtualcenter scvmm rhevm)
end
def self.ems_physical_infra_discovery_types
@ems_physical_infra_discovery_types ||= %w(lenovo_ph_infra)
end
def disable!
_log.info("Disabling EMS [#{name}] id [#{id}].")
update!(:enabled => false)
end
def enable!
_log.info("Enabling EMS [#{name}] id [#{id}].")
update!(:enabled => true)
end
# override destroy_queue from AsyncDeleteMixin
def self.destroy_queue(ids)
find(Array.wrap(ids)).each(&:destroy_queue)
end
def destroy_queue
_log.info("Queuing destroy of #{self.class.name} with id: #{id}")
child_managers.each(&:destroy_queue)
self.class.schedule_destroy_queue(id)
end
def self.schedule_destroy_queue(id, deliver_on = nil)
MiqQueue.put(
:class_name => name,
:instance_id => id,
:method_name => "orchestrate_destroy",
:deliver_on => deliver_on,
)
end
# Wait until all associated workers are dead to destroy this ems
def orchestrate_destroy
disable! if enabled?
if self.destroy == false
_log.info("Cannot destroy #{self.class.name} with id: #{id}, workers still in progress. Requeuing destroy...")
self.class.schedule_destroy_queue(id, 15.seconds.from_now)
else
_log.info("#{self.class.name} with id: #{id} destroyed")
end
end
before_destroy :assert_no_queues_present
def assert_no_queues_present
throw(:abort) if MiqWorker.find_alive.where(:queue_name => queue_name).any?
end
def disconnect_inv
hosts.each { |h| h.disconnect_ems(self) }
vms.each { |v| v.disconnect_ems(self) }
ems_folders.destroy_all
ems_clusters.destroy_all
resource_pools.destroy_all
end
def queue_name
"ems_#{id}"
end
def enforce_policy(target, event)
inputs = {:ext_management_system => self}
inputs[:vm] = target if target.kind_of?(Vm)
inputs[:host] = target if target.kind_of?(Host)
MiqEvent.raise_evm_event(target, event, inputs)
end
alias_method :all_storages, :storages
alias_method :datastores, :storages # Used by web-services to return datastores as the property name
#
# Relationship methods
#
# Folder relationship methods
def ems_folder_root
folders.first
end
def folders
children(:of_type => 'EmsFolder').sort_by { |c| c.name.downcase }
end
alias_method :add_folder, :set_child
alias_method :remove_folder, :remove_child
def remove_all_folders
remove_all_children(:of_type => 'EmsFolder')
end
def get_folder_paths(folder = nil)
exclude_root_folder = folder.nil?
folder ||= ems_folder_root
return [] if folder.nil?
folder.child_folder_paths(
:exclude_root_folder => exclude_root_folder,
:exclude_datacenters => true,
:exclude_non_display_folders => true
)
end
def resource_pools_non_default
if @association_cache.include?(:resource_pools)
resource_pools.select { |r| !r.is_default }
else
resource_pools.where("is_default != ?", true).to_a
end
end
def event_where_clause(assoc = :ems_events)
["#{events_table_name(assoc)}.ems_id = ?", id]
end
def vm_count_by_state(state)
vms.inject(0) { |t, vm| vm.power_state == state ? t + 1 : t }
end
def total_vms_on; vm_count_by_state("on"); end
def total_vms_off; vm_count_by_state("off"); end
def total_vms_unknown; vm_count_by_state("unknown"); end
def total_vms_never; vm_count_by_state("never"); end
def total_vms_suspended; vm_count_by_state("suspended"); end
def supports_block_storage
supports_block_storage?
end
def supports_cloud_object_store_container_create
supports_cloud_object_store_container_create?
end
def get_reserve(field)
(hosts + ems_clusters).inject(0) { |v, obj| v + (obj.send(field) || 0) }
end
def cpu_reserve
get_reserve(:cpu_reserve)
end
def memory_reserve
get_reserve(:memory_reserve)
end
def vm_log_user_event(_vm, user_event)
$log.info(user_event)
$log.warn("User event logging is not available on [#{self.class.name}] Name:[#{name}]")
end
#
# Metric methods
#
PERF_ROLLUP_CHILDREN = :hosts
def perf_rollup_parents(interval_name = nil)
[MiqRegion.my_region].compact unless interval_name == 'realtime'
end
def perf_capture_enabled?
return @perf_capture_enabled unless @perf_capture_enabled.nil?
@perf_capture_enabled = ems_clusters.any?(&:perf_capture_enabled?) || host.any?(&:perf_capture_enabled?)
end
alias_method :perf_capture_enabled, :perf_capture_enabled?
Vmdb::Deprecation.deprecate_methods(self, :perf_capture_enabled => :perf_capture_enabled?)
###################################
# Event Monitor
###################################
def after_update_authentication
stop_event_monitor_queue_on_credential_change
end
def self.event_monitor_class
nil
end
delegate :event_monitor_class, :to => :class
def event_monitor
return if event_monitor_class.nil?
event_monitor_class.find_by_ems(self).first
end
def start_event_monitor
return if event_monitor_class.nil?
event_monitor_class.start_worker_for_ems(self)
end
def stop_event_monitor
return if event_monitor_class.nil?
_log.info("EMS [#{name}] id [#{id}]: Stopping event monitor.")
event_monitor_class.stop_worker_for_ems(self)
end
def stop_event_monitor_queue
MiqQueue.put_unless_exists(
:class_name => self.class.name,
:method_name => "stop_event_monitor",
:instance_id => id,
:priority => MiqQueue::HIGH_PRIORITY,
:zone => my_zone,
:role => "event"
)
end
def stop_event_monitor_queue_on_change
if event_monitor_class && !self.new_record? && default_endpoint.changed.include_any?("hostname", "ipaddress")
_log.info("EMS: [#{name}], Hostname or IP address has changed, stopping Event Monitor. It will be restarted by the WorkerMonitor.")
stop_event_monitor_queue
end
end
def stop_event_monitor_queue_on_credential_change
if event_monitor_class && !self.new_record? && self.credentials_changed?
_log.info("EMS: [#{name}], Credentials have changed, stopping Event Monitor. It will be restarted by the WorkerMonitor.")
stop_event_monitor_queue
end
end
def blacklisted_event_names
(
self.class.blacklisted_events.where(:enabled => true).pluck(:event_name) +
blacklisted_events.where(:enabled => true).pluck(:event_name)
).uniq.sort
end
def self.blacklisted_events
BlacklistedEvent.where(:provider_model => name, :ems_id => nil)
end
# @return [Boolean] true if a datastore exists for this type of ems
def self.datastore?
IsoDatastore.where(:ems_id => all).exists?
end
def tenant_identity
User.super_admin.tap { |u| u.current_group = tenant.default_miq_group }
end
private
def build_connection(options = {})
build_endpoint_by_role(options[:endpoint])
build_authentication_by_role(options[:authentication])
end
def build_endpoint_by_role(options)
return if options.blank?
endpoint = endpoints.detect { |e| e.role == options[:role].to_s }
if endpoint
endpoint.assign_attributes(options)
else
endpoints.build(options)
end
end
def build_authentication_by_role(options)
return if options.blank?
role = options.delete(:role)
creds = {}
creds[role] = options
update_authentication(creds,options)
end
def clear_association_cache
@storages = nil
super
end
end