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.
- checksums.yaml +4 -4
- data/donce.gemspec +1 -1
- data/lib/donce.rb +21 -17
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 794fd321c85b3efd46c082e5555c69dfe4d97cd2eaed233b812f50dfb4453f47
|
|
4
|
+
data.tar.gz: b8f59039bc21dc5e585ebba73bf825fdcfb30d249f77879d0e50666c9b5514c5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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.
|
|
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 [
|
|
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,
|
|
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 "
|
|
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:
|
|
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:
|
|
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
|
-
|
|
126
|
+
out = nil
|
|
125
127
|
code = 0
|
|
126
128
|
cmd = [
|
|
127
|
-
docker,
|
|
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
|
-
|
|
144
|
+
out, code =
|
|
142
145
|
Timeout.timeout(timeout) do
|
|
143
146
|
qbash(
|
|
144
147
|
cmd,
|
|
145
|
-
stdout
|
|
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 #{
|
|
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
|
|
165
|
+
stdout:,
|
|
166
|
+
stderr:
|
|
163
167
|
)
|
|
164
|
-
|
|
165
|
-
qbash("#{docker} rm --force #{Shellwords.escape(container)}", stdout:
|
|
168
|
+
out = logs if block_given?
|
|
169
|
+
qbash("#{docker} rm --force #{Shellwords.escape(container)}", stdout:, stderr:)
|
|
166
170
|
end
|
|
167
|
-
|
|
171
|
+
out
|
|
168
172
|
ensure
|
|
169
173
|
Timeout.timeout(10) do
|
|
170
|
-
qbash("#{docker} rmi #{img}", stdout:
|
|
174
|
+
qbash("#{docker} rmi #{img}", stdout:, stderr:) unless image
|
|
171
175
|
end
|
|
172
176
|
end
|
|
173
177
|
end
|