Compare commits
306 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
625be70e4d | ||
|
|
aafaee7ac8 | ||
|
|
97a190300d | ||
|
|
326711a3e0 | ||
|
|
82be521e66 | ||
|
|
21110080d5 | ||
|
|
ef107c41b6 | ||
|
|
1bf4b6b76f | ||
|
|
36a3b13bf4 | ||
|
|
01483140f5 | ||
|
|
0e19ead37c | ||
|
|
048aecf352 | ||
|
|
38c85e8021 | ||
|
|
88a7413b3e | ||
|
|
9cc73fed9a | ||
|
|
787ef96639 | ||
|
|
1e8edc25e2 | ||
|
|
b7877c59b4 | ||
|
|
35b5b317af | ||
|
|
4c448f7eb1 | ||
|
|
263a24afe3 | ||
|
|
a2d99e48bf | ||
|
|
a22e27dbf8 | ||
|
|
bb74a74dc4 | ||
|
|
c611a1616a | ||
|
|
98e7b995d5 | ||
|
|
ae2effb80c | ||
|
|
f719540e0c | ||
|
|
cbda851436 | ||
|
|
8854bb63a1 | ||
|
|
35ea9f3c81 | ||
|
|
18312f5191 | ||
|
|
71bc9bcf54 | ||
|
|
c83b74dcb7 | ||
|
|
971a91da15 | ||
|
|
86d6f8d674 | ||
|
|
7fe24d5048 | ||
|
|
a72f95f44d | ||
|
|
dc3be30b16 | ||
|
|
54881a0298 | ||
|
|
19527b4f65 | ||
|
|
bfb70b2118 | ||
|
|
e85bd5ff63 | ||
|
|
d0f66db33c | ||
|
|
650f9b1fbf | ||
|
|
1170e2311e | ||
|
|
94f87edded | ||
|
|
548a1019c1 | ||
|
|
ca2e2bac2e | ||
|
|
494a1ae089 | ||
|
|
a77428143f | ||
|
|
4fa6a6c06d | ||
|
|
2ad0dc0703 | ||
|
|
df067e4893 | ||
|
|
cd668066ff | ||
|
|
1a7d123746 | ||
|
|
52ca5b846a | ||
|
|
126e0bbd06 | ||
|
|
9ec3895dab | ||
|
|
a6245a6bc9 | ||
|
|
0d80709e2d | ||
|
|
aceabb3824 | ||
|
|
99fe31d4b4 | ||
|
|
bcf8a927f5 | ||
|
|
f055766918 | ||
|
|
a8726be20e | ||
|
|
100b72e4b4 | ||
|
|
828e56912e | ||
|
|
df202d6ef4 | ||
|
|
f530009a6e | ||
|
|
4b36df5dab | ||
|
|
79d46ceb16 | ||
|
|
bc8875e020 | ||
|
|
d4a72da9d8 | ||
|
|
04a04c05e0 | ||
|
|
cff8b058af | ||
|
|
b6f7d94ac3 | ||
|
|
3ab16c8994 | ||
|
|
b6743e5e1c | ||
|
|
9ddb181f50 | ||
|
|
fbe1458478 | ||
|
|
2f1393cd92 | ||
|
|
76673c0c1b | ||
|
|
fb62f2e6e1 | ||
|
|
051556674f | ||
|
|
3cbf4aea46 | ||
|
|
5ed431b807 | ||
|
|
60a19f0b30 | ||
|
|
2d0a7e1b67 | ||
|
|
49df19fb0d | ||
|
|
cef8fddfb4 | ||
|
|
c59eb00dd0 | ||
|
|
43f7409de0 | ||
|
|
448ea7719f | ||
|
|
72b70e3e9e | ||
|
|
e8697327fa | ||
|
|
0bfd4ca780 | ||
|
|
12e3a562c4 | ||
|
|
ab54dbdb8b | ||
|
|
ac3771447a | ||
|
|
daa0c9b5be | ||
|
|
c3393c8213 | ||
|
|
03d933d10b | ||
|
|
579b4cd9aa | ||
|
|
f9436d5673 | ||
|
|
8ae5331d97 | ||
|
|
4d47fbdf41 | ||
|
|
e980f1164e | ||
|
|
e2f6db5cae | ||
|
|
d3936363d0 | ||
|
|
cfc8fa0590 | ||
|
|
161ebe4bc1 | ||
|
|
514b2aa243 | ||
|
|
18031bc552 | ||
|
|
d8c61004e4 | ||
|
|
c4df440c79 | ||
|
|
fb1718ca6d | ||
|
|
7d17a6c3b5 | ||
|
|
f4133de896 | ||
|
|
a9488e935d | ||
|
|
ac61528dfc | ||
|
|
0eb7a8d087 | ||
|
|
7559f439e9 | ||
|
|
54a5b90d8f | ||
|
|
a245adfad2 | ||
|
|
f386c3bdab | ||
|
|
2a3e576182 | ||
|
|
f3e3196ce5 | ||
|
|
fca5b11682 | ||
|
|
d09cddde8d | ||
|
|
3969f56fa6 | ||
|
|
c60cc92dfe | ||
|
|
cb3c5a53f4 | ||
|
|
ef04410d77 | ||
|
|
bd8f13dd5e | ||
|
|
2146f6d0ec | ||
|
|
52d8c112d3 | ||
|
|
c9afd66222 | ||
|
|
36c458407f | ||
|
|
c137b38c87 | ||
|
|
f851d6528d | ||
|
|
12632aa7f9 | ||
|
|
2f97bc488f | ||
|
|
032266a76a | ||
|
|
33cc6c8bae | ||
|
|
5638ab8594 | ||
|
|
60916cdac3 | ||
|
|
1f83b5f6be | ||
|
|
070c6e8e75 | ||
|
|
2957388bf6 | ||
|
|
7f178101f7 | ||
|
|
aed345466f | ||
|
|
c06585fef4 | ||
|
|
fd5313ec3e | ||
|
|
4184d3204e | ||
|
|
15a41d3fd8 | ||
|
|
03614bfb79 | ||
|
|
078d68b170 | ||
|
|
cec82ac641 | ||
|
|
05488e4c1e | ||
|
|
01a2b678d7 | ||
|
|
84540cee7b | ||
|
|
5bbb4aeb58 | ||
|
|
6a27a46e5f | ||
|
|
b5ccc1fa5d | ||
|
|
e2e5e18af9 | ||
|
|
4fa71834ad | ||
|
|
65663ae2ea | ||
|
|
4044abdde1 | ||
|
|
bc64a07a95 | ||
|
|
fdb2502216 | ||
|
|
a9bb8d7376 | ||
|
|
53095a053e | ||
|
|
4ab5199853 | ||
|
|
348f5844d5 | ||
|
|
9b43a6b23b | ||
|
|
1f196045a9 | ||
|
|
86e99fb079 | ||
|
|
494e29d672 | ||
|
|
93423f2f20 | ||
|
|
8d8f9f6ada | ||
|
|
17e74910e4 | ||
|
|
8ebcafd3d8 | ||
|
|
89b4b909db | ||
|
|
c89b77127b | ||
|
|
9c27ead21f | ||
|
|
c3de89bb59 | ||
|
|
20a6bc31cd | ||
|
|
ba5bdf95ec | ||
|
|
3392fc6c1b | ||
|
|
7369be48ff | ||
|
|
4670db7f6d | ||
|
|
e859a581ab | ||
|
|
5d5d58a4ec | ||
|
|
cf38feb1d6 | ||
|
|
e2d10ec5a9 | ||
|
|
035e4afff7 | ||
|
|
1887a6518e | ||
|
|
1ed4a37da2 | ||
|
|
7e1596e722 | ||
|
|
e7e3cd98eb | ||
|
|
a1fc00347b | ||
|
|
f73c526890 | ||
|
|
65b90dd5c8 | ||
|
|
9648721ce7 | ||
|
|
e409281bb2 | ||
|
|
bab8e42965 | ||
|
|
110df5244b | ||
|
|
01d684746e | ||
|
|
951a71f38e | ||
|
|
8b755c6973 | ||
|
|
9a909ba7eb | ||
|
|
14512fe409 | ||
|
|
e97216b0ea | ||
|
|
f3d93d3899 | ||
|
|
53d7f9d528 | ||
|
|
c870e560c1 | ||
|
|
04b1d5e49e | ||
|
|
714960f184 | ||
|
|
c0d5b48f22 | ||
|
|
fb3353084f | ||
|
|
19104cafb4 | ||
|
|
1bdfc217c4 | ||
|
|
83dc82661b | ||
|
|
790be0f5f3 | ||
|
|
49d60a045a | ||
|
|
60faf27a05 | ||
|
|
43d1ecc94b | ||
|
|
00b970323b | ||
|
|
d0c4030257 | ||
|
|
9591096131 | ||
|
|
b635b3198f | ||
|
|
662873de49 | ||
|
|
b5372988f7 | ||
|
|
c3d0382935 | ||
|
|
2de5250486 | ||
|
|
491777221f | ||
|
|
d167e48584 | ||
|
|
d071246865 | ||
|
|
dae8b14469 | ||
|
|
b166f3fbf4 | ||
|
|
d33b723afb | ||
|
|
aae290cefc | ||
|
|
4c542930c5 | ||
|
|
a15603655c | ||
|
|
11af999800 | ||
|
|
cb824bdc42 | ||
|
|
85a0267447 | ||
|
|
886914c82e | ||
|
|
5b506a2daa | ||
|
|
9843c5e1ce | ||
|
|
c2ca269eb6 | ||
|
|
53046efad4 | ||
|
|
2db1bfde00 | ||
|
|
2cea12c56b | ||
|
|
43a1b42f8c | ||
|
|
c282461265 | ||
|
|
dcbe038555 | ||
|
|
3fd2f3f2c5 | ||
|
|
46dad1ee6c | ||
|
|
3ca5bc50b6 | ||
|
|
b668ce3f25 | ||
|
|
253d4ac37b | ||
|
|
50ee954ca9 | ||
|
|
0ac2cd2a4b | ||
|
|
72e0184e9f | ||
|
|
577cf2cec9 | ||
|
|
5010850b86 | ||
|
|
fa07c2403c | ||
|
|
c29d1ddeba | ||
|
|
cb15800d25 | ||
|
|
3e0b71b631 | ||
|
|
9b666e54f3 | ||
|
|
d2f76dac6b | ||
|
|
bf3d3f3ba7 | ||
|
|
20733a4493 | ||
|
|
a267c1e835 | ||
|
|
c1c26a154d | ||
|
|
5969ff66d5 | ||
|
|
b1f5165dc0 | ||
|
|
cce0fafdc4 | ||
|
|
6232175ef8 | ||
|
|
47af6d9483 | ||
|
|
ff0170076e | ||
|
|
9b39f2f3ab | ||
|
|
600902ef5e | ||
|
|
bb241dea43 | ||
|
|
f26beeaa9f | ||
|
|
41a5cb2a04 | ||
|
|
643cb2c520 | ||
|
|
b2c819fe32 | ||
|
|
439b681308 | ||
|
|
e5c5e89232 | ||
|
|
4bf77ccd1b | ||
|
|
57e9231c5e | ||
|
|
ccf8762c98 | ||
|
|
418bc13ae7 | ||
|
|
7d4dfc4c86 | ||
|
|
fdb0c8ee91 | ||
|
|
6b11303230 | ||
|
|
901484d75d | ||
|
|
e178907a21 | ||
|
|
3026a92c98 | ||
|
|
ab7c6c6540 | ||
|
|
8b913068de | ||
|
|
170562c7e7 |
13
.github/workflows/ci.yml
vendored
13
.github/workflows/ci.yml
vendored
@@ -1,5 +1,9 @@
|
||||
name: CI
|
||||
on: [push, pull_request]
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
pull_request:
|
||||
jobs:
|
||||
tests:
|
||||
strategy:
|
||||
@@ -8,12 +12,15 @@ jobs:
|
||||
- "2.7"
|
||||
- "3.1"
|
||||
- "3.2"
|
||||
gemfile:
|
||||
- Gemfile
|
||||
- gemfiles/rails_edge.gemfile
|
||||
continue-on-error: [false]
|
||||
|
||||
name: ${{ format('Tests (Ruby {0})', matrix.ruby-version) }}
|
||||
runs-on: ubuntu-latest
|
||||
continue-on-error: ${{ matrix.continue-on-error }}
|
||||
|
||||
env:
|
||||
BUNDLE_GEMFILE: ${{ github.workspace }}/${{ matrix.gemfile }}
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
|
||||
6
.github/workflows/docker-publish.yml
vendored
6
.github/workflows/docker-publish.yml
vendored
@@ -16,10 +16,6 @@ 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/', '') }}"
|
||||
-
|
||||
name: Set up QEMU
|
||||
uses: docker/setup-qemu-action@v2
|
||||
@@ -42,4 +38,4 @@ jobs:
|
||||
push: true
|
||||
tags: |
|
||||
ghcr.io/mrsked/mrsk:latest
|
||||
ghcr.io/mrsked/mrsk:${{ steps.extract_version.outputs.version }}
|
||||
ghcr.io/mrsked/mrsk:${{ github.ref_name }}
|
||||
|
||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,4 +1,5 @@
|
||||
.byebug_history
|
||||
*.gem
|
||||
coverage/*
|
||||
.DS_Store
|
||||
.DS_Store
|
||||
gemfiles/*.lock
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
# Use the official Ruby 3.2.0 Alpine image as the base image
|
||||
FROM ruby:3.2.0-alpine
|
||||
|
||||
# Install docker/buildx-bin
|
||||
COPY --from=docker/buildx-bin /buildx /usr/libexec/docker/cli-plugins/docker-buildx
|
||||
|
||||
# Set the working directory to /mrsk
|
||||
WORKDIR /mrsk
|
||||
|
||||
@@ -11,7 +14,7 @@ COPY Gemfile Gemfile.lock mrsk.gemspec ./
|
||||
COPY lib/mrsk/version.rb /mrsk/lib/mrsk/version.rb
|
||||
|
||||
# Install system dependencies
|
||||
RUN apk add --no-cache --update build-base git docker openrc \
|
||||
RUN apk add --no-cache --update build-base git docker openrc openssh-client-default \
|
||||
&& rc-update add docker boot \
|
||||
&& gem install bundler --version=2.4.3 \
|
||||
&& bundle install
|
||||
@@ -28,6 +31,10 @@ RUN gem build mrsk.gemspec && \
|
||||
# Set the working directory to /workdir
|
||||
WORKDIR /workdir
|
||||
|
||||
# Tell git it's safe to access /workdir/.git even if
|
||||
# the directory is owned by a different user
|
||||
RUN git config --global --add safe.directory /workdir
|
||||
|
||||
# Set the entrypoint to run the installed binary in /workdir
|
||||
# Example: docker run -it -v "$PWD:/workdir" mrsk init
|
||||
ENTRYPOINT ["mrsk"]
|
||||
|
||||
4
Gemfile
4
Gemfile
@@ -2,7 +2,3 @@ source 'https://rubygems.org'
|
||||
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
|
||||
|
||||
gemspec
|
||||
|
||||
gem "debug"
|
||||
gem "mocha"
|
||||
gem "railties"
|
||||
|
||||
61
Gemfile.lock
61
Gemfile.lock
@@ -1,11 +1,12 @@
|
||||
PATH
|
||||
remote: .
|
||||
specs:
|
||||
mrsk (0.9.1)
|
||||
mrsk (0.12.1)
|
||||
activesupport (>= 7.0)
|
||||
bcrypt_pbkdf (~> 1.0)
|
||||
dotenv (~> 2.8)
|
||||
ed25519 (~> 1.2)
|
||||
net-ssh (~> 7.0)
|
||||
sshkit (~> 1.21)
|
||||
thor (~> 1.2)
|
||||
zeitwerk (~> 2.5)
|
||||
@@ -13,29 +14,29 @@ PATH
|
||||
GEM
|
||||
remote: https://rubygems.org/
|
||||
specs:
|
||||
actionpack (7.0.4)
|
||||
actionview (= 7.0.4)
|
||||
activesupport (= 7.0.4)
|
||||
actionpack (7.0.4.3)
|
||||
actionview (= 7.0.4.3)
|
||||
activesupport (= 7.0.4.3)
|
||||
rack (~> 2.0, >= 2.2.0)
|
||||
rack-test (>= 0.6.3)
|
||||
rails-dom-testing (~> 2.0)
|
||||
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
||||
actionview (7.0.4)
|
||||
activesupport (= 7.0.4)
|
||||
actionview (7.0.4.3)
|
||||
activesupport (= 7.0.4.3)
|
||||
builder (~> 3.1)
|
||||
erubi (~> 1.4)
|
||||
rails-dom-testing (~> 2.0)
|
||||
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
||||
activesupport (7.0.4)
|
||||
activesupport (7.0.4.3)
|
||||
concurrent-ruby (~> 1.0, >= 1.0.2)
|
||||
i18n (>= 1.6, < 2)
|
||||
minitest (>= 5.1)
|
||||
tzinfo (~> 2.0)
|
||||
bcrypt_pbkdf (1.1.0)
|
||||
builder (3.2.4)
|
||||
concurrent-ruby (1.1.10)
|
||||
concurrent-ruby (1.2.2)
|
||||
crass (1.0.6)
|
||||
debug (1.7.1)
|
||||
debug (1.7.2)
|
||||
irb (>= 1.5.0)
|
||||
reline (>= 0.3.1)
|
||||
dotenv (2.8.1)
|
||||
@@ -44,59 +45,55 @@ GEM
|
||||
i18n (1.12.0)
|
||||
concurrent-ruby (~> 1.0)
|
||||
io-console (0.6.0)
|
||||
irb (1.6.2)
|
||||
irb (1.6.3)
|
||||
reline (>= 0.3.0)
|
||||
loofah (2.19.1)
|
||||
loofah (2.20.0)
|
||||
crass (~> 1.0.2)
|
||||
nokogiri (>= 1.5.9)
|
||||
method_source (1.0.0)
|
||||
minitest (5.17.0)
|
||||
minitest (5.18.0)
|
||||
mocha (2.0.2)
|
||||
ruby2_keywords (>= 0.0.5)
|
||||
net-scp (4.0.0)
|
||||
net-ssh (>= 2.6.5, < 8.0.0)
|
||||
net-ssh (7.0.1)
|
||||
nokogiri (1.14.0-arm64-darwin)
|
||||
net-ssh (7.1.0)
|
||||
nokogiri (1.14.2-arm64-darwin)
|
||||
racc (~> 1.4)
|
||||
nokogiri (1.14.0-x86_64-darwin)
|
||||
nokogiri (1.14.2-x86_64-darwin)
|
||||
racc (~> 1.4)
|
||||
nokogiri (1.14.0-x86_64-linux)
|
||||
nokogiri (1.14.2-x86_64-linux)
|
||||
racc (~> 1.4)
|
||||
racc (1.6.2)
|
||||
rack (2.2.5)
|
||||
rack-test (2.0.2)
|
||||
rack (2.2.6.4)
|
||||
rack-test (2.1.0)
|
||||
rack (>= 1.3)
|
||||
rails-dom-testing (2.0.3)
|
||||
activesupport (>= 4.2.0)
|
||||
nokogiri (>= 1.6)
|
||||
rails-html-sanitizer (1.4.4)
|
||||
rails-html-sanitizer (1.5.0)
|
||||
loofah (~> 2.19, >= 2.19.1)
|
||||
railties (7.0.4)
|
||||
actionpack (= 7.0.4)
|
||||
activesupport (= 7.0.4)
|
||||
railties (7.0.4.3)
|
||||
actionpack (= 7.0.4.3)
|
||||
activesupport (= 7.0.4.3)
|
||||
method_source
|
||||
rake (>= 12.2)
|
||||
thor (~> 1.0)
|
||||
zeitwerk (~> 2.5)
|
||||
rake (13.0.6)
|
||||
reline (0.3.2)
|
||||
reline (0.3.3)
|
||||
io-console (~> 0.5)
|
||||
ruby2_keywords (0.0.5)
|
||||
sshkit (1.21.3)
|
||||
sshkit (1.21.4)
|
||||
net-scp (>= 1.1.2)
|
||||
net-ssh (>= 2.8.0)
|
||||
thor (1.2.1)
|
||||
tzinfo (2.0.5)
|
||||
tzinfo (2.0.6)
|
||||
concurrent-ruby (~> 1.0)
|
||||
zeitwerk (2.6.6)
|
||||
zeitwerk (2.6.7)
|
||||
|
||||
PLATFORMS
|
||||
arm64-darwin-20
|
||||
arm64-darwin-21
|
||||
arm64-darwin-22
|
||||
x86_64-darwin-20
|
||||
x86_64-darwin-21
|
||||
x86_64-darwin-22
|
||||
arm64-darwin
|
||||
x86_64-darwin
|
||||
x86_64-linux
|
||||
|
||||
DEPENDENCIES
|
||||
|
||||
312
README.md
312
README.md
@@ -4,9 +4,25 @@ MRSK deploys web apps anywhere from bare metal to cloud VMs using Docker with ze
|
||||
|
||||
Watch the screencast: https://www.youtube.com/watch?v=LL1cV2FXZ5I
|
||||
|
||||
Join us on Discord: https://discord.gg/YgHVT7GCXS
|
||||
|
||||
Ask questions: https://github.com/mrsked/mrsk/discussions
|
||||
|
||||
## Installation
|
||||
|
||||
Install MRSK globally with `gem install mrsk`. Then, inside your app directory, run `mrsk init` (or `mrsk init --bundle` within Rails apps where you want a bin/mrsk binstub). Now edit the new file `config/deploy.yml`. It could look as simple as this:
|
||||
If you have a Ruby environment available, you can install MRSK globally with:
|
||||
|
||||
```sh
|
||||
gem install mrsk
|
||||
```
|
||||
|
||||
...otherwise, you can run a dockerized version via an alias (add this to your .bashrc or similar to simplify re-use):
|
||||
|
||||
```sh
|
||||
alias mrsk='docker run --rm -it -v $HOME/.ssh:/root/.ssh -v /var/run/docker.sock:/var/run/docker.sock -v ${PWD}/:/workdir ghcr.io/mrsked/mrsk'
|
||||
```
|
||||
|
||||
Then, inside your app directory, run `mrsk init` (or `mrsk init --bundle` within Rails 7+ apps where you want a bin/mrsk binstub). Now edit the new file `config/deploy.yml`. It could look as simple as this:
|
||||
|
||||
```yaml
|
||||
service: hey
|
||||
@@ -23,7 +39,7 @@ env:
|
||||
- RAILS_MASTER_KEY
|
||||
```
|
||||
|
||||
Then edit your `.env` file to add your registry password as `MRSK_REGISTRY_PASSWORD` (and your `RAILS_MASTER_KEY` for production with a Rails app).
|
||||
Then edit your `.env` file to add your registry password as `MRSK_REGISTRY_PASSWORD` (and your `RAILS_MASTER_KEY` for production with a Rails app).
|
||||
|
||||
Now you're ready to deploy to the servers:
|
||||
|
||||
@@ -34,7 +50,7 @@ mrsk deploy
|
||||
This will:
|
||||
|
||||
1. Connect to the servers over SSH (using root by default, authenticated by your ssh key)
|
||||
2. Install Docker on any server that might be missing it (using apt-get)
|
||||
2. Install Docker on any server that might be missing it (using apt-get): root access is needed via ssh for this.
|
||||
3. Log into the registry both locally and remotely
|
||||
4. Build the image using the standard Dockerfile in the root of the application.
|
||||
5. Push the image to the registry.
|
||||
@@ -67,6 +83,16 @@ Docker Swarm is much simpler than Kubernetes, but it's still built on the same d
|
||||
|
||||
Ultimately, there are a myriad of ways to deploy web apps, but this is the toolkit we're using at [37signals](https://37signals.com) to bring [HEY](https://www.hey.com) [home from the cloud](https://world.hey.com/dhh/why-we-re-leaving-the-cloud-654b47e0) without losing the advantages of modern containerization tooling.
|
||||
|
||||
## Running MRSK from Docker
|
||||
|
||||
MRSK is packaged up in a Docker container similarly to [rails/docked](https://github.com/rails/docked). This will allow you to run MRSK (from your application directory) without having to install any dependencies other than Docker. Add the following alias to your profile configuration to make working with the container more convenient:
|
||||
|
||||
```bash
|
||||
alias mrsk="docker run -it --rm -v '${PWD}:/workdir' -v '${SSH_AUTH_SOCK}:/ssh-agent' -v /var/run/docker.sock:/var/run/docker.sock -e 'SSH_AUTH_SOCK=/ssh-agent' ghcr.io/mrsked/mrsk:latest"
|
||||
```
|
||||
|
||||
Since MRSK uses SSH to establish a remote connection, it will need access to your SSH agent. The above command uses a volume mount to make it available inside the container and configures the SSH agent inside the container to make use of it.
|
||||
|
||||
## Configuration
|
||||
|
||||
### Using .env file to load required environment variables
|
||||
@@ -99,9 +125,9 @@ If you need separate env variables for different destinations, you can set them
|
||||
|
||||
#### 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.
|
||||
If you are using open source secret store like bitwarden, you can create `.env.erb` as a template which looks up the secrets.
|
||||
|
||||
You can store `SOME_SECRET` in a secure note in bitwarden vault.
|
||||
You can store `SOME_SECRET` in a secure note in bitwarden vault.
|
||||
|
||||
```
|
||||
$ bw list items --search SOME_SECRET | jq
|
||||
@@ -140,7 +166,7 @@ SOME_SECRET=<%= `bw get notes 123123123-1232-4224-222f-234234234234 --session #{
|
||||
<% else raise ArgumentError, "session_token token missing" end %>
|
||||
```
|
||||
|
||||
Then everyone deploying the app can run `mrsk envify` and mrsk will generate `.env`
|
||||
Then everyone deploying the app can run `mrsk envify` and mrsk will generate `.env`
|
||||
|
||||
|
||||
### Using another registry than Docker Hub
|
||||
@@ -150,9 +176,9 @@ The default registry is Docker Hub, but you can change it using `registry/server
|
||||
```yaml
|
||||
registry:
|
||||
server: registry.digitalocean.com
|
||||
username:
|
||||
username:
|
||||
- DOCKER_REGISTRY_TOKEN
|
||||
password:
|
||||
password:
|
||||
- DOCKER_REGISTRY_TOKEN
|
||||
```
|
||||
|
||||
@@ -167,6 +193,15 @@ ssh:
|
||||
user: app
|
||||
```
|
||||
|
||||
If you are using non-root user, you need to bootstrap your servers manually, before using them with MRSK. On Ubuntu, you'd do:
|
||||
|
||||
```bash
|
||||
sudo apt update
|
||||
sudo apt upgrade -y
|
||||
sudo apt install -y docker.io curl git
|
||||
sudo usermod -a -G docker ubuntu
|
||||
```
|
||||
|
||||
### Using a proxy SSH host
|
||||
|
||||
If you need to connect to server through a proxy host, you can use `ssh/proxy`:
|
||||
@@ -183,6 +218,13 @@ ssh:
|
||||
proxy: "app@192.168.0.1"
|
||||
```
|
||||
|
||||
Also if you need specific proxy command to connect to the server:
|
||||
|
||||
```yaml
|
||||
ssh:
|
||||
proxy_command: aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p' --region=us-east-1 ## ssh via aws ssm
|
||||
```
|
||||
|
||||
### Using env variables
|
||||
|
||||
You can inject env variables into the app containers using `env`:
|
||||
@@ -222,6 +264,12 @@ volumes:
|
||||
- "/local/path:/container/path"
|
||||
```
|
||||
|
||||
### MRSK env variables
|
||||
|
||||
The following env variables are set when your container runs:
|
||||
|
||||
`MRSK_CONTAINER_NAME` : this contains the current container name and version
|
||||
|
||||
### Using different roles for servers
|
||||
|
||||
If your application uses separate hosts for running jobs or other roles beyond the default web running, you can specify these hosts in a dedicated role with a new entrypoint command like so:
|
||||
@@ -256,12 +304,13 @@ servers:
|
||||
|
||||
You can specialize the default Traefik rules by setting labels on the containers that are being started:
|
||||
|
||||
```
|
||||
```yaml
|
||||
labels:
|
||||
traefik.http.routers.hey.rule: Host(\`app.hey.com\`)
|
||||
traefik.http.routers.hey-web.rule: Host(`app.hey.com`)
|
||||
```
|
||||
Traefik rules are in the "service-role-destination" format. The default role will be `web` if no rule is specified. If the destination is not specified, it is not included. To give an example, the above rule would become "traefik.http.routers.hey-web-staging.rule" if it was for the "staging" destination.
|
||||
|
||||
Note: The escaped backticks are needed to ensure the rule is passed in correctly and not treated as command substitution by Bash!
|
||||
Note: The backticks are needed to ensure the rule is passed in correctly and not treated as command substitution by Bash!
|
||||
|
||||
This allows you to run multiple applications on the same server sharing the same Traefik instance and port.
|
||||
See https://doc.traefik.io/traefik/routing/routers/#rule for a full list of available routing rules.
|
||||
@@ -282,6 +331,21 @@ servers:
|
||||
my-label: "50"
|
||||
```
|
||||
|
||||
### Using shell expansion
|
||||
|
||||
You can use shell expansion to interpolate values from the host machine into labels and env variables with the `${}` syntax.
|
||||
Anything within the curly braces will be executed on the host machine and the result will be interpolated into the label or env variable.
|
||||
|
||||
```yaml
|
||||
labels:
|
||||
host-machine: "${cat /etc/hostname}"
|
||||
|
||||
env:
|
||||
HOST_DEPLOYMENT_DIR: "${PWD}"
|
||||
```
|
||||
|
||||
Note: Any other occurrence of `$` will be escaped to prevent unwanted shell expansion!
|
||||
|
||||
### Using container options
|
||||
|
||||
You can specialize the options used to start containers using the `options` definitions:
|
||||
@@ -303,6 +367,29 @@ servers:
|
||||
|
||||
That'll start the job containers with `docker run ... --cap-add --cpu-count 4 ...`.
|
||||
|
||||
### Configuring logging
|
||||
|
||||
You can configure the logging driver and options passed to Docker using `logging`:
|
||||
|
||||
```yaml
|
||||
logging:
|
||||
driver: awslogs
|
||||
options:
|
||||
awslogs-region: "eu-central-2"
|
||||
awslogs-group: "my-app"
|
||||
```
|
||||
|
||||
If nothing is configured, the default option `max-size=10m` is used for all containers. The default logging driver of Docker is `json-file`.
|
||||
|
||||
### Using a different stop wait time
|
||||
|
||||
On a new deploy, each old running container is gracefully shut down with a `SIGTERM`, and after a grace period of `10` seconds a `SIGKILL` is sent.
|
||||
You can configure this value via the `stop_wait_time` option:
|
||||
|
||||
```yaml
|
||||
stop_wait_time: 30
|
||||
```
|
||||
|
||||
### Using remote builder for native multi-arch
|
||||
|
||||
If you're developing on ARM64 (like Apple Silicon), but you want to deploy on AMD64 (x86 64-bit), you can use multi-architecture images. By default, MRSK will setup a local buildx configuration that does this through QEMU emulation. But this can be quite slow, especially on the first build.
|
||||
@@ -386,9 +473,9 @@ RUN --mount=type=secret,id=GITHUB_TOKEN \
|
||||
rm -rf /usr/local/bundle/cache
|
||||
```
|
||||
|
||||
### Using command arguments for Traefik
|
||||
### Traefik command arguments
|
||||
|
||||
You can customize the traefik command line:
|
||||
Customize the Traefik command line using `args`:
|
||||
|
||||
```yaml
|
||||
traefik:
|
||||
@@ -397,17 +484,90 @@ traefik:
|
||||
accesslog.format: json
|
||||
```
|
||||
|
||||
This will start the traefik container with `--accesslog=true accesslog.format=json`.
|
||||
This starts the Traefik container with `--accesslog=true --accesslog.format=json` arguments.
|
||||
|
||||
### Traefik's host port binding
|
||||
### Traefik host port binding
|
||||
|
||||
By default Traefik binds to port 80 of the host machine, it can be configured to use an alternative port:
|
||||
Traefik binds to port 80 by default. Specify an alternative port using `host_port`:
|
||||
|
||||
```yaml
|
||||
traefik:
|
||||
host_port: 8080
|
||||
```
|
||||
|
||||
### Traefik version, upgrades, and custom images
|
||||
|
||||
MRSK runs the traefik:v2.9 image to track Traefik 2.9.x releases.
|
||||
|
||||
To pin Traefik to a specific version or an image published to your registry,
|
||||
specify `image`:
|
||||
|
||||
```yaml
|
||||
traefik:
|
||||
image: traefik:v2.10.0-rc1
|
||||
```
|
||||
|
||||
This is useful for downgrading Traefik if there's an unexpected breaking
|
||||
change in a minor version release, upgrading Traefik to test forthcoming
|
||||
releases, or running your own Traefik-derived image.
|
||||
|
||||
MRSK has not been tested for compatibility with Traefik 3 betas. Please do!
|
||||
|
||||
### Traefik container configuration
|
||||
|
||||
Pass additional Docker configuration for the Traefik container using `options`:
|
||||
|
||||
```yaml
|
||||
traefik:
|
||||
options:
|
||||
publish:
|
||||
- 8080:8080
|
||||
volumes:
|
||||
- /tmp/example.json:/tmp/example.json
|
||||
memory: 512m
|
||||
```
|
||||
|
||||
This starts the Traefik container with `--volume /tmp/example.json:/tmp/example.json --publish 8080:8080 --memory 512m` arguments to `docker run`.
|
||||
|
||||
### Traefik container labels
|
||||
|
||||
Add labels to Traefik Docker container.
|
||||
|
||||
```yaml
|
||||
traefik:
|
||||
labels:
|
||||
traefik.enable: true
|
||||
traefik.http.routers.dashboard.rule: Host(`traefik.example.com`) && (PathPrefix(`/api`) || PathPrefix(`/dashboard`))
|
||||
traefik.http.routers.dashboard.service: api@internal
|
||||
traefik.http.routers.dashboard.middlewares: auth
|
||||
traefik.http.middlewares.auth.basicauth.users: test:$2y$05$H2o72tMaO.TwY1wNQUV1K.fhjRgLHRDWohFvUZOJHBEtUXNKrqUKi # test:password
|
||||
```
|
||||
|
||||
This labels Traefik container with `--label traefik.http.routers.dashboard.middlewares=\"auth\"` and so on.
|
||||
|
||||
### Traefik alternate entrypoints
|
||||
|
||||
You can configure multiple entrypoints for Traefik like so:
|
||||
|
||||
```yaml
|
||||
service: myservice
|
||||
|
||||
labels:
|
||||
traefik.tcp.routers.other.rule: 'HostSNI(`*`)'
|
||||
traefik.tcp.routers.other.entrypoints: otherentrypoint
|
||||
traefik.tcp.services.other.loadbalancer.server.port: 9000
|
||||
traefik.http.routers.myservice.entrypoints: web
|
||||
traefik.http.services.myservice.loadbalancer.server.port: 8080
|
||||
|
||||
traefik:
|
||||
options:
|
||||
publish:
|
||||
- 9000:9000
|
||||
args:
|
||||
entrypoints.web.address: ':80'
|
||||
entrypoints.otherentrypoint.address: ':9000'
|
||||
```
|
||||
|
||||
### Configuring build args for new images
|
||||
|
||||
Build arguments that aren't secret can also be configured:
|
||||
@@ -427,7 +587,7 @@ FROM ruby:$RUBY_VERSION-slim as base
|
||||
|
||||
### Using accessories for database, cache, search services
|
||||
|
||||
You can manage your accessory services via MRSK as well. The services will build off public images, and will not be automatically updated when you deploy:
|
||||
You can manage your accessory services via MRSK as well. Accessories are long-lived services that your app depends on. They are not updated when you deploy.
|
||||
|
||||
```yaml
|
||||
accessories:
|
||||
@@ -442,16 +602,44 @@ accessories:
|
||||
- MYSQL_ROOT_PASSWORD
|
||||
volumes:
|
||||
- /var/lib/mysql:/var/lib/mysql
|
||||
options:
|
||||
cpus: 4
|
||||
memory: "2GB"
|
||||
redis:
|
||||
image: redis:latest
|
||||
host: 1.1.1.4
|
||||
roles:
|
||||
- web
|
||||
port: "36379:6379"
|
||||
volumes:
|
||||
- /var/lib/redis:/data
|
||||
internal-example:
|
||||
image: registry.digitalocean.com/user/otherservice:latest
|
||||
host: 1.1.1.5
|
||||
port: 44444
|
||||
```
|
||||
|
||||
The hosts that the accessories will run on can be specified by hosts or roles:
|
||||
|
||||
```yaml
|
||||
# Single host
|
||||
mysql:
|
||||
host: 1.1.1.1
|
||||
# Multiple hosts
|
||||
redis:
|
||||
hosts:
|
||||
- 1.1.1.1
|
||||
- 1.1.1.2
|
||||
# By role
|
||||
monitoring:
|
||||
roles:
|
||||
- web
|
||||
- jobs
|
||||
```
|
||||
|
||||
Now run `mrsk accessory start mysql` to start the MySQL server on 1.1.1.3. See `mrsk accessory` for all the commands possible.
|
||||
|
||||
Accessory images must be public or tagged in your private registry.
|
||||
|
||||
### Using Cron
|
||||
|
||||
You can use a specific container to run your Cron jobs:
|
||||
@@ -489,18 +677,60 @@ That'll post a line like follows to a preconfigured chatbot in Basecamp:
|
||||
[My App] [dhh] Rolled back to version d264c4e92470ad1bd18590f04466787262f605de
|
||||
```
|
||||
|
||||
### Using custom healthcheck path or port
|
||||
`MRSK_*` environment variables are available to the broadcast command for
|
||||
fine-grained audit reporting, e.g. for triggering deployment reports or
|
||||
firing a JSON webhook. These variables include:
|
||||
- `MRSK_RECORDED_AT` - UTC timestamp in ISO 8601 format, e.g. `2023-04-14T17:07:31Z`
|
||||
- `MRSK_PERFORMER` - the local user performing the command (from `whoami`)
|
||||
- `MRSK_MESSAGE` - the full audit message, e.g. "Deployed app@150b24f"
|
||||
- `MRSK_DESTINATION` - optional: destination, e.g. "staging"
|
||||
- `MRSK_ROLE` - optional: role targeted, e.g. "web"
|
||||
|
||||
MRSK defaults to checking the health of your application again `/up` on port 3000. You can tailor both with the `healthcheck` setting:
|
||||
Use `mrsk broadcast` to test and troubleshoot your broadcast command:
|
||||
|
||||
```bash
|
||||
mrsk broadcast -m "test audit message"
|
||||
```
|
||||
|
||||
### Healthcheck
|
||||
|
||||
MRSK uses Docker healtchecks to check the health of your application during deployment. Traefik uses this same healthcheck status to determine when a container is ready to receive traffic.
|
||||
|
||||
The healthcheck defaults to testing the HTTP response to the path `/up` on port 3000, up to 7 times. You can tailor this behaviour with the `healthcheck` setting:
|
||||
|
||||
```yaml
|
||||
healthcheck:
|
||||
path: /healthz
|
||||
port: 4000
|
||||
max_attempts: 7
|
||||
```
|
||||
|
||||
This will ensure your application is configured with a traefik label for the healthcheck against `/healthz` and that the pre-deploy healthcheck that MRSK performs is done against the same path on port 4000.
|
||||
|
||||
You can also specify a custom healthcheck command, which is useful for non-HTTP services:
|
||||
|
||||
```yaml
|
||||
healthcheck:
|
||||
cmd: /bin/check_health
|
||||
```
|
||||
|
||||
The top-level healthcheck configuration applies to all services that use
|
||||
Traefik, by default. You can also specialize the configuration at the role
|
||||
level:
|
||||
|
||||
```yaml
|
||||
servers:
|
||||
job:
|
||||
hosts: ...
|
||||
cmd: bin/jobs
|
||||
healthcheck:
|
||||
cmd: bin/check
|
||||
```
|
||||
|
||||
The healthcheck allows for an optional `max_attempts` setting, which will attempt the healthcheck up to the specified number of times before failing the deploy. This is useful for applications that take a while to start up. The default is 7.
|
||||
|
||||
Note: The HTTP health checks assume that the `curl` command is available inside the container. If that's not the case, use the healthcheck's `cmd` option to specify an alternative check that the container supports.
|
||||
|
||||
## Commands
|
||||
|
||||
### Running commands on servers
|
||||
@@ -616,6 +846,48 @@ Note that by default old containers are pruned after 3 days when you run `mrsk d
|
||||
|
||||
If you wish to remove the entire application, including Traefik, containers, images, and registry session, you can run `mrsk remove`. This will leave the servers clean.
|
||||
|
||||
## Locking
|
||||
|
||||
Commands that are unsafe to run concurrently will take a deploy lock while they run. The lock is the `mrsk_lock` directory on the primary server.
|
||||
|
||||
You can check the lock status with:
|
||||
|
||||
```
|
||||
mrsk lock status
|
||||
|
||||
Locked by: AN Other at 2023-03-24 09:49:03 UTC
|
||||
Version: 77f45c0686811c68989d6576748475a60bf53fc2
|
||||
Message: Automatic deploy lock
|
||||
```
|
||||
|
||||
You can also manually acquire and release the lock
|
||||
|
||||
```
|
||||
mrsk lock acquire -m "Doing maintanence"
|
||||
```
|
||||
|
||||
```
|
||||
mrsk lock release
|
||||
```
|
||||
|
||||
## Rolling deployments
|
||||
|
||||
When deploying to large numbers of hosts, you might prefer not to restart your services on every host at the same time.
|
||||
|
||||
MRSK's default is to boot new containers on all hosts in parallel. But you can control this by configuring `boot/limit` and `boot/wait` as options:
|
||||
|
||||
```yaml
|
||||
service: myservice
|
||||
|
||||
boot:
|
||||
limit: 10 # Can also specify as a percentage of total hosts, such as "25%"
|
||||
wait: 2
|
||||
```
|
||||
|
||||
When `limit` is specified, containers will be booted on, at most, `limit` hosts at once. MRSK will pause for `wait` seconds between batches.
|
||||
|
||||
These settings only apply when booting containers (using `mrsk deploy`, or `mrsk app boot`). For other commands, MRSK continues to run commands in parallel across all hosts.
|
||||
|
||||
## Stage of development
|
||||
|
||||
This is beta software. Commands may still move around. But we're live in production at [37signals](https://37signals.com).
|
||||
|
||||
2
bin/mrsk
2
bin/mrsk
@@ -10,7 +10,9 @@ begin
|
||||
rescue SSHKit::Runner::ExecuteError => e
|
||||
puts " \e[31mERROR (#{e.cause.class}): #{e.cause.message}\e[0m"
|
||||
puts e.cause.backtrace if ENV["VERBOSE"]
|
||||
exit 1
|
||||
rescue => e
|
||||
puts " \e[31mERROR (#{e.class}): #{e.message}\e[0m"
|
||||
puts e.backtrace if ENV["VERBOSE"]
|
||||
exit 1
|
||||
end
|
||||
|
||||
9
gemfiles/rails_edge.gemfile
Normal file
9
gemfiles/rails_edge.gemfile
Normal file
@@ -0,0 +1,9 @@
|
||||
source 'https://rubygems.org'
|
||||
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
|
||||
|
||||
git "https://github.com/rails/rails.git" do
|
||||
gem "railties"
|
||||
gem "activesupport"
|
||||
end
|
||||
|
||||
gemspec path: "../"
|
||||
@@ -1,6 +1,7 @@
|
||||
module Mrsk
|
||||
end
|
||||
|
||||
require "active_support"
|
||||
require "zeitwerk"
|
||||
|
||||
loader = Zeitwerk::Loader.for_gem
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
module Mrsk::Cli
|
||||
class LockError < StandardError; end
|
||||
end
|
||||
|
||||
# SSHKit uses instance eval, so we need a global const for ergonomics
|
||||
|
||||
@@ -1,33 +1,38 @@
|
||||
class Mrsk::Cli::Accessory < Mrsk::Cli::Base
|
||||
desc "boot [NAME]", "Boot new accessory service on host (use NAME=all to boot all accessories)"
|
||||
def boot(name)
|
||||
if name == "all"
|
||||
MRSK.accessory_names.each { |accessory_name| boot(accessory_name) }
|
||||
else
|
||||
with_accessory(name) do |accessory|
|
||||
directories(name)
|
||||
upload(name)
|
||||
with_lock do
|
||||
if name == "all"
|
||||
MRSK.accessory_names.each { |accessory_name| boot(accessory_name) }
|
||||
else
|
||||
with_accessory(name) do |accessory|
|
||||
directories(name)
|
||||
upload(name)
|
||||
|
||||
on(accessory.host) do
|
||||
execute *MRSK.auditor.record("Booted #{name} accessory"), verbosity: :debug
|
||||
execute *accessory.run
|
||||
on(accessory.hosts) do
|
||||
execute *MRSK.registry.login
|
||||
execute *MRSK.auditor.record("Booted #{name} accessory"), verbosity: :debug
|
||||
execute *accessory.run
|
||||
end
|
||||
|
||||
audit_broadcast "Booted accessory #{name}" unless options[:skip_broadcast]
|
||||
end
|
||||
|
||||
audit_broadcast "Booted accessory #{name}" unless options[:skip_broadcast]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
desc "upload [NAME]", "Upload accessory files to host", hide: true
|
||||
def upload(name)
|
||||
with_accessory(name) do |accessory|
|
||||
on(accessory.host) do
|
||||
accessory.files.each do |(local, remote)|
|
||||
accessory.ensure_local_file_present(local)
|
||||
with_lock do
|
||||
with_accessory(name) do |accessory|
|
||||
on(accessory.hosts) do
|
||||
accessory.files.each do |(local, remote)|
|
||||
accessory.ensure_local_file_present(local)
|
||||
|
||||
execute *accessory.make_directory_for(remote)
|
||||
upload! local, remote
|
||||
execute :chmod, "755", remote
|
||||
execute *accessory.make_directory_for(remote)
|
||||
upload! local, remote
|
||||
execute :chmod, "755", remote
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -35,10 +40,12 @@ class Mrsk::Cli::Accessory < Mrsk::Cli::Base
|
||||
|
||||
desc "directories [NAME]", "Create accessory directories on host", hide: true
|
||||
def directories(name)
|
||||
with_accessory(name) do |accessory|
|
||||
on(accessory.host) do
|
||||
accessory.directories.keys.each do |host_path|
|
||||
execute *accessory.make_directory(host_path)
|
||||
with_lock do
|
||||
with_accessory(name) do |accessory|
|
||||
on(accessory.hosts) do
|
||||
accessory.directories.keys.each do |host_path|
|
||||
execute *accessory.make_directory(host_path)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -46,38 +53,46 @@ class Mrsk::Cli::Accessory < Mrsk::Cli::Base
|
||||
|
||||
desc "reboot [NAME]", "Reboot existing accessory on host (stop container, remove container, start new container)"
|
||||
def reboot(name)
|
||||
with_accessory(name) do |accessory|
|
||||
stop(name)
|
||||
remove_container(name)
|
||||
boot(name)
|
||||
with_lock do
|
||||
with_accessory(name) do |accessory|
|
||||
stop(name)
|
||||
remove_container(name)
|
||||
boot(name)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
desc "start [NAME]", "Start existing accessory container on host"
|
||||
def start(name)
|
||||
with_accessory(name) do |accessory|
|
||||
on(accessory.host) do
|
||||
execute *MRSK.auditor.record("Started #{name} accessory"), verbosity: :debug
|
||||
execute *accessory.start
|
||||
with_lock do
|
||||
with_accessory(name) do |accessory|
|
||||
on(accessory.hosts) do
|
||||
execute *MRSK.auditor.record("Started #{name} accessory"), verbosity: :debug
|
||||
execute *accessory.start
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
desc "stop [NAME]", "Stop existing accessory container on host"
|
||||
def stop(name)
|
||||
with_accessory(name) do |accessory|
|
||||
on(accessory.host) do
|
||||
execute *MRSK.auditor.record("Stopped #{name} accessory"), verbosity: :debug
|
||||
execute *accessory.stop, raise_on_non_zero_exit: false
|
||||
with_lock do
|
||||
with_accessory(name) do |accessory|
|
||||
on(accessory.hosts) do
|
||||
execute *MRSK.auditor.record("Stopped #{name} accessory"), verbosity: :debug
|
||||
execute *accessory.stop, raise_on_non_zero_exit: false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
desc "restart [NAME]", "Restart existing accessory container on host"
|
||||
def restart(name)
|
||||
with_accessory(name) do
|
||||
stop(name)
|
||||
start(name)
|
||||
with_lock do
|
||||
with_accessory(name) do
|
||||
stop(name)
|
||||
start(name)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -87,7 +102,7 @@ class Mrsk::Cli::Accessory < Mrsk::Cli::Base
|
||||
MRSK.accessory_names.each { |accessory_name| details(accessory_name) }
|
||||
else
|
||||
with_accessory(name) do |accessory|
|
||||
on(accessory.host) { puts capture_with_info(*accessory.info) }
|
||||
on(accessory.hosts) { puts capture_with_info(*accessory.info) }
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -108,14 +123,14 @@ class Mrsk::Cli::Accessory < Mrsk::Cli::Base
|
||||
|
||||
when options[:reuse]
|
||||
say "Launching command from existing container...", :magenta
|
||||
on(accessory.host) do
|
||||
on(accessory.hosts) do
|
||||
execute *MRSK.auditor.record("Executed cmd '#{cmd}' on #{name} accessory"), verbosity: :debug
|
||||
capture_with_info(*accessory.execute_in_existing_container(cmd))
|
||||
end
|
||||
|
||||
else
|
||||
say "Launching command from new container...", :magenta
|
||||
on(accessory.host) do
|
||||
on(accessory.hosts) do
|
||||
execute *MRSK.auditor.record("Executed cmd '#{cmd}' on #{name} accessory"), verbosity: :debug
|
||||
capture_with_info(*accessory.execute_in_new_container(cmd))
|
||||
end
|
||||
@@ -134,7 +149,7 @@ class Mrsk::Cli::Accessory < Mrsk::Cli::Base
|
||||
|
||||
if options[:follow]
|
||||
run_locally do
|
||||
info "Following logs on #{accessory.host}..."
|
||||
info "Following logs on #{accessory.hosts}..."
|
||||
info accessory.follow_logs(grep: grep)
|
||||
exec accessory.follow_logs(grep: grep)
|
||||
end
|
||||
@@ -142,25 +157,27 @@ class Mrsk::Cli::Accessory < Mrsk::Cli::Base
|
||||
since = options[:since]
|
||||
lines = options[:lines].presence || ((since || grep) ? nil : 100) # Default to 100 lines if since or grep isn't set
|
||||
|
||||
on(accessory.host) do
|
||||
on(accessory.hosts) do
|
||||
puts capture_with_info(*accessory.logs(since: since, lines: lines, grep: grep))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
desc "remove [NAME]", "Remove accessory container and image from host (use NAME=all to remove all accessories)"
|
||||
desc "remove [NAME]", "Remove accessory container, image and data directory from host (use NAME=all to remove all accessories)"
|
||||
option :confirmed, aliases: "-y", type: :boolean, default: false, desc: "Proceed without confirmation question"
|
||||
def remove(name)
|
||||
if name == "all"
|
||||
MRSK.accessory_names.each { |accessory_name| remove(accessory_name) }
|
||||
else
|
||||
if options[:confirmed] || ask("This will remove all containers and images for #{name}. Are you sure?", limited_to: %w( y N ), default: "N") == "y"
|
||||
with_accessory(name) do
|
||||
stop(name)
|
||||
remove_container(name)
|
||||
remove_image(name)
|
||||
remove_service_directory(name)
|
||||
with_lock do
|
||||
if name == "all"
|
||||
MRSK.accessory_names.each { |accessory_name| remove(accessory_name) }
|
||||
else
|
||||
if options[:confirmed] || ask("This will remove all containers, images and data directories for #{name}. Are you sure?", limited_to: %w( y N ), default: "N") == "y"
|
||||
with_accessory(name) do
|
||||
stop(name)
|
||||
remove_container(name)
|
||||
remove_image(name)
|
||||
remove_service_directory(name)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -168,29 +185,35 @@ class Mrsk::Cli::Accessory < Mrsk::Cli::Base
|
||||
|
||||
desc "remove_container [NAME]", "Remove accessory container from host", hide: true
|
||||
def remove_container(name)
|
||||
with_accessory(name) do |accessory|
|
||||
on(accessory.host) do
|
||||
execute *MRSK.auditor.record("Remove #{name} accessory container"), verbosity: :debug
|
||||
execute *accessory.remove_container
|
||||
with_lock do
|
||||
with_accessory(name) do |accessory|
|
||||
on(accessory.hosts) do
|
||||
execute *MRSK.auditor.record("Remove #{name} accessory container"), verbosity: :debug
|
||||
execute *accessory.remove_container
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
desc "remove_image [NAME]", "Remove accessory image from host", hide: true
|
||||
def remove_image(name)
|
||||
with_accessory(name) do |accessory|
|
||||
on(accessory.host) do
|
||||
execute *MRSK.auditor.record("Removed #{name} accessory image"), verbosity: :debug
|
||||
execute *accessory.remove_image
|
||||
with_lock do
|
||||
with_accessory(name) do |accessory|
|
||||
on(accessory.hosts) do
|
||||
execute *MRSK.auditor.record("Removed #{name} accessory image"), verbosity: :debug
|
||||
execute *accessory.remove_image
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
desc "remove_service_directory [NAME]", "Remove accessory directory used for uploaded files and data directories from host", hide: true
|
||||
def remove_service_directory(name)
|
||||
with_accessory(name) do |accessory|
|
||||
on(accessory.host) do
|
||||
execute *accessory.remove_service_directory
|
||||
with_lock do
|
||||
with_accessory(name) do |accessory|
|
||||
on(accessory.hosts) do
|
||||
execute *accessory.remove_service_directory
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,33 +1,38 @@
|
||||
class Mrsk::Cli::App < Mrsk::Cli::Base
|
||||
desc "boot", "Boot app on servers (or reboot app if already running)"
|
||||
def boot
|
||||
say "Get most recent version available as an image...", :magenta unless options[:version]
|
||||
using_version(options[:version] || most_recent_version_available) do |version|
|
||||
say "Start container with version #{version} using a #{MRSK.config.readiness_delay}s readiness delay (or reboot if already running)...", :magenta
|
||||
with_lock do
|
||||
say "Get most recent version available as an image...", :magenta unless options[:version]
|
||||
using_version(version_or_latest) do |version|
|
||||
say "Start container with version #{version} using a #{MRSK.config.readiness_delay}s readiness delay (or reboot if already running)...", :magenta
|
||||
|
||||
cli = self
|
||||
|
||||
MRSK.config.roles.each do |role|
|
||||
on(role.hosts) do |host|
|
||||
execute *MRSK.auditor.record("Booted app version #{version}"), verbosity: :debug
|
||||
on(MRSK.hosts) do
|
||||
execute *MRSK.auditor.record("Tagging #{MRSK.config.absolute_image} as the latest image"), verbosity: :debug
|
||||
execute *MRSK.app.tag_current_as_latest
|
||||
end
|
||||
|
||||
begin
|
||||
old_version = capture_with_info(*MRSK.app.current_running_version).strip
|
||||
execute *MRSK.app.run(role: role.name)
|
||||
sleep MRSK.config.readiness_delay
|
||||
execute *MRSK.app.stop(version: old_version), raise_on_non_zero_exit: false if old_version.present?
|
||||
on(MRSK.hosts, **MRSK.boot_strategy) do |host|
|
||||
roles = MRSK.roles_on(host)
|
||||
|
||||
rescue SSHKit::Command::Failed => e
|
||||
if e.message =~ /already in use/
|
||||
error "Rebooting container with same version #{version} already deployed on #{host} (may cause gap in zero-downtime promise!)"
|
||||
execute *MRSK.auditor.record("Rebooted app version #{version}"), verbosity: :debug
|
||||
roles.each do |role|
|
||||
app = MRSK.app(role: role)
|
||||
auditor = MRSK.auditor(role: role)
|
||||
|
||||
execute *MRSK.app.stop(version: version)
|
||||
execute *MRSK.app.remove_container(version: version)
|
||||
execute *MRSK.app.run(role: role.name)
|
||||
else
|
||||
raise
|
||||
execute *auditor.record("Booted app version #{version}"), verbosity: :debug
|
||||
|
||||
if capture_with_info(*app.container_id_for_version(version), raise_on_non_zero_exit: false).present?
|
||||
tmp_version = "#{version}_#{SecureRandom.hex(8)}"
|
||||
info "Renaming container #{version} to #{tmp_version} as already deployed on #{host}"
|
||||
execute *auditor.record("Renaming container #{version} to #{tmp_version}"), verbosity: :debug
|
||||
execute *app.rename_container(version: version, new_version: tmp_version)
|
||||
end
|
||||
|
||||
old_version = capture_with_info(*app.current_running_version, raise_on_non_zero_exit: false).strip
|
||||
execute *app.run
|
||||
|
||||
Mrsk::Utils::HealthcheckPoller.wait_for_healthy(pause_after_ready: true) { capture_with_info(*app.status(version: version)) }
|
||||
|
||||
execute *app.stop(version: old_version), raise_on_non_zero_exit: false if old_version.present?
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -36,24 +41,42 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
|
||||
|
||||
desc "start", "Start existing app container on servers"
|
||||
def start
|
||||
on(MRSK.hosts) do
|
||||
execute *MRSK.auditor.record("Started app version #{MRSK.version}"), verbosity: :debug
|
||||
execute *MRSK.app.start, raise_on_non_zero_exit: false
|
||||
with_lock do
|
||||
on(MRSK.hosts) do |host|
|
||||
roles = MRSK.roles_on(host)
|
||||
|
||||
roles.each do |role|
|
||||
execute *MRSK.auditor.record("Started app version #{MRSK.config.version}"), verbosity: :debug
|
||||
execute *MRSK.app(role: role).start, raise_on_non_zero_exit: false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
desc "stop", "Stop app container on servers"
|
||||
def stop
|
||||
on(MRSK.hosts) do
|
||||
execute *MRSK.auditor.record("Stopped app"), verbosity: :debug
|
||||
execute *MRSK.app.stop, raise_on_non_zero_exit: false
|
||||
with_lock do
|
||||
on(MRSK.hosts) do |host|
|
||||
roles = MRSK.roles_on(host)
|
||||
|
||||
roles.each do |role|
|
||||
execute *MRSK.auditor.record("Stopped app", role: role), verbosity: :debug
|
||||
execute *MRSK.app(role: role).stop, raise_on_non_zero_exit: false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# FIXME: Drop in favor of just containers?
|
||||
desc "details", "Show details about app containers"
|
||||
def details
|
||||
on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.info) }
|
||||
on(MRSK.hosts) do |host|
|
||||
roles = MRSK.roles_on(host)
|
||||
|
||||
roles.each do |role|
|
||||
puts_by_host host, capture_with_info(*MRSK.app(role: role).info)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
desc "exec [CMD]", "Execute a custom command on servers (use --help to show options)"
|
||||
@@ -65,12 +88,12 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
|
||||
say "Get current version of running container...", :magenta unless options[:version]
|
||||
using_version(options[:version] || current_running_version) do |version|
|
||||
say "Launching interactive command with version #{version} via SSH from existing container on #{MRSK.primary_host}...", :magenta
|
||||
run_locally { exec MRSK.app.execute_in_existing_container_over_ssh(cmd, host: MRSK.primary_host) }
|
||||
run_locally { exec MRSK.app(role: "web").execute_in_existing_container_over_ssh(cmd, host: MRSK.primary_host) }
|
||||
end
|
||||
|
||||
when options[:interactive]
|
||||
say "Get most recent version available as an image...", :magenta unless options[:version]
|
||||
using_version(options[:version] || most_recent_version_available) do |version|
|
||||
using_version(version_or_latest) do |version|
|
||||
say "Launching interactive command with version #{version} via SSH from new container on #{MRSK.primary_host}...", :magenta
|
||||
run_locally { exec MRSK.app.execute_in_new_container_over_ssh(cmd, host: MRSK.primary_host) }
|
||||
end
|
||||
@@ -81,14 +104,18 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
|
||||
say "Launching command with version #{version} from existing container...", :magenta
|
||||
|
||||
on(MRSK.hosts) do |host|
|
||||
execute *MRSK.auditor.record("Executed cmd '#{cmd}' on app version #{version}"), verbosity: :debug
|
||||
puts_by_host host, capture_with_info(*MRSK.app.execute_in_existing_container(cmd))
|
||||
roles = MRSK.roles_on(host)
|
||||
|
||||
roles.each do |role|
|
||||
execute *MRSK.auditor.record("Executed cmd '#{cmd}' on app version #{version}", role: role), verbosity: :debug
|
||||
puts_by_host host, capture_with_info(*MRSK.app(role: role).execute_in_existing_container(cmd))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
else
|
||||
say "Get most recent version available as an image...", :magenta unless options[:version]
|
||||
using_version(options[:version] || most_recent_version_available) do |version|
|
||||
using_version(version_or_latest) do |version|
|
||||
say "Launching command with version #{version} from new container...", :magenta
|
||||
on(MRSK.hosts) do |host|
|
||||
execute *MRSK.auditor.record("Executed cmd '#{cmd}' on app version #{version}"), verbosity: :debug
|
||||
@@ -103,6 +130,31 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
|
||||
on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.list_containers) }
|
||||
end
|
||||
|
||||
desc "stale_containers", "Detect app stale containers"
|
||||
option :stop, aliases: "-s", type: :boolean, default: false, desc: "Stop the stale containers found"
|
||||
def stale_containers
|
||||
with_lock do
|
||||
stop = options[:stop]
|
||||
|
||||
cli = self
|
||||
|
||||
on(MRSK.hosts) do |host|
|
||||
roles = MRSK.roles_on(host)
|
||||
|
||||
roles.each do |role|
|
||||
cli.send(:stale_versions, host: host, role: role).each do |version|
|
||||
if stop
|
||||
puts_by_host host, "Stopping stale container for role #{role} with version #{version}"
|
||||
execute *MRSK.app(role: role).stop(version: version), raise_on_non_zero_exit: false
|
||||
else
|
||||
puts_by_host host, "Detected stale container for role #{role} with version #{version} (use `mrsk app stale_containers --stop` to stop)"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
desc "images", "Show app images on servers"
|
||||
def images
|
||||
on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.list_images) }
|
||||
@@ -121,18 +173,26 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
|
||||
if options[:follow]
|
||||
run_locally do
|
||||
info "Following logs on #{MRSK.primary_host}..."
|
||||
info MRSK.app.follow_logs(host: MRSK.primary_host, grep: grep)
|
||||
exec MRSK.app.follow_logs(host: MRSK.primary_host, grep: grep)
|
||||
|
||||
MRSK.specific_roles ||= ["web"]
|
||||
role = MRSK.roles_on(MRSK.primary_host).first
|
||||
|
||||
info MRSK.app(role: role).follow_logs(host: MRSK.primary_host, grep: grep)
|
||||
exec MRSK.app(role: role).follow_logs(host: MRSK.primary_host, grep: grep)
|
||||
end
|
||||
else
|
||||
since = options[:since]
|
||||
lines = options[:lines].presence || ((since || grep) ? nil : 100) # Default to 100 lines if since or grep isn't set
|
||||
|
||||
on(MRSK.hosts) do |host|
|
||||
begin
|
||||
puts_by_host host, capture_with_info(*MRSK.app.logs(since: since, lines: lines, grep: grep))
|
||||
rescue SSHKit::Command::Failed
|
||||
puts_by_host host, "Nothing found"
|
||||
roles = MRSK.roles_on(host)
|
||||
|
||||
roles.each do |role|
|
||||
begin
|
||||
puts_by_host host, capture_with_info(*MRSK.app(role: role).logs(since: since, lines: lines, grep: grep))
|
||||
rescue SSHKit::Command::Failed
|
||||
puts_by_host host, "Nothing found"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -140,32 +200,48 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
|
||||
|
||||
desc "remove", "Remove app containers and images from servers"
|
||||
def remove
|
||||
stop
|
||||
remove_containers
|
||||
remove_images
|
||||
with_lock do
|
||||
stop
|
||||
remove_containers
|
||||
remove_images
|
||||
end
|
||||
end
|
||||
|
||||
desc "remove_container [VERSION]", "Remove app container with given version from servers", hide: true
|
||||
def remove_container(version)
|
||||
on(MRSK.hosts) do
|
||||
execute *MRSK.auditor.record("Removed app container with version #{version}"), verbosity: :debug
|
||||
execute *MRSK.app.remove_container(version: version)
|
||||
with_lock do
|
||||
on(MRSK.hosts) do |host|
|
||||
roles = MRSK.roles_on(host)
|
||||
|
||||
roles.each do |role|
|
||||
execute *MRSK.auditor.record("Removed app container with version #{version}", role: role), verbosity: :debug
|
||||
execute *MRSK.app(role: role).remove_container(version: version)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
desc "remove_containers", "Remove all app containers from servers", hide: true
|
||||
def remove_containers
|
||||
on(MRSK.hosts) do
|
||||
execute *MRSK.auditor.record("Removed all app containers"), verbosity: :debug
|
||||
execute *MRSK.app.remove_containers
|
||||
with_lock do
|
||||
on(MRSK.hosts) do |host|
|
||||
roles = MRSK.roles_on(host)
|
||||
|
||||
roles.each do |role|
|
||||
execute *MRSK.auditor.record("Removed all app containers", role: role), verbosity: :debug
|
||||
execute *MRSK.app(role: role).remove_containers
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
desc "remove_images", "Remove all app images from servers", hide: true
|
||||
def remove_images
|
||||
on(MRSK.hosts) do
|
||||
execute *MRSK.auditor.record("Removed all app images"), verbosity: :debug
|
||||
execute *MRSK.app.remove_images
|
||||
with_lock do
|
||||
on(MRSK.hosts) do
|
||||
execute *MRSK.auditor.record("Removed all app images"), verbosity: :debug
|
||||
execute *MRSK.app.remove_images
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -189,20 +265,24 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
|
||||
end
|
||||
end
|
||||
|
||||
def most_recent_version_available(host: MRSK.primary_host)
|
||||
version = nil
|
||||
on(host) { version = capture_with_info(*MRSK.app.most_recent_version_from_available_images).strip }
|
||||
|
||||
if version == "<none>"
|
||||
raise "Most recent image available was not tagged with a version (returned <none>)"
|
||||
else
|
||||
version.presence
|
||||
end
|
||||
end
|
||||
|
||||
def current_running_version(host: MRSK.primary_host)
|
||||
version = nil
|
||||
on(host) { version = capture_with_info(*MRSK.app.current_running_version).strip }
|
||||
version.presence
|
||||
end
|
||||
|
||||
def stale_versions(host:, role:)
|
||||
versions = nil
|
||||
on(host) do
|
||||
versions = \
|
||||
capture_with_info(*MRSK.app(role: role).list_versions, raise_on_non_zero_exit: false)
|
||||
.split("\n")
|
||||
.drop(1)
|
||||
end
|
||||
versions
|
||||
end
|
||||
|
||||
def version_or_latest
|
||||
options[:version] || "latest"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -25,7 +25,7 @@ module Mrsk::Cli
|
||||
def initialize(*)
|
||||
super
|
||||
load_envs
|
||||
initialize_commander(options)
|
||||
initialize_commander(options_with_subcommand_class_options)
|
||||
end
|
||||
|
||||
private
|
||||
@@ -37,16 +37,12 @@ module Mrsk::Cli
|
||||
end
|
||||
end
|
||||
|
||||
def options_with_subcommand_class_options
|
||||
options.merge(@_initializer.last[:class_options] || {})
|
||||
end
|
||||
|
||||
def initialize_commander(options)
|
||||
MRSK.tap do |commander|
|
||||
commander.config_file = Pathname.new(File.expand_path(options[:config_file]))
|
||||
commander.destination = options[:destination]
|
||||
commander.version = options[:version]
|
||||
|
||||
commander.specific_hosts = options[:hosts]&.split(",")
|
||||
commander.specific_roles = options[:roles]&.split(",")
|
||||
commander.specific_primary! if options[:primary]
|
||||
|
||||
if options[:verbose]
|
||||
ENV["VERBOSE"] = "1" # For backtraces via cli/start
|
||||
commander.verbosity = :debug
|
||||
@@ -55,6 +51,15 @@ module Mrsk::Cli
|
||||
if options[:quiet]
|
||||
commander.verbosity = :error
|
||||
end
|
||||
|
||||
commander.configure \
|
||||
config_file: Pathname.new(File.expand_path(options[:config_file])),
|
||||
destination: options[:destination],
|
||||
version: options[:version]
|
||||
|
||||
commander.specific_hosts = options[:hosts]&.split(",")
|
||||
commander.specific_roles = options[:roles]&.split(",")
|
||||
commander.specific_primary! if options[:primary]
|
||||
end
|
||||
end
|
||||
|
||||
@@ -70,5 +75,58 @@ module Mrsk::Cli
|
||||
def audit_broadcast(line)
|
||||
run_locally { execute *MRSK.auditor.broadcast(line), verbosity: :debug }
|
||||
end
|
||||
|
||||
def with_lock
|
||||
if MRSK.holding_lock?
|
||||
yield
|
||||
else
|
||||
acquire_lock
|
||||
|
||||
begin
|
||||
yield
|
||||
rescue
|
||||
if MRSK.hold_lock_on_error?
|
||||
error " \e[31mDeploy lock was not released\e[0m"
|
||||
else
|
||||
release_lock
|
||||
end
|
||||
|
||||
raise
|
||||
end
|
||||
|
||||
release_lock
|
||||
end
|
||||
end
|
||||
|
||||
def acquire_lock
|
||||
say "Acquiring the deploy lock"
|
||||
on(MRSK.primary_host) { execute *MRSK.lock.acquire("Automatic deploy lock", MRSK.config.version) }
|
||||
|
||||
MRSK.holding_lock = true
|
||||
rescue SSHKit::Runner::ExecuteError => e
|
||||
if e.message =~ /cannot create directory/
|
||||
on(MRSK.primary_host) { execute *MRSK.lock.status }
|
||||
raise LockError, "Deploy lock found"
|
||||
else
|
||||
raise e
|
||||
end
|
||||
end
|
||||
|
||||
def release_lock
|
||||
say "Releasing the deploy lock"
|
||||
on(MRSK.primary_host) { execute *MRSK.lock.release }
|
||||
|
||||
MRSK.holding_lock = false
|
||||
end
|
||||
|
||||
def hold_lock_on_error
|
||||
if MRSK.hold_lock_on_error?
|
||||
yield
|
||||
else
|
||||
MRSK.hold_lock_on_error = true
|
||||
yield
|
||||
MRSK.hold_lock_on_error = false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,26 +1,34 @@
|
||||
class Mrsk::Cli::Build < Mrsk::Cli::Base
|
||||
class BuildError < StandardError; end
|
||||
|
||||
desc "deliver", "Build app and push app image to registry then pull image on servers"
|
||||
def deliver
|
||||
push
|
||||
pull
|
||||
with_lock do
|
||||
push
|
||||
pull
|
||||
end
|
||||
end
|
||||
|
||||
desc "push", "Build and push app image to registry"
|
||||
def push
|
||||
cli = self
|
||||
with_lock do
|
||||
cli = self
|
||||
|
||||
run_locally do
|
||||
begin
|
||||
MRSK.with_verbosity(:debug) { execute *MRSK.builder.push }
|
||||
rescue SSHKit::Command::Failed => e
|
||||
if e.message =~ /(no builder)|(no such file or directory)/
|
||||
error "Missing compatible builder, so creating a new one first"
|
||||
|
||||
if cli.create
|
||||
run_locally do
|
||||
begin
|
||||
if cli.verify_local_dependencies
|
||||
MRSK.with_verbosity(:debug) { execute *MRSK.builder.push }
|
||||
end
|
||||
else
|
||||
raise
|
||||
rescue SSHKit::Command::Failed => e
|
||||
if e.message =~ /(no builder)|(no such file or directory)/
|
||||
error "Missing compatible builder, so creating a new one first"
|
||||
|
||||
if cli.create
|
||||
MRSK.with_verbosity(:debug) { execute *MRSK.builder.push }
|
||||
end
|
||||
else
|
||||
raise
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -28,25 +36,29 @@ class Mrsk::Cli::Build < Mrsk::Cli::Base
|
||||
|
||||
desc "pull", "Pull app image from registry onto servers"
|
||||
def pull
|
||||
on(MRSK.hosts) do
|
||||
execute *MRSK.auditor.record("Pulled image with version #{MRSK.version}"), verbosity: :debug
|
||||
execute *MRSK.builder.clean, raise_on_non_zero_exit: false
|
||||
execute *MRSK.builder.pull
|
||||
with_lock do
|
||||
on(MRSK.hosts) do
|
||||
execute *MRSK.auditor.record("Pulled image with version #{MRSK.config.version}"), verbosity: :debug
|
||||
execute *MRSK.builder.clean, raise_on_non_zero_exit: false
|
||||
execute *MRSK.builder.pull
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
desc "create", "Create a build setup"
|
||||
def create
|
||||
run_locally do
|
||||
begin
|
||||
debug "Using builder: #{MRSK.builder.name}"
|
||||
execute *MRSK.builder.create
|
||||
rescue SSHKit::Command::Failed => e
|
||||
if e.message =~ /stderr=(.*)/
|
||||
error "Couldn't create remote builder: #{$1}"
|
||||
false
|
||||
else
|
||||
raise
|
||||
with_lock do
|
||||
run_locally do
|
||||
begin
|
||||
debug "Using builder: #{MRSK.builder.name}"
|
||||
execute *MRSK.builder.create
|
||||
rescue SSHKit::Command::Failed => e
|
||||
if e.message =~ /stderr=(.*)/
|
||||
error "Couldn't create remote builder: #{$1}"
|
||||
false
|
||||
else
|
||||
raise
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -54,9 +66,11 @@ class Mrsk::Cli::Build < Mrsk::Cli::Base
|
||||
|
||||
desc "remove", "Remove build setup"
|
||||
def remove
|
||||
run_locally do
|
||||
debug "Using builder: #{MRSK.builder.name}"
|
||||
execute *MRSK.builder.remove
|
||||
with_lock do
|
||||
run_locally do
|
||||
debug "Using builder: #{MRSK.builder.name}"
|
||||
execute *MRSK.builder.remove
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -67,4 +81,22 @@ class Mrsk::Cli::Build < Mrsk::Cli::Base
|
||||
puts capture(*MRSK.builder.info)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
desc "", "" # Really a private method, but needed to be invoked from #push
|
||||
def verify_local_dependencies
|
||||
run_locally do
|
||||
begin
|
||||
execute *MRSK.builder.ensure_local_dependencies_installed
|
||||
rescue SSHKit::Command::Failed => e
|
||||
build_error = e.message =~ /command not found/ ?
|
||||
"Docker is not installed locally" :
|
||||
"Docker buildx plugin is not installed locally"
|
||||
|
||||
raise BuildError, build_error
|
||||
end
|
||||
end
|
||||
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,8 +1,4 @@
|
||||
class Mrsk::Cli::Healthcheck < Mrsk::Cli::Base
|
||||
MAX_ATTEMPTS = 7
|
||||
|
||||
class HealthcheckError < StandardError; end
|
||||
|
||||
default_command :perform
|
||||
|
||||
desc "perform", "Health check current app version"
|
||||
@@ -10,37 +6,11 @@ class Mrsk::Cli::Healthcheck < Mrsk::Cli::Base
|
||||
on(MRSK.primary_host) do
|
||||
begin
|
||||
execute *MRSK.healthcheck.run
|
||||
|
||||
target = "Health check against #{MRSK.config.healthcheck["path"]}"
|
||||
attempt = 1
|
||||
|
||||
begin
|
||||
status = capture_with_info(*MRSK.healthcheck.curl)
|
||||
|
||||
if status == "200"
|
||||
info "#{target} succeeded with 200 OK!"
|
||||
else
|
||||
raise HealthcheckError, "#{target} failed with status #{status}"
|
||||
end
|
||||
rescue SSHKit::Command::Failed
|
||||
if attempt <= MAX_ATTEMPTS
|
||||
info "#{target} failed to respond, retrying in #{attempt}s..."
|
||||
sleep attempt
|
||||
attempt += 1
|
||||
|
||||
retry
|
||||
else
|
||||
raise
|
||||
end
|
||||
end
|
||||
rescue SSHKit::Command::Failed, HealthcheckError => e
|
||||
Mrsk::Utils::HealthcheckPoller.wait_for_healthy { capture_with_info(*MRSK.healthcheck.status) }
|
||||
rescue Mrsk::Utils::HealthcheckPoller::HealthcheckError => e
|
||||
error capture_with_info(*MRSK.healthcheck.logs)
|
||||
|
||||
if e.message =~ /curl/
|
||||
raise SSHKit::Command::Failed, "#{target} failed to return 200 OK!"
|
||||
else
|
||||
raise
|
||||
end
|
||||
error capture_with_pretty_json(*MRSK.healthcheck.container_health_log)
|
||||
raise
|
||||
ensure
|
||||
execute *MRSK.healthcheck.stop, raise_on_non_zero_exit: false
|
||||
execute *MRSK.healthcheck.remove, raise_on_non_zero_exit: false
|
||||
|
||||
37
lib/mrsk/cli/lock.rb
Normal file
37
lib/mrsk/cli/lock.rb
Normal file
@@ -0,0 +1,37 @@
|
||||
class Mrsk::Cli::Lock < Mrsk::Cli::Base
|
||||
desc "status", "Report lock status"
|
||||
def status
|
||||
handle_missing_lock do
|
||||
on(MRSK.primary_host) { puts capture_with_info(*MRSK.lock.status) }
|
||||
end
|
||||
end
|
||||
|
||||
desc "acquire", "Acquire the deploy lock"
|
||||
option :message, aliases: "-m", type: :string, desc: "A lock mesasge", required: true
|
||||
def acquire
|
||||
message = options[:message]
|
||||
handle_missing_lock do
|
||||
on(MRSK.primary_host) { execute *MRSK.lock.acquire(message, MRSK.config.version) }
|
||||
say "Acquired the deploy lock"
|
||||
end
|
||||
end
|
||||
|
||||
desc "release", "Release the deploy lock"
|
||||
def release
|
||||
handle_missing_lock do
|
||||
on(MRSK.primary_host) { execute *MRSK.lock.release }
|
||||
say "Released the deploy lock"
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def handle_missing_lock
|
||||
yield
|
||||
rescue SSHKit::Runner::ExecuteError => e
|
||||
if e.message =~ /No such file or directory/
|
||||
say "There is no deploy lock"
|
||||
else
|
||||
raise
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,77 +1,123 @@
|
||||
class Mrsk::Cli::Main < Mrsk::Cli::Base
|
||||
desc "setup", "Setup all accessories and deploy app to servers"
|
||||
def setup
|
||||
print_runtime do
|
||||
invoke "mrsk:cli:server:bootstrap"
|
||||
invoke "mrsk:cli:accessory:boot", [ "all" ]
|
||||
deploy
|
||||
with_lock do
|
||||
print_runtime do
|
||||
invoke "mrsk:cli:server:bootstrap"
|
||||
invoke "mrsk:cli:accessory:boot", [ "all" ]
|
||||
deploy
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
desc "deploy", "Deploy app to servers"
|
||||
option :skip_push, aliases: "-P", type: :boolean, default: false, desc: "Skip image build and push"
|
||||
def deploy
|
||||
runtime = print_runtime do
|
||||
say "Ensure curl and Docker are installed...", :magenta
|
||||
invoke "mrsk:cli:server:bootstrap"
|
||||
with_lock do
|
||||
invoke_options = deploy_options
|
||||
|
||||
say "Log into image registry...", :magenta
|
||||
invoke "mrsk:cli:registry:login"
|
||||
runtime = print_runtime do
|
||||
say "Log into image registry...", :magenta
|
||||
invoke "mrsk:cli:registry:login", [], invoke_options
|
||||
|
||||
say "Build and push app image...", :magenta
|
||||
invoke "mrsk:cli:build:deliver"
|
||||
if options[:skip_push]
|
||||
say "Pull app image...", :magenta
|
||||
invoke "mrsk:cli:build:pull", [], invoke_options
|
||||
else
|
||||
say "Build and push app image...", :magenta
|
||||
invoke "mrsk:cli:build:deliver", [], invoke_options
|
||||
end
|
||||
|
||||
say "Ensure Traefik is running...", :magenta
|
||||
invoke "mrsk:cli:traefik:boot"
|
||||
say "Ensure Traefik is running...", :magenta
|
||||
invoke "mrsk:cli:traefik:boot", [], invoke_options
|
||||
|
||||
say "Ensure app can pass healthcheck...", :magenta
|
||||
invoke "mrsk:cli:healthcheck:perform"
|
||||
say "Ensure app can pass healthcheck...", :magenta
|
||||
invoke "mrsk:cli:healthcheck:perform", [], invoke_options
|
||||
|
||||
invoke "mrsk:cli:app:boot"
|
||||
say "Detect stale containers...", :magenta
|
||||
invoke "mrsk:cli:app:stale_containers", [], invoke_options
|
||||
|
||||
say "Prune old containers and images...", :magenta
|
||||
invoke "mrsk:cli:prune:all"
|
||||
hold_lock_on_error do
|
||||
invoke "mrsk:cli:app:boot", [], invoke_options
|
||||
end
|
||||
|
||||
say "Prune old containers and images...", :magenta
|
||||
invoke "mrsk:cli:prune:all", [], invoke_options
|
||||
end
|
||||
|
||||
audit_broadcast "Deployed #{service_version} in #{runtime.round} seconds" unless options[:skip_broadcast]
|
||||
end
|
||||
|
||||
audit_broadcast "Deployed app in #{runtime.to_i} seconds" unless options[:skip_broadcast]
|
||||
end
|
||||
|
||||
desc "redeploy", "Deploy app to servers without bootstrapping servers, starting Traefik, pruning, and registry login"
|
||||
option :skip_push, aliases: "-P", type: :boolean, default: false, desc: "Skip image build and push"
|
||||
def redeploy
|
||||
runtime = print_runtime do
|
||||
say "Build and push app image...", :magenta
|
||||
invoke "mrsk:cli:build:deliver"
|
||||
with_lock do
|
||||
invoke_options = deploy_options
|
||||
|
||||
say "Ensure app can pass healthcheck...", :magenta
|
||||
invoke "mrsk:cli:healthcheck:perform"
|
||||
runtime = print_runtime do
|
||||
if options[:skip_push]
|
||||
say "Pull app image...", :magenta
|
||||
invoke "mrsk:cli:build:pull", [], invoke_options
|
||||
else
|
||||
say "Build and push app image...", :magenta
|
||||
invoke "mrsk:cli:build:deliver", [], invoke_options
|
||||
end
|
||||
|
||||
invoke "mrsk:cli:app:boot"
|
||||
say "Ensure app can pass healthcheck...", :magenta
|
||||
invoke "mrsk:cli:healthcheck:perform", [], invoke_options
|
||||
|
||||
say "Detect stale containers...", :magenta
|
||||
invoke "mrsk:cli:app:stale_containers", [], invoke_options
|
||||
|
||||
hold_lock_on_error do
|
||||
invoke "mrsk:cli:app:boot", [], invoke_options
|
||||
end
|
||||
end
|
||||
|
||||
audit_broadcast "Redeployed #{service_version} in #{runtime.round} seconds" unless options[:skip_broadcast]
|
||||
end
|
||||
|
||||
audit_broadcast "Redeployed app in #{runtime.to_i} seconds" unless options[:skip_broadcast]
|
||||
end
|
||||
|
||||
desc "rollback [VERSION]", "Rollback app to VERSION"
|
||||
def rollback(version)
|
||||
MRSK.version = version
|
||||
with_lock do
|
||||
invoke_options = deploy_options
|
||||
|
||||
if container_name_available?(MRSK.config.service_with_version)
|
||||
say "Start version #{version}, then wait #{MRSK.config.readiness_delay}s for app to boot before stopping the old version...", :magenta
|
||||
hold_lock_on_error do
|
||||
MRSK.config.version = version
|
||||
old_version = nil
|
||||
|
||||
cli = self
|
||||
if container_available?(version)
|
||||
say "Start version #{version}, then wait #{MRSK.config.readiness_delay}s for app to boot before stopping the old version...", :magenta
|
||||
|
||||
on(MRSK.hosts) do |host|
|
||||
old_version = capture_with_info(*MRSK.app.current_running_version).strip.presence
|
||||
on(MRSK.hosts) do
|
||||
execute *MRSK.auditor.record("Tagging #{MRSK.config.absolute_image} as the latest image"), verbosity: :debug
|
||||
execute *MRSK.app.tag_current_as_latest
|
||||
end
|
||||
|
||||
execute *MRSK.app.start
|
||||
on(MRSK.hosts) do |host|
|
||||
roles = MRSK.roles_on(host)
|
||||
|
||||
sleep MRSK.config.readiness_delay
|
||||
roles.each do |role|
|
||||
app = MRSK.app(role: role)
|
||||
old_version = capture_with_info(*app.current_running_version).strip.presence
|
||||
|
||||
execute *MRSK.app.stop(version: old_version), raise_on_non_zero_exit: false
|
||||
execute *app.start
|
||||
|
||||
if old_version
|
||||
sleep MRSK.config.readiness_delay
|
||||
|
||||
execute *app.stop(version: old_version), raise_on_non_zero_exit: false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
audit_broadcast "Rolled back #{service_version(Mrsk::Utils.abbreviate_version(old_version))} to #{service_version}" unless options[:skip_broadcast]
|
||||
else
|
||||
say "The app version '#{version}' is not available as a container (use 'mrsk app containers' for available versions)", :red
|
||||
end
|
||||
end
|
||||
|
||||
audit_broadcast "Rolled back app to version #{version}" unless options[:skip_broadcast]
|
||||
else
|
||||
say "The app version '#{version}' is not available as a container (use 'mrsk app containers' for available versions)", :red
|
||||
end
|
||||
end
|
||||
|
||||
@@ -92,7 +138,7 @@ class Mrsk::Cli::Main < Mrsk::Cli::Base
|
||||
desc "config", "Show combined config (including secrets!)"
|
||||
def config
|
||||
run_locally do
|
||||
puts MRSK.config.to_h.to_yaml
|
||||
puts Mrsk::Utils.redacted(MRSK.config.to_h).to_yaml
|
||||
end
|
||||
end
|
||||
|
||||
@@ -119,8 +165,10 @@ class Mrsk::Cli::Main < Mrsk::Cli::Base
|
||||
puts "Binstub already exists in bin/mrsk (remove first to create a new one)"
|
||||
else
|
||||
puts "Adding MRSK to Gemfile and bundle..."
|
||||
`bundle add mrsk`
|
||||
`bundle binstubs mrsk`
|
||||
run_locally do
|
||||
execute :bundle, :add, :mrsk
|
||||
execute :bundle, :binstubs, :mrsk
|
||||
end
|
||||
puts "Created binstub file in bin/mrsk"
|
||||
end
|
||||
end
|
||||
@@ -142,14 +190,23 @@ class Mrsk::Cli::Main < Mrsk::Cli::Base
|
||||
desc "remove", "Remove Traefik, app, accessories, and registry session from servers"
|
||||
option :confirmed, aliases: "-y", type: :boolean, default: false, desc: "Proceed without confirmation question"
|
||||
def remove
|
||||
if options[:confirmed] || ask("This will remove all containers and images. Are you sure?", limited_to: %w( y N ), default: "N") == "y"
|
||||
invoke "mrsk:cli:traefik:remove", [], options.without(:confirmed)
|
||||
invoke "mrsk:cli:app:remove", [], options.without(:confirmed)
|
||||
invoke "mrsk:cli:accessory:remove", [ "all" ], options
|
||||
invoke "mrsk:cli:registry:logout", [], options.without(:confirmed)
|
||||
with_lock do
|
||||
if options[:confirmed] || ask("This will remove all containers and images. Are you sure?", limited_to: %w( y N ), default: "N") == "y"
|
||||
invoke "mrsk:cli:traefik:remove", [], options.without(:confirmed)
|
||||
invoke "mrsk:cli:app:remove", [], options.without(:confirmed)
|
||||
invoke "mrsk:cli:accessory:remove", [ "all" ], options
|
||||
invoke "mrsk:cli:registry:logout", [], options.without(:confirmed)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
desc "broadcast", "Broadcast an audit message"
|
||||
option :message, aliases: "-m", type: :string, desc: "Audit mesasge", required: true
|
||||
def broadcast
|
||||
say "Broadcast: #{options[:message]}", :magenta
|
||||
audit_broadcast options[:message]
|
||||
end
|
||||
|
||||
desc "version", "Show MRSK version"
|
||||
def version
|
||||
puts Mrsk::VERSION
|
||||
@@ -179,10 +236,35 @@ class Mrsk::Cli::Main < Mrsk::Cli::Base
|
||||
desc "traefik", "Manage Traefik load balancer"
|
||||
subcommand "traefik", Mrsk::Cli::Traefik
|
||||
|
||||
desc "lock", "Manage the deploy lock"
|
||||
subcommand "lock", Mrsk::Cli::Lock
|
||||
|
||||
private
|
||||
def container_name_available?(container_name, host: MRSK.primary_host)
|
||||
container_names = nil
|
||||
on(host) { container_names = capture_with_info(*MRSK.app.list_container_names).split("\n") }
|
||||
Array(container_names).include?(container_name)
|
||||
def container_available?(version)
|
||||
begin
|
||||
on(MRSK.hosts) do
|
||||
MRSK.roles_on(host).each do |role|
|
||||
container_id = capture_with_info(*MRSK.app(role: role).container_id_for_version(version))
|
||||
raise "Container not found" unless container_id.present?
|
||||
end
|
||||
end
|
||||
rescue SSHKit::Runner::ExecuteError => e
|
||||
if e.message =~ /Container not found/
|
||||
say "Error looking for container version #{version}: #{e.message}"
|
||||
return false
|
||||
else
|
||||
raise
|
||||
end
|
||||
end
|
||||
|
||||
true
|
||||
end
|
||||
|
||||
def deploy_options
|
||||
{ "version" => MRSK.config.version }.merge(options.without("skip_push"))
|
||||
end
|
||||
|
||||
def service_version(version = MRSK.config.abbreviated_version)
|
||||
[ MRSK.config.service, version ].compact.join("@")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,23 +1,29 @@
|
||||
class Mrsk::Cli::Prune < Mrsk::Cli::Base
|
||||
desc "all", "Prune unused images and stopped containers"
|
||||
def all
|
||||
containers
|
||||
images
|
||||
end
|
||||
|
||||
desc "images", "Prune unused images older than 7 days"
|
||||
def images
|
||||
on(MRSK.hosts) do
|
||||
execute *MRSK.auditor.record("Pruned images"), verbosity: :debug
|
||||
execute *MRSK.prune.images
|
||||
with_lock do
|
||||
containers
|
||||
images
|
||||
end
|
||||
end
|
||||
|
||||
desc "containers", "Prune stopped containers older than 3 days"
|
||||
desc "images", "Prune dangling images"
|
||||
def images
|
||||
with_lock do
|
||||
on(MRSK.hosts) do
|
||||
execute *MRSK.auditor.record("Pruned images"), verbosity: :debug
|
||||
execute *MRSK.prune.images
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
desc "containers", "Prune all stopped containers, except the last 5"
|
||||
def containers
|
||||
on(MRSK.hosts) do
|
||||
execute *MRSK.auditor.record("Pruned containers"), verbosity: :debug
|
||||
execute *MRSK.prune.containers
|
||||
with_lock do
|
||||
on(MRSK.hosts) do
|
||||
execute *MRSK.auditor.record("Pruned containers"), verbosity: :debug
|
||||
execute *MRSK.prune.containers
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,15 +1,21 @@
|
||||
class Mrsk::Cli::Server < Mrsk::Cli::Base
|
||||
desc "bootstrap", "Ensure curl and Docker are installed on servers"
|
||||
desc "bootstrap", "Set up Docker to run MRSK apps"
|
||||
def bootstrap
|
||||
on(MRSK.hosts + MRSK.accessory_hosts) do
|
||||
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
|
||||
missing = []
|
||||
|
||||
if dependencies_to_install.any?
|
||||
execute "apt-get update -y && apt-get install #{dependencies_to_install.join(" ")} -y"
|
||||
on(MRSK.hosts | MRSK.accessory_hosts) do |host|
|
||||
unless execute(*MRSK.docker.installed?, raise_on_non_zero_exit: false)
|
||||
if execute(*MRSK.docker.superuser?, raise_on_non_zero_exit: false)
|
||||
info "Missing Docker on #{host}. Installing…"
|
||||
execute *MRSK.docker.install
|
||||
else
|
||||
missing << host
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if missing.any?
|
||||
raise "Docker is not installed on #{missing.join(", ")} and can't be automatically installed without having root access and the `curl` command available. Install Docker manually: https://docs.docker.com/engine/install/"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -13,6 +13,8 @@ registry:
|
||||
# Specify the registry server, if you're not using Docker Hub
|
||||
# server: registry.digitalocean.com / ghcr.io / ...
|
||||
username: my-user
|
||||
|
||||
# Always use an access token rather than real password when possible.
|
||||
password:
|
||||
- MRSK_REGISTRY_PASSWORD
|
||||
|
||||
|
||||
@@ -1,36 +1,49 @@
|
||||
class Mrsk::Cli::Traefik < Mrsk::Cli::Base
|
||||
desc "boot", "Boot Traefik on servers"
|
||||
def boot
|
||||
on(MRSK.traefik_hosts) { execute *MRSK.traefik.run, raise_on_non_zero_exit: false }
|
||||
with_lock do
|
||||
on(MRSK.traefik_hosts) do
|
||||
execute *MRSK.registry.login
|
||||
execute *MRSK.traefik.run, raise_on_non_zero_exit: false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
desc "reboot", "Reboot Traefik on servers (stop container, remove container, start new container)"
|
||||
def reboot
|
||||
stop
|
||||
remove_container
|
||||
boot
|
||||
with_lock do
|
||||
stop
|
||||
remove_container
|
||||
boot
|
||||
end
|
||||
end
|
||||
|
||||
desc "start", "Start existing Traefik container on servers"
|
||||
def start
|
||||
on(MRSK.traefik_hosts) do
|
||||
execute *MRSK.auditor.record("Started traefik"), verbosity: :debug
|
||||
execute *MRSK.traefik.start, raise_on_non_zero_exit: false
|
||||
with_lock do
|
||||
on(MRSK.traefik_hosts) do
|
||||
execute *MRSK.auditor.record("Started traefik"), verbosity: :debug
|
||||
execute *MRSK.traefik.start, raise_on_non_zero_exit: false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
desc "stop", "Stop existing Traefik container on servers"
|
||||
def stop
|
||||
on(MRSK.traefik_hosts) do
|
||||
execute *MRSK.auditor.record("Stopped traefik"), verbosity: :debug
|
||||
execute *MRSK.traefik.stop, raise_on_non_zero_exit: false
|
||||
with_lock do
|
||||
on(MRSK.traefik_hosts) do
|
||||
execute *MRSK.auditor.record("Stopped traefik"), verbosity: :debug
|
||||
execute *MRSK.traefik.stop, raise_on_non_zero_exit: false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
desc "restart", "Restart existing Traefik container on servers"
|
||||
def restart
|
||||
stop
|
||||
start
|
||||
with_lock do
|
||||
stop
|
||||
start
|
||||
end
|
||||
end
|
||||
|
||||
desc "details", "Show details about Traefik container from servers"
|
||||
@@ -64,24 +77,30 @@ class Mrsk::Cli::Traefik < Mrsk::Cli::Base
|
||||
|
||||
desc "remove", "Remove Traefik container and image from servers"
|
||||
def remove
|
||||
stop
|
||||
remove_container
|
||||
remove_image
|
||||
with_lock do
|
||||
stop
|
||||
remove_container
|
||||
remove_image
|
||||
end
|
||||
end
|
||||
|
||||
desc "remove_container", "Remove Traefik container from servers", hide: true
|
||||
def remove_container
|
||||
on(MRSK.traefik_hosts) do
|
||||
execute *MRSK.auditor.record("Removed traefik container"), verbosity: :debug
|
||||
execute *MRSK.traefik.remove_container
|
||||
with_lock do
|
||||
on(MRSK.traefik_hosts) do
|
||||
execute *MRSK.auditor.record("Removed traefik container"), verbosity: :debug
|
||||
execute *MRSK.traefik.remove_container
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
desc "remove_container", "Remove Traefik image from servers", hide: true
|
||||
desc "remove_image", "Remove Traefik image from servers", hide: true
|
||||
def remove_image
|
||||
on(MRSK.traefik_hosts) do
|
||||
execute *MRSK.auditor.record("Removed traefik image"), verbosity: :debug
|
||||
execute *MRSK.traefik.remove_image
|
||||
with_lock do
|
||||
on(MRSK.traefik_hosts) do
|
||||
execute *MRSK.auditor.record("Removed traefik image"), verbosity: :debug
|
||||
execute *MRSK.traefik.remove_image
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,35 +1,66 @@
|
||||
require "active_support/core_ext/enumerable"
|
||||
require "active_support/core_ext/module/delegation"
|
||||
|
||||
class Mrsk::Commander
|
||||
attr_accessor :config_file, :destination, :verbosity, :version
|
||||
attr_accessor :verbosity, :holding_lock, :hold_lock_on_error
|
||||
|
||||
def initialize(config_file: nil, destination: nil, verbosity: :info)
|
||||
@config_file, @destination, @verbosity = config_file, destination, verbosity
|
||||
def initialize
|
||||
self.verbosity = :info
|
||||
self.holding_lock = false
|
||||
self.hold_lock_on_error = false
|
||||
end
|
||||
|
||||
def config
|
||||
@config ||= \
|
||||
Mrsk::Configuration
|
||||
.create_from(config_file, destination: destination, version: cascading_version)
|
||||
.tap { |config| configure_sshkit_with(config) }
|
||||
@config ||= Mrsk::Configuration.create_from(**@config_kwargs).tap do |config|
|
||||
@config_kwargs = nil
|
||||
configure_sshkit_with(config)
|
||||
end
|
||||
end
|
||||
|
||||
attr_accessor :specific_hosts
|
||||
def configure(**kwargs)
|
||||
@config, @config_kwargs = nil, kwargs
|
||||
end
|
||||
|
||||
attr_reader :specific_roles, :specific_hosts
|
||||
|
||||
def specific_primary!
|
||||
self.specific_hosts = [ config.primary_web_host ]
|
||||
end
|
||||
|
||||
def specific_roles=(role_names)
|
||||
self.specific_hosts = config.roles.select { |r| role_names.include?(r.name) }.flat_map(&:hosts) if role_names.present?
|
||||
@specific_roles = config.roles.select { |r| role_names.include?(r.name) } if role_names.present?
|
||||
end
|
||||
|
||||
def specific_hosts=(hosts)
|
||||
@specific_hosts = config.all_hosts & hosts if hosts.present?
|
||||
end
|
||||
|
||||
def primary_host
|
||||
specific_hosts&.first || config.primary_web_host
|
||||
specific_hosts&.first || specific_roles&.first&.primary_host || config.primary_web_host
|
||||
end
|
||||
|
||||
def roles
|
||||
(specific_roles || config.roles).select do |role|
|
||||
((specific_hosts || config.all_hosts) & role.hosts).any?
|
||||
end
|
||||
end
|
||||
|
||||
def hosts
|
||||
specific_hosts || config.all_hosts
|
||||
(specific_hosts || config.all_hosts).select do |host|
|
||||
(specific_roles || config.roles).flat_map(&:hosts).include?(host)
|
||||
end
|
||||
end
|
||||
|
||||
def boot_strategy
|
||||
if config.boot.limit.present?
|
||||
{ in: :groups, limit: config.boot.limit, wait: config.boot.wait }
|
||||
else
|
||||
{}
|
||||
end
|
||||
end
|
||||
|
||||
def roles_on(host)
|
||||
roles.select { |role| role.hosts.include?(host.to_s) }.map(&:name)
|
||||
end
|
||||
|
||||
def traefik_hosts
|
||||
@@ -37,7 +68,7 @@ class Mrsk::Commander
|
||||
end
|
||||
|
||||
def accessory_hosts
|
||||
specific_hosts || config.accessories.collect(&:host)
|
||||
specific_hosts || config.accessories.flat_map(&:hosts)
|
||||
end
|
||||
|
||||
def accessory_names
|
||||
@@ -45,22 +76,26 @@ class Mrsk::Commander
|
||||
end
|
||||
|
||||
|
||||
def app
|
||||
@app ||= Mrsk::Commands::App.new(config)
|
||||
def app(role: nil)
|
||||
Mrsk::Commands::App.new(config, role: role)
|
||||
end
|
||||
|
||||
def accessory(name)
|
||||
Mrsk::Commands::Accessory.new(config, name: name)
|
||||
end
|
||||
|
||||
def auditor
|
||||
@auditor ||= Mrsk::Commands::Auditor.new(config)
|
||||
def auditor(**details)
|
||||
Mrsk::Commands::Auditor.new(config, **details)
|
||||
end
|
||||
|
||||
def builder
|
||||
@builder ||= Mrsk::Commands::Builder.new(config)
|
||||
end
|
||||
|
||||
def docker
|
||||
@docker ||= Mrsk::Commands::Docker.new(config)
|
||||
end
|
||||
|
||||
def healthcheck
|
||||
@healthcheck ||= Mrsk::Commands::Healthcheck.new(config)
|
||||
end
|
||||
@@ -77,6 +112,9 @@ class Mrsk::Commander
|
||||
@traefik ||= Mrsk::Commands::Traefik.new(config)
|
||||
end
|
||||
|
||||
def lock
|
||||
@lock ||= Mrsk::Commands::Lock.new(config)
|
||||
end
|
||||
|
||||
def with_verbosity(level)
|
||||
old_level = self.verbosity
|
||||
@@ -90,26 +128,15 @@ class Mrsk::Commander
|
||||
SSHKit.config.output_verbosity = old_level
|
||||
end
|
||||
|
||||
# Test-induced damage!
|
||||
def reset
|
||||
@config = @config_file = @destination = @version = nil
|
||||
@app = @builder = @traefik = @registry = @prune = @auditor = nil
|
||||
@verbosity = :info
|
||||
def holding_lock?
|
||||
self.holding_lock
|
||||
end
|
||||
|
||||
def hold_lock_on_error?
|
||||
self.hold_lock_on_error
|
||||
end
|
||||
|
||||
private
|
||||
def cascading_version
|
||||
version.presence || ENV["VERSION"] || current_commit_hash
|
||||
end
|
||||
|
||||
def current_commit_hash
|
||||
if system("git rev-parse")
|
||||
`git rev-parse HEAD`.strip
|
||||
else
|
||||
raise "Can't use commit hash as version, no git repository found in #{Dir.pwd}"
|
||||
end
|
||||
end
|
||||
|
||||
# Lazy setup of SSHKit
|
||||
def configure_sshkit_with(config)
|
||||
SSHKit::Backend::Netssh.configure { |ssh| ssh.ssh_options = config.ssh_options }
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
class Mrsk::Commands::Accessory < Mrsk::Commands::Base
|
||||
attr_reader :accessory_config
|
||||
delegate :service_name, :image, :host, :port, :files, :directories, :env_args, :volume_args, :label_args, to: :accessory_config
|
||||
delegate :service_name, :image, :hosts, :port, :files, :directories, :cmd,
|
||||
:publish_args, :env_args, :volume_args, :label_args, :option_args, to: :accessory_config
|
||||
|
||||
def initialize(config, name:)
|
||||
super(config)
|
||||
@@ -12,12 +13,14 @@ class Mrsk::Commands::Accessory < Mrsk::Commands::Base
|
||||
"--name", service_name,
|
||||
"--detach",
|
||||
"--restart", "unless-stopped",
|
||||
"--log-opt", "max-size=#{MAX_LOG_SIZE}",
|
||||
"--publish", port,
|
||||
*config.logging_args,
|
||||
*publish_args,
|
||||
*env_args,
|
||||
*volume_args,
|
||||
*label_args,
|
||||
image
|
||||
*option_args,
|
||||
image,
|
||||
cmd
|
||||
end
|
||||
|
||||
def start
|
||||
@@ -73,7 +76,7 @@ class Mrsk::Commands::Accessory < Mrsk::Commands::Base
|
||||
end
|
||||
|
||||
def run_over_ssh(command)
|
||||
super command, host: host
|
||||
super command, host: hosts.first
|
||||
end
|
||||
|
||||
|
||||
@@ -100,7 +103,7 @@ class Mrsk::Commands::Accessory < Mrsk::Commands::Base
|
||||
end
|
||||
|
||||
def remove_image
|
||||
docker :image, :prune, "--all", "--force", *service_filter
|
||||
docker :image, :rm, "--force", image
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
@@ -1,13 +1,22 @@
|
||||
class Mrsk::Commands::App < Mrsk::Commands::Base
|
||||
def run(role: :web)
|
||||
role = config.role(role)
|
||||
attr_reader :role
|
||||
|
||||
def initialize(config, role: nil)
|
||||
super(config)
|
||||
@role = role
|
||||
end
|
||||
|
||||
def run
|
||||
role = config.role(self.role)
|
||||
|
||||
docker :run,
|
||||
"--detach",
|
||||
"--restart unless-stopped",
|
||||
"--log-opt", "max-size=#{MAX_LOG_SIZE}",
|
||||
"--name", service_with_version,
|
||||
"--name", container_name,
|
||||
"-e", "MRSK_CONTAINER_NAME=\"#{container_name}\"",
|
||||
*role.env_args,
|
||||
*role.health_check_args,
|
||||
*config.logging_args,
|
||||
*config.volume_args,
|
||||
*role.label_args,
|
||||
*role.option_args,
|
||||
@@ -16,23 +25,27 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
|
||||
end
|
||||
|
||||
def start
|
||||
docker :start, service_with_version
|
||||
docker :start, container_name
|
||||
end
|
||||
|
||||
def status(version:)
|
||||
pipe container_id_for_version(version), xargs(docker(:inspect, "--format", DOCKER_HEALTH_STATUS_FORMAT))
|
||||
end
|
||||
|
||||
def stop(version: nil)
|
||||
pipe \
|
||||
version ? container_id_for_version(version) : current_container_id,
|
||||
xargs(docker(:stop))
|
||||
version ? container_id_for_version(version) : current_running_container_id,
|
||||
xargs(config.stop_wait_time ? docker(:stop, "-t", config.stop_wait_time) : docker(:stop))
|
||||
end
|
||||
|
||||
def info
|
||||
docker :ps, *service_filter
|
||||
docker :ps, *filter_args
|
||||
end
|
||||
|
||||
|
||||
def logs(since: nil, lines: nil, grep: nil)
|
||||
pipe \
|
||||
current_container_id,
|
||||
current_running_container_id,
|
||||
"xargs docker logs#{" --since #{since}" if since}#{" --tail #{lines}" if lines} 2>&1",
|
||||
("grep '#{grep}'" if grep)
|
||||
end
|
||||
@@ -40,7 +53,7 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
|
||||
def follow_logs(host:, grep: nil)
|
||||
run_over_ssh \
|
||||
pipe(
|
||||
current_container_id,
|
||||
current_running_container_id,
|
||||
"xargs docker logs --timestamps --tail 10 --follow 2>&1",
|
||||
(%(grep "#{grep}") if grep)
|
||||
),
|
||||
@@ -51,7 +64,7 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
|
||||
def execute_in_existing_container(*command, interactive: false)
|
||||
docker :exec,
|
||||
("-it" if interactive),
|
||||
config.service_with_version,
|
||||
container_name,
|
||||
*command
|
||||
end
|
||||
|
||||
@@ -74,33 +87,27 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
|
||||
end
|
||||
|
||||
|
||||
def current_container_id
|
||||
docker :ps, "--quiet", *service_filter
|
||||
def current_running_container_id
|
||||
docker :ps, "--quiet", *filter_args(status: :running), "--latest"
|
||||
end
|
||||
|
||||
def container_id_for_version(version)
|
||||
container_id_for(container_name: container_name(version))
|
||||
end
|
||||
|
||||
def current_running_version
|
||||
# FIXME: Find more graceful way to extract the version from "app-version" than using sed and tail!
|
||||
pipe \
|
||||
docker(:ps, "--filter", "label=service=#{config.service}", "--format", '"{{.Names}}"'),
|
||||
%(sed 's/-/\\n/g'),
|
||||
"tail -n 1"
|
||||
list_versions("--latest", status: :running)
|
||||
end
|
||||
|
||||
def most_recent_version_from_available_images
|
||||
def list_versions(*docker_args, status: nil)
|
||||
pipe \
|
||||
docker(:image, :ls, "--format", '"{{.Tag}}"', config.repository),
|
||||
"head -n 1"
|
||||
docker(:ps, *filter_args(status: status), *docker_args, "--format", '"{{.Names}}"'),
|
||||
%(grep -oE "\\-[^-]+$"), # Extract SHA from "service-role-dest-SHA"
|
||||
%(cut -c 2-)
|
||||
end
|
||||
|
||||
def all_versions_from_available_containers
|
||||
pipe \
|
||||
docker(:image, :ls, "--format", '"{{.Tag}}"', config.repository),
|
||||
"head -n 1"
|
||||
end
|
||||
|
||||
|
||||
def list_containers
|
||||
docker :container, :ls, "--all", *service_filter
|
||||
docker :container, :ls, "--all", *filter_args
|
||||
end
|
||||
|
||||
def list_container_names
|
||||
@@ -109,12 +116,16 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
|
||||
|
||||
def remove_container(version:)
|
||||
pipe \
|
||||
container_id_for(container_name: service_with_version(version)),
|
||||
container_id_for(container_name: container_name(version)),
|
||||
xargs(docker(:container, :rm))
|
||||
end
|
||||
|
||||
def rename_container(version:, new_version:)
|
||||
docker :rename, container_name(version), container_name(new_version)
|
||||
end
|
||||
|
||||
def remove_containers
|
||||
docker :container, :prune, "--force", *service_filter
|
||||
docker :container, :prune, "--force", *filter_args
|
||||
end
|
||||
|
||||
def list_images
|
||||
@@ -122,24 +133,28 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
|
||||
end
|
||||
|
||||
def remove_images
|
||||
docker :image, :prune, "--all", "--force", *service_filter
|
||||
docker :image, :prune, "--all", "--force", *filter_args
|
||||
end
|
||||
|
||||
def tag_current_as_latest
|
||||
docker :tag, config.absolute_image, config.latest_image
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
def service_with_version(version = nil)
|
||||
if version
|
||||
"#{config.service}-#{version}"
|
||||
else
|
||||
config.service_with_version
|
||||
def container_name(version = nil)
|
||||
[ config.service, role, config.destination, version || config.version ].compact.join("-")
|
||||
end
|
||||
|
||||
def filter_args(status: nil)
|
||||
argumentize "--filter", filters(status: status)
|
||||
end
|
||||
|
||||
def filters(status: nil)
|
||||
[ "label=service=#{config.service}" ].tap do |filters|
|
||||
filters << "label=destination=#{config.destination}" if config.destination
|
||||
filters << "label=role=#{role}" if role
|
||||
filters << "status=#{status}" if status
|
||||
end
|
||||
end
|
||||
|
||||
def container_id_for_version(version)
|
||||
container_id_for(container_name: service_with_version(version))
|
||||
end
|
||||
|
||||
def service_filter
|
||||
[ "--filter", "label=service=#{config.service}" ]
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,17 +1,24 @@
|
||||
require "active_support/core_ext/time/conversions"
|
||||
require "time"
|
||||
|
||||
class Mrsk::Commands::Auditor < Mrsk::Commands::Base
|
||||
attr_reader :details
|
||||
|
||||
def initialize(config, **details)
|
||||
super(config)
|
||||
@details = default_details.merge(details)
|
||||
end
|
||||
|
||||
# Runs remotely
|
||||
def record(line)
|
||||
def record(line, **details)
|
||||
append \
|
||||
[ :echo, tagged_record_line(line) ],
|
||||
[ :echo, *audit_tags(**details), line ],
|
||||
audit_log_file
|
||||
end
|
||||
|
||||
# Runs locally
|
||||
def broadcast(line)
|
||||
def broadcast(line, **details)
|
||||
if broadcast_cmd = config.audit_broadcast_cmd
|
||||
[ broadcast_cmd, tagged_broadcast_line(line) ]
|
||||
[ broadcast_cmd, *broadcast_args(line, **details), env: env_for(event: line, **details) ]
|
||||
end
|
||||
end
|
||||
|
||||
@@ -21,22 +28,32 @@ class Mrsk::Commands::Auditor < Mrsk::Commands::Base
|
||||
|
||||
private
|
||||
def audit_log_file
|
||||
"mrsk-#{config.service}-audit.log"
|
||||
[ "mrsk", config.service, config.destination, "audit.log" ].compact.join("-")
|
||||
end
|
||||
|
||||
def tagged_record_line(line)
|
||||
"'#{recorded_at_tag} #{performer_tag} #{line}'"
|
||||
def default_details
|
||||
{ recorded_at: Time.now.utc.iso8601,
|
||||
performer: `whoami`.chomp,
|
||||
destination: config.destination }
|
||||
end
|
||||
|
||||
def tagged_broadcast_line(line)
|
||||
"'#{performer_tag} #{line}'"
|
||||
def audit_tags(**details)
|
||||
tags_for **self.details.merge(details)
|
||||
end
|
||||
|
||||
def performer_tag
|
||||
"[#{`whoami`.strip}]"
|
||||
def broadcast_args(line, **details)
|
||||
"'#{broadcast_tags(**details).join(" ")} #{line}'"
|
||||
end
|
||||
|
||||
def recorded_at_tag
|
||||
"[#{Time.now.to_fs(:db)}]"
|
||||
def broadcast_tags(**details)
|
||||
tags_for **self.details.merge(details).except(:recorded_at)
|
||||
end
|
||||
|
||||
def tags_for(**details)
|
||||
details.compact.values.map { |value| "[#{value}]" }
|
||||
end
|
||||
|
||||
def env_for(**details)
|
||||
self.details.merge(details).compact.transform_keys { |detail| "MRSK_#{detail.upcase}" }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
module Mrsk::Commands
|
||||
class Base
|
||||
delegate :redact, to: Mrsk::Utils
|
||||
delegate :sensitive, :argumentize, to: Mrsk::Utils
|
||||
|
||||
MAX_LOG_SIZE = "10m"
|
||||
DOCKER_HEALTH_STATUS_FORMAT = "'{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}'"
|
||||
DOCKER_HEALTH_LOG_FORMAT = "'{{json .State.Health}}'"
|
||||
|
||||
attr_accessor :config
|
||||
|
||||
@@ -18,7 +19,7 @@ module Mrsk::Commands
|
||||
end
|
||||
|
||||
def container_id_for(container_name:)
|
||||
docker :container, :ls, "--all", "--filter", "name=#{container_name}", "--quiet"
|
||||
docker :container, :ls, "--all", "--filter", "name=^#{container_name}$", "--quiet"
|
||||
end
|
||||
|
||||
private
|
||||
@@ -41,6 +42,10 @@ module Mrsk::Commands
|
||||
combine *commands, by: ">>"
|
||||
end
|
||||
|
||||
def write(*commands)
|
||||
combine *commands, by: ">"
|
||||
end
|
||||
|
||||
def xargs(command)
|
||||
[ :xargs, command ].flatten
|
||||
end
|
||||
|
||||
@@ -2,7 +2,7 @@ class Mrsk::Commands::Builder < Mrsk::Commands::Base
|
||||
delegate :create, :remove, :push, :clean, :pull, :info, to: :target
|
||||
|
||||
def name
|
||||
target.class.to_s.remove("Mrsk::Commands::Builder::").underscore
|
||||
target.class.to_s.remove("Mrsk::Commands::Builder::").underscore.inquiry
|
||||
end
|
||||
|
||||
def target
|
||||
@@ -33,4 +33,24 @@ class Mrsk::Commands::Builder < Mrsk::Commands::Base
|
||||
def multiarch_remote
|
||||
@multiarch_remote ||= Mrsk::Commands::Builder::Multiarch::Remote.new(config)
|
||||
end
|
||||
|
||||
|
||||
def ensure_local_dependencies_installed
|
||||
if name.native?
|
||||
ensure_local_docker_installed
|
||||
else
|
||||
combine \
|
||||
ensure_local_docker_installed,
|
||||
ensure_local_buildx_installed
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def ensure_local_docker_installed
|
||||
docker "--version"
|
||||
end
|
||||
|
||||
def ensure_local_buildx_installed
|
||||
docker :buildx, "version"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
|
||||
class Mrsk::Commands::Builder::Base < Mrsk::Commands::Base
|
||||
class BuilderError < StandardError; end
|
||||
|
||||
delegate :argumentize, to: Mrsk::Utils
|
||||
|
||||
def clean
|
||||
@@ -17,6 +20,7 @@ class Mrsk::Commands::Builder::Base < Mrsk::Commands::Base
|
||||
context
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
def build_tags
|
||||
[ "-t", config.absolute_image, "-t", config.latest_image ]
|
||||
@@ -27,7 +31,7 @@ class Mrsk::Commands::Builder::Base < Mrsk::Commands::Base
|
||||
end
|
||||
|
||||
def build_args
|
||||
argumentize "--build-arg", args, redacted: true
|
||||
argumentize "--build-arg", args, sensitive: true
|
||||
end
|
||||
|
||||
def build_secrets
|
||||
@@ -35,7 +39,11 @@ class Mrsk::Commands::Builder::Base < Mrsk::Commands::Base
|
||||
end
|
||||
|
||||
def build_dockerfile
|
||||
argumentize "--file", dockerfile
|
||||
if Pathname.new(File.expand_path(dockerfile)).exist?
|
||||
argumentize "--file", dockerfile
|
||||
else
|
||||
raise BuilderError, "Missing #{dockerfile}"
|
||||
end
|
||||
end
|
||||
|
||||
def args
|
||||
|
||||
@@ -10,7 +10,8 @@ class Mrsk::Commands::Builder::Native < Mrsk::Commands::Builder::Base
|
||||
def push
|
||||
combine \
|
||||
docker(:build, *build_options, build_context),
|
||||
docker(:push, config.absolute_image)
|
||||
docker(:push, config.absolute_image),
|
||||
docker(:push, config.latest_image)
|
||||
end
|
||||
|
||||
def info
|
||||
|
||||
21
lib/mrsk/commands/docker.rb
Normal file
21
lib/mrsk/commands/docker.rb
Normal file
@@ -0,0 +1,21 @@
|
||||
class Mrsk::Commands::Docker < Mrsk::Commands::Base
|
||||
# Install Docker using the https://github.com/docker/docker-install convenience script.
|
||||
def install
|
||||
pipe [ :curl, "-fsSL", "https://get.docker.com" ], :sh
|
||||
end
|
||||
|
||||
# Checks the Docker client version. Fails if Docker is not installed.
|
||||
def installed?
|
||||
docker "-v"
|
||||
end
|
||||
|
||||
# Checks the Docker server version. Fails if Docker is not running.
|
||||
def running?
|
||||
docker :version
|
||||
end
|
||||
|
||||
# Do we have superuser access to install Docker and start system services?
|
||||
def superuser?
|
||||
[ '[ "${EUID:-$(id -u)}" -eq 0 ]' ]
|
||||
end
|
||||
end
|
||||
@@ -9,14 +9,21 @@ class Mrsk::Commands::Healthcheck < Mrsk::Commands::Base
|
||||
"--name", container_name_with_version,
|
||||
"--publish", "#{EXPOSED_PORT}:#{config.healthcheck["port"]}",
|
||||
"--label", "service=#{container_name}",
|
||||
"-e", "MRSK_CONTAINER_NAME=\"#{container_name}\"",
|
||||
*web.env_args,
|
||||
*web.health_check_args,
|
||||
*config.volume_args,
|
||||
*web.option_args,
|
||||
config.absolute_image,
|
||||
web.cmd
|
||||
end
|
||||
|
||||
def curl
|
||||
[ :curl, "--silent", "--output", "/dev/null", "--write-out", "'%{http_code}'", "--max-time", "2", health_url ]
|
||||
def status
|
||||
pipe container_id, xargs(docker(:inspect, "--format", DOCKER_HEALTH_STATUS_FORMAT))
|
||||
end
|
||||
|
||||
def container_health_log
|
||||
pipe container_id, xargs(docker(:inspect, "--format", DOCKER_HEALTH_LOG_FORMAT))
|
||||
end
|
||||
|
||||
def logs
|
||||
@@ -33,15 +40,15 @@ class Mrsk::Commands::Healthcheck < Mrsk::Commands::Base
|
||||
|
||||
private
|
||||
def container_name
|
||||
"healthcheck-#{config.service}"
|
||||
[ "healthcheck", config.service, config.destination ].compact.join("-")
|
||||
end
|
||||
|
||||
def container_name_with_version
|
||||
"healthcheck-#{config.service_with_version}"
|
||||
"#{container_name}-#{config.version}"
|
||||
end
|
||||
|
||||
def container_id
|
||||
container_id_for(container_name: container_name)
|
||||
container_id_for(container_name: container_name_with_version)
|
||||
end
|
||||
|
||||
def health_url
|
||||
|
||||
63
lib/mrsk/commands/lock.rb
Normal file
63
lib/mrsk/commands/lock.rb
Normal file
@@ -0,0 +1,63 @@
|
||||
require "active_support/duration"
|
||||
require "time"
|
||||
|
||||
class Mrsk::Commands::Lock < Mrsk::Commands::Base
|
||||
def acquire(message, version)
|
||||
combine \
|
||||
[:mkdir, lock_dir],
|
||||
write_lock_details(message, version)
|
||||
end
|
||||
|
||||
def release
|
||||
combine \
|
||||
[:rm, lock_details_file],
|
||||
[:rm, "-r", lock_dir]
|
||||
end
|
||||
|
||||
def status
|
||||
combine \
|
||||
stat_lock_dir,
|
||||
read_lock_details
|
||||
end
|
||||
|
||||
private
|
||||
def write_lock_details(message, version)
|
||||
write \
|
||||
[:echo, "\"#{Base64.encode64(lock_details(message, version))}\""],
|
||||
lock_details_file
|
||||
end
|
||||
|
||||
def read_lock_details
|
||||
pipe \
|
||||
[:cat, lock_details_file],
|
||||
[:base64, "-d"]
|
||||
end
|
||||
|
||||
def stat_lock_dir
|
||||
write \
|
||||
[:stat, lock_dir],
|
||||
"/dev/null"
|
||||
end
|
||||
|
||||
def lock_dir
|
||||
:mrsk_lock
|
||||
end
|
||||
|
||||
def lock_details_file
|
||||
[lock_dir, :details].join("/")
|
||||
end
|
||||
|
||||
def lock_details(message, version)
|
||||
<<~DETAILS.strip
|
||||
Locked by: #{locked_by} at #{Time.now.utc.iso8601}
|
||||
Version: #{version}
|
||||
Message: #{message}
|
||||
DETAILS
|
||||
end
|
||||
|
||||
def locked_by
|
||||
`git config user.name`.strip
|
||||
rescue Errno::ENOENT
|
||||
"Unknown"
|
||||
end
|
||||
end
|
||||
@@ -2,11 +2,19 @@ require "active_support/duration"
|
||||
require "active_support/core_ext/numeric/time"
|
||||
|
||||
class Mrsk::Commands::Prune < Mrsk::Commands::Base
|
||||
def images(until_hours: 7.days.in_hours.to_i)
|
||||
docker :image, :prune, "--all", "--force", "--filter", "label=service=#{config.service}", "--filter", "until=#{until_hours}h"
|
||||
def images
|
||||
docker :image, :prune, "--force", "--filter", "label=service=#{config.service}", "--filter", "dangling=true"
|
||||
end
|
||||
|
||||
def containers(until_hours: 3.days.in_hours.to_i)
|
||||
docker :container, :prune, "--force", "--filter", "label=service=#{config.service}", "--filter", "until=#{until_hours}h"
|
||||
def containers(keep_last: 5)
|
||||
pipe \
|
||||
docker(:ps, "-q", "-a", "--filter", "label=service=#{config.service}", *stopped_containers_filters),
|
||||
"tail -n +#{keep_last + 1}",
|
||||
"while read container_id; do docker rm $container_id; done"
|
||||
end
|
||||
|
||||
private
|
||||
def stopped_containers_filters
|
||||
[ "created", "exited", "dead" ].flat_map { |status| ["--filter", "status=#{status}"] }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,7 +2,7 @@ class Mrsk::Commands::Registry < Mrsk::Commands::Base
|
||||
delegate :registry, to: :config
|
||||
|
||||
def login
|
||||
docker :login, registry["server"], "-u", redact(lookup("username")), "-p", redact(lookup("password"))
|
||||
docker :login, registry["server"], "-u", sensitive(lookup("username")), "-p", sensitive(lookup("password"))
|
||||
end
|
||||
|
||||
def logout
|
||||
|
||||
@@ -1,16 +1,19 @@
|
||||
class Mrsk::Commands::Traefik < Mrsk::Commands::Base
|
||||
delegate :optionize, to: Mrsk::Utils
|
||||
delegate :argumentize, :optionize, to: Mrsk::Utils
|
||||
|
||||
DEFAULT_IMAGE = "traefik:v2.9"
|
||||
CONTAINER_PORT = 80
|
||||
|
||||
def run
|
||||
docker :run, "--name traefik",
|
||||
"--detach",
|
||||
"--restart", "unless-stopped",
|
||||
"--log-opt", "max-size=#{MAX_LOG_SIZE}",
|
||||
"--publish", port,
|
||||
"--volume", "/var/run/docker.sock:/var/run/docker.sock",
|
||||
"traefik",
|
||||
*config.logging_args,
|
||||
*label_args,
|
||||
*docker_options_args,
|
||||
image,
|
||||
"--providers.docker",
|
||||
"--log.level=DEBUG",
|
||||
*cmd_option_args
|
||||
@@ -25,7 +28,7 @@ class Mrsk::Commands::Traefik < Mrsk::Commands::Base
|
||||
end
|
||||
|
||||
def info
|
||||
docker :ps, "--filter", "name=traefik"
|
||||
docker :ps, "--filter", "name=^traefik$"
|
||||
end
|
||||
|
||||
def logs(since: nil, lines: nil, grep: nil)
|
||||
@@ -49,20 +52,36 @@ class Mrsk::Commands::Traefik < Mrsk::Commands::Base
|
||||
docker :image, :prune, "--all", "--force", "--filter", "label=org.opencontainers.image.title=Traefik"
|
||||
end
|
||||
|
||||
def port
|
||||
def port
|
||||
"#{host_port}:#{CONTAINER_PORT}"
|
||||
end
|
||||
|
||||
private
|
||||
def label_args
|
||||
argumentize "--label", labels
|
||||
end
|
||||
|
||||
def labels
|
||||
config.traefik["labels"] || []
|
||||
end
|
||||
|
||||
def image
|
||||
config.traefik.fetch("image") { DEFAULT_IMAGE }
|
||||
end
|
||||
|
||||
def docker_options_args
|
||||
optionize(config.traefik["options"] || {})
|
||||
end
|
||||
|
||||
def cmd_option_args
|
||||
if args = config.raw_config.dig(:traefik, "args")
|
||||
optionize args
|
||||
if args = config.traefik["args"]
|
||||
optionize args, with: "="
|
||||
else
|
||||
[]
|
||||
end
|
||||
end
|
||||
|
||||
def host_port
|
||||
config.raw_config.dig(:traefik, "host_port") || CONTAINER_PORT
|
||||
config.traefik["host_port"] || CONTAINER_PORT
|
||||
end
|
||||
end
|
||||
|
||||
@@ -6,23 +6,24 @@ require "erb"
|
||||
require "net/ssh/proxy/jump"
|
||||
|
||||
class Mrsk::Configuration
|
||||
delegate :service, :image, :servers, :env, :labels, :registry, :builder, to: :raw_config, allow_nil: true
|
||||
delegate :argumentize, :argumentize_env_with_secrets, to: Mrsk::Utils
|
||||
delegate :service, :image, :servers, :env, :labels, :registry, :builder, :stop_wait_time, to: :raw_config, allow_nil: true
|
||||
delegate :argumentize, :argumentize_env_with_secrets, :optionize, to: Mrsk::Utils
|
||||
|
||||
attr_accessor :version
|
||||
attr_accessor :destination
|
||||
attr_accessor :raw_config
|
||||
|
||||
class << self
|
||||
def create_from(base_config_file, destination: nil, version: "missing")
|
||||
new(load_config_file(base_config_file).tap do |config|
|
||||
if destination
|
||||
config.deep_merge! \
|
||||
load_config_file destination_config_file(base_config_file, destination)
|
||||
end
|
||||
end, version: version)
|
||||
def create_from(config_file:, destination: nil, version: nil)
|
||||
raw_config = load_config_files(config_file, *destination_config_file(config_file, destination))
|
||||
|
||||
new raw_config, destination: destination, version: version
|
||||
end
|
||||
|
||||
private
|
||||
def load_config_files(*files)
|
||||
files.inject({}) { |config, file| config.deep_merge! load_config_file(file) }
|
||||
end
|
||||
|
||||
def load_config_file(file)
|
||||
if file.exist?
|
||||
YAML.load(ERB.new(IO.read(file)).result).symbolize_keys
|
||||
@@ -32,18 +33,31 @@ class Mrsk::Configuration
|
||||
end
|
||||
|
||||
def destination_config_file(base_config_file, destination)
|
||||
dir, basename = base_config_file.split
|
||||
dir.join basename.to_s.remove(".yml") + ".#{destination}.yml"
|
||||
base_config_file.sub_ext(".#{destination}.yml") if destination
|
||||
end
|
||||
end
|
||||
|
||||
def initialize(raw_config, version: "missing", validate: true)
|
||||
def initialize(raw_config, destination: nil, version: nil, validate: true)
|
||||
@raw_config = ActiveSupport::InheritableOptions.new(raw_config)
|
||||
@version = version
|
||||
@destination = destination
|
||||
@declared_version = version
|
||||
valid? if validate
|
||||
end
|
||||
|
||||
|
||||
def version=(version)
|
||||
@declared_version = version
|
||||
end
|
||||
|
||||
def version
|
||||
@declared_version.presence || ENV["VERSION"] || current_commit_hash
|
||||
end
|
||||
|
||||
def abbreviated_version
|
||||
Mrsk::Utils.abbreviate_version(version)
|
||||
end
|
||||
|
||||
|
||||
def roles
|
||||
@roles ||= role_names.collect { |role_name| Role.new(role_name, config: self) }
|
||||
end
|
||||
@@ -62,15 +76,19 @@ class Mrsk::Configuration
|
||||
|
||||
|
||||
def all_hosts
|
||||
roles.flat_map(&:hosts)
|
||||
roles.flat_map(&:hosts).uniq
|
||||
end
|
||||
|
||||
def primary_web_host
|
||||
role(:web).hosts.first
|
||||
role(:web).primary_host
|
||||
end
|
||||
|
||||
def traefik_hosts
|
||||
roles.select(&:running_traefik?).flat_map(&:hosts)
|
||||
roles.select(&:running_traefik?).flat_map(&:hosts).uniq
|
||||
end
|
||||
|
||||
def boot
|
||||
Mrsk::Configuration::Boot.new(config: self)
|
||||
end
|
||||
|
||||
|
||||
@@ -107,6 +125,15 @@ class Mrsk::Configuration
|
||||
end
|
||||
end
|
||||
|
||||
def logging_args
|
||||
if raw_config.logging.present?
|
||||
optionize({ "log-driver" => raw_config.logging["driver"] }.compact) +
|
||||
argumentize("--log-opt", raw_config.logging["options"])
|
||||
else
|
||||
argumentize("--log-opt", { "max-size" => "10m" })
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def ssh_user
|
||||
if raw_config.ssh.present?
|
||||
@@ -120,6 +147,8 @@ class Mrsk::Configuration
|
||||
if raw_config.ssh.present? && raw_config.ssh["proxy"]
|
||||
Net::SSH::Proxy::Jump.new \
|
||||
raw_config.ssh["proxy"].include?("@") ? raw_config.ssh["proxy"] : "root@#{raw_config.ssh["proxy"]}"
|
||||
elsif raw_config.ssh.present? && raw_config.ssh["proxy_command"]
|
||||
Net::SSH::Proxy::Command.new(raw_config.ssh["proxy_command"])
|
||||
end
|
||||
end
|
||||
|
||||
@@ -133,7 +162,7 @@ class Mrsk::Configuration
|
||||
end
|
||||
|
||||
def healthcheck
|
||||
{ "path" => "/up", "port" => 3000 }.merge(raw_config.healthcheck || {})
|
||||
{ "path" => "/up", "port" => 3000, "max_attempts" => 7 }.merge(raw_config.healthcheck || {})
|
||||
end
|
||||
|
||||
def readiness_delay
|
||||
@@ -159,10 +188,14 @@ class Mrsk::Configuration
|
||||
ssh_options: ssh_options,
|
||||
builder: raw_config.builder,
|
||||
accessories: raw_config.accessories,
|
||||
logging: logging_args,
|
||||
healthcheck: healthcheck
|
||||
}.compact
|
||||
end
|
||||
|
||||
def traefik
|
||||
raw_config.traefik || {}
|
||||
end
|
||||
|
||||
private
|
||||
# Will raise ArgumentError if any required config keys are missing
|
||||
@@ -179,6 +212,12 @@ class Mrsk::Configuration
|
||||
raise ArgumentError, "You must specify a password for the registry in config/deploy.yml (or set the ENV variable if that's used)"
|
||||
end
|
||||
|
||||
roles.each do |role|
|
||||
if role.hosts.empty?
|
||||
raise ArgumentError, "No servers specified for the #{role.name} role"
|
||||
end
|
||||
end
|
||||
|
||||
true
|
||||
end
|
||||
|
||||
@@ -193,4 +232,13 @@ class Mrsk::Configuration
|
||||
def role_names
|
||||
raw_config.servers.is_a?(Array) ? [ "web" ] : raw_config.servers.keys.sort
|
||||
end
|
||||
|
||||
def current_commit_hash
|
||||
@current_commit_hash ||=
|
||||
if system("git rev-parse")
|
||||
`git rev-parse HEAD`.strip
|
||||
else
|
||||
raise "Can't use commit hash as version, no git repository found in #{Dir.pwd}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
class Mrsk::Configuration::Accessory
|
||||
delegate :argumentize, :argumentize_env_with_secrets, to: Mrsk::Utils
|
||||
delegate :argumentize, :argumentize_env_with_secrets, :optionize, to: Mrsk::Utils
|
||||
|
||||
attr_accessor :name, :specifics
|
||||
|
||||
@@ -15,18 +15,24 @@ class Mrsk::Configuration::Accessory
|
||||
specifics["image"]
|
||||
end
|
||||
|
||||
def host
|
||||
specifics["host"] || raise(ArgumentError, "Missing host for accessory")
|
||||
def hosts
|
||||
if (specifics.keys & ["host", "hosts", "roles"]).size != 1
|
||||
raise ArgumentError, "Specify one of `host`, `hosts` or `roles` for accessory `#{name}`"
|
||||
end
|
||||
|
||||
hosts_from_host || hosts_from_hosts || hosts_from_roles
|
||||
end
|
||||
|
||||
def port
|
||||
if specifics["port"].to_s.include?(":")
|
||||
specifics["port"]
|
||||
else
|
||||
"#{specifics["port"]}:#{specifics["port"]}"
|
||||
if port = specifics["port"]&.to_s
|
||||
port.include?(":") ? port : "#{port}:#{port}"
|
||||
end
|
||||
end
|
||||
|
||||
def publish_args
|
||||
argumentize "--publish", port if port
|
||||
end
|
||||
|
||||
def labels
|
||||
default_labels.merge(specifics["labels"] || {})
|
||||
end
|
||||
@@ -65,6 +71,18 @@ class Mrsk::Configuration::Accessory
|
||||
argumentize "--volume", volumes
|
||||
end
|
||||
|
||||
def option_args
|
||||
if args = specifics["options"]
|
||||
optionize args
|
||||
else
|
||||
[]
|
||||
end
|
||||
end
|
||||
|
||||
def cmd
|
||||
specifics["cmd"]
|
||||
end
|
||||
|
||||
private
|
||||
attr_accessor :config
|
||||
|
||||
@@ -120,4 +138,32 @@ class Mrsk::Configuration::Accessory
|
||||
def service_data_directory
|
||||
"$PWD/#{service_name}"
|
||||
end
|
||||
|
||||
def hosts_from_host
|
||||
if specifics.key?("host")
|
||||
host = specifics["host"]
|
||||
if host
|
||||
[host]
|
||||
else
|
||||
raise ArgumentError, "Missing host for accessory `#{name}`"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def hosts_from_hosts
|
||||
if specifics.key?("hosts")
|
||||
hosts = specifics["hosts"]
|
||||
if hosts.is_a?(Array)
|
||||
hosts
|
||||
else
|
||||
raise ArgumentError, "Hosts should be an Array for accessory `#{name}`"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def hosts_from_roles
|
||||
if specifics.key?("roles")
|
||||
specifics["roles"].flat_map { |role| config.role(role).hosts }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
20
lib/mrsk/configuration/boot.rb
Normal file
20
lib/mrsk/configuration/boot.rb
Normal file
@@ -0,0 +1,20 @@
|
||||
class Mrsk::Configuration::Boot
|
||||
def initialize(config:)
|
||||
@options = config.raw_config.boot || {}
|
||||
@host_count = config.all_hosts.count
|
||||
end
|
||||
|
||||
def limit
|
||||
limit = @options["limit"]
|
||||
|
||||
if limit.to_s.end_with?("%")
|
||||
@host_count * limit.to_i / 100
|
||||
else
|
||||
limit
|
||||
end
|
||||
end
|
||||
|
||||
def wait
|
||||
@options["wait"]
|
||||
end
|
||||
end
|
||||
@@ -7,6 +7,10 @@ class Mrsk::Configuration::Role
|
||||
@name, @config = name.inquiry, config
|
||||
end
|
||||
|
||||
def primary_host
|
||||
hosts.first
|
||||
end
|
||||
|
||||
def hosts
|
||||
@hosts ||= extract_hosts_from_config
|
||||
end
|
||||
@@ -31,6 +35,21 @@ class Mrsk::Configuration::Role
|
||||
argumentize_env_with_secrets env
|
||||
end
|
||||
|
||||
def health_check_args
|
||||
if health_check_cmd.present?
|
||||
optionize({ "health-cmd" => health_check_cmd, "health-interval" => "1s" })
|
||||
else
|
||||
[]
|
||||
end
|
||||
end
|
||||
|
||||
def health_check_cmd
|
||||
options = specializations["healthcheck"] || {}
|
||||
options = config.healthcheck.merge(options) if running_traefik?
|
||||
|
||||
options["cmd"] || http_health_check(port: options["port"], path: options["path"])
|
||||
end
|
||||
|
||||
def cmd
|
||||
specializations["cmd"]
|
||||
end
|
||||
@@ -55,28 +74,38 @@ class Mrsk::Configuration::Role
|
||||
config.servers
|
||||
else
|
||||
servers = config.servers[name]
|
||||
servers.is_a?(Array) ? servers : servers["hosts"]
|
||||
servers.is_a?(Array) ? servers : Array(servers["hosts"])
|
||||
end
|
||||
end
|
||||
|
||||
def default_labels
|
||||
{ "service" => config.service, "role" => name }
|
||||
if config.destination
|
||||
{ "service" => config.service, "role" => name, "destination" => config.destination }
|
||||
else
|
||||
{ "service" => config.service, "role" => name }
|
||||
end
|
||||
end
|
||||
|
||||
def traefik_labels
|
||||
if running_traefik?
|
||||
{
|
||||
"traefik.http.routers.#{config.service}.rule" => "PathPrefix(`/`)",
|
||||
"traefik.http.services.#{config.service}.loadbalancer.healthcheck.path" => config.healthcheck["path"],
|
||||
"traefik.http.services.#{config.service}.loadbalancer.healthcheck.interval" => "1s",
|
||||
"traefik.http.middlewares.#{config.service}.retry.attempts" => "5",
|
||||
"traefik.http.middlewares.#{config.service}.retry.initialinterval" => "500ms"
|
||||
# Setting a service property ensures that the generated service name will be consistent between versions
|
||||
"traefik.http.services.#{traefik_service}.loadbalancer.server.scheme" => "http",
|
||||
|
||||
"traefik.http.routers.#{traefik_service}.rule" => "PathPrefix(`/`)",
|
||||
"traefik.http.middlewares.#{traefik_service}-retry.retry.attempts" => "5",
|
||||
"traefik.http.middlewares.#{traefik_service}-retry.retry.initialinterval" => "500ms",
|
||||
"traefik.http.routers.#{traefik_service}.middlewares" => "#{traefik_service}-retry@docker"
|
||||
}
|
||||
else
|
||||
{}
|
||||
end
|
||||
end
|
||||
|
||||
def traefik_service
|
||||
[ config.service, name, config.destination ].compact.join("-")
|
||||
end
|
||||
|
||||
def custom_labels
|
||||
Hash.new.tap do |labels|
|
||||
labels.merge!(config.labels) if config.labels.present?
|
||||
@@ -112,4 +141,8 @@ class Mrsk::Configuration::Role
|
||||
new_env["clear"] = (clear_app_env + clear_role_env).uniq
|
||||
end
|
||||
end
|
||||
|
||||
def http_health_check(port:, path:)
|
||||
"curl -f #{URI.join("http://localhost:#{port}", path)} || exit 1" if path.present? || port.present?
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,12 +1,52 @@
|
||||
require "sshkit"
|
||||
require "sshkit/dsl"
|
||||
require "active_support/core_ext/hash/deep_merge"
|
||||
require "json"
|
||||
|
||||
class SSHKit::Backend::Abstract
|
||||
def capture_with_info(*args)
|
||||
capture(*args, verbosity: Logger::INFO)
|
||||
def capture_with_info(*args, **kwargs)
|
||||
capture(*args, **kwargs, verbosity: Logger::INFO)
|
||||
end
|
||||
|
||||
def capture_with_pretty_json(*args, **kwargs)
|
||||
JSON.pretty_generate(JSON.parse(capture(*args, **kwargs)))
|
||||
end
|
||||
|
||||
def puts_by_host(host, output, type: "App")
|
||||
puts "#{type} Host: #{host}\n#{output}\n\n"
|
||||
end
|
||||
|
||||
# Our execution pattern is for the CLI execute args lists returned
|
||||
# from commands, but this doesn't support returning execution options
|
||||
# from the command.
|
||||
#
|
||||
# Support this by using kwargs for CLI options and merging with the
|
||||
# args-extracted options.
|
||||
module CommandEnvMerge
|
||||
private
|
||||
|
||||
# Override to merge options returned by commands in the args list with
|
||||
# options passed by the CLI and pass them along as kwargs.
|
||||
def command(args, options)
|
||||
more_options, args = args.partition { |a| a.is_a? Hash }
|
||||
more_options << options
|
||||
|
||||
build_command(args, **more_options.reduce(:deep_merge))
|
||||
end
|
||||
|
||||
# Destructure options to pluck out env for merge
|
||||
def build_command(args, env: nil, **options)
|
||||
# Rely on native Ruby kwargs precedence rather than explicit Hash merges
|
||||
SSHKit::Command.new(*args, **default_command_options, **options, env: env_for(env))
|
||||
end
|
||||
|
||||
def default_command_options
|
||||
{ in: pwd_path, host: @host, user: @user, group: @group }
|
||||
end
|
||||
|
||||
def env_for(env)
|
||||
@env.to_h.merge(env.to_h)
|
||||
end
|
||||
end
|
||||
prepend CommandEnvMerge
|
||||
end
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
module Mrsk::Utils
|
||||
extend self
|
||||
|
||||
DOLLAR_SIGN_WITHOUT_SHELL_EXPANSION_REGEX = /\$(?!{[^\}]*\})/
|
||||
|
||||
# Return a list of escaped shell arguments using the same named argument against the passed attributes (hash or array).
|
||||
def argumentize(argument, attributes, redacted: false)
|
||||
def argumentize(argument, attributes, sensitive: false)
|
||||
Array(attributes).flat_map do |key, value|
|
||||
if value.present?
|
||||
escaped_pair = [ key, escape_shell_value(value) ].join("=")
|
||||
[ argument, redacted ? redact(escaped_pair) : escaped_pair ]
|
||||
attr = "#{key}=#{escape_shell_value(value)}"
|
||||
attr = self.sensitive(attr, redaction: "#{key}=[REDACTED]") if sensitive
|
||||
[ argument, attr]
|
||||
else
|
||||
[ argument, key ]
|
||||
end
|
||||
@@ -17,24 +20,70 @@ module Mrsk::Utils
|
||||
# but redacts and expands secrets.
|
||||
def argumentize_env_with_secrets(env)
|
||||
if (secrets = env["secret"]).present?
|
||||
argumentize("-e", secrets.to_h { |key| [ key, ENV.fetch(key) ] }, redacted: true) + argumentize("-e", env["clear"])
|
||||
argumentize("-e", secrets.to_h { |key| [ key, ENV.fetch(key) ] }, sensitive: true) + argumentize("-e", env["clear"])
|
||||
else
|
||||
argumentize "-e", env.fetch("clear", env)
|
||||
end
|
||||
end
|
||||
|
||||
# Returns a list of shell-dashed option arguments. If the value is true, it's treated like a value-less option.
|
||||
def optionize(args)
|
||||
args.collect { |(key, value)| [ "--#{key}", value == true ? nil : escape_shell_value(value) ] }.flatten.compact
|
||||
def optionize(args, with: nil)
|
||||
options = if with
|
||||
flatten_args(args).collect { |(key, value)| value == true ? "--#{key}" : "--#{key}#{with}#{escape_shell_value(value)}" }
|
||||
else
|
||||
flatten_args(args).collect { |(key, value)| [ "--#{key}", value == true ? nil : escape_shell_value(value) ] }
|
||||
end
|
||||
|
||||
options.flatten.compact
|
||||
end
|
||||
|
||||
# Copied from SSHKit::Backend::Abstract#redact to be available inside Commands classes
|
||||
def redact(arg) # Used in execute_command to hide redact() args a user passes in
|
||||
arg.to_s.extend(SSHKit::Redaction) # to_s due to our inability to extend Integer, etc
|
||||
# Flattens a one-to-many structure into an array of two-element arrays each containing a key-value pair
|
||||
def flatten_args(args)
|
||||
args.flat_map { |key, value| value.try(:map) { |entry| [key, entry] } || [ [ key, value ] ] }
|
||||
end
|
||||
|
||||
# Marks sensitive values for redaction in logs and human-visible output.
|
||||
# Pass `redaction:` to change the default `"[REDACTED]"` redaction, e.g.
|
||||
# `sensitive "#{arg}=#{secret}", redaction: "#{arg}=xxxx"
|
||||
def sensitive(...)
|
||||
Mrsk::Utils::Sensitive.new(...)
|
||||
end
|
||||
|
||||
def redacted(value)
|
||||
case
|
||||
when value.respond_to?(:redaction)
|
||||
value.redaction
|
||||
when value.respond_to?(:transform_values)
|
||||
value.transform_values { |value| redacted value }
|
||||
when value.respond_to?(:map)
|
||||
value.map { |element| redacted element }
|
||||
else
|
||||
value
|
||||
end
|
||||
end
|
||||
|
||||
def unredacted(value)
|
||||
case
|
||||
when value.respond_to?(:unredacted)
|
||||
value.unredacted
|
||||
when value.respond_to?(:transform_values)
|
||||
value.transform_values { |value| unredacted value }
|
||||
when value.respond_to?(:map)
|
||||
value.map { |element| unredacted element }
|
||||
else
|
||||
value
|
||||
end
|
||||
end
|
||||
|
||||
# Escape a value to make it safe for shell use.
|
||||
def escape_shell_value(value)
|
||||
value.to_s.dump.gsub(/`/, '\\\\`')
|
||||
value.to_s.dump
|
||||
.gsub(/`/, '\\\\`')
|
||||
.gsub(DOLLAR_SIGN_WITHOUT_SHELL_EXPANSION_REGEX, '\$')
|
||||
end
|
||||
|
||||
# Abbreviate a git revhash for concise display
|
||||
def abbreviate_version(version)
|
||||
version[0...7] if version
|
||||
end
|
||||
end
|
||||
|
||||
39
lib/mrsk/utils/healthcheck_poller.rb
Normal file
39
lib/mrsk/utils/healthcheck_poller.rb
Normal file
@@ -0,0 +1,39 @@
|
||||
class Mrsk::Utils::HealthcheckPoller
|
||||
TRAEFIK_HEALTHY_DELAY = 2
|
||||
|
||||
class HealthcheckError < StandardError; end
|
||||
|
||||
class << self
|
||||
def wait_for_healthy(pause_after_ready: false, &block)
|
||||
attempt = 1
|
||||
max_attempts = MRSK.config.healthcheck["max_attempts"]
|
||||
|
||||
begin
|
||||
case status = block.call
|
||||
when "healthy"
|
||||
sleep TRAEFIK_HEALTHY_DELAY if pause_after_ready
|
||||
when "running" # No health check configured
|
||||
sleep MRSK.config.readiness_delay if pause_after_ready
|
||||
else
|
||||
raise HealthcheckError, "container not ready (#{status})"
|
||||
end
|
||||
rescue HealthcheckError => e
|
||||
if attempt <= max_attempts
|
||||
info "#{e.message}, retrying in #{attempt}s (attempt #{attempt}/#{max_attempts})..."
|
||||
sleep attempt
|
||||
attempt += 1
|
||||
retry
|
||||
else
|
||||
raise
|
||||
end
|
||||
end
|
||||
|
||||
info "Container is healthy!"
|
||||
end
|
||||
|
||||
private
|
||||
def info(message)
|
||||
SSHKit.config.output.info(message)
|
||||
end
|
||||
end
|
||||
end
|
||||
19
lib/mrsk/utils/sensitive.rb
Normal file
19
lib/mrsk/utils/sensitive.rb
Normal file
@@ -0,0 +1,19 @@
|
||||
require "active_support/core_ext/module/delegation"
|
||||
|
||||
class Mrsk::Utils::Sensitive
|
||||
# So SSHKit knows to redact these values.
|
||||
include SSHKit::Redaction
|
||||
|
||||
attr_reader :unredacted, :redaction
|
||||
delegate :to_s, to: :unredacted
|
||||
delegate :inspect, to: :redaction
|
||||
|
||||
def initialize(value, redaction: "[REDACTED]")
|
||||
@unredacted, @redaction = value, redaction
|
||||
end
|
||||
|
||||
# Sensitive values won't leak into YAML output.
|
||||
def encode_with(coder)
|
||||
coder.represent_scalar nil, redaction
|
||||
end
|
||||
end
|
||||
@@ -1,3 +1,3 @@
|
||||
module Mrsk
|
||||
VERSION = "0.9.1"
|
||||
VERSION = "0.12.1"
|
||||
end
|
||||
|
||||
@@ -14,9 +14,14 @@ Gem::Specification.new do |spec|
|
||||
|
||||
spec.add_dependency "activesupport", ">= 7.0"
|
||||
spec.add_dependency "sshkit", "~> 1.21"
|
||||
spec.add_dependency "net-ssh", "~> 7.0"
|
||||
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"
|
||||
|
||||
spec.add_development_dependency "debug"
|
||||
spec.add_development_dependency "mocha"
|
||||
spec.add_development_dependency "railties"
|
||||
end
|
||||
|
||||
@@ -1,42 +1,138 @@
|
||||
require_relative "cli_test_case"
|
||||
|
||||
class CliAccessoryTest < CliTestCase
|
||||
test "boot" do
|
||||
Mrsk::Cli::Accessory.any_instance.expects(:directories).with("mysql")
|
||||
Mrsk::Cli::Accessory.any_instance.expects(:upload).with("mysql")
|
||||
|
||||
run_command("boot", "mysql").tap do |output|
|
||||
assert_match /docker login.*on 1.1.1.3/, output
|
||||
assert_match "docker run --name app-mysql --detach --restart unless-stopped --log-opt max-size=\"10m\" --publish 3306:3306 -e [REDACTED] -e MYSQL_ROOT_HOST=\"%\" --volume $PWD/app-mysql/etc/mysql/my.cnf:/etc/mysql/my.cnf --volume $PWD/app-mysql/data:/var/lib/mysql --label service=\"app-mysql\" mysql:5.7 on 1.1.1.3", output
|
||||
end
|
||||
end
|
||||
|
||||
test "boot all" do
|
||||
Mrsk::Cli::Accessory.any_instance.expects(:directories).with("mysql")
|
||||
Mrsk::Cli::Accessory.any_instance.expects(:upload).with("mysql")
|
||||
Mrsk::Cli::Accessory.any_instance.expects(:directories).with("redis")
|
||||
Mrsk::Cli::Accessory.any_instance.expects(:upload).with("redis")
|
||||
|
||||
run_command("boot", "all").tap do |output|
|
||||
assert_match /docker login.*on 1.1.1.3/, output
|
||||
assert_match /docker login.*on 1.1.1.1/, output
|
||||
assert_match /docker login.*on 1.1.1.2/, output
|
||||
assert_match "docker run --name app-mysql --detach --restart unless-stopped --log-opt max-size=\"10m\" --publish 3306:3306 -e [REDACTED] -e MYSQL_ROOT_HOST=\"%\" --volume $PWD/app-mysql/etc/mysql/my.cnf:/etc/mysql/my.cnf --volume $PWD/app-mysql/data:/var/lib/mysql --label service=\"app-mysql\" mysql:5.7 on 1.1.1.3", output
|
||||
assert_match "docker run --name app-redis --detach --restart unless-stopped --log-opt max-size=\"10m\" --publish 6379:6379 --volume $PWD/app-redis/data:/data --label service=\"app-redis\" redis:latest on 1.1.1.1", output
|
||||
assert_match "docker run --name app-redis --detach --restart unless-stopped --log-opt max-size=\"10m\" --publish 6379:6379 --volume $PWD/app-redis/data:/data --label service=\"app-redis\" redis:latest on 1.1.1.2", output
|
||||
end
|
||||
end
|
||||
|
||||
test "upload" do
|
||||
assert_match "test/fixtures/files/my.cnf app-mysql/etc/mysql/my.cnf", run_command("upload", "mysql")
|
||||
run_command("upload", "mysql").tap do |output|
|
||||
assert_match "mkdir -p app-mysql/etc/mysql", output
|
||||
assert_match "test/fixtures/files/my.cnf app-mysql/etc/mysql/my.cnf", output
|
||||
assert_match "chmod 755 app-mysql/etc/mysql/my.cnf", output
|
||||
end
|
||||
end
|
||||
|
||||
test "directories" do
|
||||
assert_match "mkdir -p $PWD/app-mysql/data", run_command("directories", "mysql")
|
||||
end
|
||||
|
||||
test "remove service direcotry" do
|
||||
assert_match "rm -rf app-mysql", run_command("remove_service_directory", "mysql")
|
||||
test "reboot" do
|
||||
Mrsk::Cli::Accessory.any_instance.expects(:stop).with("mysql")
|
||||
Mrsk::Cli::Accessory.any_instance.expects(:remove_container).with("mysql")
|
||||
Mrsk::Cli::Accessory.any_instance.expects(:boot).with("mysql")
|
||||
|
||||
run_command("reboot", "mysql")
|
||||
end
|
||||
|
||||
test "boot" do
|
||||
assert_match "Running docker run --name app-mysql --detach --restart unless-stopped --log-opt max-size=10m --publish 3306:3306 -e [REDACTED] -e MYSQL_ROOT_HOST=\"%\" --volume $PWD/app-mysql/etc/mysql/my.cnf:/etc/mysql/my.cnf --volume $PWD/app-mysql/data:/var/lib/mysql --label service=\"app-mysql\" mysql:5.7 on 1.1.1.3", run_command("boot", "mysql")
|
||||
test "start" do
|
||||
assert_match "docker container start app-mysql", run_command("start", "mysql")
|
||||
end
|
||||
|
||||
test "stop" do
|
||||
assert_match "docker container stop app-mysql", run_command("stop", "mysql")
|
||||
end
|
||||
|
||||
test "restart" do
|
||||
Mrsk::Cli::Accessory.any_instance.expects(:stop).with("mysql")
|
||||
Mrsk::Cli::Accessory.any_instance.expects(:start).with("mysql")
|
||||
|
||||
run_command("restart", "mysql")
|
||||
end
|
||||
|
||||
test "details" do
|
||||
assert_match "docker ps --filter label=service=app-mysql", run_command("details", "mysql")
|
||||
end
|
||||
|
||||
test "details with all" do
|
||||
run_command("details", "all").tap do |output|
|
||||
assert_match "docker ps --filter label=service=app-mysql", output
|
||||
assert_match "docker ps --filter label=service=app-redis", output
|
||||
end
|
||||
end
|
||||
|
||||
test "exec" do
|
||||
run_command("exec", "mysql", "mysql -v").tap do |output|
|
||||
assert_match /Launching command from new container/, output
|
||||
assert_match /mysql -v/, output
|
||||
assert_match "Launching command from new container", output
|
||||
assert_match "mysql -v", output
|
||||
end
|
||||
end
|
||||
|
||||
test "exec with reuse" do
|
||||
run_command("exec", "mysql", "--reuse", "mysql -v").tap do |output|
|
||||
assert_match /Launching command from existing container/, output
|
||||
assert_match %r[docker exec app-mysql mysql -v], output
|
||||
assert_match "Launching command from existing container", output
|
||||
assert_match "docker exec app-mysql mysql -v", output
|
||||
end
|
||||
end
|
||||
|
||||
test "logs" do
|
||||
SSHKit::Backend::Abstract.any_instance.stubs(:exec)
|
||||
.with("ssh -t root@1.1.1.3 'docker logs app-mysql --timestamps --tail 10 2>&1'")
|
||||
|
||||
assert_match "docker logs app-mysql --tail 100 --timestamps 2>&1", run_command("logs", "mysql")
|
||||
end
|
||||
|
||||
test "logs with follow" do
|
||||
SSHKit::Backend::Abstract.any_instance.stubs(:exec)
|
||||
.with("ssh -t root@1.1.1.3 'docker logs app-mysql --timestamps --tail 10 --follow 2>&1'")
|
||||
|
||||
assert_match "docker logs app-mysql --timestamps --tail 10 --follow 2>&1", run_command("logs", "mysql", "--follow")
|
||||
end
|
||||
|
||||
test "remove with confirmation" do
|
||||
run_command("remove", "mysql", "-y").tap do |output|
|
||||
assert_match /docker container stop app-mysql/, output
|
||||
assert_match /docker image prune --all --force --filter label=service=app-mysql/, output
|
||||
assert_match /rm -rf app-mysql/, output
|
||||
end
|
||||
Mrsk::Cli::Accessory.any_instance.expects(:stop).with("mysql")
|
||||
Mrsk::Cli::Accessory.any_instance.expects(:remove_container).with("mysql")
|
||||
Mrsk::Cli::Accessory.any_instance.expects(:remove_image).with("mysql")
|
||||
Mrsk::Cli::Accessory.any_instance.expects(:remove_service_directory).with("mysql")
|
||||
|
||||
run_command("remove", "mysql", "-y")
|
||||
end
|
||||
|
||||
test "remove all with confirmation" do
|
||||
Mrsk::Cli::Accessory.any_instance.expects(:stop).with("mysql")
|
||||
Mrsk::Cli::Accessory.any_instance.expects(:remove_container).with("mysql")
|
||||
Mrsk::Cli::Accessory.any_instance.expects(:remove_image).with("mysql")
|
||||
Mrsk::Cli::Accessory.any_instance.expects(:remove_service_directory).with("mysql")
|
||||
Mrsk::Cli::Accessory.any_instance.expects(:stop).with("redis")
|
||||
Mrsk::Cli::Accessory.any_instance.expects(:remove_container).with("redis")
|
||||
Mrsk::Cli::Accessory.any_instance.expects(:remove_image).with("redis")
|
||||
Mrsk::Cli::Accessory.any_instance.expects(:remove_service_directory).with("redis")
|
||||
|
||||
run_command("remove", "all", "-y")
|
||||
end
|
||||
|
||||
test "remove_container" do
|
||||
assert_match "docker container prune --force --filter label=service=app-mysql", run_command("remove_container", "mysql")
|
||||
end
|
||||
|
||||
test "remove_image" do
|
||||
assert_match "docker image rm --force mysql", run_command("remove_image", "mysql")
|
||||
end
|
||||
|
||||
test "remove_service_directory" do
|
||||
assert_match "rm -rf app-mysql", run_command("remove_service_directory", "mysql")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
@@ -2,88 +2,173 @@ require_relative "cli_test_case"
|
||||
|
||||
class CliAppTest < CliTestCase
|
||||
test "boot" do
|
||||
# Stub current version fetch
|
||||
SSHKit::Backend::Abstract.any_instance.stubs(:capture)
|
||||
.returns("999") # new version
|
||||
.then
|
||||
.returns("123") # old version
|
||||
SSHKit::Backend::Abstract.any_instance.stubs(:capture_with_info).returns("123") # old version
|
||||
|
||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-web-latest$", "--quiet", "|", :xargs, :docker, :inspect, "--format", "'{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}'")
|
||||
.returns("running") # health check
|
||||
|
||||
run_command("boot").tap do |output|
|
||||
assert_match /docker run --detach --restart unless-stopped/, output
|
||||
assert_match /docker container ls --all --filter name=app-123 --quiet | xargs docker stop/, output
|
||||
assert_match "docker tag dhh/app:latest dhh/app:latest", output
|
||||
assert_match "docker run --detach --restart unless-stopped", output
|
||||
assert_match "docker container ls --all --filter name=^app-web-123$ --quiet | xargs docker stop", output
|
||||
end
|
||||
end
|
||||
|
||||
test "boot will reboot if same version is already running" do
|
||||
test "boot will rename if same version is already running" do
|
||||
run_command("details") # Preheat MRSK const
|
||||
|
||||
# Prevent expected failures from outputting to terminal
|
||||
Thread.report_on_exception = false
|
||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-web-latest$", "--quiet", raise_on_non_zero_exit: false)
|
||||
.returns("12345678") # running version
|
||||
|
||||
MRSK.app.stubs(:run)
|
||||
.raises(SSHKit::Command::Failed.new("already in use"))
|
||||
.then
|
||||
.raises(SSHKit::Command::Failed.new("already in use"))
|
||||
.then
|
||||
.returns([ :docker, :run ])
|
||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-web-latest$", "--quiet", "|", :xargs, :docker, :inspect, "--format", "'{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}'")
|
||||
.returns("running") # health check
|
||||
|
||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
||||
.with(:docker, :ps, "--filter", "label=service=app", "--filter", "label=role=web", "--filter", "status=running", "--latest", "--format", "\"{{.Names}}\"", "|", "grep -oE \"\\-[^-]+$\"", "|", "cut -c 2-", raise_on_non_zero_exit: false)
|
||||
.returns("123") # old version
|
||||
|
||||
run_command("boot").tap do |output|
|
||||
assert_match /Rebooting container with same version 999 already deployed/, output # Can't start what's already running
|
||||
assert_match /docker container ls --all --filter name=app-999 --quiet | xargs docker container rm/, output # Stop old running
|
||||
assert_match /docker container ls --all --filter name=app-999 --quiet | xargs docker container rm/, output # Remove old container
|
||||
assert_match /docker run/, output # Start new container
|
||||
assert_match /Renaming container .* to .* as already deployed on 1.1.1.1/, output # Rename
|
||||
assert_match /docker rename .* .*/, output
|
||||
assert_match "docker run --detach --restart unless-stopped", output
|
||||
assert_match "docker container ls --all --filter name=^app-web-123$ --quiet | xargs docker stop", output
|
||||
end
|
||||
ensure
|
||||
Thread.report_on_exception = true
|
||||
end
|
||||
|
||||
test "boot uses group strategy when specified" do
|
||||
Mrsk::Cli::App.any_instance.stubs(:on).with("1.1.1.1").twice # acquire & release lock
|
||||
Mrsk::Cli::App.any_instance.stubs(:on).with([ "1.1.1.1" ]) # tag container
|
||||
|
||||
# Strategy is used when booting the containers
|
||||
Mrsk::Cli::App.any_instance.expects(:on).with([ "1.1.1.1" ], in: :groups, limit: 3, wait: 2).with_block_given
|
||||
|
||||
run_command("boot", config: :with_boot_strategy)
|
||||
end
|
||||
|
||||
test "start" do
|
||||
run_command("start").tap do |output|
|
||||
assert_match /docker start app-999/, output
|
||||
assert_match "docker start app-web-999", output
|
||||
end
|
||||
end
|
||||
|
||||
test "stop" do
|
||||
run_command("stop").tap do |output|
|
||||
assert_match /docker ps --quiet --filter label=service=app \| xargs docker stop/, output
|
||||
assert_match "docker ps --quiet --filter label=service=app --filter label=role=web --filter status=running --latest | xargs docker stop", output
|
||||
end
|
||||
end
|
||||
|
||||
test "stale_containers" do
|
||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
||||
.with(:docker, :ps, "--filter", "label=service=app", "--filter", "label=role=web", "--format", "\"{{.Names}}\"", "|", "grep -oE \"\\-[^-]+$\"", "|", "cut -c 2-", raise_on_non_zero_exit: false)
|
||||
.returns("12345678\n87654321")
|
||||
|
||||
run_command("stale_containers").tap do |output|
|
||||
assert_match /Detected stale container for role web with version 87654321/, output
|
||||
end
|
||||
end
|
||||
|
||||
test "stop stale_containers" do
|
||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
||||
.with(:docker, :ps, "--filter", "label=service=app", "--filter", "label=role=web", "--format", "\"{{.Names}}\"", "|", "grep -oE \"\\-[^-]+$\"", "|", "cut -c 2-", raise_on_non_zero_exit: false)
|
||||
.returns("12345678\n87654321")
|
||||
|
||||
run_command("stale_containers", "--stop").tap do |output|
|
||||
assert_match /Stopping stale container for role web with version 87654321/, output
|
||||
assert_match /#{Regexp.escape("docker container ls --all --filter name=^app-web-87654321$ --quiet | xargs docker stop")}/, output
|
||||
end
|
||||
end
|
||||
|
||||
test "details" do
|
||||
run_command("details").tap do |output|
|
||||
assert_match /docker ps --filter label=service=app/, output
|
||||
assert_match "docker ps --filter label=service=app --filter label=role=web", output
|
||||
end
|
||||
end
|
||||
|
||||
test "remove" do
|
||||
run_command("remove").tap do |output|
|
||||
assert_match /docker ps --quiet --filter label=service=app | xargs docker stop/, output
|
||||
assert_match /docker container prune --force --filter label=service=app/, output
|
||||
assert_match /docker image prune --all --force --filter label=service=app/, output
|
||||
assert_match /#{Regexp.escape("docker ps --quiet --filter label=service=app --filter label=role=web --filter status=running --latest | xargs docker stop")}/, output
|
||||
assert_match /#{Regexp.escape("docker container prune --force --filter label=service=app")}/, output
|
||||
assert_match /#{Regexp.escape("docker image prune --all --force --filter label=service=app")}/, output
|
||||
end
|
||||
end
|
||||
|
||||
test "remove_container" do
|
||||
run_command("remove_container", "1234567").tap do |output|
|
||||
assert_match /docker container ls --all --filter name=app-1234567 --quiet \| xargs docker container rm/, output
|
||||
assert_match "docker container ls --all --filter name=^app-web-1234567$ --quiet | xargs docker container rm", output
|
||||
end
|
||||
end
|
||||
|
||||
test "remove_containers" do
|
||||
run_command("remove_containers").tap do |output|
|
||||
assert_match "docker container prune --force --filter label=service=app", output
|
||||
end
|
||||
end
|
||||
|
||||
test "remove_images" do
|
||||
run_command("remove_images").tap do |output|
|
||||
assert_match "docker image prune --all --force --filter label=service=app", output
|
||||
end
|
||||
end
|
||||
|
||||
test "exec" do
|
||||
run_command("exec", "ruby -v").tap do |output|
|
||||
assert_match /ruby -v/, output
|
||||
assert_match "docker run --rm dhh/app:latest ruby -v", output
|
||||
end
|
||||
end
|
||||
|
||||
test "exec with reuse" do
|
||||
run_command("exec", "--reuse", "ruby -v").tap do |output|
|
||||
assert_match %r[docker ps --filter label=service=app --format \"{{.Names}}\" | sed 's/-/\\n/g' | tail -n 1], output # Get current version
|
||||
assert_match %r[docker exec app-999 ruby -v], output
|
||||
assert_match "docker ps --filter label=service=app --filter status=running --latest --format \"{{.Names}}\" | grep -oE \"\\-[^-]+$\" | cut -c 2-", output # Get current version
|
||||
assert_match "docker exec app-web-999 ruby -v", output
|
||||
end
|
||||
end
|
||||
|
||||
test "containers" do
|
||||
run_command("containers").tap do |output|
|
||||
assert_match "docker container ls --all --filter label=service=app", output
|
||||
end
|
||||
end
|
||||
|
||||
test "images" do
|
||||
run_command("images").tap do |output|
|
||||
assert_match "docker image ls dhh/app", output
|
||||
end
|
||||
end
|
||||
|
||||
test "logs" do
|
||||
SSHKit::Backend::Abstract.any_instance.stubs(:exec)
|
||||
.with("ssh -t root@1.1.1.1 'docker ps --quiet --filter label=service=app --filter label=role=web --filter status=running --latest| xargs docker logs --timestamps --tail 10 2>&1'")
|
||||
|
||||
assert_match "docker ps --quiet --filter label=service=app --filter label=role=web --filter status=running --latest | xargs docker logs --tail 100 2>&1", run_command("logs")
|
||||
end
|
||||
|
||||
test "logs with follow" do
|
||||
SSHKit::Backend::Abstract.any_instance.stubs(:exec)
|
||||
.with("ssh -t root@1.1.1.1 'docker ps --quiet --filter label=service=app --filter label=role=web --filter status=running --latest | xargs docker logs --timestamps --tail 10 --follow 2>&1'")
|
||||
|
||||
assert_match "docker ps --quiet --filter label=service=app --filter label=role=web --filter status=running --latest | xargs docker logs --timestamps --tail 10 --follow 2>&1", run_command("logs", "--follow")
|
||||
end
|
||||
|
||||
test "version" do
|
||||
run_command("version").tap do |output|
|
||||
assert_match "docker ps --filter label=service=app --filter status=running --latest --format \"{{.Names}}\" | grep -oE \"\\-[^-]+$\" | cut -c 2-", output
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
test "version through main" do
|
||||
stdouted { Mrsk::Cli::Main.start(["app", "version", "-c", "test/fixtures/deploy_with_accessories.yml", "--hosts", "1.1.1.1"]) }.tap do |output|
|
||||
assert_match "docker ps --filter label=service=app --filter status=running --latest --format \"{{.Names}}\" | grep -oE \"\\-[^-]+$\" | cut -c 2-", output
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def run_command(*command)
|
||||
stdouted { Mrsk::Cli::App.start([*command, "-c", "test/fixtures/deploy_with_accessories.yml"]) }
|
||||
def run_command(*command, config: :with_accessories)
|
||||
stdouted { Mrsk::Cli::App.start([*command, "-c", "test/fixtures/deploy_#{config}.yml", "--hosts", "1.1.1.1"]) }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,6 +1,34 @@
|
||||
require_relative "cli_test_case"
|
||||
|
||||
class CliBuildTest < CliTestCase
|
||||
test "deliver" do
|
||||
Mrsk::Cli::Build.any_instance.expects(:push)
|
||||
Mrsk::Cli::Build.any_instance.expects(:pull)
|
||||
|
||||
run_command("deliver")
|
||||
end
|
||||
|
||||
test "push" do
|
||||
Mrsk::Cli::Build.any_instance.stubs(:verify_local_dependencies).returns(true)
|
||||
run_command("push").tap do |output|
|
||||
assert_match /docker buildx build --push --platform linux\/amd64,linux\/arm64 --builder mrsk-app-multiarch -t dhh\/app:999 -t dhh\/app:latest --label service="app" --file Dockerfile \. as .*@localhost/, output
|
||||
end
|
||||
end
|
||||
|
||||
test "push without builder" do
|
||||
stub_locking
|
||||
Mrsk::Cli::Build.any_instance.stubs(:verify_local_dependencies).returns(true)
|
||||
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
||||
.with { |arg| arg == :docker }
|
||||
.raises(SSHKit::Command::Failed.new("no builder"))
|
||||
.then
|
||||
.returns(true)
|
||||
|
||||
run_command("push").tap do |output|
|
||||
assert_match /Missing compatible builder, so creating a new one first/, output
|
||||
end
|
||||
end
|
||||
|
||||
test "pull" do
|
||||
run_command("pull").tap do |output|
|
||||
assert_match /docker image rm --force dhh\/app:999/, output
|
||||
@@ -8,8 +36,66 @@ class CliBuildTest < CliTestCase
|
||||
end
|
||||
end
|
||||
|
||||
test "create" do
|
||||
run_command("create").tap do |output|
|
||||
assert_match /docker buildx create --use --name mrsk-app-multiarch/, output
|
||||
end
|
||||
end
|
||||
|
||||
test "create with error" do
|
||||
stub_locking
|
||||
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
||||
.with { |arg| arg == :docker }
|
||||
.raises(SSHKit::Command::Failed.new("stderr=error"))
|
||||
|
||||
run_command("create").tap do |output|
|
||||
assert_match /Couldn't create remote builder: error/, output
|
||||
end
|
||||
end
|
||||
|
||||
test "remove" do
|
||||
run_command("remove").tap do |output|
|
||||
assert_match /docker buildx rm mrsk-app-multiarch/, output
|
||||
end
|
||||
end
|
||||
|
||||
test "details" do
|
||||
SSHKit::Backend::Abstract.any_instance.stubs(:capture)
|
||||
.with(:docker, :context, :ls, "&&", :docker, :buildx, :ls)
|
||||
.returns("docker builder info")
|
||||
|
||||
run_command("details").tap do |output|
|
||||
assert_match /Builder: multiarch/, output
|
||||
assert_match /docker builder info/, output
|
||||
end
|
||||
end
|
||||
|
||||
test "verify local dependencies" do
|
||||
Mrsk::Commands::Builder.any_instance.stubs(:name).returns("remote".inquiry)
|
||||
|
||||
run_command("verify_local_dependencies").tap do |output|
|
||||
assert_match /docker --version && docker buildx version/, output
|
||||
end
|
||||
end
|
||||
|
||||
test "verify local dependencies with no buildx plugin" do
|
||||
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
||||
.with(:docker, "--version", "&&", :docker, :buildx, "version")
|
||||
.raises(SSHKit::Command::Failed.new("no buildx"))
|
||||
|
||||
Mrsk::Commands::Builder.any_instance.stubs(:native_and_local?).returns(false)
|
||||
assert_raises(Mrsk::Cli::Build::BuildError) { run_command("verify_local_dependencies") }
|
||||
end
|
||||
|
||||
private
|
||||
def run_command(*command)
|
||||
stdouted { Mrsk::Cli::Build.start([*command, "-c", "test/fixtures/deploy_with_accessories.yml"]) }
|
||||
end
|
||||
|
||||
def stub_locking
|
||||
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
||||
.with { |arg1, arg2| arg1 == :mkdir && arg2 == :mrsk_lock }
|
||||
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
||||
.with { |arg1, arg2| arg1 == :rm && arg2 == "mrsk_lock/details" }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
require "test_helper"
|
||||
require "active_support/testing/stream"
|
||||
|
||||
class CliTestCase < ActiveSupport::TestCase
|
||||
include ActiveSupport::Testing::Stream
|
||||
@@ -8,17 +7,13 @@ class CliTestCase < ActiveSupport::TestCase
|
||||
ENV["VERSION"] = "999"
|
||||
ENV["RAILS_MASTER_KEY"] = "123"
|
||||
ENV["MYSQL_ROOT_PASSWORD"] = "secret123"
|
||||
Object.send(:remove_const, :MRSK)
|
||||
Object.const_set(:MRSK, Mrsk::Commander.new)
|
||||
end
|
||||
|
||||
teardown do
|
||||
ENV.delete("RAILS_MASTER_KEY")
|
||||
ENV.delete("MYSQL_ROOT_PASSWORD")
|
||||
ENV.delete("VERSION")
|
||||
MRSK.reset
|
||||
end
|
||||
|
||||
private
|
||||
def stdouted
|
||||
capture(:stdout) { yield }.strip
|
||||
end
|
||||
end
|
||||
|
||||
71
test/cli/healthcheck_test.rb
Normal file
71
test/cli/healthcheck_test.rb
Normal file
@@ -0,0 +1,71 @@
|
||||
require_relative "cli_test_case"
|
||||
|
||||
class CliHealthcheckTest < CliTestCase
|
||||
test "perform" do
|
||||
# Prevent expected failures from outputting to terminal
|
||||
Thread.report_on_exception = false
|
||||
|
||||
Mrsk::Utils::HealthcheckPoller.stubs(:sleep) # No sleeping when retrying
|
||||
|
||||
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^healthcheck-app-999$", "--quiet", "|", :xargs, :docker, :stop, raise_on_non_zero_exit: false)
|
||||
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
||||
.with(:docker, :run, "--detach", "--name", "healthcheck-app-999", "--publish", "3999:3000", "--label", "service=healthcheck-app", "-e", "MRSK_CONTAINER_NAME=\"healthcheck-app\"", "--health-cmd", "\"curl -f http://localhost:3000/up || exit 1\"", "--health-interval", "\"1s\"", "dhh/app:999")
|
||||
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^healthcheck-app-999$", "--quiet", "|", :xargs, :docker, :container, :rm, raise_on_non_zero_exit: false)
|
||||
|
||||
# Fail twice to test retry logic
|
||||
SSHKit::Backend::Abstract.any_instance.stubs(:capture_with_info)
|
||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^healthcheck-app-999$", "--quiet", "|", :xargs, :docker, :inspect, "--format", "'{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}'")
|
||||
.returns("starting")
|
||||
.then
|
||||
.returns("unhealthy")
|
||||
.then
|
||||
.returns("healthy")
|
||||
|
||||
run_command("perform").tap do |output|
|
||||
assert_match "container not ready (starting), retrying in 1s (attempt 1/7)...", output
|
||||
assert_match "container not ready (unhealthy), retrying in 2s (attempt 2/7)...", output
|
||||
assert_match "Container is healthy!", output
|
||||
end
|
||||
end
|
||||
|
||||
test "perform failing to become healthy" do
|
||||
# Prevent expected failures from outputting to terminal
|
||||
Thread.report_on_exception = false
|
||||
|
||||
Mrsk::Utils::HealthcheckPoller.stubs(:sleep) # No sleeping when retrying
|
||||
|
||||
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^healthcheck-app-999$", "--quiet", "|", :xargs, :docker, :stop, raise_on_non_zero_exit: false)
|
||||
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
||||
.with(:docker, :run, "--detach", "--name", "healthcheck-app-999", "--publish", "3999:3000", "--label", "service=healthcheck-app", "-e", "MRSK_CONTAINER_NAME=\"healthcheck-app\"", "--health-cmd", "\"curl -f http://localhost:3000/up || exit 1\"", "--health-interval", "\"1s\"", "dhh/app:999")
|
||||
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^healthcheck-app-999$", "--quiet", "|", :xargs, :docker, :container, :rm, raise_on_non_zero_exit: false)
|
||||
|
||||
# Continually report unhealthy
|
||||
SSHKit::Backend::Abstract.any_instance.stubs(:capture_with_info)
|
||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^healthcheck-app-999$", "--quiet", "|", :xargs, :docker, :inspect, "--format", "'{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}'")
|
||||
.returns("unhealthy")
|
||||
|
||||
# Capture logs when failing
|
||||
SSHKit::Backend::Abstract.any_instance.stubs(:capture_with_info)
|
||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^healthcheck-app-999$", "--quiet", "|", :xargs, :docker, :logs, "--tail", 50, "2>&1")
|
||||
.returns("some log output")
|
||||
|
||||
# Capture container health log when failing
|
||||
SSHKit::Backend::Abstract.any_instance.stubs(:capture_with_pretty_json)
|
||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^healthcheck-app-999$", "--quiet", "|", :xargs, :docker, :inspect, "--format", "'{{json .State.Health}}'")
|
||||
.returns('{"Status":"unhealthy","Log":[{"ExitCode": 1,"Output": "/bin/sh: 1: curl: not found\n"}]}"')
|
||||
|
||||
exception = assert_raises do
|
||||
run_command("perform")
|
||||
end
|
||||
assert_match "container not ready (unhealthy)", exception.message
|
||||
end
|
||||
|
||||
private
|
||||
def run_command(*command)
|
||||
stdouted { Mrsk::Cli::Healthcheck.start([*command, "-c", "test/fixtures/deploy_with_accessories.yml"]) }
|
||||
end
|
||||
end
|
||||
20
test/cli/lock_test.rb
Normal file
20
test/cli/lock_test.rb
Normal file
@@ -0,0 +1,20 @@
|
||||
require_relative "cli_test_case"
|
||||
|
||||
class CliLockTest < CliTestCase
|
||||
test "status" do
|
||||
run_command("status") do |output|
|
||||
assert_match "stat lock", output
|
||||
end
|
||||
end
|
||||
|
||||
test "release" do
|
||||
run_command("release") do |output|
|
||||
assert_match "rm -rf lock", output
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def run_command(*command)
|
||||
stdouted { Mrsk::Cli::Lock.start([*command, "-c", "test/fixtures/deploy_with_accessories.yml"]) }
|
||||
end
|
||||
end
|
||||
@@ -1,32 +1,304 @@
|
||||
require_relative "cli_test_case"
|
||||
|
||||
class CliMainTest < CliTestCase
|
||||
test "version" do
|
||||
version = stdouted { Mrsk::Cli::Main.new.version }
|
||||
assert_equal Mrsk::VERSION, version
|
||||
test "setup" do
|
||||
Mrsk::Cli::Main.any_instance.expects(:invoke).with("mrsk:cli:server:bootstrap")
|
||||
Mrsk::Cli::Main.any_instance.expects(:invoke).with("mrsk:cli:accessory:boot", [ "all" ])
|
||||
Mrsk::Cli::Main.any_instance.expects(:deploy)
|
||||
|
||||
run_command("setup")
|
||||
end
|
||||
|
||||
test "deploy" do
|
||||
invoke_options = { "config_file" => "test/fixtures/deploy_simple.yml", "skip_broadcast" => false, "version" => "999" }
|
||||
|
||||
Mrsk::Cli::Main.any_instance.expects(:invoke).with("mrsk:cli:registry:login", [], invoke_options)
|
||||
Mrsk::Cli::Main.any_instance.expects(:invoke).with("mrsk:cli:build:deliver", [], invoke_options)
|
||||
Mrsk::Cli::Main.any_instance.expects(:invoke).with("mrsk:cli:traefik:boot", [], invoke_options)
|
||||
Mrsk::Cli::Main.any_instance.expects(:invoke).with("mrsk:cli:healthcheck:perform", [], invoke_options)
|
||||
Mrsk::Cli::Main.any_instance.expects(:invoke).with("mrsk:cli:app:stale_containers", [], invoke_options)
|
||||
Mrsk::Cli::Main.any_instance.expects(:invoke).with("mrsk:cli:app:boot", [], invoke_options)
|
||||
Mrsk::Cli::Main.any_instance.expects(:invoke).with("mrsk:cli:prune:all", [], invoke_options)
|
||||
|
||||
run_command("deploy").tap do |output|
|
||||
assert_match /Log into image registry/, output
|
||||
assert_match /Build and push app image/, output
|
||||
assert_match /Ensure Traefik is running/, output
|
||||
assert_match /Ensure app can pass healthcheck/, output
|
||||
assert_match /Detect stale containers/, output
|
||||
assert_match /Prune old containers and images/, output
|
||||
end
|
||||
end
|
||||
|
||||
test "deploy with skip_push" do
|
||||
invoke_options = { "config_file" => "test/fixtures/deploy_simple.yml", "skip_broadcast" => false, "version" => "999" }
|
||||
|
||||
Mrsk::Cli::Main.any_instance.expects(:invoke).with("mrsk:cli:registry:login", [], invoke_options)
|
||||
Mrsk::Cli::Main.any_instance.expects(:invoke).with("mrsk:cli:build:pull", [], invoke_options)
|
||||
Mrsk::Cli::Main.any_instance.expects(:invoke).with("mrsk:cli:traefik:boot", [], invoke_options)
|
||||
Mrsk::Cli::Main.any_instance.expects(:invoke).with("mrsk:cli:healthcheck:perform", [], invoke_options)
|
||||
Mrsk::Cli::Main.any_instance.expects(:invoke).with("mrsk:cli:app:stale_containers", [], invoke_options)
|
||||
Mrsk::Cli::Main.any_instance.expects(:invoke).with("mrsk:cli:app:boot", [], invoke_options)
|
||||
Mrsk::Cli::Main.any_instance.expects(:invoke).with("mrsk:cli:prune:all", [], invoke_options)
|
||||
|
||||
run_command("deploy", "--skip_push").tap do |output|
|
||||
assert_match /Acquiring the deploy lock/, output
|
||||
assert_match /Log into image registry/, output
|
||||
assert_match /Pull app image/, output
|
||||
assert_match /Ensure Traefik is running/, output
|
||||
assert_match /Ensure app can pass healthcheck/, output
|
||||
assert_match /Detect stale containers/, output
|
||||
assert_match /Prune old containers and images/, output
|
||||
assert_match /Releasing the deploy lock/, output
|
||||
end
|
||||
end
|
||||
|
||||
test "deploy when locked" do
|
||||
Thread.report_on_exception = false
|
||||
|
||||
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
||||
.with { |*arg| arg[0..1] == [:mkdir, :mrsk_lock] }
|
||||
.raises(RuntimeError, "mkdir: cannot create directory ‘mrsk_lock’: File exists")
|
||||
|
||||
SSHKit::Backend::Abstract.any_instance.expects(:execute)
|
||||
.with(:stat, :mrsk_lock, ">", "/dev/null", "&&", :cat, "mrsk_lock/details", "|", :base64, "-d")
|
||||
|
||||
assert_raises(Mrsk::Cli::LockError) do
|
||||
run_command("deploy")
|
||||
end
|
||||
end
|
||||
|
||||
test "deploy error when locking" do
|
||||
Thread.report_on_exception = false
|
||||
|
||||
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
||||
.with { |*arg| arg[0..1] == [:mkdir, :mrsk_lock] }
|
||||
.raises(SocketError, "getaddrinfo: nodename nor servname provided, or not known")
|
||||
|
||||
assert_raises(SSHKit::Runner::ExecuteError) do
|
||||
run_command("deploy")
|
||||
end
|
||||
end
|
||||
|
||||
test "deploy errors during critical section leave lock in place" do
|
||||
invoke_options = { "config_file" => "test/fixtures/deploy_simple.yml", "skip_broadcast" => false, "version" => "999" }
|
||||
|
||||
Mrsk::Cli::Main.any_instance.expects(:invoke).with("mrsk:cli:registry:login", [], invoke_options)
|
||||
Mrsk::Cli::Main.any_instance.expects(:invoke).with("mrsk:cli:build:deliver", [], invoke_options)
|
||||
Mrsk::Cli::Main.any_instance.expects(:invoke).with("mrsk:cli:app:stale_containers", [], invoke_options)
|
||||
Mrsk::Cli::Main.any_instance.expects(:invoke).with("mrsk:cli:traefik:boot", [], invoke_options)
|
||||
Mrsk::Cli::Main.any_instance.expects(:invoke).with("mrsk:cli:healthcheck:perform", [], invoke_options)
|
||||
Mrsk::Cli::Main.any_instance.expects(:invoke).with("mrsk:cli:app:boot", [], invoke_options).raises(RuntimeError)
|
||||
|
||||
assert !MRSK.holding_lock?
|
||||
assert_raises(RuntimeError) do
|
||||
stderred { run_command("deploy") }
|
||||
end
|
||||
assert MRSK.holding_lock?
|
||||
end
|
||||
|
||||
test "deploy errors during outside section leave remove lock" do
|
||||
invoke_options = { "config_file" => "test/fixtures/deploy_simple.yml", "skip_broadcast" => false, "version" => "999" }
|
||||
|
||||
Mrsk::Cli::Main.any_instance.expects(:invoke)
|
||||
.with("mrsk:cli:registry:login", [], invoke_options)
|
||||
.raises(RuntimeError)
|
||||
|
||||
assert !MRSK.holding_lock?
|
||||
assert_raises(RuntimeError) do
|
||||
stderred { run_command("deploy") }
|
||||
end
|
||||
assert !MRSK.holding_lock?
|
||||
end
|
||||
|
||||
test "redeploy" do
|
||||
invoke_options = { "config_file" => "test/fixtures/deploy_simple.yml", "skip_broadcast" => false, "version" => "999" }
|
||||
|
||||
Mrsk::Cli::Main.any_instance.expects(:invoke).with("mrsk:cli:build:deliver", [], invoke_options)
|
||||
Mrsk::Cli::Main.any_instance.expects(:invoke).with("mrsk:cli:healthcheck:perform", [], invoke_options)
|
||||
Mrsk::Cli::Main.any_instance.expects(:invoke).with("mrsk:cli:app:stale_containers", [], invoke_options)
|
||||
Mrsk::Cli::Main.any_instance.expects(:invoke).with("mrsk:cli:app:boot", [], invoke_options)
|
||||
|
||||
run_command("redeploy").tap do |output|
|
||||
assert_match /Build and push app image/, output
|
||||
assert_match /Ensure app can pass healthcheck/, output
|
||||
end
|
||||
end
|
||||
|
||||
test "redeploy with skip_push" do
|
||||
invoke_options = { "config_file" => "test/fixtures/deploy_simple.yml", "skip_broadcast" => false, "version" => "999" }
|
||||
|
||||
Mrsk::Cli::Main.any_instance.expects(:invoke).with("mrsk:cli:build:pull", [], invoke_options)
|
||||
Mrsk::Cli::Main.any_instance.expects(:invoke).with("mrsk:cli:healthcheck:perform", [], invoke_options)
|
||||
Mrsk::Cli::Main.any_instance.expects(:invoke).with("mrsk:cli:app:stale_containers", [], invoke_options)
|
||||
Mrsk::Cli::Main.any_instance.expects(:invoke).with("mrsk:cli:app:boot", [], invoke_options)
|
||||
|
||||
run_command("redeploy", "--skip_push").tap do |output|
|
||||
assert_match /Pull app image/, output
|
||||
assert_match /Ensure app can pass healthcheck/, output
|
||||
end
|
||||
end
|
||||
|
||||
test "rollback bad version" do
|
||||
Thread.report_on_exception = false
|
||||
|
||||
run_command("details") # Preheat MRSK const
|
||||
|
||||
run_command("rollback", "nonsense").tap do |output|
|
||||
assert_match /docker container ls --all --filter label=service=app --format '{{ .Names }}'/, output
|
||||
assert_match /docker container ls --all --filter name=\^app-web-nonsense\$ --quiet/, output
|
||||
assert_match /The app version 'nonsense' is not available as a container/, output
|
||||
end
|
||||
end
|
||||
|
||||
test "rollback good version" do
|
||||
Mrsk::Cli::Main.any_instance.stubs(:container_name_available?).returns(true)
|
||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-web-123$", "--quiet")
|
||||
.returns("version-to-rollback\n").at_least_once
|
||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-workers-123$", "--quiet")
|
||||
.returns("version-to-rollback\n").at_least_once
|
||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
||||
.with(:docker, :ps, "--filter", "label=service=app", "--filter", "label=role=web", "--filter", "status=running", "--latest", "--format", "\"{{.Names}}\"", "|", "grep -oE \"\\-[^-]+$\"", "|", "cut -c 2-")
|
||||
.returns("version-to-rollback\n").at_least_once
|
||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
||||
.with(:docker, :ps, "--filter", "label=service=app", "--filter", "label=role=workers", "--filter", "status=running", "--latest", "--format", "\"{{.Names}}\"", "|", "grep -oE \"\\-[^-]+$\"", "|", "cut -c 2-")
|
||||
.returns("version-to-rollback\n").at_least_once
|
||||
|
||||
run_command("rollback", "123").tap do |output|
|
||||
assert_match /Start version 123/, output
|
||||
assert_match /docker ps -q --filter label=service=app | xargs docker stop/, output
|
||||
assert_match /docker start app-123/, output
|
||||
|
||||
run_command("rollback", "123", config_file: "deploy_with_accessories").tap do |output|
|
||||
assert_match "Start version 123", output
|
||||
assert_match "docker tag dhh/app:123 dhh/app:latest", output
|
||||
assert_match "docker start app-web-123", output
|
||||
assert_match "docker container ls --all --filter name=^app-web-version-to-rollback$ --quiet | xargs docker stop", output, "Should stop the container that was previously running"
|
||||
end
|
||||
end
|
||||
|
||||
test "rollback without old version" do
|
||||
Mrsk::Cli::Main.any_instance.stubs(:container_available?).returns(true)
|
||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info).with(:docker, :ps, "--filter", "label=service=app", "--filter", "label=role=web", "--filter", "status=running", "--latest", "--format", "\"{{.Names}}\"", "|", "grep -oE \"\\-[^-]+$\"", "|", "cut -c 2-").returns("").at_least_once
|
||||
|
||||
run_command("rollback", "123").tap do |output|
|
||||
assert_match "Start version 123", output
|
||||
assert_match "docker start app-web-123", output
|
||||
assert_no_match "docker stop", output
|
||||
end
|
||||
end
|
||||
|
||||
test "details" do
|
||||
Mrsk::Cli::Main.any_instance.expects(:invoke).with("mrsk:cli:traefik:details")
|
||||
Mrsk::Cli::Main.any_instance.expects(:invoke).with("mrsk:cli:app:details")
|
||||
Mrsk::Cli::Main.any_instance.expects(:invoke).with("mrsk:cli:accessory:details", [ "all" ])
|
||||
|
||||
run_command("details")
|
||||
end
|
||||
|
||||
test "audit" do
|
||||
run_command("audit").tap do |output|
|
||||
assert_match /tail -n 50 mrsk-app-audit.log on 1.1.1.1/, output
|
||||
assert_match /App Host: 1.1.1.1/, output
|
||||
end
|
||||
end
|
||||
|
||||
test "config" do
|
||||
run_command("config", config_file: "deploy_simple").tap do |output|
|
||||
config = YAML.load(output)
|
||||
|
||||
assert_equal ["web"], config[:roles]
|
||||
assert_equal ["1.1.1.1", "1.1.1.2"], config[:hosts]
|
||||
assert_equal "999", config[:version]
|
||||
assert_equal "dhh/app", config[:repository]
|
||||
assert_equal "dhh/app:999", config[:absolute_image]
|
||||
assert_equal "app-999", config[:service_with_version]
|
||||
end
|
||||
end
|
||||
|
||||
test "config with roles" do
|
||||
run_command("config", config_file: "deploy_with_roles").tap do |output|
|
||||
config = YAML.load(output)
|
||||
|
||||
assert_equal ["web", "workers"], config[:roles]
|
||||
assert_equal ["1.1.1.1", "1.1.1.2", "1.1.1.3", "1.1.1.4"], config[:hosts]
|
||||
assert_equal "999", config[:version]
|
||||
assert_equal "registry.digitalocean.com/dhh/app", config[:repository]
|
||||
assert_equal "registry.digitalocean.com/dhh/app:999", config[:absolute_image]
|
||||
assert_equal "app-999", config[:service_with_version]
|
||||
end
|
||||
end
|
||||
|
||||
test "config with destination" do
|
||||
run_command("config", "-d", "world", config_file: "deploy_for_dest").tap do |output|
|
||||
config = YAML.load(output)
|
||||
|
||||
assert_equal ["web"], config[:roles]
|
||||
assert_equal ["1.1.1.1", "1.1.1.2"], config[:hosts]
|
||||
assert_equal "999", config[:version]
|
||||
assert_equal "registry.digitalocean.com/dhh/app", config[:repository]
|
||||
assert_equal "registry.digitalocean.com/dhh/app:999", config[:absolute_image]
|
||||
assert_equal "app-999", config[:service_with_version]
|
||||
end
|
||||
end
|
||||
|
||||
test "init" do
|
||||
Pathname.any_instance.expects(:exist?).returns(false).twice
|
||||
FileUtils.stubs(:mkdir_p)
|
||||
FileUtils.stubs(:cp_r)
|
||||
|
||||
run_command("init").tap do |output|
|
||||
assert_match /Created configuration file in config\/deploy.yml/, output
|
||||
assert_match /Created \.env file/, output
|
||||
end
|
||||
end
|
||||
|
||||
test "init with existing config" do
|
||||
Pathname.any_instance.expects(:exist?).returns(true).twice
|
||||
|
||||
run_command("init").tap do |output|
|
||||
assert_match /Config file already exists in config\/deploy.yml \(remove first to create a new one\)/, output
|
||||
end
|
||||
end
|
||||
|
||||
test "init with bundle option" do
|
||||
Pathname.any_instance.expects(:exist?).returns(false).times(3)
|
||||
FileUtils.stubs(:mkdir_p)
|
||||
FileUtils.stubs(:cp_r)
|
||||
|
||||
run_command("init", "--bundle").tap do |output|
|
||||
assert_match /Created configuration file in config\/deploy.yml/, output
|
||||
assert_match /Created \.env file/, output
|
||||
assert_match /Adding MRSK to Gemfile and bundle/, output
|
||||
assert_match /bundle add mrsk/, output
|
||||
assert_match /bundle binstubs mrsk/, output
|
||||
assert_match /Created binstub file in bin\/mrsk/, output
|
||||
end
|
||||
end
|
||||
|
||||
test "init with bundle option and existing binstub" do
|
||||
Pathname.any_instance.expects(:exist?).returns(true).times(3)
|
||||
FileUtils.stubs(:mkdir_p)
|
||||
FileUtils.stubs(:cp_r)
|
||||
|
||||
run_command("init", "--bundle").tap do |output|
|
||||
assert_match /Config file already exists in config\/deploy.yml \(remove first to create a new one\)/, output
|
||||
assert_match /Binstub already exists in bin\/mrsk \(remove first to create a new one\)/, output
|
||||
end
|
||||
end
|
||||
|
||||
test "envify" do
|
||||
File.expects(:read).with(".env.erb").returns("HELLO=<%= 'world' %>")
|
||||
File.expects(:write).with(".env", "HELLO=world", perm: 0600)
|
||||
|
||||
run_command("envify")
|
||||
end
|
||||
|
||||
test "envify with destination" do
|
||||
File.expects(:read).with(".env.staging.erb").returns("HELLO=<%= 'world' %>")
|
||||
File.expects(:write).with(".env.staging", "HELLO=world", perm: 0600)
|
||||
|
||||
run_command("envify", "-d", "staging")
|
||||
end
|
||||
|
||||
test "remove with confirmation" do
|
||||
run_command("remove", "-y").tap do |output|
|
||||
run_command("remove", "-y", config_file: "deploy_with_accessories").tap do |output|
|
||||
assert_match /docker container stop traefik/, output
|
||||
assert_match /docker container prune --force --filter label=org.opencontainers.image.title=Traefik/, output
|
||||
assert_match /docker image prune --all --force --filter label=org.opencontainers.image.title=Traefik/, output
|
||||
@@ -37,20 +309,38 @@ class CliMainTest < CliTestCase
|
||||
|
||||
assert_match /docker container stop app-mysql/, output
|
||||
assert_match /docker container prune --force --filter label=service=app-mysql/, output
|
||||
assert_match /docker image prune --all --force --filter label=service=app-mysql/, output
|
||||
assert_match /docker image rm --force mysql/, output
|
||||
assert_match /rm -rf app-mysql/, output
|
||||
|
||||
assert_match /docker container stop app-redis/, output
|
||||
assert_match /docker container prune --force --filter label=service=app-redis/, output
|
||||
assert_match /docker image prune --all --force --filter label=service=app-redis/, output
|
||||
assert_match /docker image rm --force redis/, output
|
||||
assert_match /rm -rf app-redis/, output
|
||||
|
||||
assert_match /docker logout/, output
|
||||
end
|
||||
end
|
||||
|
||||
test "broadcast" do
|
||||
SSHKit::Backend::Abstract.any_instance.expects(:execute).with do |command, line, options, verbosity:|
|
||||
command == "bin/audit_broadcast" &&
|
||||
line =~ /\A'\[[^\]]+\] message'\z/ &&
|
||||
options[:env].keys == %w[ MRSK_RECORDED_AT MRSK_PERFORMER MRSK_EVENT ] &&
|
||||
verbosity == :debug
|
||||
end.returns("Broadcast audit message: message")
|
||||
|
||||
run_command("broadcast", "-m", "message").tap do |output|
|
||||
assert_match "Broadcast: message", output
|
||||
end
|
||||
end
|
||||
|
||||
test "version" do
|
||||
version = stdouted { Mrsk::Cli::Main.new.version }
|
||||
assert_equal Mrsk::VERSION, version
|
||||
end
|
||||
|
||||
private
|
||||
def run_command(*command)
|
||||
stdouted { Mrsk::Cli::Main.start([*command, "-c", "test/fixtures/deploy_with_accessories.yml"]) }
|
||||
def run_command(*command, config_file: "deploy_simple")
|
||||
stdouted { Mrsk::Cli::Main.start([*command, "-c", "test/fixtures/#{config_file}.yml"]) }
|
||||
end
|
||||
end
|
||||
|
||||
27
test/cli/prune_test.rb
Normal file
27
test/cli/prune_test.rb
Normal file
@@ -0,0 +1,27 @@
|
||||
require_relative "cli_test_case"
|
||||
|
||||
class CliPruneTest < CliTestCase
|
||||
test "all" do
|
||||
Mrsk::Cli::Prune.any_instance.expects(:containers)
|
||||
Mrsk::Cli::Prune.any_instance.expects(:images)
|
||||
|
||||
run_command("all")
|
||||
end
|
||||
|
||||
test "images" do
|
||||
run_command("images").tap do |output|
|
||||
assert_match /docker image prune --force --filter label=service=app --filter dangling=true on 1.1.1.\d/, output
|
||||
end
|
||||
end
|
||||
|
||||
test "containers" do
|
||||
run_command("containers").tap do |output|
|
||||
assert_match /docker ps -q -a --filter label=service=app --filter status=created --filter status=exited --filter status=dead | tail -n +6 | while read container_id; do docker rm $container_id; done on 1.1.1.\d/, output
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def run_command(*command)
|
||||
stdouted { Mrsk::Cli::Prune.start([*command, "-c", "test/fixtures/deploy_with_accessories.yml"]) }
|
||||
end
|
||||
end
|
||||
21
test/cli/registry_test.rb
Normal file
21
test/cli/registry_test.rb
Normal file
@@ -0,0 +1,21 @@
|
||||
require_relative "cli_test_case"
|
||||
|
||||
class CliRegistryTest < CliTestCase
|
||||
test "login" do
|
||||
run_command("login").tap do |output|
|
||||
assert_match /docker login -u \[REDACTED\] -p \[REDACTED\] as .*@localhost/, output
|
||||
assert_match /docker login -u \[REDACTED\] -p \[REDACTED\] on 1.1.1.\d/, output
|
||||
end
|
||||
end
|
||||
|
||||
test "logout" do
|
||||
run_command("logout").tap do |output|
|
||||
assert_match /docker logout on 1.1.1.\d/, output
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def run_command(*command)
|
||||
stdouted { Mrsk::Cli::Registry.start([*command, "-c", "test/fixtures/deploy_with_accessories.yml"]) }
|
||||
end
|
||||
end
|
||||
@@ -1,11 +1,30 @@
|
||||
require_relative "cli_test_case"
|
||||
|
||||
class CliServerTest < CliTestCase
|
||||
test "bootstrap" do
|
||||
test "bootstrap already installed" do
|
||||
SSHKit::Backend::Abstract.any_instance.expects(:execute).with(:docker, "-v", raise_on_non_zero_exit: false).returns(true).at_least_once
|
||||
|
||||
assert_equal "", run_command("bootstrap")
|
||||
end
|
||||
|
||||
test "bootstrap install as non-root user" do
|
||||
SSHKit::Backend::Abstract.any_instance.expects(:execute).with(:docker, "-v", raise_on_non_zero_exit: false).returns(false).at_least_once
|
||||
SSHKit::Backend::Abstract.any_instance.expects(:execute).with('[ "${EUID:-$(id -u)}" -eq 0 ]', raise_on_non_zero_exit: false).returns(false).at_least_once
|
||||
|
||||
assert_raise RuntimeError, "Docker is not installed on 1.1.1.1, 1.1.1.3, 1.1.1.4, 1.1.1.2 and can't be automatically intalled without having root access and the `curl` command available. Install Docker manually: https://docs.docker.com/engine/install/" do
|
||||
run_command("bootstrap")
|
||||
end
|
||||
end
|
||||
|
||||
test "bootstrap install as root user" do
|
||||
SSHKit::Backend::Abstract.any_instance.expects(:execute).with(:docker, "-v", raise_on_non_zero_exit: false).returns(false).at_least_once
|
||||
SSHKit::Backend::Abstract.any_instance.expects(:execute).with('[ "${EUID:-$(id -u)}" -eq 0 ]', raise_on_non_zero_exit: false).returns(true).at_least_once
|
||||
SSHKit::Backend::Abstract.any_instance.expects(:execute).with(:curl, "-fsSL", "https://get.docker.com", "|", :sh).at_least_once
|
||||
|
||||
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
|
||||
("1.1.1.1".."1.1.1.4").map do |host|
|
||||
assert_match "Missing Docker on #{host}. Installing…", output
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
86
test/cli/traefik_test.rb
Normal file
86
test/cli/traefik_test.rb
Normal file
@@ -0,0 +1,86 @@
|
||||
require_relative "cli_test_case"
|
||||
|
||||
class CliTraefikTest < CliTestCase
|
||||
test "boot" do
|
||||
run_command("boot").tap do |output|
|
||||
assert_match "docker login", output
|
||||
assert_match "docker run --name traefik --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --log-opt max-size=\"10m\" #{Mrsk::Commands::Traefik::DEFAULT_IMAGE} --providers.docker --log.level=DEBUG", output
|
||||
end
|
||||
end
|
||||
|
||||
test "reboot" do
|
||||
Mrsk::Cli::Traefik.any_instance.expects(:stop)
|
||||
Mrsk::Cli::Traefik.any_instance.expects(:remove_container)
|
||||
Mrsk::Cli::Traefik.any_instance.expects(:boot)
|
||||
|
||||
run_command("reboot")
|
||||
end
|
||||
|
||||
test "start" do
|
||||
run_command("start").tap do |output|
|
||||
assert_match "docker container start traefik", output
|
||||
end
|
||||
end
|
||||
|
||||
test "stop" do
|
||||
run_command("stop").tap do |output|
|
||||
assert_match "docker container stop traefik", output
|
||||
end
|
||||
end
|
||||
|
||||
test "restart" do
|
||||
Mrsk::Cli::Traefik.any_instance.expects(:stop)
|
||||
Mrsk::Cli::Traefik.any_instance.expects(:start)
|
||||
|
||||
run_command("restart")
|
||||
end
|
||||
|
||||
test "details" do
|
||||
run_command("details").tap do |output|
|
||||
assert_match "docker ps --filter name=^traefik$", output
|
||||
end
|
||||
end
|
||||
|
||||
test "logs" do
|
||||
SSHKit::Backend::Abstract.any_instance.stubs(:capture)
|
||||
.with(:docker, :logs, "traefik", " --tail 100", "--timestamps", "2>&1")
|
||||
.returns("Log entry")
|
||||
|
||||
run_command("logs").tap do |output|
|
||||
assert_match "Traefik Host: 1.1.1.1", output
|
||||
assert_match "Log entry", output
|
||||
end
|
||||
end
|
||||
|
||||
test "logs with follow" do
|
||||
SSHKit::Backend::Abstract.any_instance.stubs(:exec)
|
||||
.with("ssh -t root@1.1.1.1 'docker logs traefik --timestamps --tail 10 --follow 2>&1'")
|
||||
|
||||
assert_match "docker logs traefik --timestamps --tail 10 --follow", run_command("logs", "--follow")
|
||||
end
|
||||
|
||||
test "remove" do
|
||||
Mrsk::Cli::Traefik.any_instance.expects(:stop)
|
||||
Mrsk::Cli::Traefik.any_instance.expects(:remove_container)
|
||||
Mrsk::Cli::Traefik.any_instance.expects(:remove_image)
|
||||
|
||||
run_command("remove")
|
||||
end
|
||||
|
||||
test "remove_container" do
|
||||
run_command("remove_container").tap do |output|
|
||||
assert_match "docker container prune --force --filter label=org.opencontainers.image.title=Traefik", output
|
||||
end
|
||||
end
|
||||
|
||||
test "remove_image" do
|
||||
run_command("remove_image").tap do |output|
|
||||
assert_match "docker image prune --all --force --filter label=org.opencontainers.image.title=Traefik", output
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def run_command(*command)
|
||||
stdouted { Mrsk::Cli::Traefik.start([*command, "-c", "test/fixtures/deploy_with_accessories.yml"]) }
|
||||
end
|
||||
end
|
||||
@@ -2,38 +2,39 @@ require "test_helper"
|
||||
|
||||
class CommanderTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@mrsk = Mrsk::Commander.new config_file: Pathname.new(File.expand_path("fixtures/deploy_with_roles.yml", __dir__))
|
||||
configure_with(:deploy_with_roles)
|
||||
end
|
||||
|
||||
test "lazy configuration" do
|
||||
assert_equal Mrsk::Configuration, @mrsk.config.class
|
||||
end
|
||||
|
||||
test "commit hash as version" do
|
||||
assert_equal `git rev-parse HEAD`.strip, @mrsk.config.version
|
||||
end
|
||||
|
||||
test "commit hash as version but not in git" do
|
||||
@mrsk.expects(:system).with("git rev-parse").returns(nil)
|
||||
error = assert_raises(RuntimeError) { @mrsk.config }
|
||||
assert_match /no git repository found/, error.message
|
||||
end
|
||||
|
||||
test "overwriting hosts" do
|
||||
assert_equal [ "1.1.1.1", "1.1.1.2", "1.1.1.3", "1.1.1.4" ], @mrsk.hosts
|
||||
|
||||
@mrsk.specific_hosts = [ "1.2.3.4", "1.2.3.5" ]
|
||||
assert_equal [ "1.2.3.4", "1.2.3.5" ], @mrsk.hosts
|
||||
@mrsk.specific_hosts = [ "1.1.1.1", "1.1.1.2" ]
|
||||
assert_equal [ "1.1.1.1", "1.1.1.2" ], @mrsk.hosts
|
||||
end
|
||||
|
||||
test "overwriting hosts with roles" do
|
||||
test "filtering hosts by filtering roles" do
|
||||
assert_equal [ "1.1.1.1", "1.1.1.2", "1.1.1.3", "1.1.1.4" ], @mrsk.hosts
|
||||
|
||||
@mrsk.specific_roles = [ "workers", "web" ]
|
||||
assert_equal [ "1.1.1.1", "1.1.1.2", "1.1.1.3", "1.1.1.4" ], @mrsk.hosts
|
||||
@mrsk.specific_roles = [ "web" ]
|
||||
assert_equal [ "1.1.1.1", "1.1.1.2" ], @mrsk.hosts
|
||||
end
|
||||
|
||||
test "filtering roles" do
|
||||
assert_equal [ "web", "workers" ], @mrsk.roles.map(&:name)
|
||||
|
||||
@mrsk.specific_roles = [ "workers" ]
|
||||
assert_equal [ "1.1.1.3", "1.1.1.4" ], @mrsk.hosts
|
||||
assert_equal [ "workers" ], @mrsk.roles.map(&:name)
|
||||
end
|
||||
|
||||
test "filtering roles by filtering hosts" do
|
||||
assert_equal [ "web", "workers" ], @mrsk.roles.map(&:name)
|
||||
|
||||
@mrsk.specific_hosts = [ "1.1.1.3" ]
|
||||
assert_equal [ "workers" ], @mrsk.roles.map(&:name)
|
||||
end
|
||||
|
||||
test "overwriting hosts with primary" do
|
||||
@@ -44,7 +45,35 @@ class CommanderTest < ActiveSupport::TestCase
|
||||
end
|
||||
|
||||
test "primary_host with specific hosts via role" do
|
||||
@mrsk.specific_roles = "web"
|
||||
assert_equal "1.1.1.1", @mrsk.primary_host
|
||||
@mrsk.specific_roles = "workers"
|
||||
assert_equal "1.1.1.3", @mrsk.primary_host
|
||||
end
|
||||
|
||||
test "roles_on" do
|
||||
assert_equal [ "web" ], @mrsk.roles_on("1.1.1.1")
|
||||
assert_equal [ "workers" ], @mrsk.roles_on("1.1.1.3")
|
||||
end
|
||||
|
||||
test "default group strategy" do
|
||||
assert_empty @mrsk.boot_strategy
|
||||
end
|
||||
|
||||
test "specific limit group strategy" do
|
||||
configure_with(:deploy_with_boot_strategy)
|
||||
|
||||
assert_equal({ in: :groups, limit: 3, wait: 2 }, @mrsk.boot_strategy)
|
||||
end
|
||||
|
||||
test "percentage-based group strategy" do
|
||||
configure_with(:deploy_with_precentage_boot_strategy)
|
||||
|
||||
assert_equal({ in: :groups, limit: 1, wait: 2 }, @mrsk.boot_strategy)
|
||||
end
|
||||
|
||||
private
|
||||
def configure_with(variant)
|
||||
@mrsk = Mrsk::Commander.new.tap do |mrsk|
|
||||
mrsk.configure config_file: Pathname.new(File.expand_path("fixtures/#{variant}.yml", __dir__))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,11 +3,11 @@ require "test_helper"
|
||||
class CommandsAccessoryTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@config = {
|
||||
service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" },
|
||||
service: "app", image: "dhh/app", registry: { "server" => "private.registry", "username" => "dhh", "password" => "secret" },
|
||||
servers: [ "1.1.1.1" ],
|
||||
accessories: {
|
||||
"mysql" => {
|
||||
"image" => "mysql:8.0",
|
||||
"image" => "private.registry/mysql:8.0",
|
||||
"host" => "1.1.1.5",
|
||||
"port" => "3306",
|
||||
"env" => {
|
||||
@@ -32,14 +32,14 @@ class CommandsAccessoryTest < ActiveSupport::TestCase
|
||||
"volumes" => [
|
||||
"/var/lib/redis:/data"
|
||||
]
|
||||
},
|
||||
"busybox" => {
|
||||
"image" => "busybox:latest",
|
||||
"host" => "1.1.1.7"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@config = Mrsk::Configuration.new(@config)
|
||||
@mysql = Mrsk::Commands::Accessory.new(@config, name: :mysql)
|
||||
@redis = Mrsk::Commands::Accessory.new(@config, name: :redis)
|
||||
|
||||
ENV["MYSQL_ROOT_PASSWORD"] = "secret123"
|
||||
end
|
||||
|
||||
@@ -49,56 +49,68 @@ class CommandsAccessoryTest < ActiveSupport::TestCase
|
||||
|
||||
test "run" do
|
||||
assert_equal \
|
||||
"docker run --name app-mysql --detach --restart unless-stopped --log-opt max-size=10m --publish 3306:3306 -e MYSQL_ROOT_PASSWORD=\"secret123\" -e MYSQL_ROOT_HOST=\"%\" --label service=\"app-mysql\" mysql:8.0",
|
||||
@mysql.run.join(" ")
|
||||
"docker run --name app-mysql --detach --restart unless-stopped --log-opt max-size=\"10m\" --publish 3306:3306 -e MYSQL_ROOT_PASSWORD=\"secret123\" -e MYSQL_ROOT_HOST=\"%\" --label service=\"app-mysql\" private.registry/mysql:8.0",
|
||||
new_command(:mysql).run.join(" ")
|
||||
|
||||
assert_equal \
|
||||
"docker run --name app-redis --detach --restart unless-stopped --log-opt max-size=10m --publish 6379:6379 -e SOMETHING=\"else\" --volume /var/lib/redis:/data --label service=\"app-redis\" --label cache=\"true\" redis:latest",
|
||||
@redis.run.join(" ")
|
||||
"docker run --name app-redis --detach --restart unless-stopped --log-opt max-size=\"10m\" --publish 6379:6379 -e SOMETHING=\"else\" --volume /var/lib/redis:/data --label service=\"app-redis\" --label cache=\"true\" redis:latest",
|
||||
new_command(:redis).run.join(" ")
|
||||
|
||||
assert_equal \
|
||||
"docker run --name app-busybox --detach --restart unless-stopped --log-opt max-size=\"10m\" --label service=\"app-busybox\" busybox:latest",
|
||||
new_command(:busybox).run.join(" ")
|
||||
end
|
||||
|
||||
test "run with logging config" do
|
||||
@config[:logging] = { "driver" => "local", "options" => { "max-size" => "100m", "max-file" => "3" } }
|
||||
|
||||
assert_equal \
|
||||
"docker run --name app-busybox --detach --restart unless-stopped --log-driver \"local\" --log-opt max-size=\"100m\" --log-opt max-file=\"3\" --label service=\"app-busybox\" busybox:latest",
|
||||
new_command(:busybox).run.join(" ")
|
||||
end
|
||||
|
||||
test "start" do
|
||||
assert_equal \
|
||||
"docker container start app-mysql",
|
||||
@mysql.start.join(" ")
|
||||
new_command(:mysql).start.join(" ")
|
||||
end
|
||||
|
||||
test "stop" do
|
||||
assert_equal \
|
||||
"docker container stop app-mysql",
|
||||
@mysql.stop.join(" ")
|
||||
new_command(:mysql).stop.join(" ")
|
||||
end
|
||||
|
||||
test "info" do
|
||||
assert_equal \
|
||||
"docker ps --filter label=service=app-mysql",
|
||||
@mysql.info.join(" ")
|
||||
new_command(:mysql).info.join(" ")
|
||||
end
|
||||
|
||||
|
||||
test "execute in new container" do
|
||||
assert_equal \
|
||||
"docker run --rm -e MYSQL_ROOT_PASSWORD=\"secret123\" -e MYSQL_ROOT_HOST=\"%\" mysql:8.0 mysql -u root",
|
||||
@mysql.execute_in_new_container("mysql", "-u", "root").join(" ")
|
||||
"docker run --rm -e MYSQL_ROOT_PASSWORD=\"secret123\" -e MYSQL_ROOT_HOST=\"%\" private.registry/mysql:8.0 mysql -u root",
|
||||
new_command(:mysql).execute_in_new_container("mysql", "-u", "root").join(" ")
|
||||
end
|
||||
|
||||
test "execute in existing container" do
|
||||
assert_equal \
|
||||
"docker exec app-mysql mysql -u root",
|
||||
@mysql.execute_in_existing_container("mysql", "-u", "root").join(" ")
|
||||
new_command(:mysql).execute_in_existing_container("mysql", "-u", "root").join(" ")
|
||||
end
|
||||
|
||||
test "execute in new container over ssh" do
|
||||
@mysql.stub(:run_over_ssh, ->(cmd) { cmd.join(" ") }) do
|
||||
assert_match %r|docker run -it --rm -e MYSQL_ROOT_PASSWORD=\"secret123\" -e MYSQL_ROOT_HOST=\"%\" mysql:8.0 mysql -u root|,
|
||||
@mysql.execute_in_new_container_over_ssh("mysql", "-u", "root")
|
||||
new_command(:mysql).stub(:run_over_ssh, ->(cmd) { cmd.join(" ") }) do
|
||||
assert_match %r|docker run -it --rm -e MYSQL_ROOT_PASSWORD=\"secret123\" -e MYSQL_ROOT_HOST=\"%\" private.registry/mysql:8.0 mysql -u root|,
|
||||
new_command(:mysql).execute_in_new_container_over_ssh("mysql", "-u", "root")
|
||||
end
|
||||
end
|
||||
|
||||
test "execute in existing container over ssh" do
|
||||
@mysql.stub(:run_over_ssh, ->(cmd) { cmd.join(" ") }) do
|
||||
new_command(:mysql).stub(:run_over_ssh, ->(cmd) { cmd.join(" ") }) do
|
||||
assert_match %r|docker exec -it app-mysql mysql -u root|,
|
||||
@mysql.execute_in_existing_container_over_ssh("mysql", "-u", "root")
|
||||
new_command(:mysql).execute_in_existing_container_over_ssh("mysql", "-u", "root")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -107,28 +119,33 @@ class CommandsAccessoryTest < ActiveSupport::TestCase
|
||||
test "logs" do
|
||||
assert_equal \
|
||||
"docker logs app-mysql --timestamps 2>&1",
|
||||
@mysql.logs.join(" ")
|
||||
new_command(:mysql).logs.join(" ")
|
||||
|
||||
assert_equal \
|
||||
"docker logs app-mysql --since 5m --tail 100 --timestamps 2>&1 | grep 'thing'",
|
||||
@mysql.logs(since: "5m", lines: 100, grep: "thing").join(" ")
|
||||
new_command(:mysql).logs(since: "5m", lines: 100, grep: "thing").join(" ")
|
||||
end
|
||||
|
||||
test "follow logs" do
|
||||
assert_equal \
|
||||
"ssh -t root@1.1.1.5 'docker logs app-mysql --timestamps --tail 10 --follow 2>&1'",
|
||||
@mysql.follow_logs
|
||||
new_command(:mysql).follow_logs
|
||||
end
|
||||
|
||||
test "remove container" do
|
||||
assert_equal \
|
||||
"docker container prune --force --filter label=service=app-mysql",
|
||||
@mysql.remove_container.join(" ")
|
||||
new_command(:mysql).remove_container.join(" ")
|
||||
end
|
||||
|
||||
test "remove image" do
|
||||
assert_equal \
|
||||
"docker image prune --all --force --filter label=service=app-mysql",
|
||||
@mysql.remove_image.join(" ")
|
||||
"docker image rm --force private.registry/mysql:8.0",
|
||||
new_command(:mysql).remove_image.join(" ")
|
||||
end
|
||||
|
||||
private
|
||||
def new_command(accessory)
|
||||
Mrsk::Commands::Accessory.new(Mrsk::Configuration.new(@config), name: accessory)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -5,7 +5,6 @@ class CommandsAppTest < ActiveSupport::TestCase
|
||||
ENV["RAILS_MASTER_KEY"] = "456"
|
||||
|
||||
@config = { service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" }, servers: [ "1.1.1.1" ], env: { "secret" => [ "RAILS_MASTER_KEY" ] } }
|
||||
@app = Mrsk::Commands::App.new Mrsk::Configuration.new(@config).tap { |c| c.version = "999" }
|
||||
end
|
||||
|
||||
teardown do
|
||||
@@ -14,165 +13,294 @@ class CommandsAppTest < ActiveSupport::TestCase
|
||||
|
||||
test "run" do
|
||||
assert_equal \
|
||||
"docker run --detach --restart unless-stopped --log-opt max-size=10m --name app-999 -e RAILS_MASTER_KEY=\"456\" --label service=\"app\" --label role=\"web\" --label traefik.http.routers.app.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.services.app.loadbalancer.healthcheck.path=\"/up\" --label traefik.http.services.app.loadbalancer.healthcheck.interval=\"1s\" --label traefik.http.middlewares.app.retry.attempts=\"5\" --label traefik.http.middlewares.app.retry.initialinterval=\"500ms\" dhh/app:999",
|
||||
@app.run.join(" ")
|
||||
"docker run --detach --restart unless-stopped --name app-web-999 -e MRSK_CONTAINER_NAME=\"app-web-999\" -e RAILS_MASTER_KEY=\"456\" --health-cmd \"curl -f http://localhost:3000/up || exit 1\" --health-interval \"1s\" --log-opt max-size=\"10m\" --label service=\"app\" --label role=\"web\" --label traefik.http.services.app-web.loadbalancer.server.scheme=\"http\" --label traefik.http.routers.app-web.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.middlewares.app-web-retry.retry.attempts=\"5\" --label traefik.http.middlewares.app-web-retry.retry.initialinterval=\"500ms\" --label traefik.http.routers.app-web.middlewares=\"app-web-retry@docker\" dhh/app:999",
|
||||
new_command.run.join(" ")
|
||||
end
|
||||
|
||||
test "run with volumes" do
|
||||
@config[:volumes] = ["/local/path:/container/path" ]
|
||||
|
||||
assert_equal \
|
||||
"docker run --detach --restart unless-stopped --log-opt max-size=10m --name app-999 -e RAILS_MASTER_KEY=\"456\" --volume /local/path:/container/path --label service=\"app\" --label role=\"web\" --label traefik.http.routers.app.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.services.app.loadbalancer.healthcheck.path=\"/up\" --label traefik.http.services.app.loadbalancer.healthcheck.interval=\"1s\" --label traefik.http.middlewares.app.retry.attempts=\"5\" --label traefik.http.middlewares.app.retry.initialinterval=\"500ms\" dhh/app:999",
|
||||
@app.run.join(" ")
|
||||
"docker run --detach --restart unless-stopped --name app-web-999 -e MRSK_CONTAINER_NAME=\"app-web-999\" -e RAILS_MASTER_KEY=\"456\" --health-cmd \"curl -f http://localhost:3000/up || exit 1\" --health-interval \"1s\" --log-opt max-size=\"10m\" --volume /local/path:/container/path --label service=\"app\" --label role=\"web\" --label traefik.http.services.app-web.loadbalancer.server.scheme=\"http\" --label traefik.http.routers.app-web.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.middlewares.app-web-retry.retry.attempts=\"5\" --label traefik.http.middlewares.app-web-retry.retry.initialinterval=\"500ms\" --label traefik.http.routers.app-web.middlewares=\"app-web-retry@docker\" dhh/app:999",
|
||||
new_command.run.join(" ")
|
||||
end
|
||||
|
||||
test "run with custom healthcheck path" do
|
||||
@config[:healthcheck] = { "path" => "/healthz" }
|
||||
|
||||
assert_equal \
|
||||
"docker run --detach --restart unless-stopped --log-opt max-size=10m --name app-999 -e RAILS_MASTER_KEY=\"456\" --label service=\"app\" --label role=\"web\" --label traefik.http.routers.app.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.services.app.loadbalancer.healthcheck.path=\"/healthz\" --label traefik.http.services.app.loadbalancer.healthcheck.interval=\"1s\" --label traefik.http.middlewares.app.retry.attempts=\"5\" --label traefik.http.middlewares.app.retry.initialinterval=\"500ms\" dhh/app:999",
|
||||
@app.run.join(" ")
|
||||
"docker run --detach --restart unless-stopped --name app-web-999 -e MRSK_CONTAINER_NAME=\"app-web-999\" -e RAILS_MASTER_KEY=\"456\" --health-cmd \"curl -f http://localhost:3000/healthz || exit 1\" --health-interval \"1s\" --log-opt max-size=\"10m\" --label service=\"app\" --label role=\"web\" --label traefik.http.services.app-web.loadbalancer.server.scheme=\"http\" --label traefik.http.routers.app-web.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.middlewares.app-web-retry.retry.attempts=\"5\" --label traefik.http.middlewares.app-web-retry.retry.initialinterval=\"500ms\" --label traefik.http.routers.app-web.middlewares=\"app-web-retry@docker\" dhh/app:999",
|
||||
new_command.run.join(" ")
|
||||
end
|
||||
|
||||
test "run with custom healthcheck command" do
|
||||
@config[:healthcheck] = { "cmd" => "/bin/up" }
|
||||
|
||||
assert_equal \
|
||||
"docker run --detach --restart unless-stopped --name app-web-999 -e MRSK_CONTAINER_NAME=\"app-web-999\" -e RAILS_MASTER_KEY=\"456\" --health-cmd \"/bin/up\" --health-interval \"1s\" --log-opt max-size=\"10m\" --label service=\"app\" --label role=\"web\" --label traefik.http.services.app-web.loadbalancer.server.scheme=\"http\" --label traefik.http.routers.app-web.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.middlewares.app-web-retry.retry.attempts=\"5\" --label traefik.http.middlewares.app-web-retry.retry.initialinterval=\"500ms\" --label traefik.http.routers.app-web.middlewares=\"app-web-retry@docker\" dhh/app:999",
|
||||
new_command.run.join(" ")
|
||||
end
|
||||
|
||||
test "run with role-specific healthcheck options" do
|
||||
@config[:servers] = { "web" => { "hosts" => [ "1.1.1.1" ], "healthcheck" => { "cmd" => "/bin/healthy" } } }
|
||||
|
||||
assert_equal \
|
||||
"docker run --detach --restart unless-stopped --name app-web-999 -e MRSK_CONTAINER_NAME=\"app-web-999\" -e RAILS_MASTER_KEY=\"456\" --health-cmd \"/bin/healthy\" --health-interval \"1s\" --log-opt max-size=\"10m\" --label service=\"app\" --label role=\"web\" --label traefik.http.services.app-web.loadbalancer.server.scheme=\"http\" --label traefik.http.routers.app-web.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.middlewares.app-web-retry.retry.attempts=\"5\" --label traefik.http.middlewares.app-web-retry.retry.initialinterval=\"500ms\" --label traefik.http.routers.app-web.middlewares=\"app-web-retry@docker\" dhh/app:999",
|
||||
new_command.run.join(" ")
|
||||
end
|
||||
|
||||
test "run with custom options" do
|
||||
@config[:servers] = { "web" => [ "1.1.1.1" ], "jobs" => { "hosts" => [ "1.1.1.2" ], "cmd" => "bin/jobs", "options" => { "mount" => "somewhere", "cap-add" => true } } }
|
||||
@app = Mrsk::Commands::App.new Mrsk::Configuration.new(@config).tap { |c| c.version = "999" }
|
||||
assert_equal \
|
||||
"docker run --detach --restart unless-stopped --name app-jobs-999 -e MRSK_CONTAINER_NAME=\"app-jobs-999\" -e RAILS_MASTER_KEY=\"456\" --log-opt max-size=\"10m\" --label service=\"app\" --label role=\"jobs\" --mount \"somewhere\" --cap-add dhh/app:999 bin/jobs",
|
||||
new_command(role: "jobs").run.join(" ")
|
||||
end
|
||||
|
||||
test "run with logging config" do
|
||||
@config[:logging] = { "driver" => "local", "options" => { "max-size" => "100m", "max-file" => "3" } }
|
||||
|
||||
assert_equal \
|
||||
"docker run --detach --restart unless-stopped --log-opt max-size=10m --name app-999 -e RAILS_MASTER_KEY=\"456\" --label service=\"app\" --label role=\"jobs\" --mount \"somewhere\" --cap-add dhh/app:999 bin/jobs",
|
||||
@app.run(role: :jobs).join(" ")
|
||||
"docker run --detach --restart unless-stopped --name app-web-999 -e MRSK_CONTAINER_NAME=\"app-web-999\" -e RAILS_MASTER_KEY=\"456\" --health-cmd \"curl -f http://localhost:3000/up || exit 1\" --health-interval \"1s\" --log-driver \"local\" --log-opt max-size=\"100m\" --log-opt max-file=\"3\" --label service=\"app\" --label role=\"web\" --label traefik.http.services.app-web.loadbalancer.server.scheme=\"http\" --label traefik.http.routers.app-web.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.middlewares.app-web-retry.retry.attempts=\"5\" --label traefik.http.middlewares.app-web-retry.retry.initialinterval=\"500ms\" --label traefik.http.routers.app-web.middlewares=\"app-web-retry@docker\" dhh/app:999",
|
||||
new_command.run.join(" ")
|
||||
end
|
||||
|
||||
test "start" do
|
||||
assert_equal \
|
||||
"docker start app-999",
|
||||
@app.start.join(" ")
|
||||
"docker start app-web-999",
|
||||
new_command.start.join(" ")
|
||||
end
|
||||
|
||||
test "start with destination" do
|
||||
@destination = "staging"
|
||||
assert_equal \
|
||||
"docker start app-web-staging-999",
|
||||
new_command.start.join(" ")
|
||||
end
|
||||
|
||||
test "stop" do
|
||||
assert_equal \
|
||||
"docker ps --quiet --filter label=service=app | xargs docker stop",
|
||||
@app.stop.join(" ")
|
||||
"docker ps --quiet --filter label=service=app --filter label=role=web --filter status=running --latest | xargs docker stop",
|
||||
new_command.stop.join(" ")
|
||||
end
|
||||
|
||||
test "stop with custom stop wait time" do
|
||||
@config[:stop_wait_time] = 30
|
||||
assert_equal \
|
||||
"docker ps --quiet --filter label=service=app --filter label=role=web --filter status=running --latest | xargs docker stop -t 30",
|
||||
new_command.stop.join(" ")
|
||||
end
|
||||
|
||||
test "stop with version" do
|
||||
assert_equal \
|
||||
"docker container ls --all --filter name=^app-web-123$ --quiet | xargs docker stop",
|
||||
new_command.stop(version: "123").join(" ")
|
||||
end
|
||||
|
||||
test "info" do
|
||||
assert_equal \
|
||||
"docker ps --filter label=service=app",
|
||||
@app.info.join(" ")
|
||||
"docker ps --filter label=service=app --filter label=role=web",
|
||||
new_command.info.join(" ")
|
||||
end
|
||||
|
||||
test "info with destination" do
|
||||
@destination = "staging"
|
||||
assert_equal \
|
||||
"docker ps --filter label=service=app --filter label=destination=staging --filter label=role=web",
|
||||
new_command.info.join(" ")
|
||||
end
|
||||
|
||||
|
||||
test "logs" do
|
||||
assert_equal \
|
||||
"docker ps --quiet --filter label=service=app | xargs docker logs 2>&1",
|
||||
@app.logs.join(" ")
|
||||
"docker ps --quiet --filter label=service=app --filter label=role=web --filter status=running --latest | xargs docker logs 2>&1",
|
||||
new_command.logs.join(" ")
|
||||
|
||||
assert_equal \
|
||||
"docker ps --quiet --filter label=service=app | xargs docker logs --since 5m 2>&1",
|
||||
@app.logs(since: "5m").join(" ")
|
||||
"docker ps --quiet --filter label=service=app --filter label=role=web --filter status=running --latest | xargs docker logs --since 5m 2>&1",
|
||||
new_command.logs(since: "5m").join(" ")
|
||||
|
||||
assert_equal \
|
||||
"docker ps --quiet --filter label=service=app | xargs docker logs --tail 100 2>&1",
|
||||
@app.logs(lines: "100").join(" ")
|
||||
"docker ps --quiet --filter label=service=app --filter label=role=web --filter status=running --latest | xargs docker logs --tail 100 2>&1",
|
||||
new_command.logs(lines: "100").join(" ")
|
||||
|
||||
assert_equal \
|
||||
"docker ps --quiet --filter label=service=app | xargs docker logs --since 5m --tail 100 2>&1",
|
||||
@app.logs(since: "5m", lines: "100").join(" ")
|
||||
"docker ps --quiet --filter label=service=app --filter label=role=web --filter status=running --latest | xargs docker logs --since 5m --tail 100 2>&1",
|
||||
new_command.logs(since: "5m", lines: "100").join(" ")
|
||||
|
||||
assert_equal \
|
||||
"docker ps --quiet --filter label=service=app | xargs docker logs 2>&1 | grep 'my-id'",
|
||||
@app.logs(grep: "my-id").join(" ")
|
||||
"docker ps --quiet --filter label=service=app --filter label=role=web --filter status=running --latest | xargs docker logs 2>&1 | grep 'my-id'",
|
||||
new_command.logs(grep: "my-id").join(" ")
|
||||
|
||||
assert_equal \
|
||||
"docker ps --quiet --filter label=service=app | xargs docker logs --since 5m 2>&1 | grep 'my-id'",
|
||||
@app.logs(since: "5m", grep: "my-id").join(" ")
|
||||
"docker ps --quiet --filter label=service=app --filter label=role=web --filter status=running --latest | xargs docker logs --since 5m 2>&1 | grep 'my-id'",
|
||||
new_command.logs(since: "5m", grep: "my-id").join(" ")
|
||||
end
|
||||
|
||||
test "follow logs" do
|
||||
@app.stub(:run_over_ssh, ->(cmd, host:) { cmd.join(" ") }) do
|
||||
assert_equal \
|
||||
"docker ps --quiet --filter label=service=app | xargs docker logs --timestamps --tail 10 --follow 2>&1",
|
||||
@app.follow_logs(host: "app-1")
|
||||
assert_match \
|
||||
"docker ps --quiet --filter label=service=app --filter label=role=web --filter status=running --latest | xargs docker logs --timestamps --tail 10 --follow 2>&1",
|
||||
new_command.follow_logs(host: "app-1")
|
||||
|
||||
assert_equal \
|
||||
"docker ps --quiet --filter label=service=app | xargs docker logs --timestamps --tail 10 --follow 2>&1 | grep \"Completed\"",
|
||||
@app.follow_logs(host: "app-1", grep: "Completed")
|
||||
end
|
||||
assert_match \
|
||||
"docker ps --quiet --filter label=service=app --filter label=role=web --filter status=running --latest | xargs docker logs --timestamps --tail 10 --follow 2>&1 | grep \"Completed\"",
|
||||
new_command.follow_logs(host: "app-1", grep: "Completed")
|
||||
end
|
||||
|
||||
|
||||
test "execute in new container" do
|
||||
assert_equal \
|
||||
"docker run --rm -e RAILS_MASTER_KEY=\"456\" dhh/app:999 bin/rails db:setup",
|
||||
@app.execute_in_new_container("bin/rails", "db:setup").join(" ")
|
||||
new_command.execute_in_new_container("bin/rails", "db:setup").join(" ")
|
||||
end
|
||||
|
||||
test "execute in existing container" do
|
||||
assert_equal \
|
||||
"docker exec app-999 bin/rails db:setup",
|
||||
@app.execute_in_existing_container("bin/rails", "db:setup").join(" ")
|
||||
"docker exec app-web-999 bin/rails db:setup",
|
||||
new_command.execute_in_existing_container("bin/rails", "db:setup").join(" ")
|
||||
end
|
||||
|
||||
test "execute in new container over ssh" do
|
||||
@app.stub(:run_over_ssh, ->(cmd, host:) { cmd.join(" ") }) do
|
||||
assert_match %r|docker run -it --rm -e RAILS_MASTER_KEY=\"456\" dhh/app:999 bin/rails c|,
|
||||
@app.execute_in_new_container_over_ssh("bin/rails", "c", host: "app-1")
|
||||
end
|
||||
assert_match %r|docker run -it --rm -e RAILS_MASTER_KEY=\"456\" dhh/app:999 bin/rails c|,
|
||||
new_command.execute_in_new_container_over_ssh("bin/rails", "c", host: "app-1")
|
||||
end
|
||||
|
||||
test "execute in existing container over ssh" do
|
||||
@app.stub(:run_over_ssh, ->(cmd, host:) { cmd.join(" ") }) do
|
||||
assert_match %r|docker exec -it app-999 bin/rails c|,
|
||||
@app.execute_in_existing_container_over_ssh("bin/rails", "c", host: "app-1")
|
||||
end
|
||||
assert_match %r|docker exec -it app-web-999 bin/rails c|,
|
||||
new_command.execute_in_existing_container_over_ssh("bin/rails", "c", host: "app-1")
|
||||
end
|
||||
|
||||
test "run over ssh" do
|
||||
assert_equal "ssh -t root@1.1.1.1 'ls'", @app.run_over_ssh("ls", host: "1.1.1.1")
|
||||
assert_equal "ssh -t root@1.1.1.1 'ls'", new_command.run_over_ssh("ls", host: "1.1.1.1")
|
||||
end
|
||||
|
||||
test "run over ssh with custom user" do
|
||||
@app = Mrsk::Commands::App.new Mrsk::Configuration.new(@config.tap { |c| c[:ssh] = { "user" => "app" } })
|
||||
assert_equal "ssh -t app@1.1.1.1 'ls'", @app.run_over_ssh("ls", host: "1.1.1.1")
|
||||
@config[:ssh] = { "user" => "app" }
|
||||
assert_equal "ssh -t app@1.1.1.1 'ls'", new_command.run_over_ssh("ls", host: "1.1.1.1")
|
||||
end
|
||||
|
||||
test "run over ssh with proxy" do
|
||||
@app = Mrsk::Commands::App.new Mrsk::Configuration.new(@config.tap { |c| c[:ssh] = { "proxy" => "2.2.2.2" } })
|
||||
assert_equal "ssh -J root@2.2.2.2 -t root@1.1.1.1 'ls'", @app.run_over_ssh("ls", host: "1.1.1.1")
|
||||
@config[:ssh] = { "proxy" => "2.2.2.2" }
|
||||
assert_equal "ssh -J root@2.2.2.2 -t root@1.1.1.1 'ls'", new_command.run_over_ssh("ls", host: "1.1.1.1")
|
||||
end
|
||||
|
||||
test "run over ssh with proxy user" do
|
||||
@app = Mrsk::Commands::App.new Mrsk::Configuration.new(@config.tap { |c| c[:ssh] = { "proxy" => "app@2.2.2.2" } })
|
||||
assert_equal "ssh -J app@2.2.2.2 -t root@1.1.1.1 'ls'", @app.run_over_ssh("ls", host: "1.1.1.1")
|
||||
@config[:ssh] = { "proxy" => "app@2.2.2.2" }
|
||||
assert_equal "ssh -J app@2.2.2.2 -t root@1.1.1.1 'ls'", new_command.run_over_ssh("ls", host: "1.1.1.1")
|
||||
end
|
||||
|
||||
test "run over ssh with custom user with proxy" do
|
||||
@app = Mrsk::Commands::App.new Mrsk::Configuration.new(@config.tap { |c| c[:ssh] = { "user" => "app", "proxy" => "2.2.2.2" } })
|
||||
assert_equal "ssh -J root@2.2.2.2 -t app@1.1.1.1 'ls'", @app.run_over_ssh("ls", host: "1.1.1.1")
|
||||
@config[:ssh] = { "user" => "app", "proxy" => "2.2.2.2" }
|
||||
assert_equal "ssh -J root@2.2.2.2 -t app@1.1.1.1 'ls'", new_command.run_over_ssh("ls", host: "1.1.1.1")
|
||||
end
|
||||
|
||||
|
||||
test "current_container_id" do
|
||||
test "current_running_container_id" do
|
||||
assert_equal \
|
||||
"docker ps --quiet --filter label=service=app",
|
||||
@app.current_container_id.join(" ")
|
||||
"docker ps --quiet --filter label=service=app --filter label=role=web --filter status=running --latest",
|
||||
new_command.current_running_container_id.join(" ")
|
||||
end
|
||||
|
||||
test "current_running_container_id with destination" do
|
||||
@destination = "staging"
|
||||
assert_equal \
|
||||
"docker ps --quiet --filter label=service=app --filter label=destination=staging --filter label=role=web --filter status=running --latest",
|
||||
new_command.current_running_container_id.join(" ")
|
||||
end
|
||||
|
||||
test "container_id_for" do
|
||||
assert_equal \
|
||||
"docker container ls --all --filter name=app-999 --quiet",
|
||||
@app.container_id_for(container_name: "app-999").join(" ")
|
||||
"docker container ls --all --filter name=^app-999$ --quiet",
|
||||
new_command.container_id_for(container_name: "app-999").join(" ")
|
||||
end
|
||||
|
||||
test "current_running_version" do
|
||||
assert_equal \
|
||||
"docker ps --filter label=service=app --format \"{{.Names}}\" | sed 's/-/\\n/g' | tail -n 1",
|
||||
@app.current_running_version.join(" ")
|
||||
"docker ps --filter label=service=app --filter label=role=web --filter status=running --latest --format \"{{.Names}}\" | grep -oE \"\\-[^-]+$\" | cut -c 2-",
|
||||
new_command.current_running_version.join(" ")
|
||||
end
|
||||
|
||||
test "most_recent_version_from_available_images" do
|
||||
test "list_versions" do
|
||||
assert_equal \
|
||||
"docker image ls --format \"{{.Tag}}\" dhh/app | head -n 1",
|
||||
@app.most_recent_version_from_available_images.join(" ")
|
||||
"docker ps --filter label=service=app --filter label=role=web --format \"{{.Names}}\" | grep -oE \"\\-[^-]+$\" | cut -c 2-",
|
||||
new_command.list_versions.join(" ")
|
||||
|
||||
assert_equal \
|
||||
"docker ps --filter label=service=app --filter label=role=web --filter status=running --latest --format \"{{.Names}}\" | grep -oE \"\\-[^-]+$\" | cut -c 2-",
|
||||
new_command.list_versions("--latest", status: :running).join(" ")
|
||||
end
|
||||
|
||||
test "list_containers" do
|
||||
assert_equal \
|
||||
"docker container ls --all --filter label=service=app --filter label=role=web",
|
||||
new_command.list_containers.join(" ")
|
||||
end
|
||||
|
||||
test "list_containers with destination" do
|
||||
@destination = "staging"
|
||||
assert_equal \
|
||||
"docker container ls --all --filter label=service=app --filter label=destination=staging --filter label=role=web",
|
||||
new_command.list_containers.join(" ")
|
||||
end
|
||||
|
||||
test "list_container_names" do
|
||||
assert_equal \
|
||||
"docker container ls --all --filter label=service=app --filter label=role=web --format '{{ .Names }}'",
|
||||
new_command.list_container_names.join(" ")
|
||||
end
|
||||
|
||||
test "remove_container" do
|
||||
assert_equal \
|
||||
"docker container ls --all --filter name=^app-web-999$ --quiet | xargs docker container rm",
|
||||
new_command.remove_container(version: "999").join(" ")
|
||||
end
|
||||
|
||||
test "remove_container with destination" do
|
||||
@destination = "staging"
|
||||
assert_equal \
|
||||
"docker container ls --all --filter name=^app-web-staging-999$ --quiet | xargs docker container rm",
|
||||
new_command.remove_container(version: "999").join(" ")
|
||||
end
|
||||
|
||||
test "remove_containers" do
|
||||
assert_equal \
|
||||
"docker container prune --force --filter label=service=app --filter label=role=web",
|
||||
new_command.remove_containers.join(" ")
|
||||
end
|
||||
|
||||
test "remove_containers with destination" do
|
||||
@destination = "staging"
|
||||
assert_equal \
|
||||
"docker container prune --force --filter label=service=app --filter label=destination=staging --filter label=role=web",
|
||||
new_command.remove_containers.join(" ")
|
||||
end
|
||||
|
||||
test "list_images" do
|
||||
assert_equal \
|
||||
"docker image ls dhh/app",
|
||||
new_command.list_images.join(" ")
|
||||
end
|
||||
|
||||
test "remove_images" do
|
||||
assert_equal \
|
||||
"docker image prune --all --force --filter label=service=app --filter label=role=web",
|
||||
new_command.remove_images.join(" ")
|
||||
end
|
||||
|
||||
test "remove_images with destination" do
|
||||
@destination = "staging"
|
||||
assert_equal \
|
||||
"docker image prune --all --force --filter label=service=app --filter label=destination=staging --filter label=role=web",
|
||||
new_command.remove_images.join(" ")
|
||||
end
|
||||
|
||||
test "tag_current_as_latest" do
|
||||
assert_equal \
|
||||
"docker tag dhh/app:999 dhh/app:latest",
|
||||
new_command.tag_current_as_latest.join(" ")
|
||||
end
|
||||
|
||||
private
|
||||
def new_command(role: "web")
|
||||
Mrsk::Commands::App.new(Mrsk::Configuration.new(@config, destination: @destination, version: "999"), role: role)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -6,22 +6,65 @@ class CommandsAuditorTest < ActiveSupport::TestCase
|
||||
service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" }, servers: [ "1.1.1.1" ],
|
||||
audit_broadcast_cmd: "bin/audit_broadcast"
|
||||
}
|
||||
|
||||
@auditor = new_command
|
||||
end
|
||||
|
||||
test "record" do
|
||||
assert_match \
|
||||
/echo '.* app removed container' >> mrsk-app-audit.log/,
|
||||
new_command.record("app removed container").join(" ")
|
||||
assert_equal [
|
||||
:echo,
|
||||
"[#{@auditor.details[:recorded_at]}]", "[#{@auditor.details[:performer]}]",
|
||||
"app removed container",
|
||||
">>", "mrsk-app-audit.log"
|
||||
], @auditor.record("app removed container")
|
||||
end
|
||||
|
||||
test "record with destination" do
|
||||
new_command(destination: "staging").tap do |auditor|
|
||||
assert_equal [
|
||||
:echo,
|
||||
"[#{auditor.details[:recorded_at]}]", "[#{auditor.details[:performer]}]", "[#{auditor.details[:destination]}]",
|
||||
"app removed container",
|
||||
">>", "mrsk-app-staging-audit.log"
|
||||
], auditor.record("app removed container")
|
||||
end
|
||||
end
|
||||
|
||||
test "record with command details" do
|
||||
new_command(role: "web").tap do |auditor|
|
||||
assert_equal [
|
||||
:echo,
|
||||
"[#{auditor.details[:recorded_at]}]", "[#{auditor.details[:performer]}]", "[#{auditor.details[:role]}]",
|
||||
"app removed container",
|
||||
">>", "mrsk-app-audit.log"
|
||||
], auditor.record("app removed container")
|
||||
end
|
||||
end
|
||||
|
||||
test "record with arg details" do
|
||||
assert_equal [
|
||||
:echo,
|
||||
"[#{@auditor.details[:recorded_at]}]", "[#{@auditor.details[:performer]}]", "[value]",
|
||||
"app removed container",
|
||||
">>", "mrsk-app-audit.log"
|
||||
], @auditor.record("app removed container", detail: "value")
|
||||
end
|
||||
|
||||
test "broadcast" do
|
||||
assert_match \
|
||||
/bin\/audit_broadcast '\[.*\] app removed container'/,
|
||||
new_command.broadcast("app removed container").join(" ")
|
||||
assert_equal [
|
||||
"bin/audit_broadcast",
|
||||
"'[#{@auditor.details[:performer]}] [value] app removed container'",
|
||||
env: {
|
||||
"MRSK_RECORDED_AT" => @auditor.details[:recorded_at],
|
||||
"MRSK_PERFORMER" => @auditor.details[:performer],
|
||||
"MRSK_EVENT" => "app removed container",
|
||||
"MRSK_DETAIL" => "value"
|
||||
}
|
||||
], @auditor.broadcast("app removed container", detail: "value")
|
||||
end
|
||||
|
||||
private
|
||||
def new_command
|
||||
Mrsk::Commands::Auditor.new(Mrsk::Configuration.new(@config, version: "123"))
|
||||
def new_command(destination: nil, **details)
|
||||
Mrsk::Commands::Auditor.new(Mrsk::Configuration.new(@config, destination: destination, version: "123"), **details)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -17,7 +17,7 @@ class CommandsBuilderTest < ActiveSupport::TestCase
|
||||
builder = new_builder_command(builder: { "multiarch" => false })
|
||||
assert_equal "native", builder.name
|
||||
assert_equal \
|
||||
"docker build -t dhh/app:123 -t dhh/app:latest --label service=\"app\" --file Dockerfile . && docker push dhh/app:123",
|
||||
"docker build -t dhh/app:123 -t dhh/app:latest --label service=\"app\" --file Dockerfile . && docker push dhh/app:123 && docker push dhh/app:latest",
|
||||
builder.push.join(" ")
|
||||
end
|
||||
|
||||
@@ -52,12 +52,21 @@ class CommandsBuilderTest < ActiveSupport::TestCase
|
||||
end
|
||||
|
||||
test "build dockerfile" do
|
||||
Pathname.any_instance.expects(:exist?).returns(true).once
|
||||
builder = new_builder_command(builder: { "dockerfile" => "Dockerfile.xyz" })
|
||||
assert_equal \
|
||||
"-t dhh/app:123 -t dhh/app:latest --label service=\"app\" --file Dockerfile.xyz",
|
||||
builder.target.build_options.join(" ")
|
||||
end
|
||||
|
||||
test "missing dockerfile" do
|
||||
Pathname.any_instance.expects(:exist?).returns(false).once
|
||||
builder = new_builder_command(builder: { "dockerfile" => "Dockerfile.xyz" })
|
||||
assert_raises(Mrsk::Commands::Builder::Base::BuilderError) do
|
||||
builder.target.build_options.join(" ")
|
||||
end
|
||||
end
|
||||
|
||||
test "build context" do
|
||||
builder = new_builder_command(builder: { "context" => ".." })
|
||||
assert_equal \
|
||||
@@ -68,7 +77,7 @@ class CommandsBuilderTest < ActiveSupport::TestCase
|
||||
test "native push with build args" do
|
||||
builder = new_builder_command(builder: { "multiarch" => false, "args" => { "a" => 1, "b" => 2 } })
|
||||
assert_equal \
|
||||
"docker build -t dhh/app:123 -t dhh/app:latest --label service=\"app\" --build-arg a=\"1\" --build-arg b=\"2\" --file Dockerfile . && docker push dhh/app:123",
|
||||
"docker build -t dhh/app:123 -t dhh/app:latest --label service=\"app\" --build-arg a=\"1\" --build-arg b=\"2\" --file Dockerfile . && docker push dhh/app:123 && docker push dhh/app:latest",
|
||||
builder.push.join(" ")
|
||||
end
|
||||
|
||||
@@ -82,7 +91,7 @@ class CommandsBuilderTest < ActiveSupport::TestCase
|
||||
test "native push with with build secrets" do
|
||||
builder = new_builder_command(builder: { "multiarch" => false, "secrets" => [ "a", "b" ] })
|
||||
assert_equal \
|
||||
"docker build -t dhh/app:123 -t dhh/app:latest --label service=\"app\" --secret id=\"a\" --secret id=\"b\" --file Dockerfile . && docker push dhh/app:123",
|
||||
"docker build -t dhh/app:123 -t dhh/app:latest --label service=\"app\" --secret id=\"a\" --secret id=\"b\" --file Dockerfile . && docker push dhh/app:123 && docker push dhh/app:latest",
|
||||
builder.push.join(" ")
|
||||
end
|
||||
|
||||
|
||||
26
test/commands/docker_test.rb
Normal file
26
test/commands/docker_test.rb
Normal file
@@ -0,0 +1,26 @@
|
||||
require "test_helper"
|
||||
|
||||
class CommandsDockerTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@config = {
|
||||
service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" }, servers: [ "1.1.1.1" ]
|
||||
}
|
||||
@docker = Mrsk::Commands::Docker.new(Mrsk::Configuration.new(@config))
|
||||
end
|
||||
|
||||
test "install" do
|
||||
assert_equal "curl -fsSL https://get.docker.com | sh", @docker.install.join(" ")
|
||||
end
|
||||
|
||||
test "installed?" do
|
||||
assert_equal "docker -v", @docker.installed?.join(" ")
|
||||
end
|
||||
|
||||
test "running?" do
|
||||
assert_equal "docker version", @docker.running?.join(" ")
|
||||
end
|
||||
|
||||
test "superuser?" do
|
||||
assert_equal '[ "${EUID:-$(id -u)}" -eq 0 ]', @docker.superuser?.join(" ")
|
||||
end
|
||||
end
|
||||
@@ -10,7 +10,7 @@ class CommandsHealthcheckTest < ActiveSupport::TestCase
|
||||
|
||||
test "run" do
|
||||
assert_equal \
|
||||
"docker run --detach --name healthcheck-app-123 --publish 3999:3000 --label service=healthcheck-app dhh/app:123",
|
||||
"docker run --detach --name healthcheck-app-123 --publish 3999:3000 --label service=healthcheck-app -e MRSK_CONTAINER_NAME=\"healthcheck-app\" --health-cmd \"curl -f http://localhost:3000/up || exit 1\" --health-interval \"1s\" dhh/app:123",
|
||||
new_command.run.join(" ")
|
||||
end
|
||||
|
||||
@@ -18,38 +18,89 @@ class CommandsHealthcheckTest < ActiveSupport::TestCase
|
||||
@config[:healthcheck] = { "port" => 3001 }
|
||||
|
||||
assert_equal \
|
||||
"docker run --detach --name healthcheck-app-123 --publish 3999:3001 --label service=healthcheck-app dhh/app:123",
|
||||
"docker run --detach --name healthcheck-app-123 --publish 3999:3001 --label service=healthcheck-app -e MRSK_CONTAINER_NAME=\"healthcheck-app\" --health-cmd \"curl -f http://localhost:3001/up || exit 1\" --health-interval \"1s\" dhh/app:123",
|
||||
new_command.run.join(" ")
|
||||
end
|
||||
|
||||
test "curl" do
|
||||
test "run with destination" do
|
||||
@destination = "staging"
|
||||
|
||||
assert_equal \
|
||||
"curl --silent --output /dev/null --write-out '%{http_code}' --max-time 2 http://localhost:3999/up",
|
||||
new_command.curl.join(" ")
|
||||
"docker run --detach --name healthcheck-app-staging-123 --publish 3999:3000 --label service=healthcheck-app-staging -e MRSK_CONTAINER_NAME=\"healthcheck-app-staging\" --health-cmd \"curl -f http://localhost:3000/up || exit 1\" --health-interval \"1s\" dhh/app:123",
|
||||
new_command.run.join(" ")
|
||||
end
|
||||
|
||||
test "curl with custom path" do
|
||||
@config[:healthcheck] = { "path" => "/healthz" }
|
||||
test "run with custom healthcheck" do
|
||||
@config[:healthcheck] = { "cmd" => "/bin/up" }
|
||||
|
||||
assert_equal \
|
||||
"curl --silent --output /dev/null --write-out '%{http_code}' --max-time 2 http://localhost:3999/healthz",
|
||||
new_command.curl.join(" ")
|
||||
"docker run --detach --name healthcheck-app-123 --publish 3999:3000 --label service=healthcheck-app -e MRSK_CONTAINER_NAME=\"healthcheck-app\" --health-cmd \"/bin/up\" --health-interval \"1s\" dhh/app:123",
|
||||
new_command.run.join(" ")
|
||||
end
|
||||
|
||||
test "run with custom options" do
|
||||
@config[:servers] = { "web" => { "hosts" => [ "1.1.1.1" ], "options" => { "mount" => "somewhere" } } }
|
||||
assert_equal \
|
||||
"docker run --detach --name healthcheck-app-123 --publish 3999:3000 --label service=healthcheck-app -e MRSK_CONTAINER_NAME=\"healthcheck-app\" --health-cmd \"curl -f http://localhost:3000/up || exit 1\" --health-interval \"1s\" --mount \"somewhere\" dhh/app:123",
|
||||
new_command.run.join(" ")
|
||||
end
|
||||
|
||||
test "status" do
|
||||
assert_equal \
|
||||
"docker container ls --all --filter name=^healthcheck-app-123$ --quiet | xargs docker inspect --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}'",
|
||||
new_command.status.join(" ")
|
||||
end
|
||||
|
||||
test "container_health_log" do
|
||||
assert_equal \
|
||||
"docker container ls --all --filter name=^healthcheck-app-123$ --quiet | xargs docker inspect --format '{{json .State.Health}}'",
|
||||
new_command.container_health_log.join(" ")
|
||||
end
|
||||
|
||||
test "stop" do
|
||||
assert_equal \
|
||||
"docker container ls --all --filter name=healthcheck-app --quiet | xargs docker stop",
|
||||
"docker container ls --all --filter name=^healthcheck-app-123$ --quiet | xargs docker stop",
|
||||
new_command.stop.join(" ")
|
||||
end
|
||||
|
||||
test "stop with destination" do
|
||||
@destination = "staging"
|
||||
|
||||
assert_equal \
|
||||
"docker container ls --all --filter name=^healthcheck-app-staging-123$ --quiet | xargs docker stop",
|
||||
new_command.stop.join(" ")
|
||||
end
|
||||
|
||||
test "remove" do
|
||||
assert_equal \
|
||||
"docker container ls --all --filter name=healthcheck-app --quiet | xargs docker container rm",
|
||||
"docker container ls --all --filter name=^healthcheck-app-123$ --quiet | xargs docker container rm",
|
||||
new_command.remove.join(" ")
|
||||
end
|
||||
|
||||
test "remove with destination" do
|
||||
@destination = "staging"
|
||||
|
||||
assert_equal \
|
||||
"docker container ls --all --filter name=^healthcheck-app-staging-123$ --quiet | xargs docker container rm",
|
||||
new_command.remove.join(" ")
|
||||
end
|
||||
|
||||
test "logs" do
|
||||
assert_equal \
|
||||
"docker container ls --all --filter name=^healthcheck-app-123$ --quiet | xargs docker logs --tail 50 2>&1",
|
||||
new_command.logs.join(" ")
|
||||
end
|
||||
|
||||
test "logs with destination" do
|
||||
@destination = "staging"
|
||||
|
||||
assert_equal \
|
||||
"docker container ls --all --filter name=^healthcheck-app-staging-123$ --quiet | xargs docker logs --tail 50 2>&1",
|
||||
new_command.logs.join(" ")
|
||||
end
|
||||
|
||||
private
|
||||
def new_command
|
||||
Mrsk::Commands::Healthcheck.new(Mrsk::Configuration.new(@config, version: "123"))
|
||||
Mrsk::Commands::Healthcheck.new(Mrsk::Configuration.new(@config, destination: @destination, version: "123"))
|
||||
end
|
||||
end
|
||||
|
||||
33
test/commands/lock_test.rb
Normal file
33
test/commands/lock_test.rb
Normal file
@@ -0,0 +1,33 @@
|
||||
require "test_helper"
|
||||
|
||||
class CommandsLockTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@config = {
|
||||
service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" }, servers: [ "1.1.1.1" ],
|
||||
traefik: { "args" => { "accesslog.format" => "json", "metrics.prometheus.buckets" => "0.1,0.3,1.2,5.0" } }
|
||||
}
|
||||
end
|
||||
|
||||
test "status" do
|
||||
assert_equal \
|
||||
"stat mrsk_lock > /dev/null && cat mrsk_lock/details | base64 -d",
|
||||
new_command.status.join(" ")
|
||||
end
|
||||
|
||||
test "acquire" do
|
||||
assert_match \
|
||||
/mkdir mrsk_lock && echo ".*" > mrsk_lock\/details/m,
|
||||
new_command.acquire("Hello", "123").join(" ")
|
||||
end
|
||||
|
||||
test "release" do
|
||||
assert_match \
|
||||
"rm mrsk_lock/details && rm -r mrsk_lock",
|
||||
new_command.release.join(" ")
|
||||
end
|
||||
|
||||
private
|
||||
def new_command
|
||||
Mrsk::Commands::Lock.new(Mrsk::Configuration.new(@config, version: "123"))
|
||||
end
|
||||
end
|
||||
@@ -10,13 +10,13 @@ class CommandsPruneTest < ActiveSupport::TestCase
|
||||
|
||||
test "images" do
|
||||
assert_equal \
|
||||
"docker image prune --all --force --filter label=service=app --filter until=168h",
|
||||
"docker image prune --force --filter label=service=app --filter dangling=true",
|
||||
new_command.images.join(" ")
|
||||
end
|
||||
|
||||
test "containers" do
|
||||
assert_equal \
|
||||
"docker container prune --force --filter label=service=app --filter until=72h",
|
||||
"docker ps -q -a --filter label=service=app --filter status=created --filter status=exited --filter status=dead | tail -n +6 | while read container_id; do docker rm $container_id; done",
|
||||
new_command.containers.join(" ")
|
||||
end
|
||||
|
||||
|
||||
@@ -2,20 +2,66 @@ require "test_helper"
|
||||
|
||||
class CommandsTraefikTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@image = "traefik:test"
|
||||
|
||||
@config = {
|
||||
service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" }, servers: [ "1.1.1.1" ],
|
||||
traefik: { "args" => { "accesslog.format" => "json", "metrics.prometheus.buckets" => "0.1,0.3,1.2,5.0" } }
|
||||
traefik: { "image" => @image, "args" => { "accesslog.format" => "json", "api.insecure" => true, "metrics.prometheus.buckets" => "0.1,0.3,1.2,5.0" } }
|
||||
}
|
||||
end
|
||||
|
||||
test "run" do
|
||||
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\"",
|
||||
"docker run --name traefik --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --log-opt max-size=\"10m\" #{@image} --providers.docker --log.level=DEBUG --accesslog.format=\"json\" --api.insecure --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\"",
|
||||
"docker run --name traefik --detach --restart unless-stopped --publish 8080:80 --volume /var/run/docker.sock:/var/run/docker.sock --log-opt max-size=\"10m\" #{@image} --providers.docker --log.level=DEBUG --accesslog.format=\"json\" --api.insecure --metrics.prometheus.buckets=\"0.1,0.3,1.2,5.0\"",
|
||||
new_command.run.join(" ")
|
||||
end
|
||||
|
||||
test "run with ports configured" do
|
||||
assert_equal \
|
||||
"docker run --name traefik --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --log-opt max-size=\"10m\" #{@image} --providers.docker --log.level=DEBUG --accesslog.format=\"json\" --api.insecure --metrics.prometheus.buckets=\"0.1,0.3,1.2,5.0\"",
|
||||
new_command.run.join(" ")
|
||||
|
||||
@config[:traefik]["options"] = {"publish" => %w[9000:9000 9001:9001]}
|
||||
assert_equal \
|
||||
"docker run --name traefik --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --log-opt max-size=\"10m\" --publish \"9000:9000\" --publish \"9001:9001\" #{@image} --providers.docker --log.level=DEBUG --accesslog.format=\"json\" --api.insecure --metrics.prometheus.buckets=\"0.1,0.3,1.2,5.0\"",
|
||||
new_command.run.join(" ")
|
||||
end
|
||||
|
||||
test "run with volumes configured" do
|
||||
assert_equal \
|
||||
"docker run --name traefik --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --log-opt max-size=\"10m\" #{@image} --providers.docker --log.level=DEBUG --accesslog.format=\"json\" --api.insecure --metrics.prometheus.buckets=\"0.1,0.3,1.2,5.0\"",
|
||||
new_command.run.join(" ")
|
||||
|
||||
@config[:traefik]["options"] = {"volume" => %w[./letsencrypt/acme.json:/letsencrypt/acme.json] }
|
||||
assert_equal \
|
||||
"docker run --name traefik --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --log-opt max-size=\"10m\" --volume \"./letsencrypt/acme.json:/letsencrypt/acme.json\" #{@image} --providers.docker --log.level=DEBUG --accesslog.format=\"json\" --api.insecure --metrics.prometheus.buckets=\"0.1,0.3,1.2,5.0\"",
|
||||
new_command.run.join(" ")
|
||||
end
|
||||
|
||||
test "run with several options configured" do
|
||||
assert_equal \
|
||||
"docker run --name traefik --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --log-opt max-size=\"10m\" #{@image} --providers.docker --log.level=DEBUG --accesslog.format=\"json\" --api.insecure --metrics.prometheus.buckets=\"0.1,0.3,1.2,5.0\"",
|
||||
new_command.run.join(" ")
|
||||
|
||||
@config[:traefik]["options"] = {"volume" => %w[./letsencrypt/acme.json:/letsencrypt/acme.json], "publish" => %w[8080:8080], "memory" => "512m"}
|
||||
assert_equal \
|
||||
"docker run --name traefik --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --log-opt max-size=\"10m\" --volume \"./letsencrypt/acme.json:/letsencrypt/acme.json\" --publish \"8080:8080\" --memory \"512m\" #{@image} --providers.docker --log.level=DEBUG --accesslog.format=\"json\" --api.insecure --metrics.prometheus.buckets=\"0.1,0.3,1.2,5.0\"",
|
||||
new_command.run.join(" ")
|
||||
end
|
||||
|
||||
test "run with labels configured" do
|
||||
assert_equal \
|
||||
"docker run --name traefik --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --log-opt max-size=\"10m\" #{@image} --providers.docker --log.level=DEBUG --accesslog.format=\"json\" --api.insecure --metrics.prometheus.buckets=\"0.1,0.3,1.2,5.0\"",
|
||||
new_command.run.join(" ")
|
||||
|
||||
@config[:traefik]["labels"] = { "traefik.http.routers.dashboard.service" => "api@internal", "traefik.http.routers.dashboard.middlewares" => "auth" }
|
||||
assert_equal \
|
||||
"docker run --name traefik --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --log-opt max-size=\"10m\" --label traefik.http.routers.dashboard.service=\"api@internal\" --label traefik.http.routers.dashboard.middlewares=\"auth\" #{@image} --providers.docker --log.level=DEBUG --accesslog.format=\"json\" --api.insecure --metrics.prometheus.buckets=\"0.1,0.3,1.2,5.0\"",
|
||||
new_command.run.join(" ")
|
||||
end
|
||||
|
||||
@@ -23,7 +69,15 @@ class CommandsTraefikTest < ActiveSupport::TestCase
|
||||
@config.delete(:traefik)
|
||||
|
||||
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",
|
||||
"docker run --name traefik --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --log-opt max-size=\"10m\" #{Mrsk::Commands::Traefik::DEFAULT_IMAGE} --providers.docker --log.level=DEBUG",
|
||||
new_command.run.join(" ")
|
||||
end
|
||||
|
||||
test "run with logging config" do
|
||||
@config[:logging] = { "driver" => "local", "options" => { "max-size" => "100m", "max-file" => "3" } }
|
||||
|
||||
assert_equal \
|
||||
"docker run --name traefik --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --log-driver \"local\" --log-opt max-size=\"100m\" --log-opt max-file=\"3\" #{@image} --providers.docker --log.level=DEBUG --accesslog.format=\"json\" --api.insecure --metrics.prometheus.buckets=\"0.1,0.3,1.2,5.0\"",
|
||||
new_command.run.join(" ")
|
||||
end
|
||||
|
||||
@@ -41,7 +95,7 @@ class CommandsTraefikTest < ActiveSupport::TestCase
|
||||
|
||||
test "traefik info" do
|
||||
assert_equal \
|
||||
"docker ps --filter name=traefik",
|
||||
"docker ps --filter name=^traefik$",
|
||||
new_command.info.join(" ")
|
||||
end
|
||||
|
||||
|
||||
@@ -4,7 +4,10 @@ class ConfigurationAccessoryTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@deploy = {
|
||||
service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" },
|
||||
servers: [ "1.1.1.1", "1.1.1.2" ],
|
||||
servers: {
|
||||
"web" => [ "1.1.1.1", "1.1.1.2" ],
|
||||
"workers" => [ "1.1.1.3", "1.1.1.4" ]
|
||||
},
|
||||
env: { "REDIS_URL" => "redis://x/y" },
|
||||
accessories: {
|
||||
"mysql" => {
|
||||
@@ -29,7 +32,7 @@ class ConfigurationAccessoryTest < ActiveSupport::TestCase
|
||||
},
|
||||
"redis" => {
|
||||
"image" => "redis:latest",
|
||||
"host" => "1.1.1.6",
|
||||
"hosts" => [ "1.1.1.6", "1.1.1.7" ],
|
||||
"port" => "6379:6379",
|
||||
"labels" => {
|
||||
"cache" => true
|
||||
@@ -39,7 +42,26 @@ class ConfigurationAccessoryTest < ActiveSupport::TestCase
|
||||
},
|
||||
"volumes" => [
|
||||
"/var/lib/redis:/data"
|
||||
]
|
||||
],
|
||||
"options" => {
|
||||
"cpus" => 4,
|
||||
"memory" => "2GB"
|
||||
}
|
||||
},
|
||||
"monitoring" => {
|
||||
"image" => "monitoring:latest",
|
||||
"roles" => [ "web" ],
|
||||
"port" => "4321:4321",
|
||||
"labels" => {
|
||||
"cache" => true
|
||||
},
|
||||
"env" => {
|
||||
"STATSD_PORT" => "8126"
|
||||
},
|
||||
"options" => {
|
||||
"cpus" => 4,
|
||||
"memory" => "2GB"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -58,8 +80,9 @@ class ConfigurationAccessoryTest < ActiveSupport::TestCase
|
||||
end
|
||||
|
||||
test "host" do
|
||||
assert_equal "1.1.1.5", @config.accessory(:mysql).host
|
||||
assert_equal "1.1.1.6", @config.accessory(:redis).host
|
||||
assert_equal ["1.1.1.5"], @config.accessory(:mysql).hosts
|
||||
assert_equal ["1.1.1.6", "1.1.1.7"], @config.accessory(:redis).hosts
|
||||
assert_equal ["1.1.1.1", "1.1.1.2"], @config.accessory(:monitoring).hosts
|
||||
end
|
||||
|
||||
test "missing host" do
|
||||
@@ -67,10 +90,21 @@ class ConfigurationAccessoryTest < ActiveSupport::TestCase
|
||||
@config = Mrsk::Configuration.new(@deploy)
|
||||
|
||||
assert_raises(ArgumentError) do
|
||||
@config.accessory(:mysql).host
|
||||
@config.accessory(:mysql).hosts
|
||||
end
|
||||
end
|
||||
|
||||
test "setting host, hosts and roles" do
|
||||
@deploy[:accessories]["mysql"]["hosts"] = true
|
||||
@deploy[:accessories]["mysql"]["roles"] = true
|
||||
@config = Mrsk::Configuration.new(@deploy)
|
||||
|
||||
exception = assert_raises(ArgumentError) do
|
||||
@config.accessory(:mysql).hosts
|
||||
end
|
||||
assert_equal "Specify one of `host`, `hosts` or `roles` for accessory `mysql`", exception.message
|
||||
end
|
||||
|
||||
test "label args" do
|
||||
assert_equal ["--label", "service=\"app-mysql\""], @config.accessory(:mysql).label_args
|
||||
assert_equal ["--label", "service=\"app-redis\"", "--label", "cache=\"true\""], @config.accessory(:redis).label_args
|
||||
@@ -78,8 +112,11 @@ class ConfigurationAccessoryTest < ActiveSupport::TestCase
|
||||
|
||||
test "env args with secret" do
|
||||
ENV["MYSQL_ROOT_PASSWORD"] = "secret123"
|
||||
assert_equal ["-e", "MYSQL_ROOT_PASSWORD=\"secret123\"", "-e", "MYSQL_ROOT_HOST=\"%\""], @config.accessory(:mysql).env_args
|
||||
assert @config.accessory(:mysql).env_args[1].is_a?(SSHKit::Redaction)
|
||||
|
||||
@config.accessory(:mysql).env_args.tap do |env_args|
|
||||
assert_equal ["-e", "MYSQL_ROOT_PASSWORD=\"secret123\"", "-e", "MYSQL_ROOT_HOST=\"%\""], Mrsk::Utils.unredacted(env_args)
|
||||
assert_equal ["-e", "MYSQL_ROOT_PASSWORD=[REDACTED]", "-e", "MYSQL_ROOT_HOST=\"%\""], Mrsk::Utils.redacted(env_args)
|
||||
end
|
||||
ensure
|
||||
ENV["MYSQL_ROOT_PASSWORD"] = nil
|
||||
end
|
||||
@@ -104,4 +141,8 @@ class ConfigurationAccessoryTest < ActiveSupport::TestCase
|
||||
test "directories" do
|
||||
assert_equal({"$PWD/app-mysql/data"=>"/var/lib/mysql"}, @config.accessory(:mysql).directories)
|
||||
end
|
||||
|
||||
test "options" do
|
||||
assert_equal ["--cpus", "\"4\"", "--memory", "\"2GB\""], @config.accessory(:redis).option_args
|
||||
end
|
||||
end
|
||||
|
||||
@@ -42,7 +42,7 @@ class ConfigurationRoleTest < ActiveSupport::TestCase
|
||||
end
|
||||
|
||||
test "special label args for web" do
|
||||
assert_equal [ "--label", "service=\"app\"", "--label", "role=\"web\"", "--label", "traefik.http.routers.app.rule=\"PathPrefix(\\`/\\`)\"", "--label", "traefik.http.services.app.loadbalancer.healthcheck.path=\"/up\"", "--label", "traefik.http.services.app.loadbalancer.healthcheck.interval=\"1s\"", "--label", "traefik.http.middlewares.app.retry.attempts=\"5\"", "--label", "traefik.http.middlewares.app.retry.initialinterval=\"500ms\""], @config.role(:web).label_args
|
||||
assert_equal [ "--label", "service=\"app\"", "--label", "role=\"web\"", "--label", "traefik.http.services.app-web.loadbalancer.server.scheme=\"http\"", "--label", "traefik.http.routers.app-web.rule=\"PathPrefix(\\`/\\`)\"", "--label", "traefik.http.middlewares.app-web-retry.retry.attempts=\"5\"", "--label", "traefik.http.middlewares.app-web-retry.retry.initialinterval=\"500ms\"", "--label", "traefik.http.routers.app-web.middlewares=\"app-web-retry@docker\"" ], @config.role(:web).label_args
|
||||
end
|
||||
|
||||
test "custom labels" do
|
||||
@@ -57,8 +57,8 @@ class ConfigurationRoleTest < ActiveSupport::TestCase
|
||||
end
|
||||
|
||||
test "overwriting default traefik label" do
|
||||
@deploy[:labels] = { "traefik.http.routers.app.rule" => "\"Host(\\`example.com\\`) || (Host(\\`example.org\\`) && Path(\\`/traefik\\`))\"" }
|
||||
assert_equal "\"Host(\\`example.com\\`) || (Host(\\`example.org\\`) && Path(\\`/traefik\\`))\"", @config.role(:web).labels["traefik.http.routers.app.rule"]
|
||||
@deploy[:labels] = { "traefik.http.routers.app-web.rule" => "\"Host(\\`example.com\\`) || (Host(\\`example.org\\`) && Path(\\`/traefik\\`))\"" }
|
||||
assert_equal "\"Host(\\`example.com\\`) || (Host(\\`example.org\\`) && Path(\\`/traefik\\`))\"", @config.role(:web).labels["traefik.http.routers.app-web.rule"]
|
||||
end
|
||||
|
||||
test "default traefik label on non-web role" do
|
||||
@@ -66,7 +66,7 @@ class ConfigurationRoleTest < ActiveSupport::TestCase
|
||||
c[:servers]["beta"] = { "traefik" => "true", "hosts" => [ "1.1.1.5" ] }
|
||||
})
|
||||
|
||||
assert_equal [ "--label", "service=\"app\"", "--label", "role=\"beta\"", "--label", "traefik.http.routers.app.rule=\"PathPrefix(\\`/\\`)\"", "--label", "traefik.http.services.app.loadbalancer.healthcheck.path=\"/up\"", "--label", "traefik.http.services.app.loadbalancer.healthcheck.interval=\"1s\"", "--label", "traefik.http.middlewares.app.retry.attempts=\"5\"", "--label", "traefik.http.middlewares.app.retry.initialinterval=\"500ms\"" ], config.role(:beta).label_args
|
||||
assert_equal [ "--label", "service=\"app\"", "--label", "role=\"beta\"", "--label", "traefik.http.services.app-beta.loadbalancer.server.scheme=\"http\"", "--label", "traefik.http.routers.app-beta.rule=\"PathPrefix(\\`/\\`)\"", "--label", "traefik.http.middlewares.app-beta-retry.retry.attempts=\"5\"", "--label", "traefik.http.middlewares.app-beta-retry.retry.initialinterval=\"500ms\"", "--label", "traefik.http.routers.app-beta.middlewares=\"app-beta-retry@docker\"" ], config.role(:beta).label_args
|
||||
end
|
||||
|
||||
test "env overwritten by role" do
|
||||
@@ -97,7 +97,10 @@ class ConfigurationRoleTest < ActiveSupport::TestCase
|
||||
ENV["REDIS_PASSWORD"] = "secret456"
|
||||
ENV["DB_PASSWORD"] = "secret&\"123"
|
||||
|
||||
assert_equal ["-e", "REDIS_PASSWORD=\"secret456\"", "-e", "DB_PASSWORD=\"secret&\\\"123\"", "-e", "REDIS_URL=\"redis://a/b\"", "-e", "WEB_CONCURRENCY=\"4\""], @config_with_roles.role(:workers).env_args
|
||||
@config_with_roles.role(:workers).env_args.tap do |env_args|
|
||||
assert_equal ["-e", "REDIS_PASSWORD=\"secret456\"", "-e", "DB_PASSWORD=\"secret&\\\"123\"", "-e", "REDIS_URL=\"redis://a/b\"", "-e", "WEB_CONCURRENCY=\"4\""], Mrsk::Utils.unredacted(env_args)
|
||||
assert_equal ["-e", "REDIS_PASSWORD=[REDACTED]", "-e", "DB_PASSWORD=[REDACTED]", "-e", "REDIS_URL=\"redis://a/b\"", "-e", "WEB_CONCURRENCY=\"4\""], Mrsk::Utils.redacted(env_args)
|
||||
end
|
||||
ensure
|
||||
ENV["REDIS_PASSWORD"] = nil
|
||||
ENV["DB_PASSWORD"] = nil
|
||||
@@ -116,7 +119,10 @@ class ConfigurationRoleTest < ActiveSupport::TestCase
|
||||
|
||||
ENV["DB_PASSWORD"] = "secret123"
|
||||
|
||||
assert_equal ["-e", "DB_PASSWORD=\"secret123\"", "-e", "REDIS_URL=\"redis://a/b\"", "-e", "WEB_CONCURRENCY=\"4\""], @config_with_roles.role(:workers).env_args
|
||||
@config_with_roles.role(:workers).env_args.tap do |env_args|
|
||||
assert_equal ["-e", "DB_PASSWORD=\"secret123\"", "-e", "REDIS_URL=\"redis://a/b\"", "-e", "WEB_CONCURRENCY=\"4\""], Mrsk::Utils.unredacted(env_args)
|
||||
assert_equal ["-e", "DB_PASSWORD=[REDACTED]", "-e", "REDIS_URL=\"redis://a/b\"", "-e", "WEB_CONCURRENCY=\"4\""], Mrsk::Utils.redacted(env_args)
|
||||
end
|
||||
ensure
|
||||
ENV["DB_PASSWORD"] = nil
|
||||
end
|
||||
@@ -133,7 +139,10 @@ class ConfigurationRoleTest < ActiveSupport::TestCase
|
||||
|
||||
ENV["REDIS_PASSWORD"] = "secret456"
|
||||
|
||||
assert_equal ["-e", "REDIS_PASSWORD=\"secret456\"", "-e", "REDIS_URL=\"redis://a/b\"", "-e", "WEB_CONCURRENCY=\"4\""], @config_with_roles.role(:workers).env_args
|
||||
@config_with_roles.role(:workers).env_args.tap do |env_args|
|
||||
assert_equal ["-e", "REDIS_PASSWORD=\"secret456\"", "-e", "REDIS_URL=\"redis://a/b\"", "-e", "WEB_CONCURRENCY=\"4\""], Mrsk::Utils.unredacted(env_args)
|
||||
assert_equal ["-e", "REDIS_PASSWORD=[REDACTED]", "-e", "REDIS_URL=\"redis://a/b\"", "-e", "WEB_CONCURRENCY=\"4\""], Mrsk::Utils.redacted(env_args)
|
||||
end
|
||||
ensure
|
||||
ENV["REDIS_PASSWORD"] = nil
|
||||
end
|
||||
|
||||
@@ -3,6 +3,7 @@ require "test_helper"
|
||||
class ConfigurationTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
ENV["RAILS_MASTER_KEY"] = "456"
|
||||
ENV["VERSION"] = "missing"
|
||||
|
||||
@deploy = {
|
||||
service: "app", image: "dhh/app",
|
||||
@@ -15,23 +16,29 @@ class ConfigurationTest < ActiveSupport::TestCase
|
||||
@config = Mrsk::Configuration.new(@deploy)
|
||||
|
||||
@deploy_with_roles = @deploy.dup.merge({
|
||||
servers: { "web" => [ "1.1.1.1", "1.1.1.2" ], "workers" => { "hosts" => [ "1.1.1.3", "1.1.1.4" ] } } })
|
||||
servers: { "web" => [ "1.1.1.1", "1.1.1.2" ], "workers" => { "hosts" => [ "1.1.1.1", "1.1.1.3" ] } } })
|
||||
|
||||
@config_with_roles = Mrsk::Configuration.new(@deploy_with_roles)
|
||||
end
|
||||
|
||||
teardown do
|
||||
ENV["RAILS_MASTER_KEY"] = nil
|
||||
ENV.delete("RAILS_MASTER_KEY")
|
||||
ENV.delete("VERSION")
|
||||
end
|
||||
|
||||
test "ensure valid keys" do
|
||||
assert_raise(ArgumentError) do
|
||||
Mrsk::Configuration.new(@deploy.tap { _1.delete(:service) })
|
||||
Mrsk::Configuration.new(@deploy.tap { _1.delete(:image) })
|
||||
Mrsk::Configuration.new(@deploy.tap { _1.delete(:registry) })
|
||||
%i[ service image registry ].each do |key|
|
||||
test "#{key} config required" do
|
||||
assert_raise(ArgumentError) do
|
||||
Mrsk::Configuration.new @deploy.tap { _1.delete key }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Mrsk::Configuration.new(@deploy.tap { _1[:registry].delete("username") })
|
||||
Mrsk::Configuration.new(@deploy.tap { _1[:registry].delete("password") })
|
||||
%w[ username password ].each do |key|
|
||||
test "registry #{key} required" do
|
||||
assert_raise(ArgumentError) do
|
||||
Mrsk::Configuration.new @deploy.tap { _1[:registry].delete key }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -41,14 +48,14 @@ class ConfigurationTest < ActiveSupport::TestCase
|
||||
end
|
||||
|
||||
test "role" do
|
||||
assert_equal "web", @config.role(:web).name
|
||||
assert @config.role(:web).name.web?
|
||||
assert_equal "workers", @config_with_roles.role(:workers).name
|
||||
assert_nil @config.role(:missing)
|
||||
end
|
||||
|
||||
test "all hosts" do
|
||||
assert_equal [ "1.1.1.1", "1.1.1.2"], @config.all_hosts
|
||||
assert_equal [ "1.1.1.1", "1.1.1.2", "1.1.1.3", "1.1.1.4" ], @config_with_roles.all_hosts
|
||||
assert_equal [ "1.1.1.1", "1.1.1.2", "1.1.1.3" ], @config_with_roles.all_hosts
|
||||
end
|
||||
|
||||
test "primary web host" do
|
||||
@@ -62,12 +69,24 @@ class ConfigurationTest < ActiveSupport::TestCase
|
||||
@deploy_with_roles[:servers]["workers"]["traefik"] = true
|
||||
config = Mrsk::Configuration.new(@deploy_with_roles)
|
||||
|
||||
assert_equal [ "1.1.1.1", "1.1.1.2", "1.1.1.3", "1.1.1.4" ], config.traefik_hosts
|
||||
assert_equal [ "1.1.1.1", "1.1.1.2", "1.1.1.3" ], config.traefik_hosts
|
||||
end
|
||||
|
||||
test "version" do
|
||||
assert_equal "missing", @config.version
|
||||
assert_equal "123", Mrsk::Configuration.new(@deploy, version: "123").version
|
||||
ENV.delete("VERSION")
|
||||
|
||||
@config.expects(:system).with("git rev-parse").returns(nil)
|
||||
error = assert_raises(RuntimeError) { @config.version}
|
||||
assert_match /no git repository found/, error.message
|
||||
|
||||
@config.expects(:current_commit_hash).returns("git-version")
|
||||
assert_equal "git-version", @config.version
|
||||
|
||||
ENV["VERSION"] = "env-version"
|
||||
assert_equal "env-version", @config.version
|
||||
|
||||
@config.version = "arg-version"
|
||||
assert_equal "arg-version", @config.version
|
||||
end
|
||||
|
||||
test "repository" do
|
||||
@@ -94,12 +113,13 @@ class ConfigurationTest < ActiveSupport::TestCase
|
||||
|
||||
test "env args with clear and secrets" do
|
||||
ENV["PASSWORD"] = "secret123"
|
||||
|
||||
config = Mrsk::Configuration.new(@deploy.tap { |c| c.merge!({
|
||||
env: { "clear" => { "PORT" => "3000" }, "secret" => [ "PASSWORD" ] }
|
||||
}) })
|
||||
|
||||
assert_equal [ "-e", "PASSWORD=\"secret123\"", "-e", "PORT=\"3000\"" ], config.env_args
|
||||
assert config.env_args[1].is_a?(SSHKit::Redaction)
|
||||
assert_equal [ "-e", "PASSWORD=\"secret123\"", "-e", "PORT=\"3000\"" ], Mrsk::Utils.unredacted(config.env_args)
|
||||
assert_equal [ "-e", "PASSWORD=[REDACTED]", "-e", "PORT=\"3000\"" ], Mrsk::Utils.redacted(config.env_args)
|
||||
ensure
|
||||
ENV["PASSWORD"] = nil
|
||||
end
|
||||
@@ -114,12 +134,13 @@ class ConfigurationTest < ActiveSupport::TestCase
|
||||
|
||||
test "env args with only secrets" do
|
||||
ENV["PASSWORD"] = "secret123"
|
||||
|
||||
config = Mrsk::Configuration.new(@deploy.tap { |c| c.merge!({
|
||||
env: { "secret" => [ "PASSWORD" ] }
|
||||
}) })
|
||||
|
||||
assert_equal [ "-e", "PASSWORD=\"secret123\"" ], config.env_args
|
||||
assert config.env_args[1].is_a?(SSHKit::Redaction)
|
||||
assert_equal [ "-e", "PASSWORD=\"secret123\"" ], Mrsk::Utils.unredacted(config.env_args)
|
||||
assert_equal [ "-e", "PASSWORD=[REDACTED]" ], Mrsk::Utils.redacted(config.env_args)
|
||||
ensure
|
||||
ENV["PASSWORD"] = nil
|
||||
end
|
||||
@@ -134,6 +155,39 @@ class ConfigurationTest < ActiveSupport::TestCase
|
||||
|
||||
test "valid config" do
|
||||
assert @config.valid?
|
||||
assert @config_with_roles.valid?
|
||||
end
|
||||
|
||||
test "hosts required for all roles" do
|
||||
# Empty server list for implied web role
|
||||
assert_raises(ArgumentError) do
|
||||
Mrsk::Configuration.new @deploy.merge(servers: [])
|
||||
end
|
||||
|
||||
# Empty server list
|
||||
assert_raises(ArgumentError) do
|
||||
Mrsk::Configuration.new @deploy.merge(servers: { "web" => [] })
|
||||
end
|
||||
|
||||
# Missing hosts key
|
||||
assert_raises(ArgumentError) do
|
||||
Mrsk::Configuration.new @deploy.merge(servers: { "web" => {} })
|
||||
end
|
||||
|
||||
# Empty hosts list
|
||||
assert_raises(ArgumentError) do
|
||||
Mrsk::Configuration.new @deploy.merge(servers: { "web" => { "hosts" => [] } })
|
||||
end
|
||||
|
||||
# Nil hosts
|
||||
assert_raises(ArgumentError) do
|
||||
Mrsk::Configuration.new @deploy.merge(servers: { "web" => { "hosts" => nil } })
|
||||
end
|
||||
|
||||
# One role with hosts, one without
|
||||
assert_raises(ArgumentError) do
|
||||
Mrsk::Configuration.new @deploy.merge(servers: { "web" => %w[ web ], "workers" => { "hosts" => %w[ ] } })
|
||||
end
|
||||
end
|
||||
|
||||
test "ssh options" do
|
||||
@@ -157,18 +211,32 @@ class ConfigurationTest < ActiveSupport::TestCase
|
||||
assert_equal ["--volume", "/local/path:/container/path"], @config.volume_args
|
||||
end
|
||||
|
||||
test "logging args default" do
|
||||
assert_equal ["--log-opt", "max-size=\"10m\""], @config.logging_args
|
||||
end
|
||||
|
||||
test "logging args with configured options" do
|
||||
config = Mrsk::Configuration.new(@deploy.tap { |c| c.merge!(logging: { "options" => { "max-size" => "100m", "max-file" => 5 } }) })
|
||||
assert_equal ["--log-opt", "max-size=\"100m\"", "--log-opt", "max-file=\"5\""], @config.logging_args
|
||||
end
|
||||
|
||||
test "logging args with configured driver and options" do
|
||||
config = Mrsk::Configuration.new(@deploy.tap { |c| c.merge!(logging: { "driver" => "local", "options" => { "max-size" => "100m", "max-file" => 5 } }) })
|
||||
assert_equal ["--log-driver", "\"local\"", "--log-opt", "max-size=\"100m\"", "--log-opt", "max-file=\"5\""], @config.logging_args
|
||||
end
|
||||
|
||||
test "erb evaluation of yml config" do
|
||||
config = Mrsk::Configuration.create_from Pathname.new(File.expand_path("fixtures/deploy.erb.yml", __dir__))
|
||||
config = Mrsk::Configuration.create_from config_file: Pathname.new(File.expand_path("fixtures/deploy.erb.yml", __dir__))
|
||||
assert_equal "my-user", config.registry["username"]
|
||||
end
|
||||
|
||||
test "destination yml config merge" do
|
||||
dest_config_file = Pathname.new(File.expand_path("fixtures/deploy_for_dest.yml", __dir__))
|
||||
|
||||
config = Mrsk::Configuration.create_from dest_config_file, destination: "world"
|
||||
config = Mrsk::Configuration.create_from config_file: dest_config_file, destination: "world"
|
||||
assert_equal "1.1.1.1", config.all_hosts.first
|
||||
|
||||
config = Mrsk::Configuration.create_from dest_config_file, destination: "mars"
|
||||
config = Mrsk::Configuration.create_from config_file: dest_config_file, destination: "mars"
|
||||
assert_equal "1.1.1.3", config.all_hosts.first
|
||||
end
|
||||
|
||||
@@ -176,11 +244,11 @@ class ConfigurationTest < ActiveSupport::TestCase
|
||||
dest_config_file = Pathname.new(File.expand_path("fixtures/deploy_for_dest.yml", __dir__))
|
||||
|
||||
assert_raises(RuntimeError) do
|
||||
config = Mrsk::Configuration.create_from dest_config_file, destination: "missing"
|
||||
config = Mrsk::Configuration.create_from config_file: dest_config_file, destination: "missing"
|
||||
end
|
||||
end
|
||||
|
||||
test "to_h" do
|
||||
assert_equal({ :roles=>["web"], :hosts=>["1.1.1.1", "1.1.1.2"], :primary_host=>"1.1.1.1", :version=>"missing", :repository=>"dhh/app", :absolute_image=>"dhh/app:missing", :service_with_version=>"app-missing", :env_args=>["-e", "REDIS_URL=\"redis://x/y\""], :ssh_options=>{:user=>"root", :auth_methods=>["publickey"]}, :volume_args=>["--volume", "/local/path:/container/path"], :healthcheck=>{"path"=>"/up", "port"=>3000 }}, @config.to_h)
|
||||
assert_equal({ :roles=>["web"], :hosts=>["1.1.1.1", "1.1.1.2"], :primary_host=>"1.1.1.1", :version=>"missing", :repository=>"dhh/app", :absolute_image=>"dhh/app:missing", :service_with_version=>"app-missing", :env_args=>["-e", "REDIS_URL=\"redis://x/y\""], :ssh_options=>{:user=>"root", :auth_methods=>["publickey"]}, :volume_args=>["--volume", "/local/path:/container/path"], :logging=>["--log-opt", "max-size=\"10m\""], :healthcheck=>{"path"=>"/up", "port"=>3000, "max_attempts" => 7 }}, @config.to_h)
|
||||
end
|
||||
end
|
||||
|
||||
9
test/fixtures/deploy_simple.yml
vendored
Normal file
9
test/fixtures/deploy_simple.yml
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
service: app
|
||||
image: dhh/app
|
||||
servers:
|
||||
- "1.1.1.1"
|
||||
- "1.1.1.2"
|
||||
registry:
|
||||
username: user
|
||||
password: pw
|
||||
audit_broadcast_cmd: "bin/audit_broadcast"
|
||||
13
test/fixtures/deploy_with_accessories.yml
vendored
13
test/fixtures/deploy_with_accessories.yml
vendored
@@ -1,8 +1,12 @@
|
||||
service: app
|
||||
image: dhh/app
|
||||
servers:
|
||||
- 1.1.1.1
|
||||
- 1.1.1.2
|
||||
web:
|
||||
- "1.1.1.1"
|
||||
- "1.1.1.2"
|
||||
workers:
|
||||
- "1.1.1.3"
|
||||
- "1.1.1.4"
|
||||
registry:
|
||||
username: user
|
||||
password: pw
|
||||
@@ -23,9 +27,10 @@ accessories:
|
||||
- data:/var/lib/mysql
|
||||
redis:
|
||||
image: redis:latest
|
||||
host: 1.1.1.4
|
||||
roles:
|
||||
- web
|
||||
port: 6379
|
||||
directories:
|
||||
- data:/data
|
||||
|
||||
readiness_delay: 0
|
||||
readiness_delay: 0
|
||||
|
||||
17
test/fixtures/deploy_with_boot_strategy.yml
vendored
Normal file
17
test/fixtures/deploy_with_boot_strategy.yml
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
service: app
|
||||
image: dhh/app
|
||||
servers:
|
||||
web:
|
||||
- "1.1.1.1"
|
||||
- "1.1.1.2"
|
||||
workers:
|
||||
- "1.1.1.3"
|
||||
- "1.1.1.4"
|
||||
|
||||
registry:
|
||||
username: user
|
||||
password: pw
|
||||
|
||||
boot:
|
||||
limit: 3
|
||||
wait: 2
|
||||
17
test/fixtures/deploy_with_precentage_boot_strategy.yml
vendored
Normal file
17
test/fixtures/deploy_with_precentage_boot_strategy.yml
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
service: app
|
||||
image: dhh/app
|
||||
servers:
|
||||
web:
|
||||
- "1.1.1.1"
|
||||
- "1.1.1.2"
|
||||
workers:
|
||||
- "1.1.1.3"
|
||||
- "1.1.1.4"
|
||||
|
||||
registry:
|
||||
username: user
|
||||
password: pw
|
||||
|
||||
boot:
|
||||
limit: 25%
|
||||
wait: 2
|
||||
5
test/fixtures/deploy_with_roles.yml
vendored
5
test/fixtures/deploy_with_roles.yml
vendored
@@ -5,8 +5,9 @@ servers:
|
||||
- 1.1.1.1
|
||||
- 1.1.1.2
|
||||
workers:
|
||||
- 1.1.1.3
|
||||
- 1.1.1.4
|
||||
hosts:
|
||||
- 1.1.1.3
|
||||
- 1.1.1.4
|
||||
env:
|
||||
REDIS_URL: redis://x/y
|
||||
registry:
|
||||
|
||||
132
test/integration/deploy_test.rb
Normal file
132
test/integration/deploy_test.rb
Normal file
@@ -0,0 +1,132 @@
|
||||
require "net/http"
|
||||
require "test_helper"
|
||||
|
||||
class DeployTest < ActiveSupport::TestCase
|
||||
|
||||
setup do
|
||||
docker_compose "up --build --force-recreate -d"
|
||||
wait_for_healthy
|
||||
end
|
||||
|
||||
teardown do
|
||||
docker_compose "down -v"
|
||||
end
|
||||
|
||||
test "deploy" do
|
||||
first_version = latest_app_version
|
||||
|
||||
assert_app_is_down
|
||||
|
||||
mrsk :deploy
|
||||
|
||||
assert_app_is_up version: first_version
|
||||
|
||||
second_version = update_app_rev
|
||||
|
||||
mrsk :redeploy
|
||||
|
||||
assert_app_is_up version: second_version
|
||||
|
||||
mrsk :rollback, first_version
|
||||
|
||||
assert_app_is_up version: first_version
|
||||
|
||||
details = mrsk :details, capture: true
|
||||
|
||||
assert_match /Traefik Host: vm1/, details
|
||||
assert_match /Traefik Host: vm2/, details
|
||||
assert_match /App Host: vm1/, details
|
||||
assert_match /App Host: vm2/, details
|
||||
assert_match /traefik:v2.9/, details
|
||||
assert_match /registry:4443\/app:#{first_version}/, details
|
||||
|
||||
audit = mrsk :audit, capture: true
|
||||
|
||||
assert_match /Booted app version #{first_version}.*Booted app version #{second_version}.*Booted app version #{first_version}.*/m, audit
|
||||
end
|
||||
|
||||
private
|
||||
def docker_compose(*commands, capture: false)
|
||||
command = "docker compose #{commands.join(" ")}"
|
||||
succeeded = false
|
||||
if capture
|
||||
result = stdouted { succeeded = system("cd test/integration && #{command}") }
|
||||
else
|
||||
succeeded = system("cd test/integration && #{command}")
|
||||
end
|
||||
|
||||
raise "Command `#{command}` failed with error code `#{$?}`" unless succeeded
|
||||
result
|
||||
end
|
||||
|
||||
def deployer_exec(*commands, **options)
|
||||
docker_compose("exec deployer #{commands.join(" ")}", **options)
|
||||
end
|
||||
|
||||
def mrsk(*commands, **options)
|
||||
deployer_exec(:mrsk, *commands, **options)
|
||||
end
|
||||
|
||||
def assert_app_is_down
|
||||
assert_equal "502", app_response.code
|
||||
end
|
||||
|
||||
def assert_app_is_up(version: nil)
|
||||
code = app_response.code
|
||||
if code != "200"
|
||||
puts "Got response code #{code}, here are the traefik logs:"
|
||||
mrsk :traefik, :logs
|
||||
puts "And here are the load balancer logs"
|
||||
docker_compose :logs, :load_balancer
|
||||
puts "Tried to get the response code again and got #{app_response.code}"
|
||||
end
|
||||
assert_equal "200", code
|
||||
assert_app_version(version) if version
|
||||
end
|
||||
|
||||
def assert_app_not_found
|
||||
assert_equal "404", app_response.code
|
||||
end
|
||||
|
||||
def wait_for_app_to_be_up(timeout: 10, up_count: 3)
|
||||
timeout_at = Time.now + timeout
|
||||
up_times = 0
|
||||
response = app_response
|
||||
while up_times < up_count && timeout_at > Time.now
|
||||
sleep 0.1
|
||||
up_times += 1 if response.code == "200"
|
||||
response = app_response
|
||||
end
|
||||
assert_equal up_times, up_count
|
||||
end
|
||||
|
||||
def app_response
|
||||
Net::HTTP.get_response(URI.parse("http://localhost:12345"))
|
||||
end
|
||||
|
||||
def update_app_rev
|
||||
deployer_exec "./update_app_rev.sh"
|
||||
latest_app_version
|
||||
end
|
||||
|
||||
def latest_app_version
|
||||
deployer_exec("cat version", capture: true)
|
||||
end
|
||||
|
||||
def assert_app_version(version)
|
||||
actual_version = Net::HTTP.get_response(URI.parse("http://localhost:12345/version")).body.strip
|
||||
|
||||
assert_equal version, actual_version
|
||||
end
|
||||
|
||||
def wait_for_healthy(timeout: 20)
|
||||
timeout_at = Time.now + timeout
|
||||
while docker_compose("ps -a | tail -n +2 | grep -v '(healthy)' | wc -l", capture: true) != "0"
|
||||
if timeout_at < Time.now
|
||||
docker_compose("ps -a | tail -n +2 | grep -v '(healthy)'")
|
||||
raise "Container not healthy after #{timeout} seconds" if timeout_at < Time.now
|
||||
end
|
||||
sleep 0.1
|
||||
end
|
||||
end
|
||||
end
|
||||
50
test/integration/docker-compose.yml
Normal file
50
test/integration/docker-compose.yml
Normal file
@@ -0,0 +1,50 @@
|
||||
version: "3.7"
|
||||
name: "mrsk-test"
|
||||
|
||||
volumes:
|
||||
shared:
|
||||
|
||||
services:
|
||||
shared:
|
||||
build:
|
||||
context: docker/shared
|
||||
volumes:
|
||||
- shared:/shared
|
||||
|
||||
deployer:
|
||||
privileged: true
|
||||
build:
|
||||
context: docker/deployer
|
||||
volumes:
|
||||
- ../..:/mrsk
|
||||
- shared:/shared
|
||||
|
||||
registry:
|
||||
build:
|
||||
context: docker/registry
|
||||
environment:
|
||||
- REGISTRY_HTTP_ADDR=0.0.0.0:4443
|
||||
- REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt
|
||||
- REGISTRY_HTTP_TLS_KEY=/certs/domain.key
|
||||
volumes:
|
||||
- shared:/shared
|
||||
|
||||
vm1:
|
||||
privileged: true
|
||||
build:
|
||||
context: docker/vm
|
||||
volumes:
|
||||
- shared:/shared
|
||||
|
||||
vm2:
|
||||
privileged: true
|
||||
build:
|
||||
context: docker/vm
|
||||
volumes:
|
||||
- shared:/shared
|
||||
|
||||
load_balancer:
|
||||
build:
|
||||
context: docker/load_balancer
|
||||
ports:
|
||||
- "12345:80"
|
||||
30
test/integration/docker/deployer/Dockerfile
Normal file
30
test/integration/docker/deployer/Dockerfile
Normal file
@@ -0,0 +1,30 @@
|
||||
FROM ruby:3.2
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
RUN apt-get update --fix-missing && apt-get install -y ca-certificates openssh-client curl gnupg docker.io
|
||||
|
||||
RUN install -m 0755 -d /etc/apt/keyrings
|
||||
RUN curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
|
||||
RUN chmod a+r /etc/apt/keyrings/docker.gpg
|
||||
RUN echo \
|
||||
"deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \
|
||||
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
|
||||
tee /etc/apt/sources.list.d/docker.list > /dev/null
|
||||
|
||||
RUN apt-get update --fix-missing && apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
|
||||
|
||||
COPY *.sh .
|
||||
COPY app/ .
|
||||
|
||||
RUN ln -s /shared/ssh /root/.ssh
|
||||
RUN mkdir -p /etc/docker/certs.d/registry:4443 && ln -s /shared/certs/domain.crt /etc/docker/certs.d/registry:4443/ca.crt
|
||||
|
||||
RUN git config --global user.email "deployer@example.com"
|
||||
RUN git config --global user.name "Deployer"
|
||||
RUN git init && git add . && git commit -am "Initial version"
|
||||
RUN git rev-parse HEAD > version
|
||||
|
||||
HEALTHCHECK --interval=1s CMD pgrep sleep
|
||||
|
||||
CMD ["./boot.sh"]
|
||||
4
test/integration/docker/deployer/app/Dockerfile
Normal file
4
test/integration/docker/deployer/app/Dockerfile
Normal file
@@ -0,0 +1,4 @@
|
||||
FROM nginx:1-alpine-slim
|
||||
|
||||
COPY default.conf /etc/nginx/conf.d/default.conf
|
||||
COPY version /usr/share/nginx/html/version
|
||||
17
test/integration/docker/deployer/app/config/deploy.yml
Normal file
17
test/integration/docker/deployer/app/config/deploy.yml
Normal file
@@ -0,0 +1,17 @@
|
||||
service: app
|
||||
image: app
|
||||
servers:
|
||||
- vm1
|
||||
- vm2
|
||||
registry:
|
||||
server: registry:4443
|
||||
username: root
|
||||
password: root
|
||||
builder:
|
||||
multiarch: false
|
||||
healthcheck:
|
||||
cmd: wget -qO- http://localhost > /dev/null
|
||||
traefik:
|
||||
args:
|
||||
accesslog: true
|
||||
accesslog.format: json
|
||||
17
test/integration/docker/deployer/app/default.conf
Normal file
17
test/integration/docker/deployer/app/default.conf
Normal file
@@ -0,0 +1,17 @@
|
||||
server {
|
||||
listen 80;
|
||||
listen [::]:80;
|
||||
server_name localhost;
|
||||
|
||||
location / {
|
||||
root /usr/share/nginx/html;
|
||||
index index.html index.htm;
|
||||
}
|
||||
|
||||
# redirect server error pages to the static page /50x.html
|
||||
#
|
||||
error_page 500 502 503 504 /50x.html;
|
||||
location = /50x.html {
|
||||
root /usr/share/nginx/html;
|
||||
}
|
||||
}
|
||||
9
test/integration/docker/deployer/boot.sh
Executable file
9
test/integration/docker/deployer/boot.sh
Executable file
@@ -0,0 +1,9 @@
|
||||
#!/bin/bash
|
||||
|
||||
cd /mrsk && gem build mrsk.gemspec -o /tmp/mrsk.gem && gem install /tmp/mrsk.gem
|
||||
|
||||
dockerd &
|
||||
|
||||
trap "pkill -f sleep" term
|
||||
|
||||
sleep infinity & wait
|
||||
4
test/integration/docker/deployer/update_app_rev.sh
Executable file
4
test/integration/docker/deployer/update_app_rev.sh
Executable file
@@ -0,0 +1,4 @@
|
||||
#!/bin/bash
|
||||
|
||||
git commit -am 'Update rev' --amend
|
||||
git rev-parse HEAD > version
|
||||
5
test/integration/docker/load_balancer/Dockerfile
Normal file
5
test/integration/docker/load_balancer/Dockerfile
Normal file
@@ -0,0 +1,5 @@
|
||||
FROM nginx:1-alpine-slim
|
||||
|
||||
COPY default.conf /etc/nginx/conf.d/default.conf
|
||||
|
||||
HEALTHCHECK --interval=1s CMD pgrep nginx
|
||||
12
test/integration/docker/load_balancer/default.conf
Normal file
12
test/integration/docker/load_balancer/default.conf
Normal file
@@ -0,0 +1,12 @@
|
||||
upstream loadbalancer {
|
||||
server vm1:80;
|
||||
server vm2:80;
|
||||
}
|
||||
|
||||
server {
|
||||
listen 80;
|
||||
|
||||
location / {
|
||||
proxy_pass http://loadbalancer;
|
||||
}
|
||||
}
|
||||
9
test/integration/docker/registry/Dockerfile
Normal file
9
test/integration/docker/registry/Dockerfile
Normal file
@@ -0,0 +1,9 @@
|
||||
FROM registry
|
||||
|
||||
COPY boot.sh .
|
||||
|
||||
RUN ln -s /shared/certs /certs
|
||||
|
||||
HEALTHCHECK --interval=1s CMD pgrep registry
|
||||
|
||||
ENTRYPOINT ["./boot.sh"]
|
||||
7
test/integration/docker/registry/boot.sh
Executable file
7
test/integration/docker/registry/boot.sh
Executable file
@@ -0,0 +1,7 @@
|
||||
#!/bin/sh
|
||||
|
||||
while [ ! -f /certs/domain.crt ]; do sleep 1; done
|
||||
|
||||
trap "pkill -f registry" term
|
||||
|
||||
/entrypoint.sh /etc/docker/registry/config.yml & wait
|
||||
17
test/integration/docker/shared/Dockerfile
Normal file
17
test/integration/docker/shared/Dockerfile
Normal file
@@ -0,0 +1,17 @@
|
||||
FROM ubuntu:22.10
|
||||
|
||||
WORKDIR /work
|
||||
|
||||
RUN apt-get update --fix-missing && apt-get -y install openssh-client openssl
|
||||
|
||||
RUN mkdir ssh && \
|
||||
ssh-keygen -t rsa -f ssh/id_rsa -N ""
|
||||
|
||||
COPY registry-dns.conf .
|
||||
COPY boot.sh .
|
||||
|
||||
RUN mkdir certs && openssl req -newkey rsa:4096 -nodes -sha256 -keyout certs/domain.key -x509 -days 365 -out certs/domain.crt -subj '/CN=registry' -extensions EXT -config registry-dns.conf
|
||||
|
||||
HEALTHCHECK --interval=1s CMD pgrep sleep
|
||||
|
||||
CMD ["./boot.sh"]
|
||||
7
test/integration/docker/shared/boot.sh
Executable file
7
test/integration/docker/shared/boot.sh
Executable file
@@ -0,0 +1,7 @@
|
||||
#!/bin/bash
|
||||
|
||||
cp -r * /shared
|
||||
|
||||
trap "pkill -f sleep" term
|
||||
|
||||
sleep infinity & wait
|
||||
7
test/integration/docker/shared/registry-dns.conf
Normal file
7
test/integration/docker/shared/registry-dns.conf
Normal file
@@ -0,0 +1,7 @@
|
||||
[dn]
|
||||
CN=registry
|
||||
[req]
|
||||
distinguished_name = dn
|
||||
[EXT]
|
||||
subjectAltName=DNS:registry
|
||||
keyUsage=digitalSignature
|
||||
14
test/integration/docker/vm/Dockerfile
Normal file
14
test/integration/docker/vm/Dockerfile
Normal file
@@ -0,0 +1,14 @@
|
||||
FROM ubuntu:22.10
|
||||
|
||||
WORKDIR /work
|
||||
|
||||
RUN apt-get update --fix-missing && apt-get -y install openssh-client openssh-server docker.io
|
||||
|
||||
RUN mkdir /root/.ssh && ln -s /shared/ssh/id_rsa.pub /root/.ssh/authorized_keys
|
||||
RUN mkdir -p /etc/docker/certs.d/registry:4443 && ln -s /shared/certs/domain.crt /etc/docker/certs.d/registry:4443/ca.crt
|
||||
|
||||
COPY boot.sh .
|
||||
|
||||
HEALTHCHECK --interval=1s CMD pgrep dockerd
|
||||
|
||||
CMD ["./boot.sh"]
|
||||
11
test/integration/docker/vm/boot.sh
Executable file
11
test/integration/docker/vm/boot.sh
Executable file
@@ -0,0 +1,11 @@
|
||||
#!/bin/bash
|
||||
|
||||
while [ ! -f /root/.ssh/authorized_keys ]; do echo "Waiting for ssh keys"; sleep 1; done
|
||||
|
||||
service ssh restart
|
||||
|
||||
dockerd &
|
||||
|
||||
trap "pkill -f sleep" term
|
||||
|
||||
sleep infinity & wait
|
||||
@@ -1,6 +1,7 @@
|
||||
require "bundler/setup"
|
||||
require "active_support/test_case"
|
||||
require "active_support/testing/autorun"
|
||||
require "active_support/testing/stream"
|
||||
require "debug"
|
||||
require "mocha/minitest" # using #stubs that can alter returns
|
||||
require "minitest/autorun" # using #stub that take args
|
||||
@@ -9,7 +10,28 @@ require "mrsk"
|
||||
|
||||
ActiveSupport::LogSubscriber.logger = ActiveSupport::Logger.new(STDOUT) if ENV["VERBOSE"]
|
||||
|
||||
# Applies to remote commands only.
|
||||
SSHKit.config.backend = SSHKit::Backend::Printer
|
||||
|
||||
class ActiveSupport::TestCase
|
||||
# Ensure local commands use the printer backend too.
|
||||
# See https://github.com/capistrano/sshkit/blob/master/lib/sshkit/dsl.rb#L9
|
||||
module SSHKit
|
||||
module DSL
|
||||
def run_locally(&block)
|
||||
SSHKit::Backend::Printer.new(SSHKit::Host.new(:local), &block).run
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class ActiveSupport::TestCase
|
||||
include ActiveSupport::Testing::Stream
|
||||
|
||||
private
|
||||
def stdouted
|
||||
capture(:stdout) { yield }.strip
|
||||
end
|
||||
|
||||
def stderred
|
||||
capture(:stderr) { yield }.strip
|
||||
end
|
||||
end
|
||||
|
||||
64
test/utils_test.rb
Normal file
64
test/utils_test.rb
Normal file
@@ -0,0 +1,64 @@
|
||||
require "test_helper"
|
||||
|
||||
class UtilsTest < ActiveSupport::TestCase
|
||||
test "argumentize" do
|
||||
assert_equal [ "--label", "foo=\"\\`bar\\`\"", "--label", "baz=\"qux\"", "--label", :quux ], \
|
||||
Mrsk::Utils.argumentize("--label", { foo: "`bar`", baz: "qux", quux: nil })
|
||||
end
|
||||
|
||||
test "argumentize with redacted" do
|
||||
assert_kind_of SSHKit::Redaction, \
|
||||
Mrsk::Utils.argumentize("--label", { foo: "bar" }, sensitive: true).last
|
||||
end
|
||||
|
||||
test "argumentize_env_with_secrets" do
|
||||
ENV.expects(:fetch).with("FOO").returns("secret")
|
||||
|
||||
args = Mrsk::Utils.argumentize_env_with_secrets({ "secret" => [ "FOO" ], "clear" => { BAZ: "qux" } })
|
||||
|
||||
assert_equal [ "-e", "FOO=[REDACTED]", "-e", "BAZ=\"qux\"" ], Mrsk::Utils.redacted(args)
|
||||
assert_equal [ "-e", "FOO=\"secret\"", "-e", "BAZ=\"qux\"" ], Mrsk::Utils.unredacted(args)
|
||||
end
|
||||
|
||||
test "optionize" do
|
||||
assert_equal [ "--foo", "\"bar\"", "--baz", "\"qux\"", "--quux" ], \
|
||||
Mrsk::Utils.optionize({ foo: "bar", baz: "qux", quux: true })
|
||||
end
|
||||
|
||||
test "optionize with" do
|
||||
assert_equal [ "--foo=\"bar\"", "--baz=\"qux\"", "--quux" ], \
|
||||
Mrsk::Utils.optionize({ foo: "bar", baz: "qux", quux: true }, with: "=")
|
||||
end
|
||||
|
||||
test "no redaction from #to_s" do
|
||||
assert_equal "secret", Mrsk::Utils.sensitive("secret").to_s
|
||||
end
|
||||
|
||||
test "redact from #inspect" do
|
||||
assert_equal "[REDACTED]".inspect, Mrsk::Utils.sensitive("secret").inspect
|
||||
end
|
||||
|
||||
test "redact from SSHKit output" do
|
||||
assert_kind_of SSHKit::Redaction, Mrsk::Utils.sensitive("secret")
|
||||
end
|
||||
|
||||
test "redact from YAML output" do
|
||||
assert_equal "--- ! '[REDACTED]'\n", YAML.dump(Mrsk::Utils.sensitive("secret"))
|
||||
end
|
||||
|
||||
test "escape_shell_value" do
|
||||
assert_equal "\"foo\"", Mrsk::Utils.escape_shell_value("foo")
|
||||
assert_equal "\"\\`foo\\`\"", Mrsk::Utils.escape_shell_value("`foo`")
|
||||
|
||||
assert_equal "\"${PWD}\"", Mrsk::Utils.escape_shell_value("${PWD}")
|
||||
assert_equal "\"${cat /etc/hostname}\"", Mrsk::Utils.escape_shell_value("${cat /etc/hostname}")
|
||||
assert_equal "\"\\${PWD]\"", Mrsk::Utils.escape_shell_value("${PWD]")
|
||||
assert_equal "\"\\$(PWD)\"", Mrsk::Utils.escape_shell_value("$(PWD)")
|
||||
assert_equal "\"\\$PWD\"", Mrsk::Utils.escape_shell_value("$PWD")
|
||||
|
||||
assert_equal "\"^(https?://)www.example.com/(.*)\\$\"",
|
||||
Mrsk::Utils.escape_shell_value("^(https?://)www.example.com/(.*)$")
|
||||
assert_equal "\"https://example.com/\\$2\"",
|
||||
Mrsk::Utils.escape_shell_value("https://example.com/$2")
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user