Saltstack Official OpenSSH Formula
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 line
2.1KB

  1. # frozen_string_literal: true
  2. # system.rb -- InSpec resources for system values
  3. # Author: Daniel Dehennin <daniel.dehennin@ac-dijon.fr>
  4. # Copyright (C) 2020 Daniel Dehennin <daniel.dehennin@ac-dijon.fr>
  5. HOSTNAME_CMDS = %w[hostname hostnamectl].freeze
  6. HOSTNAME_CMDS_OPT = {
  7. 'hostname' => '-s',
  8. 'hostnamectl' => '--static'
  9. }.freeze
  10. class SystemResource < Inspec.resource(1)
  11. name 'system'
  12. attr_reader :platform
  13. attr_reader :hostname
  14. def initialize
  15. @platform = build_platform
  16. @hostname = found_hostname
  17. end
  18. private
  19. def found_hostname
  20. cmd = guess_hostname_cmd
  21. unless cmd.exit_status.zero?
  22. raise Inspec::Exceptions::ResourceSkipped,
  23. "Error running '#{cmd}': #{cmd.stderr}"
  24. end
  25. cmd.stdout.chomp
  26. end
  27. def guess_hostname_cmd
  28. HOSTNAME_CMDS.each do |cmd|
  29. if inspec.command(cmd).exist?
  30. return inspec.command("#{cmd} #{HOSTNAME_CMDS_OPT[cmd]}")
  31. end
  32. end
  33. raise Inspec::Exceptions::ResourceSkipped,
  34. "Error: #{@platform[:finger]}} has none of #{HOSTNAME_CMDS.join(', ')}"
  35. end
  36. def build_platform
  37. {
  38. family: build_platform_family,
  39. name: build_platform_name,
  40. release: build_platform_release,
  41. finger: build_platform_finger
  42. }
  43. end
  44. def build_platform_family
  45. case inspec.platform[:name]
  46. when 'arch'
  47. 'arch'
  48. else
  49. inspec.platform[:family]
  50. end
  51. end
  52. def build_platform_name
  53. case inspec.platform[:name]
  54. when 'amazon'
  55. 'amazonlinux'
  56. else
  57. inspec.platform[:name]
  58. end
  59. end
  60. def build_platform_release
  61. case inspec.platform[:name]
  62. when 'amazon'
  63. # `2018` relase is named `1` in kitchen.yaml
  64. inspec.platform[:release].gsub(/2018.*/, '1')
  65. when 'arch'
  66. 'base-latest'
  67. else
  68. inspec.platform[:release]
  69. end
  70. end
  71. def build_platform_finger
  72. "#{build_platform_name}-#{build_finger_release}"
  73. end
  74. def build_finger_release
  75. case inspec.platform[:name]
  76. when 'ubuntu'
  77. build_platform_release.split('.').slice(0, 2).join('.')
  78. else
  79. build_platform_release.split('.')[0]
  80. end
  81. end
  82. end