-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DEV-951 Host Rights API in Kubernetes (#1)
* DEV-951 Host Rights API in Kubernetes - Update to Ruby 3.2 - Add tag and deployment workflows - Add seeds for rights_log - Add v1 version to paths - Add rights_log route - General refactoring * Appease standardrb
- Loading branch information
Showing
18 changed files
with
416 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: Build latest from main and deploy to staging | ||
|
||
on: | ||
workflow_run: | ||
workflows: ['Run Tests'] | ||
branches: ['main'] | ||
types: [completed] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: read | ||
packages: write | ||
|
||
steps: | ||
- uses: hathitrust/github_actions/build@v1 | ||
with: | ||
image: ghcr.io/${{ github.repository }}-unstable | ||
dockerfile: Dockerfile.prod | ||
tag: ${{ github.sha }} | ||
push_latest: true | ||
registry_token: ${{ github.token }} | ||
|
||
# TODO: automate deployment w/ argocd | ||
# for now - update image in | ||
# https://github.com/hathitrust/ht_tanka/blob/main/environments/rights_api/staging/main.jsonnet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--- | ||
name: Docker Tag Latest Release | ||
|
||
on: | ||
release: | ||
types: [released] | ||
|
||
jobs: | ||
tag-release: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: hathitrust/github_actions/tag-release@v1 | ||
with: | ||
registry_token: ${{ github.token }} | ||
existing_tag: ghcr.io/${{ github.repository }}-unstable:${{ github.sha }} | ||
image: ghcr.io/${{ github.repository }} | ||
new_tag: ${{ github.event.release.tag_name }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
FROM ruby:3.2 | ||
ARG UNAME=app | ||
ARG UID=1000 | ||
ARG GID=1000 | ||
|
||
RUN gem install bundler | ||
RUN groupadd -g $GID -o $UNAME | ||
RUN useradd -m -d /usr/src/app -u $UID -g $GID -o -s /bin/bash $UNAME | ||
RUN mkdir -p /gems && chown $UID:$GID /gems | ||
USER $UNAME | ||
COPY --chown=$UID:$GID Gemfile* /usr/src/app/ | ||
WORKDIR /usr/src/app | ||
ENV BUNDLE_PATH /gems | ||
ENV APP_ENV production | ||
RUN bundle install | ||
COPY --chown=$UID:$GID . /usr/src/app | ||
|
||
CMD ["bundle", "exec", "ruby", "lib/rights_api/app.rb", "-o", "0.0.0.0"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require "./lib/rights_api/app" | ||
run RightsAPI::App |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# frozen_string_literal: true | ||
|
||
require "sinatra" | ||
require "sinatra/json" | ||
require "sinatra/reloader" if development? | ||
|
||
require_relative "query" | ||
|
||
module RightsAPI | ||
class App < Sinatra::Base | ||
USAGE = <<~END_USAGE | ||
API_URL => this usage summary | ||
API_URL/access_profiles => contents of the access_profiles table | ||
API_URL/access_profiles/1 => access_profile entry with id=1 | ||
API_URL/access_statements => contents of the access_stmts table | ||
API_URL/access_statements/pd => access_stmts entry with stmt_key=pd | ||
API_URL/attributes => contents of the attributes table | ||
API_URL/attributes/1 => attributes entry with id=1 | ||
API_URL/reasons => contents of the reasons table | ||
API_URL/reasons/1 => reasons entry with id=1 | ||
API_URL/rights/HTID => query rights_current for current rights on HTID | ||
API_URL/rights_log/HTID => query rights_current for rights history on HTID | ||
END_USAGE | ||
|
||
STANDARD_TABLES = %w[attributes access_profiles access_statements reasons sources] | ||
|
||
# Redirect to the current version | ||
get "/" do | ||
redirect(request.url + "v1/") | ||
end | ||
|
||
get "/v1/?" do | ||
json({usage: USAGE}) | ||
end | ||
|
||
get "/v1/rights/:htid" do |htid| | ||
json RightsAPI.rights(htid) | ||
end | ||
|
||
get "/v1/rights_log/:htid" do |htid| | ||
json RightsAPI.rights_log(htid) | ||
end | ||
|
||
# The "all" queries for most tables | ||
STANDARD_TABLES.each do |name| | ||
get "/v1/#{name}/?" do | ||
json(RightsAPI.send(name.to_sym)) | ||
end | ||
end | ||
|
||
# The "by id" queries for most tables | ||
STANDARD_TABLES.each do |name| | ||
get "/v1/#{name}/:id" do |id| | ||
data = (RightsAPI.send name.to_sym)[hash_key(name: name, key: id)] | ||
data = {} if data.nil? | ||
json data | ||
end | ||
end | ||
|
||
private | ||
|
||
# Most tables have integer primary keys, but access_stmts is indexed by string | ||
def hash_key(name:, key:) | ||
(name == "access_statements") ? key.to_s : key.to_i | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "services" | ||
|
||
module RightsAPI | ||
def access_statements | ||
@access_statements ||= db_connection[:access_stmts].to_hash(:stmt_key) | ||
end | ||
|
||
def access_profiles | ||
@access_profiles ||= db_connection[:access_profiles].to_hash(:id) | ||
end | ||
|
||
def attributes | ||
@attributes ||= db_connection[:attributes].to_hash(:id) | ||
end | ||
|
||
def reasons | ||
@reasons ||= db_connection[:reasons].to_hash(:id) | ||
end | ||
|
||
def rights(htid) | ||
rights_query(htid: htid, table: :rights_current) | ||
end | ||
|
||
def rights_log(htid) | ||
rights_query(htid: htid, table: :rights_log) | ||
end | ||
|
||
def sources | ||
@sources ||= db_connection[:sources].to_hash(:id) | ||
end | ||
|
||
module_function :access_profiles, :access_statements, :attributes, :reasons, | ||
:rights, :rights_log, :sources | ||
|
||
private | ||
|
||
def db_connection | ||
Services[:rights_database].db | ||
end | ||
|
||
# Common code for querying rights or rights_log and returning results | ||
def rights_query(htid:, table:) | ||
namespace, id = htid.split(".", 2) | ||
result = [] | ||
db_connection[table] | ||
.where(:namespace => namespace, Sequel.qualify(table, :id) => id) | ||
.order(:time) | ||
.each do |entry| | ||
result << | ||
{ | ||
htid: entry[:namespace] + "." + entry[:id], | ||
namespace: entry[:namespace], | ||
id: entry[:id], | ||
attribute: entry[:attr], | ||
reason: entry[:reason], | ||
source: entry[:source], | ||
access_profile: entry[:access_profile], | ||
time: entry[:time] | ||
} | ||
# Keep note and user out of structure for public API | ||
end | ||
result | ||
end | ||
|
||
module_function :db_connection, :rights_query | ||
end |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.