class Construi::Image

A Docker Image

Public Class Methods

build(build) click to toggle source
# File lib/construi/image.rb, line 101
def self.build(build)
  Console.progress "Building image: '#{build}'..."

  wrap Docker::Image.build_from_dir(build, rm: 0) { |s|
    Console.output build, JSON.parse(s)['stream']
  }
end
create(image) click to toggle source
# File lib/construi/image.rb, line 83
def self.create(image)
  Console.progress "Creating image: '#{image}'..."

  wrap Docker::Image.create('fromImage' => image) { |s|
    status = JSON.parse(s)

    id = status['id']
    progress = status['progressDetail']

    if progress.nil? || progress.empty?
      msg = ''
      msg << "#{id}: " unless id.nil?
      msg << status['status']
      Console.output image, msg
    end
  }
end
from(config) click to toggle source
# File lib/construi/image.rb, line 74
def self.from(config)
  image = create(config.image) unless config.image.nil?
  image = build(config.build) unless config.build.nil?

  fail Error, "Invalid image configuration: #{config}" unless image

  image.insert_locals config.files, privileged: config.privileged?
end
new(image) click to toggle source
# File lib/construi/image.rb, line 11
def initialize(image)
  @image = image.refresh!
end
wrap(image) click to toggle source
# File lib/construi/image.rb, line 109
def self.wrap(image)
  new image
end

Public Instance Methods

==(other) click to toggle source
# File lib/construi/image.rb, line 70
def ==(other)
  other.is_a?(Image) && id == other.id
end
chmod(file, permissions, options = {}) click to toggle source
# File lib/construi/image.rb, line 55
def chmod(file, permissions, options = {})
  chmod = "chmod -R #{permissions} #{file}"

  puts " > #{chmod}"
  run chmod, options
end
delete() click to toggle source
# File lib/construi/image.rb, line 19
def delete
  @image.delete
end
docker_image() click to toggle source
# File lib/construi/image.rb, line 23
def docker_image
  @image
end
id() click to toggle source
# File lib/construi/image.rb, line 15
def id
  @image.id
end
insert_local(file, options = {}) click to toggle source
# File lib/construi/image.rb, line 31
def insert_local(file, options = {})
  puts "\nCopying #{file.host} to #{file.container}...".green

  img = IntermediateImage.seed(self)

  img.map do |i|
    Image.wrap i.docker_image
      .insert_local 'localPath' => file.host, 'outputPath' => file.container
  end

  img.map { |i| i.chmod file.container, file.permissions, options } if file.permissions

  img.run "ls -l #{file.container}", options

  img.image
end
insert_locals(files, options = {}) click to toggle source
# File lib/construi/image.rb, line 48
def insert_locals(files, options = {})
  IntermediateImage
    .seed(self)
    .reduce(files) { |a, e| a.insert_local e, options }
    .image
end
run(cmd, options = {}) click to toggle source
# File lib/construi/image.rb, line 66
def run(cmd, options = {})
  Container.run self, options.merge(cmd: cmd)
end
start(options = {}) click to toggle source
# File lib/construi/image.rb, line 62
def start(options = {})
  Container.create(self, options).tap(&:start)
end
tagged?() click to toggle source
# File lib/construi/image.rb, line 27
def tagged?
  @image.info['RepoTags'] != '<none>:<none>'
end