donce 0.0.3 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -1
- data/donce.gemspec +1 -1
- data/lib/donce.rb +14 -12
- data/test/test_donce.rb +5 -0
- 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: 44c678f3ea8b379cf1d76ef37954ad59a3b4bb508001576972b799196859f547
|
4
|
+
data.tar.gz: a669642f9ddc4a45e04f8d00f9a2bda74fd2ae62f40717626b1ab2fd992f627a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1e66afd723eaef2ce89b2ec88418f9be91ef25ddbe0054015d707cdaf178086f68e0e78337c5305fbd752fc92434782212f61fe8e0f2f5bc8220c1f586668522
|
7
|
+
data.tar.gz: 819b0e6086390e483cb54b0b42ac007c2f2bdc0f39fd7170325abcfc53bc1cdd98b7df821474c46078401f5bc0d0faedb90e5e929b8a95202a049cfbd280a550
|
data/README.md
CHANGED
@@ -12,7 +12,7 @@
|
|
12
12
|
[](https://github.com/yegor256/donce/blob/master/LICENSE.txt)
|
13
13
|
|
14
14
|
This small Ruby library helps building temporary [Docker]
|
15
|
-
images,
|
15
|
+
images, runs Docker containers, and cleans up afterwards — it may be
|
16
16
|
convenient for automated tests (for example, with [Minitest]):
|
17
17
|
|
18
18
|
```ruby
|
@@ -29,6 +29,15 @@ class MyTest < Minitest::Test
|
|
29
29
|
end
|
30
30
|
```
|
31
31
|
|
32
|
+
It's possible to run Docker image in a background mode too:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
stdout = donce(image: 'ubuntu', command: 'sleep 9999') do |id|
|
36
|
+
# The "id" is the container id
|
37
|
+
# The "donce_host()" is the hostname of it
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
32
41
|
That's it.
|
33
42
|
|
34
43
|
## How to contribute
|
data/donce.gemspec
CHANGED
@@ -26,7 +26,7 @@ Gem::Specification.new do |s|
|
|
26
26
|
s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version=
|
27
27
|
s.required_ruby_version = '>=3.0'
|
28
28
|
s.name = 'donce'
|
29
|
-
s.version = '0.0.
|
29
|
+
s.version = '0.0.5'
|
30
30
|
s.license = 'MIT'
|
31
31
|
s.summary = 'Builds and starts temporary Docker containers'
|
32
32
|
s.description =
|
data/lib/donce.rb
CHANGED
@@ -74,12 +74,14 @@ module Kernel
|
|
74
74
|
# @param [Logger] log The logging destination, can be +$stdout+
|
75
75
|
# @param [String|Array<String>] args List of extra arguments for the +docker+ command
|
76
76
|
# @param [Hash<String,String>] env Environment variables going into the container
|
77
|
+
# @param [Hash<String,String>] volumes Local to container volumes mapping
|
78
|
+
# @param [Hash<String,String>] ports Local to container port mapping
|
77
79
|
# @param [Boolean] root Let user inside the container be "root"?
|
78
80
|
# @param [String|Array<String>] command The command for the script inside the container
|
79
81
|
# @param [Integer] timeout Maximum seconds to spend on each +docker+ call
|
80
82
|
# @return [String] The stdout of the container
|
81
83
|
def donce(dockerfile: nil, image: nil, home: nil, log: $stdout, args: '', env: {}, root: false, command: '',
|
82
|
-
timeout: 10)
|
84
|
+
timeout: 10, volumes: {}, ports: {})
|
83
85
|
raise 'Either use "dockerfile" or "home"' if dockerfile && home
|
84
86
|
raise 'Either use "dockerfile" or "image"' if dockerfile && image
|
85
87
|
raise 'Either use "image" or "home"' if home && image
|
@@ -89,7 +91,7 @@ module Kernel
|
|
89
91
|
if image
|
90
92
|
image
|
91
93
|
else
|
92
|
-
i = "donce-#{SecureRandom.hex(
|
94
|
+
i = "donce-#{SecureRandom.hex(6)}"
|
93
95
|
if dockerfile
|
94
96
|
Dir.mktmpdir do |tmp|
|
95
97
|
File.write(File.join(tmp, 'Dockerfile'), dockerfile)
|
@@ -100,22 +102,24 @@ module Kernel
|
|
100
102
|
end
|
101
103
|
i
|
102
104
|
end
|
103
|
-
container = "donce-#{SecureRandom.hex(
|
105
|
+
container = "donce-#{SecureRandom.hex(6)}"
|
104
106
|
begin
|
105
107
|
stdout = nil
|
106
108
|
code = 0
|
107
109
|
begin
|
108
110
|
cmd = [
|
109
111
|
docker, 'run',
|
110
|
-
block_given? ? '-d' :
|
112
|
+
block_given? ? '-d' : nil,
|
111
113
|
'--name', Shellwords.escape(container),
|
112
|
-
OS.linux? ?
|
114
|
+
OS.linux? ? nil : "--add-host #{donce_host}:host-gateway",
|
113
115
|
args,
|
114
116
|
env.map { |k, v| "-e #{Shellwords.escape("#{k}=#{v}")}" }.join(' '),
|
115
|
-
|
117
|
+
ports.map { |k, v| "-p #{Shellwords.escape("#{k}:#{v}")}" }.join(' '),
|
118
|
+
volumes.map { |k, v| "-v #{Shellwords.escape("#{k}:#{v}")}" }.join(' '),
|
119
|
+
root ? nil : "--user=#{Shellwords.escape("#{Process.uid}:#{Process.gid}")}",
|
116
120
|
Shellwords.escape(img),
|
117
121
|
command
|
118
|
-
].join(' ')
|
122
|
+
].compact.join(' ')
|
119
123
|
stdout, code =
|
120
124
|
Timeout.timeout(timeout) do
|
121
125
|
qbash(
|
@@ -132,16 +136,14 @@ module Kernel
|
|
132
136
|
"Failed to run #{cmd} " \
|
133
137
|
"(exit code is ##{code}, stdout has #{stdout.split("\n").count} lines)"
|
134
138
|
end
|
135
|
-
if block_given?
|
136
|
-
r = yield container
|
137
|
-
return r
|
138
|
-
end
|
139
|
+
yield container if block_given?
|
139
140
|
ensure
|
140
|
-
qbash(
|
141
|
+
logs = qbash(
|
141
142
|
"#{docker} logs #{Shellwords.escape(container)}",
|
142
143
|
level: code.zero? ? Logger::DEBUG : Logger::ERROR,
|
143
144
|
log:
|
144
145
|
)
|
146
|
+
stdout = logs if block_given?
|
145
147
|
qbash("#{docker} rm -f #{Shellwords.escape(container)}", log:)
|
146
148
|
end
|
147
149
|
stdout
|
data/test/test_donce.rb
CHANGED