local-development-gateway 0.3.3 → 0.4.0

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 (36) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +26 -28
  3. data/docker-compose.yml +19 -20
  4. data/docs/README.md +16 -0
  5. data/docs/architecture/gateway-lifecycle.md +39 -0
  6. data/docs/guides/consumer-developer-examples.md +369 -0
  7. data/docs/guides/docker-first-migration.md +180 -0
  8. data/docs/guides/docker-installation.md +84 -0
  9. data/docs/requirements/effective-gateway.md +37 -0
  10. data/examples/shared-databases/README.md +145 -0
  11. data/examples/shared-databases/app/.omp-verifier.json +10 -0
  12. data/examples/shared-databases/app/.oxlintrc.json +3 -0
  13. data/examples/shared-databases/app/Dockerfile +12 -0
  14. data/examples/shared-databases/app/app.mjs +91 -0
  15. data/examples/shared-databases/app/app.test.mjs +67 -0
  16. data/examples/shared-databases/app/database.mjs +31 -0
  17. data/examples/shared-databases/app/migrations/001_notes.cjs +10 -0
  18. data/examples/shared-databases/app/package-lock.json +1705 -0
  19. data/examples/shared-databases/app/package.json +18 -0
  20. data/examples/shared-databases/bin/dev +63 -0
  21. data/examples/shared-databases/bin/provision +24 -0
  22. data/examples/shared-databases/compose.yml +73 -0
  23. data/examples/shared-databases/consumer.yml +64 -0
  24. data/examples/shared-databases/gateway.yml +24 -0
  25. data/examples/shared-databases/provisioning/checkout.sh +12 -0
  26. data/examples/shared-databases/provisioning/postgres/database.sql +16 -0
  27. data/examples/shared-databases/provisioning/postgres/provision.sh +10 -0
  28. data/examples/shared-databases/provisioning/postgres/role.sql +11 -0
  29. data/examples/shared-databases/provisioning/sqlserver/database.sql +34 -0
  30. data/examples/shared-databases/provisioning/sqlserver/login.sql +18 -0
  31. data/examples/shared-databases/provisioning/sqlserver/provision.sh +10 -0
  32. data/lib/local_development_gateway/lifecycle.rb +133 -0
  33. data/lib/local_development_gateway/version.rb +1 -1
  34. data/lib/local_development_gateway.rb +150 -27
  35. metadata +31 -3
  36. data/config/traefik.yml +0 -11
@@ -13,12 +13,12 @@ module LocalDevelopmentGateway
13
13
  OBSOLETE_SERVICE_NAMES = %w[dns tds-router].freeze
14
14
  ASSET_ROOT = File.expand_path("..", __dir__)
15
15
  COMPOSE_FILE = File.join(ASSET_ROOT, "docker-compose.yml")
16
- TRAEFIK_CONFIG_FILE = File.join(ASSET_ROOT, "config", "traefik.yml")
17
16
 
18
17
  class Error < StandardError
19
18
  end
20
19
 
21
20
  require "local_development_gateway/database_router"
21
+ require "local_development_gateway/lifecycle"
22
22
 
23
23
  class DockerError < Error
24
24
  attr_reader :command, :output
@@ -50,28 +50,26 @@ module LocalDevelopmentGateway
50
50
 
51
51
  class Client
52
52
  CONTAINER_FORMAT =
53
- "{{.ID}}\t{{.Label \"com.docker.compose.project\"}}\t{{.Label \"com.docker.compose.service\"}}\t{{.Label \"local-gateway\"}}"
53
+ "{{.ID}}\t{{.Label \"com.docker.compose.project\"}}\t{{.Label \"com.docker.compose.service\"}}\t{{.Label \"local-gateway\"}}\t{{.Label \"local-gateway.lifecycle\"}}"
54
54
 
55
55
  def initialize(
56
56
  runner: Docker.new,
57
57
  timeout: 30,
58
58
  poll_interval: 0.25,
59
59
  sleeper: ->(seconds) { sleep seconds },
60
- clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }
60
+ clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) },
61
+ lifecycle: nil
61
62
  )
62
63
  @runner = runner
63
64
  @timeout = timeout
64
65
  @poll_interval = poll_interval
65
66
  @sleeper = sleeper
66
67
  @clock = clock
68
+ @lifecycle = lifecycle || Lifecycle.new(runner: runner)
67
69
  end
68
70
 
69
71
  def ensure_running(*compose_args)
70
- return :reused if compose_args.empty? && ready?
71
-
72
- compose("up", "-d", "--remove-orphans", *compose_args)
73
- wait_until_ready
74
- :started
72
+ @lifecycle.synchronize { ensure_running_locked(*compose_args) }
75
73
  end
76
74
 
77
75
  alias start ensure_running
@@ -105,22 +103,44 @@ module LocalDevelopmentGateway
105
103
  end
106
104
 
107
105
  def stop_if_unused
108
- return :not_running unless gateway_exists?
109
- return :in_use if non_gateway_containers_attached?
106
+ @lifecycle.synchronize { |directory| stop_if_unused_locked(directory) }
107
+ end
110
108
 
111
- compose("down", "--remove-orphans")
112
- :stopped
109
+ def hold(name)
110
+ @lifecycle.validate_name(name)
111
+ @lifecycle.synchronize do |directory|
112
+ ensure_running_locked
113
+ @lifecycle.hold(directory, name)
114
+ end
115
+ end
116
+
117
+ def release(name)
118
+ @lifecycle.validate_name(name)
119
+ @lifecycle.synchronize do |directory|
120
+ @lifecycle.release(directory, name)
121
+ stop_if_unused_locked(directory)
122
+ end
113
123
  end
114
124
 
115
125
  def with_running(ensure_running: true)
126
+ operation = nil
116
127
  begin
117
- self.ensure_running if ensure_running
128
+ @lifecycle.synchronize do |directory|
129
+ operation = @lifecycle.register_operation(directory)
130
+ ensure_running_locked if ensure_running
131
+ end
118
132
  yield
119
133
  ensure
120
134
  block_error = $!
121
135
  begin
122
- stop_if_unused
136
+ if operation
137
+ @lifecycle.synchronize do |directory|
138
+ @lifecycle.release_operation(operation)
139
+ stop_if_unused_locked(directory)
140
+ end
141
+ end
123
142
  rescue StandardError => cleanup_error
143
+ operation.close if operation && !operation.closed?
124
144
  raise cleanup_error unless block_error
125
145
  end
126
146
  end
@@ -134,17 +154,45 @@ module LocalDevelopmentGateway
134
154
  "--file",
135
155
  COMPOSE_FILE,
136
156
  *args,
137
- capture: capture,
138
- env: {
139
- "LOCAL_DEVELOPMENT_GATEWAY_VERSION" => VERSION
140
- }
157
+ capture: capture
141
158
  )
142
159
  end
143
160
 
144
161
  private
145
162
 
163
+ def ensure_running_locked(*compose_args)
164
+ return :reused if compose_args.empty? && ready?
165
+
166
+ compose("up", "-d", "--remove-orphans", *compose_args)
167
+ wait_until_ready
168
+ :started
169
+ end
170
+
171
+ def stop_if_unused_locked(directory)
172
+ return :in_use if @lifecycle.retained?(directory)
173
+ return :not_running unless gateway_exists?
174
+ if non_gateway_containers_attached? || explicit_consumers_running?
175
+ return :in_use
176
+ end
177
+
178
+ compose("stop")
179
+ :stopped
180
+ end
181
+
182
+ def explicit_consumers_running?
183
+ output =
184
+ @runner.call(
185
+ "ps",
186
+ "--filter",
187
+ "label=local-gateway.lifecycle=consumer",
188
+ "--quiet"
189
+ )
190
+ !output.strip.empty?
191
+ end
192
+
146
193
  def service_version_status(service, id)
147
194
  container = JSON.parse(@runner.call("inspect", id)).fetch(0)
195
+
148
196
  if service == "database-router"
149
197
  version = router_version(id)
150
198
  provenance = "process-start record"
@@ -154,16 +202,27 @@ module LocalDevelopmentGateway
154
202
  provenance = "container-creation label"
155
203
  asset_target = "/etc/traefik/traefik.yml"
156
204
  end
205
+
157
206
  version = "unknown" if version.nil? || version.empty?
158
207
  comparison = version_comparison(version)
208
+
159
209
  mount =
160
210
  container
161
211
  .fetch("Mounts", [])
162
212
  .find { |entry| entry["Destination"] == asset_target }
163
- source = "unknown"
213
+ source = "none"
164
214
  source = mount.fetch("Source") if mount
165
- "#{service} (#{id[0, 12]}): active=#{version}; #{comparison}; " \
166
- "provenance=#{provenance}; mounted assets=#{source}\n"
215
+
216
+ image = container.dig("Config", "Image") || "unknown"
217
+ image_id = container["Image"] || "unknown"
218
+
219
+ <<~STATUS
220
+ #{service} (#{id[0, 12]}): active=#{version}; #{comparison}
221
+ provenance=#{provenance}
222
+ image=#{image}
223
+ image-id=#{image_id}
224
+ mounted assets=#{source}
225
+ STATUS
167
226
  end
168
227
 
169
228
  def router_version(id)
@@ -214,7 +273,7 @@ module LocalDevelopmentGateway
214
273
  def gateway_exists?
215
274
  network_exists? &&
216
275
  SERVICE_NAMES.any? do |service|
217
- !gateway_container_ids(service: service).empty?
276
+ !gateway_container_ids(service: service, status: "running").empty?
218
277
  end
219
278
  end
220
279
 
@@ -265,15 +324,23 @@ module LocalDevelopmentGateway
265
324
  CONTAINER_FORMAT
266
325
  )
267
326
  output.lines.filter_map do |line|
268
- id, project, service, label = line.strip.split("\t", -1)
327
+ id, project, service, label, lifecycle = line.chomp.split("\t", -1)
269
328
  next if id.nil? || id.empty?
270
329
 
271
- { id: id, project: project, service: service, label: label }
330
+ {
331
+ id: id,
332
+ project: project,
333
+ service: service,
334
+ label: label,
335
+ lifecycle: lifecycle
336
+ }
272
337
  end
273
338
  end
274
339
 
275
340
  def non_gateway_containers_attached?
276
- attached_containers.any? { |container| !gateway_container?(container) }
341
+ attached_containers.any? do |container|
342
+ !gateway_container?(container) && container[:lifecycle] != "passive"
343
+ end
277
344
  end
278
345
 
279
346
  def gateway_container?(container)
@@ -284,6 +351,15 @@ module LocalDevelopmentGateway
284
351
  end
285
352
 
286
353
  class CLI
354
+ class CommandFailure < StandardError
355
+ attr_reader :status
356
+
357
+ def initialize(status)
358
+ @status = status
359
+ super("Project command exited with status #{status}")
360
+ end
361
+ end
362
+
287
363
  USAGE = <<~USAGE
288
364
  Usage: local-development-gateway COMMAND [OPTIONS]
289
365
 
@@ -293,6 +369,10 @@ module LocalDevelopmentGateway
293
369
  ready Check gateway readiness
294
370
  logs Follow gateway logs
295
371
  down, stop Stop the gateway only when unused
372
+ with-running [--no-start] -- COMMAND
373
+ Run a project command with automatic gateway lifecycle
374
+ hold NAME Start the gateway and retain it for a desktop client
375
+ release NAME Remove a desktop hold and stop the gateway if unused
296
376
 
297
377
  Other commands are passed to Docker Compose using the packaged assets.
298
378
  USAGE
@@ -315,7 +395,7 @@ module LocalDevelopmentGateway
315
395
  end
316
396
 
317
397
  case command
318
- when "up", "ensure"
398
+ when "up", "ensure", "start"
319
399
  result = @client.ensure_running(*@argv)
320
400
  @output.write(@client.status) if result == :reused
321
401
  when "status"
@@ -329,10 +409,21 @@ module LocalDevelopmentGateway
329
409
  @client.logs(*@argv, follow: true)
330
410
  when "down", "stop"
331
411
  report_stop(@client.stop_if_unused)
412
+ when "hold", "release"
413
+ unless @argv.length == 1
414
+ raise Error, "#{command} requires exactly one NAME"
415
+ end
416
+
417
+ result = @client.public_send(command, @argv.first)
418
+ report_stop(result) if command == "release"
419
+ when "with-running"
420
+ return run_with_gateway
332
421
  else
333
422
  @output.write(@client.compose(command, *@argv))
334
423
  end
335
424
  0
425
+ rescue CommandFailure => error
426
+ error.status
336
427
  rescue Error => error
337
428
  @error.puts error.message
338
429
  1
@@ -340,6 +431,30 @@ module LocalDevelopmentGateway
340
431
 
341
432
  private
342
433
 
434
+ def run_with_gateway
435
+ start = @argv.first != "--no-start"
436
+ @argv.shift unless start
437
+ unless @argv.shift == "--" && !@argv.empty?
438
+ raise Error,
439
+ "Usage: local-development-gateway with-running [--no-start] -- COMMAND"
440
+ end
441
+
442
+ @client.with_running(ensure_running: start) do
443
+ status =
444
+ begin
445
+ system([@argv.first, @argv.first], *@argv.drop(1))
446
+ child = $?
447
+ child ? (child.exitstatus || (128 + child.termsig)) : 127
448
+ rescue SystemCallError => error
449
+ @error.puts error.message
450
+ 127
451
+ end
452
+ raise CommandFailure, status unless status.zero?
453
+
454
+ status
455
+ end
456
+ end
457
+
343
458
  def print_usage(status)
344
459
  @output.write(USAGE)
345
460
  status
@@ -349,7 +464,7 @@ module LocalDevelopmentGateway
349
464
  messages = {
350
465
  not_running: "Local Development Gateway is not running",
351
466
  in_use:
352
- "Leaving Local Development Gateway running because another container is attached",
467
+ "Leaving Local Development Gateway running because a consumer, pending operation or desktop hold needs it",
353
468
  stopped: "Stopped Local Development Gateway"
354
469
  }
355
470
  @output.puts messages.fetch(result)
@@ -384,5 +499,13 @@ module LocalDevelopmentGateway
384
499
  def stop_if_unused
385
500
  Client.new.stop_if_unused
386
501
  end
502
+
503
+ def hold(name)
504
+ Client.new.hold(name)
505
+ end
506
+
507
+ def release(name)
508
+ Client.new.release(name)
509
+ end
387
510
  end
388
511
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: local-development-gateway
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marlen Brunner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-11-16 00:00:00.000000000 Z
11
+ date: 2026-11-18 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Ruby API and CLI for starting, monitoring, and conditionally stopping
14
14
  the shared Docker gateway.
@@ -21,12 +21,39 @@ extra_rdoc_files: []
21
21
  files:
22
22
  - README.md
23
23
  - bin/local-development-gateway
24
- - config/traefik.yml
25
24
  - docker-compose.yml
25
+ - docs/README.md
26
+ - docs/architecture/gateway-lifecycle.md
26
27
  - docs/architecture/local-tcp-routing.md
28
+ - docs/guides/consumer-developer-examples.md
29
+ - docs/guides/docker-first-migration.md
30
+ - docs/guides/docker-installation.md
31
+ - docs/requirements/effective-gateway.md
27
32
  - examples/database-aliases/compose.rb
28
33
  - examples/database-aliases/compose.yml
29
34
  - examples/database-aliases/gateway.yml
35
+ - examples/shared-databases/README.md
36
+ - examples/shared-databases/app/.omp-verifier.json
37
+ - examples/shared-databases/app/.oxlintrc.json
38
+ - examples/shared-databases/app/Dockerfile
39
+ - examples/shared-databases/app/app.mjs
40
+ - examples/shared-databases/app/app.test.mjs
41
+ - examples/shared-databases/app/database.mjs
42
+ - examples/shared-databases/app/migrations/001_notes.cjs
43
+ - examples/shared-databases/app/package-lock.json
44
+ - examples/shared-databases/app/package.json
45
+ - examples/shared-databases/bin/dev
46
+ - examples/shared-databases/bin/provision
47
+ - examples/shared-databases/compose.yml
48
+ - examples/shared-databases/consumer.yml
49
+ - examples/shared-databases/gateway.yml
50
+ - examples/shared-databases/provisioning/checkout.sh
51
+ - examples/shared-databases/provisioning/postgres/database.sql
52
+ - examples/shared-databases/provisioning/postgres/provision.sh
53
+ - examples/shared-databases/provisioning/postgres/role.sql
54
+ - examples/shared-databases/provisioning/sqlserver/database.sql
55
+ - examples/shared-databases/provisioning/sqlserver/login.sql
56
+ - examples/shared-databases/provisioning/sqlserver/provision.sh
30
57
  - lib/local_development_gateway.rb
31
58
  - lib/local_development_gateway/database_alias.rb
32
59
  - lib/local_development_gateway/database_router.rb
@@ -41,6 +68,7 @@ files:
41
68
  - lib/local_development_gateway/database_router/tds/tls_byte_reader.rb
42
69
  - lib/local_development_gateway/database_router/tds/tls_client_hello.rb
43
70
  - lib/local_development_gateway/database_router/wire.rb
71
+ - lib/local_development_gateway/lifecycle.rb
44
72
  - lib/local_development_gateway/version.rb
45
73
  homepage: https://github.com/icefoganalytics/local-development-gateway
46
74
  licenses:
data/config/traefik.yml DELETED
@@ -1,11 +0,0 @@
1
- # Pinned with the gateway gem so every consumer uses the same routing contract.
2
- entryPoints:
3
- web:
4
- address: ":80"
5
-
6
- ping: {}
7
-
8
- providers:
9
- docker:
10
- exposedByDefault: false
11
- network: local-gateway