2020-12-23 01:02:25 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# system.rb -- InSpec resources for system values
|
|
|
|
# Author: Daniel Dehennin <daniel.dehennin@ac-dijon.fr>
|
|
|
|
# Copyright (C) 2020 Daniel Dehennin <daniel.dehennin@ac-dijon.fr>
|
|
|
|
|
2022-02-14 09:08:45 +01:00
|
|
|
# rubocop:disable Metrics/ClassLength
|
2020-12-23 01:02:25 +01:00
|
|
|
class SystemResource < Inspec.resource(1)
|
|
|
|
name 'system'
|
|
|
|
|
|
|
|
attr_reader :platform
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
super
|
|
|
|
@platform = build_platform
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def build_platform
|
|
|
|
{
|
|
|
|
family: build_platform_family,
|
|
|
|
name: build_platform_name,
|
|
|
|
release: build_platform_release,
|
2022-02-14 09:08:45 +01:00
|
|
|
finger: build_platform_finger,
|
|
|
|
codename: build_platform_codename
|
2020-12-23 01:02:25 +01:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_platform_family
|
|
|
|
case inspec.platform[:name]
|
|
|
|
when 'arch', 'gentoo'
|
|
|
|
inspec.platform[:name]
|
|
|
|
else
|
|
|
|
inspec.platform[:family]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_platform_name
|
|
|
|
case inspec.platform[:name]
|
2021-07-02 01:07:43 +02:00
|
|
|
when 'amazon', 'oracle', 'rocky'
|
2020-12-23 01:02:25 +01:00
|
|
|
"#{inspec.platform[:name]}linux"
|
2021-03-23 22:33:02 +01:00
|
|
|
when /^windows_/
|
|
|
|
inspec.platform[:family]
|
2020-12-23 01:02:25 +01:00
|
|
|
else
|
|
|
|
inspec.platform[:name]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-29 09:56:07 +01:00
|
|
|
# rubocop:disable Metrics/MethodLength,Metrics/AbcSize,Metrics/CyclomaticComplexity
|
2020-12-23 01:02:25 +01:00
|
|
|
def build_platform_release
|
|
|
|
case inspec.platform[:name]
|
|
|
|
when 'amazon'
|
2021-07-21 19:33:52 +02:00
|
|
|
# `2018` relase is named `1` in `kitchen.yml`
|
2020-12-23 01:02:25 +01:00
|
|
|
inspec.platform[:release].gsub(/2018.*/, '1')
|
|
|
|
when 'arch'
|
|
|
|
'base-latest'
|
|
|
|
when 'gentoo'
|
|
|
|
"#{inspec.platform[:release].split('.')[0]}-#{derive_gentoo_init_system}"
|
2022-05-17 09:51:42 +02:00
|
|
|
when 'mac_os_x'
|
|
|
|
inspec.command('sw_vers -productVersion').stdout.to_s
|
2021-01-29 09:56:07 +01:00
|
|
|
when 'opensuse'
|
|
|
|
# rubocop:disable Style/NumericLiterals,Layout/LineLength
|
|
|
|
inspec.platform[:release].to_i > 20210101 ? 'tumbleweed' : inspec.platform[:release]
|
|
|
|
# rubocop:enable Style/NumericLiterals,Layout/LineLength
|
2020-12-23 01:02:25 +01:00
|
|
|
when 'windows_8.1_pro'
|
|
|
|
'8.1'
|
2021-12-22 09:02:22 +01:00
|
|
|
when 'windows_server_2022_datacenter'
|
|
|
|
'2022-server'
|
2020-12-23 01:02:25 +01:00
|
|
|
when 'windows_server_2019_datacenter'
|
|
|
|
'2019-server'
|
2021-03-23 22:33:02 +01:00
|
|
|
when 'windows_server_2016_datacenter'
|
|
|
|
'2016-server'
|
2020-12-23 01:02:25 +01:00
|
|
|
else
|
|
|
|
inspec.platform[:release]
|
|
|
|
end
|
|
|
|
end
|
2021-01-29 09:56:07 +01:00
|
|
|
# rubocop:enable Metrics/MethodLength,Metrics/AbcSize,Metrics/CyclomaticComplexity
|
2020-12-23 01:02:25 +01:00
|
|
|
|
|
|
|
def derive_gentoo_init_system
|
2021-01-29 09:56:07 +01:00
|
|
|
inspec.command('systemctl').exist? ? 'sysd' : 'sysv'
|
2020-12-23 01:02:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def build_platform_finger
|
|
|
|
"#{build_platform_name}-#{build_finger_release}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_finger_release
|
|
|
|
case inspec.platform[:name]
|
|
|
|
when 'ubuntu'
|
|
|
|
build_platform_release.split('.').slice(0, 2).join('.')
|
|
|
|
else
|
|
|
|
build_platform_release.split('.')[0]
|
|
|
|
end
|
|
|
|
end
|
2022-02-14 09:08:45 +01:00
|
|
|
|
|
|
|
# rubocop:disable Metrics/MethodLength,Metrics/CyclomaticComplexity
|
|
|
|
def build_platform_codename
|
|
|
|
case build_platform_finger
|
2022-07-17 20:48:06 +02:00
|
|
|
when 'ubuntu-22.04'
|
|
|
|
'jammy'
|
2022-02-14 09:08:45 +01:00
|
|
|
when 'ubuntu-20.04'
|
|
|
|
'focal'
|
|
|
|
when 'ubuntu-18.04'
|
|
|
|
'bionic'
|
|
|
|
when 'debian-11'
|
|
|
|
'bullseye'
|
|
|
|
when 'debian-10'
|
|
|
|
'buster'
|
|
|
|
when 'debian-9'
|
|
|
|
'stretch'
|
|
|
|
when 'almalinux-8'
|
|
|
|
"AlmaLinux #{build_platform_release} (Arctic Sphynx)"
|
|
|
|
when 'amazonlinux-2'
|
|
|
|
'Amazon Linux 2'
|
|
|
|
when 'arch-base-latest'
|
|
|
|
'Arch Linux'
|
|
|
|
when 'centos-7'
|
|
|
|
'CentOS Linux 7 (Core)'
|
|
|
|
when 'centos-8'
|
|
|
|
'CentOS Stream 8'
|
|
|
|
when 'opensuse-tumbleweed'
|
|
|
|
'openSUSE Tumbleweed'
|
|
|
|
when 'opensuse-15'
|
|
|
|
"openSUSE Leap #{build_platform_release}"
|
|
|
|
when 'oraclelinux-8', 'oraclelinux-7'
|
|
|
|
"Oracle Linux Server #{build_platform_release}"
|
|
|
|
when 'gentoo-2-sysd', 'gentoo-2-sysv'
|
|
|
|
'Gentoo/Linux'
|
|
|
|
when 'rockylinux-8'
|
|
|
|
"Rocky Linux #{build_platform_release} (Green Obsidian)"
|
|
|
|
else
|
|
|
|
''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
# rubocop:enable Metrics/MethodLength,Metrics/CyclomaticComplexity
|
2020-12-23 01:02:25 +01:00
|
|
|
end
|
2022-02-14 09:08:45 +01:00
|
|
|
# rubocop:enable Metrics/ClassLength
|