donce 0.5.0 → 0.7.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 (6) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +4 -4
  3. data/README.md +1 -1
  4. data/donce.gemspec +1 -1
  5. data/lib/donce.rb +23 -19
  6. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '03161908e407496425db2581fce2d802d834fce0e06cc97daf13de1c2edecc21'
4
- data.tar.gz: 7339a08b6820ec2aa9e0bda4067e3fd8947b387509ea96f9ac4e9971399ac24e
3
+ metadata.gz: 5d852871b216457b9f3f4490f2d99437342c1cacfb984ea02db4d3b8ad15c6b2
4
+ data.tar.gz: 64a0b65f60ba9c57a22e96c51f2fcfb7061b9ec2258423d83c6a5dd5939274eb
5
5
  SHA512:
6
- metadata.gz: 81203a7306ab4edd80ac3ce16a27bcacb8505bb73bd99c3d0ad629193cdd98222b7e954382d671a76e9a3f8f2a461914f5a18c2f7845955dc31242b3b8796c09
7
- data.tar.gz: f4978002f8c14db6783e5480f0f50e064e6c6883c6c1de61d4728022fa522bdc98be1cba1449ad88ec2db8eb08c2a2f65f18691eef9a673f71ffbaf8157e322f
6
+ metadata.gz: 7a467ad471d9acc92d0379cc84c24f168e90ba5f85c2ab9255df51f3dec36f3cc7978e186484f484e7acaf45a80a05bf88997083d97ea390c7c5cef3b9767ea0
7
+ data.tar.gz: 7cc78fd133ccd9ce2da05f5a130eb802b274c0ab38cc94fc8d3e3218adc3c47a7520e8a3d612d6177f3c964653609cf147d2b1e127b76f7c90eced7a925e6f0e
data/Gemfile.lock CHANGED
@@ -41,11 +41,11 @@ GEM
41
41
  parser (3.3.10.1)
42
42
  ast (~> 2.4.1)
43
43
  racc
44
- prism (1.8.0)
44
+ prism (1.9.0)
45
45
  psych (5.3.1)
46
46
  date
47
47
  stringio
48
- qbash (0.7.1)
48
+ qbash (0.7.2)
49
49
  backtrace (> 0)
50
50
  elapsed (> 0)
51
51
  loog (> 0)
@@ -59,7 +59,7 @@ GEM
59
59
  tsort
60
60
  regexp_parser (2.11.3)
61
61
  rexml (3.4.4)
62
- rubocop (1.82.1)
62
+ rubocop (1.84.1)
63
63
  json (~> 2.3)
64
64
  language_server-protocol (~> 3.17.0.2)
65
65
  lint_roller (~> 1.1.0)
@@ -67,7 +67,7 @@ GEM
67
67
  parser (>= 3.3.0.2)
68
68
  rainbow (>= 2.2.2, < 4.0)
69
69
  regexp_parser (>= 2.9.3, < 3.0)
70
- rubocop-ast (>= 1.48.0, < 2.0)
70
+ rubocop-ast (>= 1.49.0, < 2.0)
71
71
  ruby-progressbar (~> 1.7)
72
72
  unicode-display_width (>= 2.4.0, < 4.0)
73
73
  rubocop-ast (1.49.0)
data/README.md CHANGED
@@ -44,7 +44,7 @@ If you set `DONCE_SUDO` environment variable to `true`,
44
44
  Host group/user IDs are passed to the build, as `GID` and `UID` args.
45
45
  You can use them, when you copy resources into the image, for example:
46
46
 
47
- ```
47
+ ```dockerfile
48
48
  FROM ubuntu
49
49
  ARG UID
50
50
  ARG GID
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.5.0'
12
+ s.version = '0.7.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
@@ -48,7 +48,7 @@ module Kernel
48
48
  # The name of the localhost inside Docker container.
49
49
  # @return [String] The hostname
50
50
  def donce_host
51
- OS.linux? ? '172.17.0.1' : 'host.docker.internal'
51
+ 'host.docker.internal'
52
52
  end
53
53
 
54
54
  # Build Docker image (or use existing one), run Docker container, and then clean up.
@@ -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,7 +80,8 @@ 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
86
  raise 'The "args" must be a String or Array' unless args.is_a?(String) || args.is_a?(Array)
85
87
  args = args.join(' ') if args.is_a?(Array)
@@ -111,23 +113,24 @@ module Kernel
111
113
  Dir.mktmpdir do |tmp|
112
114
  dockerfile = dockerfile.join("\n") if dockerfile.is_a?(Array)
113
115
  File.write(File.join(tmp, 'Dockerfile'), dockerfile)
114
- qbash("#{docker} build #{a} #{Shellwords.escape(tmp)}", stdout: log)
116
+ qbash("#{docker} build #{a} #{Shellwords.escape(tmp)}", stdout:, stderr:)
115
117
  end
116
118
  elsif home
117
- qbash("#{docker} build #{a} #{Shellwords.escape(home)}", stdout: log)
119
+ qbash("#{docker} build #{a} #{Shellwords.escape(home)}", stdout:, stderr:)
118
120
  else
119
121
  raise 'Either "dockerfile" or "home" must be provided'
120
122
  end
121
123
  i
122
124
  end
123
125
  container = "donce-#{SecureRandom.hex(6)}"
124
- stdout = nil
126
+ out = nil
125
127
  code = 0
126
128
  cmd = [
127
- docker, 'run',
129
+ docker,
130
+ 'run',
128
131
  ('--detach' if block_given?),
129
132
  '--name', Shellwords.escape(container),
130
- ("--add-host #{donce_host}:host-gateway" unless OS.linux?),
133
+ "--add-host #{donce_host}:host-gateway",
131
134
  args,
132
135
  env.map { |k, v| "--env #{Shellwords.escape("#{k}=#{v}")}" }.join(' '),
133
136
  ports.map { |k, v| "--publish #{Shellwords.escape("#{k}:#{v}")}" }.join(' '),
@@ -138,36 +141,37 @@ module Kernel
138
141
  ].compact.join(' ')
139
142
  begin
140
143
  begin
141
- stdout, code =
144
+ out, code =
142
145
  Timeout.timeout(timeout) do
143
146
  qbash(
144
147
  cmd,
145
- stdout: log,
148
+ stdout:,
149
+ stderr:,
146
150
  accept: nil,
147
151
  both: true,
148
152
  env:
149
153
  )
150
154
  end
151
155
  unless code.zero?
152
- log.error(stdout)
153
156
  raise \
154
157
  "Failed to run #{cmd} " \
155
- "(exit code is ##{code}, stdout has #{stdout.split("\n").count} lines)"
158
+ "(exit code is ##{code}, stdout has #{out.split("\n").count} lines)"
156
159
  end
157
160
  yield container if block_given?
158
161
  ensure
159
162
  logs = qbash(
160
163
  "#{docker} logs #{Shellwords.escape(container)}",
161
164
  level: code.zero? ? Logger::DEBUG : Logger::ERROR,
162
- stdout: log
165
+ stdout:,
166
+ stderr:
163
167
  )
164
- stdout = logs if block_given?
165
- qbash("#{docker} rm --force #{Shellwords.escape(container)}", stdout: log)
168
+ out = logs if block_given?
169
+ qbash("#{docker} rm --force #{Shellwords.escape(container)}", stdout:, stderr:)
166
170
  end
167
- stdout
171
+ out
168
172
  ensure
169
173
  Timeout.timeout(10) do
170
- qbash("#{docker} rmi #{img}", stdout: log) unless image
174
+ qbash("#{docker} rmi #{img}", stdout:, stderr:) unless image
171
175
  end
172
176
  end
173
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.5.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko