Skip to content

Commit

Permalink
Add fallback solution for Last Reboot fact
Browse files Browse the repository at this point in the history
openSUSE dont support "uptime --since",
maybe others too, make a complicated one to
save them all.
  • Loading branch information
okopop committed Oct 23, 2024
1 parent 2ab3dc0 commit 6b76622
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ motd::warn: "This OS version is EOL since XX-YY-ZZ"
================== Motd by Puppet ==============================
Hostname.........: testhest.example.com
Platform.........: vmware, 2 CPU, 3.47 GiB RAM
Operatingsystem..: Red Hat Enterprise Linux release 8.10 (Ootpa)
Operating System.: Red Hat Enterprise Linux release 8.10 (Ootpa)
Puppet Agent.....: 7.18.0
Last reboot......: 2024-10-19 01:22
Information......: Product X | Production | Team Y
Expand Down
9 changes: 8 additions & 1 deletion lib/facter/motd_last_reboot.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Facter.add(:motd_last_reboot) do
# N.B. seconds can flip back and forth and create unwanted changes, skip them
setcode do
# This becomes empty when uptime command does not have "--since" implemented
lastboot = Facter::Core::Execution.execute('/usr/bin/uptime --since')
lastboot.slice(0..-4) # to 4th char from the end
if !lastboot.empty?
lastboot.slice(0..-4) # to 4th char from the end
else
# This is a solid fallback when uptime command does not work as expected
Facter::Core::Execution.execute('date -d "@$(($(date +%s) - $(awk \'{print int($1)}\' /proc/uptime)))" +"%Y-%m-%d %H:%M"')
end
end
end
2 changes: 1 addition & 1 deletion spec/classes/motd_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
context 'with default_facts.yml' do
it { is_expected.to contain_file('/etc/motd').with_content(%r{Hostname.........: testhest.example.com}) }
it { is_expected.to contain_file('/etc/motd').with_content(%r{Platform.........: vmware, 2 CPU, 3.64 GiB RAM}) }
it { is_expected.to contain_file('/etc/motd').with_content(%r{Operatingsystem..: }) }
it { is_expected.to contain_file('/etc/motd').with_content(%r{Operating System.: }) }
it { is_expected.to contain_file('/etc/motd').with_content(%r{Puppet Agent.....: 7.18.0}) }
it { is_expected.to contain_file('/etc/motd').with_content(%r{Last reboot......: \d{4}-\d{2}-\d{2}\s\d{2}:\d{2}}) }
end
Expand Down
2 changes: 1 addition & 1 deletion templates/motd.epp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
================== Motd by Puppet ==============================
Hostname.........: <%= $facts['networking']['fqdn'] %>
Platform.........: <%= $facts['virtual'] %>, <%= $facts['processors']['count'] %> CPU, <%= $facts['memory']['system']['total'] %> RAM
Operatingsystem..: <%= $facts['os']['distro']['description'] %>
Operating System.: <%= $facts['os']['distro']['description'] %>
Puppet Agent.....: <%= $facts['puppetversion'] %>
Last reboot......: <%= $motd_last_reboot %>
<% if $motd::info { -%>
Expand Down

0 comments on commit 6b76622

Please sign in to comment.