Skip to content

Commit

Permalink
Merge pull request #61 from curationexperts/okc
Browse files Browse the repository at this point in the history
Add OK Computer checks for core system services
  • Loading branch information
mark-dce authored Dec 14, 2021
2 parents 7a6789d + bdaefb1 commit 90aea38
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
15 changes: 15 additions & 0 deletions app/controllers/checks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true
class ChecksController < ApplicationController
before_action :authenticate_user!
before_action :ensure_admin!
with_themed_layout 'dashboard'
def index
@results = OkComputer::Registry.all.run
end

private

def ensure_admin!
authorize! :read, :admin_dashboard
end
end
16 changes: 16 additions & 0 deletions app/views/checks/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<h1>System Status</h1>


<% @results.each do |r| %>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><%=r.registrant_name%> </h3>
</div>
<div class="panel-body">
<span class="text-<%= r.failure_occurred ? 'danger' : 'success' %>">
<%=r.success?%>
</span>
</div>
</div>

<%end%>
31 changes: 31 additions & 0 deletions config/initializers/okcomputer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
OkComputer.mount_at = false # do not mount okcomputer endpoint at all

class HttpCheck < OkComputer::Check
def initialize(url)
@url = url
super()
end
def check
system("curl -s '#{@url}'") ?
mark_message("Check passed") : (mark_failure && mark_message("Check failed"))
end
end
class ServiceCheck < OkComputer::Check
def initialize(service)
@service = service
super()
end
def check
system("systemctl is-active #{@service}") ?
mark_message("Check passed") : (mark_failure && mark_message("Check failed"))
end
end


OkComputer::Registry.register "redis", ServiceCheck.new('redis-server')
OkComputer::Registry.register "solr", HttpCheck.new(ENV['SOLR_URL'])
OkComputer::Registry.register "fedora", HttpCheck.new(ENV['FEDORA_URL'])
OkComputer::Registry.register "vips", ServiceCheck.new('tomcat9')
OkComputer::Registry.register "postgres", ServiceCheck.new('postgresql')
OkComputer::Registry.register "antivirus service", ServiceCheck.new('clamav-daemon')
OkComputer::Registry.register "antivirus updates", ServiceCheck.new('clamav-freshclam')
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Rails.application.routes.draw do
resource :theme, only: [:edit, :update]
resources :jobs, only: [:index, :new, :show]
resources :checks, only: [:index]
resources :preflights, only: [:index, :new, :create, :show]
resources :imports, only: [:index, :new, :create, :show]

Expand Down
26 changes: 26 additions & 0 deletions spec/requests/checks_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true
require 'rails_helper'

RSpec.describe "/checks", type: :request do
context "not logged in" do
it "redirects" do
get checks_path
expect(response).to redirect_to new_user_session_path
end
end
context "logged in as admin" do
let(:admin) { User.create(email: 'test@example.com', password: '123456', roles: [Role.create(name: 'admin')]) }
before do
sign_in admin
end

describe "GET /index" do
it "renders a successful response" do
get checks_path
expect(response).to be_successful
expect(assigns(:results)).not_to be_nil
expect(response).to render_template('layouts/hyrax/dashboard')
end
end
end
end

0 comments on commit 90aea38

Please sign in to comment.