class Superhosting::DockerApi

Constants

AVAILABLE_DOCKER_OPTIONS

Public Class Methods

new(**kwargs) click to toggle source
# File lib/superhosting/docker_api.rb, line 8
def initialize(**kwargs)
  @socket = kwargs[:socket] || '/var/run/docker.sock'
end

Public Instance Methods

container_dead?(name) click to toggle source
# File lib/superhosting/docker_api.rb, line 146
def container_dead?(name)
  container_status?(name, 'dead')
end
container_exists?(name) click to toggle source
# File lib/superhosting/docker_api.rb, line 154
def container_exists?(name)
  self.with_dry_run do |dry_run|
    return true if dry_run and self.storage.key? name
    container_info(name).nil? ? false : true
  end
end
container_exited?(name) click to toggle source
# File lib/superhosting/docker_api.rb, line 142
def container_exited?(name)
  container_status?(name, 'exited')
end
container_image?(name, image) click to toggle source
# File lib/superhosting/docker_api.rb, line 161
def container_image?(name, image)
  container = container_info(name)
  image = image_info(image)

  if container.nil? or image.nil?
    false
  else
    container['Image'] == image['Id']
  end
end
container_info(name) click to toggle source
# File lib/superhosting/docker_api.rb, line 24
def container_info(name)
  resp_if_success raw_connection.request(method: :get, path: "/containers/#{name}/json")
end
container_kill!(name) click to toggle source
# File lib/superhosting/docker_api.rb, line 32
def container_kill!(name)
  self.debug_operation(desc: { code: :container, data: { name: name } }) do |&blk|
    self.with_dry_run do |dry_run|
      self.storage.delete(name) if dry_run
      resp_if_success raw_connection.request(method: :post, path: "/containers/#{name}/kill") unless dry_run
    end
    blk.call(code: :killed)
  end
end
container_list() click to toggle source
# File lib/superhosting/docker_api.rb, line 28
def container_list
  resp_if_success raw_connection.request(method: :get, path: '/containers/json')
end
container_not_exists?(name) click to toggle source
# File lib/superhosting/docker_api.rb, line 150
def container_not_exists?(name)
  !container_exists?(name)
end
container_not_running?(name) click to toggle source
# File lib/superhosting/docker_api.rb, line 130
def container_not_running?(name)
  !container_running?(name)
end
container_pause!(name) click to toggle source
# File lib/superhosting/docker_api.rb, line 72
def container_pause!(name)
  self.debug_operation(desc: { code: :container, data: { name: name } }) do |&blk|
    self.with_dry_run do |dry_run|
      self.storage[name] = 'paused' if dry_run
      resp_if_success raw_connection.request(method: :post, path: "/containers/#{name}/pause") unless dry_run
    end
    blk.call(code: :paused)
  end
end
container_paused?(name) click to toggle source
# File lib/superhosting/docker_api.rb, line 138
def container_paused?(name)
  container_status?(name, 'paused')
end
container_restart!(name) click to toggle source
# File lib/superhosting/docker_api.rb, line 92
def container_restart!(name)
  self.debug_operation(desc: { code: :container, data: { name: name } }) do |&blk|
    self.with_dry_run do |dry_run|
      resp_if_success raw_connection.request(method: :post, path: "/containers/#{name}/restart") unless dry_run
      self.storage[name] = 'running' if dry_run
    end
    blk.call(code: :restarted)
  end
end
container_restarting?(name) click to toggle source
# File lib/superhosting/docker_api.rb, line 134
def container_restarting?(name)
  container_status?(name, 'restarting')
end
container_rm!(name) click to toggle source
# File lib/superhosting/docker_api.rb, line 42
def container_rm!(name)
  self.debug_operation(desc: { code: :container, data: { name: name } }) do |&blk|
    self.with_dry_run do |dry_run|
      self.storage.delete(name) if dry_run
      resp_if_success raw_connection.request(method: :delete, path: "/containers/#{name}") unless dry_run
    end
    blk.call(code: :removed)
  end
end
container_rm_inactive!(name) click to toggle source
# File lib/superhosting/docker_api.rb, line 102
def container_rm_inactive!(name)
  self.container_rm!(name) if self.container_exists?(name) and !self.container_running?(name)
end
container_run(name, options, image, command) click to toggle source
# File lib/superhosting/docker_api.rb, line 172
def container_run(name, options, image, command)
  cmd = "docker run --detach --name #{name} #{options.join(' ')} #{image} #{command}"
  self.debug_operation(desc: { code: :container, data: { name: name } }) do |&blk|
    self.with_dry_run do |dry_run|
      self.storage[name] = 'running' if dry_run
    end

    self.command!(cmd).tap do
      blk.call(code: :added)
    end
  end
end
container_running?(name) click to toggle source
# File lib/superhosting/docker_api.rb, line 118
def container_running?(name)
  self.with_dry_run do |dry_run|
    return true if dry_run and self.storage[name] == 'running'
    resp = container_info(name)
    if resp.nil?
      false
    else
      resp['State']['Running'] and %w(Restarting Paused OOMKilled Dead).all? {|c| !resp['State'][c] }
    end
  end
end
container_start!(name) click to toggle source
# File lib/superhosting/docker_api.rb, line 62
def container_start!(name)
  self.debug_operation(desc: { code: :container, data: { name: name } }) do |&blk|
    self.with_dry_run do |dry_run|
      self.storage[name] = 'running' if dry_run
      resp_if_success raw_connection.request(method: :post, path: "/containers/#{name}/start") unless dry_run
    end
    blk.call(code: :started)
  end
end
container_status?(name, status) click to toggle source
# File lib/superhosting/docker_api.rb, line 106
def container_status?(name, status)
  self.with_dry_run do |dry_run|
    return true if dry_run and self.storage[name] == status
    resp = container_info(name)
    if resp.nil?
      false
    else
      resp['State'][status.capitalize]
    end
  end
end
container_stop!(name) click to toggle source
# File lib/superhosting/docker_api.rb, line 52
def container_stop!(name)
  self.debug_operation(desc: { code: :container, data: { name: name } }) do |&blk|
    self.with_dry_run do |dry_run|
      self.storage[name] = 'exited' if dry_run
      resp_if_success raw_connection.request(method: :post, path: "/containers/#{name}/stop") unless dry_run
    end
    blk.call(code: :stopped)
  end
end
container_unpause!(name) click to toggle source
# File lib/superhosting/docker_api.rb, line 82
def container_unpause!(name)
  self.debug_operation(desc: { code: :container, data: { name: name } }) do |&blk|
    self.with_dry_run do |dry_run|
      self.storage[name] = 'running' if dry_run
      resp_if_success raw_connection.request(method: :post, path: "/containers/#{name}/unpause") unless dry_run
    end
    blk.call(code: :unpaused)
  end
end
grab_container_options(command_options) click to toggle source
# File lib/superhosting/docker_api.rb, line 185
def grab_container_options(command_options)
  options = []
  AVAILABLE_DOCKER_OPTIONS.map do |k|
    unless (value = command_options[k]).nil?
      value.lines.each {|val| options << "--#{k.to_s.sub('_', '-')} #{val}" }
    end
  end
  options
end
image_info(name) click to toggle source
# File lib/superhosting/docker_api.rb, line 20
def image_info(name)
  resp_if_success raw_connection.request(method: :get, path: "/images/#{name}/json")
end
raw_connection() click to toggle source
# File lib/superhosting/docker_api.rb, line 12
def raw_connection
  Excon.new('unix:///', socket: @socket)
end
resp_if_success(resp) click to toggle source
# File lib/superhosting/docker_api.rb, line 16
def resp_if_success(resp)
  JSON.load(resp.body) if resp.status == 200
end