From 6fcba174e32dd05230dd91372a2bd76472602976 Mon Sep 17 00:00:00 2001 From: Ewoud Kohl van Wijngaarden Date: Wed, 13 Mar 2024 20:17:14 +0100 Subject: [PATCH] Only install timesync packages if needed 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. --- lib/beaker/host_prebuilt_steps.rb | 66 +++---------------------- lib/beaker/platform.rb | 55 +++++++++++++++++++++ spec/beaker/host_prebuilt_steps_spec.rb | 19 +++---- 3 files changed, 70 insertions(+), 70 deletions(-) diff --git a/lib/beaker/host_prebuilt_steps.rb b/lib/beaker/host_prebuilt_steps.rb index a08c87694..2db52ed55 100644 --- a/lib/beaker/host_prebuilt_steps.rb +++ b/lib/beaker/host_prebuilt_steps.rb @@ -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" @@ -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}" @@ -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, 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 @@ -102,44 +82,14 @@ def validate_host host, opts # @param [Host] host A host return the packages for # @return [Array] 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 diff --git a/lib/beaker/platform.rb b/lib/beaker/platform.rb index 5289d2c62..111e6a6ba 100644 --- a/lib/beaker/platform.rb +++ b/lib/beaker/platform.rb @@ -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] 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] 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 diff --git a/spec/beaker/host_prebuilt_steps_spec.rb b/spec/beaker/host_prebuilt_steps_spec.rb index c24e1ae06..cbe42beae 100644 --- a/spec/beaker/host_prebuilt_steps_spec.rb +++ b/spec/beaker/host_prebuilt_steps_spec.rb @@ -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 } } @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -339,7 +334,7 @@ it "can validate Fedora hosts" do host = make_host('host', { :stdout => stdout, :platform => 'fedora-32-x86_64'}) - 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 @@ -350,7 +345,7 @@ it "can validate Amazon hosts" do host = make_host('host', { :stdout => stdout, :platform => 'amazon-2023-x86_64'}) - 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