Class: Dockit::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/dockit/service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = "./Dockit.yaml", locals: {}) ⇒ Service

Returns a new instance of Service.



6
7
8
9
10
11
12
13
14
15
16
# 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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



3
4
5
# File 'lib/dockit/service.rb', line 3

def config
  @config
end

#imageObject (readonly)

Returns the value of attribute image.



4
5
6
# File 'lib/dockit/service.rb', line 4

def image
  @image
end

Instance Method Details

#buildObject



18
19
20
# File 'lib/dockit/service.rb', line 18

def build
  @image = Dockit::Image.create(config.get(:build))
end

#idObject



72
73
74
# File 'lib/dockit/service.rb', line 72

def id
  image.id
end

#nameObject



76
77
78
# File 'lib/dockit/service.rb', line 76

def name
  n = config.get(:create, :name) || config.get(:build, :t).split(':')[0]
end

#pull(registry, tag = nil, force = false) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# 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) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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']  ||= config.get(:build, :t)

  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