Skip to content

Commit

Permalink
Merge pull request #3 from mlibrary/move-to-sftp-gem
Browse files Browse the repository at this point in the history
Move to sftp gem
  • Loading branch information
niquerio authored Jun 16, 2022
2 parents 4b6a2cc + db9e904 commit 852c3b6
Show file tree
Hide file tree
Showing 15 changed files with 53 additions and 38 deletions.
1 change: 1 addition & 0 deletions .env-example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BUNDLE_RUBYGEMS__PKG__GITHUB__COM="Your Github Personal Access Token with read:packages access"
9 changes: 5 additions & 4 deletions .github/workflows/build-main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ jobs:
context: .
file: Dockerfile.prod
push: true
secrets: |
"gh_package_read_token=${{ secrets.GH_PACKAGE_READ_TOKEN }}"
tags: |
ghcr.io/mlibrary/${{ secrets.IMAGE_NAME }}:latest
ghcr.io/mlibrary/${{ secrets.IMAGE_NAME }}:${{ github.sha }}
- name: Deploy to testing
uses: mlibrary/deploy-to-kubernetes@v1
uses: mlibrary/deploy-to-kubernetes@v2
with:
github_username: ${{ github.actor }}
github_token: ${{ secrets.GITHUB_TOKEN }}
image: mlibrary/${{ secrets.IMAGE_NAME }}:${{ github.sha }}
registry_token: ${{ secrets.GITHUB_TOKEN }}
image: ghcr.io/mlibrary/${{ secrets.IMAGE_NAME }}:${{ github.sha }}
cluster_ca: ${{ secrets.HATCHER_CLUSTER_CA }}
cluster_server: https://hatcher.kubernetes.lib.umich.edu
namespace_token: ${{ secrets.HATCHER_TOKEN }}
Expand Down
7 changes: 3 additions & 4 deletions .github/workflows/manual-deploy-production.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,10 @@ jobs:
push: true
tags: ghcr.io/mlibrary/${{ secrets.IMAGE_NAME }}:${{steps.tag_check.outputs.tag}}
- name: Deploy to ${{ env.environment }}
uses: mlibrary/deploy-to-kubernetes@v1
uses: mlibrary/deploy-to-kubernetes@v2
with:
github_username: ${{ github.actor }}
github_token: ${{ secrets.GITHUB_TOKEN }}
image: mlibrary/${{ secrets.IMAGE_NAME }}:${{ steps.tag_check.outputs.tag }}
registry_token: ${{ secrets.GITHUB_TOKEN }}
image: ghcr.io/mlibrary/${{ secrets.IMAGE_NAME }}:${{ github.sha }}
cluster_ca: ${{ secrets.HATCHER_CLUSTER_CA }}
cluster_server: https://hatcher.kubernetes.lib.umich.edu
namespace_token: ${{ secrets.HATCHER_TOKEN }}
Expand Down
9 changes: 5 additions & 4 deletions .github/workflows/manual-deploy-testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ jobs:
context: .
file: Dockerfile.prod
push: true
secrets: |
"gh_package_read_token=${{ secrets.GH_PACKAGE_READ_TOKEN }}"
tags: ghcr.io/mlibrary/${{ secrets.IMAGE_NAME }}:${{steps.tag_check.outputs.tag}}
- name: Deploy to ${{ env.environment }}
uses: mlibrary/deploy-to-kubernetes@v1
uses: mlibrary/deploy-to-kubernetes@v2
with:
github_username: ${{ github.actor }}
github_token: ${{ secrets.GITHUB_TOKEN }}
image: mlibrary/${{ secrets.IMAGE_NAME }}:${{ steps.tag_check.outputs.tag }}
registry_token: ${{ secrets.GITHUB_TOKEN }}
image: ghcr.io/mlibrary/${{ secrets.IMAGE_NAME }}:${{ github.sha }}
cluster_ca: ${{ secrets.HATCHER_CLUSTER_CA }}
cluster_server: https://hatcher.kubernetes.lib.umich.edu
namespace_token: ${{ secrets.HATCHER_TOKEN }}
Expand Down
7 changes: 3 additions & 4 deletions .github/workflows/tag-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ jobs:
docker push $PROD_IMAGE_ID:${{ env.TAG }}
docker push $PROD_IMAGE_ID:latest
- name: Deploy to production
uses: mlibrary/deploy-to-kubernetes@v1
uses: mlibrary/deploy-to-kubernetes@v2
with:
github_username: ${{ github.actor }}
github_token: ${{ secrets.GITHUB_TOKEN }}
image: mlibrary/${{ secrets.IMAGE_NAME }}:${{ env.TAG }}
registry_token: ${{ secrets.GITHUB_TOKEN }}
image: ghcr.io/mlibrary/${{ secrets.IMAGE_NAME }}:${{ github.sha }}
cluster_ca: ${{ secrets.HATCHER_CLUSTER_CA }}
cluster_server: https://hatcher.kubernetes.lib.umich.edu
namespace_token: ${{ secrets.HATCHER_TOKEN }}
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Create .env file
run: cat .env-dev-values > .env
- name: Load .env file
Expand All @@ -19,6 +19,8 @@ jobs:
with:
ruby-version: 3.1.0
bundler-cache: true
env:
BUNDLE_RUBYGEMS__PKG__GITHUB__COM: ${{secrets.GH_PACKAGE_READ_TOKEN}}
- name: Run linter for Ruby
run: bundle exec standardrb
- name: Run tests
Expand Down
7 changes: 5 additions & 2 deletions Dockerfile.prod
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ RUN mkdir -p /gems && chown ${UID}:${GID} /gems


COPY --chown=${UID}:${GID} . /app
USER $UNAME

ENV BUNDLE_PATH /gems
ENV BUNDLE_WITHOUT development:test

WORKDIR /app

RUN bundle _${BUNDLER_VERSION}_ install
RUN --mount=type=secret,id=gh_package_read_token \
read_token="$(cat /run/secrets/gh_package_read_token)" \
&& BUNDLE_RUBYGEMS__PKG__GITHUB__COM=${read_token} bundle _${BUNDLER_VERSION}_ install

USER $UNAME

CMD ["bundle", "exec", "ruby", "alma_webhook.rb", "-o", "0.0.0.0"]
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ gem "sinatra"
gem "puma"
gem "sidekiq"

source "https://rubygems.pkg.github.com/mlibrary" do
gem "sftp", "0.1.0"
end

group :development, :test do
gem "pry"
gem "pry-byebug"
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ GEM
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)

GEM
remote: https://rubygems.pkg.github.com/mlibrary/
specs:
sftp (0.1.0)

PLATFORMS
x86_64-linux

Expand All @@ -107,6 +112,7 @@ DEPENDENCIES
puma
rack-test
rspec
sftp (= 0.1.0)!
sidekiq
simplecov
sinatra
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ build web container
docker-compose build web
```

set up your .env file
```
cp .env-example .env
```

Replace the value for `BUNDLE_RUBYGEMS__PKG__GITHUB__COM` in the `.env` file with your [personal access token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token) that has `read:packages` access.

bundle install gems to a docker volume
```
docker-compose run --rm web bundle install
Expand Down
8 changes: 7 additions & 1 deletion alma_webhook.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
require "sinatra"
require "json"
require "sidekiq"
require "sftp"
require "byebug" if settings.environment == :development

require "./lib/message_validator"
require "./lib/sftp"
require "./lib/indexing_action"
require "./lib/indexing_jobs_generator"
require "./lib/message_router"

SFTP.configure do |config|
config.user = ENV.fetch("ALMA_FILES_USER")
config.host = ENV.fetch("ALMA_FILES_HOST")
config.key_path = ENV.fetch("SSH_KEY_PATH")
end

get "/" do
content_type :json
{"challenge" => params["challenge"]}.to_json
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ services:
ports:
- "4567:4567"
env_file:
- .env
- .env-dev-values
volumes:
- .:/app
Expand Down
2 changes: 1 addition & 1 deletion lib/indexing_jobs_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def self.match?(data)
data["job_instance"]["status"]["value"] == "COMPLETED_SUCCESS"
end

def initialize(data: nil, job_id: nil, sftp: SFTP.new, logger: Logger.new($stdout),
def initialize(data: nil, job_id: nil, sftp: SFTP.client, logger: Logger.new($stdout),
push_indexing_jobs: lambda do |job_name:, files:, solr_url:|
Sidekiq::Client.push_bulk("class" => job_name, "args" => files.map { |x| [x, solr_url] })
end)
Expand Down
15 changes: 0 additions & 15 deletions lib/sftp.rb

This file was deleted.

4 changes: 2 additions & 2 deletions spec/lib/indexing_jobs_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"file_#{@job_id_from_data}_delete_1.tar.gz",
"file_new.tar.gz"
]
@sftp_double = instance_double(SFTP, ls: @files)
@sftp_double = instance_double(SFTP::Client, ls: @files)
@logger_double = instance_double(Logger, info: nil)
@push_bulk_double = double("SidekiqClient", push_bulk: nil)
@push_indexing_jobs = lambda do |job_name:, files:, solr_url:|
Expand Down Expand Up @@ -81,7 +81,7 @@
"file_#{@job_id_from_data}_delete.tar.gz",
"file_new.tar.gz"
]
@sftp_double = instance_double(SFTP, ls: @files)
@sftp_double = instance_double(SFTP::Client, ls: @files)
@logger_double = instance_double(Logger, info: nil)
@push_bulk_double = double("SidekiqClient", push_bulk: nil)
@push_indexing_jobs = lambda do |job_name:, files:, solr_url:|
Expand Down

0 comments on commit 852c3b6

Please sign in to comment.