Class: Kuby::Docker::CLI
Instance Attribute Summary collapse
-
#executable ⇒ Object
readonly
T::Sig::WithoutRuntime.sig { returns(String) }.
Instance Method Summary collapse
-
#auths ⇒ Object
T::Sig::WithoutRuntime.sig { returns(T::Array) }.
-
#build(image, build_args: {}, docker_args: [], context: nil, cache_from: nil) ⇒ Object
T::Sig::WithoutRuntime.sig { params( image: Image, build_args: T::Hash[T.any(Symbol, String), String], docker_args: T::Array, context: T.nilable(String), cache_from: T.nilable(String) ).void }.
-
#config_file ⇒ Object
T::Sig::WithoutRuntime.sig { returns(T.nilable(String)) }.
-
#default_config_file ⇒ Object
T::Sig::WithoutRuntime.sig { returns(String) }.
-
#exec_capture(container:, command:, tty: true) ⇒ Object
T::Sig::WithoutRuntime.sig { params(container: String, command: String, tty: T::Boolean).returns(String) }.
-
#images(image_url, digests: true) ⇒ Object
T::Sig::WithoutRuntime.sig { params(image_url: String, digests: T::Boolean).returns(T::Array[T::Hash[Symbol, String]]) }.
-
#initialize(executable = nil) ⇒ CLI
constructor
T::Sig::WithoutRuntime.sig { params(executable: T.nilable(String)).void }.
-
#inspect(image_url:, tag: 'latest', format: nil) ⇒ Object
T::Sig::WithoutRuntime.sig { params(image_url: String, tag: String, format: T.nilable(String)).returns(String) }.
-
#login(url:, username:, password:) ⇒ Object
T::Sig::WithoutRuntime.sig { params(url: String, username: String, password: String).void }.
-
#pull(image_url, tag) ⇒ Object
T::Sig::WithoutRuntime.sig { params(image_url: String, tag: String).void }.
-
#push(image_url, tag) ⇒ Object
T::Sig::WithoutRuntime.sig { params(image_url: String, tag: String).void }.
-
#run(image_url:, tag: 'latest', env: {}, ports: []) ⇒ Object
T::Sig::WithoutRuntime.sig { params( image_url: String, tag: String, env: T::Hash[T.any(Symbol, String), String], ports: T::Array[T.any(String, Integer)] ) .void }.
-
#status_key ⇒ Object
T::Sig::WithoutRuntime.sig { returns(Symbol) }.
-
#stderr_key ⇒ Object
T::Sig::WithoutRuntime.sig { returns(Symbol) }.
-
#stdout_key ⇒ Object
T::Sig::WithoutRuntime.sig { returns(Symbol) }.
Methods inherited from CLIBase
#after_execute, #before_execute, #last_status, #stderr, #stderr=, #stdout, #stdout=, #with_pipes
Constructor Details
Instance Attribute Details
#executable ⇒ Object (readonly)
T::Sig::WithoutRuntime.sig { returns(String) }
13 14 15 |
# File 'lib/kuby/docker/cli.rb', line 13 def executable @executable end |
Instance Method Details
#auths ⇒ Object
T::Sig::WithoutRuntime.sig { returns(T::Array) }
49 50 51 52 53 54 |
# File 'lib/kuby/docker/cli.rb', line 49 def auths return [] unless config_file config = JSON.parse(File.read(config_file)) config.fetch('auths', {}).keys end |
#build(image, build_args: {}, docker_args: [], context: nil, cache_from: nil) ⇒ Object
T::Sig::WithoutRuntime.sig
params(
image: Image,
build_args: T::Hash[T.any(Symbol, String), String],
docker_args: T::Array[String],
context: T.nilable(String),
cache_from: T.nilable(String)
).void
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/kuby/docker/cli.rb', line 65 def build(image, build_args: {}, docker_args: [], context: nil, cache_from: nil) cmd = [ executable, 'build', *image..flat_map { |tag| ['-t', "#{image.image_url}:#{tag}"] }, *build_args.flat_map do |arg, val| ['--build-arg', Shellwords.shellescape("#{arg}=#{val}")] end ] if cache_from cmd += ['--cache-from', cache_from] end cmd += [ '-f-', *docker_args, context || '.' ] open3_w(cmd) do |stdin, _wait_threads| stdin.puts(image.dockerfile.to_s) end unless last_status.success? raise BuildError, 'build failed: docker command exited with '\ "status code #{last_status.exitstatus}" end end |
#config_file ⇒ Object
T::Sig::WithoutRuntime.sig { returns(T.nilable(String)) }
21 22 23 24 25 |
# File 'lib/kuby/docker/cli.rb', line 21 def config_file if File.exist?(default_config_file) default_config_file end end |
#default_config_file ⇒ Object
T::Sig::WithoutRuntime.sig { returns(String) }
28 29 30 |
# File 'lib/kuby/docker/cli.rb', line 28 def default_config_file File.join(Dir.home, '.docker', 'config.json') end |
#exec_capture(container:, command:, tty: true) ⇒ Object
T::Sig::WithoutRuntime.sig { params(container: String, command: String, tty: T::Boolean).returns(String) }
117 118 119 120 121 122 123 |
# File 'lib/kuby/docker/cli.rb', line 117 def exec_capture(container:, command:, tty: true) cmd = [executable, 'exec'] cmd << '-it' if tty cmd += [container, command] backticks(cmd) end |
#images(image_url, digests: true) ⇒ Object
T::Sig::WithoutRuntime.sig { params(image_url: String, digests: T::Boolean).returns(T::Array[T::Hash[Symbol, String]]) }
135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/kuby/docker/cli.rb', line 135 def images(image_url, digests: true) cmd = [ executable, 'images', image_url, '--format', '"{{json . }}"' ] cmd << '--digests' if digests backticks(cmd).split("\n").map do |image_data| JSON.parse(image_data).each_with_object({}) do |(k, v), ret| ret[k.underscore.to_sym] = v end end end |
#inspect(image_url:, tag: 'latest', format: nil) ⇒ Object
T::Sig::WithoutRuntime.sig { params(image_url: String, tag: String, format: T.nilable(String)).returns(String) }
126 127 128 129 130 131 132 |
# File 'lib/kuby/docker/cli.rb', line 126 def inspect(image_url:, tag: 'latest', format: nil) cmd = [executable, 'inspect'] cmd += ['--format', "'#{format}'"] cmd << "#{image_url}:#{tag}" backticks(cmd) end |
#login(url:, username:, password:) ⇒ Object
T::Sig::WithoutRuntime.sig { params(url: String, username: String, password: String).void }
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/kuby/docker/cli.rb', line 33 def login(url:, username:, password:) cmd = [ executable, 'login', url, '--username', username, '--password-stdin' ] open3_w(cmd) do |stdin, _wait_threads| stdin.puts(password) end unless last_status.success? raise LoginError, 'build failed: docker command exited with '\ "status code #{last_status.exitstatus}" end end |
#pull(image_url, tag) ⇒ Object
T::Sig::WithoutRuntime.sig { params(image_url: String, tag: String).void }
163 164 165 166 167 168 169 170 171 172 |
# File 'lib/kuby/docker/cli.rb', line 163 def pull(image_url, tag) systemm([ executable, 'pull', "#{image_url}:#{tag}" ]) unless last_status.success? raise PullError, 'pull failed: docker command exited with '\ "status code #{last_status.exitstatus}" end end |
#push(image_url, tag) ⇒ Object
T::Sig::WithoutRuntime.sig { params(image_url: String, tag: String).void }
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/kuby/docker/cli.rb', line 151 def push(image_url, tag) systemm([ executable, 'push', "#{image_url}:#{tag}" ]) unless last_status.success? raise PushError, 'push failed: docker command exited with '\ "status code #{last_status.exitstatus}" end end |
#run(image_url:, tag: 'latest', env: {}, ports: []) ⇒ Object
T::Sig::WithoutRuntime.sig
params(
image_url: String,
tag: String,
env: T::Hash[T.any(Symbol, String), String],
ports: T::Array[T.any(String, Integer)]
)
.void
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/kuby/docker/cli.rb', line 103 def run(image_url:, tag: 'latest', env: {}, ports: []) cmd = [ executable, 'run', *env.flat_map { |k, v| ['-e', "#{k}=#{v}"] }, *ports.flat_map { |port| ['-p', "#{port}:#{port}"] }, '--init', '--rm', "#{image_url}:#{tag}" ] execc(cmd) end |
#status_key ⇒ Object
T::Sig::WithoutRuntime.sig { returns(Symbol) }
175 176 177 |
# File 'lib/kuby/docker/cli.rb', line 175 def status_key :kuby_docker_cli_last_status end |
#stderr_key ⇒ Object
T::Sig::WithoutRuntime.sig { returns(Symbol) }
185 186 187 |
# File 'lib/kuby/docker/cli.rb', line 185 def stderr_key :kuby_docker_stderr end |
#stdout_key ⇒ Object
T::Sig::WithoutRuntime.sig { returns(Symbol) }
180 181 182 |
# File 'lib/kuby/docker/cli.rb', line 180 def stdout_key :kuby_docker_stdout end |