Class: Image
- Inherits:
-
Object
- Object
- Image
- Defined in:
- lib/image.rb
Instance Attribute Summary collapse
-
#commit ⇒ Object
Returns the value of attribute commit.
-
#dockerfile ⇒ Object
readonly
Returns the value of attribute dockerfile.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#registry ⇒ Object
readonly
Returns the value of attribute registry.
-
#repository ⇒ Object
readonly
Returns the value of attribute repository.
-
#tag ⇒ Object
Returns the value of attribute tag.
Class Method Summary collapse
Instance Method Summary collapse
- #build! ⇒ Object
-
#initialize(name:, repository:, dockerfile:, commit:, tag:, registry:) ⇒ Image
constructor
A new instance of Image.
- #local_exists? ⇒ Boolean
- #local_image ⇒ Object
- #local_repo ⇒ Object
- #push! ⇒ Object
- #remote_exists? ⇒ Boolean
- #remote_image ⇒ Object
- #remote_repo ⇒ Object
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
#commit ⇒ Object
Returns the value of attribute commit.
6 7 8 |
# File 'lib/image.rb', line 6 def commit @commit end |
#dockerfile ⇒ Object (readonly)
Returns the value of attribute dockerfile.
6 7 8 |
# File 'lib/image.rb', line 6 def dockerfile @dockerfile end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/image.rb', line 6 def name @name end |
#registry ⇒ Object (readonly)
Returns the value of attribute registry.
6 7 8 |
# File 'lib/image.rb', line 6 def registry @registry end |
#repository ⇒ Object (readonly)
Returns the value of attribute repository.
6 7 8 |
# File 'lib/image.rb', line 6 def repository @repository end |
#tag ⇒ Object
Returns the value of attribute tag.
6 7 8 |
# File 'lib/image.rb', line 6 def tag @tag end |
Class Method Details
.build_cores ⇒ Object
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
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_image ⇒ Object
52 53 54 |
# File 'lib/image.rb', line 52 def local_image "#{local_repo}:#{tag}" end |
#local_repo ⇒ Object
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
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_image ⇒ Object
62 63 64 |
# File 'lib/image.rb', line 62 def remote_image "#{remote_repo}:#{tag}" end |
#remote_repo ⇒ Object
48 49 50 |
# File 'lib/image.rb', line 48 def remote_repo "#{registry}/#{name}" end |