Project

General

Profile

« Previous | Next » 

Revision 35bb7807

Added by Kavita Gaikwad over 6 years ago

Fixes #20870 - hammer ping check with default tag

View differences:

definitions/checks/hammer_ping.rb
class Checks::HammerPing < ForemanMaintain::Check
include ForemanMaintain::Concerns::Hammer
metadata do
label :hammer_ping
for_feature :hammer
description 'Check whether all services are running using hammer ping'
tags :default
end
def run
result = feature(:hammer).hammer_ping_cmd
assert(result[:success],
result[:message],
:next_steps => Procedures::KatelloService::Restart.new(:only => result[:data]))
end
end
definitions/features/hammer.rb
class Features::Hammer < ForemanMaintain::Feature
metadata do
label :hammer
end
SERVICES_MAPPING = {
'candlepin_auth' => %w[postgresql tomcat],
'candlepin' => %w[postgresql tomcat],
'pulp_auth' => %w[pulp_resource_manager pulp_workers pulp_celerybeat],
'pulp' => %w[pulp_resource_manager pulp_workers pulp_celerybeat],
'foreman_tasks' => %w[foreman-tasks]
}.freeze
def hammer_ping_cmd
cmd_output = exec_hammer_cmd('--output json ping', true)
return init_result_obj(false, cmd_output) if cmd_output.is_a?(String)
resources_failed = find_resources_which_failed(cmd_output.first)
return init_result_obj if resources_failed.empty?
services = map_resources_with_services(resources_failed)
msg_to_show = "#{resources_failed.join(', ')} resource(s) are failing."
init_result_obj(false, msg_to_show, services)
end
def find_resources_which_failed(hammer_ping_output)
resources_failed = []
hammer_ping_output.each do |resource, resp_obj|
resources_failed << resource if /FAIL/ =~ resp_obj['Status']
end
resources_failed
end
private
def map_resources_with_services(resources)
service_names = []
resources.each do |resource|
service_names.concat(SERVICES_MAPPING[resource])
end
service_names
end
def init_result_obj(success_val = true, message = '', data = [])
{
:success => success_val,
:message => message,
:data => data
}
end
def exec_hammer_cmd(cmd, required_json = false)
response = ForemanMaintain::Utils::Hammer.instance.run_command(cmd)
json_str = json_parse(response) if required_json
json_str ? json_str : response
end
end
definitions/features/katello_service.rb
label :katello_service
end
RETRIES_FOR_SERVICES_RESTART = 5
PING_RETRY_INTERVAL = 30
def make_stop(spinner, options = {})
services = find_services_for_only_filter(running_services, options)
if services.empty?
......
execute!("katello-service stop #{filters}")
yield
ensure
spinner.update 'Starting the katello services..'
spinner.update 'Starting katello services..'
execute("katello-service start #{filters}")
end
end
......
spinner.update 'No katello service to start'
else
filters = "--only #{services.join(',')}"
spinner.update 'Starting the katello services..'
spinner.update 'Starting katello services..'
execute!("katello-service start #{filters}")
end
end
def restart(options = {})
if options[:only] || options[:exclude]
filters = construct_filters_for_restart(options)
execute!("katello-service restart #{filters}")
else
execute!('katello-service restart')
end
end
def hammer_ping_retry(spinner)
RETRIES_FOR_SERVICES_RESTART.times do |retry_count|
msg = "Try #{retry_count + 1}/#{RETRIES_FOR_SERVICES_RESTART}: checking status by hammer ping"
spinner.update msg
result = feature(:hammer).hammer_ping_cmd
if result[:success]
spinner.update 'All services are running.'
break
elsif retry_count < (RETRIES_FOR_SERVICES_RESTART - 1)
apply_sleep_before_retry(spinner, result)
end
end
rescue StandardError => e
logger.error e.message
end
private
def apply_sleep_before_retry(spinner, result)
puts "\n#{result[:message]}"
spinner.update "Waiting #{PING_RETRY_INTERVAL} seconds before retry."
sleep PING_RETRY_INTERVAL
end
def construct_filters_for_restart(options)
filters = ''
if options[:only]
filters += "--only #{options[:only].join(',')}" unless options[:only].empty?
end
if options[:exclude]
filters += "--exclude #{options[:exclude].join(',')}" unless options[:exclude].empty?
end
filters
end
def find_services_for_only_filter(curr_services, options)
defaults = { :only => [], :exclude => [] }
options = defaults.merge(options)
definitions/procedures/katello_service/restart.rb
module Procedures::KatelloService
class Restart < ForemanMaintain::Procedure
metadata do
description 'katello-service restart'
param :only, 'A comma-separated list of services to include', :array => true
param :exclude, 'A comma-separated list of services to skip', :array => true
end
def run
with_spinner('restarting katello service(s)') do |spinner|
spinner.update('Restarting services')
feature(:katello_service).restart(:only => @only, :exclude => @exclude)
feature(:katello_service).hammer_ping_retry(spinner)
end
end
def runtime_message
msg = 'katello-service restart'
msg += "--only #{@only.join(',')}" unless @only.empty?
msg += "--exclude #{@exclude.join(',')}" unless @exclude.empty?
msg
end
end
end
lib/foreman_maintain.rb
require 'json'
require 'logger'
require 'yaml'
require 'timeout'
module ForemanMaintain
require 'foreman_maintain/core_ext'
test/definitions/checks/hammer_ping_test.rb
require 'test_helper'
describe Checks::HammerPing do
include DefinitionsTestHelper
subject do
Checks::HammerPing.new
end
it 'passes when all services are running' do
assume_feature_present(:hammer, :hammer_ping_cmd => hammer_ping_result_for_success)
result = run_check(subject)
assert result.success?, 'Check expected to succeed'
end
it 'fails when any of the service is not running' do
assume_feature_present(:hammer, :hammer_ping_cmd => hammer_ping_result_for_fail)
result = run_check(subject)
assert result.fail?, 'Check expected to fail'
error_msg = 'foreman_tasks resource(s) are failing.'
assert_match error_msg, result.output
assert_equal [Procedures::KatelloService::Restart], subject.next_steps.map(&:class)
end
def hammer_ping_result_for_success
{
:success => true,
:message => '',
:data => []
}
end
def hammer_ping_result_for_fail
{
:success => false,
:message => 'foreman_tasks resource(s) are failing.',
:data => ['foreman_tasks']
}
end
end

Also available in: Unified diff