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 Mar 14, 2024
1 parent 9e10133 commit e3ec2d5
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 99 deletions.
67 changes: 4 additions & 63 deletions lib/beaker/host_prebuilt_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,6 @@ module HostPrebuiltSteps
NTPSERVER = 'pool.ntp.org'
SLEEPWAIT = 5
TRIES = 5
AMAZON2023_PACKAGES = %w[chrony]
RHEL8_PACKAGES = %w[chrony]
FEDORA_PACKAGES = %w[chrony]
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]
CUMULUS_PACKAGES = %w[curl ntpdate]
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 @@ -49,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 @@ -83,13 +68,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::CUMULUS_PACKAGES} on cumulus 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 @@ -107,46 +85,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 /cumulus/
CUMULUS_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-|f5-|netscaler|cisco_/.match?(host['platform'])
UNIX_PACKAGES
else
[]
end
end
packages = host['platform'].base_packages
packages += host['platform'].timesync_packages if host[:timesync]
packages

Check warning on line 90 in lib/beaker/host_prebuilt_steps.rb

View check run for this annotation

Codecov / codecov/patch

lib/beaker/host_prebuilt_steps.rb#L88-L90

Added lines #L88 - L90 were not covered by tests
end

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

def uses_chrony?
case @variant

Check warning on line 133 in lib/beaker/platform.rb

View check run for this annotation

Codecov / codecov/patch

lib/beaker/platform.rb#L133

Added line #L133 was not covered by tests
when 'amazon', 'fedora'
true

Check warning on line 135 in lib/beaker/platform.rb

View check run for this annotation

Codecov / codecov/patch

lib/beaker/platform.rb#L135

Added line #L135 was not covered by tests
when 'el'
@version.to_i >= 8

Check warning on line 137 in lib/beaker/platform.rb

View check run for this annotation

Codecov / codecov/patch

lib/beaker/platform.rb#L137

Added line #L137 was not covered by tests
else
false

Check warning on line 139 in lib/beaker/platform.rb

View check run for this annotation

Codecov / codecov/patch

lib/beaker/platform.rb#L139

Added line #L139 was not covered by tests
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

Check warning on line 147 in lib/beaker/platform.rb

View check run for this annotation

Codecov / codecov/patch

lib/beaker/platform.rb#L147

Added line #L147 was not covered by tests
when 'el'
@version.to_i >= 8 ? [] : %w[curl]

Check warning on line 149 in lib/beaker/platform.rb

View check run for this annotation

Codecov / codecov/patch

lib/beaker/platform.rb#L149

Added line #L149 was not covered by tests
when 'debian'
%w[curl lsb-release apt-transport-https]

Check warning on line 151 in lib/beaker/platform.rb

View check run for this annotation

Codecov / codecov/patch

lib/beaker/platform.rb#L151

Added line #L151 was not covered by tests
when 'windows'
if host.is_cygwin?
raise RuntimeError, "cygwin is not installed on #{host}" if !host.cygwin_installed?

Check warning on line 154 in lib/beaker/platform.rb

View check run for this annotation

Codecov / codecov/patch

lib/beaker/platform.rb#L153-L154

Added lines #L153 - L154 were not covered by tests

%w[curl]

Check warning on line 156 in lib/beaker/platform.rb

View check run for this annotation

Codecov / codecov/patch

lib/beaker/platform.rb#L156

Added line #L156 was not covered by tests
else
[]

Check warning on line 158 in lib/beaker/platform.rb

View check run for this annotation

Codecov / codecov/patch

lib/beaker/platform.rb#L158

Added line #L158 was not covered by tests
end
when 'freebsd'
%w[curl perl5|perl]

Check warning on line 161 in lib/beaker/platform.rb

View check run for this annotation

Codecov / codecov/patch

lib/beaker/platform.rb#L161

Added line #L161 was not covered by tests
when 'solaris'
@version.to_i >= 11 ? %w[curl] : %w[CSWcurl wget]

Check warning on line 163 in lib/beaker/platform.rb

View check run for this annotation

Codecov / codecov/patch

lib/beaker/platform.rb#L163

Added line #L163 was not covered by tests
when 'archlinux'
%w[curl net-tools openssh]

Check warning on line 165 in lib/beaker/platform.rb

View check run for this annotation

Codecov / codecov/patch

lib/beaker/platform.rb#L165

Added line #L165 was not covered by tests
when 'amazon', 'fedora', 'aix', 'osx', 'f5', 'netscaler', /cisco_/
[]

Check warning on line 167 in lib/beaker/platform.rb

View check run for this annotation

Codecov / codecov/patch

lib/beaker/platform.rb#L167

Added line #L167 was not covered by tests
else
%w[curl]

Check warning on line 169 in lib/beaker/platform.rb

View check run for this annotation

Codecov / codecov/patch

lib/beaker/platform.rb#L169

Added line #L169 was not covered by tests
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?

Check warning on line 177 in lib/beaker/platform.rb

View check run for this annotation

Codecov / codecov/patch

lib/beaker/platform.rb#L177

Added line #L177 was not covered by tests

case @variant

Check warning on line 179 in lib/beaker/platform.rb

View check run for this annotation

Codecov / codecov/patch

lib/beaker/platform.rb#L179

Added line #L179 was not covered by tests
when 'freebsd', 'openbsd', 'windows', 'aix', 'osx', 'f5', 'netscaler', /cisco_/
[]

Check warning on line 181 in lib/beaker/platform.rb

View check run for this annotation

Codecov / codecov/patch

lib/beaker/platform.rb#L181

Added line #L181 was not covered by tests
when 'archlinux', 'opensuse'
['ntp']

Check warning on line 183 in lib/beaker/platform.rb

View check run for this annotation

Codecov / codecov/patch

lib/beaker/platform.rb#L183

Added line #L183 was not covered by tests
when 'sles'
@version.to_i >= 11 ? %w[ntp] : []

Check warning on line 185 in lib/beaker/platform.rb

View check run for this annotation

Codecov / codecov/patch

lib/beaker/platform.rb#L185

Added line #L185 was not covered by tests
when 'solaris'
@version.to_i >= 11 ? %w[ntp] : %w[CSWntp]

Check warning on line 187 in lib/beaker/platform.rb

View check run for this annotation

Codecov / codecov/patch

lib/beaker/platform.rb#L187

Added line #L187 was not covered by tests
else
%w[ntpdate]

Check warning on line 189 in lib/beaker/platform.rb

View check run for this annotation

Codecov / codecov/patch

lib/beaker/platform.rb#L189

Added line #L189 was not covered by tests
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 @@ -296,12 +290,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 @@ -310,12 +304,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 @@ -324,25 +317,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 @@ -351,10 +344,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 @@ -364,10 +355,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 @@ -377,10 +366,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 e3ec2d5

Please sign in to comment.