Skip to content

Commit

Permalink
Only install timesync packages if needed
Browse files Browse the repository at this point in the history
Chrony/NTP is only needed if timesync is requested. In some cases (like
containers) it never makes sense.

Which packages are installed is really platform specific logic, so it
should live in the platform class.
  • Loading branch information
ekohl committed Jul 24, 2024
1 parent b83b7ac commit 594063b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 70 deletions.
66 changes: 8 additions & 58 deletions lib/beaker/host_prebuilt_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,6 @@ module HostPrebuiltSteps
NTPSERVER = 'pool.ntp.org'
SLEEPWAIT = 5
TRIES = 5
AMAZON2023_PACKAGES = %w[chrony]
RHEL8_PACKAGES = %w[chrony iputils] # iputils provides ping. beaker assumes that's present
FEDORA_PACKAGES = %w[chrony iputils]
UNIX_PACKAGES = %w[curl ntpdate]
FREEBSD_PACKAGES = ['curl', 'perl5|perl']
OPENBSD_PACKAGES = ['curl']
ARCHLINUX_PACKAGES = %w[curl ntp net-tools openssh]
WINDOWS_PACKAGES = ['curl']
PSWINDOWS_PACKAGES = []
SLES10_PACKAGES = ['curl']
SLES_PACKAGES = %w[curl ntp]
DEBIAN_PACKAGES = %w[curl ntpdate lsb-release apt-transport-https]
SOLARIS10_PACKAGES = %w[CSWcurl CSWntp wget]
SOLARIS11_PACKAGES = %w[curl ntp]
ETC_HOSTS_PATH = "/etc/hosts"
ETC_HOSTS_PATH_SOLARIS = "/etc/inet/hosts"
ROOT_KEYS_SCRIPT = "https://raw.githubusercontent.com/puppetlabs/puppetlabs-sshkeys/master/templates/scripts/manage_root_authorized_keys"
Expand All @@ -48,7 +34,7 @@ def timesync host, opts
host.exec(Command.new("w32tm /resync"))
logger.notify "NTP date succeeded on #{host}"
else
if /amazon|el-[89]|fedora/.match?(host['platform'])
if host['platform'].uses_chrony?
ntp_command = "chronyc add server #{ntp_server} prefer trust;chronyc makestep;chronyc burst 1/2"
elsif /opensuse-|sles-/.match?(host['platform'])
ntp_command = "sntp #{ntp_server}"
Expand Down Expand Up @@ -79,12 +65,6 @@ def timesync host, opts
# Validate that hosts are prepared to be used as SUTs, if packages are missing attempt to
# install them.
#
# Verifies the presence of #{HostPrebuiltSteps::UNIX_PACKAGES} on unix platform hosts,
# {HostPrebuiltSteps::SLES_PACKAGES} on SUSE platform hosts,
# {HostPrebuiltSteps::DEBIAN_PACKAGES} on debian platform hosts,
# {HostPrebuiltSteps::WINDOWS_PACKAGES} on cygwin-installed windows platform hosts,
# and {HostPrebuiltSteps::PSWINDOWS_PACKAGES} on non-cygwin windows platform hosts.
#
# @param [Host, Array<Host>, String, Symbol] host One or more hosts to act upon
# @param [Hash{Symbol=>String}] opts Options to alter execution.
# @option opts [Beaker::Logger] :logger A {Beaker::Logger} object
Expand All @@ -102,44 +82,14 @@ def validate_host host, opts
# @param [Host] host A host return the packages for
# @return [Array<String>] A list of packages to install
def host_packages(host)
case host['platform']
when /amazon/
AMAZON2023_PACKAGES
when /el-[89]/
RHEL8_PACKAGES
when /sles-10/
SLES10_PACKAGES
when /opensuse|sles-/
SLES_PACKAGES
when /debian/
DEBIAN_PACKAGES
when /windows/
if host.is_cygwin?
raise RuntimeError, "cygwin is not installed on #{host}" if !host.cygwin_installed?

WINDOWS_PACKAGES
else
PSWINDOWS_PACKAGES
end
when /freebsd/
FREEBSD_PACKAGES
when /openbsd/
OPENBSD_PACKAGES
when /solaris-10/
SOLARIS10_PACKAGES
when /solaris-1[1-9]/
SOLARIS11_PACKAGES
when /archlinux/
ARCHLINUX_PACKAGES
when /fedora/
FEDORA_PACKAGES
else
if !/aix|solaris|osx-/.match?(host['platform'])
UNIX_PACKAGES
else
[]
end
packages = host['platform'].base_packages
if host.is_cygwin?
raise RuntimeError, "cygwin is not installed on #{host}" if !host.cygwin_installed?

packages << 'curl'
end
packages += host['platform'].timesync_packages if host[:timesync]
packages
end

# Installs the given packages if they aren't already on a host
Expand Down
55 changes: 55 additions & 0 deletions lib/beaker/platform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,60 @@ def with_version_codename
def with_version_number
[@variant, @version, @arch].join('-')
end

def uses_chrony?
case @variant
when 'amazon', 'fedora'
true
when 'el'
@version.to_i >= 8
else
false
end
end

# Return a list of packages that should always be present.
#
# @return [Array<String>] A list of packages to install
def base_packages
case @variant
when 'el'
@version.to_i >= 8 ? ['curl-minimal', 'iputils'] : %w[curl]
when 'debian'
%w[curl lsb-release apt-transport-https]
when 'freebsd'
%w[curl perl5|perl]
when 'solaris'
@version.to_i >= 11 ? %w[curl] : %w[CSWcurl wget]
when 'archlinux'
%w[curl net-tools openssh]
when 'amazon', 'fedora'
['curl-minimal', 'iputils']
when 'aix', 'osx', 'windows'
[]
else
%w[curl]
end
end

# Return a list of packages that are needed for timesync
#
# @return [Array<String>] A list of packages to install for timesync
def timesync_packages
return ['chrony'] if uses_chrony?

case @variant
when 'freebsd', 'openbsd', 'windows', 'aix', 'osx'
[]
when 'archlinux', 'opensuse'
['ntp']
when 'sles'
@version.to_i >= 11 ? %w[ntp] : []
when 'solaris'
@version.to_i >= 11 ? %w[ntp] : %w[CSWntp]
else
%w[ntpdate]
end
end
end
end
19 changes: 7 additions & 12 deletions spec/beaker/host_prebuilt_steps_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
let(:options_ntp) { make_opts.merge({ 'ntp_server' => ntpserver_set }) }
let(:ntpserver) { Beaker::HostPrebuiltSteps::NTPSERVER }
let(:sync_cmd) { Beaker::HostPrebuiltSteps::ROOT_KEYS_SYNC_CMD }
let(:windows_pkgs) { Beaker::HostPrebuiltSteps::WINDOWS_PACKAGES }
let(:sles_only_pkgs) { Beaker::HostPrebuiltSteps::SLES_PACKAGES }
let(:rhel8_packages) { Beaker::HostPrebuiltSteps::RHEL8_PACKAGES }
let(:fedora_packages) { Beaker::HostPrebuiltSteps::FEDORA_PACKAGES }
let(:amazon2023_packages) { Beaker::HostPrebuiltSteps::AMAZON2023_PACKAGES }
let(:ip) { "ip.address.0.0" }
let(:stdout) { @stdout || ip }
let(:dummy_class) { Class.new { include Beaker::HostPrebuiltSteps } }
Expand Down Expand Up @@ -283,7 +278,7 @@
it "can validate el-9 hosts" do
host = make_host('host', { :stdout => stdout, :platform => 'el-9-64' })

rhel8_packages.each do |pkg|
['curl-minimal', 'iputils'].each do |pkg|
expect(host).to receive(:check_for_package).with(pkg).once.and_return(false)
expect(host).to receive(:install_package).with(pkg).once
end
Expand All @@ -295,7 +290,7 @@
host = make_host('host', { :stdout => stdout, :platform => 'windows-11-64', :is_cygwin => true })
allow(host).to receive(:cygwin_installed?).and_return(true)

windows_pkgs.each do |pkg|
['curl'].each do |pkg|
expect(host).to receive(:check_for_package).with(pkg).once.and_return(false)
expect(host).to receive(:install_package).with(pkg).once
end
Expand All @@ -306,7 +301,7 @@
it "can validate SLES hosts" do
host = make_host('host', { :stdout => stdout, :platform => 'sles-13.1-x86_64' })

sles_only_pkgs.each do |pkg|
['curl'].each do |pkg|
expect(host).to receive(:check_for_package).with(pkg).once.and_return(false)
expect(host).to receive(:install_package).with(pkg).once
end
Expand All @@ -317,7 +312,7 @@
it "can validate opensuse hosts" do
host = make_host('host', { :stdout => stdout, :platform => 'opensuse-15-x86_x64' })

sles_only_pkgs.each do |pkg|
['curl'].each do |pkg|
expect(host).to receive(:check_for_package).with(pkg).once.and_return(false)
expect(host).to receive(:install_package).with(pkg).once
end
Expand All @@ -328,7 +323,7 @@
it "can validate RHEL8 hosts" do
host = make_host('host', { :stdout => stdout, :platform => 'el-8-64' })

rhel8_packages.each do |pkg|
['curl-minimal', 'iputils'].each do |pkg|
expect(host).to receive(:check_for_package).with(pkg).once.and_return(false)
expect(host).to receive(:install_package).with(pkg).once
end
Expand All @@ -339,7 +334,7 @@
it "can validate Fedora hosts" do
host = make_host('host', { :stdout => stdout, :platform => 'fedora-32-x86_64'})

Check failure on line 335 in spec/beaker/host_prebuilt_steps_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

Layout/SpaceInsideHashLiteralBraces: Space inside } missing. (https://rubystyle.guide#spaces-braces)

fedora_packages.each do |pkg|
['curl-minimal', 'iputils'].each do |pkg|
expect(host).to receive(:check_for_package).with(pkg).once.and_return(false)
expect(host).to receive(:install_package).with(pkg).once
end
Expand All @@ -350,7 +345,7 @@
it "can validate Amazon hosts" do
host = make_host('host', { :stdout => stdout, :platform => 'amazon-2023-x86_64'})

Check failure on line 346 in spec/beaker/host_prebuilt_steps_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

Layout/SpaceInsideHashLiteralBraces: Space inside } missing. (https://rubystyle.guide#spaces-braces)

amazon2023_packages.each do |pkg|
['curl-minimal', 'iputils'].each do |pkg|
expect(host).to receive(:check_for_package).with(pkg).once.and_return(false)
expect(host).to receive(:install_package).with(pkg).once
end
Expand Down

0 comments on commit 594063b

Please sign in to comment.