Class: Kuby::Docker::CLI

Inherits:
CLIBase show all
Extended by:
T::Sig
Defined in:
lib/kuby/docker/cli.rb

Constant Summary

Constants inherited from CLIBase

CLIBase::AfterCallback, CLIBase::BeforeCallback

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

Returns a new instance of CLI.



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

def initialize(executable = nil)
  @executable = T.let(executable || `which docker`.strip, String)
end

Instance Attribute Details

#executableObject (readonly)

Returns the value of attribute executable.



13
14
15
# File 'lib/kuby/docker/cli.rb', line 13

def executable
  @executable
end

Instance Method Details

#authsObject



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(T.must(config_file)))
  config.fetch('auths', {}).keys
end

#build(image, build_args: {}, docker_args: [], context: nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/kuby/docker/cli.rb', line 64

def build(image, build_args: {}, docker_args: [], context: 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,
    '-f-',
    *docker_args,
    context || '.'
  ]

  open3_w(cmd) do |stdin, _wait_threads|
    stdin.puts(image.dockerfile.to_s)
  end

  unless T.must(last_status).success?
    raise BuildError, 'build failed: docker command exited with '\
      "status code #{T.must(last_status).exitstatus}"
  end
end

#config_fileObject



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



28
29
30
# File 'lib/kuby/docker/cli.rb', line 28

def default_config_file
  File.join(Dir.home, '.docker', 'config.json')
end

#images(image_url) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/kuby/docker/cli.rb', line 109

def images(image_url)
  cmd = [
    executable, 'images', image_url,
    '--format', '"{{json . }}"'
  ]

  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

#login(url:, username:, password:) ⇒ Object



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 T.must(last_status).success?
    raise LoginError, 'build failed: docker command exited with '\
      "status code #{T.must(last_status).exitstatus}"
  end
end

#pull(image_url, tag) ⇒ Object



135
136
137
138
139
140
141
142
143
144
# File 'lib/kuby/docker/cli.rb', line 135

def pull(image_url, tag)
  systemm([
    executable, 'pull', "#{image_url}:#{tag}"
  ])

  unless T.must(last_status).success?
    raise PullError, 'pull failed: docker command exited with '\
      "status code #{T.must(last_status).exitstatus}"
  end
end

#push(image_url, tag) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'lib/kuby/docker/cli.rb', line 123

def push(image_url, tag)
  systemm([
    executable, 'push', "#{image_url}:#{tag}"
  ])

  unless T.must(last_status).success?
    raise PushError, 'push failed: docker command exited with '\
      "status code #{T.must(last_status).exitstatus}"
  end
end

#run(image_url:, tag: 'latest', env: {}, ports: []) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/kuby/docker/cli.rb', line 95

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



147
148
149
# File 'lib/kuby/docker/cli.rb', line 147

def status_key
  :kuby_docker_cli_last_status
end

#stderr_keyObject



157
158
159
# File 'lib/kuby/docker/cli.rb', line 157

def stderr_key
  :kuby_docker_stderr
end

#stdout_keyObject



152
153
154
# File 'lib/kuby/docker/cli.rb', line 152

def stdout_key
  :kuby_docker_stdout
end