mrsk 0.8.3 → 0.9.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: 0065b91bb3c00ef0ef6db78eb605d8db5df3606ccf97be15bb059ab1985231d5
4
- data.tar.gz: 2ed2b0c6235ea398bc5717aebf99e2a4f1e6c9434c64da5eed6d8899b0ca335e
3
+ metadata.gz: c57766d24180e114034c8c4e241eb2c5fd62774097b00b37f81bdf000643a078
4
+ data.tar.gz: cac9390c76fd4dad89a8dd37e099c64df29db16126a4417ea3ab7d9cae586055
5
5
  SHA512:
6
- metadata.gz: c00ed2e4693b62e0eeb238c17d4c17b3e06ed03ed580aea7ca724a4629c28274d77bc31fa63ca1d1e5f07b1f73e6b2ecc164a076d2ad5307b7e30625cea2ee7a
7
- data.tar.gz: ee273834e9e330640ef6b55acc4dabeeef6c4e9178112351376c3ab1e23afeb9fa389a08b6cf5a8c3bbaafc079d7cabc98d316fd08030a8886c12d5020a68653
6
+ metadata.gz: 6c5bfd3bc0663ada2ae40bd6f8298081d3bbfc7522002cd69ac564ff14d27819881bffdf5cbe7967d5cb7a7128341a8b500750e8dbae435432fca2c33d17680b
7
+ data.tar.gz: e82f424d51621b6460ec4b477799f31b265dbe0356f448f178c70c44eafd865fc326156a6be55f341f0a59294e50d40402cb98559ac1cad9ab58bbd840d4d51e
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # MRSK
2
2
 
3
- MRSK deploys web 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. It was built for Rails applications, but works with any type of web app that can be bundled with Docker.
3
+ MRSK deploys web apps anywhere from bare metal to cloud VMs using 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. It was built for Rails applications, but works with any type of web app that can be containerized with Docker.
4
+
5
+ Watch the screencast: https://www.youtube.com/watch?v=LL1cV2FXZ5I
4
6
 
5
7
  ## Installation
6
8
 
@@ -31,37 +33,39 @@ mrsk deploy
31
33
 
32
34
  This will:
33
35
 
34
- 1. Connect to the servers over SSH (using root by default, authenticated by your loaded ssh key)
36
+ 1. Connect to the servers over SSH (using root by default, authenticated by your ssh key)
35
37
  2. Install Docker on any server that might be missing it (using apt-get)
36
38
  3. Log into the registry both locally and remotely
37
39
  4. Build the image using the standard Dockerfile in the root of the application.
38
40
  5. Push the image to the registry.
39
- 6. Pull the image from the registry on the servers.
41
+ 6. Pull the image from the registry onto the servers.
40
42
  7. Ensure Traefik is running and accepting traffic on port 80.
41
43
  8. Ensure your app responds with `200 OK` to `GET /up`.
42
- 9. Stop any containers running a previous versions of the app.
43
- 10. Start a new container with the version of the app that matches the current git version hash.
44
+ 9. Start a new container with the version of the app that matches the current git version hash.
45
+ 10. Stop the old container running the previous version of the app.
44
46
  11. Prune unused images and stopped containers to ensure servers don't fill up.
45
47
 
46
48
  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.
47
49
 
48
50
  ## Vision
49
51
 
50
- In the past decade+, there's been an explosion in commercial offerings that make deploying web apps easier. Heroku kicked it off with an incredible offering that stayed ahead of the competition seemingly forever. These days we have excellent alternatives like Fly.io and Render. And hosted Kubernetes is making things easier too on AWS, GCP, Digital Ocean, and elsewhere. But these are all offerings that have you renting computers in the cloud at a premium. If you want to run on our own hardware, or even just have a clear migration path to do so, you need to carefully consider how locked in you get to these commercial platforms. Preferably before the bills swallow your business whole!
52
+ In the past decade+, there's been an explosion in commercial offerings that make deploying web apps easier. Heroku kicked it off with an incredible offering that stayed ahead of the competition seemingly forever. These days we have excellent alternatives like Fly.io and Render. And hosted Kubernetes is making things easier too on AWS, GCP, Digital Ocean, and elsewhere. But these are all offerings that have you renting computers in the cloud at a premium. If you want to run on your own hardware, or even just have a clear migration path to do so in the future, you need to carefully consider how locked in you get to these commercial platforms. Preferably before the bills swallow your business whole!
51
53
 
52
- MRSK seeks to bring the advance in ergonomics pioneered by these commercial offerings to deploying web apps anywhere. Whether that's low-cost cloud options without the managed-service markup from the likes of Digital Ocean, Hetzner, OVH, etc, or it's your own colocated metal. To MRSK, it's all the same. Feed the config file a list of IP addresses with vanilla Ubuntu servers that have seen no prep beyond an added SSH key, and you'll be running in literally minutes.
54
+ MRSK seeks to bring the advance in ergonomics pioneered by these commercial offerings to deploying web apps anywhere. Whether that's low-cost cloud options without the managed-service markup from the likes of Digital Ocean, Hetzner, OVH, etc, or it's your own colocated bare metal. To MRSK, it's all the same. Feed the config file a list of IP addresses with vanilla Ubuntu servers that have seen no prep beyond an added SSH key, and you'll be running in literally minutes.
53
55
 
54
- This structure also gives you enormous portability. You can have your web app deployed on several clouds at ease like this. Or you can buy the baseline with your own hardware, then deploy to a cloud before a big seasonal spike to get more capacity. When you're not locked into a single provider from a tooling perspective, there's a lot of compelling options available.
56
+ This approach gives you enormous portability. You can have your web app deployed on several clouds at ease like this. Or you can buy the baseline with your own hardware, then deploy to a cloud before a big seasonal spike to get more capacity. When you're not locked into a single provider from a tooling perspective, there are a lot of compelling options available.
55
57
 
56
- Ultimately, MRSK is meant to compress the complexity of going to production using open source tooling that isn't tied to any commercial offering. Not to zero, though. You're probably still better off with a fully managed service if basic Linux or Docker is still difficult, but from an early stage when those concepts are familiar.
58
+ Ultimately, MRSK is meant to compress the complexity of going to production using open source tooling that isn't tied to any commercial offering. Not to zero, mind you. You're probably still better off with a fully managed service if basic Linux or Docker is still difficult, but as soon as those concepts are familiar, you'll be ready to go with MRSK.
57
59
 
58
60
  ## Why not just run Capistrano, Kubernetes or Docker Swarm?
59
61
 
60
- 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.
62
+ MRSK basically is Capistrano for Containers, without the need to carefully prepare servers in advance. 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 list of servers in MRSK, and it'll be auto-provisioned with Docker, and run right away. Docker's layer caching also speeds up deployments with less mucking about on the server. And the images built for MRSK can be used for CI or later introspection.
61
63
 
62
64
  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, either transparently [like Render](https://thenewstack.io/render-cloud-deployment-with-less-engineering/) or explicitly on AWS/GCP, 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.
63
65
 
64
- Docker Swarm is much simpler than Kubernetes, but it's still built on the same declarative model that uses state reconciliation. MRSK is intentionally designed to around imperative commands, like Capistrano.
66
+ Docker Swarm is much simpler than Kubernetes, but it's still built on the same declarative model that uses state reconciliation. MRSK is intentionally designed around imperative commands, like Capistrano.
67
+
68
+ Ultimately, there are a myriad of ways to deploy web apps, but this is the toolkit we're using at [37signals](https://37signals.com) to bring [HEY](https://www.hey.com) [home from the cloud](https://world.hey.com/dhh/why-we-re-leaving-the-cloud-654b47e0) without losing the advantages of modern containerization tooling.
65
69
 
66
70
  ## Configuration
67
71
 
@@ -74,6 +78,71 @@ MRSK_REGISTRY_PASSWORD=pw
74
78
  DB_PASSWORD=secret123
75
79
  ```
76
80
 
81
+ ### Using a generated .env file
82
+
83
+ #### 1Password as a secret store
84
+
85
+ If you're using a centralized secret store, like 1Password, you can create `.env.erb` as a template which looks up the secrets. Example of a .env.erb file:
86
+
87
+ ```erb
88
+ <% if (session_token = `op signin --account my-one-password-account --raw`.strip) != "" %># Generated by mrsk envify
89
+ GITHUB_TOKEN=<%= `gh config get -h github.com oauth_token`.strip %>
90
+ MRSK_REGISTRY_PASSWORD=<%= `op read "op://Vault/Docker Hub/password" -n --session #{session_token}` %>
91
+ RAILS_MASTER_KEY=<%= `op read "op://Vault/My App/RAILS_MASTER_SECRET" -n --session #{session_token}` %>
92
+ MYSQL_ROOT_PASSWORD=<%= `op read "op://Vault/My App/MYSQL_ROOT_PASSWORD" -n --session #{session_token}` %>
93
+ <% else raise ArgumentError, "Session token missing" end %>
94
+ ```
95
+
96
+ This template can safely be checked into git. Then everyone deploying the app can run `mrsk envify` when they setup the app for the first time or passwords change to get the correct `.env` file.
97
+
98
+ If you need separate env variables for different destinations, you can set them with `.env.destination.erb` for the template, which will generate `.env.staging` when run with `mrsk envify -d staging`.
99
+
100
+ #### Bitwarden as a secret store
101
+
102
+ If you are using open source secret store like bitwarden, you can create `.env.erb` as a template which looks up the secrets.
103
+
104
+ You can store `SOME_SECRET` in a secure note in bitwarden vault.
105
+
106
+ ```
107
+ $ bw list items --search SOME_SECRET | jq
108
+ ? Master password: [hidden]
109
+
110
+ [
111
+ {
112
+ "object": "item",
113
+ "id": "123123123-1232-4224-222f-234234234234",
114
+ "organizationId": null,
115
+ "folderId": null,
116
+ "type": 2,
117
+ "reprompt": 0,
118
+ "name": "SOME_SECRET",
119
+ "notes": "yyy",
120
+ "favorite": false,
121
+ "secureNote": {
122
+ "type": 0
123
+ },
124
+ "collectionIds": [],
125
+ "revisionDate": "2023-02-28T23:54:47.868Z",
126
+ "creationDate": "2022-11-07T03:16:05.828Z",
127
+ "deletedDate": null
128
+ }
129
+ ]
130
+ ```
131
+
132
+ and extract the `id` of `SOME_SECRET` from the `json` above and use in the `erb` below.
133
+
134
+
135
+ Example `.env.erb` file:
136
+
137
+ ```erb
138
+ <% if (session_token=`bw unlock --raw`.strip) != "" %># Generated by mrsk envify
139
+ SOME_SECRET=<%= `bw get notes 123123123-1232-4224-222f-234234234234 --session #{session_token}` %>
140
+ <% else raise ArgumentError, "session_token token missing" end %>
141
+ ```
142
+
143
+ Then everyone deploying the app can run `mrsk envify` and mrsk will generate `.env`
144
+
145
+
77
146
  ### Using another registry than Docker Hub
78
147
 
79
148
  The default registry is Docker Hub, but you can change it using `registry/server`:
@@ -81,10 +150,14 @@ The default registry is Docker Hub, but you can change it using `registry/server
81
150
  ```yaml
82
151
  registry:
83
152
  server: registry.digitalocean.com
84
- username: registry-user-name
85
- password: <%= ENV.fetch("MRSK_REGISTRY_PASSWORD") %>
153
+ username:
154
+ - DOCKER_REGISTRY_TOKEN
155
+ password:
156
+ - DOCKER_REGISTRY_TOKEN
86
157
  ```
87
158
 
159
+ A reference to secret `DOCKER_REGISTRY_TOKEN` will look for `ENV["DOCKER_REGISTRY_TOKEN"]` on the machine running MRSK.
160
+
88
161
  ### Using a different SSH user than root
89
162
 
90
163
  The default SSH user is root, but you can change it using `ssh/user`:
@@ -209,9 +282,30 @@ servers:
209
282
  my-label: "50"
210
283
  ```
211
284
 
285
+ ### Using container options
286
+
287
+ You can specialize the options used to start containers using the `options` definitions:
288
+
289
+ ```yaml
290
+ servers:
291
+ web:
292
+ - 192.168.0.1
293
+ - 192.168.0.2
294
+ job:
295
+ hosts:
296
+ - 192.168.0.3
297
+ - 192.168.0.4
298
+ cmd: bin/jobs
299
+ options:
300
+ cap-add: true
301
+ cpu-count: 4
302
+ ```
303
+
304
+ That'll start the job containers with `docker run ... --cap-add --cpu-count 4 ...`.
305
+
212
306
  ### Using remote builder for native multi-arch
213
307
 
214
- 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.
308
+ If you're developing on ARM64 (like Apple Silicon), but you want to deploy on AMD64 (x86 64-bit), you can use multi-architecture images. By default, MRSK will setup a local buildx configuration that does this through QEMU emulation. But this can be quite slow, especially on the first build.
215
309
 
216
310
  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:
217
311
 
@@ -249,9 +343,29 @@ builder:
249
343
 
250
344
  This is also a good option if you're running MRSK from a CI server that shares architecture with the deployment servers.
251
345
 
346
+ ### Using a different Dockerfile or context when building
347
+
348
+ If you need to pass a different Dockerfile or context to the build command (e.g. if you're using a monorepo or you have
349
+ different Dockerfiles), you can do so in the builder options:
350
+
351
+ ```yaml
352
+ # Use a different Dockerfile
353
+ builder:
354
+ dockerfile: Dockerfile.xyz
355
+
356
+ # Set context
357
+ builder:
358
+ context: ".."
359
+
360
+ # Set Dockerfile and context
361
+ builder:
362
+ dockerfile: "../Dockerfile.xyz"
363
+ context: ".."
364
+ ```
365
+
252
366
  ### Using build secrets for new images
253
367
 
254
- 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:
368
+ 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:
255
369
 
256
370
  ```yaml
257
371
  builder:
@@ -285,6 +399,15 @@ traefik:
285
399
 
286
400
  This will start the traefik container with `--accesslog=true accesslog.format=json`.
287
401
 
402
+ ### Traefik's host port binding
403
+
404
+ By default Traefik binds to port 80 of the host machine, it can be configured to use an alternative port:
405
+
406
+ ```yaml
407
+ traefik:
408
+ host_port: 8080
409
+ ```
410
+
288
411
  ### Configuring build args for new images
289
412
 
290
413
  Build arguments that aren't secret can also be configured:
@@ -298,7 +421,6 @@ builder:
298
421
  This build argument can then be used in the Dockerfile:
299
422
 
300
423
  ```
301
- # Private repositories need an access token during the build
302
424
  ARG RUBY_VERSION
303
425
  FROM ruby:$RUBY_VERSION-slim as base
304
426
  ```
@@ -345,23 +467,6 @@ servers:
345
467
 
346
468
  This assumes the Cron settings are stored in `config/crontab`.
347
469
 
348
- ### Using a generated .env file
349
-
350
- If you're using a centralized secret store, like 1Password, you can create `.env.erb` as a template which looks up the secrets. Example of a .env.erb file:
351
-
352
- ```erb
353
- <% if (session_token = `op signin --account my-one-password-account --raw`.strip) != "" %># Generated by mrsk envify
354
- GITHUB_TOKEN=<%= `gh config get -h github.com oauth_token`.strip %>
355
- MRSK_REGISTRY_PASSWORD=<%= `op read "op://Vault/Docker Hub/password" -n --session #{session_token}` %>
356
- RAILS_MASTER_KEY=<%= `op read "op://Vault/My App/RAILS_MASTER_SECRET" -n --session #{session_token}` %>
357
- MYSQL_ROOT_PASSWORD=<%= `op read "op://Vault/My App/MYSQL_ROOT_PASSWORD" -n --session #{session_token}` %>
358
- <% else raise ArgumentError, "Session token missing" end %>
359
- ```
360
-
361
- This template can safely be checked into git. Then everyone deploying the app can run `mrsk envify` when they setup the app for the first time or passwords change to get the correct `.env` file.
362
-
363
- If you need separate env variables for different destinations, you can set them with `.env.destination.erb` for the template, which will generate `.env.staging` when run with `mrsk envify -d staging`.
364
-
365
470
  ### Using audit broadcasts
366
471
 
367
472
  If you'd like to broadcast audits of deploys, rollbacks, etc to a chatroom or elsewhere, you can configure the `audit_broadcast_cmd` setting with the path to a bin file that will be passed the audit line as the first argument:
@@ -153,9 +153,7 @@ class Mrsk::Cli::Accessory < Mrsk::Cli::Base
153
153
  option :confirmed, aliases: "-y", type: :boolean, default: false, desc: "Proceed without confirmation question"
154
154
  def remove(name)
155
155
  if name == "all"
156
- if options[:confirmed] || ask("This will remove all containers and images for all accessories. Are you sure?", limited_to: %w( y N ), default: "N") == "y"
157
- MRSK.accessory_names.each { |accessory_name| remove(accessory_name) }
158
- end
156
+ MRSK.accessory_names.each { |accessory_name| remove(accessory_name) }
159
157
  else
160
158
  if options[:confirmed] || ask("This will remove all containers and images for #{name}. Are you sure?", limited_to: %w( y N ), default: "N") == "y"
161
159
  with_accessory(name) do
data/lib/mrsk/cli/app.rb CHANGED
@@ -3,7 +3,7 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
3
3
  def boot
4
4
  say "Get most recent version available as an image...", :magenta unless options[:version]
5
5
  using_version(options[:version] || most_recent_version_available) do |version|
6
- say "Start container with version #{version} (or reboot if already running)...", :magenta
6
+ say "Start container with version #{version} using a #{MRSK.config.readiness_delay}s readiness delay (or reboot if already running)...", :magenta
7
7
 
8
8
  cli = self
9
9
 
@@ -14,10 +14,7 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
14
14
  begin
15
15
  old_version = capture_with_info(*MRSK.app.current_running_version).strip
16
16
  execute *MRSK.app.run(role: role.name)
17
-
18
- cli.say "Waiting #{MRSK.config.readiness_delay}s for app to boot...", :magenta
19
17
  sleep MRSK.config.readiness_delay
20
-
21
18
  execute *MRSK.app.stop(version: old_version), raise_on_non_zero_exit: false if old_version.present?
22
19
 
23
20
  rescue SSHKit::Command::Failed => e
data/lib/mrsk/cli/main.rb CHANGED
@@ -11,7 +11,7 @@ class Mrsk::Cli::Main < Mrsk::Cli::Base
11
11
  desc "deploy", "Deploy app to servers"
12
12
  def deploy
13
13
  runtime = print_runtime do
14
- say "Ensure Docker is installed...", :magenta
14
+ say "Ensure curl and Docker are installed...", :magenta
15
15
  invoke "mrsk:cli:server:bootstrap"
16
16
 
17
17
  say "Log into image registry...", :magenta
@@ -55,7 +55,7 @@ class Mrsk::Cli::Main < Mrsk::Cli::Base
55
55
  MRSK.version = version
56
56
 
57
57
  if container_name_available?(MRSK.config.service_with_version)
58
- say "Start version #{version}, then stop the old version...", :magenta
58
+ say "Start version #{version}, then wait #{MRSK.config.readiness_delay}s for app to boot before stopping the old version...", :magenta
59
59
 
60
60
  cli = self
61
61
 
@@ -64,7 +64,6 @@ class Mrsk::Cli::Main < Mrsk::Cli::Base
64
64
 
65
65
  execute *MRSK.app.start
66
66
 
67
- cli.say "Waiting #{MRSK.config.readiness_delay}s for app to start...", :magenta
68
67
  sleep MRSK.config.readiness_delay
69
68
 
70
69
  execute *MRSK.app.stop(version: old_version), raise_on_non_zero_exit: false
@@ -143,7 +142,7 @@ class Mrsk::Cli::Main < Mrsk::Cli::Base
143
142
  desc "remove", "Remove Traefik, app, accessories, and registry session from servers"
144
143
  option :confirmed, aliases: "-y", type: :boolean, default: false, desc: "Proceed without confirmation question"
145
144
  def remove
146
- if options[:confirmed] || ask(remove_confirmation_question, limited_to: %w( y N ), default: "N") == "y"
145
+ if options[:confirmed] || ask("This will remove all containers and images. Are you sure?", limited_to: %w( y N ), default: "N") == "y"
147
146
  invoke "mrsk:cli:traefik:remove", [], options.without(:confirmed)
148
147
  invoke "mrsk:cli:app:remove", [], options.without(:confirmed)
149
148
  invoke "mrsk:cli:accessory:remove", [ "all" ], options
@@ -174,7 +173,7 @@ class Mrsk::Cli::Main < Mrsk::Cli::Base
174
173
  desc "registry", "Login and -out of the image registry"
175
174
  subcommand "registry", Mrsk::Cli::Registry
176
175
 
177
- desc "server", "Bootstrap servers with Docker"
176
+ desc "server", "Bootstrap servers with curl and Docker"
178
177
  subcommand "server", Mrsk::Cli::Server
179
178
 
180
179
  desc "traefik", "Manage Traefik load balancer"
@@ -186,10 +185,4 @@ class Mrsk::Cli::Main < Mrsk::Cli::Base
186
185
  on(host) { container_names = capture_with_info(*MRSK.app.list_container_names).split("\n") }
187
186
  Array(container_names).include?(container_name)
188
187
  end
189
-
190
- def remove_confirmation_question
191
- "This will remove all containers and images. " +
192
- (MRSK.config.accessories.any? ? "Including #{MRSK.config.accessories.collect(&:name).to_sentence}. " : "") +
193
- "Are you sure?"
194
- end
195
188
  end
@@ -1,6 +1,15 @@
1
1
  class Mrsk::Cli::Server < Mrsk::Cli::Base
2
- desc "bootstrap", "Ensure Docker is installed on servers"
2
+ desc "bootstrap", "Ensure curl and Docker are installed on servers"
3
3
  def bootstrap
4
- on(MRSK.hosts + MRSK.accessory_hosts) { execute "which docker || (apt-get update -y && apt-get install docker.io -y)" }
4
+ on(MRSK.hosts + MRSK.accessory_hosts) do
5
+ dependencies_to_install = Array.new.tap do |dependencies|
6
+ dependencies << "curl" unless execute "which curl", raise_on_non_zero_exit: false
7
+ dependencies << "docker.io" unless execute "which docker", raise_on_non_zero_exit: false
8
+ end
9
+
10
+ if dependencies_to_install.any?
11
+ execute "apt-get update -y && apt-get install #{dependencies_to_install.join(" ")} -y)"
12
+ end
13
+ end
5
14
  end
6
15
  end
@@ -25,7 +25,7 @@ class Mrsk::Commander
25
25
  end
26
26
 
27
27
  def primary_host
28
- specific_hosts&.sole || config.primary_web_host
28
+ specific_hosts&.first || config.primary_web_host
29
29
  end
30
30
 
31
31
  def hosts
@@ -10,6 +10,7 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
10
10
  *role.env_args,
11
11
  *config.volume_args,
12
12
  *role.label_args,
13
+ *role.option_args,
13
14
  config.absolute_image,
14
15
  role.cmd
15
16
  end
@@ -10,7 +10,11 @@ class Mrsk::Commands::Builder::Base < Mrsk::Commands::Base
10
10
  end
11
11
 
12
12
  def build_options
13
- [ *build_tags, *build_labels, *build_args, *build_secrets ]
13
+ [ *build_tags, *build_labels, *build_args, *build_secrets, *build_dockerfile ]
14
+ end
15
+
16
+ def build_context
17
+ context
14
18
  end
15
19
 
16
20
  private
@@ -30,6 +34,10 @@ class Mrsk::Commands::Builder::Base < Mrsk::Commands::Base
30
34
  argumentize "--secret", secrets.collect { |secret| [ "id", secret ] }
31
35
  end
32
36
 
37
+ def build_dockerfile
38
+ argumentize "--file", dockerfile
39
+ end
40
+
33
41
  def args
34
42
  (config.builder && config.builder["args"]) || {}
35
43
  end
@@ -37,4 +45,12 @@ class Mrsk::Commands::Builder::Base < Mrsk::Commands::Base
37
45
  def secrets
38
46
  (config.builder && config.builder["secrets"]) || []
39
47
  end
48
+
49
+ def dockerfile
50
+ (config.builder && config.builder["dockerfile"]) || "Dockerfile"
51
+ end
52
+
53
+ def context
54
+ (config.builder && config.builder["context"]) || "."
55
+ end
40
56
  end
@@ -13,7 +13,7 @@ class Mrsk::Commands::Builder::Multiarch < Mrsk::Commands::Builder::Base
13
13
  "--platform", "linux/amd64,linux/arm64",
14
14
  "--builder", builder_name,
15
15
  *build_options,
16
- "."
16
+ build_context
17
17
  end
18
18
 
19
19
  def info
@@ -17,7 +17,7 @@ class Mrsk::Commands::Builder::Native::Remote < Mrsk::Commands::Builder::Native
17
17
  "--platform", platform,
18
18
  "--builder", builder_name,
19
19
  *build_options,
20
- "."
20
+ build_context
21
21
  end
22
22
 
23
23
  def info
@@ -9,7 +9,7 @@ class Mrsk::Commands::Builder::Native < Mrsk::Commands::Builder::Base
9
9
 
10
10
  def push
11
11
  combine \
12
- docker(:build, *build_options, "."),
12
+ docker(:build, *build_options, build_context),
13
13
  docker(:push, config.absolute_image)
14
14
  end
15
15
 
@@ -2,7 +2,7 @@ class Mrsk::Commands::Registry < Mrsk::Commands::Base
2
2
  delegate :registry, to: :config
3
3
 
4
4
  def login
5
- docker :login, registry["server"], "-u", redact(registry["username"]), "-p", redact(lookup_password)
5
+ docker :login, registry["server"], "-u", redact(lookup("username")), "-p", redact(lookup("password"))
6
6
  end
7
7
 
8
8
  def logout
@@ -10,11 +10,11 @@ class Mrsk::Commands::Registry < Mrsk::Commands::Base
10
10
  end
11
11
 
12
12
  private
13
- def lookup_password
14
- if registry["password"].is_a?(Array)
15
- ENV.fetch(registry["password"].first).dup
13
+ def lookup(key)
14
+ if registry[key].is_a?(Array)
15
+ ENV.fetch(registry[key].first).dup
16
16
  else
17
- registry["password"]
17
+ registry[key]
18
18
  end
19
19
  end
20
20
  end
@@ -1,15 +1,19 @@
1
1
  class Mrsk::Commands::Traefik < Mrsk::Commands::Base
2
+ delegate :optionize, to: Mrsk::Utils
3
+
4
+ CONTAINER_PORT = 80
5
+
2
6
  def run
3
7
  docker :run, "--name traefik",
4
8
  "--detach",
5
9
  "--restart", "unless-stopped",
6
10
  "--log-opt", "max-size=#{MAX_LOG_SIZE}",
7
- "--publish", "80:80",
11
+ "--publish", port,
8
12
  "--volume", "/var/run/docker.sock:/var/run/docker.sock",
9
13
  "traefik",
10
14
  "--providers.docker",
11
15
  "--log.level=DEBUG",
12
- *cmd_args
16
+ *cmd_option_args
13
17
  end
14
18
 
15
19
  def start
@@ -45,8 +49,20 @@ class Mrsk::Commands::Traefik < Mrsk::Commands::Base
45
49
  docker :image, :prune, "--all", "--force", "--filter", "label=org.opencontainers.image.title=Traefik"
46
50
  end
47
51
 
52
+ def port
53
+ "#{host_port}:#{CONTAINER_PORT}"
54
+ end
55
+
48
56
  private
49
- def cmd_args
50
- (config.raw_config.dig(:traefik, "args") || { }).collect { |(key, value)| [ "--#{key}", value ] }.flatten
57
+ def cmd_option_args
58
+ if args = config.raw_config.dig(:traefik, "args")
59
+ optionize args
60
+ else
61
+ []
62
+ end
63
+ end
64
+
65
+ def host_port
66
+ config.raw_config.dig(:traefik, "host_port") || CONTAINER_PORT
51
67
  end
52
68
  end
@@ -1,5 +1,5 @@
1
1
  class Mrsk::Configuration::Role
2
- delegate :argumentize, :argumentize_env_with_secrets, to: Mrsk::Utils
2
+ delegate :argumentize, :argumentize_env_with_secrets, :optionize, to: Mrsk::Utils
3
3
 
4
4
  attr_accessor :name
5
5
 
@@ -35,6 +35,14 @@ class Mrsk::Configuration::Role
35
35
  specializations["cmd"]
36
36
  end
37
37
 
38
+ def option_args
39
+ if args = specializations["options"]
40
+ optionize args
41
+ else
42
+ []
43
+ end
44
+ end
45
+
38
46
  def running_traefik?
39
47
  name.web? || specializations["traefik"]
40
48
  end
data/lib/mrsk/utils.rb CHANGED
@@ -5,7 +5,7 @@ module Mrsk::Utils
5
5
  def argumentize(argument, attributes, redacted: false)
6
6
  Array(attributes).flat_map do |key, value|
7
7
  if value.present?
8
- escaped_pair = [ key, value.to_s.dump.gsub(/`/, '\\\\`') ].join("=")
8
+ escaped_pair = [ key, escape_shell_value(value) ].join("=")
9
9
  [ argument, redacted ? redact(escaped_pair) : escaped_pair ]
10
10
  else
11
11
  [ argument, key ]
@@ -23,8 +23,18 @@ module Mrsk::Utils
23
23
  end
24
24
  end
25
25
 
26
+ # Returns a list of shell-dashed option arguments. If the value is true, it's treated like a value-less option.
27
+ def optionize(args)
28
+ args.collect { |(key, value)| [ "--#{key}", value == true ? nil : escape_shell_value(value) ] }.flatten.compact
29
+ end
30
+
26
31
  # Copied from SSHKit::Backend::Abstract#redact to be available inside Commands classes
27
32
  def redact(arg) # Used in execute_command to hide redact() args a user passes in
28
33
  arg.to_s.extend(SSHKit::Redaction) # to_s due to our inability to extend Integer, etc
29
34
  end
35
+
36
+ # Escape a value to make it safe for shell use.
37
+ def escape_shell_value(value)
38
+ value.to_s.dump.gsub(/`/, '\\\\`')
39
+ end
30
40
  end
data/lib/mrsk/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Mrsk
2
- VERSION = "0.8.3"
2
+ VERSION = "0.9.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mrsk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.3
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-02-23 00:00:00.000000000 Z
11
+ date: 2023-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -80,6 +80,34 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '2.5'
83
+ - !ruby/object:Gem::Dependency
84
+ name: ed25519
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.2'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.2'
97
+ - !ruby/object:Gem::Dependency
98
+ name: bcrypt_pbkdf
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.0'
83
111
  description:
84
112
  email: dhh@hey.com
85
113
  executables: