donce 0.4.0 → 0.6.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 (5) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +10 -8
  3. data/donce.gemspec +1 -1
  4. data/lib/donce.rb +23 -18
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e7ba4eef3f8cae9725b0919d93c200393d552c88688b8b448c9f2960a0dae2a9
4
- data.tar.gz: 3383251951e61584d0ced59f93a421f73f68da0ace2ca9eda40f0cc701e415fb
3
+ metadata.gz: 794fd321c85b3efd46c082e5555c69dfe4d97cd2eaed233b812f50dfb4453f47
4
+ data.tar.gz: b8f59039bc21dc5e585ebba73bf825fdcfb30d249f77879d0e50666c9b5514c5
5
5
  SHA512:
6
- metadata.gz: 071de606e7dde5336605819b229e83e3de1812109448fe647f95460e02a9129c16f2eacd240e06d796cfcbddedeb6d1cf8be7b6755fbfef00d2aa85127ec9b60
7
- data.tar.gz: 4b522e4e6fdfd8f211d18834211983b5c0b95e2433c697d7834f92a153d32942b5fa8a97c398026195247eb2b8599526af0e8788c722a1dab8936993bbd075e8
6
+ metadata.gz: babea47d7f7d8b6c13541102167cea6b88e9e48be2d2233535b417cfd0144946a04a710b64eebd4ecffa894e3c88320e1def531f33d2d406ca5f9cbb4f867e26
7
+ data.tar.gz: fc734444357b9b2e7a6cd4b460b5ccad6f6316fee65f182345e0b2143e1b7398fa96fc64eba08759744fe4a528ed17be48ae0c0e26b830a8c7a327dbf439c905
data/Gemfile.lock CHANGED
@@ -15,15 +15,17 @@ GEM
15
15
  builder (3.3.0)
16
16
  date (3.5.1)
17
17
  docile (1.4.1)
18
- elapsed (0.2.1)
18
+ elapsed (0.2.2)
19
19
  loog (~> 0.6)
20
20
  tago (~> 0.1)
21
+ ellipsized (0.3.0)
21
22
  erb (6.0.1)
22
23
  json (2.18.0)
23
24
  language_server-protocol (3.17.0.5)
24
25
  lint_roller (1.1.0)
25
26
  logger (1.7.0)
26
- loog (0.6.1)
27
+ loog (0.7.2)
28
+ ellipsized
27
29
  logger (~> 1.0)
28
30
  minitest (6.0.1)
29
31
  prism (~> 1.5)
@@ -32,18 +34,18 @@ GEM
32
34
  builder
33
35
  minitest (>= 5.0)
34
36
  ruby-progressbar
35
- minitest-retry (0.3.0)
37
+ minitest-retry (0.3.1)
36
38
  minitest (>= 5.0)
37
39
  os (1.1.4)
38
40
  parallel (1.27.0)
39
- parser (3.3.10.0)
41
+ parser (3.3.10.1)
40
42
  ast (~> 2.4.1)
41
43
  racc
42
- prism (1.7.0)
44
+ prism (1.8.0)
43
45
  psych (5.3.1)
44
46
  date
45
47
  stringio
46
- qbash (0.5.0)
48
+ qbash (0.7.1)
47
49
  backtrace (> 0)
48
50
  elapsed (> 0)
49
51
  loog (> 0)
@@ -51,7 +53,7 @@ GEM
51
53
  racc (1.8.1)
52
54
  rainbow (3.1.1)
53
55
  rake (13.3.1)
54
- rdoc (7.0.3)
56
+ rdoc (7.1.0)
55
57
  erb
56
58
  psych (>= 4.0.0)
57
59
  tsort
@@ -93,7 +95,7 @@ GEM
93
95
  simplecov-html (0.13.2)
94
96
  simplecov_json_formatter (0.1.4)
95
97
  stringio (3.2.0)
96
- tago (0.6.0)
98
+ tago (0.7.0)
97
99
  tsort (0.2.0)
98
100
  unicode-display_width (3.2.0)
99
101
  unicode-emoji (~> 4.1)
data/donce.gemspec CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
9
9
  s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version=
10
10
  s.required_ruby_version = '>=3.0'
11
11
  s.name = 'donce'
12
- s.version = '0.4.0'
12
+ s.version = '0.6.0'
13
13
  s.license = 'MIT'
14
14
  s.summary = 'Builds and starts temporary Docker containers'
15
15
  s.description =
data/lib/donce.rb CHANGED
@@ -57,7 +57,8 @@ module Kernel
57
57
  # (if array is provided, it will be concatenated)
58
58
  # @param [String] home The directory with Dockerfile and all other necessary files
59
59
  # @param [String] image The name of Docker image, e.g. "ubuntu:22.04"
60
- # @param [Logger] log The logging destination, can be +$stdout+
60
+ # @param [IO] stdout The stdout destination
61
+ # @param [IO] stderr The stderr destination
61
62
  # @param [String|Array<String>] args List of extra arguments for the +docker+ command
62
63
  # @param [Hash<String,String>] env Environment variables going into the container
63
64
  # @param [Hash<String,String>] volumes Local to container volumes mapping
@@ -70,8 +71,8 @@ module Kernel
70
71
  # @yield [String] Container ID if block is given (runs container in daemon mode)
71
72
  # @yieldparam [String] id The ID of the running container
72
73
  # @yieldreturn [void] The container will be stopped after the block execution
73
- def donce(dockerfile: nil, image: nil, home: nil, log: $stdout, args: '', env: {}, root: false, command: '',
74
- timeout: 60, volumes: {}, ports: {}, build_args: {})
74
+ def donce(dockerfile: nil, image: nil, home: nil, stdout: $stdout, stderr: $stdout, args: '', env: {}, root: false,
75
+ command: '', timeout: 60, volumes: {}, ports: {}, build_args: {})
75
76
  raise 'Either use "dockerfile" or "home"' if dockerfile && home
76
77
  raise 'Either use "dockerfile" or "image"' if dockerfile && image
77
78
  raise 'Either use "image" or "home"' if home && image
@@ -79,9 +80,11 @@ module Kernel
79
80
  raise 'The "timeout" must be an integer or nil' unless timeout.nil? || timeout.is_a?(Integer)
80
81
  raise 'The "volumes" is nil' if volumes.nil?
81
82
  raise 'The "volumes" must be a Hash' unless volumes.is_a?(Hash)
82
- raise 'The "log" is nil' if log.nil?
83
+ raise 'The "stdout" is nil' if stdout.nil?
84
+ raise 'The "stderr" is nil' if stderr.nil?
83
85
  raise 'The "args" is nil' if args.nil?
84
- raise 'The "args" must be a String' unless args.is_a?(String)
86
+ raise 'The "args" must be a String or Array' unless args.is_a?(String) || args.is_a?(Array)
87
+ args = args.join(' ') if args.is_a?(Array)
85
88
  raise 'The "env" is nil' if env.nil?
86
89
  raise 'The "env" must be a Hash' unless env.is_a?(Hash)
87
90
  raise 'The "command" is nil' if command.nil?
@@ -110,20 +113,21 @@ module Kernel
110
113
  Dir.mktmpdir do |tmp|
111
114
  dockerfile = dockerfile.join("\n") if dockerfile.is_a?(Array)
112
115
  File.write(File.join(tmp, 'Dockerfile'), dockerfile)
113
- qbash("#{docker} build #{a} #{Shellwords.escape(tmp)}", log:)
116
+ qbash("#{docker} build #{a} #{Shellwords.escape(tmp)}", stdout:, stderr:)
114
117
  end
115
118
  elsif home
116
- qbash("#{docker} build #{a} #{Shellwords.escape(home)}", log:)
119
+ qbash("#{docker} build #{a} #{Shellwords.escape(home)}", stdout:, stderr:)
117
120
  else
118
121
  raise 'Either "dockerfile" or "home" must be provided'
119
122
  end
120
123
  i
121
124
  end
122
125
  container = "donce-#{SecureRandom.hex(6)}"
123
- stdout = nil
126
+ out = nil
124
127
  code = 0
125
128
  cmd = [
126
- docker, 'run',
129
+ docker,
130
+ 'run',
127
131
  ('--detach' if block_given?),
128
132
  '--name', Shellwords.escape(container),
129
133
  ("--add-host #{donce_host}:host-gateway" unless OS.linux?),
@@ -137,36 +141,37 @@ module Kernel
137
141
  ].compact.join(' ')
138
142
  begin
139
143
  begin
140
- stdout, code =
144
+ out, code =
141
145
  Timeout.timeout(timeout) do
142
146
  qbash(
143
147
  cmd,
144
- log:,
148
+ stdout:,
149
+ stderr:,
145
150
  accept: nil,
146
151
  both: true,
147
152
  env:
148
153
  )
149
154
  end
150
155
  unless code.zero?
151
- log.error(stdout)
152
156
  raise \
153
157
  "Failed to run #{cmd} " \
154
- "(exit code is ##{code}, stdout has #{stdout.split("\n").count} lines)"
158
+ "(exit code is ##{code}, stdout has #{out.split("\n").count} lines)"
155
159
  end
156
160
  yield container if block_given?
157
161
  ensure
158
162
  logs = qbash(
159
163
  "#{docker} logs #{Shellwords.escape(container)}",
160
164
  level: code.zero? ? Logger::DEBUG : Logger::ERROR,
161
- log:
165
+ stdout:,
166
+ stderr:
162
167
  )
163
- stdout = logs if block_given?
164
- qbash("#{docker} rm --force #{Shellwords.escape(container)}", log:)
168
+ out = logs if block_given?
169
+ qbash("#{docker} rm --force #{Shellwords.escape(container)}", stdout:, stderr:)
165
170
  end
166
- stdout
171
+ out
167
172
  ensure
168
173
  Timeout.timeout(10) do
169
- qbash("#{docker} rmi #{img}", log:) unless image
174
+ qbash("#{docker} rmi #{img}", stdout:, stderr:) unless image
170
175
  end
171
176
  end
172
177
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: donce
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko