Class: Image

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Image.



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

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

Instance Attribute Details

#commitObject

Returns the value of attribute commit.



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

def commit
  @commit
end

#dockerfileObject (readonly)

Returns the value of attribute dockerfile.



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

def dockerfile
  @dockerfile
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#registryObject (readonly)

Returns the value of attribute registry.



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

def registry
  @registry
end

#repositoryObject (readonly)

Returns the value of attribute repository.



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

def repository
  @repository
end

#tagObject

Returns the value of attribute tag.



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

def tag
  @tag
end

Class Method Details

.build_coresObject



2
3
4
# File 'lib/image.rb', line 2

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



23
24
25
26
27
28
29
30
# File 'lib/image.rb', line 23

def build!
  return  if local_exists?

  Dir.mktmpdir("#{name}-build") do |dir|
    system("git -C #{repository} archive #{commit} | tar -x -C #{dir}") and
    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

Returns:

  • (Boolean)


56
57
58
59
60
# File 'lib/image.rb', line 56

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

#local_imageObject



52
53
54
# File 'lib/image.rb', line 52

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

#local_repoObject



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

def local_repo
  name
end

#push!Object



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

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

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/image.rb', line 66

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.include?('error parsing manifest blob')
      @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



62
63
64
# File 'lib/image.rb', line 62

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

#remote_repoObject



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

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