Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vb 3788 #1336

Merged
merged 4 commits into from
Jun 28, 2024
Merged

Vb 3788 #1336

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ references:
--build-arg COMMIT_ID=${CIRCLE_SHA1} \
--build-arg BUILD_DATE=${BUILD_DATE} \
--build-arg BUILD_TAG=${CIRCLE_BRANCH} \
--build-arg BUILD_NUMBER=${BUILD_DATE}.${CIRCLE_BUILD_NUM}.${CIRCLE_SHA1:0:6} \
--build-arg GIT_BRANCH=${CIRCLE_BRANCH} \
--build-arg GIT_REF=${CIRCLE_SHA1} \
-t app .

# push_docker_image: &push_docker_image
Expand Down
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
FROM ruby:3.2.2-bullseye

ARG BUILD_NUMBER
ARG GIT_BRANCH
ARG GIT_REF

RUN \
set -ex \
&& apt-get update \
Expand Down Expand Up @@ -63,6 +67,10 @@ RUN bundle update --bundler
RUN bundle install --without development test --jobs 2 --retry 3
COPY . /app

ENV BUILD_NUMBER=${BUILD_NUMBER}
ENV GIT_BRANCH=${GIT_BRANCH}
ENV GIT_REF=${GIT_REF}

RUN mkdir -p /home/appuser && \
useradd appuser -u 1001 --user-group --home /home/appuser && \
chown -R appuser:appuser /app && \
Expand Down
26 changes: 26 additions & 0 deletions app/controllers/health_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class HealthController < ApplicationController
def index
nomis_health = Healthcheck::NomisApiCheck.new('Nomis API healthcheck')
vsip_health = Healthcheck::VsipApiCheck.new('Vsip API healthcheck')

render status:, json: {
status: nomis_health.ok? && vsip_health.ok? ? :UP : :DOWN,
components: {
nomis: {
status: nomis_health.ok? ? :UP : :DOWN,
detail: nomis_health.ok? ? :UP : nomis_health.report[:error]
},
vsip: {
status: vsip_health.ok? ? :UP : :DOWN,
detail: vsip_health.ok? ? :UP : vsip_health.report[:error]
}
},
uptime: Time.zone.now - Rails.configuration.booted_at,
build: {
buildNumber: ENV['BUILD_NUMBER'],
gitRef: ENV['GIT_REF']
},
version: ENV['BUILD_NUMBER']
}
end
end
17 changes: 17 additions & 0 deletions app/controllers/info_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

class InfoController < ApplicationController
def index
render json: {
git: {
branch: ENV['GIT_BRANCH']
},
build: {
artifact: 'prison-visits-public',
version: ENV['BUILD_NUMBER'],
name: 'prison-visits-public'
},
productId: ENV['PRODUCT_ID']
}
end
end
21 changes: 21 additions & 0 deletions app/services/healthcheck/nomis_api_check.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Healthcheck
class NomisApiCheck
include CheckComponent

def initialize(description)
build_report(description) do
{ ok: healthy_pvb_connection }
end
end

private

def healthy_pvb_connection
client.healthcheck.status == 200
end

def client
Nomis::Client.new(Rails.configuration.prison_api_host)
end
end
end
21 changes: 21 additions & 0 deletions app/services/healthcheck/vsip_api_check.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Healthcheck
class VsipApiCheck
include CheckComponent

def initialize(description)
build_report(description) do
{ ok: healthy_pvb_connection }
end
end

private

def healthy_pvb_connection
client.healthcheck.status == 200
end

def client
Vsip::Client.new(Rails.configuration.vsip_host)
end
end
end
4 changes: 4 additions & 0 deletions app/services/nomis/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ def get(route, params = {})
request(:get, route, params, idempotent: true)
end

def healthcheck
@connection.head(path: 'health')
end

private

def request(method, route, params, idempotent:, options: {})
Expand Down
4 changes: 4 additions & 0 deletions app/services/vsip/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ def get(route, params = {})
request(:get, route, params, idempotent: true)
end

def healthcheck
@connection.head(path: 'health')
end

private

def request(method, route, params, idempotent:, options: {})
Expand Down
2 changes: 2 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require_relative '../app/middleware/http_method_not_allowed'
require_relative '../app/middleware/robots_tag'
require 'ostruct'
require 'active_support'

# require "action_mailer/railtie"
# require "action_mailbox/engine"
Expand Down Expand Up @@ -126,5 +127,6 @@ class Application < Rails::Application
config.vsip_host = ENV['VSIP_HOST']
config.vsip_supported_prisons_retrieved = false
config.use_vsip = ENV['USE_VSIP'] == 'true'
config.booted_at = Time.current
end
end
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
constraints format: 'json' do
get 'ping', to: 'ping#index'
get 'healthcheck', to: 'healthcheck#index'
get 'info', to: 'info#index'
get 'health', to: 'health#index'
end

constraints format: 'html' do
Expand Down
2 changes: 2 additions & 0 deletions deploy/development/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ spec:
memory: "125Mi"
cpu: "15m"
env:
- name: PRODUCT_ID
value: "DPS031"
- name: SECRET_KEY_BASE
valueFrom:
secretKeyRef:
Expand Down
2 changes: 2 additions & 0 deletions deploy/production/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ spec:
memory: "125Mi"
cpu: "15m"
env:
- name: PRODUCT_ID
value: "DPS031"
- name: SECRET_KEY_BASE
valueFrom:
secretKeyRef:
Expand Down
2 changes: 2 additions & 0 deletions deploy/staging/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ spec:
memory: "125Mi"
cpu: "15m"
env:
- name: PRODUCT_ID
value: "DPS031"
- name: SECRET_KEY_BASE
valueFrom:
secretKeyRef:
Expand Down
40 changes: 40 additions & 0 deletions spec/controllers/health_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'rails_helper'

RSpec.describe HealthController, type: :controller do
let(:parsed_body) {
JSON.parse(response.body)
}

subject(:index_request) { get :index }

context 'when everything is OK' do
before do
allow_any_instance_of(Nomis::Client).to receive(:healthcheck).and_return(OpenStruct.new(status: 200))
allow_any_instance_of(Vsip::Client).to receive(:healthcheck).and_return(OpenStruct.new(status: 200))
end

it { is_expected.to be_successful }

it 'returns the healthcheck data as JSON' do
index_request

expect(parsed_body['components']['nomis']['status']).to eq('UP')
expect(parsed_body['components']['vsip']['status']).to eq('UP')
expect(parsed_body['status']).to eq('UP')
end
end

context 'when the healthcheck is not OK' do
before do
allow_any_instance_of(Nomis::Client).to receive(:healthcheck).and_return(OpenStruct.new(status: 500))
allow_any_instance_of(Vsip::Client).to receive(:healthcheck).and_return(OpenStruct.new(status: 500))
end

it 'returns the healthcheck data as JSON' do
index_request

expect(parsed_body["components"]).to eq({ "nomis" => { "detail" => nil, "status" => "DOWN" },
"vsip" => { "detail" => nil, "status" => "DOWN" } })
end
end
end
30 changes: 30 additions & 0 deletions spec/controllers/info_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'rails_helper'

RSpec.describe InfoController, type: :controller do
let(:parsed_body) {
JSON.parse(response.body)
}

subject(:index_request) { get :index }

context 'when everything is OK' do
before do
ENV['GIT_BRANCH'] = 'GIT_BRANCH'
ENV['BUILD_NUMBER'] = 'BUILD_NUMBER'
ENV['PRODUCT_ID'] = 'PRODUCT_ID'
end

it { is_expected.to be_successful }

it 'returns the healthcheck data as JSON' do
index_request

expect(parsed_body).to eq(
'build' => { "artifact" => "prison-visits-public",
"name" => "prison-visits-public",
"version" => "BUILD_NUMBER" }, "git" => { "branch" => "GIT_BRANCH" },
"productId" => "PRODUCT_ID"
)
end
end
end
6 changes: 6 additions & 0 deletions spec/services/nomis/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,10 @@
}.to raise_error(Nomis::APIError)
end
end

describe 'healthcheck' do
it 'run healthcheck' do
expect(described_class.new(Rails.configuration.prison_api_host).healthcheck.body).to eq('')
end
end
end
6 changes: 6 additions & 0 deletions spec/services/vsip/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,10 @@ def body
expect(described_class.new(Rails.configuration.vsip_host).send(:params_options, :method, :params)).to eq({ query: :params })
end
end

describe 'healthcheck' do
it 'run healthcheck' do
expect(described_class.new(Rails.configuration.vsip_host).healthcheck.body).to eq('')
end
end
end