From 78927aa7a20fc67063f43f960aaf2f10b88152f7 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Mar 2023 15:00:56 +0100 Subject: [PATCH 01/19] Create CONTRIBUTING.md --- CONTRIBUTING.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 CONTRIBUTING.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 00000000..31ea6e8a --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,3 @@ +# Contributing to MRSK development + +MRSK is written in Ruby. You should have Ruby 3.2+ installed on your machine in order to work on MRSK. If that's already setup, run `bundle` in the root directory to install all dependencies. Then you can run `bin/test` to run all tests. From dcc4db113732f04158b5f94b7bb0f9df12eca63d Mon Sep 17 00:00:00 2001 From: Rasmus Kjellberg <2277443+kjellberg@users.noreply.github.com> Date: Fri, 3 Mar 2023 17:44:04 +0100 Subject: [PATCH 02/19] Update CONTRIBUTING.md --- CONTRIBUTING.md | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 31ea6e8a..7d8ad883 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,3 +1,42 @@ # Contributing to MRSK development +Thank you for considering contributing to MRSK! This document outlines some guidelines for contributing to this open source project. + +There are several ways you can contribute to the betterment of the project: + +- **Report an issue?** - If the issue isn’t reported, we can’t fix it. Please report any bugs, feature, and/or improvement requests on the [MRSK GitHub Issues tracker](https://github.com/mrsked/mrsk/issues). +- **Submit patches** - Do you have a new feature or a fix you'd like to share? [Submit a pull request](https://github.com/mrsked/mrsk/pulls)! +- **Write blog articles** - Are you using MRSK? We'd love to hear how you're using it with your projects. Write a tutorial and post it on your blog! + +## Issues +If you encounter any issues with the project, please check the [existing issues](https://github.com/mrsked/mrsk/issues) first to see if the issue has already been reported. If the issue hasn't been reported, please open a new issue with a clear description of the problem and steps to reproduce it. + +## Pull Requests +Please keep the following guidelines in mind when opening a pull request: + +- Ensure that your code passes the project's minitests by running ./bin/test. +- Provide a clear and detailed description of your changes. +- Keep your changes focused on a single concern. +- Write clean and readable code that follows the project's code style. +- Use descriptive variable and function names. +- Write clear and concise commit messages. +- Add tests for your changes, if possible. +- Ensure that your changes don't break existing functionality. + +#### Commit message guidline +A good commit message should describe what changed and why. + +## Development +The `main` branch is regularly built and tested, but it is not guaranteed to be completely stable. Tags are created regularly from release branches to indicate new official, stable release versions of MRSK. + MRSK is written in Ruby. You should have Ruby 3.2+ installed on your machine in order to work on MRSK. If that's already setup, run `bundle` in the root directory to install all dependencies. Then you can run `bin/test` to run all tests. + +1. Fork the project repository. +2. Create a new branch for your contribution. +3. Write your code or make the desired changes. +4. **Ensure that your code passes the project's minitests by running ./bin/test.** +5. Commit your changes and push them to your forked repository. +6. [Open a pull request](https://github.com/mrsked/mrsk/pulls) to the main project repository with a detailed description of your changes. + +## License +MRSK is released under the MIT License. By contributing to this project, you agree to license your contributions under the same license. From 38b8fe0d55664efb0668522e9de3e88c83b6cd49 Mon Sep 17 00:00:00 2001 From: Rasmus <2277443+kjellberg@users.noreply.github.com> Date: Fri, 3 Mar 2023 19:17:35 +0100 Subject: [PATCH 03/19] Added CODE_OF_CONDUCT.md --- CODE_OF_CONDUCT.md | 41 +++++++++++++++++++++++++++++++++++++++++ CONTRIBUTING.md | 7 +++++++ 2 files changed, 48 insertions(+) create mode 100644 CODE_OF_CONDUCT.md diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 00000000..f849cd2d --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,41 @@ +# Contributor Code of Conduct + +As contributors and maintainers of the MRSK project, we pledge to create a welcoming and inclusive environment for everyone. We value the participation of each member of our community and want all contributors to feel respected and valued. + +We are committed to providing a harassment-free experience for everyone, regardless of gender, gender identity and expression, sexual orientation, disability, physical appearance, body size, race, age, or religion (or lack thereof). We do not tolerate harassment of participants in any form. + +This code of conduct applies to all MRSK project spaces, including but not limited to project code, issue trackers, chat rooms, and mailing lists. Violations of this code of conduct may result in removal from the project community. + +## Our standards + +Examples of behavior that contributes to creating a positive environment include: + +- Using welcoming and inclusive language +- Being respectful of differing viewpoints and experiences +- Gracefully accepting constructive criticism +- Focusing on what is best for the community +- Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +- The use of sexualized language or imagery and unwelcome sexual attention or advances +- Trolling, insulting/derogatory comments, and personal or political attacks +- Public or private harassment +- Publishing others' private information, such as a physical or electronic address, without explicit permission +- Other conduct which could reasonably be considered inappropriate in a professional setting + +## Responsibilities + +Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. + +Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned with this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. + +## Reporting + +If you are subject to or witness unacceptable behavior, or have any other concerns, please notify a project maintainer. All reports will be kept confidential and will be reviewed and investigated promptly. + +We will investigate every complaint and take appropriate action. We reserve the right to remove any content that violates this Code of Conduct, or to temporarily or permanently ban any contributor for other behaviors that we deem inappropriate, threatening, offensive, or harmful. + +## Attribution + +This Code of Conduct is adapted from the Contributor Covenant, version 1.4, available at . diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7d8ad883..fea12c01 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -2,6 +2,8 @@ Thank you for considering contributing to MRSK! This document outlines some guidelines for contributing to this open source project. +Please make sure to review our [Code of Conduct](CODE_OF_CONDUCT.md) before contributing to MRSK. + There are several ways you can contribute to the betterment of the project: - **Report an issue?** - If the issue isn’t reported, we can’t fix it. Please report any bugs, feature, and/or improvement requests on the [MRSK GitHub Issues tracker](https://github.com/mrsked/mrsk/issues). @@ -9,9 +11,11 @@ There are several ways you can contribute to the betterment of the project: - **Write blog articles** - Are you using MRSK? We'd love to hear how you're using it with your projects. Write a tutorial and post it on your blog! ## Issues + If you encounter any issues with the project, please check the [existing issues](https://github.com/mrsked/mrsk/issues) first to see if the issue has already been reported. If the issue hasn't been reported, please open a new issue with a clear description of the problem and steps to reproduce it. ## Pull Requests + Please keep the following guidelines in mind when opening a pull request: - Ensure that your code passes the project's minitests by running ./bin/test. @@ -24,9 +28,11 @@ Please keep the following guidelines in mind when opening a pull request: - Ensure that your changes don't break existing functionality. #### Commit message guidline + A good commit message should describe what changed and why. ## Development + The `main` branch is regularly built and tested, but it is not guaranteed to be completely stable. Tags are created regularly from release branches to indicate new official, stable release versions of MRSK. MRSK is written in Ruby. You should have Ruby 3.2+ installed on your machine in order to work on MRSK. If that's already setup, run `bundle` in the root directory to install all dependencies. Then you can run `bin/test` to run all tests. @@ -39,4 +45,5 @@ MRSK is written in Ruby. You should have Ruby 3.2+ installed on your machine in 6. [Open a pull request](https://github.com/mrsked/mrsk/pulls) to the main project repository with a detailed description of your changes. ## License + MRSK is released under the MIT License. By contributing to this project, you agree to license your contributions under the same license. From 57b1a474fe63ecf31679b8dc1dc96e327de18aec Mon Sep 17 00:00:00 2001 From: Rasmus <2277443+kjellberg@users.noreply.github.com> Date: Fri, 3 Mar 2023 23:57:07 +0100 Subject: [PATCH 04/19] Create Dockerfile --- Dockerfile | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..121a0d7f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,32 @@ +# Use the official Ruby 3.2.0 Alpine image as the base image +FROM ruby:3.2.0-alpine + +# Install system dependencies +RUN apk add --no-cache build-base + +RUN gem install bundler --version=2.4.3 + +# Create a directory for your application +RUN mkdir -p /mrsk + +# Set the working directory to /mrsk +WORKDIR /mrsk + +# Copy the Gemfile, Gemfile.lock into the container +COPY Gemfile Gemfile.lock mrsk.gemspec ./ + +# Required in mrsk.gemspec +COPY lib/mrsk/version.rb /mrsk/lib/mrsk/version.rb + +# Install gems +RUN bundle install + +# Copy the rest of your application code into the container +COPY . . + +# Install the gem locally from the project folder +RUN gem build mrsk.gemspec && \ + gem install ./mrsk-*.gem --no-document + +# Set the entrypoint to run the installed binary +ENTRYPOINT ["mrsk"] \ No newline at end of file From 81cbd760d53fdd87f1724493c742a576c3133b70 Mon Sep 17 00:00:00 2001 From: Rasmus <2277443+kjellberg@users.noreply.github.com> Date: Sat, 4 Mar 2023 07:38:29 +0100 Subject: [PATCH 05/19] Group RUN commands - reduce image size --- Dockerfile | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/Dockerfile b/Dockerfile index 121a0d7f..a863e197 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,6 @@ # Use the official Ruby 3.2.0 Alpine image as the base image FROM ruby:3.2.0-alpine -# Install system dependencies -RUN apk add --no-cache build-base - -RUN gem install bundler --version=2.4.3 - -# Create a directory for your application -RUN mkdir -p /mrsk - # Set the working directory to /mrsk WORKDIR /mrsk @@ -18,15 +10,25 @@ COPY Gemfile Gemfile.lock mrsk.gemspec ./ # Required in mrsk.gemspec COPY lib/mrsk/version.rb /mrsk/lib/mrsk/version.rb -# Install gems -RUN bundle install +# Install system dependencies +RUN apk add --no-cache --update build-base git docker openrc \ + && rc-update add docker boot \ + && gem install bundler --version=2.4.3 \ + && mkdir -p /mrsk \ + && bundle install -# Copy the rest of your application code into the container +# Copy the rest of our application code into the container. +# We do this after bundle install, to avoid having to run bundle +# everytime we do small fixes in the source code. COPY . . # Install the gem locally from the project folder RUN gem build mrsk.gemspec && \ gem install ./mrsk-*.gem --no-document -# Set the entrypoint to run the installed binary -ENTRYPOINT ["mrsk"] \ No newline at end of file +# Set the working directory to /workdir +WORKDIR /workdir + +# Set the entrypoint to run the installed binary in /workdir +# Example: docker run -it -v "$PWD:/workdir" mrsk init +ENTRYPOINT ["mrsk"] From 8faef72d33cdcae3a5ca90aed6fbd251416b51a4 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sat, 4 Mar 2023 08:15:32 +0100 Subject: [PATCH 06/19] Already created by WORKDIR --- Dockerfile | 1 - 1 file changed, 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index a863e197..98dd3c69 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,7 +14,6 @@ COPY lib/mrsk/version.rb /mrsk/lib/mrsk/version.rb RUN apk add --no-cache --update build-base git docker openrc \ && rc-update add docker boot \ && gem install bundler --version=2.4.3 \ - && mkdir -p /mrsk \ && bundle install # Copy the rest of our application code into the container. From 8ef88859ec8f3e91554d5d3f32a8a08ad1220e30 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sat, 4 Mar 2023 08:23:24 +0100 Subject: [PATCH 07/19] Build image on push --- .github/workflows/docker-publish.yml | 37 ++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 .github/workflows/docker-publish.yml diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml new file mode 100644 index 00000000..be6f1c61 --- /dev/null +++ b/.github/workflows/docker-publish.yml @@ -0,0 +1,37 @@ +name: Docker + +on: + push: + branches: main + +jobs: + build-and-push-image: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + steps: + - + name: Checkout + uses: actions/checkout@v3 + - + name: Set up QEMU + uses: docker/setup-qemu-action@v2 + - + name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + - + name: Login to GitHub Container Registry + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + - + name: Build and push + uses: docker/build-push-action@v3 + with: + context: . + platforms: linux/amd64,linux/arm64 + push: true + tags: ghcr.io/mrsked/mrsk:latest From cf73ae67a5ef1af30bb53bfddf3fab1ec4fc650b Mon Sep 17 00:00:00 2001 From: Samuel Sieg Date: Sat, 4 Mar 2023 14:07:20 +0100 Subject: [PATCH 08/19] Fix README --- README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index aa4f826d..83b5fb7e 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,7 @@ DB_PASSWORD=secret123 ### Using a generated .env file -#### 1password as a secret store +#### 1Password as a secret store If you're using a centralized secret store, like 1Password, you can create `.env.erb` as a template which looks up the secrets. Example of a .env.erb file: @@ -97,7 +97,7 @@ This template can safely be checked into git. Then everyone deploying the app ca If you need separate env variables for different destinations, you can set them with `.env.destination.erb` for the template, which will generate `.env.staging` when run with `mrsk envify -d staging`. -#### bitwarden as a secret store +#### Bitwarden as a secret store If you are using open source secret store like bitwarden, you can create `.env.erb` as a template which looks up the secrets. @@ -367,7 +367,6 @@ builder: This build argument can then be used in the Dockerfile: ``` -# Private repositories need an access token during the build ARG RUBY_VERSION FROM ruby:$RUBY_VERSION-slim as base ``` From 11267b43c2dd86270938e934267c20cc6d94d6cb Mon Sep 17 00:00:00 2001 From: Rasmus Kjellberg <2277443+kjellberg@users.noreply.github.com> Date: Sat, 4 Mar 2023 17:05:50 +0100 Subject: [PATCH 09/19] Publish and tag docker on a new version release --- .github/workflows/docker-publish.yml | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml index be6f1c61..40d0e9fe 100644 --- a/.github/workflows/docker-publish.yml +++ b/.github/workflows/docker-publish.yml @@ -1,8 +1,10 @@ name: Docker on: - push: - branches: main + release: + types: [created] + tags: + - 'v*' jobs: build-and-push-image: @@ -14,6 +16,10 @@ jobs: - name: Checkout uses: actions/checkout@v3 + - + name: Extract Version Number + id: extract_version + run: echo "::set-output name=version::${{ github.ref | replace('refs/tags/v', '') }}" - name: Set up QEMU uses: docker/setup-qemu-action@v2 @@ -34,4 +40,6 @@ jobs: context: . platforms: linux/amd64,linux/arm64 push: true - tags: ghcr.io/mrsked/mrsk:latest + tags: | + ghcr.io/mrsked/mrsk:latest + ghcr.io/mrsked/mrsk:${{ steps.extract_version.outputs.version }} From 059979b88948b1a66c8630de54eb3c67dde6ecad Mon Sep 17 00:00:00 2001 From: Rasmus Kjellberg <2277443+kjellberg@users.noreply.github.com> Date: Sat, 4 Mar 2023 17:14:02 +0100 Subject: [PATCH 10/19] Update docker-publish.yml --- .github/workflows/docker-publish.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml index 40d0e9fe..da7e99a5 100644 --- a/.github/workflows/docker-publish.yml +++ b/.github/workflows/docker-publish.yml @@ -19,7 +19,7 @@ jobs: - name: Extract Version Number id: extract_version - run: echo "::set-output name=version::${{ github.ref | replace('refs/tags/v', '') }}" + run: echo "::set-output name=version::${{ github.ref | replace('refs/tags/', '') }}" - name: Set up QEMU uses: docker/setup-qemu-action@v2 From 3b6571ae5535630231cdbd8789b3dccda7fee3de Mon Sep 17 00:00:00 2001 From: Axel Gustav Date: Sat, 4 Mar 2023 16:07:58 -0400 Subject: [PATCH 11/19] Make sure ed25519 and bcrypt_pbkdf are in gemspec dependencies --- Gemfile | 2 -- Gemfile.lock | 4 ++-- mrsk.gemspec | 2 ++ 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Gemfile b/Gemfile index 55477dfa..9c31a8f8 100644 --- a/Gemfile +++ b/Gemfile @@ -6,5 +6,3 @@ gemspec gem "debug" gem "mocha" gem "railties" -gem "ed25519" -gem "bcrypt_pbkdf" diff --git a/Gemfile.lock b/Gemfile.lock index 607c52f4..5640134a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -3,7 +3,9 @@ PATH specs: mrsk (0.8.4) activesupport (>= 7.0) + bcrypt_pbkdf (~> 1.0) dotenv (~> 2.8) + ed25519 (~> 1.2) sshkit (~> 1.21) thor (~> 1.2) zeitwerk (~> 2.5) @@ -98,9 +100,7 @@ PLATFORMS x86_64-linux DEPENDENCIES - bcrypt_pbkdf debug - ed25519 mocha mrsk! railties diff --git a/mrsk.gemspec b/mrsk.gemspec index 6645fd2c..6cd77252 100644 --- a/mrsk.gemspec +++ b/mrsk.gemspec @@ -17,4 +17,6 @@ Gem::Specification.new do |spec| spec.add_dependency "thor", "~> 1.2" spec.add_dependency "dotenv", "~> 2.8" spec.add_dependency "zeitwerk", "~> 2.5" + spec.add_dependency "ed25519", "~> 1.2" + spec.add_dependency "bcrypt_pbkdf", "~> 1.0" end From 963b96ff622df6aa0fb32991bd4288dc7955ee33 Mon Sep 17 00:00:00 2001 From: Chris Lowder Date: Sat, 4 Mar 2023 21:41:08 +0000 Subject: [PATCH 12/19] Customizable Traefik host port Allow users to free up port 80 on the host machine, without losing Traefik's Docker routing super-powers. --- README.md | 9 +++++++++ lib/mrsk/commands/traefik.rb | 6 +++++- test/commands/traefik_test.rb | 5 +++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 83b5fb7e..58db9525 100644 --- a/README.md +++ b/README.md @@ -354,6 +354,15 @@ traefik: This will start the traefik container with `--accesslog=true accesslog.format=json`. +### Traefik's host port binding + +By default Traefik binds to port 80 of the host machine, it can be configured to use an alternative port: + +```yaml +traefik: + host_port: 8080 +``` + ### Configuring build args for new images Build arguments that aren't secret can also be configured: diff --git a/lib/mrsk/commands/traefik.rb b/lib/mrsk/commands/traefik.rb index 67f7c09b..b8ae8c28 100644 --- a/lib/mrsk/commands/traefik.rb +++ b/lib/mrsk/commands/traefik.rb @@ -4,7 +4,7 @@ class Mrsk::Commands::Traefik < Mrsk::Commands::Base "--detach", "--restart", "unless-stopped", "--log-opt", "max-size=#{MAX_LOG_SIZE}", - "--publish", "80:80", + "--publish", port, "--volume", "/var/run/docker.sock:/var/run/docker.sock", "traefik", "--providers.docker", @@ -45,6 +45,10 @@ class Mrsk::Commands::Traefik < Mrsk::Commands::Base docker :image, :prune, "--all", "--force", "--filter", "label=org.opencontainers.image.title=Traefik" end + def port + "#{config.raw_config.traefik.fetch("host_port", "80")}:80" + end + private def cmd_args (config.raw_config.dig(:traefik, "args") || { }).collect { |(key, value)| [ "--#{key}", value ] }.flatten diff --git a/test/commands/traefik_test.rb b/test/commands/traefik_test.rb index 6d162138..ae0d471e 100644 --- a/test/commands/traefik_test.rb +++ b/test/commands/traefik_test.rb @@ -12,6 +12,11 @@ class CommandsTraefikTest < ActiveSupport::TestCase assert_equal \ "docker run --name traefik --detach --restart unless-stopped --log-opt max-size=10m --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock traefik --providers.docker --log.level=DEBUG --accesslog.format json --metrics.prometheus.buckets 0.1,0.3,1.2,5.0", new_command.run.join(" ") + + @config[:traefik]["host_port"] = "8080" + assert_equal \ + "docker run --name traefik --detach --restart unless-stopped --log-opt max-size=10m --publish 8080:80 --volume /var/run/docker.sock:/var/run/docker.sock traefik --providers.docker --log.level=DEBUG --accesslog.format json --metrics.prometheus.buckets 0.1,0.3,1.2,5.0", + new_command.run.join(" ") end test "traefik start" do From 9ae3886b2be0fdfa8de7fce8826c0086bd5d40f0 Mon Sep 17 00:00:00 2001 From: Simon Rand Date: Sun, 5 Mar 2023 10:56:51 +0000 Subject: [PATCH 13/19] Ensure curl is installed during bootstrapping --- lib/mrsk/cli/main.rb | 4 ++-- lib/mrsk/cli/server.rb | 13 +++++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/mrsk/cli/main.rb b/lib/mrsk/cli/main.rb index db80d9f6..73268f50 100644 --- a/lib/mrsk/cli/main.rb +++ b/lib/mrsk/cli/main.rb @@ -11,7 +11,7 @@ class Mrsk::Cli::Main < Mrsk::Cli::Base desc "deploy", "Deploy app to servers" def deploy runtime = print_runtime do - say "Ensure Docker is installed...", :magenta + say "Ensure curl and Docker are installed...", :magenta invoke "mrsk:cli:server:bootstrap" say "Log into image registry...", :magenta @@ -173,7 +173,7 @@ class Mrsk::Cli::Main < Mrsk::Cli::Base desc "registry", "Login and -out of the image registry" subcommand "registry", Mrsk::Cli::Registry - desc "server", "Bootstrap servers with Docker" + desc "server", "Bootstrap servers with curl and Docker" subcommand "server", Mrsk::Cli::Server desc "traefik", "Manage Traefik load balancer" diff --git a/lib/mrsk/cli/server.rb b/lib/mrsk/cli/server.rb index 9030ffd6..d6ae074a 100644 --- a/lib/mrsk/cli/server.rb +++ b/lib/mrsk/cli/server.rb @@ -1,6 +1,15 @@ class Mrsk::Cli::Server < Mrsk::Cli::Base - desc "bootstrap", "Ensure Docker is installed on servers" + desc "bootstrap", "Ensure curl and Docker are installed on servers" def bootstrap - on(MRSK.hosts + MRSK.accessory_hosts) { execute "which docker || (apt-get update -y && apt-get install docker.io -y)" } + on(MRSK.hosts + MRSK.accessory_hosts) do + dependencies_to_install = [].tap do |dependencies| + dependencies << "curl" unless execute "which curl", raise_on_non_zero_exit: false + dependencies << "docker.io" unless execute "which docker", raise_on_non_zero_exit: false + end + + if dependencies_to_install.any? + execute "apt-get update -y && apt-get install #{dependencies_to_install.join(' ')} -y)" + end + end end end From f531874be458aac3449e389f81f892efb082ff68 Mon Sep 17 00:00:00 2001 From: Rasmus <2277443+kjellberg@users.noreply.github.com> Date: Tue, 7 Mar 2023 09:49:08 +0100 Subject: [PATCH 14/19] Allow registry username to be a reference to secret --- README.md | 8 ++++++-- lib/mrsk/commands/registry.rb | 16 +++++++++------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 83b5fb7e..6dc106fd 100644 --- a/README.md +++ b/README.md @@ -150,10 +150,14 @@ The default registry is Docker Hub, but you can change it using `registry/server ```yaml registry: server: registry.digitalocean.com - username: registry-user-name - password: <%= ENV.fetch("MRSK_REGISTRY_PASSWORD") %> + username: + - DOCKER_REGISTRY_TOKEN + password: + - DOCKER_REGISTRY_TOKEN ``` +A reference to secret `DOCKER_REGISTRY_TOKEN` will look for `ENV["DOCKER_REGISTRY_TOKEN"]` on the machine running MRSK. + ### Using a different SSH user than root The default SSH user is root, but you can change it using `ssh/user`: diff --git a/lib/mrsk/commands/registry.rb b/lib/mrsk/commands/registry.rb index 03304864..62ba58ce 100644 --- a/lib/mrsk/commands/registry.rb +++ b/lib/mrsk/commands/registry.rb @@ -2,7 +2,7 @@ class Mrsk::Commands::Registry < Mrsk::Commands::Base delegate :registry, to: :config def login - docker :login, registry["server"], "-u", redact(registry["username"]), "-p", redact(lookup_password) + docker :login, registry["server"], "-u", redact_credentials("username"), "-p", redact_credentials("password") end def logout @@ -10,11 +10,13 @@ class Mrsk::Commands::Registry < Mrsk::Commands::Base end private - def lookup_password - if registry["password"].is_a?(Array) - ENV.fetch(registry["password"].first).dup - else - registry["password"] - end + def redact_credentials(key) + value = if registry[key].is_a?(Array) + ENV.fetch(registry[key].first).dup + else + registry[key] + end + + redact(value) end end From b809a971e29017006e7410d4343c75e8acd2b160 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 8 Mar 2023 17:43:23 +0100 Subject: [PATCH 15/19] One purpose per method --- lib/mrsk/commands/registry.rb | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/lib/mrsk/commands/registry.rb b/lib/mrsk/commands/registry.rb index 62ba58ce..1276da09 100644 --- a/lib/mrsk/commands/registry.rb +++ b/lib/mrsk/commands/registry.rb @@ -2,7 +2,7 @@ class Mrsk::Commands::Registry < Mrsk::Commands::Base delegate :registry, to: :config def login - docker :login, registry["server"], "-u", redact_credentials("username"), "-p", redact_credentials("password") + docker :login, registry["server"], "-u", redact(lookup("username")), "-p", redact(lookup("password")) end def logout @@ -10,13 +10,11 @@ class Mrsk::Commands::Registry < Mrsk::Commands::Base end private - def redact_credentials(key) - value = if registry[key].is_a?(Array) - ENV.fetch(registry[key].first).dup - else - registry[key] - end - - redact(value) + def lookup(key) + if registry[key].is_a?(Array) + ENV.fetch(registry[key].first).dup + else + registry[key] + end end end From a4d576f105a3165394a24a59ee5f569cc5ec09f0 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 8 Mar 2023 17:43:29 +0100 Subject: [PATCH 16/19] Test ENV username --- test/commands/registry_test.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/commands/registry_test.rb b/test/commands/registry_test.rb index 01be2b34..0d62de8e 100755 --- a/test/commands/registry_test.rb +++ b/test/commands/registry_test.rb @@ -30,6 +30,17 @@ class CommandsRegistryTest < ActiveSupport::TestCase ENV.delete("MRSK_REGISTRY_PASSWORD") end + test "registry login with ENV username" do + ENV["MRSK_REGISTRY_USERNAME"] = "also-secret" + @config[:registry]["username"] = [ "MRSK_REGISTRY_USERNAME" ] + + assert_equal \ + "docker login hub.docker.com -u also-secret -p secret", + @registry.login.join(" ") + ensure + ENV.delete("MRSK_REGISTRY_USERNAME") + end + test "registry logout" do assert_equal \ "docker logout hub.docker.com", From 155384472a63c7428a426ca08f08ec0f11283f94 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 8 Mar 2023 18:00:13 +0100 Subject: [PATCH 17/19] Allow primary host even when a specific role has been set --- lib/mrsk/commander.rb | 2 +- test/commander_test.rb | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/mrsk/commander.rb b/lib/mrsk/commander.rb index 1b3106e2..be2fd2e3 100644 --- a/lib/mrsk/commander.rb +++ b/lib/mrsk/commander.rb @@ -25,7 +25,7 @@ class Mrsk::Commander end def primary_host - specific_hosts&.sole || config.primary_web_host + specific_hosts&.first || config.primary_web_host end def hosts diff --git a/test/commander_test.rb b/test/commander_test.rb index 31bee04a..b4ce24dc 100644 --- a/test/commander_test.rb +++ b/test/commander_test.rb @@ -42,4 +42,9 @@ class CommanderTest < ActiveSupport::TestCase @mrsk.specific_primary! assert_equal [ "1.1.1.1" ], @mrsk.hosts end + + test "primary_host with specific hosts via role" do + @mrsk.specific_roles = "web" + assert_equal "1.1.1.1", @mrsk.primary_host + end end From 3457c3f606310e90c60fbf3007f0e570e55ffc49 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 8 Mar 2023 18:05:00 +0100 Subject: [PATCH 18/19] Style --- lib/mrsk/cli/server.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/mrsk/cli/server.rb b/lib/mrsk/cli/server.rb index d6ae074a..39861ec2 100644 --- a/lib/mrsk/cli/server.rb +++ b/lib/mrsk/cli/server.rb @@ -2,13 +2,13 @@ class Mrsk::Cli::Server < Mrsk::Cli::Base desc "bootstrap", "Ensure curl and Docker are installed on servers" def bootstrap on(MRSK.hosts + MRSK.accessory_hosts) do - dependencies_to_install = [].tap do |dependencies| + dependencies_to_install = Array.new.tap do |dependencies| dependencies << "curl" unless execute "which curl", raise_on_non_zero_exit: false dependencies << "docker.io" unless execute "which docker", raise_on_non_zero_exit: false end if dependencies_to_install.any? - execute "apt-get update -y && apt-get install #{dependencies_to_install.join(' ')} -y)" + execute "apt-get update -y && apt-get install #{dependencies_to_install.join(" ")} -y)" end end end From 347046019f55f6b1a3e9d69293c1e847ed23e5e0 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 8 Mar 2023 18:05:06 +0100 Subject: [PATCH 19/19] Add test --- test/cli/server_test.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 test/cli/server_test.rb diff --git a/test/cli/server_test.rb b/test/cli/server_test.rb new file mode 100644 index 00000000..bf0119f4 --- /dev/null +++ b/test/cli/server_test.rb @@ -0,0 +1,16 @@ +require_relative "cli_test_case" + +class CliServerTest < CliTestCase + test "bootstrap" do + run_command("bootstrap").tap do |output| + assert_match /which curl/, output + assert_match /which docker/, output + assert_match /apt-get update -y && apt-get install curl docker.io -y/, output + end + end + + private + def run_command(*command) + stdouted { Mrsk::Cli::Server.start([*command, "-c", "test/fixtures/deploy_with_accessories.yml"]) } + end +end