Merge branch 'main' into fix-spelling-of-label

This commit is contained in:
David Heinemeier Hansson
2023-04-12 15:58:41 +02:00
committed by GitHub
3 changed files with 36 additions and 6 deletions

View File

@@ -331,6 +331,21 @@ servers:
my-label: "50" my-label: "50"
``` ```
### Using shell expansion
You can use shell expansion to interpolate values from the host machine into labels and env variables with the `${}` syntax.
Anything within the curly braces will be executed on the host machine and the result will be interpolated into the label or env variable.
```yaml
labels:
host-machine: "${cat /etc/hostname}"
env:
HOST_DEPLOYMENT_DIR: "${PWD}"
```
Note: Any other occurrence of `$` will be escaped to prevent unwanted shell expansion!
### Using container options ### Using container options
You can specialize the options used to start containers using the `options` definitions: You can specialize the options used to start containers using the `options` definitions:
@@ -521,11 +536,11 @@ Add labels to Traefik Docker container.
```yaml ```yaml
traefik: traefik:
labels: labels:
- traefik.enable: true traefik.enable: true
- traefik.http.routers.dashboard.rule: Host(`traefik.example.com`) && (PathPrefix(`/api`) || PathPrefix(`/dashboard`)) traefik.http.routers.dashboard.rule: Host(`traefik.example.com`) && (PathPrefix(`/api`) || PathPrefix(`/dashboard`))
- traefik.http.routers.dashboard.service: api@internal traefik.http.routers.dashboard.service: api@internal
- traefik.http.routers.dashboard.middlewares: auth traefik.http.routers.dashboard.middlewares: auth
- traefik.http.middlewares.auth.basicauth.users: test:$2y$05$H2o72tMaO.TwY1wNQUV1K.fhjRgLHRDWohFvUZOJHBEtUXNKrqUKi # test:password traefik.http.middlewares.auth.basicauth.users: test:$2y$05$H2o72tMaO.TwY1wNQUV1K.fhjRgLHRDWohFvUZOJHBEtUXNKrqUKi # test:password
``` ```
This labels Traefik container with `--label traefik.http.routers.dashboard.middlewares=\"auth\"` and so on. This labels Traefik container with `--label traefik.http.routers.dashboard.middlewares=\"auth\"` and so on.

View File

@@ -1,6 +1,8 @@
module Mrsk::Utils module Mrsk::Utils
extend self extend self
DOLLAR_SIGN_WITHOUT_SHELL_EXPANSION_REGEX = /\$(?!{[^\}]*\})/
# Return a list of escaped shell arguments using the same named argument against the passed attributes (hash or array). # Return a list of escaped shell arguments using the same named argument against the passed attributes (hash or array).
def argumentize(argument, attributes, sensitive: false) def argumentize(argument, attributes, sensitive: false)
Array(attributes).flat_map do |key, value| Array(attributes).flat_map do |key, value|
@@ -75,7 +77,9 @@ module Mrsk::Utils
# Escape a value to make it safe for shell use. # Escape a value to make it safe for shell use.
def escape_shell_value(value) def escape_shell_value(value)
value.to_s.dump.gsub(/`/, '\\\\`') value.to_s.dump
.gsub(/`/, '\\\\`')
.gsub(DOLLAR_SIGN_WITHOUT_SHELL_EXPANSION_REGEX, '\$')
end end
# Abbreviate a git revhash for concise display # Abbreviate a git revhash for concise display

View File

@@ -49,5 +49,16 @@ class UtilsTest < ActiveSupport::TestCase
test "escape_shell_value" do test "escape_shell_value" do
assert_equal "\"foo\"", Mrsk::Utils.escape_shell_value("foo") assert_equal "\"foo\"", Mrsk::Utils.escape_shell_value("foo")
assert_equal "\"\\`foo\\`\"", Mrsk::Utils.escape_shell_value("`foo`") assert_equal "\"\\`foo\\`\"", Mrsk::Utils.escape_shell_value("`foo`")
assert_equal "\"${PWD}\"", Mrsk::Utils.escape_shell_value("${PWD}")
assert_equal "\"${cat /etc/hostname}\"", Mrsk::Utils.escape_shell_value("${cat /etc/hostname}")
assert_equal "\"\\${PWD]\"", Mrsk::Utils.escape_shell_value("${PWD]")
assert_equal "\"\\$(PWD)\"", Mrsk::Utils.escape_shell_value("$(PWD)")
assert_equal "\"\\$PWD\"", Mrsk::Utils.escape_shell_value("$PWD")
assert_equal "\"^(https?://)www.example.com/(.*)\\$\"",
Mrsk::Utils.escape_shell_value("^(https?://)www.example.com/(.*)$")
assert_equal "\"https://example.com/\\$2\"",
Mrsk::Utils.escape_shell_value("https://example.com/$2")
end end
end end