4061c59c81d97c38d1e4d5055d04dcc114cf9cfd
[mirror/dsa-puppet.git] / 3rdparty / modules / systemd / lib / facter / systemd.rb
1 # Fact: systemd
2 #
3 # Purpose:
4 #   Determine whether systemd is the init system on the node
5 #
6 # Resolution:
7 #   Check if the service_provider fact is systemd
8 #
9 # Caveats:
10 #   If you override the service provider then it will return false, even if the
11 #   underlying system still is systemd.
12 #
13
14 # Fact: systemd_version
15 #
16 # Purpose:
17 #   Determine the version of systemd installed
18 #
19 # Resolution:
20 #   Check the output of systemctl --version
21 #
22 # Caveats:
23 #
24
25 # Fact: systemd_internal_services
26 #
27 # Purpose:
28 #   List all systemd internal real services + their state
29 #
30 # Resolution:
31 #   Check the output of systemctl --list-unit-files systemd-* and parse it into
32 #   a hash with the status
33 #
34 # Caveats:
35 #
36 Facter.add(:systemd) do
37   confine :kernel => :linux
38   setcode do
39     Facter.value(:service_provider) == 'systemd'
40   end
41 end
42
43 Facter.add(:systemd_version) do
44   confine :systemd => true
45   setcode do
46     Facter::Util::Resolution.exec("systemctl --version | awk '/systemd/{ print $2 }'")
47   end
48 end
49
50 Facter.add(:systemd_internal_services) do
51   confine :systemd => true
52   setcode do
53     command_output = Facter::Util::Resolution.exec(
54       'systemctl list-unit-files --no-legend --no-pager "systemd-*" -t service --state=enabled,disabled,enabled-runtime,indirect'
55     )
56     lines = command_output.lines.lazy.map { |line| line.split(/\s+/) }
57     lines.each_with_object({}) do |(service, status, *), result|
58       result[service] = status
59     end
60   end
61 end