mrsk 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +131 -53
- data/bin/mrsk +5 -0
- data/lib/mrsk/cli/app.rb +97 -0
- data/lib/mrsk/cli/base.rb +27 -0
- data/lib/mrsk/cli/build.rb +53 -0
- data/lib/mrsk/cli/main.rb +99 -0
- data/lib/mrsk/cli/prune.rb +19 -0
- data/lib/mrsk/cli/registry.rb +14 -0
- data/lib/mrsk/cli/server.rb +8 -0
- data/lib/mrsk/cli/templates/deploy.yml +17 -0
- data/lib/mrsk/cli/traefik.rb +44 -0
- data/lib/mrsk/cli.rb +9 -0
- data/lib/mrsk/commander.rb +57 -0
- data/lib/mrsk/commands/app.rb +12 -15
- data/lib/mrsk/commands/base.rb +27 -0
- data/lib/mrsk/commands/builder/multiarch/remote.rb +58 -0
- data/lib/mrsk/commands/builder/multiarch.rb +30 -0
- data/lib/mrsk/commands/builder/native.rb +25 -0
- data/lib/mrsk/commands/builder.rb +39 -0
- data/lib/mrsk/commands/prune.rb +17 -0
- data/lib/mrsk/commands/registry.rb +2 -0
- data/lib/mrsk/commands/traefik.rb +3 -1
- data/lib/mrsk/commands.rb +0 -23
- data/lib/mrsk/configuration/role.rb +15 -8
- data/lib/mrsk/configuration.rb +14 -4
- data/lib/mrsk/version.rb +1 -1
- data/lib/mrsk.rb +1 -4
- metadata +40 -16
- data/lib/mrsk/engine.rb +0 -4
- data/lib/tasks/mrsk/app.rake +0 -98
- data/lib/tasks/mrsk/mrsk.rake +0 -26
- data/lib/tasks/mrsk/prune.rake +0 -18
- data/lib/tasks/mrsk/registry.rake +0 -18
- data/lib/tasks/mrsk/server.rake +0 -11
- data/lib/tasks/mrsk/setup.rb +0 -16
- data/lib/tasks/mrsk/templates/deploy.yml +0 -21
- data/lib/tasks/mrsk/traefik.rake +0 -38
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 45be50e3f2f3c5c629b1ec46ee23394b1913c758385ee431d44553506f40cc01
|
4
|
+
data.tar.gz: fe11053be553d9af7d090b03b019dd09d1578bed94050b6be5699f99f6711be8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6eee63c66d3f6585c2ac142d78ffdf952b141073793a8f8dd8d7b1551fbd5a1a98f12c590e83517f8e52e1ac58b07ebd48b55571ebe66c3c8a2ec677c47ef00d
|
7
|
+
data.tar.gz: f46b8fc945be5c43427f7bf2300cbd95e80e30598fd8294e327f83253b25430937d44c255c8edb1d6b0fce613224acb367972bbeef125e7615293c83c23ddb2c
|
data/README.md
CHANGED
@@ -1,80 +1,159 @@
|
|
1
1
|
# MRSK
|
2
2
|
|
3
|
-
MRSK
|
3
|
+
MRSK deploys Rails apps in containers to servers running Docker with zero downtime. It uses the dynamic reverse-proxy Traefik to hold requests while the new application container is started and the old one is stopped. It works seamlessly across multiple hosts, using SSHKit to execute commands.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
7
|
-
|
7
|
+
Install MRSK globally with `gem install mrsk`. Then, inside your app directory, run `mrsk install`. Now edit the new file `config/deploy.yml`. It could look as simple as this:
|
8
8
|
|
9
9
|
```yaml
|
10
10
|
service: hey
|
11
11
|
image: 37s/hey
|
12
12
|
servers:
|
13
|
-
-
|
14
|
-
-
|
15
|
-
env:
|
16
|
-
DATABASE_URL: mysql2://db1/hey_production/
|
17
|
-
REDIS_URL: redis://redis1:6379/1
|
13
|
+
- 192.168.0.1
|
14
|
+
- 192.168.0.2
|
18
15
|
registry:
|
19
|
-
|
20
|
-
|
21
|
-
password: <%= Rails.application.credentials.registry["password"] %>
|
16
|
+
username: registry-user-name
|
17
|
+
password: <%= ENV["MRSK_REGISTRY_PASSWORD"] %>
|
22
18
|
```
|
23
19
|
|
24
|
-
|
20
|
+
Now you're ready to deploy a multi-arch image to the servers:
|
25
21
|
|
26
22
|
```
|
23
|
+
mrsk deploy
|
24
|
+
```
|
25
|
+
|
26
|
+
This will:
|
27
|
+
|
28
|
+
1. Install Docker on any server that might be missing it (using apt-get)
|
29
|
+
2. Log into the registry both locally and remotely
|
30
|
+
3. Build the image using the standard Dockerfile in the root of the application.
|
31
|
+
4. Push the image to the registry.
|
32
|
+
5. Pull the image from the registry on the servers.
|
33
|
+
6. Ensure Traefik is running and accepting traffic on port 80.
|
34
|
+
7. Stop any containers running a previous versions of the app.
|
35
|
+
8. Start a new container with the version of the app that matches the current git version hash.
|
36
|
+
9. Prune unused images and stopped containers to ensure servers don't fill up.
|
37
|
+
|
38
|
+
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.
|
39
|
+
|
40
|
+
## Configuration
|
41
|
+
|
42
|
+
### Using another registry than Docker Hub
|
43
|
+
|
44
|
+
The default registry for Docker is Docker Hub. If you'd like to use a different one, just configure the server, like so:
|
45
|
+
|
46
|
+
```yaml
|
27
47
|
registry:
|
28
|
-
|
29
|
-
|
48
|
+
server: registry.digitalocean.com
|
49
|
+
username: registry-user-name
|
50
|
+
password: <%= ENV["MRSK_REGISTRY_PASSWORD"] %>
|
30
51
|
```
|
31
52
|
|
32
|
-
|
53
|
+
### Using a different SSH user than root
|
33
54
|
|
55
|
+
The default SSH user is root, but you can change it using `ssh_user`:
|
56
|
+
|
57
|
+
```yaml
|
58
|
+
ssh_user: app
|
34
59
|
```
|
35
|
-
|
60
|
+
|
61
|
+
### Adding custom env variables
|
62
|
+
|
63
|
+
You can inject custom env variables into the app containers using `env`:
|
64
|
+
|
65
|
+
```yaml
|
66
|
+
env:
|
67
|
+
DATABASE_URL: mysql2://db1/hey_production/
|
68
|
+
REDIS_URL: redis://redis1:6379/1
|
36
69
|
```
|
37
70
|
|
38
|
-
|
71
|
+
### Splitting servers into different roles
|
39
72
|
|
40
|
-
|
41
|
-
2. Build the image using the standard Dockerfile in the root of the application.
|
42
|
-
3. Push the image to the registry.
|
43
|
-
4. Pull the image from the registry on the servers.
|
44
|
-
5. Ensure Traefik is running and accepting traffic on port 80.
|
45
|
-
6. Stop any containers running a previous versions of the app.
|
46
|
-
7. Start a new container with the version of the app that matches the current git version hash.
|
47
|
-
8. Prune unused images and stopped containers to ensure servers don't fill up.
|
73
|
+
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:
|
48
74
|
|
49
|
-
|
75
|
+
```yaml
|
76
|
+
servers:
|
77
|
+
web:
|
78
|
+
- 192.168.0.1
|
79
|
+
- 192.168.0.2
|
80
|
+
job:
|
81
|
+
hosts:
|
82
|
+
- 192.168.0.3
|
83
|
+
- 192.168.0.4
|
84
|
+
cmd: bin/jobs
|
85
|
+
```
|
86
|
+
|
87
|
+
Traefik will only be installed and run on the servers in the `web` role (and on all servers if no roles are defined).
|
50
88
|
|
51
|
-
|
89
|
+
### Adding custom container labels
|
52
90
|
|
53
|
-
|
91
|
+
You can specialize the default Traefik rules by setting custom labels on the containers that are being started:
|
54
92
|
|
55
|
-
|
93
|
+
```
|
94
|
+
labels:
|
95
|
+
traefik.http.routers.hey.rule: '''Host(`app.hey.com`)'''
|
96
|
+
```
|
97
|
+
|
98
|
+
(Note: The extra quotes are needed to ensure the rule is passed in correctly!)
|
99
|
+
|
100
|
+
This allows you to run multiple applications on the same server sharing the same Traefik instance and port.
|
101
|
+
See https://doc.traefik.io/traefik/routing/routers/#rule for a full list of available routing rules.
|
102
|
+
|
103
|
+
The labels can even be applied on a per-role basis:
|
56
104
|
|
57
105
|
```yaml
|
58
106
|
servers:
|
59
107
|
web:
|
60
|
-
-
|
61
|
-
-
|
108
|
+
- 192.168.0.1
|
109
|
+
- 192.168.0.2
|
62
110
|
job:
|
63
111
|
hosts:
|
64
|
-
-
|
65
|
-
-
|
112
|
+
- 192.168.0.3
|
113
|
+
- 192.168.0.4
|
66
114
|
cmd: bin/jobs
|
115
|
+
labels:
|
116
|
+
my-custom-label: "50"
|
117
|
+
```
|
118
|
+
|
119
|
+
### Configuring remote builder for native multi-arch
|
120
|
+
|
121
|
+
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.
|
122
|
+
|
123
|
+
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:
|
124
|
+
|
125
|
+
```yaml
|
126
|
+
builder:
|
127
|
+
local:
|
128
|
+
arch: arm64
|
129
|
+
host: unix:///Users/dhh/.docker/run/docker.sock
|
130
|
+
remote:
|
131
|
+
arch: amd64
|
132
|
+
host: ssh://root@192.168.0.1
|
133
|
+
```
|
134
|
+
|
135
|
+
Note: You must have Docker running on the remote host being used as a builder.
|
136
|
+
|
137
|
+
With that configuration in place, you can setup the local/remote configuration using `mrsk build create`. If you wish to remove the contexts and buildx instances again, you can run `mrsk build remove`. If you had already built using the standard emulation setup, run `mrsk build remove` before doing `mrsk build remote`.
|
138
|
+
|
139
|
+
### Configuring native builder when multi-arch isn't needed
|
140
|
+
|
141
|
+
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:
|
142
|
+
|
143
|
+
```yaml
|
144
|
+
builder:
|
145
|
+
multiarch: false
|
67
146
|
```
|
68
147
|
|
69
|
-
|
148
|
+
## Commands
|
70
149
|
|
71
|
-
###
|
150
|
+
### Remote execution
|
72
151
|
|
73
|
-
If you need to execute commands inside the Rails containers, you can use `
|
152
|
+
If you need to execute commands inside the Rails containers, you can use `mrsk app exec`, `mrsk app exec --once`, `mrsk app runner`, and `mrsk app runner --once`. Examples:
|
74
153
|
|
75
154
|
```bash
|
76
155
|
# Runs command on all servers
|
77
|
-
|
156
|
+
mrsk app exec 'ruby -v'
|
78
157
|
App Host: xxx.xxx.xxx.xxx
|
79
158
|
ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [x86_64-linux]
|
80
159
|
|
@@ -82,11 +161,11 @@ App Host: xxx.xxx.xxx.xxx
|
|
82
161
|
ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [x86_64-linux]
|
83
162
|
|
84
163
|
# Runs command on first server
|
85
|
-
|
164
|
+
mrsk app exec --once 'cat .ruby-version'
|
86
165
|
3.1.3
|
87
166
|
|
88
167
|
# Runs Rails command on all servers
|
89
|
-
|
168
|
+
mrsk app exec 'bin/rails about'
|
90
169
|
App Host: xxx.xxx.xxx.xxx
|
91
170
|
About your application's environment
|
92
171
|
Rails version 7.1.0.alpha
|
@@ -111,15 +190,18 @@ Environment production
|
|
111
190
|
Database adapter sqlite3
|
112
191
|
Database schema version 20221231233303
|
113
192
|
|
114
|
-
# Runs Rails
|
115
|
-
|
116
|
-
|
117
|
-
Current version: 20221231233303
|
193
|
+
# Runs Rails runner on first server
|
194
|
+
mrsk app runner 'puts Rails.application.config.time_zone'
|
195
|
+
UTC
|
118
196
|
```
|
119
197
|
|
198
|
+
### Running a Rails console on the primary host
|
199
|
+
|
200
|
+
If you need to interact with the production console for the app, you can use `mrsk app console`, which will start a Rails console session on the primary host. You can start the console on a different host using `mrsk app console --host 192.168.0.2`. Be mindful that this is a live wire! Any changes made to the production database will take effect immeditately.
|
201
|
+
|
120
202
|
### Inspecting
|
121
203
|
|
122
|
-
You can see the state of your servers by running `
|
204
|
+
You can see the state of your servers by running `mrsk details`. It'll show something like this:
|
123
205
|
|
124
206
|
```
|
125
207
|
Traefik Host: xxx.xxx.xxx.xxx
|
@@ -139,11 +221,11 @@ CONTAINER ID IMAGE
|
|
139
221
|
1d3c91ed1f55 registry.digitalocean.com/user/app:6ef8a6a84c525b123c5245345a8483f86d05a123 "/rails/bin/docker-e…" 13 minutes ago Up 13 minutes 3000/tcp chat-6ef8a6a84c525b123c5245345a8483f86d05a123
|
140
222
|
```
|
141
223
|
|
142
|
-
You can also see just info for app containers with `
|
224
|
+
You can also see just info for app containers with `mrsk app details` or just for Traefik with `mrsk traefik details`.
|
143
225
|
|
144
226
|
### Rollback
|
145
227
|
|
146
|
-
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 `
|
228
|
+
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 `mrsk app containers`. It'll give you a presentation similar to `mrsk app details`, but include all the old containers as well. Showing something like this:
|
147
229
|
|
148
230
|
```
|
149
231
|
App Host: 164.92.105.119
|
@@ -157,21 +239,17 @@ badb1aa51db4 registry.digitalocean.com/user/app:6ef8a6a84c525b123c5245345a8483
|
|
157
239
|
6f170d1172ae registry.digitalocean.com/user/app:e5d9d7c2b898289dfbc5f7f1334140d984eedae4 "/rails/bin/docker-e…" 31 minutes ago Exited (1) 27 minutes ago chat-e5d9d7c2b898289dfbc5f7f1334140d984eedae4
|
158
240
|
```
|
159
241
|
|
160
|
-
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 `
|
242
|
+
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 `mrsk rollback 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.
|
161
243
|
|
162
|
-
Note that by default old containers are pruned after 3 days when you run `
|
244
|
+
Note that by default old containers are pruned after 3 days when you run `mrsk deploy`.
|
163
245
|
|
164
246
|
### Removing
|
165
247
|
|
166
|
-
If you wish to remove the entire application, including Traefik, containers, images, and registry session, you can run `
|
248
|
+
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.
|
167
249
|
|
168
250
|
## Stage of development
|
169
251
|
|
170
|
-
This is alpha software. Lots of stuff is missing.
|
171
|
-
|
172
|
-
- Adapterize commands to work with Podman and other container runners
|
173
|
-
- Possibly switching to a bin/mrsk command rather than raw rake
|
174
|
-
- Integrate with cloud CI pipelines
|
252
|
+
This is alpha software. Lots of stuff is missing. Lots of stuff will keep moving around for a while.
|
175
253
|
|
176
254
|
## License
|
177
255
|
|
data/bin/mrsk
ADDED
data/lib/mrsk/cli/app.rb
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
require "mrsk/cli/base"
|
2
|
+
|
3
|
+
class Mrsk::Cli::App < Mrsk::Cli::Base
|
4
|
+
desc "boot", "Boot app on servers (or start them if they've already been booted)"
|
5
|
+
def boot
|
6
|
+
MRSK.config.roles.each do |role|
|
7
|
+
on(role.hosts) do |host|
|
8
|
+
begin
|
9
|
+
execute *MRSK.app.run(role: role.name)
|
10
|
+
rescue SSHKit::Command::Failed => e
|
11
|
+
if e.message =~ /already in use/
|
12
|
+
error "Container with same version already deployed on #{host}, starting that instead"
|
13
|
+
execute *MRSK.app.start, host: host
|
14
|
+
else
|
15
|
+
raise
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
desc "start", "Start existing app on servers (use --version=<git-hash> to designate specific version)"
|
23
|
+
option :version, desc: "Defaults to the most recent git-hash in local repository"
|
24
|
+
def start
|
25
|
+
if (version = options[:version]).present?
|
26
|
+
on(MRSK.config.hosts) { execute *MRSK.app.start(version: version) }
|
27
|
+
else
|
28
|
+
on(MRSK.config.hosts) { execute *MRSK.app.start, raise_on_non_zero_exit: false }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
desc "stop", "Stop app on servers"
|
33
|
+
def stop
|
34
|
+
on(MRSK.config.hosts) { execute *MRSK.app.stop, raise_on_non_zero_exit: false }
|
35
|
+
end
|
36
|
+
|
37
|
+
desc "details", "Display details about app containers"
|
38
|
+
def details
|
39
|
+
on(MRSK.config.hosts) { |host| puts "App Host: #{host}\n" + capture(*MRSK.app.info, verbosity: Logger::INFO) + "\n\n" }
|
40
|
+
end
|
41
|
+
|
42
|
+
desc "exec [CMD]", "Execute a custom task on servers passed in as CMD='bin/rake some:task'"
|
43
|
+
option :once, type: :boolean, default: false
|
44
|
+
def exec(cmd)
|
45
|
+
if options[:once]
|
46
|
+
on(MRSK.config.primary_host) { puts capture(*MRSK.app.exec(cmd), verbosity: Logger::INFO) }
|
47
|
+
else
|
48
|
+
on(MRSK.config.hosts) { |host| puts "App Host: #{host}\n" + capture(*MRSK.app.exec(cmd), verbosity: Logger::INFO) + "\n\n" }
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
desc "console", "Start Rails Console on primary host"
|
53
|
+
option :host, desc: "Start console on a different host"
|
54
|
+
def console
|
55
|
+
host = options[:host] || MRSK.config.primary_host
|
56
|
+
|
57
|
+
run_locally do
|
58
|
+
puts "Launching Rails console on #{host}..."
|
59
|
+
exec MRSK.app.console(host: host)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
desc "runner [EXPRESSION]", "Execute Rails runner with given expression"
|
64
|
+
option :once, type: :boolean, default: false, desc:
|
65
|
+
def runner(expression)
|
66
|
+
if options[:once]
|
67
|
+
on(MRSK.config.primary_host) { puts capture(*MRSK.app.exec("bin/rails", "runner", "'#{expression}'"), verbosity: Logger::INFO) }
|
68
|
+
else
|
69
|
+
on(MRSK.config.hosts) { |host| puts "App Host: #{host}\n" + capture(*MRSK.app.exec("bin/rails", "runner", "'#{expression}'"), verbosity: Logger::INFO) + "\n\n" }
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
desc "containers", "List all the app containers currently on servers"
|
74
|
+
def containers
|
75
|
+
on(MRSK.config.hosts) { |host| puts "App Host: #{host}\n" + capture(*MRSK.app.list_containers, verbosity: Logger::INFO) + "\n\n" }
|
76
|
+
end
|
77
|
+
|
78
|
+
desc "logs", "Show last 100 log lines from app on servers"
|
79
|
+
def logs
|
80
|
+
# FIXME: Catch when app containers aren't running
|
81
|
+
on(MRSK.config.hosts) { |host| puts "App Host: #{host}\n" + capture(*MRSK.app.logs) + "\n\n" }
|
82
|
+
end
|
83
|
+
|
84
|
+
desc "remove", "Remove app containers and images from servers"
|
85
|
+
option :only, default: "", desc: "Use 'containers' or 'images'"
|
86
|
+
def remove
|
87
|
+
case options[:only]
|
88
|
+
when "containers"
|
89
|
+
on(MRSK.config.hosts) { execute *MRSK.app.remove_containers }
|
90
|
+
when "images"
|
91
|
+
on(MRSK.config.hosts) { execute *MRSK.app.remove_images }
|
92
|
+
else
|
93
|
+
on(MRSK.config.hosts) { execute *MRSK.app.remove_containers }
|
94
|
+
on(MRSK.config.hosts) { execute *MRSK.app.remove_images }
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require "thor"
|
2
|
+
require "sshkit"
|
3
|
+
require "sshkit/dsl"
|
4
|
+
|
5
|
+
module Mrsk::Cli
|
6
|
+
class Base < Thor
|
7
|
+
include SSHKit::DSL
|
8
|
+
|
9
|
+
def self.exit_on_failure?() true end
|
10
|
+
|
11
|
+
class_option :verbose, type: :boolean, aliases: "-v", desc: "Detailed logging"
|
12
|
+
|
13
|
+
def initialize(*)
|
14
|
+
super
|
15
|
+
MRSK.verbose = options[:verbose]
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
def print_runtime
|
20
|
+
started_at = Time.now
|
21
|
+
yield
|
22
|
+
ensure
|
23
|
+
runtime = Time.now - started_at
|
24
|
+
puts " Finished all in #{sprintf("%.1f seconds", runtime)}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require "mrsk/cli/base"
|
2
|
+
|
3
|
+
class Mrsk::Cli::Build < Mrsk::Cli::Base
|
4
|
+
desc "deliver", "Deliver a newly built app image to servers"
|
5
|
+
def deliver
|
6
|
+
invoke :push
|
7
|
+
invoke :pull
|
8
|
+
end
|
9
|
+
|
10
|
+
desc "push", "Build locally and push app image to registry"
|
11
|
+
def push
|
12
|
+
run_locally do
|
13
|
+
begin
|
14
|
+
debug "Using builder: #{MRSK.builder.name}"
|
15
|
+
info "Building image may take a while (run with --verbose for progress logging)"
|
16
|
+
execute *MRSK.builder.push
|
17
|
+
rescue SSHKit::Command::Failed => e
|
18
|
+
error "Missing compatible builder, so creating a new one first"
|
19
|
+
execute *MRSK.builder.create
|
20
|
+
execute *MRSK.builder.push
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
desc "pull", "Pull app image from the registry onto servers"
|
26
|
+
def pull
|
27
|
+
on(MRSK.config.hosts) { execute *MRSK.builder.pull }
|
28
|
+
end
|
29
|
+
|
30
|
+
desc "create", "Create a local build setup"
|
31
|
+
def create
|
32
|
+
run_locally do
|
33
|
+
debug "Using builder: #{MRSK.builder.name}"
|
34
|
+
execute *MRSK.builder.create
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
desc "remove", "Remove local build setup"
|
39
|
+
def remove
|
40
|
+
run_locally do
|
41
|
+
debug "Using builder: #{MRSK.builder.name}"
|
42
|
+
execute *MRSK.builder.remove
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
desc "details", "Show the name of the configured builder"
|
47
|
+
def details
|
48
|
+
run_locally do
|
49
|
+
puts "Builder: #{MRSK.builder.name} (#{MRSK.builder.target.class.name})"
|
50
|
+
puts capture(*MRSK.builder.info)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require "mrsk/cli/base"
|
2
|
+
|
3
|
+
require "mrsk/cli/app"
|
4
|
+
require "mrsk/cli/build"
|
5
|
+
require "mrsk/cli/prune"
|
6
|
+
require "mrsk/cli/registry"
|
7
|
+
require "mrsk/cli/server"
|
8
|
+
require "mrsk/cli/traefik"
|
9
|
+
|
10
|
+
class Mrsk::Cli::Main < Mrsk::Cli::Base
|
11
|
+
desc "deploy", "Deploy the app to servers"
|
12
|
+
def deploy
|
13
|
+
print_runtime do
|
14
|
+
invoke "mrsk:cli:server:bootstrap"
|
15
|
+
invoke "mrsk:cli:registry:login"
|
16
|
+
invoke "mrsk:cli:build:deliver"
|
17
|
+
invoke "mrsk:cli:traefik:boot"
|
18
|
+
invoke "mrsk:cli:app:stop"
|
19
|
+
invoke "mrsk:cli:app:boot"
|
20
|
+
invoke "mrsk:cli:prune:all"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
desc "redeploy", "Deploy new version of the app to servers (without bootstrapping servers, starting Traefik, pruning, and registry login)"
|
25
|
+
def redeploy
|
26
|
+
print_runtime do
|
27
|
+
invoke "mrsk:cli:build:deliver"
|
28
|
+
invoke "mrsk:cli:app:stop"
|
29
|
+
invoke "mrsk:cli:app:boot"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
desc "rollback [VERSION]", "Rollback the app to VERSION (that must already be on servers)"
|
34
|
+
def rollback(version)
|
35
|
+
on(MRSK.config.hosts) do
|
36
|
+
execute *MRSK.app.stop, raise_on_non_zero_exit: false
|
37
|
+
execute *MRSK.app.start(version: version)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
desc "details", "Display details about Traefik and app containers"
|
42
|
+
def details
|
43
|
+
invoke "mrsk:cli:traefik:details"
|
44
|
+
invoke "mrsk:cli:app:details"
|
45
|
+
end
|
46
|
+
|
47
|
+
desc "install", "Create config stub in config/deploy.yml and binstub in bin/mrsk"
|
48
|
+
option :skip_binstub, type: :boolean, default: false, desc: "Skip adding MRSK to the Gemfile and creating bin/mrsk binstub"
|
49
|
+
def install
|
50
|
+
require "fileutils"
|
51
|
+
|
52
|
+
if (deploy_file = Pathname.new(File.expand_path("config/deploy.yml"))).exist?
|
53
|
+
puts "Config file already exists in config/deploy.yml (remove first to create a new one)"
|
54
|
+
else
|
55
|
+
FileUtils.cp_r Pathname.new(File.expand_path("templates/deploy.yml", __dir__)), deploy_file
|
56
|
+
puts "Created configuration file in config/deploy.yml"
|
57
|
+
end
|
58
|
+
|
59
|
+
unless options[:skip_binstub]
|
60
|
+
if (binstub = Pathname.new(File.expand_path("bin/mrsk"))).exist?
|
61
|
+
puts "Binstub already exists in bin/mrsk (remove first to create a new one)"
|
62
|
+
else
|
63
|
+
`bundle add mrsk`
|
64
|
+
`bundle binstubs mrsk`
|
65
|
+
puts "Created binstub file in bin/mrsk"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
desc "remove", "Remove Traefik, app, and registry session from servers"
|
71
|
+
def remove
|
72
|
+
invoke "mrsk:cli:traefik:remove"
|
73
|
+
invoke "mrsk:cli:app:remove"
|
74
|
+
invoke "mrsk:cli:registry:logout"
|
75
|
+
end
|
76
|
+
|
77
|
+
desc "version", "Display the MRSK version"
|
78
|
+
def version
|
79
|
+
puts Mrsk::VERSION
|
80
|
+
end
|
81
|
+
|
82
|
+
desc "app", "Manage the application"
|
83
|
+
subcommand "app", Mrsk::Cli::App
|
84
|
+
|
85
|
+
desc "build", "Build the application image"
|
86
|
+
subcommand "build", Mrsk::Cli::Build
|
87
|
+
|
88
|
+
desc "prune", "Prune old application images and containers"
|
89
|
+
subcommand "prune", Mrsk::Cli::Prune
|
90
|
+
|
91
|
+
desc "registry", "Login and out of the image registry"
|
92
|
+
subcommand "registry", Mrsk::Cli::Registry
|
93
|
+
|
94
|
+
desc "server", "Bootstrap servers with Docker"
|
95
|
+
subcommand "server", Mrsk::Cli::Server
|
96
|
+
|
97
|
+
desc "traefik", "Manage the Traefik load balancer"
|
98
|
+
subcommand "traefik", Mrsk::Cli::Traefik
|
99
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require "mrsk/cli/base"
|
2
|
+
|
3
|
+
class Mrsk::Cli::Prune < Mrsk::Cli::Base
|
4
|
+
desc "all", "Prune unused images and stopped containers"
|
5
|
+
def all
|
6
|
+
invoke :containers
|
7
|
+
invoke :images
|
8
|
+
end
|
9
|
+
|
10
|
+
desc "images", "Prune unused images older than 30 days"
|
11
|
+
def images
|
12
|
+
on(MRSK.config.hosts) { execute *MRSK.prune.images }
|
13
|
+
end
|
14
|
+
|
15
|
+
desc "containers", "Prune stopped containers for the service older than 3 days"
|
16
|
+
def containers
|
17
|
+
on(MRSK.config.hosts) { execute *MRSK.prune.containers }
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require "mrsk/cli/base"
|
2
|
+
|
3
|
+
class Mrsk::Cli::Registry < Mrsk::Cli::Base
|
4
|
+
desc "login", "Login to the registry locally and remotely"
|
5
|
+
def login
|
6
|
+
run_locally { execute *MRSK.registry.login }
|
7
|
+
on(MRSK.config.hosts) { execute *MRSK.registry.login }
|
8
|
+
end
|
9
|
+
|
10
|
+
desc "logout", "Logout of the registry remotely"
|
11
|
+
def logout
|
12
|
+
on(MRSK.config.hosts) { execute *MRSK.registry.logout }
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# Name of your application. Used to uniquely configuring Traefik and app containers.
|
2
|
+
# Your Dockerfile should set LABEL service=the-same-value to ensure image pruning works.
|
3
|
+
service: my-app
|
4
|
+
|
5
|
+
# Name of the container image.
|
6
|
+
image: user/my-app
|
7
|
+
|
8
|
+
# Deploy to these servers.
|
9
|
+
servers:
|
10
|
+
- 192.168.0.1
|
11
|
+
|
12
|
+
# Credentials for your image host.
|
13
|
+
registry:
|
14
|
+
# Specify the registry server, if you're not using Docker Hub
|
15
|
+
# server: registry.digitalocean.com / ghcr.io / ...
|
16
|
+
username: my-user
|
17
|
+
password: my-password-should-go-somewhere-safe
|