Skip to content

Commit

Permalink
fix: updates Dockerfile, creates init.sh, updates github actions work…
Browse files Browse the repository at this point in the history
…flows

Also updates the README with developer startup instructions
  • Loading branch information
niquerio committed Feb 8, 2024
1 parent 1e66aa5 commit ed0c6f3
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 34 deletions.
25 changes: 25 additions & 0 deletions .github/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".
#!/bin/sh

set -e
rubyfiles=$(git diff --cached --name-only --diff-filter=ACM "*.rb" "Gemfile" | tr '\n' ' ')
[ -z "$rubyfiles" ] && exit 0

# Standardize all ruby files
echo "🧹 Formatting staged Ruby files using standardrb ($(echo $rubyfiles | wc -w | awk '{print $1}') total)"
echo "$rubyfiles" | xargs docker compose run -T --rm web bundle exec standardrb --fix

# Add back the modified/prettified files to staging
echo "$rubyfiles" | xargs git add

echo "📋 Running tests with rspec"
docker compose run -T --rm web bundle exec rspec --format progress

exit 0
15 changes: 7 additions & 8 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,24 @@ on:
workflows: [ 'Run Tests' ]
branches: [ 'main' ]
types: [ completed ]

permissions:
contents: write
pull-requests: write
jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: GoogleCloudPlatform/release-please-action@v2
- uses: google-github-actions/release-please-action@v4
id: release
with:
release-type: ruby
package-name: sftp
bump-minor-pre-major: true
version-file: "lib/sftp/version.rb"
# Checkout code if release was created
- uses: actions/checkout@v2
- uses: actions/checkout@v4
if: ${{ steps.release.outputs.release_created }}
# Setup ruby if a release was created
- uses: ruby/setup-ruby@v1
if: ${{ steps.release.outputs.release_created }}
with:
ruby-version: 3.1
ruby-version: 3.2
- name: Set Credentials
if: ${{ steps.release.outputs.release_created }}
run: |
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ jobs:
name: Ruby ${{ matrix.ruby }}
strategy:
matrix:
ruby: [2.6, 2.7, 3.0, 3.1]
ruby: [2.7, 3.0, 3.1, 3.2]

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
server/ssh/*
!server/ssh/README.md
Gemfile.lock
.bash_history

# rspec failure tracking
.rspec_status
10 changes: 5 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
FROM ruby:3.1
FROM ruby:3.2
ARG UNAME=app
ARG UID=1000
ARG GID=1000

LABEL maintainer="[email protected]"

RUN apt-get update -yqq && apt-get install -yqq --no-install-recommends \
vim

RUN gem install bundler:2.3
RUN gem install bundler
ENV BUNDLE_PATH /gems
ENV PATH="$PATH:/app/exe:/app/bin"

RUN groupadd -g ${GID} -o ${UNAME}
RUN useradd -m -d /app -u ${UID} -g ${GID} -o -s /bin/bash ${UNAME}
RUN mkdir -p /gems && chown ${UID}:${GID} /gems
USER $UNAME


WORKDIR /app
COPY --chown=${UID}:${GID} . /app
RUN bundle install
50 changes: 44 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Sftp
# SFTP

This gem wraps shell `sftp` to make working with it in ruby scripts easier

## Installation

Add this line to your application's Gemfile (niquerio todo: figure out what actually needs to be put here):
Add this line to your application's Gemfile (niquerio todo: figure out what
actually needs to be put here):

```ruby
gem 'sftp'
Expand All @@ -27,7 +28,7 @@ require 'sftp'
SFTP.configure do |config|
config.user = "your_sftp_user"
config.host = "your_sftp_host"
congig.key_paty = "path/to/your/ssh/key/file"
config.key_path = "path/to/your/ssh/key/file"
end
client = SFTP.client
Expand Down Expand Up @@ -64,11 +65,48 @@ SFTP.client.ls("directory")

## Development

In the application root folder set up the ssh_keys
Clone the repo

```bash
git clone [email protected]:mlibrary/account.git
cd account
```

run the `init.sh` script. This will copy a pre-commit hook for git, build the
container, and set up ssh keys for development.
```bash
./init.sh
```
start containers

```bash
docker compose up -d
```

The compose.yml has a fileserver service running sftp. The files are in the
`server/files` directory.

To try out the gem you can run:
```bash
docker compose run --rm app console
SFTP.client.ls
```
./bin/set_up_development_ssh_keys.sh

This will load the gem in irb, and connect you to the sftp service in compose.yml

### Troubleshooting
If the the `app` service can't connect to the `sftp` service, try restarting by
doing:
```bash
docker compose down
docker compose up -d
```

The ssh keys volume mounted in may not have been properly copied to
`authorized_keys` in the `fileserver` service, and doing this hard restart will
get the appropriate ones copied in.

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/mlibraray/sftp
Bug reports and pull requests are welcome on GitHub at
https://github.com/mlibraray/sftp
11 changes: 6 additions & 5 deletions bin/set_up_development_ssh_keys.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#!/bin/bash

#clear out any current ssh keys
echo "🧹 clear out any current ssh keys"
rm server/ssh/ssh_*
rm ssh_*


#generate the host ssh keys for the sftp service
echo "🔑 generating host ssh keys for the sftp service"
ssh-keygen -t ed25519 -f ssh_host_ed25519_key < /dev/null
ssh-keygen -t rsa -b 4096 -f ssh_host_rsa_key < /dev/null


echo "🚛 move the keys to ssh dir"
#move the keys into the sftp/ssh directory so they can be picked up
#by docker-compose bind mounts for the sftp service
mv ssh_host_ed25519_key server/ssh/
Expand All @@ -18,10 +17,12 @@ mv ssh_host_rsa_key server/ssh/
mv ssh_host_rsa_key.pub .ssh/known_hosts

#remove the unnecessary files
echo "🧹 removing unecessary ssh_host* files"
rm ssh_host*

#generate actual host login keys
echo "🔑 generate actual host login keys"
ssh-keygen -t rsa -b 4096 -f ssh_client_rsa_key < /dev/null

echo "🚛 Moving client ssh keys to server"
mv ssh_client_rsa_key.pub server/ssh/
mv ssh_client_rsa_key server/ssh/
12 changes: 4 additions & 8 deletions docker-compose.yml → compose.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
version: '3'

services:
web:
app:
build: .
volumes:
- .:/app
- ./server/ssh/ssh_client_rsa_key:/etc/secret-volume/id_rsa:ro
- gem_cache:/gems
env_file:
- .env-dev-values
- env.development

fileserver:
image: 'atmoz/sftp'
volumes:
- ./server/files:/home/my_user/files
- ./server/ssh/ssh_client_rsa_key.pub:/home/my_user/.ssh/keys/id_rsa.pub:ro
- ./server/ssh/ssh_host_ed25519_key:/etc/ssh/ssh_host_ed25519_key
- ./server/ssh/ssh_host_rsa_key:/etc/ssh/ssh_host_rsa_key
command: my_user:1001
volumes:
gem_cache:
command: my_user::1001
File renamed without changes.
14 changes: 14 additions & 0 deletions init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

if [ -f ".git/hooks/pre-commit" ]; then
echo "🪝 .git/hooks/pre-commit exists. Leaving alone"
else
echo "🪝 .git/hooks/pre-commit does not exist. Copying .github/pre-commit to .git/hooks/"
cp .github/pre-commit .git/hooks/pre-commit
fi

echo "🚢 Build docker images"
docker compose build

echo "🔑 Set up development keys"
./bin/set_up_development_ssh_keys.sh
10 changes: 10 additions & 0 deletions release-please-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"release-type": "ruby",
"bump-minor-pre-major": true,
"packages": {
".": {
"release-type": "ruby",
"package-name": "sftp"
}
}
}

0 comments on commit ed0c6f3

Please sign in to comment.