donce 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3870e1c444a1259a5cb5d3a2b32e73d66dba22e8bf1a519ba377d6a520d1637c
4
- data.tar.gz: d19607fe2d7090bfa106b5858e051442b5b7d48839b479ef2df49c8c38f90d2f
3
+ metadata.gz: ad7d3419ce887d6b42f6f92f6dc9ad8414b0c3c89c1fb0ef4600972ac54f754e
4
+ data.tar.gz: a8d95d419418e037968ae4f659a5b538a8c67c44fee323974befefa7d5651b5c
5
5
  SHA512:
6
- metadata.gz: 85863c16fd11008e5fd302724ddd23314cd7519e10803292a39a58312a8371e8345509dac4878cab32afd5198bd65079e03f2f496b701635d84af619fe7bfc45
7
- data.tar.gz: 618eb11dd8880fbcdd08863fcfa332d7567bb1d405e6cf3e268398d6f3cb5123169867ecefe4a308e4b40d609bb03285593c4a98acea98437bb2f68d3b656ffd
6
+ metadata.gz: 22738b8cdd2b2eef8188c1e69dfea55a7d3b7317f73623fd2230cd65695dd033994f58a62fca0e549a5e846245c61b1ec0d870760d79becda4e4a769f62ba9dc
7
+ data.tar.gz: 86ee931f8f8149192661d2541b19abbd58377dd8d6aa7fc5770fbe1fdc66819aeb8ff409224938b74acedf5a94b5d961c40439f4263d83cdf3eb1e84b9e24169
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.1'
29
+ s.version = '0.0.3'
30
30
  s.license = 'MIT'
31
31
  s.summary = 'Builds and starts temporary Docker containers'
32
32
  s.description =
data/lib/donce.rb CHANGED
@@ -48,9 +48,8 @@ require 'shellwords'
48
48
  # background execution of the container (in daemon mode):
49
49
  #
50
50
  # def test_runs_daemon
51
- # donce(dockerfile: "FROM ubuntu\nCMD sleep 9999") do |id, host|
51
+ # donce(dockerfile: "FROM ubuntu\nCMD sleep 9999") do |id|
52
52
  # refute_empty(id) # the ID of the container
53
- # refute_empty(host) # the hostname of it
54
53
  # end
55
54
  # end
56
55
  #
@@ -61,6 +60,12 @@ require 'shellwords'
61
60
  # Copyright:: Copyright (c) 2025 Yegor Bugayenko
62
61
  # License:: MIT
63
62
  module Kernel
63
+ # The name of the localhost inside Docker container.
64
+ # @return [String] The hostname
65
+ def donce_host
66
+ OS.linux? ? '172.17.0.1' : 'host.docker.internal'
67
+ end
68
+
64
69
  # Build Docker image (or use existing one), run Docker container, and then clean up.
65
70
  #
66
71
  # @param [String] dockerfile The content of the +Dockerfile+
@@ -72,6 +77,7 @@ module Kernel
72
77
  # @param [Boolean] root Let user inside the container be "root"?
73
78
  # @param [String|Array<String>] command The command for the script inside the container
74
79
  # @param [Integer] timeout Maximum seconds to spend on each +docker+ call
80
+ # @return [String] The stdout of the container
75
81
  def donce(dockerfile: nil, image: nil, home: nil, log: $stdout, args: '', env: {}, root: false, command: '',
76
82
  timeout: 10)
77
83
  raise 'Either use "dockerfile" or "home"' if dockerfile && home
@@ -85,15 +91,16 @@ module Kernel
85
91
  else
86
92
  i = "donce-#{SecureRandom.hex(8)}"
87
93
  if dockerfile
88
- Dir.mktmpdir do |home|
89
- File.write(File.join(home, 'Dockerfile'), dockerfile)
90
- qbash("#{docker} build #{Shellwords.escape(home)} -t #{i}", log:)
94
+ Dir.mktmpdir do |tmp|
95
+ File.write(File.join(tmp, 'Dockerfile'), dockerfile)
96
+ qbash("#{docker} build #{Shellwords.escape(tmp)} -t #{i}", log:)
91
97
  end
98
+ else
99
+ qbash("#{docker} build #{Shellwords.escape(home)} -t #{i}", log:)
92
100
  end
93
101
  i
94
102
  end
95
103
  container = "donce-#{SecureRandom.hex(8)}"
96
- host = OS.linux? ? '172.17.0.1' : 'host.docker.internal'
97
104
  begin
98
105
  stdout = nil
99
106
  code = 0
@@ -102,7 +109,7 @@ module Kernel
102
109
  docker, 'run',
103
110
  block_given? ? '-d' : '',
104
111
  '--name', Shellwords.escape(container),
105
- OS.linux? ? '' : "--add-host #{host}:host-gateway",
112
+ OS.linux? ? '' : "--add-host #{donce_host}:host-gateway",
106
113
  args,
107
114
  env.map { |k, v| "-e #{Shellwords.escape("#{k}=#{v}")}" }.join(' '),
108
115
  root ? '' : "--user=#{Shellwords.escape("#{Process.uid}:#{Process.gid}")}",
@@ -126,7 +133,7 @@ module Kernel
126
133
  "(exit code is ##{code}, stdout has #{stdout.split("\n").count} lines)"
127
134
  end
128
135
  if block_given?
129
- r = yield container, host
136
+ r = yield container
130
137
  return r
131
138
  end
132
139
  ensure
data/test/test_donce.rb CHANGED
@@ -34,12 +34,24 @@ class TestDonce < Minitest::Test
34
34
  assert_equal("hello\n", stdout)
35
35
  end
36
36
 
37
+ def test_runs_existing_image
38
+ stdout = donce(image: 'ubuntu:24.04', command: 'echo hello', log: Loog::NULL)
39
+ assert_equal("hello\n", stdout)
40
+ end
41
+
42
+ def test_runs_from_home
43
+ Dir.mktmpdir do |home|
44
+ File.write(File.join(home, 'Dockerfile'), "FROM ubuntu\nCMD echo hello")
45
+ stdout = donce(home:, log: Loog::NULL)
46
+ assert_equal("hello\n", stdout)
47
+ end
48
+ end
49
+
37
50
  def test_runs_daemon
38
51
  seen = false
39
- donce(dockerfile: "FROM ubuntu\nCMD while true; do sleep 1; echo sleeping; done", log: Loog::NULL) do |id, host|
52
+ donce(dockerfile: "FROM ubuntu\nCMD while true; do sleep 1; echo sleeping; done", log: Loog::NULL) do |id|
40
53
  seen = true
41
54
  refute_empty(id)
42
- refute_empty(host)
43
55
  end
44
56
  assert(seen)
45
57
  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.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko