Skip to content

Commit

Permalink
find_many: find_many! fails, retry one by one
Browse files Browse the repository at this point in the history
  • Loading branch information
mvidner committed Sep 4, 2017
1 parent 3cb8e7e commit 57e1b38
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions library/systemd/src/modules/systemd_service.rb
Expand Up @@ -66,6 +66,8 @@ def initialize(service_name)
# service = Yast::SystemdService.find('sshd', :type=>'Type')
# service.properties.type # 'simple'
class SystemdServiceClass < Module
include Yast::Logger

UNIT_SUFFIX = ".service".freeze

# @param service_name [String] "foo" or "foo.service"
Expand All @@ -86,15 +88,18 @@ def find!(service_name, propmap = {})
find(service_name, propmap) || raise(SystemdServiceNotFound, service_name)
end

def find_many(service_names, propmap = {})
# FIXME: move this to SystemdUnit / Service ?
# @param service_names [Array<String>] "foo" or "foo.service"
# @param propmap [SystemdUnit::PropMap]
# @return [Array<Service>]
# @raise [SystemdServiceNotFound] if an unexpected problem occurs
def find_many!(service_names, propmap = {})
snames = service_names.map { |n| n + UNIT_SUFFIX unless n.end_with?(UNIT_SUFFIX) }
snames_s = snames.join(" ")
pnames_s = SystemdUnit::DEFAULT_PROPMAP.merge(propmap).values.join(",")
out = Systemctl.execute("show --property=#{pnames_s} #{snames_s}")
raise "FIXME: #{out.exit}, #{out.stderr}" unless out.exit.zero? && out.stderr.empty?
log.error "returned #{out.exit}, #{out.stderr}" unless out.exit.zero? && out.stderr.empty?
property_texts = out.stdout.split("\n\n")
raise "FIXME MISMATCH" unless snames.size == property_texts.size
raise(SystemdServiceNotFound, "?") unless snames.size == property_texts.size

snames.zip(property_texts).map do |service_name, property_text|
service = Service.new(service_name, propmap, property_text)
Expand All @@ -103,6 +108,16 @@ def find_many(service_names, propmap = {})
end
end

# @param service_names [Array<String>] "foo" or "foo.service"
# @param propmap [SystemdUnit::PropMap]
# @return [Array<Service,nil>]
def find_many(service_names, propmap = {})
find_many!(service_names, propmap)
rescue SystemdServiceNotFound
log.info "Retrying one by one"
service_names.map { |n| find(n, propmap) }
end

# @param propmap [SystemdUnit::PropMap]
# @return [Array<Service>]
def all(propmap = {})
Expand Down

0 comments on commit 57e1b38

Please sign in to comment.