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 3d564e8 commit 27c5b04
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 95 deletions.
63 changes: 4 additions & 59 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,9 @@ 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
end
packages = host['platform'].base_packages
packages += host['platform'].timesync_packages if host[:timesync]
packages
end

# Installs the given packages if they aren't already on a host
Expand Down
63 changes: 63 additions & 0 deletions lib/beaker/platform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,68 @@ 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-minmal', 'iputils'] : %w[curl]
when 'debian'
%w[curl lsb-release apt-transport-https]
when 'windows'
if host.is_cygwin?
raise RuntimeError, "cygwin is not installed on #{host}" if !host.cygwin_installed?

%w[curl]
else
[]
end
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'
[]
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
59 changes: 23 additions & 36 deletions spec/beaker/host_prebuilt_steps_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +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(:unix_only_pkgs) { Beaker::HostPrebuiltSteps::UNIX_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(:platform) { @platform || 'unix' }
let(:ip) { "ip.address.0.0" }
let(:stdout) { @stdout || ip }
Expand Down Expand Up @@ -292,12 +286,12 @@
subject { dummy_class.new }

it "can validate unix hosts" do
# rubocop:disable RSpec/IteratedExpectation
hosts.each do |host|
unix_only_pkgs.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
expect(host).to receive(:check_for_package).with('curl').once.and_return(false)
expect(host).to receive(:install_package).with('curl').once
end
# rubocop:enable RSpec/IteratedExpectation

subject.validate_host(hosts, options)
end
Expand All @@ -306,12 +300,11 @@
@platform = 'windows'

hosts.each do |host|
windows_pkgs.each do |pkg|
allow(host).to receive(:cygwin_installed?).and_return(true)
allow(host).to receive(:is_cygwin?).and_return(true)
expect(host).to receive(:check_for_package).with(pkg).once.and_return(false)
expect(host).to receive(:install_package).with(pkg).once
end
pkg = 'curl'
allow(host).to receive(:cygwin_installed?).and_return(true)
allow(host).to receive(:is_cygwin?).and_return(true)
expect(host).to receive(:check_for_package).with(pkg).once.and_return(false)
expect(host).to receive(:install_package).with(pkg).once
end

subject.validate_host(hosts, options)
Expand All @@ -320,25 +313,25 @@
it "can validate SLES hosts" do
@platform = 'sles-13.1-x64'

# rubocop:disable RSpec/IteratedExpectation
hosts.each do |host|
sles_only_pkgs.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
expect(host).to receive(:check_for_package).with('curl').once.and_return(false)
expect(host).to receive(:install_package).with('curl').once
end
# rubocop:enable RSpec/IteratedExpectation

subject.validate_host(hosts, options)
end

it "can validate opensuse hosts" do
@platform = 'opensuse-15-x86_x64'

# rubocop:disable RSpec/IteratedExpectation
hosts.each do |host|
sles_only_pkgs.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
expect(host).to receive(:check_for_package).with('curl').once.and_return(false)
expect(host).to receive(:install_package).with('curl').once
end
# rubocop:enable RSpec/IteratedExpectation

subject.validate_host(hosts, options)
end
Expand All @@ -347,10 +340,8 @@
@platform = 'el-8-x86_x64'

hosts.each do |host|
rhel8_packages.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
expect(host).not_to receive(:check_for_package)
expect(host).not_to receive(:install_package)
end

subject.validate_host(hosts, options)
Expand All @@ -360,10 +351,8 @@
@platform = 'fedora-32-x86_64'

hosts.each do |host|
fedora_packages.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
expect(host).not_to receive(:check_for_package)
expect(host).not_to receive(:install_package)
end

subject.validate_host(hosts, options)
Expand All @@ -373,10 +362,8 @@
@platform = 'amazon-2023-x86_64'

hosts.each do |host|
amazon2023_packages.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
expect(host).not_to receive(:check_for_package)
expect(host).not_to receive(:install_package)
end

subject.validate_host(hosts, options)
Expand Down

0 comments on commit 27c5b04

Please sign in to comment.