diff --git a/.fixtures.yml b/.fixtures.yml index 76f4e25..97f036d 100644 --- a/.fixtures.yml +++ b/.fixtures.yml @@ -2,5 +2,6 @@ fixtures: repositories: stdlib: "https://github.com/puppetlabs/puppetlabs-stdlib.git" concat: "https://github.com/puppetlabs/puppetlabs-concat.git" + cron: "https://github.com/puppetlabs/puppetlabs-cron_core.git" symlinks: check_mk: "#{source_dir}" diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..9032a01 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,5 @@ +*.rb eol=lf +*.erb eol=lf +*.pp eol=lf +*.sh eol=lf +*.epp eol=lf diff --git a/.gitignore b/.gitignore index 6ee20c2..2767022 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,27 @@ -pkg/ -*.swp +.git/ +.*.sw[op] +.metadata +.yardoc +.yardwarns +*.iml +/.bundle/ +/.idea/ +/.vagrant/ +/coverage/ +/bin/ +/doc/ +/Gemfile.local +/Gemfile.lock +/junit/ +/log/ +/pkg/ +/spec/fixtures/manifests/ +/spec/fixtures/modules/ +/tmp/ +/vendor/ +/convert_report.txt +/update_report.txt +.DS_Store +.project +.envrc +/inventory.yaml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..81e6d76 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,41 @@ +--- +stages: + - syntax + - unit + +cache: + paths: + - vendor/bundle + +before_script: + - bundle -v + - rm Gemfile.lock || true + - gem update --system $RUBYGEMS_VERSION + - gem --version + - bundle -v + - bundle install --without system_tests --path vendor/bundle --jobs $(nproc) + +syntax lint metadata_lint check:symlinks check:git_ignore check:dot_underscore check:test_file rubocop-Ruby 2.5.3-Puppet ~> 6: + stage: syntax + image: ruby:2.5.3 + script: + - bundle exec rake syntax lint metadata_lint check:symlinks check:git_ignore check:dot_underscore check:test_file rubocop + variables: + PUPPET_GEM_VERSION: '~> 6' + +parallel_spec-Ruby 2.5.3-Puppet ~> 6: + stage: unit + image: ruby:2.5.3 + script: + - bundle exec rake parallel_spec + variables: + PUPPET_GEM_VERSION: '~> 6' + +parallel_spec-Ruby 2.4.5-Puppet ~> 5: + stage: unit + image: ruby:2.4.5 + script: + - bundle exec rake parallel_spec + variables: + PUPPET_GEM_VERSION: '~> 5' + diff --git a/.pdkignore b/.pdkignore new file mode 100644 index 0000000..e6215cd --- /dev/null +++ b/.pdkignore @@ -0,0 +1,42 @@ +.git/ +.*.sw[op] +.metadata +.yardoc +.yardwarns +*.iml +/.bundle/ +/.idea/ +/.vagrant/ +/coverage/ +/bin/ +/doc/ +/Gemfile.local +/Gemfile.lock +/junit/ +/log/ +/pkg/ +/spec/fixtures/manifests/ +/spec/fixtures/modules/ +/tmp/ +/vendor/ +/convert_report.txt +/update_report.txt +.DS_Store +.project +.envrc +/inventory.yaml +/appveyor.yml +/.fixtures.yml +/Gemfile +/.gitattributes +/.gitignore +/.gitlab-ci.yml +/.pdkignore +/Rakefile +/rakelib/ +/.rspec +/.rubocop.yml +/.travis.yml +/.yardopts +/spec/ +/.vscode/ diff --git a/.puppet-lint.rc b/.puppet-lint.rc new file mode 100644 index 0000000..cc96ece --- /dev/null +++ b/.puppet-lint.rc @@ -0,0 +1 @@ +--relative diff --git a/.rspec b/.rspec index 0357065..624a7f7 100644 --- a/.rspec +++ b/.rspec @@ -1,3 +1,2 @@ ---color ---format documentation -progress +--color +--format documentation diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..617d14b --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,122 @@ +--- +require: rubocop-rspec +AllCops: + DisplayCopNames: true + TargetRubyVersion: '2.1' + Include: + - "./**/*.rb" + Exclude: + - bin/* + - ".vendor/**/*" + - "**/Gemfile" + - "**/Rakefile" + - pkg/**/* + - spec/fixtures/**/* + - vendor/**/* + - "**/Puppetfile" + - "**/Vagrantfile" + - "**/Guardfile" +Metrics/LineLength: + Description: People have wide screens, use them. + Max: 200 +GetText/DecorateString: + Description: We don't want to decorate test output. + Exclude: + - spec/* +RSpec/BeforeAfterAll: + Description: Beware of using after(:all) as it may cause state to leak between tests. + A necessary evil in acceptance testing. + Exclude: + - spec/acceptance/**/*.rb +RSpec/HookArgument: + Description: Prefer explicit :each argument, matching existing module's style + EnforcedStyle: each +Style/BlockDelimiters: + Description: Prefer braces for chaining. Mostly an aesthetical choice. Better to + be consistent then. + EnforcedStyle: braces_for_chaining +Style/ClassAndModuleChildren: + Description: Compact style reduces the required amount of indentation. + EnforcedStyle: compact +Style/EmptyElse: + Description: Enforce against empty else clauses, but allow `nil` for clarity. + EnforcedStyle: empty +Style/FormatString: + Description: Following the main puppet project's style, prefer the % format format. + EnforcedStyle: percent +Style/FormatStringToken: + Description: Following the main puppet project's style, prefer the simpler template + tokens over annotated ones. + EnforcedStyle: template +Style/Lambda: + Description: Prefer the keyword for easier discoverability. + EnforcedStyle: literal +Style/RegexpLiteral: + Description: Community preference. See https://github.com/voxpupuli/modulesync_config/issues/168 + EnforcedStyle: percent_r +Style/TernaryParentheses: + Description: Checks for use of parentheses around ternary conditions. Enforce parentheses + on complex expressions for better readability, but seriously consider breaking + it up. + EnforcedStyle: require_parentheses_when_complex +Style/TrailingCommaInArguments: + Description: Prefer always trailing comma on multiline argument lists. This makes + diffs, and re-ordering nicer. + EnforcedStyleForMultiline: comma +Style/TrailingCommaInLiteral: + Description: Prefer always trailing comma on multiline literals. This makes diffs, + and re-ordering nicer. + EnforcedStyleForMultiline: comma +Style/SymbolArray: + Description: Using percent style obscures symbolic intent of array's contents. + EnforcedStyle: brackets +RSpec/MessageSpies: + EnforcedStyle: receive +Style/Documentation: + Exclude: + - lib/puppet/parser/functions/**/* + - spec/**/* +Style/WordArray: + EnforcedStyle: brackets +Style/CollectionMethods: + Enabled: true +Style/MethodCalledOnDoEndBlock: + Enabled: true +Style/StringMethods: + Enabled: true +Layout/EndOfLine: + Enabled: false +Layout/IndentHeredoc: + Enabled: false +Metrics/AbcSize: + Enabled: false +Metrics/BlockLength: + Enabled: false +Metrics/ClassLength: + Enabled: false +Metrics/CyclomaticComplexity: + Enabled: false +Metrics/MethodLength: + Enabled: false +Metrics/ModuleLength: + Enabled: false +Metrics/ParameterLists: + Enabled: false +Metrics/PerceivedComplexity: + Enabled: false +RSpec/DescribeClass: + Enabled: false +RSpec/ExampleLength: + Enabled: false +RSpec/MessageExpectation: + Enabled: false +RSpec/MultipleExpectations: + Enabled: false +RSpec/NestedGroups: + Enabled: false +Style/AsciiComments: + Enabled: false +Style/IfUnlessModifier: + Enabled: false +Style/SymbolProc: + Enabled: false diff --git a/.travis.yml b/.travis.yml index 4aa34de..074a10f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,22 +1,53 @@ -sudo: false -cache: bundler +--- +dist: trusty language: ruby -bundler_args: "--without development acceptance" -script: bundle exec rake validate lint +cache: bundler +before_install: + - bundle -v + - rm -f Gemfile.lock + - gem update --system $RUBYGEMS_VERSION + - gem --version + - bundle -v +script: + - 'bundle exec rake $CHECK' +bundler_args: --without system_tests +rvm: + - 2.5.3 +stages: + - static + - spec + - acceptance + - + if: tag =~ ^v\d + name: deploy matrix: fast_finish: true include: - - rvm: 2.2.5 - env: PUPPET_GEM_VERSION="~> 5.0" STRICT_VARIABLES="yes" DEPLOY_TO_FORGE="yes" - - rvm: 2.2.5 - env: PUPPET_GEM_VERSION="~> 4.0" STRICT_VARIABLES="yes" - - rvm: 2.1.6 - env: PUPPET_GEM_VERSION="~> 4.0" STRICT_VARIABLES="yes" + - + env: CHECK="check:symlinks check:git_ignore check:dot_underscore check:test_file rubocop syntax lint metadata_lint" + stage: static + - + env: PUPPET_GEM_VERSION="~> 5.0" CHECK=parallel_spec + rvm: 2.4.5 + stage: spec + - + env: PUPPET_GEM_VERSION="~> 6.0" CHECK=parallel_spec + rvm: 2.5.3 + stage: spec + - + env: DEPLOY_TO_FORGE=yes + stage: deploy +branches: + only: + - master + - /^v\d/ +notifications: + email: false deploy: provider: puppetforge - user: gnubilafrance + user: puppet password: - secure: Jr2gk1aVstZ6RusmWChZFYfthV3wrl8FTPVuR09lrDVh3eyNJicOCS+FwCu2kXzFNFSzXGQpDWoLDNjqO7Tq/m75962wJkQdtDsivUnYKvmNWMvjI5cgDydqEcgDKxV3XtphMzpAILkr5IdfNGEV9AMXcsc/kp8UUInCaOgBDL0= + secure: "" on: tags: true all_branches: true diff --git a/.yardopts b/.yardopts new file mode 100644 index 0000000..9c0d33f --- /dev/null +++ b/.yardopts @@ -0,0 +1 @@ +--markup markdown diff --git a/Gemfile b/Gemfile index 40339a9..cf2c387 100644 --- a/Gemfile +++ b/Gemfile @@ -1,23 +1,71 @@ -source ENV['GEM_SOURCE'] || "https://rubygems.org" - -group :development, :unit_tests do - gem 'rake', :require => false - gem 'rspec-puppet', :require => false - gem 'puppetlabs_spec_helper', :require => false - gem 'puppet-lint', :require => false - gem 'metadata-json-lint', :require => false +source ENV['GEM_SOURCE'] || 'https://rubygems.org' + +def location_for(place_or_version, fake_version = nil) + git_url_regex = %r{\A(?(https?|git)[:@][^#]*)(#(?.*))?} + file_url_regex = %r{\Afile:\/\/(?.*)} + + if place_or_version && (git_url = place_or_version.match(git_url_regex)) + [fake_version, { git: git_url[:url], branch: git_url[:branch], require: false }].compact + elsif place_or_version && (file_url = place_or_version.match(file_url_regex)) + ['>= 0', { path: File.expand_path(file_url[:path]), require: false }] + else + [place_or_version, { require: false }] + end +end + +ruby_version_segments = Gem::Version.new(RUBY_VERSION.dup).segments +minor_version = ruby_version_segments[0..1].join('.') + +group :development do + gem "fast_gettext", '1.1.0', require: false if Gem::Version.new(RUBY_VERSION.dup) < Gem::Version.new('2.1.0') + gem "fast_gettext", require: false if Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new('2.1.0') + gem "json_pure", '<= 2.0.1', require: false if Gem::Version.new(RUBY_VERSION.dup) < Gem::Version.new('2.0.0') + gem "json", '= 1.8.1', require: false if Gem::Version.new(RUBY_VERSION.dup) == Gem::Version.new('2.1.9') + gem "json", '= 2.0.4', require: false if Gem::Requirement.create('~> 2.4.2').satisfied_by?(Gem::Version.new(RUBY_VERSION.dup)) + gem "json", '= 2.1.0', require: false if Gem::Requirement.create(['>= 2.5.0', '< 2.7.0']).satisfied_by?(Gem::Version.new(RUBY_VERSION.dup)) + gem "puppet-module-posix-default-r#{minor_version}", require: false, platforms: [:ruby] + gem "puppet-module-posix-dev-r#{minor_version}", require: false, platforms: [:ruby] + gem "puppet-module-win-default-r#{minor_version}", require: false, platforms: [:mswin, :mingw, :x64_mingw] + gem "puppet-module-win-dev-r#{minor_version}", require: false, platforms: [:mswin, :mingw, :x64_mingw] end -if puppetversion = ENV['PUPPET_GEM_VERSION'] - gem 'puppet', puppetversion, :require => false -else - gem 'puppet', :require => false +puppet_version = ENV['PUPPET_GEM_VERSION'] +facter_version = ENV['FACTER_GEM_VERSION'] +hiera_version = ENV['HIERA_GEM_VERSION'] + +gems = {} + +gems['puppet'] = location_for(puppet_version) + +# If facter or hiera versions have been specified via the environment +# variables + +gems['facter'] = location_for(facter_version) if facter_version +gems['hiera'] = location_for(hiera_version) if hiera_version + +if Gem.win_platform? && puppet_version =~ %r{^(file:///|git://)} + # If we're using a Puppet gem on Windows which handles its own win32-xxx gem + # dependencies (>= 3.5.0), set the maximum versions (see PUP-6445). + gems['win32-dir'] = ['<= 0.4.9', require: false] + gems['win32-eventlog'] = ['<= 0.6.5', require: false] + gems['win32-process'] = ['<= 0.7.5', require: false] + gems['win32-security'] = ['<= 0.2.5', require: false] + gems['win32-service'] = ['0.8.8', require: false] end -if rspecpuppetversion = ENV['RSPEC_GEM_VERSION'] - gem 'rspec', rspecpuppetversion, :require => false -else - gem 'rspec', :require => false +gems.each do |gem_name, gem_params| + gem gem_name, *gem_params end -# vim:ft=ruby +# Evaluate Gemfile.local and ~/.gemfile if they exist +extra_gemfiles = [ + "#{__FILE__}.local", + File.join(Dir.home, '.gemfile'), +] + +extra_gemfiles.each do |gemfile| + if File.file?(gemfile) && File.readable?(gemfile) + eval(File.read(gemfile), binding) + end +end +# vim: syntax=ruby diff --git a/Rakefile b/Rakefile index aa2d928..750ef46 100644 --- a/Rakefile +++ b/Rakefile @@ -1,7 +1,76 @@ -require 'rubygems' require 'puppetlabs_spec_helper/rake_tasks' -require 'puppet-lint/tasks/puppet-lint' +require 'puppet-syntax/tasks/puppet-syntax' +require 'puppet_blacksmith/rake_tasks' if Bundler.rubygems.find_name('puppet-blacksmith').any? +require 'github_changelog_generator/task' if Bundler.rubygems.find_name('github_changelog_generator').any? +require 'puppet-strings/tasks' if Bundler.rubygems.find_name('puppet-strings').any? + +def changelog_user + return unless Rake.application.top_level_tasks.include? "changelog" + returnVal = nil || JSON.load(File.read('metadata.json'))['author'] + raise "unable to find the changelog_user in .sync.yml, or the author in metadata.json" if returnVal.nil? + puts "GitHubChangelogGenerator user:#{returnVal}" + returnVal +end + +def changelog_project + return unless Rake.application.top_level_tasks.include? "changelog" + returnVal = nil || JSON.load(File.read('metadata.json'))['name'] + raise "unable to find the changelog_project in .sync.yml or the name in metadata.json" if returnVal.nil? + puts "GitHubChangelogGenerator project:#{returnVal}" + returnVal +end + +def changelog_future_release + return unless Rake.application.top_level_tasks.include? "changelog" + returnVal = "v%s" % JSON.load(File.read('metadata.json'))['version'] + raise "unable to find the future_release (version) in metadata.json" if returnVal.nil? + puts "GitHubChangelogGenerator future_release:#{returnVal}" + returnVal +end + +PuppetLint.configuration.send('disable_relative') + +if Bundler.rubygems.find_name('github_changelog_generator').any? + GitHubChangelogGenerator::RakeTask.new :changelog do |config| + raise "Set CHANGELOG_GITHUB_TOKEN environment variable eg 'export CHANGELOG_GITHUB_TOKEN=valid_token_here'" if Rake.application.top_level_tasks.include? "changelog" and ENV['CHANGELOG_GITHUB_TOKEN'].nil? + config.user = "#{changelog_user}" + config.project = "#{changelog_project}" + config.future_release = "#{changelog_future_release}" + config.exclude_labels = ['maintenance'] + config.header = "# Change log\n\nAll notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org)." + config.add_pr_wo_labels = true + config.issues = false + config.merge_prefix = "### UNCATEGORIZED PRS; GO LABEL THEM" + config.configure_sections = { + "Changed" => { + "prefix" => "### Changed", + "labels" => ["backwards-incompatible"], + }, + "Added" => { + "prefix" => "### Added", + "labels" => ["feature", "enhancement"], + }, + "Fixed" => { + "prefix" => "### Fixed", + "labels" => ["bugfix"], + }, + } + end +else + desc 'Generate a Changelog from GitHub' + task :changelog do + raise <= Gem::Version.new('2.2.2')" +EOM + end +end -PuppetLint.configuration.fail_on_warnings -PuppetLint.configuration.send('disable_80chars') -PuppetLint.configuration.ignore_paths = ["spec/**/*.pp", "pkg/**/*.pp"] diff --git a/TODO b/TODO deleted file mode 100644 index 1e0a106..0000000 --- a/TODO +++ /dev/null @@ -1,2 +0,0 @@ -Use nagios_hostgroup type rather than clumsily creating our own. -Add support for ignored_services to eliminate false alerts. diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 0000000..e10ba3b --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,55 @@ +--- +version: 1.1.x.{build} +branches: + only: + - master +skip_commits: + message: /^\(?doc\)?.*/ +clone_depth: 10 +init: + - SET + - 'mkdir C:\ProgramData\PuppetLabs\code && exit 0' + - 'mkdir C:\ProgramData\PuppetLabs\facter && exit 0' + - 'mkdir C:\ProgramData\PuppetLabs\hiera && exit 0' + - 'mkdir C:\ProgramData\PuppetLabs\puppet\var && exit 0' +environment: + matrix: + - + RUBY_VERSION: 24-x64 + CHECK: syntax lint metadata_lint check:symlinks check:git_ignore check:dot_underscore check:test_file rubocop + - + PUPPET_GEM_VERSION: ~> 5.0 + RUBY_VERSION: 24 + CHECK: parallel_spec + - + PUPPET_GEM_VERSION: ~> 5.0 + RUBY_VERSION: 24-x64 + CHECK: parallel_spec + - + PUPPET_GEM_VERSION: ~> 6.0 + RUBY_VERSION: 25 + CHECK: parallel_spec + - + PUPPET_GEM_VERSION: ~> 6.0 + RUBY_VERSION: 25-x64 + CHECK: parallel_spec +matrix: + fast_finish: true +install: + - set PATH=C:\Ruby%RUBY_VERSION%\bin;%PATH% + - bundle install --jobs 4 --retry 2 --without system_tests + - type Gemfile.lock +build: off +test_script: + - bundle exec puppet -V + - ruby -v + - gem -v + - bundle -v + - bundle exec rake %CHECK% +notifications: + - provider: Email + to: + - nobody@nowhere.com + on_build_success: false + on_build_failure: false + on_build_status_changed: false diff --git a/example.yaml b/example.yaml deleted file mode 100644 index b7280d0..0000000 --- a/example.yaml +++ /dev/null @@ -1,71 +0,0 @@ -# Monitoring Server -check_mk::filestore: 'puppet:///files/check_mk' -check_mk::package: 'omd-0.56-rh60-29.x86_64.rpm' - -#check_parameters = [ -# ( (95, 99), ALL_HOSTS, [ "fs_/boot" ]), -# ( (3192, 3584), ALL_HOSTS, [ "JVM PODDSv3 Memory" ]), -# ( (150, 200), ALL_HOSTS, [ "JVM PODDSv3 Threads" ]), -# ( (4000, 6000), [ 'coherence' ], ALL_HOSTS, [ "Number of threads" ]), -#] -# Defaults: -# hosts: ALL_HOSTS -# tags: undef -check_mk::check_parameters: - 'fs_/boot': - warning: '95' - critical: '99' - 'JVM MyApp Memory': - warning: '3192' - critical: '3584' - 'JVM MyApp Threads': - warning: '150' - critical: '200' - 'Number of threads': - tags: [ 'coherence' ] - warning: '4000' - critical: '6000' - 'fs_/': - hosts: [ 'myhost1.domain.com', 'myhost2.domain.com' ] - warning: '60' - critical: '70' - -check_mk::host_groups: - 'Puppet_Masters': - host_tags: - - 'puppet-master' - - 'My_App': - description: 'My Application' - host_tags: - - 'my-app' - - 'My_DB': - description: 'My Database' - host_tags: - - 'my-db' - -#ignored_services = [ -# ( [ "windows" ], ALL_HOSTS, [ "LOG Security" ] ), -# ( ALL_HOSTS, [ "NFS mount /home/" ] ) -#] - -check_mk::ignored_services: - 'LOG security': - tags: - 'windows' - 'NFS mount /home/': - hosts: - - 'lnxuser1.domain.com' - - 'lnxuser2.domain.com' - -# Monitoring Agent -check_mk::agent::filestore: 'puppet:///files/check_mk' -check_mk::agent::version: '1.2.0p3-1' - -# Set host tags based on built-in and custom facts -check_mk::agent::host_tags: - - '%{envtype}' - - '%{kernel}' - - '%{role}' - - '%{location}' diff --git a/manifests/agent.pp b/manifests/agent.pp index 2a0ae78..0857bda 100644 --- a/manifests/agent.pp +++ b/manifests/agent.pp @@ -1,23 +1,22 @@ +# == Class: check_mk::agent +# +# Configures and install the check_mk agent. class check_mk::agent ( - $filestore = undef, - $host_tags = undef, - $ip_whitelist = undef, - $port = '6556', - $server_dir = '/usr/bin', - $use_cache = false, - $user = 'root', - $version = undef, - $workspace = '/root/check_mk', - $package = undef, - $mrpe_checks = {}, -) { - validate_hash($mrpe_checks) + Optional[String] $filestore = undef, + Optional[Array] $host_tags = undef, + Stdlib::Absolutepath $workspace = '/root/check_mk', + Optional[String] $package = 'check-mk-agent', + Hash $mrpe_checks = {}, + Optional[String] $encryption_secret = undef, + Stdlib::Absolutepath $config_dir = $check_mk::agent::params::config_dir, +) inherits check_mk::agent::params { include check_mk::agent::install include check_mk::agent::config - include check_mk::agent::service Class['check_mk::agent::install'] -> Class['check_mk::agent::config'] + @@check_mk::host { $::fqdn: host_tags => $host_tags, } + create_resources('check_mk::agent::mrpe', $mrpe_checks) } diff --git a/manifests/agent/config.pp b/manifests/agent/config.pp index dbf7b42..a9c9cb6 100644 --- a/manifests/agent/config.pp +++ b/manifests/agent/config.pp @@ -4,66 +4,17 @@ # # === Parameters # -# [*ip_whitelist*] -# Array of IP allowed to access the check_mk agent. -# Default: undef -# -# [*port*] -# Check_mk port -# Default: undef -# -# [*server_dir*] -# Check_mk server directory. -# Default: undef -# -# [*use_cache*] -# Enable cache. -# Default: undef -# -# [*user*] -# Check_mk user. -# Default: undef # class check_mk::agent::config ( - $ip_whitelist = $check_mk::agent::ip_whitelist, - $port = $check_mk::agent::port, - $server_dir = $check_mk::agent::server_dir, - $use_cache = $check_mk::agent::use_cache, - $user = $check_mk::agent::user, + Optional[String] $encryption_secret = $check_mk::agent::encryption_secret, + Stdlib::Absolutepath $config_dir = $check_mk::agent::config_dir, ) inherits check_mk::agent { - if $use_cache { - $server = "${server_dir}/check_mk_caching_agent" - } else { - $server = "${server_dir}/check_mk_agent" - } - - if $ip_whitelist { - $only_from = join($ip_whitelist, ' ') - } else { - $only_from = undef - } - - $xinetd_file = $::osfamily ? { - 'RedHat' => '/etc/xinetd.d/check-mk-agent', - default => '/etc/xinetd.d/check_mk', - } - - file { $xinetd_file: - ensure => 'file', - owner => 'root', - group => 'root', - mode => '0444', - content => template('check_mk/agent/check_mk.erb'), - require => Package['check_mk-agent'], - notify => Class['check_mk::agent::service'], - } - - # Delete file from older check_mk package version - if $::osfamily == 'RedHat' { - file { '/etc/xinetd.d/check_mk': - ensure => 'absent', + if $encryption_secret { + file {'encryption_config': + ensure => file, + mode => '0600', + path => "${config_dir}/encryption.cfg", + content => template('check_mk/agent/encryption.cfg.erb'), } } } - -# vim: set et sta sw=2 ts=2 sts=2 noci noai:a diff --git a/manifests/agent/install.pp b/manifests/agent/install.pp index a377e56..e38b263 100644 --- a/manifests/agent/install.pp +++ b/manifests/agent/install.pp @@ -1,49 +1,49 @@ +# == Class: check_mk::agent::install +# +# Install check_mk agent. class check_mk::agent::install ( - $version = $check_mk::agent::version, - $filestore = undef, + $filestore = $check_mk::agent::filestore, $workspace = $check_mk::agent::workspace, - $package = undef, + $package = $check_mk::agent::package, ) inherits check_mk::agent { - if ! defined(Package['xinetd']) { - package { 'xinetd': - ensure => present, - } - } if $filestore { - if ! $version { - fail('version must be specified.') - } - if ! defined(File[$workspace]) { file { $workspace: ensure => directory, } } - file { "${workspace}/check_mk-agent-${version}.noarch.rpm": - ensure => present, - source => "${filestore}/check_mk-agent-${version}.noarch.rpm", - require => Package['xinetd'], - } - package { 'check_mk-agent': - ensure => present, - provider => 'rpm', - source => "${workspace}/check_mk-agent-${version}.noarch.rpm", - require => File["${workspace}/check_mk-agent-${version}.noarch.rpm"], - } - } - else { - $check_mk_agent = $package ? { - undef => $::osfamily ? { - 'Debian' => 'check-mk-agent', - 'RedHat' => 'check-mk-agent', - default => 'check_mk-agent', - }, - default => $package, + + # check-mk-agent_1.5.0p7-1_all.deb + if $package =~ /^(check-mk-(\w*))(-|_)(\d*\.\d*\.\d*p\d*).+\.(\w+)$/ { + case $5 { + 'deb': { + $type = 'dpkg' + } + default: { + $type = $5 + } + } + $package_name = $1 + + file { "${workspace}/${package}": + ensure => present, + source => "${filestore}/${package}", + } + + package { 'check_mk-agent': + ensure => present, + name => $package_name, + provider => $type, + source => "${workspace}/${package}", + require => File["${workspace}/${package}"], + } + } else { + fail('Package does not match format like check-mk-agent_1.5.0p7-1_all.deb') } + } else { package { 'check_mk-agent': - ensure => present, - name => $check_mk_agent, - require => Package['xinetd'], + ensure => present, + name => $package, } } } diff --git a/manifests/agent/mrpe.pp b/manifests/agent/mrpe.pp index eb0c80f..89e3718 100644 --- a/manifests/agent/mrpe.pp +++ b/manifests/agent/mrpe.pp @@ -15,30 +15,21 @@ # } # ``` # -## Authors -# -# * Bas Grolleman -# + define check_mk::agent::mrpe ( - $command, + String $command, + Stdlib::Absolutepath $config_dir = $check_mk::agent::config_dir, ) { - $mrpe_config_file = $::operatingsystem ? { - centos => '/etc/check-mk-agent/mrpe.cfg', - redhat => '/etc/check-mk-agent/mrpe.cfg', - default => undef, - } + $mrpe_config_file = "${config_dir}/mrpe.cfg" - if ( $mrpe_config_file ) { - if ! defined(Concat[$mrpe_config_file]) { - concat { $mrpe_config_file: - ensure => 'present', - } + if ! defined(Concat[$mrpe_config_file]) { + concat { $mrpe_config_file: + ensure => 'present', } - concat::fragment { "${name}-mrpe-check": - target => $mrpe_config_file, - content => "${name} ${command}\n", - } - } else { - fail("Creating mrpe.cfg is unsupported for operatingsystem ${::operatingsystem}") + } + + concat::fragment { "${name}-mrpe-check": + target => $mrpe_config_file, + content => "${name} ${command}\n", } } diff --git a/manifests/agent/params.pp b/manifests/agent/params.pp new file mode 100644 index 0000000..ff54dc0 --- /dev/null +++ b/manifests/agent/params.pp @@ -0,0 +1,15 @@ +# == Class: check_mk::agent::params +# +class check_mk::agent::params { + case $::osfamily { + 'RedHat': { + $config_dir = '/etc/check-mk-agent' + } + 'Debian': { + $config_dir = '/etc/check_mk' + } + default: { + fail("OS familly ${::osfamily} is not managed!") + } + } +} diff --git a/manifests/agent/service.pp b/manifests/agent/service.pp deleted file mode 100644 index 0d3ff04..0000000 --- a/manifests/agent/service.pp +++ /dev/null @@ -1,17 +0,0 @@ -class check_mk::agent::service { - if ! defined(Service['xinetd']) { - if $::operatingsystem == 'Debian' and - versioncmp($::operatingsystemmajrelease, '7') == 0 { - service { 'xinetd': - ensure => 'running', - enable => true, - hasstatus => false, - } - } else { - service { 'xinetd': - ensure => 'running', - enable => true, - } - } - } -} diff --git a/manifests/config.pp b/manifests/config.pp index 328f3d9..1586d04 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -1,58 +1,64 @@ - +# == Class: check_mk::config +# +# Configures checkmk class check_mk::config ( - $site, - $host_groups = undef, + String $site, + Optional[Hash] $host_groups = undef, + Optional[Array] $all_hosts_static = undef, ) { $etc_dir = "/omd/sites/${site}/etc" $bin_dir = "/omd/sites/${site}/bin" file { "${etc_dir}/nagios/local": ensure => directory, + owner => $site, + group => $site, } + file_line { 'nagios-add-check_mk-cfg_dir': ensure => present, line => "cfg_dir=${etc_dir}/nagios/local", path => "${etc_dir}/nagios/nagios.cfg", require => File["${etc_dir}/nagios/local"], - notify => Class['check_mk::service'], - } - file_line { 'add-guest-users': - ensure => present, - line => 'guest_users = [ "guest" ]', - path => "${etc_dir}/check_mk/multisite.mk", - notify => Class['check_mk::service'], - } - file { "${etc_dir}/check_mk/all_hosts_static": - ensure => file, - content => template('check_mk/all_hosts_static.erb'), } + concat { "${etc_dir}/check_mk/main.mk": - owner => 'root', - group => 'root', - mode => 'u=rw,go=r', - notify => Exec['check_mk-refresh'], + owner => $site, + group => $site, + mode => '0644', + notify => Exec['check_mk-reload'], } - # all_hosts + + # # all_hosts concat::fragment { 'all_hosts-header': target => "${etc_dir}/check_mk/main.mk", content => "all_hosts = [\n", order => 10, } + + file { "${etc_dir}/check_mk/all_hosts_static": + ensure => file, + content => template('check_mk/all_hosts_static.erb'), + } + + # # local list of hosts is in /omd/sites/${site}/etc/check_mk/all_hosts_static and is appended + concat::fragment { 'all-hosts-static': + source => "${etc_dir}/check_mk/all_hosts_static", + target => "${etc_dir}/check_mk/main.mk", + order => 18, + } + concat::fragment { 'all_hosts-footer': target => "${etc_dir}/check_mk/main.mk", content => "]\n", order => 19, } + + #TODO: Check if nodes are added automatically because we removed the exec Check_mk::Host <<| |>> { target => "${etc_dir}/check_mk/main.mk", - notify => Exec['check_mk-refresh'] - } - # local list of hosts is in /omd/sites/${site}/etc/check_mk/all_hosts_static and is appended - concat::fragment { 'all-hosts-static': - source => "${etc_dir}/check_mk/all_hosts_static", - target => "${etc_dir}/check_mk/main.mk", - order => 18, } - # host_groups + + # # host_groups if $host_groups { file { "${etc_dir}/nagios/local/hostgroups": ensure => directory, @@ -72,36 +78,30 @@ dir => "${etc_dir}/nagios/local/hostgroups", hostgroups => $host_groups, target => "${etc_dir}/check_mk/main.mk", - notify => Exec['check_mk-refresh'] } } - # local config is in /omd/sites/${site}/etc/check_mk/main.mk.local and is appended + + # # local config is in /omd/sites/${site}/etc/check_mk/main.mk.local and is appended file { "${etc_dir}/check_mk/main.mk.local": ensure => file, owner => 'root', group => 'root', mode => 'u=rw,go=r', } + concat::fragment { 'check_mk-local-config': source => "${etc_dir}/check_mk/main.mk.local", target => "${etc_dir}/check_mk/main.mk", order => 99, } - # re-read config if it changes - exec { 'check_mk-refresh': - command => "/bin/su -l -c '${bin_dir}/check_mk -I' ${site}", - refreshonly => true, - notify => Exec['check_mk-reload'], - } + exec { 'check_mk-reload': - command => "/bin/su -l -c '${bin_dir}/check_mk -O' ${site}", + command => "/bin/su -l -c '${bin_dir}/check_mk --reload' ${site}", refreshonly => true, } - # re-read inventory daily - cron { 'check_mk-refresh-inventory-daily': - user => 'root', - command => "su -l -c '${bin_dir}/check_mk -O' ${site}", - minute => 0, - hour => 0, - } + + # In the original code 2 execs would be here, but is is not recommended + # to do a reindex, see https://mathias-kettner.de/checkmk_inventory.html + # This breaks large installs. The services class is subscribed to the + # config class so new changes should be noticed automatically. } diff --git a/manifests/host.pp b/manifests/host.pp index a584e71..cb43f0f 100644 --- a/manifests/host.pp +++ b/manifests/host.pp @@ -1,6 +1,9 @@ +# == Class: check_mk::host +# +# Resource to create a check_mk host define check_mk::host ( - $target = undef, - $host_tags = [], + Optional[String] $target = undef, + Optional[Array] $host_tags = [], ) { $host = $title if size($host_tags) > 0 { diff --git a/manifests/hostgroup.pp b/manifests/hostgroup.pp index baec45f..6c44d8f 100644 --- a/manifests/hostgroup.pp +++ b/manifests/hostgroup.pp @@ -1,3 +1,6 @@ +# == Class: check_mk::hostgroup +# +# Resource to create a hostgroup define check_mk::hostgroup ( $dir, $hostgroups, diff --git a/manifests/init.pp b/manifests/init.pp index 3f1b6c7..b7f91ea 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -1,3 +1,6 @@ +# == Class: check_mk +# +# Main check_mk class class check_mk ( $checkmk_service = $check_mk::params::checkmk_service, $filestore = $check_mk::params::filestore, @@ -22,6 +25,6 @@ class { 'check_mk::service': checkmk_service => $checkmk_service, httpd_service => $httpd_service, - require => Class['check_mk::config'], + subscribe => Class['check_mk::config'], } } diff --git a/manifests/install.pp b/manifests/install.pp index 009918d..384f613 100644 --- a/manifests/install.pp +++ b/manifests/install.pp @@ -1,8 +1,11 @@ +# == Class: check_mk::install +# +# Installs check_mk through a deb or rpm file class check_mk::install ( - $site, - $workspace, - $filestore = undef, - $package = undef, + String $site, + Stdlib::Absolutepath $workspace, + Optional[String] $filestore = undef, + String $package = undef, ) { if $filestore { if ! defined(File[$workspace]) { @@ -15,28 +18,46 @@ source => "${filestore}/${package}", require => File[$workspace], } - # omd-0.56-rh60-29.x86_64.rpm - if $package =~ /^(omd-\d+\.\d+)-(.*?)\.(rpm|deb)$/ { + + # check-mk-raw-1.5.0p7_0.stretch_amd64.deb + if $package =~ /^(check-mk-(\w*))(-|_)(\d*\.\d*\.\d*p\d*).+\.(\w+)$/ { + $type = $5 $package_name = $1 - $type = $3 - package { $package_name: - ensure => installed, - provider => $type, - source => "${workspace}/${package}", - require => File["${workspace}/${package}"], + + if $type == 'deb' { + package {'gdebi': + ensure => present + } + + exec {'install-check-mk': + command => "/usr/bin/gdebi --non-interactive ${workspace}/${package}", + unless => "/usr/bin/dpkg-query -W --showformat '\${Status} \${Package}\\n' | grep ${package_name} | grep -q 'install ok installed'", # lint:ignore:140chars + require => Package['gdebi'], + before => Exec['omd-create-site'], + } + } else { + package { $package_name: + ensure => installed, + provider => $type, + source => "${workspace}/${package}", + require => File["${workspace}/${package}"], + before => Exec['omd-create-site'], + } } + } else { + fail('Package does not match format like check-mk-raw-1.5.0p7_0.stretch_amd64.deb') } } else { $package_name = $package package { $package_name: ensure => installed, + before => Exec['omd-create-site'], } } $etc_dir = "/omd/sites/${site}/etc" exec { 'omd-create-site': command => "/usr/bin/omd create ${site}", creates => $etc_dir, - require => Package[$package_name], } } diff --git a/manifests/install_tarball.pp b/manifests/install_tarball.pp deleted file mode 100644 index f09e770..0000000 --- a/manifests/install_tarball.pp +++ /dev/null @@ -1,91 +0,0 @@ -class check_mk::install_tarball ( - $filestore, - $version, - $workspace, -) { - package { 'nagios': - ensure => present, - notify => Exec['set-nagiosadmin-password', 'set-guest-password', 'add-apache-to-nagios-group'], - } - file { '/etc/nagios/passwd': - ensure => present, - owner => 'root', - group => 'apache', - mode => '0640', - } - exec { 'set-nagiosadmin-password': - command => '/usr/bin/htpasswd -b /etc/nagios/passwd nagiosadmin letmein', - refreshonly => true, - require => File['/etc/nagios/passwd'], - } - exec { 'set-guest-password': - command => '/usr/bin/htpasswd -b /etc/nagios/passwd guest guest', - refreshonly => true, - require => File['/etc/nagios/passwd'], - } - exec { 'add-apache-to-nagios-group': - command => '/usr/sbin/usermod -a -G nagios apache', - refreshonly => true, - } - package { 'nagios-plugins-all': - ensure => present, - require => Package['nagios'], - } - package { [ 'xinetd', 'mod_python', 'make', 'gcc-c++', 'tar', 'gzip' ]: - ensure => present, - } - file { "${workspace}/check_mk-${version}.tar.gz": - ensure => present, - source => "${filestore}/check_mk-${version}.tar.gz", - } - exec { 'unpack-check_mk-tarball': - command => "/bin/tar -zxf ${workspace}/check_mk-${version}.tar.gz", - cwd => $workspace, - creates => "${workspace}/check_mk-${version}", - require => File["${workspace}/check_mk-${version}.tar.gz"], - } - exec { 'change-setup-config-location': - command => "/usr/bin/perl -pi -e 's#^SETUPCONF=.*?$#SETUPCONF=${workspace}/check_mk_setup.conf#' ${workspace}/check_mk-${version}/setup.sh", - unless => "/bin/egrep '^SETUPCONF=${workspace}/check_mk_setup.conf$' ${workspace}/check_mk-${version}/setup.sh", - require => Exec['unpack-check_mk-tarball'], - } - # Avoid header like 'Written by setup of check_mk 1.2.0p3 at Thu Feb 7 12:26:17 GMT 2013' - # that changes every time the setup script is run - exec { 'remove-setup-header': - command => "/usr/bin/perl -pi -e 's#^DIRINFO=.*?$#DIRINFO=#' ${workspace}/check_mk-${version}/setup.sh", - unless => "/bin/egrep '^DIRINFO=$' ${workspace}/check_mk-${version}/setup.sh", - require => Exec['unpack-check_mk-tarball'], - } - file { "${workspace}/check_mk_setup.conf": - ensure => present, - content => template('check_mk/setup.conf.erb'), - notify => Exec['check_mk-setup'], - } - file { '/etc/nagios/check_mk': - ensure => directory, - owner => 'nagios', - group => 'nagios', - recurse => true, - require => Package['nagios'], - } - file { '/etc/nagios/check_mk/hostgroups': - ensure => directory, - owner => 'nagios', - group => 'nagios', - require => File['/etc/nagios/check_mk'], - } - exec { 'check_mk-setup': - command => "${workspace}/check_mk-${version}/setup.sh --yes", - cwd => "${workspace}/check_mk-${version}", - refreshonly => true, - require => [ - Exec['change-setup-config-location'], - Exec['remove-setup-header'], - Exec['unpack-check_mk-tarball'], - File["${workspace}/check_mk_setup.conf"], - File['/etc/nagios/check_mk'], - Package['nagios'], - ], - notify => Class['check_mk::service'], - } -} diff --git a/manifests/params.pp b/manifests/params.pp index 80abc9b..2c5e039 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -1,3 +1,5 @@ +# == Class: check_mk::params +# class check_mk::params { # common variables diff --git a/manifests/service.pp b/manifests/service.pp index 5494eee..ae55023 100644 --- a/manifests/service.pp +++ b/manifests/service.pp @@ -1,6 +1,9 @@ +# == Class: check_mk::service +# +# Configures the check mk service class check_mk::service ( - $checkmk_service, - $httpd_service, + String $checkmk_service, + String $httpd_service, ) { if ! defined(Service[$httpd_service]) { service { $httpd_service: @@ -15,7 +18,8 @@ } } service { $checkmk_service: - ensure => 'running', - enable => true, + ensure => 'running', + enable => true, + hasrestart => true, } } diff --git a/metadata.json b/metadata.json index 301d7c7..8517838 100644 --- a/metadata.json +++ b/metadata.json @@ -2,20 +2,25 @@ "name": "gnubilafrance-check_mk", "version": "0.7.2", "author": "Baptiste Grenier ", - "license": "Apache-2.0", "summary": "Check_MK installation and configuration", - + "license": "Apache-2.0", "source": "https://github.com/gnubila-france/puppet-check_mk", "project_page": "https://github.com/gnubila-france/puppet-check_mk", "issues_url": "https://github.com/gnubila-france/puppet-check_mk/issues", - - "tags": [ "check_mk", "monitoring", "nagios" ], - + "dependencies": [ + { + "name": "puppetlabs/concat", + "version_requirement": ">= 4.0.0 <6.0.0" + }, + { + "name": "puppetlabs/stdlib", + "version_requirement": ">= 4.0.0 <6.0.0" + } + ], "operatingsystem_support": [ { "operatingsystem": "RedHat", "operatingsystemrelease": [ - "5", "6", "7" ] @@ -23,7 +28,6 @@ { "operatingsystem": "CentOS", "operatingsystemrelease": [ - "5", "6", "7" ] @@ -31,14 +35,24 @@ { "operatingsystem": "Debian", "operatingsystemrelease": [ - "6", - "7" + "8", + "9" ] } ], - - "dependencies": [ - { "name": "puppetlabs/concat", "version_requirement": ">= 1.0.0 <5.0.0" }, - { "name": "puppetlabs/stdlib", "version_requirement": ">= 2.3.0 <5.0.0" } - ] + "requirements": [ + { + "name": "puppet", + "version_requirement": ">= 5.0.0 < 7.0.0" + } + ], + "tags": [ + "check_mk", + "monitoring", + "nagios", + "omd" + ], + "pdk-version": "1.10.0", + "template-url": "C:\\Program Files\\Puppet Labs\\DevelopmentKit\\share\\cache\\pdk-templates.git#1.10.0", + "template-ref": "1.10.0-0-gbba9ac3" } diff --git a/spec/classes/check_mk_agent_config_spec.rb b/spec/classes/check_mk_agent_config_spec.rb index 68fd2aa..7b33839 100644 --- a/spec/classes/check_mk_agent_config_spec.rb +++ b/spec/classes/check_mk_agent_config_spec.rb @@ -1,70 +1,41 @@ require 'spec_helper' -describe 'check_mk::agent::config', :type => :class do - context 'Redhat Linux' do - let :facts do - { - :osfamily => 'RedHat', - } - end - context 'with defaults for all parameters' do - it { should contain_class('check_mk::agent::config') } - it { should contain_file('/etc/xinetd.d/check-mk-agent'). - with_content(/^\tport\s+ = 6556$/). - with_content(/^\tuser\s+ = root$/). - with_content(/^\tserver\s+ = \/usr\/bin\/check_mk_agent$/). - without_content(/only_from/). - with_notify('Class[Check_mk::Agent::Service]') - } - it { should contain_file('/etc/xinetd.d/check_mk').with_ensure('absent') } - end - context 'with use_cache' do - let :params do - { - :use_cache => true, - } - end - it { should contain_file('/etc/xinetd.d/check-mk-agent'). - with_content(/^\tserver\s+ = \/usr\/bin\/check_mk_caching_agent$/) - } - end - context 'with ip_whitelist' do - let :params do - { - :ip_whitelist => [ - '1.2.3.4', - '5.6.7.8', - ], - } - end - it { should contain_file('/etc/xinetd.d/check-mk-agent'). - with_content(/^\tonly_from\s+= 127.0.0.1 1.2.3.4 5.6.7.8$/) +describe 'check_mk::agent::config' do + on_supported_os.each do |os, os_facts| + context "with default parameters set on #{os}" do + let(:facts) { os_facts } + + it { + is_expected.to compile } end - context 'with custom user' do - let :params do + context "with default parameters set on #{os}" do + let(:facts) { os_facts } + let(:params) do { - :user => 'custom', + encryption_secret: 'SECRET', } end - it { should contain_file('/etc/xinetd.d/check-mk-agent'). - with_content(/^\tuser\s+ = custom$/) - } - end - end - context 'Other OS' do - context 'with defaults for all parameters' do - it { should contain_file('/etc/xinetd.d/check_mk'). - with_content(/^\tport\s+ = 6556$/). - with_content(/^\tuser\s+ = root$/). - with_content(/^\tserver\s+ = \/usr\/bin\/check_mk_agent$/). - without_content(/only_from/). - with_notify('Class[Check_mk::Agent::Service]') + it { + is_expected.to compile + case facts[:osfamily] + when 'RedHat' + is_expected.to contain_file('encryption_config').with( + 'ensure' => 'file', + 'mode' => '0600', + 'path' => '/etc/check-mk-agent/encryption.cfg', + 'content' => %r{PASSPHRASE=SECRET\n}, + ) + when 'Debian' + is_expected.to contain_file('encryption_config').with( + 'ensure' => 'file', + 'mode' => '0600', + 'path' => '/etc/check_mk/encryption.cfg', + 'content' => %r{PASSPHRASE=SECRET\n}, + ) + end } - it { should_not contain_file('/etc/xinetd.d/check_mk').with_ensure('absent') } end end end - - diff --git a/spec/classes/check_mk_agent_install_spec.rb b/spec/classes/check_mk_agent_install_spec.rb index dce3bd2..96ca2d6 100644 --- a/spec/classes/check_mk_agent_install_spec.rb +++ b/spec/classes/check_mk_agent_install_spec.rb @@ -1,119 +1,61 @@ require 'spec_helper' -describe 'check_mk::agent::install', :type => :class do - context 'RedHat Linux' do - let :facts do - { - :osfamily => 'Redhat', - } - end +describe 'check_mk::agent::install' do + on_supported_os.each do |os, os_facts| + context "with default parameters set on #{os}" do + let(:facts) { os_facts } - context 'with default parameters' do - it { should contain_class('check_mk::agent::install').with( - { - :version => nil, - :filestore => nil, - :workspace => '/root/check_mk', - :package => nil, - } + it { + is_expected.to compile + is_expected.to contain_package('check_mk-agent').with( + 'ensure' => 'present', + 'name' => 'check-mk-agent', ) + is_expected.to contain_class('check_mk::agent::install') } - it { should contain_package('xinetd') } - it { should contain_package('check_mk-agent').with_name('check-mk-agent') } end - context 'with custom package' do - let :params do + context "with custom package on #{os}" do + let(:facts) { os_facts } + let(:params) do { - :package => 'custom-package', + package: 'custom-package', } end - it { should contain_class('check_mk::agent::install') } - it { should contain_package('xinetd') } - it { should contain_package('check_mk-agent').with_name('custom-package') } - end - context 'with filestore' do - context 'without version' do - let :params do - { - :filestore => '/filestore', - } - end - it 'should fail' do - expect { catalogue }.to raise_error(Puppet::Error, /version must be specified/) - end - end - context 'with custom parameters' do - let :params do - { - :version => '1.2.3', - :filestore => '/filestore', - :workspace => '/workspace', - } - end - it { should contain_class('check_mk::agent::install') } - it { should contain_package('xinetd') } - it { should contain_file('/workspace').with_ensure('directory') } - it { should contain_File('/workspace/check_mk-agent-1.2.3.noarch.rpm').with( - { - :ensure => 'present', - :source => '/filestore/check_mk-agent-1.2.3.noarch.rpm', - :require => 'Package[xinetd]', - } - ).that_comes_before('Package[check_mk-agent]') - } - it { should contain_package('check_mk-agent').with( - { - :provider => 'rpm', - :source => '/workspace/check_mk-agent-1.2.3.noarch.rpm', - } + it { + is_expected.to compile + is_expected.to contain_class('check_mk::agent::install') + is_expected.to contain_package('check_mk-agent').with( + 'name' => 'custom-package', ) - } - end - end - end - - context 'Debian Linux' do - let :facts do - { - :osfamily => 'Debian', } end - context 'with default parameters' do - it { should contain_class('check_mk::agent::install') } - it { should contain_package('xinetd') } - it { should contain_package('check_mk-agent').with_name('check-mk-agent') } - end - - context 'with custom package' do - let :params do + context "with filestore on #{os}" do + let(:facts) { os_facts } + let(:params) do { - :package => 'custom-package', + filestore: '/filestore', + package: 'check-mk-agent_1.5.0p7-1_all.deb', + workspace: '/workspace', } end - it { should contain_class('check_mk::agent::install') } - it { should contain_package('xinetd') } - it { should contain_package('check_mk-agent').with_name('custom-package') } - end - end - context 'Unknown OS' do - context 'with default parameters' do - it { should contain_class('check_mk::agent::install') } - it { should contain_package('xinetd') } - it { should contain_package('check_mk-agent').with_name('check_mk-agent') } - end - context 'with custom package' do - let :params do - { - :package => 'custom-package', - } - end - it { should contain_class('check_mk::agent::install') } - it { should contain_package('xinetd') } - it { should contain_package('check_mk-agent').with_name('custom-package') } + it { + is_expected.to compile + is_expected.to contain_class('check_mk::agent::install') + is_expected.to contain_file('/workspace/check-mk-agent_1.5.0p7-1_all.deb').with( + 'ensure' => 'present', + 'source' => '/filestore/check-mk-agent_1.5.0p7-1_all.deb', + ) + is_expected.to contain_package('check_mk-agent').with( + 'ensure' => 'present', + 'name' => 'check-mk-agent', + 'provider' => 'dpkg', + 'source' => '/workspace/check-mk-agent_1.5.0p7-1_all.deb', + ).that_requires('File[/workspace/check-mk-agent_1.5.0p7-1_all.deb]') + } end end end diff --git a/spec/classes/check_mk_agent_service_spec.rb b/spec/classes/check_mk_agent_service_spec.rb deleted file mode 100644 index a85b3e9..0000000 --- a/spec/classes/check_mk_agent_service_spec.rb +++ /dev/null @@ -1,29 +0,0 @@ -require 'spec_helper' -describe 'check_mk::agent::service', :type => :class do - - context 'all' do - it { should contain_class('check_mk::agent::service') } - it { should contain_service('xinetd').with({ - :ensure => 'running', - :enable => true, - }) - } - end - - context 'Debian 7' do - let :facts do - { - :operatingsystem => 'Debian', - :operatingsystemmajrelease => '7', - } - end - it { should contain_class('check_mk::agent::service') } - it { should contain_service('xinetd').with({ - :ensure => 'running', - :enable => true, - :hasstatus => false, - }) - } - end - -end diff --git a/spec/classes/check_mk_agent_spec.rb b/spec/classes/check_mk_agent_spec.rb index 6d08a4d..0903235 100644 --- a/spec/classes/check_mk_agent_spec.rb +++ b/spec/classes/check_mk_agent_spec.rb @@ -1,43 +1,38 @@ require 'spec_helper' -describe 'check_mk::agent', :type => :class do - context 'Redhat Linux' do - let :facts do - { - :kernel => 'Linux', - :operatingsystem => 'Redhat', - :osfamily => 'Redhat', + +describe 'check_mk::agent' do + on_supported_os.each do |os, os_facts| + context "with default parameters set on #{os}" do + let(:facts) { os_facts } + + it { + is_expected.to compile + is_expected.to contain_class('check_mk::agent') + is_expected.to contain_class('check_mk::agent::install') + is_expected.to contain_class('check_mk::agent::config').that_requires('Class[check_mk::agent::install]') } end - context 'with defaults for all parameters' do - it { should contain_class('check_mk::agent') } - it { should contain_class('check_mk::agent::install').that_comes_before('Class[check_mk::agent::config]') } - it { should contain_class('check_mk::agent::config') } - it { should contain_class('check_mk::agent::service') } - end - context 'with mrpe_checks' do - context 'not a hash' do - let :params do - { - :mrpe_checks => 'not_a_hash', - } - end - it 'should fail' do - expect { catalogue }.to raise_error(Puppet::Error, /\"not_a_hash\" is not a Hash./) - end - end - context 'defined correctly' do - let :params do - { - :mrpe_checks => { - 'check1' => {'command' => 'command1'}, - 'check2' => {'command' => 'command2'}, - } - } - end - it { should contain_class('check_mk::agent') } - it { should contain_check_mk__agent__mrpe('check1').with_command('command1') } - it { should contain_check_mk__agent__mrpe('check2').with_command('command2') } + context "with mrpe_checks on #{os}" do + let(:facts) { os_facts } + let(:params) do + { + mrpe_checks: { + check1: { + command: 'command1', + }, + check2: { + command: 'command2', + }, + }, + } end + + it { + is_expected.to compile + is_expected.to contain_class('check_mk::agent') + is_expected.to contain_class('check_mk::agent::install') + is_expected.to contain_class('check_mk::agent::config').that_requires('Class[check_mk::agent::install]') + } end end end diff --git a/spec/classes/check_mk_config_spec.rb b/spec/classes/check_mk_config_spec.rb index 9de298b..2927070 100644 --- a/spec/classes/check_mk_config_spec.rb +++ b/spec/classes/check_mk_config_spec.rb @@ -1,124 +1,116 @@ require 'spec_helper' -describe 'check_mk::config', :type => :class do - context 'with site set' do - let :params do - { - :site => 'TEST_SITE' + +describe 'check_mk::config' do + on_supported_os.each do |os, os_facts| + context "with site set on #{os}" do + let(:facts) { os_facts } + let(:params) { { site: 'TEST_SITE' } } + + it { + is_expected.to compile + + is_expected.to contain_class('check_mk::config') + + is_expected.to contain_file('/omd/sites/TEST_SITE/etc/nagios/local').with( + 'ensure' => 'directory', + 'owner' => 'TEST_SITE', + 'group' => 'TEST_SITE', + ) + + is_expected.to contain_file_line('nagios-add-check_mk-cfg_dir').with( + 'ensure' => 'present', + 'line' => 'cfg_dir=/omd/sites/TEST_SITE/etc/nagios/local', + 'path' => '/omd/sites/TEST_SITE/etc/nagios/nagios.cfg', + ).that_requires('File[/omd/sites/TEST_SITE/etc/nagios/local]') + + is_expected.to contain_file('/omd/sites/TEST_SITE/etc/check_mk/all_hosts_static').with( + 'ensure' => 'file', + 'content' => '', + ) + + is_expected.to contain_concat('/omd/sites/TEST_SITE/etc/check_mk/main.mk').with( + 'owner' => 'TEST_SITE', + 'group' => 'TEST_SITE', + 'mode' => '0644', + ).that_notifies('Exec[check_mk-reload]') + + is_expected.to contain_concat__fragment('all_hosts-header').with( + 'target' => '/omd/sites/TEST_SITE/etc/check_mk/main.mk', + 'content' => %r{all_hosts = \[\n}, + 'order' => 10, + ) + + is_expected.to contain_concat__fragment('all_hosts-footer').with( + 'target' => '/omd/sites/TEST_SITE/etc/check_mk/main.mk', + 'content' => %r{\]\n}, + 'order' => 19, + ) + + is_expected.to contain_concat__fragment('all-hosts-static').with( + 'source' => '/omd/sites/TEST_SITE/etc/check_mk/all_hosts_static', + 'target' => '/omd/sites/TEST_SITE/etc/check_mk/main.mk', + 'order' => 18, + ) + + is_expected.to contain_concat__fragment('check_mk-local-config').with( + 'source' => '/omd/sites/TEST_SITE/etc/check_mk/main.mk.local', + 'target' => '/omd/sites/TEST_SITE/etc/check_mk/main.mk', + 'order' => 99, + ) + + is_expected.to contain_exec('check_mk-reload').with( + 'command' => '/bin/su -l -c \'/omd/sites/TEST_SITE/bin/check_mk --reload\' TEST_SITE', + 'refreshonly' => true, + ) + + is_expected.not_to contain_file('/omd/sites/TEST_SITE/etc/nagios/local/hostgroups') + is_expected.not_to contain_concat__fragment('host_groups-header') + is_expected.not_to contain_concat__fragment('host_groups-footer') } end - it { should contain_class('check_mk::config') } - it { should contain_file('/omd/sites/TEST_SITE/etc/nagios/local').with_ensure_directory. - that_comes_before('File_line[nagios-add-check_mk-cfg_dir]') - } - it { should contain_file_line('nagios-add-check_mk-cfg_dir').with({ - :ensure => 'present', - :line => 'cfg_dir=/omd/sites/TEST_SITE/etc/nagios/local', - :path => '/omd/sites/TEST_SITE/etc/nagios/nagios.cfg', - :notify => 'Class[Check_mk::Service]', - }) - } - it { should contain_file_line('add-guest-users').with({ - :ensure => 'present', - :line => 'guest_users = [ "guest" ]', - :path => '/omd/sites/TEST_SITE/etc/check_mk/multisite.mk', - }) - } - it { should contain_file('/omd/sites/TEST_SITE/etc/check_mk/all_hosts_static').with({ - :ensure => 'file', - :content => '', - }) - } - it { should contain_concat('/omd/sites/TEST_SITE/etc/check_mk/main.mk').with({ - :owner => 'root', - :group => 'root', - :mode => '0644', - :notify => 'Exec[check_mk-refresh]', - }) - } - it { should contain_concat__fragment('all_hosts-header').with({ - :target => '/omd/sites/TEST_SITE/etc/check_mk/main.mk', - :content => /all_hosts = \[\n/, - :order => 10, - }) - } - it { should contain_concat__fragment('all_hosts-footer').with({ - :target => '/omd/sites/TEST_SITE/etc/check_mk/main.mk', - :content => /\]\n/, - :order => 19, - }) - } - it { should contain_concat__fragment('all-hosts-static').with({ - :ensure => '/omd/sites/TEST_SITE/etc/check_mk/all_hosts_static', - :target => '/omd/sites/TEST_SITE/etc/check_mk/main.mk', - :order => 18, - }) - } - it { should_not contain_file('/omd/sites/TEST_SITE/etc/nagios/local/hostgroups') } - it { should_not contain_concat__fragment('host_groups-header') } - it { should_not contain_concat__fragment('host_groups-footer') } - it { should_not contain_check_mk__hostgroup } - it { should contain_concat__fragment('check_mk-local-config').with({ - :ensure => '/omd/sites/TEST_SITE/etc/check_mk/main.mk.local', - :target => '/omd/sites/TEST_SITE/etc/check_mk/main.mk', - :order => 99, - }) - } - it { should contain_exec('check_mk-refresh').with({ - :command => /\/bin\/su -l -c '\/omd\/sites\/TEST_SITE\/bin\/check_mk -I' TEST_SITE/, - :refreshonly => true, - }) - } - it { should contain_exec('check_mk-reload').with({ - :command => /\/bin\/su -l -c '\/omd\/sites\/TEST_SITE\/bin\/check_mk -O' TEST_SITE/, - :refreshonly => true, - }) - } - it { should contain_cron('check_mk-refresh-inventory-daily').with({ - :user => 'root', - :command => /su -l -c '\/omd\/sites\/TEST_SITE\/bin\/check_mk -O' TEST_SITE/, - :minute => 0, - :hour => 0, - }) - } - end - context 'with host_groups' do - host_groups = { - 'group1' => {'host_tags' => []}, - 'group2' => {'host_tags' => []}, - } - let :params do - { - :site => 'TEST_SITE', - :host_groups => host_groups, + context "with site set on #{os}" do + let(:facts) { os_facts } + let(:params) do + { + site: 'TEST_SITE', + host_groups: { + group1: { + host_tags: [], + }, + group2: { + host_tags: [], + }, + }, + } + end + + it { + is_expected.to compile + is_expected.to contain_class('check_mk::config') + is_expected.to contain_file('/omd/sites/TEST_SITE/etc/nagios/local/hostgroups').with( + 'ensure' => 'directory', + ) + is_expected.to contain_concat__fragment('host_groups-header').with( + 'target' => '/omd/sites/TEST_SITE/etc/check_mk/main.mk', + 'content' => %r{host_groups = \[\n}, + 'order' => 20, + ) + is_expected.to contain_concat__fragment('host_groups-footer').with( + 'target' => '/omd/sites/TEST_SITE/etc/check_mk/main.mk', + 'content' => %r{\]\n}, + 'order' => 29, + ) + is_expected.to contain_check_mk__hostgroup('group1').with( + 'dir' => '/omd/sites/TEST_SITE/etc/nagios/local/hostgroups', + 'host_groups' => params['host_groups'], + 'target' => '/omd/sites/TEST_SITE/etc/check_mk/main.mk', + ) + is_expected.to contain_check_mk__hostgroup('group2').with( + 'dir' => '/omd/sites/TEST_SITE/etc/nagios/local/hostgroups', + 'host_groups' => params['host_groups'], + 'target' => '/omd/sites/TEST_SITE/etc/check_mk/main.mk', + ) } end - it { should contain_class('check_mk::config') } - it { should contain_file('/omd/sites/TEST_SITE/etc/nagios/local/hostgroups').with_ensure_directory } - it { should contain_concat__fragment('host_groups-header').with({ - :target => '/omd/sites/TEST_SITE/etc/check_mk/main.mk', - :content => /host_groups = \[\n/, - :order => 20, - }) - } - it { should contain_concat__fragment('host_groups-footer').with({ - :target => '/omd/sites/TEST_SITE/etc/check_mk/main.mk', - :content => /\]\n/, - :order => 29, - }) - } - it { should contain_check_mk__hostgroup('group1').with({ - :dir => '/omd/sites/TEST_SITE/etc/nagios/local/hostgroups', - :hostgroups => host_groups, - :target => '/omd/sites/TEST_SITE/etc/check_mk/main.mk', - :notify => 'Exec[check_mk-refresh]', - }) - } - it { should contain_check_mk__hostgroup('group2').with({ - :dir => '/omd/sites/TEST_SITE/etc/nagios/local/hostgroups', - :hostgroups => host_groups, - :target => '/omd/sites/TEST_SITE/etc/check_mk/main.mk', - :notify => 'Exec[check_mk-refresh]', - }) - } end end diff --git a/spec/classes/check_mk_install_spec.rb b/spec/classes/check_mk_install_spec.rb index 9cf0b09..6f727f7 100644 --- a/spec/classes/check_mk_install_spec.rb +++ b/spec/classes/check_mk_install_spec.rb @@ -1,20 +1,64 @@ require 'spec_helper' -describe 'check_mk::install', :type => :class do - context 'with necessary parameters set' do - let :params do - { - :filestore => false, - :package => 'package', - :site => 'site', - :workspace => 'workspace', + +describe 'check_mk::install' do + on_supported_os.each do |os, os_facts| + context "with necessary parameters set on #{os}" do + let(:facts) { os_facts } + let(:params) do + { + filestore: '/filestore/', + package: 'check-mk-raw-1.5.0p7_0.stretch_amd64.deb', + site: 'site', + workspace: '/workspace', + } + end + + it { + is_expected.to compile + + is_expected.to contain_class('check_mk::install') + + is_expected.to contain_exec('omd-create-site').with( + 'command' => '/usr/bin/omd create site', + 'creates' => '/omd/sites/site/etc', + ).that_requires('Exec[install-check-mk]') + + is_expected.to contain_package('gdebi').with( + 'ensure' => 'present', + ) + + is_expected.to contain_exec('install-check-mk').with( + 'command' => '/usr/bin/gdebi --non-interactive /workspace/check-mk-raw-1.5.0p7_0.stretch_amd64.deb', + 'unless' => '/usr/bin/dpkg-query -W --showformat \'${Status} ${Package}\n\' | grep check-mk-raw | grep -q \'install ok installed\'', + ).that_requires('Package[gdebi]') + } + end + + context "with rpm file set on #{os}" do + let(:facts) { os_facts } + let(:params) do + { + filestore: '/filestore/', + package: 'check-mk-raw-1.5.0p7-el7-38.x86_64.rpm', + site: 'site', + workspace: '/workspace', + } + end + + it { + is_expected.to compile + + is_expected.to contain_exec('omd-create-site').with( + 'command' => '/usr/bin/omd create site', + 'creates' => '/omd/sites/site/etc', + ).that_requires('Package[check-mk-raw]') + + is_expected.to contain_package('check-mk-raw').with( + 'ensure' => 'installed', + 'provider' => 'rpm', + 'source' => '/workspace/check-mk-raw-1.5.0p7-el7-38.x86_64.rpm', + ).that_requires('File[/workspace/check-mk-raw-1.5.0p7-el7-38.x86_64.rpm]') } end - it { should contain_class('check_mk::install') } - it { should contain_package('package').with_ensure('installed').that_comes_before('Exec[omd-create-site]') } - it { should contain_exec('omd-create-site').with({ - :command => '/usr/bin/omd create site', - :creates => '/omd/sites/site/etc', - }) - } end end diff --git a/spec/classes/check_mk_install_tarball_spec.rb b/spec/classes/check_mk_install_tarball_spec.rb deleted file mode 100644 index 7537d85..0000000 --- a/spec/classes/check_mk_install_tarball_spec.rb +++ /dev/null @@ -1,102 +0,0 @@ -require 'spec_helper' -describe 'check_mk::install_tarball', :type => :class do - context 'with necessary parameters set' do - let :params do - { - :filestore => '/filestore', - :version => '1.2.3', - :workspace => '/workspace', - } - end - it { should contain_class('check_mk::install_tarball') } - it { should contain_package('nagios').with_ensure('present').that_comes_before('Package[nagios-plugins-all]') } - installed_packages = [ - 'xinetd', - 'mod_python', - 'make', - 'gcc-c++', - 'tar', - 'gzip' - ] - installed_packages.each do |package| - it { should contain_package(package).with_ensure('present') } - end - it { should contain_file('/etc/nagios/passwd').with({ - :ensure => 'present', - :owner => 'root', - :group => 'apache', - :mode => '0640', - }) - } - it { should contain_exec('set-nagiosadmin-password').with({ - :refreshonly => true, - :require => 'File[/etc/nagios/passwd]', - }) - } - it { should contain_exec('set-guest-password').with({ - :refreshonly => true, - :require => 'File[/etc/nagios/passwd]', - }) - } - it { should contain_exec('add-apache-to-nagios-group').with({ - :refreshonly => true, - }) - } - it { should contain_package('nagios-plugins-all').with({ - :ensure => 'present', - :require => 'Package[nagios]', - }) - } - it { should contain_exec('unpack-check_mk-tarball').with({ - :command => '/bin/tar -zxf /workspace/check_mk-1.2.3.tar.gz', - :cwd => '/workspace', - :creates => '/workspace/check_mk-1.2.3', - :require => 'File[/workspace/check_mk-1.2.3.tar.gz]', - }) - } - it { should contain_exec('change-setup-config-location').with({ - :command => "/usr/bin/perl -pi -e 's#^SETUPCONF=.*?$#SETUPCONF=/workspace/check_mk_setup.conf#' /workspace/check_mk-1.2.3/setup.sh", - :unless => "/bin/egrep '^SETUPCONF=/workspace/check_mk_setup.conf$' /workspace/check_mk-1.2.3/setup.sh", - :require => 'Exec[unpack-check_mk-tarball]', - }) - } - it { should contain_exec('remove-setup-header').with({ - :command => "/usr/bin/perl -pi -e 's#^DIRINFO=.*?$#DIRINFO=#' /workspace/check_mk-1.2.3/setup.sh", - :unless => "/bin/egrep '^DIRINFO=$' /workspace/check_mk-1.2.3/setup.sh", - :require => 'Exec[unpack-check_mk-tarball]', - }) - } - it { should contain_file('/workspace/check_mk_setup.conf').that_notifies('Exec[check_mk-setup]') } - it { should contain_file('/etc/nagios/check_mk').with({ - :ensure => 'directory', - :owner => 'nagios', - :group => 'nagios', - :recurse => true, - :require => 'Package[nagios]', - }) - } - it { should contain_file('/etc/nagios/check_mk/hostgroups').with({ - :ensure => 'directory', - :owner => 'nagios', - :group => 'nagios', - :require => 'File[/etc/nagios/check_mk]', - }) - } - it { should contain_exec('check_mk-setup').with({ - :command => '/workspace/check_mk-1.2.3/setup.sh --yes', - :cwd => '/workspace/check_mk-1.2.3', - :refreshonly => true, - :require => [ - 'Exec[change-setup-config-location]', - 'Exec[remove-setup-header]', - 'Exec[unpack-check_mk-tarball]', - 'File[/workspace/check_mk_setup.conf]', - 'File[/etc/nagios/check_mk]', - 'Package[nagios]', - ], - :notify => 'Class[Check_mk::Service]', - }) - } - - end -end diff --git a/spec/classes/check_mk_service_spec.rb b/spec/classes/check_mk_service_spec.rb index bb05c3d..4641803 100644 --- a/spec/classes/check_mk_service_spec.rb +++ b/spec/classes/check_mk_service_spec.rb @@ -1,16 +1,36 @@ require 'spec_helper' -describe 'check_mk::service', :type => :class do - context 'with defaults for all parameters' do - it { should contain_class('check_mk::service') } - it { should contain_service('httpd').with({ - :ensure => 'running', - :enable => 'true', - }) - } - it { should contain_service('omd').with({ - :ensure => 'running', - :enable => 'true', - }) - } + +describe 'check_mk::service' do + on_supported_os.each do |os, os_facts| + context "with required parameters set on #{os}" do + let(:facts) { os_facts } + let(:params) do + { + checkmk_service: 'omd', + httpd_service: 'httpd', + } + end + + it { + is_expected.to compile + + is_expected.to contain_class('check_mk::service') + is_expected.to contain_service('httpd').with( + 'ensure' => 'running', + 'enable' => 'true', + ) + + is_expected.to contain_service('xinetd').with( + 'ensure' => 'running', + 'enable' => 'true', + ) + + is_expected.to contain_service('omd').with( + 'ensure' => 'running', + 'enable' => 'true', + 'hasrestart' => 'true', + ) + } + end end end diff --git a/spec/classes/check_mk_spec.rb b/spec/classes/check_mk_spec.rb index 169438f..9b100f4 100644 --- a/spec/classes/check_mk_spec.rb +++ b/spec/classes/check_mk_spec.rb @@ -1,18 +1,44 @@ require 'spec_helper' -describe 'check_mk', :type => :class do - context 'with defaults for all parameters' do - it { should contain_class('check_mk') } - it { should contain_class('check_mk::install').with({ - :filestore => nil, - :package => 'omd-0.56', - :site => 'monitoring', - :workspace => '/root/check_mk', - }).that_comes_before('Class[check_mk::config]') } - it { should contain_class('check_mk::config').with({ - :host_groups => nil, - :site => 'monitoring', - }).that_comes_before('Class[check_mk::service]') } - it { should contain_class('check_mk::service') } +describe 'check_mk' do + on_supported_os.each do |os, os_facts| + context "with defaults for all parameters on #{os}" do + let(:facts) { os_facts } + + it { + is_expected.to compile + + is_expected.to contain_class('check_mk') + + is_expected.to contain_class('check_mk::install').with( + 'filestore' => nil, + 'package' => 'omd-0.56', + 'site' => 'monitoring', + 'workspace' => '/root/check_mk', + ) + + is_expected.to contain_class('check_mk::config').with( + 'host_groups' => nil, + 'site' => 'monitoring', + 'require' => 'Class[Check_mk::Install]', + ) + + is_expected.to contain_class('check_mk::service').with( + 'subscribe' => 'Class[Check_mk::Config]', + ) + case facts[:osfamily] + when 'RedHat' + is_expected.to contain_service('httpd').with( + 'ensure' => 'running', + 'enable' => 'true', + ) + when 'Debian' + is_expected.to contain_service('apache2').with( + 'ensure' => 'running', + 'enable' => 'true', + ) + end + } + end end end diff --git a/spec/classes/coverage_spec.rb b/spec/classes/coverage_spec.rb deleted file mode 100644 index 12513b8..0000000 --- a/spec/classes/coverage_spec.rb +++ /dev/null @@ -1 +0,0 @@ -at_exit { RSpec::Puppet::Coverage.report! } diff --git a/spec/default_facts.yml b/spec/default_facts.yml new file mode 100644 index 0000000..ea1e480 --- /dev/null +++ b/spec/default_facts.yml @@ -0,0 +1,7 @@ +# Use default_module_facts.yml for module specific facts. +# +# Facts specified here will override the values provided by rspec-puppet-facts. +--- +ipaddress: "172.16.254.254" +is_pe: false +macaddress: "AA:AA:AA:AA:AA:AA" diff --git a/spec/defines/check_mk_agent_mrpe_spec.rb b/spec/defines/check_mk_agent_mrpe_spec.rb index b911d0a..587afa6 100644 --- a/spec/defines/check_mk_agent_mrpe_spec.rb +++ b/spec/defines/check_mk_agent_mrpe_spec.rb @@ -1,34 +1,42 @@ require 'spec_helper' -describe 'check_mk::agent::mrpe', :type => :define do - let :title do - 'checkname' + +describe 'check_mk::agent::mrpe' do + let(:pre_condition) do + "class { 'check_mk::agent': }" end - context 'Unsupported OS' do - context 'with mandatory command' do - let :params do - {:command => 'command'} - end - it 'should fail' do - expect { catalogue }.to raise_error(Puppet::Error, /Creating mrpe.cfg is unsupported for operatingsystem/) - end - end + let(:title) { 'check1' } + let(:params) do + { + command: 'command1', + } end - context 'RedHat Linux' do - let :facts do - { - :operatingsystem => 'redhat', - } - end - context 'with mandatory command' do - let :params do - {:command => 'command'} - end - it { should contain_check_mk__agent__mrpe('checkname') } - it { should contain_concat('/etc/check-mk-agent/mrpe.cfg').with_ensure('present') } - it { should contain_concat__fragment('checkname-mrpe-check').with({ - :target => '/etc/check-mk-agent/mrpe.cfg', - :content => /^checkname command\n$/, - }) + + on_supported_os.each do |os, os_facts| + context "on #{os}" do + let(:facts) { os_facts } + + it { + is_expected.to compile + case facts[:osfamily] + when 'RedHat' + is_expected.to contain_concat('/etc/check-mk-agent/mrpe.cfg').with( + 'ensure' => 'present', + ) + + is_expected.to contain_concat__fragment('check1-mrpe-check').with( + 'target' => '/etc/check-mk-agent/mrpe.cfg', + 'content' => %r{check1 command1\n}, + ) + when 'Debian' + is_expected.to contain_concat('/etc/check_mk/mrpe.cfg').with( + 'ensure' => 'present', + ) + + is_expected.to contain_concat__fragment('check1-mrpe-check').with( + 'target' => '/etc/check_mk/mrpe.cfg', + 'content' => %r{check1 command1\n}, + ) + end } end end diff --git a/spec/defines/check_mk_host_spec.rb b/spec/defines/check_mk_host_spec.rb index 0a60870..7f1d0bc 100644 --- a/spec/defines/check_mk_host_spec.rb +++ b/spec/defines/check_mk_host_spec.rb @@ -1,33 +1,44 @@ require 'spec_helper' -describe 'check_mk::host', :type => :define do - let :title do - 'host' - end - context 'with empty host_tags array' do - let :params do - {:target => 'target'} + +describe 'check_mk::host' do + let(:title) { 'host1' } + + on_supported_os.each do |os, os_facts| + context "on #{os}" do + let(:facts) { os_facts } + let(:params) do + { + target: 'target', + } + end + + it { + is_expected.to contain_check_mk__host('host1') + is_expected.to contain_concat__fragment('check_mk-host1').with( + 'target' => 'target', + 'content' => %r{^ 'host1',\n$}, + 'order' => 11, + ) + } end - it { should contain_check_mk__host('host') } - it { should contain_concat__fragment('check_mk-host').with({ - :target => 'target', - :content => /^ 'host',\n$/, - :order => 11, - }) - } - end - context 'with custom host_tags' do - let :params do - { - :target => 'target', - :host_tags => ['tag1', 'tag2'], + + context "on #{os} with host_tags" do + let(:facts) { os_facts } + let(:params) do + { + target: 'target', + host_tags: ['tag1', 'tag2'], + } + end + + it { + is_expected.to contain_check_mk__host('host1') + is_expected.to contain_concat__fragment('check_mk-host1').with( + 'target' => 'target', + 'content' => %r{^ 'host1|tag1|tag2',\n$}, + 'order' => 11, + ) } end - it { should contain_check_mk__host('host') } - it { should contain_concat__fragment('check_mk-host').with({ - :target => 'target', - :content => /^ 'host|tag1|tag2',\n$/, - :order => 11, - }) - } end end diff --git a/spec/defines/check_mk_hostgroup_spec.rb b/spec/defines/check_mk_hostgroup_spec.rb deleted file mode 100644 index d2ba88e..0000000 --- a/spec/defines/check_mk_hostgroup_spec.rb +++ /dev/null @@ -1,83 +0,0 @@ -require 'spec_helper' -describe 'check_mk::hostgroup', :type => :define do - - context 'with hostgroups, host_tags and description' do - let :title do - 'TEST_HOSTGROUP' - end - hostgroups = { - 'TEST_HOSTGROUP' => { - 'host_tags' => [ - 'tag1', - 'tag2', - ], - 'description' => 'TEST_DESCRIPTION', - }, - } - let :params do - { - :dir => '/dir', - :hostgroups => hostgroups, - :target => 'target' - } - end - it { should contain_check_mk__hostgroup('TEST_HOSTGROUP') } - it { should contain_concat__fragment('check_mk-hostgroup-TEST_HOSTGROUP').with({ - :target => 'target', - :content => /^ \( 'TEST_HOSTGROUP', \[ 'tag1', 'tag2' \], ALL_HOSTS \),\n$/, - :order => 21, - }) - } - expected_file_content = < 'present', - :content => expected_file_content, - }) - } - end - - context 'with hostgroups without description' do - let :title do - 'TEST_HOUSTGROUP_WITH_UNDERSCORES' - end - hostgroups = { - 'TEST_HOUSTGROUP_WITH_UNDERSCORES' => { - 'host_tags' => [ - 'tag1', - 'tag2', - ], - }, - } - let :params do - { - :dir => '/dir', - :hostgroups => hostgroups, - :target => '/target' - } - end - it { should contain_check_mk__hostgroup('TEST_HOUSTGROUP_WITH_UNDERSCORES') } - it { should contain_concat__fragment('check_mk-hostgroup-TEST_HOUSTGROUP_WITH_UNDERSCORES').with({ - :target => '/target', - :content => /^ \( 'TEST_HOUSTGROUP_WITH_UNDERSCORES', \[ 'tag1', 'tag2' \], ALL_HOSTS \),\n$/, - :order => 21, - }) - } - expected_file_content = < 'present', - :content => expected_file_content, - }) - } - end - -end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2c6f566..93b25ec 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1 +1,47 @@ require 'puppetlabs_spec_helper/module_spec_helper' +require 'rspec-puppet-facts' + +require 'spec_helper_local' if File.file?(File.join(File.dirname(__FILE__), 'spec_helper_local.rb')) + +include RspecPuppetFacts + +default_facts = { + puppetversion: Puppet.version, + facterversion: Facter.version, +} + +default_fact_files = [ + File.expand_path(File.join(File.dirname(__FILE__), 'default_facts.yml')), + File.expand_path(File.join(File.dirname(__FILE__), 'default_module_facts.yml')), +] + +default_fact_files.each do |f| + next unless File.exist?(f) && File.readable?(f) && File.size?(f) + + begin + default_facts.merge!(YAML.safe_load(File.read(f), [], [], true)) + rescue => e + RSpec.configuration.reporter.message "WARNING: Unable to load #{f}: #{e}" + end +end + +RSpec.configure do |c| + c.default_facts = default_facts + c.before :each do + # set to strictest setting for testing + # by default Puppet runs at warning level + Puppet.settings[:strict] = :warning + end + c.filter_run_excluding(bolt: true) unless ENV['GEM_BOLT'] + c.after(:suite) do + end +end + +def ensure_module_defined(module_name) + module_name.split('::').reduce(Object) do |last_module, next_module| + last_module.const_set(next_module, Module.new) unless last_module.const_defined?(next_module, false) + last_module.const_get(next_module, false) + end +end + +# 'spec_overrides' from sync.yml will appear below this line diff --git a/templates/agent/check_mk.erb b/templates/agent/check_mk.erb deleted file mode 100644 index 47824a9..0000000 --- a/templates/agent/check_mk.erb +++ /dev/null @@ -1,39 +0,0 @@ -# +------------------------------------------------------------------+ -# | ____ _ _ __ __ _ __ | -# | / ___| |__ ___ ___| | __ | \/ | |/ / | -# | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / | -# | | |___| | | | __/ (__| < | | | | . \ | -# | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ | -# | | -# | Copyright Mathias Kettner 2012 mk@mathias-kettner.de | -# +------------------------------------------------------------------+ -# -# This file is part of Check_MK. -# The official homepage is at http://mathias-kettner.de/check_mk. -# -# check_mk is free software; you can redistribute it and/or modify it -# under the terms of the GNU General Public License as published by -# the Free Software Foundation in version 2. check_mk is distributed -# in the hope that it will be useful, but WITHOUT ANY WARRANTY; with- -# out even the implied warranty of MERCHANTABILITY or FITNESS FOR A -# PARTICULAR PURPOSE. See the GNU General Public License for more de- -# ails. You should have received a copy of the GNU General Public -# License along with GNU Make; see the file COPYING. If not, write -# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, -# Boston, MA 02110-1301 USA. - -service check_mk -{ - type = UNLISTED - port = <%= @port %> - socket_type = stream - protocol = tcp - wait = no - user = <%= @user %> - server = <%= @server %> -<% if @only_from -%> - only_from = 127.0.0.1 <%= @only_from %> -<% end -%> - log_on_success = - disable = no -} diff --git a/templates/agent/encryption.cfg.erb b/templates/agent/encryption.cfg.erb new file mode 100644 index 0000000..8660136 --- /dev/null +++ b/templates/agent/encryption.cfg.erb @@ -0,0 +1,2 @@ +ENCRYPTED=yes +PASSPHRASE=<%= @encryption_secret %>