Compare commits
55 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
be89077917 | ||
|
|
6bfcc582c8 | ||
|
|
fd5172266e | ||
|
|
e85c8161df | ||
|
|
a1fc01639e | ||
|
|
7e764cbcd9 | ||
|
|
f177ee4cfe | ||
|
|
ea9a50ec95 | ||
|
|
6ea06fd04e | ||
|
|
6ccb3d2319 | ||
|
|
05f1ef5ee8 | ||
|
|
f1a98457b0 | ||
|
|
7ae596ef60 | ||
|
|
2257c99189 | ||
|
|
5afadb10ca | ||
|
|
b3992973d6 | ||
|
|
08c30a14b9 | ||
|
|
76d34d2a1c | ||
|
|
184ab18667 | ||
|
|
87abf06076 | ||
|
|
453570b895 | ||
|
|
f61beb6827 | ||
|
|
c481938cdb | ||
|
|
7e9b73f86a | ||
|
|
1f06b1ff94 | ||
|
|
d554ae8500 | ||
|
|
730de486b7 | ||
|
|
b333c4a05b | ||
|
|
eec6670dbf | ||
|
|
4aa96d6578 | ||
|
|
d3ab10be22 | ||
|
|
d92318e234 | ||
|
|
e62610069b | ||
|
|
a0582c1bdf | ||
|
|
880ce46c39 | ||
|
|
d049d73547 | ||
|
|
453fea6c45 | ||
|
|
2694cf5d5f | ||
|
|
5324fbe3d0 | ||
|
|
5e214cde3c | ||
|
|
f61f41ad73 | ||
|
|
d9cdbb87f9 | ||
|
|
543af475d5 | ||
|
|
1bb9fe9095 | ||
|
|
c6fd4399f1 | ||
|
|
a4a9f619ad | ||
|
|
4392bf0ee9 | ||
|
|
3b3ab48120 | ||
|
|
606550d46b | ||
|
|
e1b327915f | ||
|
|
9d3871d667 | ||
|
|
7d83be2d18 | ||
|
|
3e2c48782c | ||
|
|
bcdeeff94f | ||
|
|
c5249b4a9e |
@@ -1,7 +1,7 @@
|
|||||||
PATH
|
PATH
|
||||||
remote: .
|
remote: .
|
||||||
specs:
|
specs:
|
||||||
mrsk (0.0.2)
|
mrsk (0.0.3)
|
||||||
railties (>= 7.0.0)
|
railties (>= 7.0.0)
|
||||||
sshkit (~> 1.21)
|
sshkit (~> 1.21)
|
||||||
|
|
||||||
|
|||||||
153
README.md
153
README.md
@@ -1,22 +1,18 @@
|
|||||||
# MRSK
|
# MRSK
|
||||||
|
|
||||||
MRSK ships zero-downtime deploys of Rails apps packed as containers to any host. It uses the dynamic reverse-proxy Traefik to hold requests while the new application container is started and the old one is wound down. It works across multiple hosts at the same time, using SSHKit to execute commands.
|
MRSK ships zero-downtime deploys of Rails apps packed as containers to any host. It uses the dynamic reverse-proxy Traefik to hold requests while the new application container is started and the old one is wound down. It works seamlessly across multiple hosts, using SSHKit to execute commands.
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
Add the gem with `bundle add mrsk`, then run `rake mrsk:init`, and then edit the new file in `config/deploy.yml` to use the proper service name, image reference, servers to deploy on, and so on. It could look something like this:
|
Add the gem with `bundle add mrsk`, then run `rake mrsk:init`, and then edit the new file in `config/deploy.yml`. It could look as simple as this:
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
service: hey
|
service: hey
|
||||||
image: 37s/hey
|
image: 37s/hey
|
||||||
servers:
|
servers:
|
||||||
- xxx.xxx.xxx.xxx
|
- 192.168.0.1
|
||||||
- xxx.xxx.xxx.xxx
|
- 192.168.0.2
|
||||||
env:
|
|
||||||
DATABASE_URL: mysql2://db1/hey_production/
|
|
||||||
REDIS_URL: redis://redis1:6379/1
|
|
||||||
registry:
|
registry:
|
||||||
server: registry.digitalocean.com
|
|
||||||
username: <%= Rails.application.credentials.registry["username"] %>
|
username: <%= Rails.application.credentials.registry["username"] %>
|
||||||
password: <%= Rails.application.credentials.registry["password"] %>
|
password: <%= Rails.application.credentials.registry["password"] %>
|
||||||
```
|
```
|
||||||
@@ -26,13 +22,13 @@ Then ensure your encrypted credentials have the registry username + password by
|
|||||||
```
|
```
|
||||||
registry:
|
registry:
|
||||||
username: real-user-name
|
username: real-user-name
|
||||||
password: real-password
|
password: real-registry-password-or-token
|
||||||
```
|
```
|
||||||
|
|
||||||
Now you're ready to deploy a multi-arch image (FIXME: currently you need to manually run `docker buildx create --use` once first):
|
Now you're ready to deploy a multi-arch image to the servers:
|
||||||
|
|
||||||
```
|
```
|
||||||
rake mrsk:deploy
|
./bin/mrsk deploy
|
||||||
```
|
```
|
||||||
|
|
||||||
This will:
|
This will:
|
||||||
@@ -48,33 +44,123 @@ This will:
|
|||||||
|
|
||||||
Voila! All the servers are now serving the app on port 80. If you're just running a single server, you're ready to go. If you're running multiple servers, you need to put a load balancer in front of them.
|
Voila! All the servers are now serving the app on port 80. If you're just running a single server, you're ready to go. If you're running multiple servers, you need to put a load balancer in front of them.
|
||||||
|
|
||||||
## Operations
|
## Configuration
|
||||||
|
|
||||||
### Running job hosts separately
|
### Using another registry than Docker Hub
|
||||||
|
|
||||||
If your application uses separate job running hosts, or other roles beyond the default web running, you can specify these hosts and their custom command like so:
|
The default registry for Docker is Docker Hub. If you'd like to use a different one, just configure the server, like so:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
registry:
|
||||||
|
server: registry.digitalocean.com
|
||||||
|
username: <%= Rails.application.credentials.registry["username"] %>
|
||||||
|
password: <%= Rails.application.credentials.registry["password"] %>
|
||||||
|
```
|
||||||
|
|
||||||
|
### Using a different SSH user than root
|
||||||
|
|
||||||
|
The default SSH user is root, but you can change it using `ssh_user`:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
ssh_user: app
|
||||||
|
```
|
||||||
|
|
||||||
|
### Adding custom env variables
|
||||||
|
|
||||||
|
You can inject custom env variables into the app containers using `env`:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
env:
|
||||||
|
DATABASE_URL: mysql2://db1/hey_production/
|
||||||
|
REDIS_URL: redis://redis1:6379/1
|
||||||
|
```
|
||||||
|
|
||||||
|
### Splitting servers into different roles
|
||||||
|
|
||||||
|
If your application uses separate hosts for running jobs or other roles beyond the default web running, you can specify these hosts and their custom entrypoint command like so:
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
servers:
|
servers:
|
||||||
web:
|
web:
|
||||||
- xxx.xxx.xxx.xxx
|
- 192.168.0.1
|
||||||
- xxx.xxx.xxx.xxx
|
- 192.168.0.2
|
||||||
job:
|
job:
|
||||||
hosts:
|
hosts:
|
||||||
- xxx.xxx.xxx.xxx
|
- 192.168.0.3
|
||||||
- xxx.xxx.xxx.xxx
|
- 192.168.0.4
|
||||||
cmd: bin/jobs
|
cmd: bin/jobs
|
||||||
```
|
```
|
||||||
|
|
||||||
The application will be deployed to all hosts, but only those in the `web` role will be labeled to run under traefik. If you want to run custom commands on all hosts in a role, you can use `rake mrsk:app:exec:rails CMD=about ROLES=job`.
|
Traefik will only be installed and run on the servers in the `web` role (and on all servers if no roles are defined).
|
||||||
|
|
||||||
### Executing commands
|
### Adding custom container labels
|
||||||
|
|
||||||
If you need to execute commands inside the Rails containers, you can use `rake mrsk:app:exec`, `rake mrsk:app:exec:once`, `rake mrsk:app:exec:rails`, and `rake mrsk:app:exec:once:rails`. Examples:
|
You can specialize the default Traefik rules by setting custom labels on the containers that are being started:
|
||||||
|
|
||||||
|
```
|
||||||
|
labels:
|
||||||
|
traefik.http.routers.hey.rule: '''Host(`app.hey.com`)'''
|
||||||
|
```
|
||||||
|
|
||||||
|
(Note: The extra quotes are needed to ensure the rule is passed in correctly!)
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
The labels can even be applied on a per-role basis:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
servers:
|
||||||
|
web:
|
||||||
|
- 192.168.0.1
|
||||||
|
- 192.168.0.2
|
||||||
|
job:
|
||||||
|
hosts:
|
||||||
|
- 192.168.0.3
|
||||||
|
- 192.168.0.4
|
||||||
|
cmd: bin/jobs
|
||||||
|
labels:
|
||||||
|
my-custom-label: "50"
|
||||||
|
```
|
||||||
|
|
||||||
|
### Configuring 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 have to use multi-archecture images. By default, MRSK will setup a local buildx configuration that allows for this through QEMU emulation. This can be slow, especially on the first build.
|
||||||
|
|
||||||
|
If you want to speed up this process by using a remote AMD64 host to natively build the AMD64 part of the image, while natively building the ARM64 part locally, you can do so using builder options like follows:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
builder:
|
||||||
|
local:
|
||||||
|
arch: arm64
|
||||||
|
host: unix:///Users/dhh/.docker/run/docker.sock
|
||||||
|
remote:
|
||||||
|
arch: amd64
|
||||||
|
host: ssh://root@192.168.0.1
|
||||||
|
```
|
||||||
|
|
||||||
|
Note: You must have Docker running on the remote host being used as a builder.
|
||||||
|
|
||||||
|
With that configuration in place, you can setup the local/remote configuration using `./bin/mrsk build:remote:create`. If you wish to remove the contexts and buildx instances again, you can run `./bin/mrsk build:remote:remove`. If you had already built using the standard emulation setup, run `./bin/mrsk build:remove` before doing `./bin/mrsk build:remote:create`.
|
||||||
|
|
||||||
|
### Configuring native builder when multi-arch isn't needed
|
||||||
|
|
||||||
|
If you're developing on the same architecture as the one you're deploying on, you can speed up the build a lot by forgoing a multi-arch image. This can be done by configuring the builder like so:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
builder:
|
||||||
|
multiarch: false
|
||||||
|
```
|
||||||
|
|
||||||
|
## Commands
|
||||||
|
|
||||||
|
### Remote execution
|
||||||
|
|
||||||
|
If you need to execute commands inside the Rails containers, you can use `./bin/mrsk app:exec`, `./bin/mrsk app:exec:once`, `./bin/mrsk app:exec:rails`, and `./bin/mrsk app:exec:once:rails`. Examples:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Runs command on all servers
|
# Runs command on all servers
|
||||||
rake mrsk:app:exec CMD='ruby -v'
|
./bin/mrsk app:exec CMD='ruby -v'
|
||||||
App Host: xxx.xxx.xxx.xxx
|
App Host: xxx.xxx.xxx.xxx
|
||||||
ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [x86_64-linux]
|
ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [x86_64-linux]
|
||||||
|
|
||||||
@@ -82,11 +168,11 @@ App Host: xxx.xxx.xxx.xxx
|
|||||||
ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [x86_64-linux]
|
ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [x86_64-linux]
|
||||||
|
|
||||||
# Runs command on first server
|
# Runs command on first server
|
||||||
rake mrsk:app:exec:once CMD='cat .ruby-version'
|
./bin/mrsk app:exec:once CMD='cat .ruby-version'
|
||||||
3.1.3
|
3.1.3
|
||||||
|
|
||||||
# Runs Rails command on all servers
|
# Runs Rails command on all servers
|
||||||
rake mrsk:app:exec:rails CMD=about
|
./bin/mrsk app:exec:rails CMD=about
|
||||||
App Host: xxx.xxx.xxx.xxx
|
App Host: xxx.xxx.xxx.xxx
|
||||||
About your application's environment
|
About your application's environment
|
||||||
Rails version 7.1.0.alpha
|
Rails version 7.1.0.alpha
|
||||||
@@ -112,14 +198,18 @@ Database adapter sqlite3
|
|||||||
Database schema version 20221231233303
|
Database schema version 20221231233303
|
||||||
|
|
||||||
# Runs Rails command on first server
|
# Runs Rails command on first server
|
||||||
rake mrsk:app:exec:once:rails CMD='db:version'
|
./bin/mrsk app:exec:once:rails CMD='db:version'
|
||||||
database: storage/production.sqlite3
|
database: storage/production.sqlite3
|
||||||
Current version: 20221231233303
|
Current version: 20221231233303
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Running a Rails console on the primary host
|
||||||
|
|
||||||
|
If you need to interact with the production console for the app, you can use `./bin/mrsk app:console`, which will start a Rails console session on the primary host. Be mindful that this is a live wire! Any changes made to the production database will take effect immeditately.
|
||||||
|
|
||||||
### Inspecting
|
### Inspecting
|
||||||
|
|
||||||
You can see the state of your servers by running `rake mrsk:info`. It'll show something like this:
|
You can see the state of your servers by running `./bin/mrsk info`. It'll show something like this:
|
||||||
|
|
||||||
```
|
```
|
||||||
Traefik Host: xxx.xxx.xxx.xxx
|
Traefik Host: xxx.xxx.xxx.xxx
|
||||||
@@ -139,11 +229,11 @@ CONTAINER ID IMAGE
|
|||||||
1d3c91ed1f55 registry.digitalocean.com/user/app:6ef8a6a84c525b123c5245345a8483f86d05a123 "/rails/bin/docker-e…" 13 minutes ago Up 13 minutes 3000/tcp chat-6ef8a6a84c525b123c5245345a8483f86d05a123
|
1d3c91ed1f55 registry.digitalocean.com/user/app:6ef8a6a84c525b123c5245345a8483f86d05a123 "/rails/bin/docker-e…" 13 minutes ago Up 13 minutes 3000/tcp chat-6ef8a6a84c525b123c5245345a8483f86d05a123
|
||||||
```
|
```
|
||||||
|
|
||||||
You can also see just info for app containers with `rake mrsk:app:info` or just for Traefik with `rake mrsk:traefik:info`.
|
You can also see just info for app containers with `./bin/mrsk app:info` or just for Traefik with `./bin/mrsk traefik:info`.
|
||||||
|
|
||||||
### Rollback
|
### Rollback
|
||||||
|
|
||||||
If you've discovered a bad deploy, you can quickly rollback by reactivating the old, paused container image. You can see what old containers are available for rollback by running `rake mrsk:app:containers`. It'll give you a presentation similar to `rake mrsk:app:info`, but include all the old containers as well. Showing something like this:
|
If you've discovered a bad deploy, you can quickly rollback by reactivating the old, paused container image. You can see what old containers are available for rollback by running `./bin/mrsk app:containers`. It'll give you a presentation similar to `./bin/mrsk app:info`, but include all the old containers as well. Showing something like this:
|
||||||
|
|
||||||
```
|
```
|
||||||
App Host: 164.92.105.119
|
App Host: 164.92.105.119
|
||||||
@@ -157,20 +247,19 @@ badb1aa51db4 registry.digitalocean.com/user/app:6ef8a6a84c525b123c5245345a8483
|
|||||||
6f170d1172ae registry.digitalocean.com/user/app:e5d9d7c2b898289dfbc5f7f1334140d984eedae4 "/rails/bin/docker-e…" 31 minutes ago Exited (1) 27 minutes ago chat-e5d9d7c2b898289dfbc5f7f1334140d984eedae4
|
6f170d1172ae registry.digitalocean.com/user/app:e5d9d7c2b898289dfbc5f7f1334140d984eedae4 "/rails/bin/docker-e…" 31 minutes ago Exited (1) 27 minutes ago chat-e5d9d7c2b898289dfbc5f7f1334140d984eedae4
|
||||||
```
|
```
|
||||||
|
|
||||||
From the example above, we can see that `e5d9d7c2b898289dfbc5f7f1334140d984eedae4` was the last version, so it's available as a rollback target. We can perform this rollback by running `rake mrsk:rollback VERSION=e5d9d7c2b898289dfbc5f7f1334140d984eedae4`. That'll stop `6ef8a6a84c525b123c5245345a8483f86d05a123` and then start `e5d9d7c2b898289dfbc5f7f1334140d984eedae4`. Because the old container is still available, this is very quick. Nothing to download from the registry.
|
From the example above, we can see that `e5d9d7c2b898289dfbc5f7f1334140d984eedae4` was the last version, so it's available as a rollback target. We can perform this rollback by running `./bin/mrsk rollback VERSION=e5d9d7c2b898289dfbc5f7f1334140d984eedae4`. That'll stop `6ef8a6a84c525b123c5245345a8483f86d05a123` and then start `e5d9d7c2b898289dfbc5f7f1334140d984eedae4`. Because the old container is still available, this is very quick. Nothing to download from the registry.
|
||||||
|
|
||||||
Note that by default old containers are pruned after 3 days when you run `rake mrsk:deploy`.
|
Note that by default old containers are pruned after 3 days when you run `./bin/mrsk deploy`.
|
||||||
|
|
||||||
### Removing
|
### Removing
|
||||||
|
|
||||||
If you wish to remove the entire application, including Traefik, containers, images, and registry session, you can run `rake mrsk:remove`. This will leave the servers clean.
|
If you wish to remove the entire application, including Traefik, containers, images, and registry session, you can run `./bin/mrsk remove`. This will leave the servers clean.
|
||||||
|
|
||||||
## Stage of development
|
## Stage of development
|
||||||
|
|
||||||
This is alpha software. Lots of stuff is missing. Here are some of the areas we seek to improve:
|
This is alpha software. Lots of stuff is missing. Here are some of the areas we seek to improve:
|
||||||
|
|
||||||
- Adapterize commands to work with Podman and other container runners
|
- Adapterize commands to work with Podman and other container runners
|
||||||
- Possibly switching to a bin/mrsk command rather than raw rake
|
|
||||||
- Integrate with cloud CI pipelines
|
- Integrate with cloud CI pipelines
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|||||||
@@ -3,6 +3,4 @@ end
|
|||||||
|
|
||||||
require "mrsk/version"
|
require "mrsk/version"
|
||||||
require "mrsk/engine"
|
require "mrsk/engine"
|
||||||
|
require "mrsk/commander"
|
||||||
require "mrsk/configuration"
|
|
||||||
require "mrsk/commands"
|
|
||||||
|
|||||||
56
lib/mrsk/commander.rb
Normal file
56
lib/mrsk/commander.rb
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
require "mrsk/configuration"
|
||||||
|
require "mrsk/commands/app"
|
||||||
|
require "mrsk/commands/builder"
|
||||||
|
require "mrsk/commands/prune"
|
||||||
|
require "mrsk/commands/traefik"
|
||||||
|
require "mrsk/commands/registry"
|
||||||
|
|
||||||
|
class Mrsk::Commander
|
||||||
|
attr_reader :config_file, :config, :verbose
|
||||||
|
|
||||||
|
def initialize(config_file:, verbose: false)
|
||||||
|
@config_file, @verbose = config_file, verbose
|
||||||
|
end
|
||||||
|
|
||||||
|
def config
|
||||||
|
@config ||= Mrsk::Configuration.load_file(config_file).tap { |config| setup_with(config) }
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
def app
|
||||||
|
@app ||= Mrsk::Commands::App.new(config)
|
||||||
|
end
|
||||||
|
|
||||||
|
def builder
|
||||||
|
@builder ||= Mrsk::Commands::Builder.new(config)
|
||||||
|
end
|
||||||
|
|
||||||
|
def traefik
|
||||||
|
@traefik ||= Mrsk::Commands::Traefik.new(config)
|
||||||
|
end
|
||||||
|
|
||||||
|
def registry
|
||||||
|
@registry ||= Mrsk::Commands::Registry.new(config)
|
||||||
|
end
|
||||||
|
|
||||||
|
def prune
|
||||||
|
@prune ||= Mrsk::Commands::Prune.new(config)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
def verbosity(level)
|
||||||
|
old_level = SSHKit.config.output_verbosity
|
||||||
|
SSHKit.config.output_verbosity = level
|
||||||
|
yield
|
||||||
|
ensure
|
||||||
|
SSHKit.config.output_verbosity = old_level
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
# Lazy setup of SSHKit
|
||||||
|
def setup_with(config)
|
||||||
|
SSHKit::Backend::Netssh.configure { |ssh| ssh.ssh_options = config.ssh_options }
|
||||||
|
SSHKit.config.command_map[:docker] = "docker" # No need to use /usr/bin/env, just clogs up the logs
|
||||||
|
SSHKit.config.output_verbosity = :debug if verbose
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,25 +1,2 @@
|
|||||||
require "sshkit"
|
|
||||||
|
|
||||||
module Mrsk::Commands
|
module Mrsk::Commands
|
||||||
class Base
|
|
||||||
attr_accessor :config
|
|
||||||
|
|
||||||
def initialize(config)
|
|
||||||
@config = config
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
def docker(*args)
|
|
||||||
args.compact.unshift :docker
|
|
||||||
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
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
require "mrsk/commands/app"
|
|
||||||
require "mrsk/commands/traefik"
|
|
||||||
require "mrsk/commands/registry"
|
|
||||||
|
|||||||
@@ -1,14 +1,6 @@
|
|||||||
|
require "mrsk/commands/base"
|
||||||
|
|
||||||
class Mrsk::Commands::App < Mrsk::Commands::Base
|
class Mrsk::Commands::App < Mrsk::Commands::Base
|
||||||
def push
|
|
||||||
# TODO: Run 'docker buildx create --use' when needed
|
|
||||||
# TODO: Make multiarch an option so Linux users can enjoy speedier builds
|
|
||||||
docker :buildx, :build, "--push", "--platform linux/amd64,linux/arm64", "-t", config.absolute_image, "."
|
|
||||||
end
|
|
||||||
|
|
||||||
def pull
|
|
||||||
docker :pull, config.absolute_image
|
|
||||||
end
|
|
||||||
|
|
||||||
def run(role: :web)
|
def run(role: :web)
|
||||||
role = config.role(role)
|
role = config.role(role)
|
||||||
|
|
||||||
@@ -36,17 +28,22 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def logs
|
def logs
|
||||||
[ "docker ps -q #{service_filter.join(" ")} | xargs docker logs -f" ]
|
[ "docker ps -q #{service_filter.join(" ")} | xargs docker logs -n 100 -t" ]
|
||||||
end
|
end
|
||||||
|
|
||||||
def exec(*command)
|
def exec(*command, interactive: false)
|
||||||
docker :exec,
|
docker :exec,
|
||||||
|
("-it" if interactive),
|
||||||
"-e", redact("RAILS_MASTER_KEY=#{config.master_key}"),
|
"-e", redact("RAILS_MASTER_KEY=#{config.master_key}"),
|
||||||
*config.env_args,
|
*config.env_args,
|
||||||
config.service_with_version,
|
config.service_with_version,
|
||||||
*command
|
*command
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def console
|
||||||
|
"ssh -t #{config.ssh_user}@#{config.primary_host} '#{exec("bin/rails", "c", interactive: true).join(" ")}'"
|
||||||
|
end
|
||||||
|
|
||||||
def list_containers
|
def list_containers
|
||||||
docker :container, :ls, "-a", *service_filter
|
docker :container, :ls, "-a", *service_filter
|
||||||
end
|
end
|
||||||
|
|||||||
27
lib/mrsk/commands/base.rb
Normal file
27
lib/mrsk/commands/base.rb
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
require "sshkit"
|
||||||
|
|
||||||
|
module Mrsk::Commands
|
||||||
|
class Base
|
||||||
|
attr_accessor :config
|
||||||
|
|
||||||
|
def initialize(config)
|
||||||
|
@config = config
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def combine(*commands)
|
||||||
|
commands
|
||||||
|
.collect { |command| command + [ "&&" ] }.flatten # Join commands with &&
|
||||||
|
.tap { |commands| commands.pop } # Remove trailing &&
|
||||||
|
end
|
||||||
|
|
||||||
|
def docker(*args)
|
||||||
|
args.compact.unshift :docker
|
||||||
|
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
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
39
lib/mrsk/commands/builder.rb
Normal file
39
lib/mrsk/commands/builder.rb
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
require "mrsk/commands/base"
|
||||||
|
|
||||||
|
class Mrsk::Commands::Builder < Mrsk::Commands::Base
|
||||||
|
delegate :create, :remove, :push, :pull, :info, to: :target
|
||||||
|
delegate :native?, :multiarch?, :remote?, to: :name
|
||||||
|
|
||||||
|
def name
|
||||||
|
target.class.to_s.demodulize.downcase.inquiry
|
||||||
|
end
|
||||||
|
|
||||||
|
def target
|
||||||
|
case
|
||||||
|
when config.builder.nil?
|
||||||
|
multiarch
|
||||||
|
when config.builder["multiarch"] == false
|
||||||
|
native
|
||||||
|
when config.builder["local"] && config.builder["local"]
|
||||||
|
multiarch_remote
|
||||||
|
else
|
||||||
|
raise ArgumentError, "Builder configuration incorrect: #{config.builder.inspect}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def native
|
||||||
|
@native ||= Mrsk::Commands::Builder::Native.new(config)
|
||||||
|
end
|
||||||
|
|
||||||
|
def multiarch
|
||||||
|
@multiarch ||= Mrsk::Commands::Builder::Multiarch.new(config)
|
||||||
|
end
|
||||||
|
|
||||||
|
def multiarch_remote
|
||||||
|
@multiarch_remote ||= Mrsk::Commands::Builder::Multiarch::Remote.new(config)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
require "mrsk/commands/builder/native"
|
||||||
|
require "mrsk/commands/builder/multiarch"
|
||||||
|
require "mrsk/commands/builder/multiarch/remote"
|
||||||
25
lib/mrsk/commands/builder/multiarch.rb
Normal file
25
lib/mrsk/commands/builder/multiarch.rb
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
require "mrsk/commands/base"
|
||||||
|
|
||||||
|
class Mrsk::Commands::Builder::Multiarch < Mrsk::Commands::Base
|
||||||
|
def create
|
||||||
|
docker :buildx, :create, "--use", "--name", "mrsk"
|
||||||
|
end
|
||||||
|
|
||||||
|
def remove
|
||||||
|
docker :buildx, :rm, "mrsk"
|
||||||
|
end
|
||||||
|
|
||||||
|
def push
|
||||||
|
docker :buildx, :build, "--push", "--platform linux/amd64,linux/arm64", "-t", config.absolute_image, "."
|
||||||
|
end
|
||||||
|
|
||||||
|
def pull
|
||||||
|
docker :pull, config.absolute_image
|
||||||
|
end
|
||||||
|
|
||||||
|
def info
|
||||||
|
combine \
|
||||||
|
docker(:context, :ls),
|
||||||
|
docker(:buildx, :ls)
|
||||||
|
end
|
||||||
|
end
|
||||||
53
lib/mrsk/commands/builder/multiarch/remote.rb
Normal file
53
lib/mrsk/commands/builder/multiarch/remote.rb
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
require "mrsk/commands/builder/multiarch"
|
||||||
|
|
||||||
|
class Mrsk::Commands::Builder::Multiarch::Remote < Mrsk::Commands::Builder::Multiarch
|
||||||
|
def create
|
||||||
|
combine \
|
||||||
|
create_contexts,
|
||||||
|
create_local_buildx,
|
||||||
|
append_remote_buildx
|
||||||
|
end
|
||||||
|
|
||||||
|
def remove
|
||||||
|
combine \
|
||||||
|
remove_contexts,
|
||||||
|
super
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def create_local_buildx
|
||||||
|
docker :buildx, :create, "--use", "--name", "mrsk", "mrsk-#{local["arch"]}", "--platform", "linux/#{local["arch"]}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def append_remote_buildx
|
||||||
|
docker :buildx, :create, "--append", "--name", "mrsk", "mrsk-#{remote["arch"]}", "--platform", "linux/#{remote["arch"]}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def create_contexts
|
||||||
|
combine \
|
||||||
|
create_context(local["arch"], local["host"]),
|
||||||
|
create_context(remote["arch"], remote["host"])
|
||||||
|
end
|
||||||
|
|
||||||
|
def create_context(arch, host)
|
||||||
|
docker :context, :create, "mrsk-#{arch}", "--description", "'MRSK #{arch} Native Host'", "--docker", "'host=#{host}'"
|
||||||
|
end
|
||||||
|
|
||||||
|
def remove_contexts
|
||||||
|
combine \
|
||||||
|
remove_context(local["arch"]),
|
||||||
|
remove_context(remote["arch"])
|
||||||
|
end
|
||||||
|
|
||||||
|
def remove_context(arch)
|
||||||
|
docker :context, :rm, "mrsk-#{arch}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def local
|
||||||
|
config.builder["local"]
|
||||||
|
end
|
||||||
|
|
||||||
|
def remote
|
||||||
|
config.builder["remote"]
|
||||||
|
end
|
||||||
|
end
|
||||||
25
lib/mrsk/commands/builder/native.rb
Normal file
25
lib/mrsk/commands/builder/native.rb
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
require "mrsk/commands/base"
|
||||||
|
|
||||||
|
class Mrsk::Commands::Builder::Native < Mrsk::Commands::Base
|
||||||
|
def create
|
||||||
|
# No-op on native
|
||||||
|
end
|
||||||
|
|
||||||
|
def remove
|
||||||
|
# No-op on native
|
||||||
|
end
|
||||||
|
|
||||||
|
def push
|
||||||
|
combine \
|
||||||
|
docker(:build, "-t", config.absolute_image, "."),
|
||||||
|
docker(:push, config.absolute_image)
|
||||||
|
end
|
||||||
|
|
||||||
|
def pull
|
||||||
|
docker :pull, config.absolute_image
|
||||||
|
end
|
||||||
|
|
||||||
|
def info
|
||||||
|
# No-op on native
|
||||||
|
end
|
||||||
|
end
|
||||||
17
lib/mrsk/commands/prune.rb
Normal file
17
lib/mrsk/commands/prune.rb
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
require "mrsk/commands/base"
|
||||||
|
require "active_support/duration"
|
||||||
|
require "active_support/core_ext/numeric/time"
|
||||||
|
|
||||||
|
class Mrsk::Commands::Prune < Mrsk::Commands::Base
|
||||||
|
PRUNE_IMAGES_AFTER = 30.days.in_hours.to_i
|
||||||
|
PRUNE_CONTAINERS_AFTER = 3.days.in_hours.to_i
|
||||||
|
|
||||||
|
def images
|
||||||
|
docker :image, :prune, "-f", "--filter", "until=#{PRUNE_IMAGES_AFTER}h"
|
||||||
|
end
|
||||||
|
|
||||||
|
def containers
|
||||||
|
docker :image, :prune, "-f", "--filter", "until=#{PRUNE_IMAGES_AFTER}h"
|
||||||
|
docker :container, :prune, "-f", "--filter", "label=service=#{config.service}", "--filter", "'until=#{PRUNE_CONTAINERS_AFTER}h'"
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
require "mrsk/commands/base"
|
||||||
|
|
||||||
class Mrsk::Commands::Registry < Mrsk::Commands::Base
|
class Mrsk::Commands::Registry < Mrsk::Commands::Base
|
||||||
delegate :registry, to: :config
|
delegate :registry, to: :config
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
require "mrsk/commands/base"
|
||||||
|
|
||||||
class Mrsk::Commands::Traefik < Mrsk::Commands::Base
|
class Mrsk::Commands::Traefik < Mrsk::Commands::Base
|
||||||
def run
|
def run
|
||||||
docker :run, "--name traefik",
|
docker :run, "--name traefik",
|
||||||
@@ -22,7 +24,7 @@ class Mrsk::Commands::Traefik < Mrsk::Commands::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def logs
|
def logs
|
||||||
docker :logs, "traefik"
|
docker :logs, "traefik", "-n", "100", "-t"
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_container
|
def remove_container
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ require "active_support/core_ext/string/inquiry"
|
|||||||
require "erb"
|
require "erb"
|
||||||
|
|
||||||
class Mrsk::Configuration
|
class Mrsk::Configuration
|
||||||
delegate :service, :image, :servers, :env, :registry, :ssh_user, to: :config, allow_nil: true
|
delegate :service, :image, :servers, :env, :labels, :registry, :builder, to: :config, allow_nil: true
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
def load_file(file)
|
def load_file(file)
|
||||||
@@ -75,11 +75,19 @@ class Mrsk::Configuration
|
|||||||
|
|
||||||
|
|
||||||
def env_args
|
def env_args
|
||||||
self.class.argumentize "-e", config.env if config.env.present?
|
if config.env.present?
|
||||||
|
self.class.argumentize "-e", config.env
|
||||||
|
else
|
||||||
|
[]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def ssh_user
|
||||||
|
config.ssh_user || "root"
|
||||||
end
|
end
|
||||||
|
|
||||||
def ssh_options
|
def ssh_options
|
||||||
{ user: config.ssh_user || "root", auth_methods: [ "publickey" ] }
|
{ user: ssh_user, auth_methods: [ "publickey" ] }
|
||||||
end
|
end
|
||||||
|
|
||||||
def master_key
|
def master_key
|
||||||
|
|||||||
@@ -11,6 +11,14 @@ class Mrsk::Configuration::Role
|
|||||||
@hosts ||= extract_hosts_from_config
|
@hosts ||= extract_hosts_from_config
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def labels
|
||||||
|
if name.web?
|
||||||
|
default_labels.merge(traefik_labels).merge(custom_labels)
|
||||||
|
else
|
||||||
|
default_labels.merge(custom_labels)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def label_args
|
def label_args
|
||||||
argumentize "--label", labels
|
argumentize "--label", labels
|
||||||
end
|
end
|
||||||
@@ -31,14 +39,6 @@ class Mrsk::Configuration::Role
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def labels
|
|
||||||
if name.web?
|
|
||||||
default_labels.merge(traefik_labels)
|
|
||||||
else
|
|
||||||
default_labels
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def default_labels
|
def default_labels
|
||||||
{ "service" => config.service, "role" => name }
|
{ "service" => config.service, "role" => name }
|
||||||
end
|
end
|
||||||
@@ -53,6 +53,13 @@ class Mrsk::Configuration::Role
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def custom_labels
|
||||||
|
Hash.new.tap do |labels|
|
||||||
|
labels.merge!(config.labels) if config.labels.present?
|
||||||
|
labels.merge!(specializations["labels"]) if specializations["labels"].present?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def specializations
|
def specializations
|
||||||
if config.servers.is_a?(Array) || config.servers[name].is_a?(Array)
|
if config.servers.is_a?(Array) || config.servers[name].is_a?(Array)
|
||||||
{ }
|
{ }
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
module Mrsk
|
module Mrsk
|
||||||
VERSION = "0.0.2"
|
VERSION = "0.0.3"
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,32 +1,17 @@
|
|||||||
require_relative "setup"
|
require_relative "setup"
|
||||||
|
|
||||||
app = Mrsk::Commands::App.new(MRSK_CONFIG)
|
|
||||||
|
|
||||||
namespace :mrsk do
|
namespace :mrsk do
|
||||||
namespace :app do
|
namespace :app do
|
||||||
desc "Deliver a newly built app image to servers"
|
|
||||||
task deliver: %i[ push pull ]
|
|
||||||
|
|
||||||
desc "Build locally and push app image to registry"
|
|
||||||
task :push do
|
|
||||||
run_locally { execute *app.push } unless ENV["VERSION"]
|
|
||||||
end
|
|
||||||
|
|
||||||
desc "Pull app image from the registry onto servers"
|
|
||||||
task :pull do
|
|
||||||
on(MRSK_CONFIG.hosts) { execute *app.pull }
|
|
||||||
end
|
|
||||||
|
|
||||||
desc "Run app on servers (or start them if they've already been run)"
|
desc "Run app on servers (or start them if they've already been run)"
|
||||||
task :run do
|
task :run do
|
||||||
MRSK_CONFIG.roles.each do |role|
|
MRSK.config.roles.each do |role|
|
||||||
on(role.hosts) do |host|
|
on(role.hosts) do |host|
|
||||||
begin
|
begin
|
||||||
execute *app.run(role: role.name)
|
execute *MRSK.app.run(role: role.name)
|
||||||
rescue SSHKit::Command::Failed => e
|
rescue SSHKit::Command::Failed => e
|
||||||
if e.message =~ /already in use/
|
if e.message =~ /already in use/
|
||||||
puts "Container with same version already deployed on #{host}, starting that instead"
|
error "Container with same version already deployed on #{host}, starting that instead"
|
||||||
execute *app.start, host: host
|
execute *MRSK.app.start, host: host
|
||||||
else
|
else
|
||||||
raise
|
raise
|
||||||
end
|
end
|
||||||
@@ -35,14 +20,14 @@ namespace :mrsk do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "Start existing app on servers"
|
desc "Start existing app on servers (use VERSION=<git-hash> to designate which version)"
|
||||||
task :start do
|
task :start do
|
||||||
on(MRSK_CONFIG.hosts) { execute *app.start, raise_on_non_zero_exit: false }
|
on(MRSK.config.hosts) { execute *MRSK.app.start, raise_on_non_zero_exit: false }
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "Stop app on servers"
|
desc "Stop app on servers"
|
||||||
task :stop do
|
task :stop do
|
||||||
on(MRSK_CONFIG.hosts) { execute *app.stop, raise_on_non_zero_exit: false }
|
on(MRSK.config.hosts) { execute *MRSK.app.stop, raise_on_non_zero_exit: false }
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "Start app on servers (use VERSION=<git-hash> to designate which version)"
|
desc "Start app on servers (use VERSION=<git-hash> to designate which version)"
|
||||||
@@ -50,48 +35,62 @@ namespace :mrsk do
|
|||||||
|
|
||||||
desc "Display information about app containers"
|
desc "Display information about app containers"
|
||||||
task :info do
|
task :info do
|
||||||
on(MRSK_CONFIG.hosts) { |host| puts "App Host: #{host}\n" + capture(*app.info) + "\n\n" }
|
on(MRSK.config.hosts) { |host| puts "App Host: #{host}\n" + capture(*MRSK.app.info) + "\n\n" }
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "Execute a custom task on servers passed in as CMD='bin/rake some:task'"
|
desc "Execute a custom task on servers passed in as CMD='bin/rake some:task'"
|
||||||
task :exec do
|
task :exec do
|
||||||
on(MRSK_CONFIG.hosts) { |host| puts "App Host: #{host}\n" + capture(*app.exec(ENV["CMD"])) + "\n\n" }
|
on(MRSK.config.hosts) { |host| puts "App Host: #{host}\n" + capture(*MRSK.app.exec(ENV["CMD"])) + "\n\n" }
|
||||||
|
end
|
||||||
|
|
||||||
|
desc "Start Rails Console on primary host"
|
||||||
|
task :console do
|
||||||
|
puts "Launching Rails console on #{MRSK.config.primary_host}..."
|
||||||
|
exec app.console
|
||||||
end
|
end
|
||||||
|
|
||||||
namespace :exec do
|
namespace :exec do
|
||||||
desc "Execute Rails command on servers, like CMD='runner \"puts %(Hello World)\""
|
desc "Execute Rails command on servers, like CMD='runner \"puts %(Hello World)\""
|
||||||
task :rails do
|
task :rails do
|
||||||
on(MRSK_CONFIG.hosts) { |host| puts "App Host: #{host}\n" + capture(*app.exec("bin/rails", ENV["CMD"])) + "\n\n" }
|
on(MRSK.config.hosts) { |host| puts "App Host: #{host}\n" + capture(*MRSK.app.exec("bin/rails", ENV["CMD"])) + "\n\n" }
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "Execute a custom task on the first defined server"
|
desc "Execute a custom task on the first defined server"
|
||||||
task :once do
|
task :once do
|
||||||
on(MRSK_CONFIG.primary_host) { |host| puts capture(*app.exec(ENV["CMD"])) }
|
on(MRSK.config.primary_host) { puts capture(*MRSK.app.exec(ENV["CMD"])) }
|
||||||
end
|
end
|
||||||
|
|
||||||
namespace :once do
|
namespace :once do
|
||||||
desc "Execute Rails command on the first defined server, like CMD='runner \"puts %(Hello World)\""
|
desc "Execute Rails command on the first defined server, like CMD='runner \"puts %(Hello World)\""
|
||||||
task :rails do
|
task :rails do
|
||||||
on(MRSK_CONFIG.primary_host) { puts capture(*app.exec("bin/rails", ENV["CMD"])) }
|
on(MRSK.config.primary_host) { puts capture(*MRSK.app.exec("bin/rails", ENV["CMD"])) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "List all the app containers currently on servers"
|
desc "List all the app containers currently on servers"
|
||||||
task :containers do
|
task :containers do
|
||||||
on(MRSK_CONFIG.hosts) { |host| puts "App Host: #{host}\n" + capture(*app.list_containers) + "\n\n" }
|
on(MRSK.config.hosts) { |host| puts "App Host: #{host}\n" + capture(*MRSK.app.list_containers) + "\n\n" }
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "Tail logs from app containers"
|
desc "Show last 100 log lines from app on servers"
|
||||||
task :logs do
|
task :logs do
|
||||||
on(MRSK_CONFIG.hosts) { execute *app.logs }
|
# FIXME: Catch when app containers aren't running
|
||||||
|
on(MRSK.config.hosts) { |host| puts "App Host: #{host}\n" + capture(*MRSK.app.logs) + "\n\n" }
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "Remove app containers and images from servers"
|
desc "Remove app containers and images from servers"
|
||||||
task remove: %i[ stop ] do
|
task remove: %w[ remove:containers remove:images ]
|
||||||
on(MRSK_CONFIG.hosts) do
|
|
||||||
execute *app.remove_containers
|
namespace :remove do
|
||||||
execute *app.remove_images
|
desc "Remove app containers from servers"
|
||||||
|
task :containers do
|
||||||
|
on(MRSK.config.hosts) { execute *MRSK.app.remove_containers }
|
||||||
|
end
|
||||||
|
|
||||||
|
desc "Remove app images from servers"
|
||||||
|
task :images do
|
||||||
|
on(MRSK.config.hosts) { execute *MRSK.app.remove_images }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
52
lib/tasks/mrsk/build.rake
Normal file
52
lib/tasks/mrsk/build.rake
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
require_relative "setup"
|
||||||
|
|
||||||
|
namespace :mrsk do
|
||||||
|
namespace :build do
|
||||||
|
desc "Deliver a newly built app image to servers"
|
||||||
|
task deliver: %i[ push pull ]
|
||||||
|
|
||||||
|
desc "Build locally and push app image to registry"
|
||||||
|
task :push do
|
||||||
|
run_locally do
|
||||||
|
begin
|
||||||
|
debug "Using builder: #{MRSK.builder.name}"
|
||||||
|
info "Building image may take a while (run with VERBOSE=1 for progress logging)"
|
||||||
|
execute *MRSK.builder.push
|
||||||
|
rescue SSHKit::Command::Failed => e
|
||||||
|
error "Missing compatible builder, so creating a new one first"
|
||||||
|
execute *MRSK.builder.create
|
||||||
|
execute *MRSK.builder.push
|
||||||
|
end
|
||||||
|
end unless ENV["VERSION"]
|
||||||
|
end
|
||||||
|
|
||||||
|
desc "Pull app image from the registry onto servers"
|
||||||
|
task :pull do
|
||||||
|
on(MRSK.config.hosts) { execute *MRSK.builder.pull }
|
||||||
|
end
|
||||||
|
|
||||||
|
desc "Create a local build setup"
|
||||||
|
task :create do
|
||||||
|
run_locally do
|
||||||
|
debug "Using builder: #{MRSK.builder.name}"
|
||||||
|
execute *MRSK.builder.create
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
desc "Remove local build setup"
|
||||||
|
task :remove do
|
||||||
|
run_locally do
|
||||||
|
debug "Using builder: #{MRSK.builder.name}"
|
||||||
|
execute *MRSK.builder.remove
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
desc "Show the name of the configured builder"
|
||||||
|
task :info do
|
||||||
|
run_locally do
|
||||||
|
puts "Builder: #{MRSK.builder.name} (#{MRSK.builder.target.class.name})"
|
||||||
|
puts capture(*MRSK.builder.info)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -2,10 +2,10 @@ require_relative "setup"
|
|||||||
|
|
||||||
namespace :mrsk do
|
namespace :mrsk do
|
||||||
desc "Deploy app for the first time to a fresh server"
|
desc "Deploy app for the first time to a fresh server"
|
||||||
task fresh: %w[ server:bootstrap registry:login app:deliver traefik:run app:stop app:run ]
|
task fresh: %w[ server:bootstrap registry:login build:deliver traefik:run app:stop app:run ]
|
||||||
|
|
||||||
desc "Push the latest version of the app, ensure Traefik is running, then restart app"
|
desc "Push the latest version of the app, ensure Traefik is running, then restart app"
|
||||||
task deploy: %w[ registry:login app:deliver traefik:run app:stop app:run prune ]
|
task deploy: %w[ registry:login build:deliver traefik:run app:stop app:run prune ]
|
||||||
|
|
||||||
desc "Rollback to VERSION=x that was already run as a container on servers"
|
desc "Rollback to VERSION=x that was already run as a container on servers"
|
||||||
task rollback: %w[ app:restart ]
|
task rollback: %w[ app:restart ]
|
||||||
@@ -16,9 +16,20 @@ namespace :mrsk do
|
|||||||
desc "Create config stub in config/deploy.yml"
|
desc "Create config stub in config/deploy.yml"
|
||||||
task :init do
|
task :init do
|
||||||
require "fileutils"
|
require "fileutils"
|
||||||
FileUtils.cp_r \
|
|
||||||
Pathname.new(File.expand_path("templates/deploy.yml", __dir__)),
|
if (deploy_file = Rails.root.join("config/deploy.yml")).exist?
|
||||||
Rails.root.join("config/deploy.yml")
|
puts "Config file already exists in config/deploy.yml (remove first to create a new one)"
|
||||||
|
else
|
||||||
|
FileUtils.cp_r Pathname.new(File.expand_path("templates/deploy.yml", __dir__)), deploy_file
|
||||||
|
puts "Created configuration file in config/deploy.yml"
|
||||||
|
end
|
||||||
|
|
||||||
|
if (binstub = Rails.root.join("bin/mrsk")).exist?
|
||||||
|
puts "Binstub already exists in bin/mrsk (remove first to create a new one)"
|
||||||
|
else
|
||||||
|
FileUtils.cp_r Pathname.new(File.expand_path("templates/mrsk", __dir__)), binstub
|
||||||
|
puts "Created binstub file in bin/mrsk"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "Remove Traefik, app, and registry session from servers"
|
desc "Remove Traefik, app, and registry session from servers"
|
||||||
|
|||||||
@@ -7,12 +7,12 @@ namespace :mrsk do
|
|||||||
namespace :prune do
|
namespace :prune do
|
||||||
desc "Prune unused images older than 30 days"
|
desc "Prune unused images older than 30 days"
|
||||||
task :images do
|
task :images do
|
||||||
on(MRSK_CONFIG.hosts) { execute "docker image prune -f --filter 'until=720h'" }
|
on(MRSK.config.hosts) { MRSK.verbosity(:debug) { execute *MRSK.prune.images } }
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "Prune stopped containers for the service older than 3 days"
|
desc "Prune stopped containers for the service older than 3 days"
|
||||||
task :containers do
|
task :containers do
|
||||||
on(MRSK_CONFIG.hosts) { execute "docker container prune -f --filter label=service=#{MRSK_CONFIG.service} --filter 'until=72h'" }
|
on(MRSK.config.hosts) { MRSK.verbosity(:debug) { execute *MRSK.prune.containers } }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,18 +1,16 @@
|
|||||||
require_relative "setup"
|
require_relative "setup"
|
||||||
|
|
||||||
registry = Mrsk::Commands::Registry.new(MRSK_CONFIG)
|
|
||||||
|
|
||||||
namespace :mrsk do
|
namespace :mrsk do
|
||||||
namespace :registry do
|
namespace :registry do
|
||||||
desc "Login to the registry locally and remotely"
|
desc "Login to the registry locally and remotely"
|
||||||
task :login do
|
task :login do
|
||||||
run_locally { execute *registry.login }
|
run_locally { execute *MRSK.registry.login }
|
||||||
on(MRSK_CONFIG.hosts) { execute *registry.login }
|
on(MRSK.config.hosts) { execute *MRSK.registry.login }
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "Logout of the registry remotely"
|
desc "Logout of the registry remotely"
|
||||||
task :logout do
|
task :logout do
|
||||||
on(MRSK_CONFIG.hosts) { execute *registry.logout }
|
on(MRSK.config.hosts) { execute *MRSK.registry.logout }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ namespace :mrsk do
|
|||||||
desc "Setup Docker on the remote servers"
|
desc "Setup Docker on the remote servers"
|
||||||
task :bootstrap do
|
task :bootstrap do
|
||||||
# FIXME: Detect when apt-get is not available and use the appropriate alternative
|
# FIXME: Detect when apt-get is not available and use the appropriate alternative
|
||||||
on(MRSK_CONFIG.hosts) { execute "apt-get install docker.io -y" }
|
on(MRSK.config.hosts) { execute "apt-get install docker.io -y" }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -3,14 +3,4 @@ require "sshkit/dsl"
|
|||||||
|
|
||||||
include SSHKit::DSL
|
include SSHKit::DSL
|
||||||
|
|
||||||
if (config_file = Rails.root.join("config/deploy.yml")).exist?
|
MRSK = Mrsk::Commander.new config_file: Rails.root.join("config/deploy.yml"), verbose: ENV["VERBOSE"]
|
||||||
MRSK_CONFIG = Mrsk::Configuration.load_file(config_file)
|
|
||||||
|
|
||||||
SSHKit::Backend::Netssh.configure { |ssh| ssh.ssh_options = MRSK_CONFIG.ssh_options }
|
|
||||||
|
|
||||||
# No need to use /usr/bin/env, just clogs up the logs
|
|
||||||
SSHKit.config.command_map[:docker] = "docker"
|
|
||||||
else
|
|
||||||
# MRSK is missing config/deploy.yml – run 'rake mrsk:init'
|
|
||||||
MRSK_CONFIG = Mrsk::Configuration.new({}, validate: false)
|
|
||||||
end
|
|
||||||
|
|||||||
@@ -3,19 +3,22 @@
|
|||||||
service: my-app
|
service: my-app
|
||||||
|
|
||||||
# Name of the container image
|
# Name of the container image
|
||||||
image: user/chat
|
image: user/my-app
|
||||||
|
|
||||||
# All the servers targeted for deploy. You can reference a single server for a command by using SERVERS=xxx.xxx.xxx.xxx
|
# All the servers targeted for deploy. You can reference a single server for a command by using SERVERS=192.168.0.1
|
||||||
servers:
|
servers:
|
||||||
- xxx.xxx.xxx.xxx
|
- 192.168.0.1
|
||||||
|
|
||||||
# The following envs are made available to the container when started
|
# The following envs are made available to the container when started
|
||||||
env:
|
env:
|
||||||
# Remember never to put passwords or tokens directly into this file, use encrypted credentials
|
# Remember never to put passwords or tokens directly into this file, use encrypted credentials
|
||||||
# REDIS_URL: redis://x/y
|
# REDIS_URL: redis://x/y
|
||||||
|
|
||||||
|
# Where your images will be hosted
|
||||||
registry:
|
registry:
|
||||||
# Specify the registry server, if you're not using Docker Hub
|
# Specify the registry server, if you're not using Docker Hub
|
||||||
# server: registry.digitalocean.com / ghcr.io / ...
|
# server: registry.digitalocean.com / ghcr.io / ...
|
||||||
username: <%= Rails.application.credentials.registry["username"] %>
|
|
||||||
password: <%= Rails.application.credentials.registry["password"] %>
|
# Set credentials with bin/rails credentials:edit
|
||||||
|
username: my-user
|
||||||
|
password: my-password-should-go-in-credentials
|
||||||
|
|||||||
8
lib/tasks/mrsk/templates/mrsk
Executable file
8
lib/tasks/mrsk/templates/mrsk
Executable file
@@ -0,0 +1,8 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
if [ "${*}" == "" ]; then
|
||||||
|
# Improve so list matches
|
||||||
|
exec bin/rake -T mrsk
|
||||||
|
else
|
||||||
|
exec bin/rake "mrsk:$@"
|
||||||
|
fi
|
||||||
@@ -1,22 +1,20 @@
|
|||||||
require_relative "setup"
|
require_relative "setup"
|
||||||
|
|
||||||
traefik = Mrsk::Commands::Traefik.new(MRSK_CONFIG)
|
|
||||||
|
|
||||||
namespace :mrsk do
|
namespace :mrsk do
|
||||||
namespace :traefik do
|
namespace :traefik do
|
||||||
desc "Run Traefik on servers"
|
desc "Run Traefik on servers"
|
||||||
task :run do
|
task :run do
|
||||||
on(MRSK_CONFIG.role(:web).hosts) { execute *traefik.run, raise_on_non_zero_exit: false }
|
on(MRSK.config.role(:web).hosts) { execute *MRSK.traefik.run, raise_on_non_zero_exit: false }
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "Start existing Traefik on servers"
|
desc "Start existing Traefik on servers"
|
||||||
task :start do
|
task :start do
|
||||||
on(MRSK_CONFIG.role(:web).hosts) { execute *traefik.start, raise_on_non_zero_exit: false }
|
on(MRSK.config.role(:web).hosts) { execute *MRSK.traefik.start, raise_on_non_zero_exit: false }
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "Stop Traefik on servers"
|
desc "Stop Traefik on servers"
|
||||||
task :stop do
|
task :stop do
|
||||||
on(MRSK_CONFIG.role(:web).hosts) { execute *traefik.stop, raise_on_non_zero_exit: false }
|
on(MRSK.config.role(:web).hosts) { execute *MRSK.traefik.stop, raise_on_non_zero_exit: false }
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "Restart Traefik on servers"
|
desc "Restart Traefik on servers"
|
||||||
@@ -24,14 +22,19 @@ namespace :mrsk do
|
|||||||
|
|
||||||
desc "Display information about Traefik containers from servers"
|
desc "Display information about Traefik containers from servers"
|
||||||
task :info do
|
task :info do
|
||||||
on(MRSK_CONFIG.role(:web).hosts) { |host| puts "Traefik Host: #{host}\n" + capture(*traefik.info) + "\n\n" }
|
on(MRSK.config.role(:web).hosts) { |host| puts "Traefik Host: #{host}\n" + capture(*MRSK.traefik.info) + "\n\n" }
|
||||||
|
end
|
||||||
|
|
||||||
|
desc "Show last 100 log lines from Traefik on servers"
|
||||||
|
task :logs do
|
||||||
|
on(MRSK.config.hosts) { |host| puts "Traefik Host: #{host}\n" + capture(*MRSK.traefik.logs) + "\n\n" }
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "Remove Traefik container and image from servers"
|
desc "Remove Traefik container and image from servers"
|
||||||
task remove: %i[ stop ] do
|
task remove: %i[ stop ] do
|
||||||
on(MRSK_CONFIG.role(:web).hosts) do
|
on(MRSK.config.role(:web).hosts) do
|
||||||
execute *traefik.remove_container
|
execute *MRSK.traefik.remove_container
|
||||||
execute *traefik.remove_image
|
execute *MRSK.traefik.remove_image
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
require "test_helper"
|
require "test_helper"
|
||||||
require "mrsk/configuration"
|
require "mrsk/configuration"
|
||||||
require "mrsk/commands"
|
require "mrsk/commands/app"
|
||||||
|
|
||||||
ENV["VERSION"] = "123"
|
ENV["VERSION"] = "123"
|
||||||
ENV["RAILS_MASTER_KEY"] = "456"
|
ENV["RAILS_MASTER_KEY"] = "456"
|
||||||
|
|||||||
24
test/builder_command_test.rb
Normal file
24
test/builder_command_test.rb
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
require "test_helper"
|
||||||
|
require "mrsk/configuration"
|
||||||
|
require "mrsk/commands/builder"
|
||||||
|
|
||||||
|
class BuilderCommandTest < ActiveSupport::TestCase
|
||||||
|
setup do
|
||||||
|
@config = { service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" }, servers: [ "1.1.1.1" ] }
|
||||||
|
end
|
||||||
|
|
||||||
|
test "target multiarch by default" do
|
||||||
|
builder = Mrsk::Commands::Builder.new(Mrsk::Configuration.new(@config))
|
||||||
|
assert builder.multiarch?
|
||||||
|
end
|
||||||
|
|
||||||
|
test "target native when multiarch is off" do
|
||||||
|
builder = Mrsk::Commands::Builder.new(Mrsk::Configuration.new(@config.merge({ builder: { "multiarch" => false } })))
|
||||||
|
assert builder.native?
|
||||||
|
end
|
||||||
|
|
||||||
|
test "target multiarch remote when local and remote is set" do
|
||||||
|
builder = Mrsk::Commands::Builder.new(Mrsk::Configuration.new(@config.merge({ builder: { "local" => { }, "remote" => { } } })))
|
||||||
|
assert builder.remote?
|
||||||
|
end
|
||||||
|
end
|
||||||
12
test/commander_test.rb
Normal file
12
test/commander_test.rb
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
require "test_helper"
|
||||||
|
require "mrsk/commander"
|
||||||
|
|
||||||
|
class CommanderTest < ActiveSupport::TestCase
|
||||||
|
setup do
|
||||||
|
@mrsk = Mrsk::Commander.new config_file: Pathname.new(File.expand_path("fixtures/deploy.erb.yml", __dir__))
|
||||||
|
end
|
||||||
|
|
||||||
|
test "lazy configuration" do
|
||||||
|
assert_equal Mrsk::Configuration, @mrsk.config.class
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -30,6 +30,11 @@ class ConfigurationRoleTest < ActiveSupport::TestCase
|
|||||||
assert_equal [ "1.1.1.3", "1.1.1.4" ], @config_with_roles.role(:workers).hosts
|
assert_equal [ "1.1.1.3", "1.1.1.4" ], @config_with_roles.role(:workers).hosts
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "cmd" do
|
||||||
|
assert_nil @config.role(:web).cmd
|
||||||
|
assert_equal "bin/jobs", @config_with_roles.role(:workers).cmd
|
||||||
|
end
|
||||||
|
|
||||||
test "label args" do
|
test "label args" do
|
||||||
assert_equal [ "--label", "service=app", "--label", "role=workers" ], @config_with_roles.role(:workers).label_args
|
assert_equal [ "--label", "service=app", "--label", "role=workers" ], @config_with_roles.role(:workers).label_args
|
||||||
end
|
end
|
||||||
@@ -38,8 +43,19 @@ class ConfigurationRoleTest < ActiveSupport::TestCase
|
|||||||
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=3", "--label", "traefik.http.middlewares.app.retry.initialinterval=500ms"], @config.role(:web).label_args
|
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=3", "--label", "traefik.http.middlewares.app.retry.initialinterval=500ms"], @config.role(:web).label_args
|
||||||
end
|
end
|
||||||
|
|
||||||
test "cmd" do
|
test "custom labels" do
|
||||||
assert_nil @config.role(:web).cmd
|
@deploy[:labels] = { "my.custom.label" => "50" }
|
||||||
assert_equal "bin/jobs", @config_with_roles.role(:workers).cmd
|
assert_equal "50", @config.role(:web).labels["my.custom.label"]
|
||||||
|
end
|
||||||
|
|
||||||
|
test "custom labels via role specialization" do
|
||||||
|
@deploy_with_roles[:labels] = { "my.custom.label" => "50" }
|
||||||
|
@deploy_with_roles[:servers]["workers"]["labels"] = { "my.custom.label" => "70" }
|
||||||
|
assert_equal "70", @config_with_roles.role(:workers).labels["my.custom.label"]
|
||||||
|
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"]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user