diff --git a/.github/pre-commit b/.github/pre-commit new file mode 100755 index 0000000..e27d91e --- /dev/null +++ b/.github/pre-commit @@ -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 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 8b9802a..dd84fb7 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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: | diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 411ba10..51231f8 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -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: diff --git a/.gitignore b/.gitignore index a6b0bfc..41b8a4f 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ server/ssh/* !server/ssh/README.md Gemfile.lock +.bash_history # rspec failure tracking .rspec_status diff --git a/Dockerfile b/Dockerfile index d9b49aa..1a4c6b1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,20 +1,20 @@ -FROM ruby:3.1 +FROM ruby:3.2 ARG UNAME=app ARG UID=1000 ARG GID=1000 -LABEL maintainer="mrio@umich.edu" - 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 diff --git a/README.md b/README.md index 2b3d5cc..616377e 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Sftp +# SFTP This gem wraps shell `sftp` to make working with it in Ruby scripts easier. @@ -29,7 +29,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 @@ -88,11 +88,48 @@ SFTP.client.ls("directory") ## Development -In the application root folder, set up the ssh_keys: +Clone the repo + +```bash +git clone git@github.com: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/mlibrary/sftp +Bug reports and pull requests are welcome on GitHub at +https://github.com/mlibraray/sftp diff --git a/bin/set_up_development_ssh_keys.sh b/bin/set_up_development_ssh_keys.sh index 78a721f..8c3c947 100755 --- a/bin/set_up_development_ssh_keys.sh +++ b/bin/set_up_development_ssh_keys.sh @@ -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/ @@ -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/ diff --git a/docker-compose.yml b/compose.yml similarity index 79% rename from docker-compose.yml rename to compose.yml index 051e470..cab481a 100644 --- a/docker-compose.yml +++ b/compose.yml @@ -1,14 +1,12 @@ -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: @@ -16,6 +14,4 @@ services: - ./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 diff --git a/.env-dev-values b/env.development similarity index 100% rename from .env-dev-values rename to env.development diff --git a/init.sh b/init.sh new file mode 100755 index 0000000..fdaeae0 --- /dev/null +++ b/init.sh @@ -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 diff --git a/release-please-config.json b/release-please-config.json new file mode 100644 index 0000000..9d3c81e --- /dev/null +++ b/release-please-config.json @@ -0,0 +1,10 @@ +{ + "release-type": "ruby", + "bump-minor-pre-major": true, + "packages": { + ".": { + "release-type": "ruby", + "package-name": "sftp" + } + } +}