Class: Katello::Ping

Inherits:
Object
  • Object
show all
Defined in:
app/models/katello/ping.rb

Constant Summary collapse

OK_RETURN_CODE =
'ok'
FAIL_RETURN_CODE =
'FAIL'
PACKAGES =
%w(katello candlepin pulp thumbslug qpid)
SERVICES =
[:pulp, :pulp_auth, :candlepin, :candlepin_auth, :foreman_tasks]

Class Method Summary collapse

Class Method Details

.exception_watch(result) ⇒ Object

check for exception - set the result code properly



83
84
85
86
87
88
89
90
91
92
# File 'app/models/katello/ping.rb', line 83

def exception_watch(result)
  start = Time.new
  yield
  result[:status] = OK_RETURN_CODE
  result[:duration_ms] = ((Time.new - start) * 1000).round.to_s
rescue => e
  Rails.logger.warn(e.backtrace ? [e.message, e.backtrace].join("\n") : e.message)
  result[:status] = FAIL_RETURN_CODE
  result[:message] = e.message
end

.packagesObject

get package information for katello and its components



95
96
97
98
99
# File 'app/models/katello/ping.rb', line 95

def packages
  names = PACKAGES.join("|")
  packages = `rpm -qa | egrep "#{names}"`
  packages.split("\n").sort
end

.ping(services: SERVICES, capsule_id: nil) ⇒ Object

Calls “status” services in all backend engines.

This should be called with User.current set if you want to check pulp_auth

TODO: break up this method rubocop:disable MethodLength



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/models/katello/ping.rb', line 19

def ping(services: SERVICES, capsule_id: nil)
  result = { :status => OK_RETURN_CODE, :services => {}}
  services.each { |service| result[:services][service] = {} }

  # pulp - ping without oauth
  if services.include?(:pulp)
    exception_watch(result[:services][:pulp]) do
      Ping.pulp_without_auth(pulp_url(capsule_id))
    end
  end

  # candlepin - ping without oauth
  if services.include?(:candlepin)
    url = SETTINGS[:katello][:candlepin][:url]
    exception_watch(result[:services][:candlepin]) do
      RestClient.get "#{url}/status"
    end
  end

  # pulp - ping with oauth
  if User.current && services.include?(:pulp_auth)
    exception_watch(result[:services][:pulp_auth]) do
      if result[:services][:pulp][:status] == OK_RETURN_CODE
        Katello.pulp_server.resources.user.retrieve_all
      else
        fail _("Skipped pulp_auth check after failed pulp check")
      end
    end
  else
    result[:services].delete(:pulp_auth)
  end

  if services.include?(:candlepin_auth)
    # candlepin - ping with oauth
    exception_watch(result[:services][:candlepin_auth]) do
      Katello::Resources::Candlepin::CandlepinPing.ping
    end
  end

  if services.include?(:foreman_tasks)
    exception_watch(result[:services][:foreman_tasks]) do
      timeout   = 2
      world     = ForemanTasks.dynflow.world
      executors = world.coordinator.find_worlds(true)
      if executors.empty?
        fail _("foreman-tasks service not running or is not ready yet")
      end

      checks = executors.map { |executor| world.ping(executor.id, timeout) }
      checks.each(&:wait)
      if checks.any?(&:failed?)
        fail _("some executors are not responding, check %{status_url}") % { :status_url => '/foreman_tasks/dynflow/status' }
      end
    end
  end

  # set overall status result code
  result[:services].each_value do |v|
    result[:status] = FAIL_RETURN_CODE unless v[:status] == OK_RETURN_CODE
  end
  result
end

.pulp_url(capsule_id) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'app/models/katello/ping.rb', line 101

def pulp_url(capsule_id)
  if capsule_id
    capsule_content = ::Katello::CapsuleContent.new(SmartProxy.find(capsule_id))
    uri = URI.parse(capsule_content.pulp_url)
    "#{uri.scheme}://#{uri.host.downcase}/pulp/api/v2"
  else
    SETTINGS[:katello][:pulp][:url]
  end
end

.pulp_without_auth(url) ⇒ Object

this checks Pulp is running and responding without need for authentication. We don’t use RestClient.options here because it returns empty string, which is not enough to say pulp is the one that responded



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/models/katello/ping.rb', line 115

def pulp_without_auth(url)
  body = RestClient.get("#{url}/status/")
  fail _("Pulp does not appear to be running.") if body.empty?
  json = JSON.parse(body)

  if json['known_workers'].empty?
    fail _("No pulp workers running.")
  end

  if json['database_connection'] && json['database_connection']['connected'] != true
    fail _("Pulp database connection issue.")
  end

  if json['messaging_connection'] && json['messaging_connection']['connected'] != true
    fail _("Pulp message bus connection issue.")
  end

  json
end