2020-08-25 00:03:17 -04: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>
|
|
|
|
|
|
|
|
class SystemResource < Inspec.resource(1)
|
|
|
|
name 'system'
|
|
|
|
|
|
|
|
attr_reader :platform
|
|
|
|
|
|
|
|
def initialize
|
2020-11-24 15:51:05 -05:00
|
|
|
super
|
2020-08-25 00:03:17 -04:00
|
|
|
@platform = build_platform
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def build_platform
|
|
|
|
{
|
|
|
|
family: build_platform_family,
|
|
|
|
name: build_platform_name,
|
|
|
|
release: build_platform_release,
|
|
|
|
finger: build_platform_finger
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_platform_family
|
|
|
|
case inspec.platform[:name]
|
2020-12-21 19:43:11 -05:00
|
|
|
when 'arch', 'gentoo'
|
|
|
|
inspec.platform[:name]
|
2020-08-25 00:03:17 -04:00
|
|
|
else
|
|
|
|
inspec.platform[:family]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_platform_name
|
|
|
|
case inspec.platform[:name]
|
2020-12-21 19:43:11 -05:00
|
|
|
when 'amazon', 'oracle'
|
|
|
|
"#{inspec.platform[:name]}linux"
|
2021-03-23 17:33:36 -04:00
|
|
|
when /^windows_/
|
|
|
|
inspec.platform[:family]
|
2020-08-25 00:03:17 -04:00
|
|
|
else
|
|
|
|
inspec.platform[:name]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-01 02:24:11 -05:00
|
|
|
# rubocop:disable Metrics/MethodLength,Metrics/AbcSize,Metrics/CyclomaticComplexity
|
2020-08-25 00:03:17 -04:00
|
|
|
def build_platform_release
|
|
|
|
case inspec.platform[:name]
|
|
|
|
when 'amazon'
|
|
|
|
# `2018` relase is named `1` in kitchen.yaml
|
|
|
|
inspec.platform[:release].gsub(/2018.*/, '1')
|
|
|
|
when 'arch'
|
|
|
|
'base-latest'
|
2020-11-24 15:51:05 -05:00
|
|
|
when 'gentoo'
|
|
|
|
"#{inspec.platform[:release].split('.')[0]}-#{derive_gentoo_init_system}"
|
2021-02-01 02:24:11 -05: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-21 19:43:11 -05:00
|
|
|
when 'windows_8.1_pro'
|
|
|
|
'8.1'
|
|
|
|
when 'windows_server_2019_datacenter'
|
|
|
|
'2019-server'
|
2021-03-23 17:33:36 -04:00
|
|
|
when 'windows_server_2016_datacenter'
|
|
|
|
'2016-server'
|
2020-08-25 00:03:17 -04:00
|
|
|
else
|
|
|
|
inspec.platform[:release]
|
|
|
|
end
|
|
|
|
end
|
2021-02-01 02:24:11 -05:00
|
|
|
# rubocop:enable Metrics/MethodLength,Metrics/AbcSize,Metrics/CyclomaticComplexity
|
2020-08-25 00:03:17 -04:00
|
|
|
|
2020-11-24 15:51:05 -05:00
|
|
|
def derive_gentoo_init_system
|
2021-02-01 02:24:11 -05:00
|
|
|
inspec.command('systemctl').exist? ? 'sysd' : 'sysv'
|
2020-11-24 15:51:05 -05:00
|
|
|
end
|
|
|
|
|
2020-08-25 00:03:17 -04:00
|
|
|
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
|
|
|
|
end
|