class Centurion::Service

Attributes

cap_adds[RW]
cap_drops[RW]
command[RW]
cpu_shares[R]
dns[RW]
env_vars[R]
extra_hosts[RW]
image[RW]
memory[R]
name[RW]
network_mode[RW]
port_bindings[RW]
volumes[RW]

Public Class Methods

from_env() click to toggle source
# File lib/centurion/service.rb, line 21
def self.from_env
  Service.new(fetch(:name)).tap do |s|
    s.image = if fetch(:tag, nil)
      "#{fetch(:image, nil)}:#{fetch(:tag)}"
    else
      fetch(:image, nil)
    end

    s.cap_adds      = fetch(:cap_adds, [])
    s.cap_drops     = fetch(:cap_drops, [])
    s.dns           = fetch(:dns, nil)
    s.extra_hosts   = fetch(:extra_hosts, nil)
    s.volumes       = fetch(:binds, [])
    s.port_bindings = fetch(:port_bindings, [])
    s.network_mode  = fetch(:network_mode, 'bridge')
    s.command       = fetch(:command, nil)

    s.add_env_vars(fetch(:env_vars, {}))
  end
end
new(name) click to toggle source
# File lib/centurion/service.rb, line 11
def initialize(name)
  @name          = name
  @env_vars      = {}
  @volumes       = []
  @port_bindings = []
  @cap_adds      = []
  @cap_drops     = []
  @network_mode  = 'bridge'
end

Public Instance Methods

add_env_vars(new_vars) click to toggle source
# File lib/centurion/service.rb, line 42
def add_env_vars(new_vars)
  @env_vars.merge!(new_vars)
end
add_port_bindings(host_port, container_port, type = 'tcp', host_ip = nil) click to toggle source
# File lib/centurion/service.rb, line 46
def add_port_bindings(host_port, container_port, type = 'tcp', host_ip = nil)
  @port_bindings << PortBinding.new(host_port, container_port, type, host_ip)
end
add_volume(host_volume, container_volume) click to toggle source
# File lib/centurion/service.rb, line 50
def add_volume(host_volume, container_volume)
  @volumes << Volume.new(host_volume, container_volume)
end
build_config(server_hostname, &block) click to toggle source
# File lib/centurion/service.rb, line 90
def build_config(server_hostname, &block)
  container_config = {}.tap do |c|
    c['Image'] = image
    c['Hostname'] = block.call(server_hostname) if block_given?
    c['Cmd'] = command if command
    c['Memory'] = memory if memory
    c['CpuShares'] = cpu_shares if cpu_shares
  end

  unless port_bindings.empty?
    container_config['ExposedPorts'] = port_bindings.reduce({}) do |config, binding|
      config["#{binding.container_port}/#{binding.type}"] = {}
      config
    end
  end

  unless env_vars.empty?
    container_config['Env'] = env_vars.map do |k,v|
      "#{k}=#{interpolate_var(v, server_hostname)}"
    end
  end

  unless volumes.empty?
    container_config['Volumes'] = volumes.inject({}) do |memo, v|
      memo[v.container_volume] = {}
      memo
    end
  end

  container_config
end
build_console_config(server_name, &block) click to toggle source
# File lib/centurion/service.rb, line 158
def build_console_config(server_name, &block)
  build_config(server_name, &block).merge({
    'Cmd' => ['/bin/bash'],
    'AttachStdin' => true,
    'Tty'         => true,
    'OpenStdin'   => true,
  })
end
build_host_config(restart_policy = nil) click to toggle source
# File lib/centurion/service.rb, line 122
def build_host_config(restart_policy = nil)
  host_config = {}

  # Set capability additions and drops
  host_config['CapAdd'] = cap_adds if cap_adds
  host_config['CapDrop'] = cap_drops if cap_drops

  # Map some host volumes if needed
  host_config['Binds'] = volume_binds_config if volume_binds_config

  # Bind the ports
  host_config['PortBindings'] = port_bindings_config

  # Set the network mode
  host_config['NetworkMode'] = network_mode

  # DNS if specified
  host_config['Dns'] = dns if dns

  # Add ExtraHosts if needed
  host_config['ExtraHosts'] = extra_hosts if extra_hosts

  # Restart Policy
  if restart_policy
    host_config['RestartPolicy'] = {}

    restart_policy_name = restart_policy.name
    restart_policy_name = 'on-failure' unless ["always", "on-failure", "no"].include?(restart_policy_name)

    host_config['RestartPolicy']['Name'] = restart_policy_name
    host_config['RestartPolicy']['MaximumRetryCount'] = restart_policy.max_retry_count || 10 if restart_policy_name == 'on-failure'
  end

  host_config
end
cap_adds=(capabilites) click to toggle source
# File lib/centurion/service.rb, line 54
def cap_adds=(capabilites)
  unless capabilites.is_a? Array
    raise ArgumentError, "invalid value for capability additions: #{capabilites}, value must be an array"
  end
  @cap_adds = capabilites
end
cap_drops=(capabilites) click to toggle source
# File lib/centurion/service.rb, line 61
def cap_drops=(capabilites)
  unless capabilites.is_a? Array
    raise ArgumentError, "invalid value for capability drops: #{capabilites}, value must be an array"
  end
  @cap_drops = capabilites
end
cpu_shares=(shares) click to toggle source
# File lib/centurion/service.rb, line 79
def cpu_shares=(shares)
  if !shares || !is_a_uint64?(shares)
    raise ArgumentError, "invalid value for cgroup CPU constraint: #{shares}, value must be a between 0 and 18446744073709551615"
  end
  @cpu_shares = shares
end
image=(image) click to toggle source
# File lib/centurion/service.rb, line 86
def image=(image)
  @image = image
end
memory=(bytes) click to toggle source
# File lib/centurion/service.rb, line 72
def memory=(bytes)
  if !bytes || !is_a_uint64?(bytes)
    raise ArgumentError, "invalid value for cgroup memory constraint: #{bytes}, value must be a between 0 and 18446744073709551615"
  end
  @memory = bytes
end
network_mode=(mode) click to toggle source
# File lib/centurion/service.rb, line 68
def network_mode=(mode)
  @network_mode = mode
end
port_bindings_config() click to toggle source
# File lib/centurion/service.rb, line 171
def port_bindings_config
  @port_bindings.inject({}) do |memo, binding|
    config = {}
    config['HostPort'] = binding.host_port.to_s
    config['HostIp'] = binding.host_ip if binding.host_ip
    memo["#{binding.container_port}/#{binding.type}"] = [config]
    memo
  end
end
public_ports() click to toggle source
# File lib/centurion/service.rb, line 181
def public_ports
  @port_bindings.map(&:host_port)
end
volume_binds_config() click to toggle source
# File lib/centurion/service.rb, line 167
def volume_binds_config
  @volumes.map { |volume| "#{volume.host_volume}:#{volume.container_volume}" }
end

Private Instance Methods

host_ip(hostname) click to toggle source
# File lib/centurion/service.rb, line 203
def host_ip(hostname)
  @host_ip ||= {}
  return @host_ip[hostname] if @host_ip.has_key?(hostname)
  @host_ip[hostname] = Socket.getaddrinfo(hostname, nil).first[2]
end
interpolate_var(val, hostname) click to toggle source
# File lib/centurion/service.rb, line 198
def interpolate_var(val, hostname)
  val.to_s.gsub('%DOCKER_HOSTNAME%', hostname)
    .gsub('%DOCKER_HOST_IP%', host_ip(hostname))
end
is_a_uint64?(value) click to toggle source
# File lib/centurion/service.rb, line 187
def is_a_uint64?(value)
  result = false
  if !value.is_a? Integer
    return result
  end
  if value < 0 || value > 0xFFFFFFFFFFFFFFFF
    return result
  end
  return true
end