mrsk 0.1.0 → 0.2.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: 04a17f68f19cfaf130747feded6450c389fdcb01bb83c4d44b1acb0c33d6fb60
4
+ data.tar.gz: 4d60e6c22acd3bf07fa22b0e488138166cf2265cedcd102ebb3245bb6318f5e0
5
5
  SHA512:
6
- metadata.gz: 6eee63c66d3f6585c2ac142d78ffdf952b141073793a8f8dd8d7b1551fbd5a1a98f12c590e83517f8e52e1ac58b07ebd48b55571ebe66c3c8a2ec677c47ef00d
7
- data.tar.gz: f46b8fc945be5c43427f7bf2300cbd95e80e30598fd8294e327f83253b25430937d44c255c8edb1d6b0fce613224acb367972bbeef125e7615293c83c23ddb2c
6
+ metadata.gz: 379a355dafd696bbe470367c712e318e6ff9b215143059bde147dc1fdca2870bc7e46a1f47057976c2c6a5ec2601ca7cef722d928c33d3b0f1ee0700a1ee934c
7
+ data.tar.gz: b0a6a7e0efa5824e116c3728726a3be1d6c9310903eefa992411a2d91731dddd8db6903d6a8697c5d5643b831b39adad9ec088ff28262d5b79e4f4aad3a90c46
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`:
124
+
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
+ ```
88
136
 
89
- ### Adding custom container labels
137
+ ### Using container labels
90
138
 
91
- You can specialize the default Traefik rules by setting custom labels on the containers that are being started:
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,139 @@ 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.
136
184
 
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`.
185
+ ### Using remote builder for single-arch
138
186
 
139
- ### Configuring native builder when multi-arch isn't needed
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.
140
188
 
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:
189
+ ```yaml
190
+ builder:
191
+ remote:
192
+ arch: amd64
193
+ host: ssh://root@192.168.0.1
194
+ ```
195
+
196
+ ### Using native builder when multi-arch isn't needed
197
+
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 accessories for database, cache, search services
248
+
249
+ 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:
250
+
251
+ ```yaml
252
+ accessories:
253
+ mysql:
254
+ image: mysql:5.7
255
+ host: 1.1.1.3
256
+ port: 3306
257
+ env:
258
+ clear:
259
+ MYSQL_ROOT_HOST: '%'
260
+ secret:
261
+ - MYSQL_ROOT_PASSWORD
262
+ volumes:
263
+ - /var/lib/mysql:/var/lib/mysql
264
+ redis:
265
+ image: redis:latest
266
+ host: 1.1.1.4
267
+ port: "36379:6379"
268
+ volumes:
269
+ - /var/lib/redis:/data
270
+ ```
271
+
272
+ Now run `mrsk accessory start mysql` to start the MySQL server on 1.1.1.3. See `mrsk accessory` for all the commands possible.
273
+
148
274
  ## Commands
149
275
 
150
- ### Remote execution
276
+ ### Running remote execution and runners
151
277
 
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:
278
+ If you need to execute commands inside the Rails containers, you can use `mrsk app exec` and `mrsk app runner`. Examples:
153
279
 
154
280
  ```bash
155
281
  # Runs command on all servers
156
282
  mrsk app exec 'ruby -v'
157
- App Host: xxx.xxx.xxx.xxx
283
+ App Host: 192.168.0.1
158
284
  ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [x86_64-linux]
159
285
 
160
- App Host: xxx.xxx.xxx.xxx
286
+ App Host: 192.168.0.2
161
287
  ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [x86_64-linux]
162
288
 
163
- # Runs command on first server
164
- mrsk app exec --once 'cat .ruby-version'
289
+ # Runs command on primary server
290
+ mrsk app exec --primary 'cat .ruby-version'
291
+ App Host: 192.168.0.1
165
292
  3.1.3
166
293
 
167
294
  # Runs Rails command on all servers
168
295
  mrsk app exec 'bin/rails about'
169
- App Host: xxx.xxx.xxx.xxx
296
+ App Host: 192.168.0.1
170
297
  About your application's environment
171
298
  Rails version 7.1.0.alpha
172
299
  Ruby version ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [x86_64-linux]
@@ -178,7 +305,7 @@ Environment production
178
305
  Database adapter sqlite3
179
306
  Database schema version 20221231233303
180
307
 
181
- App Host: xxx.xxx.xxx.xxx
308
+ App Host: 192.168.0.2
182
309
  About your application's environment
183
310
  Rails version 7.1.0.alpha
184
311
  Ruby version ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [x86_64-linux]
@@ -190,50 +317,50 @@ Environment production
190
317
  Database adapter sqlite3
191
318
  Database schema version 20221231233303
192
319
 
193
- # Runs Rails runner on first server
194
- mrsk app runner 'puts Rails.application.config.time_zone'
320
+ # Run Rails runner on primary server
321
+ mrsk app runner -p 'puts Rails.application.config.time_zone'
195
322
  UTC
196
323
  ```
197
324
 
198
- ### Running a Rails console on the primary host
325
+ ### Running a Rails console
199
326
 
200
327
  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
328
 
202
- ### Inspecting
329
+ ### Running details to see state of containers
203
330
 
204
- You can see the state of your servers by running `mrsk details`. It'll show something like this:
331
+ You can see the state of your servers by running `mrsk details`:
205
332
 
206
333
  ```
207
- Traefik Host: xxx.xxx.xxx.xxx
334
+ Traefik Host: 192.168.0.1
208
335
  CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
209
336
  6195b2a28c81 traefik "/entrypoint.sh --pr…" 30 minutes ago Up 19 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp traefik
210
337
 
211
- Traefik Host: 164.92.105.119
338
+ Traefik Host: 192.168.0.2
212
339
  CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
213
340
  de14a335d152 traefik "/entrypoint.sh --pr…" 30 minutes ago Up 19 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp traefik
214
341
 
215
- App Host: 164.90.145.60
342
+ App Host: 192.168.0.1
216
343
  CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
217
344
  badb1aa51db3 registry.digitalocean.com/user/app:6ef8a6a84c525b123c5245345a8483f86d05a123 "/rails/bin/docker-e…" 13 minutes ago Up 13 minutes 3000/tcp chat-6ef8a6a84c525b123c5245345a8483f86d05a123
218
345
 
219
- App Host: 164.92.105.119
346
+ App Host: 192.168.0.2
220
347
  CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
221
348
  1d3c91ed1f55 registry.digitalocean.com/user/app:6ef8a6a84c525b123c5245345a8483f86d05a123 "/rails/bin/docker-e…" 13 minutes ago Up 13 minutes 3000/tcp chat-6ef8a6a84c525b123c5245345a8483f86d05a123
222
349
  ```
223
350
 
224
351
  You can also see just info for app containers with `mrsk app details` or just for Traefik with `mrsk traefik details`.
225
352
 
226
- ### Rollback
353
+ ### Running rollback to fix a bad deploy
227
354
 
228
355
  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
356
 
230
357
  ```
231
- App Host: 164.92.105.119
358
+ App Host: 192.168.0.1
232
359
  CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
233
360
  1d3c91ed1f51 registry.digitalocean.com/user/app:6ef8a6a84c525b123c5245345a8483f86d05a123 "/rails/bin/docker-e…" 19 minutes ago Up 19 minutes 3000/tcp chat-6ef8a6a84c525b123c5245345a8483f86d05a123
234
361
  539f26b28369 registry.digitalocean.com/user/app:e5d9d7c2b898289dfbc5f7f1334140d984eedae4 "/rails/bin/docker-e…" 31 minutes ago Exited (1) 27 minutes ago chat-e5d9d7c2b898289dfbc5f7f1334140d984eedae4
235
362
 
236
- App Host: 164.90.145.60
363
+ App Host: 192.168.0.2
237
364
  CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
238
365
  badb1aa51db4 registry.digitalocean.com/user/app:6ef8a6a84c525b123c5245345a8483f86d05a123 "/rails/bin/docker-e…" 19 minutes ago Up 19 minutes 3000/tcp chat-6ef8a6a84c525b123c5245345a8483f86d05a123
239
366
  6f170d1172ae registry.digitalocean.com/user/app:e5d9d7c2b898289dfbc5f7f1334140d984eedae4 "/rails/bin/docker-e…" 31 minutes ago Exited (1) 27 minutes ago chat-e5d9d7c2b898289dfbc5f7f1334140d984eedae4
@@ -243,7 +370,7 @@ From the example above, we can see that `e5d9d7c2b898289dfbc5f7f1334140d984eedae
243
370
 
244
371
  Note that by default old containers are pruned after 3 days when you run `mrsk deploy`.
245
372
 
246
- ### Removing
373
+ ### Running removal to clean up servers
247
374
 
248
375
  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
376
 
@@ -0,0 +1,85 @@
1
+ require "mrsk/cli/base"
2
+
3
+ class Mrsk::Cli::Accessory < Mrsk::Cli::Base
4
+ desc "boot [NAME]", "Boot accessory service on host"
5
+ def boot(name)
6
+ accessory = MRSK.accessory(name)
7
+ on(accessory.host) { execute *accessory.run }
8
+ end
9
+
10
+ desc "reboot [NAME]", "Reboot accessory on host (stop container, remove container, start new container)"
11
+ def reboot(name)
12
+ invoke :stop, [ name ]
13
+ invoke :remove_container, [ name ]
14
+ invoke :boot, [ name ]
15
+ end
16
+
17
+ desc "start [NAME]", "Start existing accessory on host"
18
+ def start(name)
19
+ accessory = MRSK.accessory(name)
20
+ on(accessory.host) { execute *accessory.start }
21
+ end
22
+
23
+ desc "stop [NAME]", "Stop accessory on host"
24
+ def stop(name)
25
+ accessory = MRSK.accessory(name)
26
+ on(accessory.host) { execute *accessory.stop }
27
+ end
28
+
29
+ desc "restart [NAME]", "Restart accessory on host"
30
+ def restart(name)
31
+ invoke :stop, [ name ]
32
+ invoke :start, [ name ]
33
+ end
34
+
35
+ desc "details [NAME]", "Display details about accessory on host"
36
+ def details(name)
37
+ accessory = MRSK.accessory(name)
38
+ on(accessory.host) { puts capture_with_info(*accessory.info) }
39
+ end
40
+
41
+ desc "logs [NAME]", "Show log lines from accessory on host"
42
+ option :since, aliases: "-s", desc: "Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)"
43
+ option :lines, type: :numeric, aliases: "-n", desc: "Number of log lines to pull from each server"
44
+ option :grep, aliases: "-g", desc: "Show lines with grep match only (use this to fetch specific requests by id)"
45
+ option :follow, aliases: "-f", desc: "Follow logs on primary server (or specific host set by --hosts)"
46
+ def logs(name)
47
+ accessory = MRSK.accessory(name)
48
+
49
+ grep = options[:grep]
50
+
51
+ if options[:follow]
52
+ run_locally do
53
+ info "Following logs on #{accessory.host}..."
54
+ info accessory.follow_logs(grep: grep)
55
+ exec accessory.follow_logs(grep: grep)
56
+ end
57
+ else
58
+ since = options[:since]
59
+ lines = options[:lines]
60
+
61
+ on(accessory.host) do
62
+ puts capture_with_info(*accessory.logs(since: since, lines: lines, grep: grep))
63
+ end
64
+ end
65
+ end
66
+
67
+ desc "remove [NAME]", "Remove accessory container and image from host"
68
+ def remove(name)
69
+ invoke :stop, [ name ]
70
+ invoke :remove_container, [ name ]
71
+ invoke :remove_image, [ name ]
72
+ end
73
+
74
+ desc "remove_container [NAME]", "Remove accessory container from host"
75
+ def remove_container(name)
76
+ accessory = MRSK.accessory(name)
77
+ on(accessory.host) { execute *accessory.remove_container }
78
+ end
79
+
80
+ desc "remove_container [NAME]", "Remove accessory image from servers"
81
+ def remove_image(name)
82
+ accessory = MRSK.accessory(name)
83
+ on(accessory.host) { execute *accessory.remove_image }
84
+ end
85
+ end
data/lib/mrsk/cli/app.rb CHANGED
@@ -23,75 +23,101 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
23
23
  option :version, desc: "Defaults to the most recent git-hash in local repository"
24
24
  def start
25
25
  if (version = options[:version]).present?
26
- on(MRSK.config.hosts) { execute *MRSK.app.start(version: version) }
26
+ on(MRSK.hosts) { execute *MRSK.app.start(version: version) }
27
27
  else
28
- on(MRSK.config.hosts) { execute *MRSK.app.start, raise_on_non_zero_exit: false }
28
+ on(MRSK.hosts) { execute *MRSK.app.start, raise_on_non_zero_exit: false }
29
29
  end
30
30
  end
31
31
 
32
32
  desc "stop", "Stop app on servers"
33
33
  def stop
34
- on(MRSK.config.hosts) { execute *MRSK.app.stop, raise_on_non_zero_exit: false }
34
+ on(MRSK.hosts) { execute *MRSK.app.stop, raise_on_non_zero_exit: false }
35
35
  end
36
36
 
37
37
  desc "details", "Display details about app containers"
38
38
  def details
39
- on(MRSK.config.hosts) { |host| puts "App Host: #{host}\n" + capture(*MRSK.app.info, verbosity: Logger::INFO) + "\n\n" }
39
+ on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.info) }
40
40
  end
41
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
42
+ desc "exec [CMD]", "Execute a custom command on servers"
43
+ option :run, type: :boolean, default: false, desc: "Start a new container to run the command rather than reusing existing"
44
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
45
+ runner = options[:run] ? :run_exec : :exec
46
+ on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.send(runner, cmd)) }
50
47
  end
51
-
52
- desc "console", "Start Rails Console on primary host"
53
- option :host, desc: "Start console on a different host"
48
+
49
+ desc "console", "Start Rails Console on primary host (or specific host set by --hosts)"
54
50
  def console
55
- host = options[:host] || MRSK.config.primary_host
51
+ run_locally do
52
+ info "Launching Rails console on #{MRSK.primary_host}"
53
+ exec MRSK.app.console(host: MRSK.primary_host)
54
+ end
55
+ end
56
56
 
57
+ desc "bash", "Start a bash session on primary host (or specific host set by --hosts)"
58
+ def bash
57
59
  run_locally do
58
- puts "Launching Rails console on #{host}..."
59
- exec MRSK.app.console(host: host)
60
+ info "Launching bash session on #{MRSK.primary_host}"
61
+ exec MRSK.app.bash(host: MRSK.primary_host)
60
62
  end
61
63
  end
62
64
 
63
65
  desc "runner [EXPRESSION]", "Execute Rails runner with given expression"
64
- option :once, type: :boolean, default: false, desc:
65
66
  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
67
+ on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.exec("bin/rails", "runner", "'#{expression}'")) }
71
68
  end
72
69
 
73
70
  desc "containers", "List all the app containers currently on servers"
74
71
  def containers
75
- on(MRSK.config.hosts) { |host| puts "App Host: #{host}\n" + capture(*MRSK.app.list_containers, verbosity: Logger::INFO) + "\n\n" }
72
+ on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.list_containers) }
73
+ end
74
+
75
+ desc "current", "Return the current running container ID"
76
+ def current
77
+ on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.current_container_id) }
76
78
  end
77
79
 
78
- desc "logs", "Show last 100 log lines from app on servers"
80
+ desc "logs", "Show lines from app on servers"
81
+ option :since, aliases: "-s", desc: "Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)"
82
+ option :lines, type: :numeric, aliases: "-n", desc: "Number of log lines to pull from each server"
83
+ option :grep, aliases: "-g", desc: "Show lines with grep match only (use this to fetch specific requests by id)"
84
+ option :follow, aliases: "-f", desc: "Follow logs on primary server (or specific host set by --hosts)"
79
85
  def logs
80
86
  # 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" }
87
+
88
+ grep = options[:grep]
89
+
90
+ if options[:follow]
91
+ run_locally do
92
+ info "Following logs on #{MRSK.primary_host}..."
93
+ info MRSK.app.follow_logs(host: MRSK.primary_host, grep: grep)
94
+ exec MRSK.app.follow_logs(host: MRSK.primary_host, grep: grep)
95
+ end
96
+ else
97
+ since = options[:since]
98
+ lines = options[:lines]
99
+
100
+ on(MRSK.hosts) do |host|
101
+ begin
102
+ puts_by_host host, capture_with_info(*MRSK.app.logs(since: since, lines: lines, grep: grep))
103
+ rescue SSHKit::Command::Failed
104
+ puts_by_host host, "Nothing found"
105
+ end
106
+ end
107
+ end
82
108
  end
83
-
109
+
84
110
  desc "remove", "Remove app containers and images from servers"
85
111
  option :only, default: "", desc: "Use 'containers' or 'images'"
86
112
  def remove
87
113
  case options[:only]
88
114
  when "containers"
89
- on(MRSK.config.hosts) { execute *MRSK.app.remove_containers }
115
+ on(MRSK.hosts) { execute *MRSK.app.remove_containers }
90
116
  when "images"
91
- on(MRSK.config.hosts) { execute *MRSK.app.remove_images }
117
+ on(MRSK.hosts) { execute *MRSK.app.remove_images }
92
118
  else
93
- on(MRSK.config.hosts) { execute *MRSK.app.remove_containers }
94
- on(MRSK.config.hosts) { execute *MRSK.app.remove_images }
119
+ on(MRSK.hosts) { execute *MRSK.app.remove_containers }
120
+ on(MRSK.hosts) { execute *MRSK.app.remove_images }
95
121
  end
96
122
  end
97
123
  end
data/lib/mrsk/cli/base.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  require "thor"
2
- require "sshkit"
3
- require "sshkit/dsl"
2
+ require "mrsk/sshkit_with_ext"
4
3
 
5
4
  module Mrsk::Cli
6
5
  class Base < Thor
@@ -10,12 +9,34 @@ module Mrsk::Cli
10
9
 
11
10
  class_option :verbose, type: :boolean, aliases: "-v", desc: "Detailed logging"
12
11
 
12
+ class_option :version, desc: "Run commands against a specific app version"
13
+
14
+ class_option :primary, type: :boolean, aliases: "-p", desc: "Run commands only on primary host instead of all"
15
+ class_option :hosts, aliases: "-h", desc: "Run commands on these hosts instead of all (separate by comma)"
16
+ class_option :roles, aliases: "-r", desc: "Run commands on these roles instead of all (separate by comma)"
17
+
18
+ class_option :config_file, aliases: "-c", default: "config/deploy.yml", desc: "Path to config file (default: config/deploy.yml)"
19
+ class_option :destination, aliases: "-d", desc: "Specify destination to be used for config file (west -> deploy.west.yml)"
20
+
13
21
  def initialize(*)
14
22
  super
15
- MRSK.verbose = options[:verbose]
23
+ initialize_commander(options)
16
24
  end
17
25
 
18
26
  private
27
+ def initialize_commander(options)
28
+ MRSK.tap do |commander|
29
+ commander.config_file = Pathname.new(File.expand_path(options[:config_file]))
30
+ commander.destination = options[:destination]
31
+ commander.verbose = options[:verbose]
32
+ commander.version = options[:version]
33
+
34
+ commander.specific_hosts = options[:hosts]&.split(",")
35
+ commander.specific_roles = options[:roles]&.split(",")
36
+ commander.specific_primary! if options[:primary]
37
+ end
38
+ end
39
+
19
40
  def print_runtime
20
41
  started_at = Time.now
21
42
  yield