mrsk 0.1.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 45be50e3f2f3c5c629b1ec46ee23394b1913c758385ee431d44553506f40cc01
4
- data.tar.gz: fe11053be553d9af7d090b03b019dd09d1578bed94050b6be5699f99f6711be8
3
+ metadata.gz: a3cbef66064bc4d1a20707788eefd3827d4054d1278d85a8e2b425c4c5ad643e
4
+ data.tar.gz: 26e48f594d93f1b1a409a4601cc43c6d62fc35e935c7000c17addd6d5f720ac9
5
5
  SHA512:
6
- metadata.gz: 6eee63c66d3f6585c2ac142d78ffdf952b141073793a8f8dd8d7b1551fbd5a1a98f12c590e83517f8e52e1ac58b07ebd48b55571ebe66c3c8a2ec677c47ef00d
7
- data.tar.gz: f46b8fc945be5c43427f7bf2300cbd95e80e30598fd8294e327f83253b25430937d44c255c8edb1d6b0fce613224acb367972bbeef125e7615293c83c23ddb2c
6
+ metadata.gz: 3c84c6bada6dc82715349630807685d4f8a6c5dd64995760efd88666bc99a1c1b20230f47b23a7b71f8dfab5f4b2f34317d1fd2a5245b8c8a6b45575fa606002
7
+ data.tar.gz: 83565d28bcde4d782a829312685c2669512f6111233685309b015ed2b087ac5557456faae0460f472b18c480ef20892ae712a07f1b22de2cb2340a9f187c936b
data/README.md CHANGED
@@ -14,40 +14,47 @@ servers:
14
14
  - 192.168.0.2
15
15
  registry:
16
16
  username: registry-user-name
17
- password: <%= ENV["MRSK_REGISTRY_PASSWORD"] %>
17
+ password: <%= ENV.fetch("MRSK_REGISTRY_PASSWORD") %>
18
18
  ```
19
19
 
20
20
  Now you're ready to deploy a multi-arch image to the servers:
21
21
 
22
22
  ```
23
- mrsk deploy
23
+ MRSK_REGISTRY_PASSWORD=pw mrsk deploy
24
24
  ```
25
25
 
26
26
  This will:
27
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.
28
+ 1. Connect to the servers over SSH (using root by default, authenticated by your loaded ssh key)
29
+ 2. Install Docker on any server that might be missing it (using apt-get)
30
+ 3. Log into the registry both locally and remotely
31
+ 4. Build the image using the standard Dockerfile in the root of the application.
32
+ 5. Push the image to the registry.
33
+ 6. Pull the image from the registry on the servers.
34
+ 7. Ensure Traefik is running and accepting traffic on port 80.
35
+ 8. Stop any containers running a previous versions of the app.
36
+ 9. Start a new container with the version of the app that matches the current git version hash.
37
+ 10. Prune unused images and stopped containers to ensure servers don't fill up.
37
38
 
38
39
  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
 
41
+ ## Why not just run Capistrano or Kubernetes?
42
+
43
+ MRSK basically is Capistrano for Containers, which allow us to use vanilla servers as the hosts. No need to ensure that the servers have just the right version of Ruby or other dependencies you need. That all lives in the Docker image now. You can boot a brand new Ubuntu (or whatever) server, add it to the deploy servers of MRSK, and it'll be auto-provisioned with Docker, and run right away. Docker's layer caching also allows for quicker deployments with less mucking about on the server. And the images built for MRSK can be used for CI or later introspection.
44
+
45
+ Kubernetes is a beast. Running it yourself on your own hardware is not for the faint of heart. It's a fine option if you want to run on someone else's platform, like Render or Fly, but if you'd like the freedom to move between cloud and your own hardware, or even mix the two, MRSK is much simpler. You can see everything that's going on, it's just basic Docker commands being called.
46
+
40
47
  ## Configuration
41
48
 
42
49
  ### Using another registry than Docker Hub
43
50
 
44
- The default registry for Docker is Docker Hub. If you'd like to use a different one, just configure the server, like so:
51
+ The default registry is Docker Hub, but you can change it using `registry/server`:
45
52
 
46
53
  ```yaml
47
54
  registry:
48
55
  server: registry.digitalocean.com
49
56
  username: registry-user-name
50
- password: <%= ENV["MRSK_REGISTRY_PASSWORD"] %>
57
+ password: <%= ENV.fetch("MRSK_REGISTRY_PASSWORD") %>
51
58
  ```
52
59
 
53
60
  ### Using a different SSH user than root
@@ -58,9 +65,9 @@ The default SSH user is root, but you can change it using `ssh_user`:
58
65
  ssh_user: app
59
66
  ```
60
67
 
61
- ### Adding custom env variables
68
+ ### Using env variables
62
69
 
63
- You can inject custom env variables into the app containers using `env`:
70
+ You can inject env variables into the app containers using `env`:
64
71
 
65
72
  ```yaml
66
73
  env:
@@ -68,9 +75,38 @@ env:
68
75
  REDIS_URL: redis://redis1:6379/1
69
76
  ```
70
77
 
71
- ### Splitting servers into different roles
78
+ ### Using secret env variables
79
+
80
+ If you have env variables that are secret, you can divide the `env` block into `clear` and `secret`:
81
+
82
+ ```yaml
83
+ env:
84
+ clear:
85
+ DATABASE_URL: mysql2://db1/hey_production/
86
+ REDIS_URL: redis://redis1:6379/1
87
+ secret:
88
+ - DATABASE_PASSWORD
89
+ - REDIS_PASSWORD
90
+ ```
91
+
92
+ The list of secret env variables will be expanded at run time from your local machine. So a reference to a secret `DATABASE_PASSWORD` will look for `ENV["DATABASE_PASSWORD"]` on the machine running MRSK. Just like with build secrets.
93
+
94
+ If the referenced secret ENVs are missing, the configuration will be halted with a `KeyError` exception.
95
+
96
+ Note: Marking an ENV as secret currently only redacts its value in the output for MRSK. The ENV is still injected in the clear into the container at runtime.
97
+
98
+ ### Using volumes
99
+
100
+ You can add custom volumes into the app containers using `volumes`:
101
+
102
+ ```yaml
103
+ volumes:
104
+ - "/local/path:/container/path"
105
+ ```
106
+
107
+ ### Using different roles for servers
72
108
 
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:
109
+ 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:
74
110
 
75
111
  ```yaml
76
112
  servers:
@@ -84,23 +120,35 @@ servers:
84
120
  cmd: bin/jobs
85
121
  ```
86
122
 
87
- Traefik will only be installed and run on the servers in the `web` role (and on all servers if no roles are defined).
123
+ Note: Traefik will only by default be installed and run on the servers in the `web` role (and on all servers if no roles are defined). If you need Traefik on hosts in other roles than `web`, add `traefik: true`:
88
124
 
89
- ### Adding custom container labels
125
+ ```yaml
126
+ servers:
127
+ web:
128
+ - 192.168.0.1
129
+ - 192.168.0.2
130
+ web2:
131
+ traefik: true
132
+ hosts:
133
+ - 192.168.0.3
134
+ - 192.168.0.4
135
+ ```
90
136
 
91
- You can specialize the default Traefik rules by setting custom labels on the containers that are being started:
137
+ ### Using container labels
138
+
139
+ You can specialize the default Traefik rules by setting labels on the containers that are being started:
92
140
 
93
141
  ```
94
142
  labels:
95
143
  traefik.http.routers.hey.rule: '''Host(`app.hey.com`)'''
96
144
  ```
97
145
 
98
- (Note: The extra quotes are needed to ensure the rule is passed in correctly!)
146
+ Note: The extra quotes are needed to ensure the rule is passed in correctly!
99
147
 
100
148
  This allows you to run multiple applications on the same server sharing the same Traefik instance and port.
101
149
  See https://doc.traefik.io/traefik/routing/routers/#rule for a full list of available routing rules.
102
150
 
103
- The labels can even be applied on a per-role basis:
151
+ The labels can also be applied on a per-role basis:
104
152
 
105
153
  ```yaml
106
154
  servers:
@@ -113,60 +161,147 @@ servers:
113
161
  - 192.168.0.4
114
162
  cmd: bin/jobs
115
163
  labels:
116
- my-custom-label: "50"
164
+ my-label: "50"
117
165
  ```
118
166
 
119
- ### Configuring remote builder for native multi-arch
167
+ ### Using remote builder for native multi-arch
120
168
 
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.
169
+ If you're developing on ARM64 (like Apple Silicon), but you want to deploy on AMD64 (x86 64-bit), you can use multi-archecture 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.
122
170
 
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:
171
+ 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:
124
172
 
125
173
  ```yaml
126
174
  builder:
127
175
  local:
128
176
  arch: arm64
129
- host: unix:///Users/dhh/.docker/run/docker.sock
177
+ host: unix:///Users/<%= `whoami`.strip %>/.docker/run/docker.sock
130
178
  remote:
131
179
  arch: amd64
132
180
  host: ssh://root@192.168.0.1
133
181
  ```
134
182
 
135
- Note: You must have Docker running on the remote host being used as a builder.
183
+ Note: You must have Docker running on the remote host being used as a builder. This instance should only be shared for builds using the same registry and credentials.
184
+
185
+ ### Using remote builder for single-arch
136
186
 
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`.
187
+ If you're developing on ARM64 (like Apple Silicon), want to deploy on AMD64 (x86 64-bit), but don't need to run the image locally (or on other ARM64 hosts), you can configure a remote builder that just targets AMD64. This is a bit faster than building with multi-arch, as there's nothing to build locally.
188
+
189
+ ```yaml
190
+ builder:
191
+ remote:
192
+ arch: amd64
193
+ host: ssh://root@192.168.0.1
194
+ ```
138
195
 
139
- ### Configuring native builder when multi-arch isn't needed
196
+ ### Using native builder when multi-arch isn't needed
140
197
 
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:
198
+ If you're developing on the same architecture as the one you're deploying on, you can speed up the build by forgoing both multi-arch and remote building:
142
199
 
143
200
  ```yaml
144
201
  builder:
145
202
  multiarch: false
146
203
  ```
147
204
 
205
+ This is also a good option if you're running MRSK from a CI server that shares architecture with the deployment servers.
206
+
207
+ ### Using build secrets for new images
208
+
209
+ Some images need a secret passed in during build time, like a GITHUB_TOKEN to give access to private gem repositories. This can be done by having the secret in ENV, then referencing it in the builder configuration:
210
+
211
+ ```yaml
212
+ builder:
213
+ secrets:
214
+ - GITHUB_TOKEN
215
+ ```
216
+
217
+ This build secret can then be referenced in the Dockerfile:
218
+
219
+ ```
220
+ # Copy Gemfiles
221
+ COPY Gemfile Gemfile.lock ./
222
+
223
+ # Install dependencies, including private repositories via access token
224
+ RUN --mount=type=secret,id=GITHUB_TOKEN \
225
+ BUNDLE_GITHUB__COM=x-access-token:$(cat /run/secrets/GITHUB_TOKEN) \
226
+ bundle install
227
+ ```
228
+
229
+ ### Configuring build args for new images
230
+
231
+ Build arguments that aren't secret can also be configured:
232
+
233
+ ```yaml
234
+ builder:
235
+ args:
236
+ RUBY_VERSION: 3.2.0
237
+ ```
238
+
239
+ This build argument can then be used in the Dockerfile:
240
+
241
+ ```
242
+ # Private repositories need an access token during the build
243
+ ARG RUBY_VERSION
244
+ FROM ruby:$RUBY_VERSION-slim as base
245
+ ```
246
+
247
+ ### Using without RAILS_MASTER_KEY
248
+
249
+ If you're using MRSK with older Rails apps that predate RAILS_MASTER_KEY, or with a non-Rails app, you can skip the default usage and reference:
250
+
251
+ ```yaml
252
+ skip_master_key: true
253
+ ```
254
+
255
+ ### Using accessories for database, cache, search services
256
+
257
+ 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:
258
+
259
+ ```yaml
260
+ accessories:
261
+ mysql:
262
+ image: mysql:5.7
263
+ host: 1.1.1.3
264
+ port: 3306
265
+ env:
266
+ clear:
267
+ MYSQL_ROOT_HOST: '%'
268
+ secret:
269
+ - MYSQL_ROOT_PASSWORD
270
+ volumes:
271
+ - /var/lib/mysql:/var/lib/mysql
272
+ redis:
273
+ image: redis:latest
274
+ host: 1.1.1.4
275
+ port: "36379:6379"
276
+ volumes:
277
+ - /var/lib/redis:/data
278
+ ```
279
+
280
+ Now run `mrsk accessory start mysql` to start the MySQL server on 1.1.1.3. See `mrsk accessory` for all the commands possible.
281
+
148
282
  ## Commands
149
283
 
150
- ### Remote execution
284
+ ### Running remote execution and runners
151
285
 
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:
286
+ If you need to execute commands inside the Rails containers, you can use `mrsk app exec` and `mrsk app runner`. Examples:
153
287
 
154
288
  ```bash
155
289
  # Runs command on all servers
156
290
  mrsk app exec 'ruby -v'
157
- App Host: xxx.xxx.xxx.xxx
291
+ App Host: 192.168.0.1
158
292
  ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [x86_64-linux]
159
293
 
160
- App Host: xxx.xxx.xxx.xxx
294
+ App Host: 192.168.0.2
161
295
  ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [x86_64-linux]
162
296
 
163
- # Runs command on first server
164
- mrsk app exec --once 'cat .ruby-version'
297
+ # Runs command on primary server
298
+ mrsk app exec --primary 'cat .ruby-version'
299
+ App Host: 192.168.0.1
165
300
  3.1.3
166
301
 
167
302
  # Runs Rails command on all servers
168
303
  mrsk app exec 'bin/rails about'
169
- App Host: xxx.xxx.xxx.xxx
304
+ App Host: 192.168.0.1
170
305
  About your application's environment
171
306
  Rails version 7.1.0.alpha
172
307
  Ruby version ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [x86_64-linux]
@@ -178,7 +313,7 @@ Environment production
178
313
  Database adapter sqlite3
179
314
  Database schema version 20221231233303
180
315
 
181
- App Host: xxx.xxx.xxx.xxx
316
+ App Host: 192.168.0.2
182
317
  About your application's environment
183
318
  Rails version 7.1.0.alpha
184
319
  Ruby version ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [x86_64-linux]
@@ -190,50 +325,50 @@ Environment production
190
325
  Database adapter sqlite3
191
326
  Database schema version 20221231233303
192
327
 
193
- # Runs Rails runner on first server
194
- mrsk app runner 'puts Rails.application.config.time_zone'
328
+ # Run Rails runner on primary server
329
+ mrsk app runner -p 'puts Rails.application.config.time_zone'
195
330
  UTC
196
331
  ```
197
332
 
198
- ### Running a Rails console on the primary host
333
+ ### Running a Rails console
199
334
 
200
335
  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
336
 
202
- ### Inspecting
337
+ ### Running details to see state of containers
203
338
 
204
- You can see the state of your servers by running `mrsk details`. It'll show something like this:
339
+ You can see the state of your servers by running `mrsk details`:
205
340
 
206
341
  ```
207
- Traefik Host: xxx.xxx.xxx.xxx
342
+ Traefik Host: 192.168.0.1
208
343
  CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
209
344
  6195b2a28c81 traefik "/entrypoint.sh --pr…" 30 minutes ago Up 19 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp traefik
210
345
 
211
- Traefik Host: 164.92.105.119
346
+ Traefik Host: 192.168.0.2
212
347
  CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
213
348
  de14a335d152 traefik "/entrypoint.sh --pr…" 30 minutes ago Up 19 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp traefik
214
349
 
215
- App Host: 164.90.145.60
350
+ App Host: 192.168.0.1
216
351
  CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
217
352
  badb1aa51db3 registry.digitalocean.com/user/app:6ef8a6a84c525b123c5245345a8483f86d05a123 "/rails/bin/docker-e…" 13 minutes ago Up 13 minutes 3000/tcp chat-6ef8a6a84c525b123c5245345a8483f86d05a123
218
353
 
219
- App Host: 164.92.105.119
354
+ App Host: 192.168.0.2
220
355
  CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
221
356
  1d3c91ed1f55 registry.digitalocean.com/user/app:6ef8a6a84c525b123c5245345a8483f86d05a123 "/rails/bin/docker-e…" 13 minutes ago Up 13 minutes 3000/tcp chat-6ef8a6a84c525b123c5245345a8483f86d05a123
222
357
  ```
223
358
 
224
359
  You can also see just info for app containers with `mrsk app details` or just for Traefik with `mrsk traefik details`.
225
360
 
226
- ### Rollback
361
+ ### Running rollback to fix a bad deploy
227
362
 
228
363
  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:
229
364
 
230
365
  ```
231
- App Host: 164.92.105.119
366
+ App Host: 192.168.0.1
232
367
  CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
233
368
  1d3c91ed1f51 registry.digitalocean.com/user/app:6ef8a6a84c525b123c5245345a8483f86d05a123 "/rails/bin/docker-e…" 19 minutes ago Up 19 minutes 3000/tcp chat-6ef8a6a84c525b123c5245345a8483f86d05a123
234
369
  539f26b28369 registry.digitalocean.com/user/app:e5d9d7c2b898289dfbc5f7f1334140d984eedae4 "/rails/bin/docker-e…" 31 minutes ago Exited (1) 27 minutes ago chat-e5d9d7c2b898289dfbc5f7f1334140d984eedae4
235
370
 
236
- App Host: 164.90.145.60
371
+ App Host: 192.168.0.2
237
372
  CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
238
373
  badb1aa51db4 registry.digitalocean.com/user/app:6ef8a6a84c525b123c5245345a8483f86d05a123 "/rails/bin/docker-e…" 19 minutes ago Up 19 minutes 3000/tcp chat-6ef8a6a84c525b123c5245345a8483f86d05a123
239
374
  6f170d1172ae registry.digitalocean.com/user/app:e5d9d7c2b898289dfbc5f7f1334140d984eedae4 "/rails/bin/docker-e…" 31 minutes ago Exited (1) 27 minutes ago chat-e5d9d7c2b898289dfbc5f7f1334140d984eedae4
@@ -243,7 +378,7 @@ From the example above, we can see that `e5d9d7c2b898289dfbc5f7f1334140d984eedae
243
378
 
244
379
  Note that by default old containers are pruned after 3 days when you run `mrsk deploy`.
245
380
 
246
- ### Removing
381
+ ### Running removal to clean up servers
247
382
 
248
383
  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.
249
384
 
data/bin/mrsk CHANGED
@@ -1,5 +1,13 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
+ # Prevent failures from being reported twice.
4
+ Thread.report_on_exception = false
5
+
3
6
  require "mrsk/cli"
4
7
 
5
- Mrsk::Cli::Main.start(ARGV)
8
+ begin
9
+ Mrsk::Cli::Main.start(ARGV)
10
+ rescue SSHKit::Runner::ExecuteError => e
11
+ puts " \e[31mERROR (#{e.cause.class}): #{e.cause.message}\e[0m"
12
+ puts e.cause.backtrace if ENV["VERBOSE"]
13
+ end
@@ -0,0 +1,181 @@
1
+ require "mrsk/cli/base"
2
+
3
+ class Mrsk::Cli::Accessory < Mrsk::Cli::Base
4
+ desc "boot [NAME]", "Boot accessory service on host (use NAME=all to boot all accessories)"
5
+ def boot(name)
6
+ if name == "all"
7
+ MRSK.accessory_names.each { |accessory_name| boot(accessory_name) }
8
+ else
9
+ with_accessory(name) do |accessory|
10
+ directories(name)
11
+ upload(name)
12
+ on(accessory.host) { execute *accessory.run }
13
+ end
14
+ end
15
+ end
16
+
17
+ desc "upload [NAME]", "Upload accessory files to host"
18
+ def upload(name)
19
+ with_accessory(name) do |accessory|
20
+ on(accessory.host) do
21
+ accessory.files.each do |(local, remote)|
22
+ accessory.ensure_local_file_present(local)
23
+
24
+ execute *accessory.make_directory_for(remote)
25
+ upload! local, remote
26
+ execute :chmod, "755", remote
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ desc "directories [NAME]", "Create accessory directories on host"
33
+ def directories(name)
34
+ with_accessory(name) do |accessory|
35
+ on(accessory.host) do
36
+ accessory.directories.keys.each do |host_path|
37
+ execute *accessory.make_directory(host_path)
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ desc "reboot [NAME]", "Reboot accessory on host (stop container, remove container, start new container)"
44
+ def reboot(name)
45
+ with_accessory(name) do |accessory|
46
+ stop(name)
47
+ remove_container(name)
48
+ boot(name)
49
+ end
50
+ end
51
+
52
+ desc "start [NAME]", "Start existing accessory on host"
53
+ def start(name)
54
+ with_accessory(name) do |accessory|
55
+ on(accessory.host) { execute *accessory.start }
56
+ end
57
+ end
58
+
59
+ desc "stop [NAME]", "Stop accessory on host"
60
+ def stop(name)
61
+ with_accessory(name) do |accessory|
62
+ on(accessory.host) { execute *accessory.stop, raise_on_non_zero_exit: false }
63
+ end
64
+ end
65
+
66
+ desc "restart [NAME]", "Restart accessory on host"
67
+ def restart(name)
68
+ with_accessory(name) do
69
+ stop(name)
70
+ start(name)
71
+ end
72
+ end
73
+
74
+ desc "details [NAME]", "Display details about accessory on host (use NAME=all to boot all accessories)"
75
+ def details(name)
76
+ if name == "all"
77
+ MRSK.accessory_names.each { |accessory_name| details(accessory_name) }
78
+ else
79
+ with_accessory(name) do |accessory|
80
+ on(accessory.host) { puts capture_with_info(*accessory.info) }
81
+ end
82
+ end
83
+ end
84
+
85
+ desc "exec [NAME] [CMD]", "Execute a custom command on accessory host"
86
+ option :run, type: :boolean, default: false, desc: "Start a new container to run the command rather than reusing existing"
87
+ def exec(name, cmd)
88
+ with_accessory(name) do |accessory|
89
+ runner = options[:run] ? :run_exec : :exec
90
+ on(accessory.host) { |host| puts_by_host host, capture_with_info(*accessory.send(runner, cmd)) }
91
+ end
92
+ end
93
+
94
+ desc "bash [NAME]", "Start a bash session on primary host (or specific host set by --hosts)"
95
+ def bash(name)
96
+ with_accessory(name) do |accessory|
97
+ run_locally do
98
+ info "Launching bash session on #{accessory.host}"
99
+ exec accessory.bash(host: accessory.host)
100
+ end
101
+ end
102
+ end
103
+
104
+ desc "logs [NAME]", "Show log lines from accessory on host"
105
+ option :since, aliases: "-s", desc: "Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)"
106
+ option :lines, type: :numeric, aliases: "-n", desc: "Number of log lines to pull from each server"
107
+ option :grep, aliases: "-g", desc: "Show lines with grep match only (use this to fetch specific requests by id)"
108
+ option :follow, aliases: "-f", desc: "Follow logs on primary server (or specific host set by --hosts)"
109
+ def logs(name)
110
+ with_accessory(name) do |accessory|
111
+ grep = options[:grep]
112
+
113
+ if options[:follow]
114
+ run_locally do
115
+ info "Following logs on #{accessory.host}..."
116
+ info accessory.follow_logs(grep: grep)
117
+ exec accessory.follow_logs(grep: grep)
118
+ end
119
+ else
120
+ since = options[:since]
121
+ lines = options[:lines]
122
+
123
+ on(accessory.host) do
124
+ puts capture_with_info(*accessory.logs(since: since, lines: lines, grep: grep))
125
+ end
126
+ end
127
+ end
128
+ end
129
+
130
+ desc "remove [NAME]", "Remove accessory container and image from host (use NAME=all to boot all accessories)"
131
+ def remove(name)
132
+ if name == "all"
133
+ MRSK.accessory_names.each { |accessory_name| remove(accessory_name) }
134
+ else
135
+ with_accessory(name) do
136
+ stop(name)
137
+ remove_container(name)
138
+ remove_image(name)
139
+ remove_service_directory(name)
140
+ end
141
+ end
142
+ end
143
+
144
+ desc "remove_container [NAME]", "Remove accessory container from host"
145
+ def remove_container(name)
146
+ with_accessory(name) do |accessory|
147
+ on(accessory.host) { execute *accessory.remove_container }
148
+ end
149
+ end
150
+
151
+ desc "remove_container [NAME]", "Remove accessory image from host"
152
+ def remove_image(name)
153
+ with_accessory(name) do |accessory|
154
+ on(accessory.host) { execute *accessory.remove_image }
155
+ end
156
+ end
157
+
158
+ desc "remove_service_directory [NAME]", "Remove accessory directory used for uploaded files and data directories from host"
159
+ def remove_service_directory(name)
160
+ with_accessory(name) do |accessory|
161
+ on(accessory.host) { execute *accessory.remove_service_directory }
162
+ end
163
+ end
164
+
165
+ private
166
+ def with_accessory(name)
167
+ if accessory = MRSK.accessory(name)
168
+ yield accessory
169
+ else
170
+ error_on_missing_accessory(name)
171
+ end
172
+ end
173
+
174
+ def error_on_missing_accessory(name)
175
+ options = MRSK.accessory_names.presence
176
+
177
+ error \
178
+ "No accessory by the name of '#{name}'" +
179
+ (options ? " (options: #{options.to_sentence})" : "")
180
+ end
181
+ end