Class: Image

Inherits:
Buildable show all
Defined in:
lib/image.rb

Instance Attribute Summary collapse

Attributes inherited from Buildable

#commit, #name, #repository

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Buildable

#checkout

Constructor Details

#initialize(name:, repository:, commit:, dockerfile:, tag:, registry:) ⇒ Image



10
11
12
13
14
15
# File 'lib/image.rb', line 10

def initialize(name:, repository:, commit:, dockerfile:, tag:, registry:)
  super(name: name, repository: repository, commit: commit)
  @dockerfile = dockerfile
  @tag = tag
  @registry = registry
end

Instance Attribute Details

#dockerfileObject (readonly)

Returns the value of attribute dockerfile.



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

def dockerfile
  @dockerfile
end

#registryObject (readonly)

Returns the value of attribute registry.



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

def registry
  @registry
end

#tagObject

Returns the value of attribute tag.



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

def tag
  @tag
end

Class Method Details

.build_coresObject



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

def self.build_cores
  @build_cores ||= ENV.fetch('R360_BUILD_CORES', [ 1, Concurrent::Utility::ProcessorCounter.new.processor_count - 1 ].max).to_i
end

Instance Method Details

#build!Object



22
23
24
25
26
27
# File 'lib/image.rb', line 22

def build!
  return  if local_exists?
  checkout do |dir|
    system("#{Deployer.docker} build -f #{dockerfile} -t #{local_image} --build-arg JOBS=#{Image.build_cores} --build-arg GIT_VERSION=#{commit} #{dir}")
  end
end

#local_exists?Boolean



53
54
55
56
57
# File 'lib/image.rb', line 53

def local_exists?
  if @local_exists.nil?
    @local_exists = system("#{Deployer.docker} image exists #{local_image}")
  end
end

#local_imageObject



49
50
51
# File 'lib/image.rb', line 49

def local_image
  "#{local_repo}:#{tag}"
end

#local_repoObject



41
42
43
# File 'lib/image.rb', line 41

def local_repo
  name
end

#push!Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/image.rb', line 29

def push!
  return  if remote_exists?

  if Deployer.podman?
    system("#{Deployer.docker} push #{local_image} #{remote_image}")
  else
    system("#{Deployer.docker} tag  #{local_image} #{remote_image}") and
    system("#{Deployer.docker} push #{remote_image}") and
    system("#{Deployer.docker} rmi  #{remote_image}")
  end
end

#remote_exists?Boolean



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/image.rb', line 63

def remote_exists?
  if @remote_exists.nil?
    # this fails to parse the manifest of some images (built with Podman?), and gives warnings on others
    _stdout, stderr, status = Open3.capture3("DOCKER_CLI_EXPERIMENTAL=enabled #{Deployer.docker} manifest inspect #{remote_image}")
    if status.success? || stderr.match(/error\W+parsing manifest blob/i)
      @remote_exists = true
    else
      puts stderr  if stderr.present? && !stderr.include?('manifest unknown') && !stderr.include?('no such manifest')
      @remote_exists = false
    end
  end
  @remote_exists
end

#remote_imageObject



59
60
61
# File 'lib/image.rb', line 59

def remote_image
  "#{remote_repo}:#{tag}"
end

#remote_repoObject



45
46
47
# File 'lib/image.rb', line 45

def remote_repo
  "#{registry}/#{name}"
end