dry-stack 0.1.63 → 0.1.64

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/dry-stack/stack.rb +66 -18
  3. data/lib/version.rb +1 -1
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ce9d6102c6ff6cb17fedabdde36397e51449af498dd69e939b978f3dc70fa2cf
4
- data.tar.gz: 0667c093ab72a2c2c09ac7975ea16b0f43971d7e8bf5cf11e5556af8a107400c
3
+ metadata.gz: 95ad6d62ec5d85d07851461288f12ed5ce8bbebaf328df21aeec840e4afd6e79
4
+ data.tar.gz: 29d03966adbc9b8b8364440fe32922d9b62fdd95f11c54badf5d43f3d370a2ab
5
5
  SHA512:
6
- metadata.gz: f201a90b5f82f24fd95ac74cd2e1c0d8b4674abb7de6fcb90ca9b1424dc1ba613d916bbc07c0bf17ecef03de762d39536e89a04d51443674f2576f3aa95ef795
7
- data.tar.gz: 3b398ac9953210735570f4720da66e3e4dedd45c0e1e7924f45a683539dfeaabdb693002c209838a6155eef9325c93bf6175c4f944da69db269a3bc9ac3a0b0a
6
+ metadata.gz: 61044ae2310b923a97b773e4e08ef9091dbfeed006175a077e2d0b5b0650d2d09db6c4679cc73843264b6328c177c6d7f27f5082ceaa87ef18466e40a03d8b5e
7
+ data.tar.gz: 5a21c7f7d3f65137286ab1c1cbe1a03ebccdcc281e12231c46354da2ba880b34c8cb1c1ec1ef0323d8482f30c1e57d4e37a866fba398ee5725d52e74aef57a05
@@ -149,15 +149,43 @@ module Dry
149
149
  conflicts = %i[path path_sub path_regexp host_sni] & ing.keys
150
150
  raise ArgumentError, "ingress mount cannot be combined with #{conflicts.join(', ')}" unless conflicts.empty?
151
151
 
152
- mount = ing[:mount]
153
- unless mount.is_a?(String) && mount.match?(%r{\A/[A-Za-z0-9._~!$&'()*+;=:@/-]+\z})
154
- raise ArgumentError, 'ingress mount requires a literal absolute URL path without encoding, query, fragment or comma'
152
+ ingress_path(ing[:mount], :mount)
153
+ end
154
+
155
+ private def ingress_path(value, option, root: false)
156
+ return '' if root && value == '/'
157
+
158
+ unless value.is_a?(String) && value.match?(%r{\A/[A-Za-z0-9._~!$&'()*+;=:@/-]+\z})
159
+ raise ArgumentError, "ingress #{option} requires a literal absolute URL path without encoding, query, fragment or comma"
160
+ end
161
+ path = value.delete_suffix('/')
162
+ if path.empty? || path.split('/', -1).drop(1).any? { _1.empty? || %w[. ..].include?(_1) }
163
+ raise ArgumentError, "ingress #{option} must have nonempty path segments other than . or .."
164
+ end
165
+ path
166
+ end
167
+
168
+ private def ingress_http_options(ing, mount)
169
+ %i[target_prefix redirect_root].each do |option|
170
+ next unless ing.key?(option)
171
+
172
+ conflicts = %i[path path_sub path_regexp host_sni] & ing.keys
173
+ raise ArgumentError, "ingress #{option} cannot be combined with #{conflicts.join(', ')}" unless conflicts.empty?
174
+ end
175
+ target = ingress_path(ing[:target_prefix], :target_prefix, root: true) if ing.key?(:target_prefix)
176
+ if ing.key?(:redirect_root)
177
+ redirect = ingress_path(ing[:redirect_root], :redirect_root, root: true)
178
+ raise ArgumentError, 'ingress redirect_root must differ from the public route root' if redirect == mount.to_s
155
179
  end
156
- mount = mount.delete_suffix('/')
157
- if mount.empty? || mount.split('/', -1).drop(1).any? { _1.empty? || %w[. ..].include?(_1) }
158
- raise ArgumentError, 'ingress mount must have nonempty path segments other than . or ..'
180
+ if ing.key?(:service)
181
+ backend = ing[:service]
182
+ unless backend.is_a?(String) && backend.match?(/\A[A-Za-z0-9_.-]+(?:@[A-Za-z0-9_.-]+)?\z/)
183
+ raise ArgumentError, 'ingress service requires a Traefik service name, optionally qualified with @provider'
184
+ end
185
+ conflicts = %i[port url host_sni] & ing.keys
186
+ raise ArgumentError, "ingress service cannot be combined with #{conflicts.join(', ')}" unless conflicts.empty?
159
187
  end
160
- mount
188
+ [target, ing[:redirect_root], backend]
161
189
  end
162
190
 
163
191
  def to_compose(opts = @options)
@@ -212,14 +240,13 @@ module Dry
212
240
 
213
241
  if ingress[0] && (opts[:traefik] || opts[:traefik_tls])
214
242
  service[:deploy][:labels] << 'traefik.enable=true'
215
- ingress[0][:port] ||= service[:ports]&.first
216
-
217
243
  ingress.each_with_index do |ing, index|
218
244
  mount = ingress_mount(ing)
219
- ing[:port] ||= service[:ports]&.first
245
+ target, redirect, backend = ingress_http_options(ing, mount)
246
+ port = backend ? nil : (ing[:port] || service[:ports]&.first)
220
247
  load_balancer = []
221
248
  load_balancer << "server.url=#{ing[:url]}" if ing[:url]
222
- load_balancer << "server.port=#{ing[:port]}" if ing[:port]
249
+ load_balancer << "server.port=#{port}" if port
223
250
 
224
251
  if ing[:host_sni]
225
252
  domain = opts[:tls_domain] || 'example.com'
@@ -251,7 +278,7 @@ module Dry
251
278
 
252
279
  else
253
280
  service[:deploy][:labels] += [
254
- "traefik.http.routers.#{service_name}-#{index}.service=#{service_name}-#{index}",
281
+ "traefik.http.routers.#{service_name}-#{index}.service=#{backend || "#{service_name}-#{index}"}",
255
282
  ]
256
283
  service[:deploy][:labels] += load_balancer.map { "traefik.http.services.#{service_name}-#{index}.loadbalancer.#{_1}" }
257
284
 
@@ -304,18 +331,32 @@ module Dry
304
331
  service[:deploy][:labels] << "traefik.http.routers.#{service_name}-#{index}.rule=#{rule.join ' && '}"
305
332
  service[:deploy][:labels] << "traefik.http.routers.#{service_name}-#{index}.priority=#{ing[:priority].to_i}" if ing[:priority]
306
333
 
307
- if mount
334
+ if mount || redirect
308
335
  prefix = "#{service_name}-#{index}-mount"
309
- regex = "^(https?://[^/]+#{Regexp.escape(mount)})(\\?.*)?$"
310
- middlewares += ["#{prefix}-redirect", "#{prefix}-strip"]
336
+ regex = "^(https?://[^/]+#{Regexp.escape(mount.to_s)})(\\?.*)?$"
337
+ replacement = '${1}/${2}'
338
+ if redirect
339
+ root = mount ? "#{Regexp.escape(mount)}/?" : '/'
340
+ regex = "^(https?://[^/]+)#{root}(\\?.*)?$"
341
+ replacement = "${1}#{redirect.gsub('$', '$$')}${2}"
342
+ end
343
+ middlewares << "#{prefix}-redirect"
311
344
  service[:deploy][:labels] += [
312
345
  "traefik.http.middlewares.#{prefix}-redirect.redirectregex.regex=#{regex.gsub('$', '$$')}",
313
- "traefik.http.middlewares.#{prefix}-redirect.redirectregex.replacement=$${1}/$${2}",
314
- "traefik.http.middlewares.#{prefix}-redirect.redirectregex.permanent=false",
315
- "traefik.http.middlewares.#{prefix}-strip.stripprefix.prefixes=#{mount.gsub('$', '$$')}"
346
+ "traefik.http.middlewares.#{prefix}-redirect.redirectregex.replacement=#{replacement.gsub('$', '$$')}",
347
+ "traefik.http.middlewares.#{prefix}-redirect.redirectregex.permanent=false"
316
348
  ]
317
349
  end
318
350
 
351
+ if mount && mount != target
352
+ middlewares << "#{service_name}-#{index}-mount-strip"
353
+ service[:deploy][:labels] << "traefik.http.middlewares.#{service_name}-#{index}-mount-strip.stripprefix.prefixes=#{mount.gsub('$', '$$')}"
354
+ end
355
+ if target && !target.empty? && target != mount
356
+ middlewares << "#{service_name}-#{index}-target"
357
+ service[:deploy][:labels] << "traefik.http.middlewares.#{service_name}-#{index}-target.addprefix.prefix=#{target.gsub('$', '$$')}"
358
+ end
359
+
319
360
  if ing[:path_sub]
320
361
  middlewares << "#{service_name}-#{index}-path_sub"
321
362
  service[:deploy][:labels] += [
@@ -327,6 +368,13 @@ module Dry
327
368
  service[:deploy][:labels] << "traefik.http.routers.#{service_name}-#{index}.middlewares=#{middlewares.join ","}" unless middlewares.empty?
328
369
  end
329
370
  end
371
+
372
+ http_ingress = ingress.reject { _1[:host_sni] }
373
+ if http_ingress.any? && http_ingress.all? { _1[:service] } &&
374
+ service[:deploy][:labels].none? { _1.start_with?('traefik.http.services.') }
375
+ # Swarm requires a port even when every HTTP router uses an existing service.
376
+ service[:deploy][:labels] << "traefik.http.services.#{service_name}-unused.loadbalancer.server.port=9999"
377
+ end
330
378
  end
331
379
  service.delete :ingress
332
380
  service.delete :oauth_provider
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Dry
2
2
  class Stack
3
- VERSION = '0.1.63'
3
+ VERSION = '0.1.64'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dry-stack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.63
4
+ version: 0.1.64
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artyom B
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-11-15 00:00:00.000000000 Z
11
+ date: 2026-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake