class Dockit::Service

Attributes

config[R]
image[R]

Public Class Methods

new(file="./Dockit.yaml", locals: {}) click to toggle source
# File lib/dockit/service.rb, line 6
def initialize(file="./Dockit.yaml", locals: {})
  @config = Dockit::Config.new(file, locals)

  # get the image if it is specified and already exists
  if name = config.get(:create, :Image) || config.get(:build, :t)
    begin
      @image = Dockit::Image.get(name)
    rescue Docker::Error::NotFoundError
    end
  end
end

Public Instance Methods

build() click to toggle source
# File lib/dockit/service.rb, line 18
def build
  @image = Dockit::Image.create(config.get(:build))
end
id() click to toggle source
# File lib/dockit/service.rb, line 72
def id
  image.id
end
name() click to toggle source
# File lib/dockit/service.rb, line 76
def name
  (
    config.get(:create, :name) || config.get(:build, :t).split(':')[0]
  ).gsub(/[^a-zA-Z0-9_.-]/, '-')
end
pull(registry, tag=nil, force=false) click to toggle source
# File lib/dockit/service.rb, line 57
def pull(registry, tag=nil, force=false)
  unless repo = config.get(:build, 't')
    STDERR.puts "No such locally built image"
    exit 1
  end

  name = "#{registry}/#{repo}"
  image = Docker::Image.create(fromImage: name) do |chunk|
    Dockit::Log::print_chunk(chunk)
  end

  puts "Tagging #{name} as #{repo}:#{tag||'latest'}"
  image.tag(repo: repo, tag: tag, force: force)
end
push(registry, tag=nil, force=false) click to toggle source
# File lib/dockit/service.rb, line 44
def push(registry, tag=nil, force=false)
  raise "No image found!" unless image

  image.tag(repo: "#{registry}/#{config.get(:build, 't')}", force: force)
  STDOUT.sync = true
  image.push(tag: tag) do |chunk|
    chunk = JSON.parse(chunk)
    progress = chunk['progress']
    id = progress ? '' : "#{chunk['id']} "
    print chunk['status'], ' ', id, progress, progress ? "\r" : "\n"
  end
end
start(options) click to toggle source
# File lib/dockit/service.rb, line 22
def start(options)
  opts = merge_config(:create, stringify(options[:create]))
  unless image || opts['Image']
    raise "No runnable image found or specified!"
  end

  opts['Image'] ||= image.id if image
  opts['name']  ||= name

  run = merge_config(:run, stringify(options[:run]))

  if options[:verbose]
    cmd = [(opts['Entrypoint']||[]), ((opts['Cmd'] || %w[default]))].flatten
    puts " * %s (%s)" % [ opts['name'] || 'unnamed', cmd.join(' ') ]

    puts " * #{run}" if run.length > 0
  end

  Dockit::Container.new(opts).start(
    run, verbose: options[:verbose], transient: options[:transient])
end

Private Instance Methods

merge_config(key, opts) click to toggle source
# File lib/dockit/service.rb, line 83
def merge_config(key, opts)
  (config.get(key) || {}).merge(opts||{})
end
stringify(hash) click to toggle source
# File lib/dockit/service.rb, line 87
def stringify(hash)
  return nil unless hash
  Hash[hash.map {|k,v| [k.to_s, v]}]
end