Skip to content

Commit

Permalink
Merge pull request #22915 from agrare/set_system_uid_for_systemd_and_…
Browse files Browse the repository at this point in the history
…process

Set system_uid for systemd workers
  • Loading branch information
Fryguy authored Feb 27, 2024
2 parents e883fa0 + f54cc63 commit b646694
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
8 changes: 4 additions & 4 deletions app/models/miq_server/worker_management/systemd.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class MiqServer::WorkerManagement::Systemd < MiqServer::WorkerManagement
def sync_from_system
self.miq_services = systemd_services.select { |unit| manageiq_service?(unit) }
self.miq_services_by_guid = miq_services.index_by { |w| w[:name].match(/^.+@(?<guid>.+)\.service$/).named_captures["guid"] }
self.miq_services = systemd_services.select { |unit| manageiq_service?(unit) }
self.miq_services_by_unit = miq_services.index_by { |w| w[:name] }
end

def sync_starting_workers
Expand All @@ -10,7 +10,7 @@ def sync_starting_workers
starting.each do |worker|
next if worker.class.rails_worker?

systemd_worker = miq_services_by_guid[worker[:guid]]
systemd_worker = miq_services_by_unit[worker[:system_uid]]
if systemd_worker[:load_state] == "loaded" && systemd_worker[:active_state] == "active" && systemd_worker[:sub_state] == "running"
worker.update!(:status => "started")
starting.delete(worker)
Expand All @@ -36,7 +36,7 @@ def cleanup_failed_systemd_services

private

attr_accessor :miq_services, :miq_services_by_guid
attr_accessor :miq_services, :miq_services_by_unit

def systemd_manager
@systemd_manager ||= begin
Expand Down
7 changes: 6 additions & 1 deletion app/models/miq_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ class MiqWorker < ApplicationRecord
include SystemdCommon
include UuidMixin

before_destroy :error_out_tasks_with_active_queue_message, :log_destroy_of_worker_messages
after_initialize :set_system_uid
before_destroy :error_out_tasks_with_active_queue_message, :log_destroy_of_worker_messages

belongs_to :miq_server
has_many :messages, :as => :handler, :class_name => 'MiqQueue'
Expand Down Expand Up @@ -483,6 +484,10 @@ def clean_active_messages
end
end

private def set_system_uid
self.system_uid = unit_name if systemd_worker?
end

private def error_out_tasks_with_active_queue_message
message = "Task Handler: [#{friendly_name}] ID [#{id}] has been deleted!"
processed_messages.includes(:miq_task).where.not(:miq_task_id => nil).each do |m|
Expand Down
16 changes: 12 additions & 4 deletions app/models/miq_worker/systemd_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,23 @@ def service_base_name
"manageiq-#{minimal_class_name.underscore.tr("/", "_")}"
end

def service_file
"#{service_base_name}@.service"
end

def target_file
"#{service_base_name}.target"
end

def systemd_unit_dir
Pathname.new("/lib/systemd/system")
end
end

def unit_name
"#{service_base_name}#{unit_instance}.service"
end

def start_systemd_worker
enable_systemd_unit
write_unit_settings_file
Expand Down Expand Up @@ -77,10 +89,6 @@ def service_base_name
self.class.service_base_name
end

def unit_name
"#{service_base_name}#{unit_instance}.service"
end

def unit_instance
"@#{guid}"
end
Expand Down
7 changes: 1 addition & 6 deletions spec/models/miq_worker/systemd_common_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@
expected_units -= %w[manageiq-db-ready.service manageiq-messaging-ready.service evmserverd.service manageiq.target]

found_units = MiqWorkerType.worker_classes.flat_map do |worker_class|
service_base_name = worker_class.service_base_name

service_file = "#{service_base_name}@.service"
target_file = "#{service_base_name}.target"

[service_file, target_file]
[worker_class.service_file, worker_class.target_file]
end

expect(found_units).to match_array(expected_units)
Expand Down

0 comments on commit b646694

Please sign in to comment.