mkit 0.10.6 → 0.11.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 (55) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +42 -0
  3. data/README.md +4 -1
  4. data/config/mkit_config.yml +3 -0
  5. data/docs/architecture.md +102 -0
  6. data/lib/mkit/app/controllers/pods_controller.rb +2 -2
  7. data/lib/mkit/app/controllers/services_controller.rb +4 -4
  8. data/lib/mkit/app/model/ingress.rb +1 -1
  9. data/lib/mkit/app/model/mkit_job.rb +0 -1
  10. data/lib/mkit/app/model/pod.rb +51 -36
  11. data/lib/mkit/app/model/resource.rb +7 -4
  12. data/lib/mkit/app/model/service.rb +51 -23
  13. data/lib/mkit/app/model/volume.rb +3 -3
  14. data/lib/mkit/app/templates/haproxy/app_haproxy.cfg.erb +1 -1
  15. data/lib/mkit/container/abstract.rb +16 -0
  16. data/lib/mkit/container/api.rb +22 -0
  17. data/lib/mkit/container/containers.rb +22 -0
  18. data/lib/mkit/container/docker/api.rb +25 -0
  19. data/lib/mkit/container/docker/containers.rb +178 -0
  20. data/lib/mkit/container/docker/events.rb +22 -0
  21. data/lib/mkit/container/docker/mapper.rb +54 -0
  22. data/lib/mkit/container/docker/networks.rb +37 -0
  23. data/lib/mkit/container/docker/payload.rb +41 -0
  24. data/lib/mkit/container/docker/stream_demux.rb +51 -0
  25. data/lib/mkit/container/docker/volumes.rb +29 -0
  26. data/lib/mkit/container/errors.rb +21 -0
  27. data/lib/mkit/container/event.rb +29 -0
  28. data/lib/mkit/container/events.rb +15 -0
  29. data/lib/mkit/container/http/unix_client.rb +139 -0
  30. data/lib/mkit/container/info.rb +41 -0
  31. data/lib/mkit/container/networks.rb +19 -0
  32. data/lib/mkit/container/registry.rb +37 -0
  33. data/lib/mkit/container/spec.rb +67 -0
  34. data/lib/mkit/container/utf8_buffer.rb +62 -0
  35. data/lib/mkit/container/volumes.rb +15 -0
  36. data/lib/mkit/ctypes.rb +0 -1
  37. data/lib/mkit/exceptions.rb +0 -2
  38. data/lib/mkit/pods/container_exec_command.rb +50 -0
  39. data/lib/mkit/pods/container_listener.rb +187 -0
  40. data/lib/mkit/pods/container_log_listener.rb +68 -0
  41. data/lib/mkit/pods/pod_reconciler.rb +48 -0
  42. data/lib/mkit/sagas/create_pod_saga.rb +22 -5
  43. data/lib/mkit/sagas/destroy_pod_saga.rb +6 -3
  44. data/lib/mkit/version.rb +1 -1
  45. data/lib/mkit/workers/haproxy_worker.rb +16 -10
  46. data/lib/mkit.rb +6 -4
  47. data/mkit.gemspec +2 -0
  48. metadata +59 -14
  49. data/lib/mkit/app/helpers/docker_helper.rb +0 -106
  50. data/lib/mkit/app/templates/docker/docker_run.sh.erb +0 -10
  51. data/lib/mkit/cmd/shell_client.rb +0 -57
  52. data/lib/mkit/cmd_runner.rb +0 -27
  53. data/lib/mkit/pods/docker_exec_command.rb +0 -38
  54. data/lib/mkit/pods/docker_listener.rb +0 -123
  55. data/lib/mkit/pods/docker_log_listener.rb +0 -40
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mkit/container/api'
4
+ require 'mkit/container/http/unix_client'
5
+ require 'mkit/container/docker/containers'
6
+ require 'mkit/container/docker/networks'
7
+ require 'mkit/container/docker/volumes'
8
+ require 'mkit/container/docker/events'
9
+
10
+ module MKIt
11
+ module Container
12
+ module Docker
13
+ DEFAULT_SOCKET = '/var/run/docker.sock'
14
+
15
+ # Container runtime backed by the Docker Engine API.
16
+ module Api
17
+ def self.build(socket: nil)
18
+ client = Http::UnixClient.new(socket: socket || DEFAULT_SOCKET)
19
+ Container::Api.new(containers: Containers.new(client), networks: Networks.new(client),
20
+ volumes: Volumes.new(client), events: Events.new(client))
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,178 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'shellwords'
5
+ require 'mkit/container/containers'
6
+ require 'mkit/container/errors'
7
+ require 'mkit/container/docker/payload'
8
+ require 'mkit/container/docker/mapper'
9
+ require 'mkit/container/docker/stream_demux'
10
+ require 'mkit/container/utf8_buffer'
11
+
12
+ module MKIt
13
+ module Container
14
+ module Docker
15
+ # Container operations through the Docker Engine API.
16
+ class Containers < Container::Containers
17
+ PULL_TIMEOUT = 600
18
+
19
+ def initialize(client)
20
+ super()
21
+ @client = client
22
+ end
23
+
24
+ # Equivalent of `docker run -d`.
25
+ def run(spec)
26
+ id = create(spec)
27
+ start(id)
28
+ id
29
+ end
30
+
31
+ def start(id)
32
+ @client.post("/containers/#{id}/start")
33
+ end
34
+
35
+ def stop(id)
36
+ @client.post("/containers/#{id}/stop")
37
+ end
38
+
39
+ def remove(id)
40
+ @client.delete("/containers/#{id}", force: true)
41
+ end
42
+
43
+ def find(id)
44
+ inspect = inspect_raw(id)
45
+ inspect.nil? ? nil : Mapper.info(inspect)
46
+ end
47
+
48
+ def exec(id, cmd)
49
+ exec_id = create_exec(id, exec_config(Shellwords.split(cmd)))
50
+ output = @client.post_raw("/exec/#{exec_id}/start", { 'Detach' => false, 'Tty' => true })
51
+ code = @client.get("/exec/#{exec_id}/json")['ExitCode']
52
+ raise ExecFailed, "exec '#{cmd[0..30]}' exited with #{code}" unless code.to_i.zero?
53
+
54
+ output.to_s
55
+ end
56
+
57
+ def logs(id, tail: 20)
58
+ body = @client.get_raw("/containers/#{id}/logs", log_query(tail: tail))
59
+ return '' if body.nil?
60
+
61
+ tty?(id) ? Container::Utf8Buffer.decode(body) : StreamDemux.decode(body)
62
+ end
63
+
64
+ def log_path(id)
65
+ inspect_raw(id)&.dig('LogPath')
66
+ end
67
+
68
+ def follow_logs(id, **query)
69
+ decoder = tty?(id) ? Container::Utf8Buffer.new : StreamDemux.new
70
+ @client.stream_chunks("/containers/#{id}/logs", log_query(**query)) do |chunk|
71
+ feed_logs(decoder, chunk) { |text| yield text }
72
+ end
73
+ flush_logs(decoder) { |text| yield text }
74
+ end
75
+
76
+ def attach_exec(id, cmd, detached: false)
77
+ exec_id = create_exec(id, exec_config(cmd, detached: detached, stdin: true))
78
+ @client.hijack("/exec/#{exec_id}/start", 'Detach' => detached, 'Tty' => !detached)
79
+ end
80
+
81
+ private
82
+
83
+ # tty streams are raw bytes, the others are multiplexed frames
84
+ def feed_logs(decoder, chunk)
85
+ if decoder.is_a?(StreamDemux)
86
+ decoder.feed(chunk) { |text| yield text }
87
+ else
88
+ text = decoder.feed(chunk)
89
+ yield text unless text.empty?
90
+ end
91
+ end
92
+
93
+ def flush_logs(decoder)
94
+ if decoder.is_a?(StreamDemux)
95
+ decoder.flush { |text| yield text }
96
+ else
97
+ text = decoder.flush
98
+ yield text unless text.empty?
99
+ end
100
+ end
101
+
102
+ def inspect_raw(id)
103
+ @client.get("/containers/#{id}/json")
104
+ end
105
+
106
+ def tty?(id)
107
+ inspect_raw(id)&.dig('Config', 'Tty') == true
108
+ end
109
+
110
+ def create(spec)
111
+ created = post_create(spec)
112
+ if created.nil? # 404: image not present locally, like `docker run` we pull it
113
+ pull_image(spec.image)
114
+ created = post_create(spec)
115
+ raise NotFound, "image #{spec.image} not found" if created.nil?
116
+ end
117
+ created['Id']
118
+ end
119
+
120
+ def post_create(spec)
121
+ @client.post('/containers/create', Payload.new(spec).to_h, name: spec.name)
122
+ end
123
+
124
+ # the daemon answers 404 when the container does not exist
125
+ def create_exec(id, config)
126
+ created = @client.post("/containers/#{id}/exec", config)
127
+ raise NotFound, "container #{id} not found" if created.nil?
128
+
129
+ created['Id']
130
+ end
131
+
132
+ # Pulls can take minutes and report failures (auth, rate limit) inside the progress stream.
133
+ def pull_image(image)
134
+ from, tag = split_image(image)
135
+ progress = @client.post_raw('/images/create', nil, { fromImage: from, tag: tag }.compact,
136
+ read_timeout: PULL_TIMEOUT)
137
+ raise_pull_error(image, progress)
138
+ end
139
+
140
+ def raise_pull_error(image, progress)
141
+ progress.to_s.each_line do |line|
142
+ message = pull_error(line)
143
+ raise Error, "could not pull #{image}: #{message}" if message
144
+ end
145
+ end
146
+
147
+ def pull_error(line)
148
+ JSON.parse(line)['error']
149
+ rescue JSON::ParserError
150
+ nil
151
+ end
152
+
153
+ # "repo:5000/img:1.0" => ["repo:5000/img", "1.0"]; digests stay in fromImage
154
+ def split_image(image)
155
+ return [image, nil] if image.include?('@')
156
+
157
+ name, _, tag = image.rpartition(':')
158
+ return [image, 'latest'] if name.empty? || tag.include?('/')
159
+
160
+ [name, tag]
161
+ end
162
+
163
+ def exec_config(cmd, detached: false, stdin: false)
164
+ attach = !detached
165
+ {
166
+ 'Cmd' => cmd, 'AttachStdin' => stdin && attach, 'AttachStdout' => attach,
167
+ 'AttachStderr' => attach, 'Tty' => attach, 'Detach' => detached
168
+ }
169
+ end
170
+
171
+ def log_query(tail: nil, follow: false, timestamps: false, since: nil, until_: nil, details: false)
172
+ { stdout: 1, stderr: 1, tail: tail, follow: follow ? 1 : nil, timestamps: timestamps ? 1 : nil,
173
+ since: since, until: until_, details: details ? 1 : nil }.compact
174
+ end
175
+ end
176
+ end
177
+ end
178
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mkit/container/events'
4
+ require 'mkit/container/docker/mapper'
5
+
6
+ module MKIt
7
+ module Container
8
+ module Docker
9
+ # https://docs.docker.com/engine/api/v1.43/#tag/System/operation/SystemEvents
10
+ class Events < Container::Events
11
+ def initialize(client)
12
+ super()
13
+ @client = client
14
+ end
15
+
16
+ def each(&block)
17
+ @client.stream('/events') { |payload| block.call(Mapper.event(payload)) }
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mkit/container/info'
4
+ require 'mkit/container/event'
5
+
6
+ module MKIt
7
+ module Container
8
+ module Docker
9
+ # Translates Docker Engine API payloads to the runtime neutral value objects.
10
+ module Mapper
11
+ module_function
12
+
13
+ # container inspect (GET /containers/{id}/json) => Info
14
+ def info(inspect)
15
+ Info.new(
16
+ id: inspect['Id'],
17
+ name: inspect['Name'].to_s.delete_prefix('/'),
18
+ running: inspect.dig('State', 'Running') == true,
19
+ tty: inspect.dig('Config', 'Tty') == true,
20
+ addresses: addresses(inspect),
21
+ exit_code: inspect.dig('State', 'ExitCode'),
22
+ oom_killed: inspect.dig('State', 'OOMKilled') == true,
23
+ error: inspect.dig('State', 'Error')
24
+ )
25
+ end
26
+
27
+ # event of GET /events => Event
28
+ def event(payload)
29
+ attributes = payload.dig('Actor', 'Attributes') || {}
30
+ kind = payload['Type'].to_s.to_sym
31
+ Event.new(
32
+ kind: kind,
33
+ action: payload['Action'].to_s.split(':').first.to_s.strip.to_sym,
34
+ container_id: container_id(kind, payload, attributes),
35
+ container_name: kind == :container ? attributes['name'] : nil,
36
+ exit_code: attributes['exitCode']&.to_i
37
+ )
38
+ end
39
+
40
+ def addresses(inspect)
41
+ (inspect.dig('NetworkSettings', 'Networks') || {}).transform_values { |net| net['IPAddress'] }
42
+ end
43
+
44
+ # network events carry the container in the attributes
45
+ def container_id(kind, payload, attributes)
46
+ case kind
47
+ when :container then payload['id'] || payload.dig('Actor', 'ID')
48
+ when :network then attributes['container']
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mkit/container/networks'
4
+
5
+ module MKIt
6
+ module Container
7
+ module Docker
8
+ # Network operations through the Docker Engine API.
9
+ class Networks < Container::Networks
10
+ def initialize(client)
11
+ super()
12
+ @client = client
13
+ end
14
+
15
+ def create(name)
16
+ @client.post('/networks/create', 'Name' => name)
17
+ end
18
+
19
+ def find(name)
20
+ @client.get("/networks/#{name}")
21
+ end
22
+
23
+ def attach(network, container)
24
+ @client.post("/networks/#{network}/connect", 'Container' => container)
25
+ end
26
+
27
+ def detach(network, container)
28
+ @client.post("/networks/#{network}/disconnect", 'Container' => container)
29
+ end
30
+
31
+ def remove(network)
32
+ @client.delete("/networks/#{network}")
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MKIt
4
+ module Container
5
+ module Docker
6
+ # Builds the Docker Engine API "create container" payload from a Container::Spec.
7
+ class Payload
8
+ MIB = 1024 * 1024
9
+
10
+ def initialize(spec)
11
+ @spec = spec
12
+ end
13
+
14
+ def to_h
15
+ payload = { 'Image' => @spec.image, 'Env' => @spec.env, 'HostConfig' => host_config }
16
+ cmd = @spec.command
17
+ payload['Cmd'] = cmd unless cmd.empty?
18
+ payload
19
+ end
20
+
21
+ private
22
+
23
+ def host_config
24
+ {
25
+ 'Binds' => @spec.binds,
26
+ 'NetworkMode' => @spec.network,
27
+ 'Dns' => [@spec.dns].compact
28
+ }.merge(limits)
29
+ end
30
+
31
+ def limits
32
+ {
33
+ 'NanoCpus' => @spec.cpu_millicores&.*(1_000_000),
34
+ 'Memory' => @spec.memory_mib&.*(MIB),
35
+ 'MemorySwap' => @spec.memory_swap_mib&.*(MIB)
36
+ }.compact
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mkit/container/utf8_buffer'
4
+
5
+ module MKIt
6
+ module Container
7
+ module Docker
8
+ # Decodes Docker's multiplexed stdout/stderr stream (containers without a
9
+ # TTY): frames of [type, 0, 0, 0, size(4 bytes big endian)] + payload.
10
+ # Characters split between frames of the same stream (stdout/stderr) are kept whole.
11
+ class StreamDemux
12
+ HEADER = 8
13
+
14
+ def initialize
15
+ @buffer = String.new(encoding: Encoding::BINARY)
16
+ @texts = Hash.new { |hash, type| hash[type] = Container::Utf8Buffer.new }
17
+ end
18
+
19
+ # Feeds a chunk, yields the text of each complete frame.
20
+ def feed(chunk)
21
+ @buffer << chunk.b
22
+ while @buffer.bytesize >= HEADER
23
+ size = @buffer.byteslice(4, 4).unpack1('N')
24
+ break if @buffer.bytesize < HEADER + size
25
+
26
+ type = @buffer.getbyte(0)
27
+ text = @texts[type].feed(@buffer.byteslice(HEADER, size))
28
+ @buffer = @buffer.byteslice(HEADER + size..)
29
+ yield text unless text.empty?
30
+ end
31
+ end
32
+
33
+ # End of the stream: yields what was held back
34
+ def flush
35
+ @texts.each_value do |text|
36
+ rest = text.flush
37
+ yield rest unless rest.empty?
38
+ end
39
+ end
40
+
41
+ def self.decode(data)
42
+ out = +''
43
+ demux = new
44
+ demux.feed(data) { |text| out << text }
45
+ demux.flush { |text| out << text }
46
+ out
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mkit/container/volumes'
4
+
5
+ module MKIt
6
+ module Container
7
+ module Docker
8
+ # Volume operations through the Docker Engine API.
9
+ class Volumes < Container::Volumes
10
+ def initialize(client)
11
+ super()
12
+ @client = client
13
+ end
14
+
15
+ def create(name)
16
+ @client.post('/volumes/create', 'Name' => name)
17
+ end
18
+
19
+ def find(name)
20
+ @client.get("/volumes/#{name}")
21
+ end
22
+
23
+ def remove(name)
24
+ @client.delete("/volumes/#{name}")
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MKIt
4
+ module Container
5
+ # Base error of the container runtime abstraction.
6
+ class Error < StandardError
7
+ attr_reader :status
8
+
9
+ def initialize(message = nil, status: nil)
10
+ super(message)
11
+ @status = status
12
+ end
13
+ end
14
+
15
+ # A referenced resource (image, container...) does not exist.
16
+ class NotFound < Error; end
17
+
18
+ # A command executed inside a container returned a non zero exit code.
19
+ class ExecFailed < Error; end
20
+ end
21
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MKIt
4
+ module Container
5
+ # Runtime neutral event of the container runtime.
6
+ #
7
+ # kind: :container, :network ... (what the event is about)
8
+ # action: :create, :start, :die, :connect ... (without arguments, e.g. exec_start: ls => :exec_start)
9
+ # container_id: container involved (nil when none)
10
+ # container_name: container name when the runtime reports it
11
+ # exit_code: exit code of the process (die events), nil otherwise
12
+ class Event
13
+ attr_reader :kind, :action, :container_id, :container_name, :exit_code
14
+
15
+ def initialize(kind:, action:, container_id: nil, container_name: nil, exit_code: nil)
16
+ @kind = kind
17
+ @action = action
18
+ @container_id = container_id
19
+ @container_name = container_name
20
+ @exit_code = exit_code
21
+ end
22
+
23
+ def to_s
24
+ text = "<#{kind}> <#{action}> container=#{container_id || '-'} name=#{container_name || '-'}"
25
+ exit_code.nil? ? text : "#{text} exit_code=#{exit_code}"
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mkit/container/abstract'
4
+
5
+ module MKIt
6
+ module Container
7
+ # Contract for the runtime event stream.
8
+ class Events
9
+ extend Abstract
10
+
11
+ # each { |event| }: blocks yielding Container::Event until the stream closes
12
+ abstract_methods :each
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,139 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'socket'
5
+ require 'uri'
6
+ require 'net_x/http_unix'
7
+ require 'mkit/container/errors'
8
+
9
+ module MKIt
10
+ module Container
11
+ module Http
12
+ # Minimal JSON/HTTP client over a unix socket, as spoken by container runtimes.
13
+ class UnixClient
14
+ DEFAULT_READ_TIMEOUT = 60
15
+
16
+ attr_reader :socket
17
+
18
+ def initialize(socket:)
19
+ @socket = socket
20
+ end
21
+
22
+ # Returns parsed JSON (or raw body when not JSON); nil when not found (404).
23
+ def get(path, query = {})
24
+ request(:get, path, query: query)
25
+ end
26
+
27
+ def post(path, body = nil, query = {})
28
+ request(:post, path, body: body, query: query)
29
+ end
30
+
31
+ def delete(path, query = {})
32
+ request(:delete, path, query: query)
33
+ end
34
+
35
+ # Raw (non-JSON) response body, nil when not found.
36
+ def get_raw(path, query = {})
37
+ raw_request(:get, path, query: query)
38
+ end
39
+
40
+ # POST whose response is not JSON (exec output, image pull progress), nil when not found.
41
+ # Long operations (pulls) need a bigger read_timeout.
42
+ def post_raw(path, body = nil, query = {}, read_timeout: DEFAULT_READ_TIMEOUT)
43
+ raw_request(:post, path, body: body, query: query, read_timeout: read_timeout)
44
+ end
45
+
46
+ # Yields raw chunks as they arrive. Blocks until the connection closes.
47
+ def stream_chunks(path, query = {}, &block)
48
+ http = connection(read_timeout: nil)
49
+ http.request(build(:get, path, query: query)) do |res|
50
+ raise_for(res) unless res.is_a?(Net::HTTPSuccess)
51
+ res.read_body(&block)
52
+ end
53
+ end
54
+
55
+ # Streams newline-delimited JSON objects (e.g. /events) to the block.
56
+ def stream(path, query = {})
57
+ buffer = +''
58
+ stream_chunks(path, query) do |chunk|
59
+ buffer << chunk
60
+ while (line = buffer.slice!(/\A.*\n/))
61
+ line = line.strip
62
+ yield JSON.parse(line) unless line.empty?
63
+ end
64
+ end
65
+ end
66
+
67
+ # POST that upgrades the connection to a raw bidirectional stream
68
+ # (exec start). Returns the connected socket; the caller must close it.
69
+ def hijack(path, body)
70
+ payload = body.to_json
71
+ sock = UNIXSocket.new(@socket)
72
+ sock.write("POST #{path} HTTP/1.1\r\nHost: localhost\r\nContent-Type: application/json\r\n" \
73
+ "Connection: Upgrade\r\nUpgrade: tcp\r\nContent-Length: #{payload.bytesize}\r\n\r\n#{payload}")
74
+ status = sock.gets.to_s
75
+ while (line = sock.gets) && line != "\r\n"; end
76
+ unless status =~ %r{\AHTTP/1\.\d (101|200)}
77
+ sock.close
78
+ raise Error.new("hijack failed: #{status.strip}", status: status.split[1].to_i)
79
+ end
80
+ sock
81
+ rescue Errno::ENOENT, Errno::ECONNREFUSED, Errno::EACCES => e
82
+ raise Error, "cannot reach socket #{@socket}: #{e.message}"
83
+ end
84
+
85
+ private
86
+
87
+ def connection(read_timeout: DEFAULT_READ_TIMEOUT)
88
+ http = NetX::HTTPUnix.new("unix://#{@socket}")
89
+ http.read_timeout = read_timeout
90
+ http
91
+ end
92
+
93
+ def build(method, path, body: nil, query: {})
94
+ uri = query.empty? ? path : "#{path}?#{URI.encode_www_form(query)}"
95
+ req = Net::HTTP.const_get(method.to_s.capitalize).new(uri)
96
+ unless body.nil?
97
+ req['Content-Type'] = 'application/json'
98
+ req.body = body.to_json
99
+ end
100
+ req
101
+ end
102
+
103
+ def request(method, path, body: nil, query: {})
104
+ parse(raw_request(method, path, body: body, query: query))
105
+ end
106
+
107
+ def raw_request(method, path, body: nil, query: {}, read_timeout: DEFAULT_READ_TIMEOUT)
108
+ res = connection(read_timeout: read_timeout).request(build(method, path, body: body, query: query))
109
+ return nil if res.is_a?(Net::HTTPNotFound)
110
+
111
+ raise_for(res) unless ok?(res)
112
+ res.body
113
+ rescue Errno::ENOENT, Errno::ECONNREFUSED, Errno::EACCES => e
114
+ raise Error, "cannot reach socket #{@socket}: #{e.message}"
115
+ rescue Net::ReadTimeout, Net::OpenTimeout => e
116
+ raise Error, "timeout talking to #{@socket} (#{method.to_s.upcase} #{path}): #{e.message}"
117
+ end
118
+
119
+ # 304: already started/stopped
120
+ def ok?(res)
121
+ res.is_a?(Net::HTTPSuccess) || res.is_a?(Net::HTTPNotModified)
122
+ end
123
+
124
+ def parse(body)
125
+ return nil if body.nil? || body.empty?
126
+
127
+ JSON.parse(body)
128
+ rescue JSON::ParserError
129
+ body
130
+ end
131
+
132
+ def raise_for(res)
133
+ msg = (JSON.parse(res.body)['message'] rescue res.body)
134
+ raise Error.new(msg, status: res.code.to_i)
135
+ end
136
+ end
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MKIt
4
+ module Container
5
+ # Runtime neutral view of an existing container.
6
+ class Info
7
+ attr_reader :id, :name, :addresses, :exit_code, :error
8
+
9
+ # addresses: { network name => ip }
10
+ # exit_code: of the last run (nil when it never ran); error: runtime message about a failed start/run
11
+ def initialize(id:, name:, running: false, tty: false, addresses: {}, exit_code: nil, oom_killed: false, error: nil)
12
+ @id = id
13
+ @name = name
14
+ @running = running
15
+ @tty = tty
16
+ @addresses = addresses
17
+ @exit_code = exit_code
18
+ @oom_killed = oom_killed
19
+ @error = error.to_s.empty? ? nil : error
20
+ end
21
+
22
+ def oom_killed?
23
+ @oom_killed
24
+ end
25
+
26
+ def running?
27
+ @running
28
+ end
29
+
30
+ def tty?
31
+ @tty
32
+ end
33
+
34
+ # ip of the container in the network, nil while none is assigned
35
+ def ip_on(network)
36
+ ip = @addresses[network]
37
+ ip.nil? || ip.empty? ? nil : ip
38
+ end
39
+ end
40
+ end
41
+ end