Class: Kuby::Docker::CLI

Inherits:
CLIBase show all
Defined in:
lib/kuby/docker/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from CLIBase

#after_execute, #before_execute, #last_status, #stderr, #stderr=, #stdout, #stdout=, #with_pipes

Constructor Details

#initialize(executable = nil) ⇒ CLI

T::Sig::WithoutRuntime.sig { params(executable: T.nilable(String)).void }



16
17
18
# File 'lib/kuby/docker/cli.rb', line 16

def initialize(executable = nil)
  @executable = executable || Kuby::Utils.which('docker')
end

Instance Attribute Details

#executableObject (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

#authsObject

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.tags.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_fileObject

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_fileObject

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 (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_keyObject

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_keyObject

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_keyObject

T::Sig::WithoutRuntime.sig { returns(Symbol) }



180
181
182
# File 'lib/kuby/docker/cli.rb', line 180

def stdout_key
  :kuby_docker_stdout
end