Class: Argus::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/argus/docker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Image

Returns a new instance of Image.



8
9
10
# File 'lib/argus/docker.rb', line 8

def initialize(name)
  @repo, @branch = name.split(':')
end

Instance Attribute Details

#branchObject (readonly)

Returns the value of attribute branch.



6
7
8
# File 'lib/argus/docker.rb', line 6

def branch
  @branch
end

#build_timeObject (readonly)

Returns the value of attribute build_time.



6
7
8
# File 'lib/argus/docker.rb', line 6

def build_time
  @build_time
end

#imageObject (readonly)

Returns the value of attribute image.



6
7
8
# File 'lib/argus/docker.rb', line 6

def image
  @image
end

#push_timeObject (readonly)

Returns the value of attribute push_time.



6
7
8
# File 'lib/argus/docker.rb', line 6

def push_time
  @push_time
end

#repoObject (readonly)

Returns the value of attribute repo.



6
7
8
# File 'lib/argus/docker.rb', line 6

def repo
  @repo
end

Instance Method Details

#build!Object

build docker image



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/argus/docker.rb', line 32

def build!
  puts "building #{self}"

  @build_time = Benchmark.realtime do
    @image = Docker::Image.build_from_dir('.', dockerfile: 'Dockerfile') do |chunk|
      stream = JSON.parse(chunk)['stream']
      unless (stream.nil? || stream.match(/^[\s\.]+$/)) # very verbose about build progress
        puts stream.chomp
      end
    end
  end
end

#is_ok?Boolean

check if image built ok

Returns:

  • (Boolean)


46
47
48
# File 'lib/argus/docker.rb', line 46

def is_ok?
  image.is_a?(Docker::Image)
end

#pullObject

make a heroic attempt to pre-load as many layers as we can



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/argus/docker.rb', line 17

def pull
  [branch, :master, :latest].each do |tag|
    puts "Attempting to pull #{repo}:#{tag}"
    begin
      attempt = Docker::Image.create(fromImage: "#{repo}:#{tag}")
    rescue Docker::Error::ArgumentError
      puts "failed pull"
    rescue Docker::Error::NotFoundError
      puts "image not found"
    end
    break if attempt.is_a?(Docker::Image)
  end
end

#push(sha) ⇒ Object

push image and all tags to registry



59
60
61
62
63
64
65
66
# File 'lib/argus/docker.rb', line 59

def push(sha)
  @push_time = Benchmark.realtime do
    [sha, branch].each do |tag|
      puts "pushing #{repo}:#{tag}"
      image.push(nil, tag: tag)
    end
  end
end

#tag!(sha) ⇒ Object

tag with both sha and branch



51
52
53
54
55
56
# File 'lib/argus/docker.rb', line 51

def tag!(sha)
  [sha, branch].map do |tag|
    puts "tagging #{repo}:#{tag}"
    image.tag(repo: repo, tag: tag, force: true)
  end
end

#to_sObject



12
13
14
# File 'lib/argus/docker.rb', line 12

def to_s
  "#{repo}:#{branch}"
end