Class: Dev::Docker

Inherits:
Object show all
Defined in:
lib/firespring_dev_commands/docker.rb,
lib/firespring_dev_commands/docker/status.rb,
lib/firespring_dev_commands/docker/compose.rb

Overview

Class contains many useful methods for interfacing with the docker api

Defined Under Namespace

Classes: Compose, Config, Status

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDocker

Returns a new instance of Docker.



41
42
43
# File 'lib/firespring_dev_commands/docker.rb', line 41

def initialize
  check_version
end

Class Method Details

.config {|@config| ... } ⇒ Object Also known as: configure

Instantiates a new top level config object if one hasn’t already been created Yields that config object to any given block Returns the resulting config object

Yields:



26
27
28
29
30
# File 'lib/firespring_dev_commands/docker.rb', line 26

def config
  @config ||= Config.new
  yield(@config) if block_given?
  @config
end

.versionObject

Returns the version of the docker engine running on the system



36
37
38
# File 'lib/firespring_dev_commands/docker.rb', line 36

def version
  @version ||= JSON.parse(::Docker.connection.get('/version'))['Version']
end

Instance Method Details

#check_versionObject

Checks the min and max version against the current docker version if they have been configured



46
47
48
49
50
51
52
# File 'lib/firespring_dev_commands/docker.rb', line 46

def check_version
  min_version = self.class.config.min_version
  raise "requires docker version >= #{min_version} (found #{self.class.version})" if min_version && !Dev::Common.new.version_greater_than(min_version, self.class.version)

  max_version = self.class.config.max_version
  raise "requires docker version < #{max_version} (found #{self.class.version})" if max_version && Dev::Common.new.version_greater_than(max_version, self.class.version)
end

#container_by_name(service_name, prefix = nil, status: [Docker::Status::RUNNING]) ⇒ Object

Deprecated.

Calls the docker compose method with the given inputs



129
130
131
132
# File 'lib/firespring_dev_commands/docker.rb', line 129

def container_by_name(service_name, prefix = nil, status: [Docker::Status::RUNNING])
  warn '[DEPRECATION] `Docker#container_by_name` is deprecated.  Please use `Docker::Compose#container_by_name` instead.'
  Docker::Compose.new.container_by_name(service_name, prefix, status)
end

#copy_from_container(container, source_path, dest_path, required: true) ⇒ Object

Copies the source path on the container to the destination path on your local machine If required is set to true, the command will fail if the source path does not exist on the container



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/firespring_dev_commands/docker.rb', line 156

def copy_from_container(container, source_path, dest_path, required: true)
  LOG.info "Copying #{source_path} to #{dest_path}... "

  tar = StringIO.new
  begin
    container.archive_out(source_path) do |chunk|
      tar.write(chunk)
    end
  rescue => e
    raise e if required

    puts 'Not Found'
  end

  Dev::Tar.new(tar).unpack(source_path, dest_path)
end

#copy_to_container(container, source_path, dest_path) ⇒ Object

Copies the source path on your local machine to the destination path on the container



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/firespring_dev_commands/docker.rb', line 142

def copy_to_container(container, source_path, dest_path)
  LOG.info "Copying #{source_path} to #{dest_path}... "

  container.archive_in(source_path, dest_path, overwrite: true)
  return unless File.directory?(source_path)

  dest_file = File.basename(source_path)
  # TODO: Can we find a better solution for this? Seems pretty brittle
  retcode = container.exec(['bash', '-c', "cd #{dest_path}; tar -xf #{dest_file}; rm -f #{dest_file}"])[-1]
  raise 'Unable to unpack on container' unless retcode.zero?
end

#force_remove_images(name_and_tag) ⇒ Object

Remove docker images with the “force” option set to true This will remove the images even if they are currently in use and cause unintended side effects.



122
123
124
125
# File 'lib/firespring_dev_commands/docker.rb', line 122

def force_remove_images(name_and_tag)
  images = ::Docker::Image.all(filter: name_and_tag)
  ::Docker::Image.remove(images[0].id, force: true) unless images.empty?
end

#mapped_public_port(name, private_port) ⇒ Object

Deprecated.

Calls the docker compose method with the given inputs



136
137
138
139
# File 'lib/firespring_dev_commands/docker.rb', line 136

def mapped_public_port(name, private_port)
  warn '[DEPRECATION] `Docker#mapped_public_port` is deprecated.  Please use `Docker::Compose#mapped_public_port` instead.'
  Docker::Compose.new.mapped_public_port(name, private_port)
end

Display a nicely formatted table of containers and their associated information



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/firespring_dev_commands/docker.rb', line 222

def print_containers
  idsize = 15
  imagesize = 20
  archsize = 7
  commandsize = 20
  createsize = 15
  statussize = 15
  portsize = 20
  namesize = 16
  total = idsize + imagesize + archsize + commandsize + createsize + statussize + portsize + namesize

  # If there is additional width available, add it to the repo and tag columns
  additional = [((Rake.application.terminal_width - total) / 3).floor, 0].max

  # If there's enough extra, give some to the name as well
  if additional > 40
    namesize += 15
    additional -= 5
  end
  imagesize += additional
  commandsize += additional
  portsize += additional

  format = "%-#{idsize}s%-#{imagesize}s%-#{archsize}s%-#{commandsize}s%-#{createsize}s%-#{statussize}s%-#{portsize}s%-#{namesize}s"
  puts format(format, :'CONTAINER ID', :IMAGE, :ARCH, :COMMAND, :CREATED, :STATUS, :PORTS, :NAMES)
  ::Docker::Container.all.each do |container|
    id, image, arch, command, created, status, ports, names = container_info(container)
    puts format(format, id, image.truncate(imagesize - 5), arch, command.truncate(commandsize - 5), created, status, ports.truncate(portsize - 5), names)
  end
end

rubocop:disable Metrics/ParameterLists Display a nicely formatted table of images and their associated information



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/firespring_dev_commands/docker.rb', line 175

def print_images
  reposize   = 70
  tagsize    = 70
  archsize   = 9
  imagesize  = 15
  createsize = 20
  sizesize   = 10
  total = reposize + tagsize + archsize + imagesize + createsize + sizesize

  # If there is additional width available, add it to the repo and tag columns
  additional = [((Rake.application.terminal_width - total) / 2).floor, 0].max
  reposize += additional
  tagsize += additional

  format = "%-#{reposize}s%-#{tagsize}s%-#{archsize}s%-#{imagesize}s%-#{createsize}s%s"
  puts format(format, :REPOSITORY, :TAG, :ARCH, :'IMAGE ID', :CREATED, :SIZE)
  ::Docker::Image.all.each do |image|
    image_info(image).each do |repo, tag, arch, id, created, size|
      puts format(format, repo, tag, arch, id, created, size)
    end
  end
end

#pruneObject

Prunes/removes all unused containers, networks, volumes, and images



55
56
57
58
59
60
# File 'lib/firespring_dev_commands/docker.rb', line 55

def prune
  prune_containers
  prune_networks
  prune_volumes
  prune_images
end

#prune_containersObject

Prunes/removes all unused containers



63
64
65
# File 'lib/firespring_dev_commands/docker.rb', line 63

def prune_containers
  _prune('containers')
end

#prune_imagesObject

Prunes/removes all unused images



81
82
83
# File 'lib/firespring_dev_commands/docker.rb', line 81

def prune_images
  _prune('images')
end

#prune_networksObject

Prunes/removes all unused networks



68
69
70
# File 'lib/firespring_dev_commands/docker.rb', line 68

def prune_networks
  _prune('networks')
end

#prune_volumesObject

Prunes/removes all unused volumes Specify ALL_VOLUMES=false in your environment to only clean anonymous volumes (docker version 23.x+)



74
75
76
77
78
# File 'lib/firespring_dev_commands/docker.rb', line 74

def prune_volumes
  opts = {}
  opts[:filters] = {all: ['true']}.to_json if Dev::Common.new.version_greater_than('22.9999.0', self.class.version) && ENV['ALL_VOLUMES'].to_s.strip != 'false'
  _prune('volumes', opts:)
end