deployagram-environment 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5cd7800d620933008d8ca8bf63b70edb1c322a1828912044d5a6930c9412cc34
4
+ data.tar.gz: 402281edb8a40d5877d57b9792495980429abfa4c8e1f8245dbdadae7707d6bb
5
+ SHA512:
6
+ metadata.gz: ebdc9c907aee029b7ad01c8f22c009ec5b21871332dcec3518e4dae0470336a198b7a189909bdb7f3639a9f7a3b0109a15ce29cb010b304d5a85cf8ea804cd33
7
+ data.tar.gz: ad3be98ab37498ebc4bb1bf8ed6f3ab33f1516be760d3e0183c00bbeacec5be7c421931af8f6ae76f730d8d4e11730ec1b5bf70c870488b2fc59cdeca0155763
data/LICENSE ADDED
@@ -0,0 +1,14 @@
1
+ DEPLOYAGRAM PROPRIETARY LICENSE
2
+
3
+ Copyright (c) 2025 Deployagram, Cookiebit and Andy Muir.
4
+
5
+ Permission is hereby granted to use this software solely for its intended purpose.
6
+
7
+ RESTRICTIONS:
8
+ - No modification of the software is permitted
9
+ - No reverse engineering, decompilation, or disassembly
10
+ - No redistribution without explicit written permission
11
+ - Source code remains proprietary and confidential
12
+ - Usage is limited to the compiled npm package (the distributed tarball) artifact only
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
data/README.md ADDED
@@ -0,0 +1,9 @@
1
+ # deployagram-environment
2
+
3
+ Finds or starts the Deployagram Collector and HTTP Proxy with the Docker CLI.
4
+
5
+ A URL in DEPLOYAGRAM_LOGGER_URL or DEPLOYAGRAM_PROXY_URL is used first, then a running container, then a new one. A run stops only the containers it started.
6
+
7
+ Depends on: `deployagram-logger-client`, `deployagram-core`.
8
+
9
+ Part of the Deployagram Ruby packages. See `../../README.md`.
@@ -0,0 +1,74 @@
1
+ require "open3"
2
+ require "socket"
3
+ require "deployagram/errors"
4
+
5
+ module Deployagram
6
+ # Thin wrapper over the Docker CLI. Values passed in env go to Docker through the process environment,
7
+ # so secrets stay off the command line.
8
+ module Docker
9
+ NETWORK = "deployagram".freeze
10
+
11
+ module_function
12
+
13
+ # Runs docker and returns [output, status], with stderr merged into the output.
14
+ def capture(*args, env: {})
15
+ Open3.capture2e(env, "docker", *args)
16
+ end
17
+
18
+ # Runs docker and raises EnvironmentError if it fails.
19
+ def run!(*args, env: {})
20
+ out, status = capture(*args, env: env)
21
+ raise EnvironmentError, "docker #{args.first} failed: #{out}" unless status.success?
22
+
23
+ out
24
+ end
25
+
26
+ # Creates the shared network if it does not exist.
27
+ def ensure_network
28
+ _, status = capture("network", "inspect", NETWORK)
29
+ run!("network", "create", NETWORK) unless status.success?
30
+ end
31
+
32
+ # Starts a detached container and returns its id. Each name in env is passed with -e NAME, so the value
33
+ # comes from the process environment passed in env.
34
+ def run_detached(image, env:, network_alias:, publish: [], extra: [], name: nil)
35
+ args = ["run", "-d", "--network", NETWORK, "--network-alias", network_alias]
36
+ args += ["--name", name] if name
37
+ publish.each { |port| args += ["-p", port] }
38
+ args += extra
39
+ env.each_key { |key| args += ["-e", key] }
40
+ args << image
41
+ run!(*args, env: env).strip
42
+ end
43
+
44
+ # Stops and removes a container. Only call this for containers the caller started.
45
+ def remove(container_id)
46
+ capture("rm", "-f", container_id)
47
+ end
48
+
49
+ def published_port(container_id, private_port)
50
+ out, status = capture("port", container_id, private_port.to_s)
51
+ return nil unless status.success?
52
+
53
+ out[/:(\d+)\s*\z/, 1]&.to_i
54
+ end
55
+
56
+ def logs(container_id)
57
+ capture("logs", container_id).first
58
+ end
59
+
60
+ def stopped?(container_id)
61
+ out, status = capture("inspect", "-f", "{{.State.Running}}", container_id)
62
+ status.success? && out.strip == "false"
63
+ end
64
+
65
+ # A TCP port on this machine that is free now. Used when a container's advertised address must be known
66
+ # before it starts.
67
+ def free_port
68
+ server = TCPServer.new("127.0.0.1", 0)
69
+ port = server.addr[1]
70
+ server.close
71
+ port
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,253 @@
1
+ require "open3"
2
+ require "socket"
3
+ require "uri"
4
+ require "deployagram/errors"
5
+ require "deployagram/logger_client"
6
+ require "deployagram/proxy_config"
7
+
8
+ module Deployagram
9
+ # Finds or starts the Collector and the HTTP Proxy. For each service, in order:
10
+ # 1. a URL from the options or environment (DEPLOYAGRAM_LOGGER_URL, DEPLOYAGRAM_PROXY_URL), if set;
11
+ # 2. a running container with the right image;
12
+ # 3. a new container, started with the Docker CLI.
13
+ # Mirrors the Node @deployagram/environment package. A run stops only the containers it started.
14
+ class Environment
15
+ COLLECTOR_IMAGE =
16
+ "325701203566.dkr.ecr.eu-west-2.amazonaws.com/deployagram-docker:deployagram-collector-multi-latest".freeze
17
+ PROXY_IMAGE =
18
+ "325701203566.dkr.ecr.eu-west-2.amazonaws.com/deployagram-docker:http-diagramming-proxy-multi-latest".freeze
19
+ NETWORK = "deployagram".freeze
20
+ PRIVATE_PORT = 8080
21
+ READY_LINE = "Profile prod activated".freeze
22
+ READY_TIMEOUT_SECONDS = 60
23
+ LICENCE_FILE = ".deployagram".freeze
24
+ LOGGER_ALIAS = "diagram-logger".freeze
25
+
26
+ # Placeholder proxy settings, so the Proxy starts before a test posts its own configuration.
27
+ DEFAULT_PROXY_CONFIG = {
28
+ "proxy.namesOfSourceApps.dummy/dummy" => "Dummy",
29
+ "proxy.proxiedAppNames.dummy/dummy" => "Dummy",
30
+ "proxy.proxiedApps.dummy/dummy" => "http://dummy",
31
+ }.freeze
32
+
33
+ # A service the run can reach: its URL on this machine, and whether this run started it.
34
+ Service = Struct.new(:url, :container_id, :started, :source, keyword_init: true)
35
+
36
+ attr_reader :logger, :proxy
37
+
38
+ # Starts nothing if a Collector and Proxy are already running. Pass proxy: false for a run with no Proxy.
39
+ def self.start(env: ENV, cwd: Dir.pwd, proxy: true, proxy_config: nil, logger_environment: {})
40
+ new(env: env, cwd: cwd, proxy: proxy, proxy_config: proxy_config, logger_environment: logger_environment)
41
+ end
42
+
43
+ def initialize(env:, cwd:, proxy:, proxy_config:, logger_environment:)
44
+ @env = env
45
+ @cwd = cwd
46
+ @logger_environment = logger_environment
47
+ @started = []
48
+ @logger = resolve_logger
49
+ @proxy = proxy ? resolve_proxy(proxy_config) : nil
50
+ rescue StandardError
51
+ # A failed start must not leave the containers this run began behind.
52
+ stop
53
+ raise
54
+ end
55
+
56
+ # The Collector's client, pointed at whichever Collector this run uses.
57
+ def logger_client
58
+ LoggerClient.new(base_url: logger.url)
59
+ end
60
+
61
+ # Stops only the containers this run started, last started first.
62
+ def stop
63
+ @started.reverse_each { |container_id| docker_capture("rm", "-f", container_id) }
64
+ @started.clear
65
+ end
66
+
67
+ private
68
+
69
+ def resolve_logger
70
+ configured = @env["DEPLOYAGRAM_LOGGER_URL"]
71
+ return reachable_service(configured, "DEPLOYAGRAM_LOGGER_URL") if configured
72
+
73
+ running = find_running(COLLECTOR_IMAGE)
74
+ return running if running
75
+
76
+ start_logger
77
+ end
78
+
79
+ def resolve_proxy(proxy_config)
80
+ configured = @env["DEPLOYAGRAM_PROXY_URL"]
81
+ return reachable_service(configured, "DEPLOYAGRAM_PROXY_URL") if configured
82
+
83
+ running = find_running(PROXY_IMAGE)
84
+ return running if running
85
+
86
+ start_proxy(proxy_config || DEFAULT_PROXY_CONFIG)
87
+ end
88
+
89
+ def reachable_service(url, variable)
90
+ unless wait_until_reachable(url)
91
+ raise EnvironmentError, "#{variable} did not answer within #{READY_TIMEOUT_SECONDS}s: #{url}"
92
+ end
93
+
94
+ Service.new(url: url, container_id: nil, started: false, source: :url)
95
+ end
96
+
97
+ def find_running(image)
98
+ out, = docker_capture("ps", "--format", "{{.ID}} {{.Image}}")
99
+ out.each_line do |line|
100
+ id, running_image = line.split(" ", 2)
101
+ next unless running_image&.start_with?(image)
102
+
103
+ host_port = published_port(id)
104
+ return Service.new(url: "http://localhost:#{host_port}", container_id: id, started: false, source: :docker) if host_port
105
+ end
106
+ nil
107
+ end
108
+
109
+ def published_port(container_id)
110
+ out, status = docker_capture("port", container_id, PRIVATE_PORT.to_s)
111
+ return nil unless status.success?
112
+
113
+ out[/:(\d+)\s*\z/, 1]
114
+ end
115
+
116
+ def start_logger
117
+ ensure_network
118
+ environment = logger_environment
119
+ id = run_container(COLLECTOR_IMAGE, environment, alias_name: LOGGER_ALIAS)
120
+ url = "http://localhost:#{published_port(id)}"
121
+ Service.new(url: url, container_id: id, started: true, source: :started)
122
+ end
123
+
124
+ def start_proxy(proxy_config)
125
+ ensure_network
126
+ logger_host, logger_port = logger_address
127
+ environment = proxy_config.merge(
128
+ "DEPLOYAGRAM_LICENSE" => licence,
129
+ "DEPLOYAGRAM_LICENSE_KEY" => licence_key,
130
+ "diagramLoggerHost" => logger_host,
131
+ "diagramLoggerPort" => logger_port.to_s,
132
+ )
133
+ # The Quarkus key has quotes and dots in its name, so it is passed as name=value, not through the environment.
134
+ collector_url_setting = %Q{quarkus.rest-client."com.github.muirandy.proxy.SimpleLoggerClient".url=} \
135
+ "http://#{logger_host}:#{logger_port}"
136
+ extra = ["--add-host", "host.docker.internal:host-gateway", "-e", collector_url_setting]
137
+ id = run_container(PROXY_IMAGE, environment, alias_name: "http-proxy", extra: extra)
138
+ Service.new(url: "http://localhost:#{published_port(id)}", container_id: id, started: true, source: :started)
139
+ end
140
+
141
+ # The Proxy reaches the Collector through the Docker network when this run started it. A Collector
142
+ # found running (or set by URL) is reached through the host.
143
+ def logger_address
144
+ return [LOGGER_ALIAS, PRIVATE_PORT] if logger.started
145
+
146
+ if logger.source == :docker && logger.container_id
147
+ return [logger.container_id[0, 12], PRIVATE_PORT]
148
+ end
149
+
150
+ [ "host.docker.internal", URI(logger.url).port ]
151
+ end
152
+
153
+ def logger_environment
154
+ {
155
+ **@logger_environment,
156
+ "DEPLOYAGRAM_LICENSE" => licence,
157
+ "DEPLOYAGRAM_LICENSE_KEY" => licence_key,
158
+ "DEPLOYAGRAM_CLIENT_ID" => @env["DEPLOYAGRAM_CLIENT_ID"] || "",
159
+ "DEPLOYAGRAM_CLOUD_USERNAME" => @env["DEPLOYAGRAM_CLOUD_USERNAME"] || "",
160
+ "DEPLOYAGRAM_CLOUD_PASSWORD" => @env["DEPLOYAGRAM_CLOUD_PASSWORD"] || "",
161
+ }
162
+ end
163
+
164
+ # Runs a detached container. Values reach Docker through the process environment, with -e NAME,
165
+ # so secrets do not appear in the command line.
166
+ def run_container(image, environment, alias_name:, extra: [])
167
+ args = ["run", "-d", "--network", NETWORK, "--network-alias", alias_name, "-p", PRIVATE_PORT.to_s]
168
+ args += extra
169
+ environment.each_key { |name| args += ["-e", name] }
170
+ args << image
171
+
172
+ out, = docker_capture(*args, env: environment)
173
+ id = out.strip
174
+ @started << id
175
+ wait_until_ready(id)
176
+ id
177
+ end
178
+
179
+ def wait_until_ready(container_id)
180
+ deadline = Time.now + READY_TIMEOUT_SECONDS
181
+ until Time.now > deadline
182
+ out, = docker_capture("logs", container_id)
183
+ if container_stopped?(container_id)
184
+ raise_exited(container_id, out)
185
+ end
186
+
187
+ if out.include?(READY_LINE)
188
+ # The ready line is logged before the licence check, so a container with a rejected licence
189
+ # logs it and then exits. Wait a moment, and treat an exit as a failed start.
190
+ sleep 2
191
+ raise_exited(container_id, docker_capture("logs", container_id).first) if container_stopped?(container_id)
192
+ return
193
+ end
194
+
195
+ sleep 1
196
+ end
197
+ raise EnvironmentError, "Container #{container_id[0, 12]} did not become ready within #{READY_TIMEOUT_SECONDS}s"
198
+ end
199
+
200
+ def raise_exited(container_id, output)
201
+ # Licence responses are logged as JSON. Leave them out of the message, so they do not reach CI logs.
202
+ lines = output.lines.reject { |line| line.start_with?('{"data"') }
203
+ raise EnvironmentError,
204
+ "Container #{container_id[0, 12]} stopped. Its last output:\n#{lines.last(40).join}"
205
+ end
206
+
207
+ def container_stopped?(container_id)
208
+ out, status = docker_capture("inspect", "-f", "{{.State.Running}}", container_id)
209
+ status.success? && out.strip == "false"
210
+ end
211
+
212
+ def ensure_network
213
+ _, status = docker_capture("network", "inspect", NETWORK)
214
+ docker("network", "create", NETWORK) unless status.success?
215
+ end
216
+
217
+ def wait_until_reachable(url)
218
+ uri = URI(url)
219
+ deadline = Time.now + READY_TIMEOUT_SECONDS
220
+ until Time.now > deadline
221
+ begin
222
+ Socket.tcp(uri.host, uri.port, connect_timeout: 1) { return true }
223
+ rescue SystemCallError, SocketError, IO::TimeoutError
224
+ sleep 1
225
+ end
226
+ end
227
+ false
228
+ end
229
+
230
+ def licence
231
+ @env["DEPLOYAGRAM_LICENSE"] || begin
232
+ file = File.join(@cwd, LICENCE_FILE)
233
+ File.exist?(file) ? File.read(file) : raise(EnvironmentError,
234
+ "No Deployagram licence found. Set DEPLOYAGRAM_LICENSE or create #{LICENCE_FILE} in #{@cwd}.")
235
+ end
236
+ end
237
+
238
+ def licence_key
239
+ @env["DEPLOYAGRAM_LICENSE_KEY"] || raise(EnvironmentError, "DEPLOYAGRAM_LICENSE_KEY is not set.")
240
+ end
241
+
242
+ def docker(*args, env: {})
243
+ out, status = docker_capture(*args, env: env)
244
+ raise EnvironmentError, "docker #{args.first} failed: #{out}" unless status.success?
245
+
246
+ out
247
+ end
248
+
249
+ def docker_capture(*args, env: {})
250
+ Open3.capture2e(env, "docker", *args)
251
+ end
252
+ end
253
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: deployagram-environment
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Deployagram
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: deployagram-logger-client
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ -
17
+ - "="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ -
25
+ - "="
26
+ - !ruby/object:Gem::Version
27
+ version: 0.1.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: deployagram-core
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ -
33
+ - "="
34
+ - !ruby/object:Gem::Version
35
+ version: 0.1.0
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ -
41
+ - "="
42
+ - !ruby/object:Gem::Version
43
+ version: 0.1.0
44
+ description: "A URL in DEPLOYAGRAM_LOGGER_URL or DEPLOYAGRAM_PROXY_URL is used first, then a running container, then a new one. A run stops only the containers it started."
45
+ email:
46
+ - andy@deployagram.com
47
+ executables: []
48
+ extensions: []
49
+ extra_rdoc_files: []
50
+ files:
51
+ - LICENSE
52
+ - README.md
53
+ - lib/deployagram/docker.rb
54
+ - lib/deployagram/environment.rb
55
+ licenses:
56
+ - SEE LICENSE IN LICENSE
57
+ metadata:
58
+ rubygems_mfa_required: "true"
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ -
65
+ - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "3.2"
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ -
71
+ - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: "0"
74
+ requirements: []
75
+ rubygems_version: 4.1.0
76
+ specification_version: 4
77
+ summary: Finds or starts the Deployagram Collector and HTTP Proxy with the Docker CLI
78
+ test_files: []