donce 0.5.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 (4) hide show
  1. checksums.yaml +4 -4
  2. data/donce.gemspec +1 -1
  3. data/lib/donce.rb +21 -17
  4. 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: 794fd321c85b3efd46c082e5555c69dfe4d97cd2eaed233b812f50dfb4453f47
4
+ data.tar.gz: b8f59039bc21dc5e585ebba73bf825fdcfb30d249f77879d0e50666c9b5514c5
5
5
  SHA512:
6
- metadata.gz: 81203a7306ab4edd80ac3ce16a27bcacb8505bb73bd99c3d0ad629193cdd98222b7e954382d671a76e9a3f8f2a461914f5a18c2f7845955dc31242b3b8796c09
7
- data.tar.gz: f4978002f8c14db6783e5480f0f50e064e6c6883c6c1de61d4728022fa522bdc98be1cba1449ad88ec2db8eb08c2a2f65f18691eef9a673f71ffbaf8157e322f
6
+ metadata.gz: babea47d7f7d8b6c13541102167cea6b88e9e48be2d2233535b417cfd0144946a04a710b64eebd4ecffa894e3c88320e1def531f33d2d406ca5f9cbb4f867e26
7
+ data.tar.gz: fc734444357b9b2e7a6cd4b460b5ccad6f6316fee65f182345e0b2143e1b7398fa96fc64eba08759744fe4a528ed17be48ae0c0e26b830a8c7a327dbf439c905
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.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,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,20 +113,21 @@ 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
133
  ("--add-host #{donce_host}:host-gateway" unless OS.linux?),
@@ -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.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko