Class: Sle2Docker::PrebuiltImage

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

Overview

This class takes care of handling the pre-build images for SUSE Linux Enterprise

Constant Summary collapse

IMAGES_DIR =
'/usr/share/suse-docker-images'
DOCKERFILE_TEMPLATE =
File.join(
File.expand_path('../../templates/docker_build', __FILE__),
'dockerfile.erb')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image_name, options) ⇒ PrebuiltImage

Returns a new instance of PrebuiltImage.



22
23
24
25
26
# File 'lib/sle2docker/prebuilt_image.rb', line 22

def initialize(image_name, options)
  @image_name = image_name
  @options    = options
  compute_repository_and_tag
end

Instance Attribute Details

#image_idObject (readonly)

Returns the value of attribute image_id.



10
11
12
# File 'lib/sle2docker/prebuilt_image.rb', line 10

def image_id
  @image_id
end

Class Method Details

.listObject



12
13
14
15
16
17
18
19
20
# File 'lib/sle2docker/prebuilt_image.rb', line 12

def self.list
  if File.exist?(PrebuiltImage::IMAGES_DIR)
    Dir[File.join(IMAGES_DIR, '*.tar.xz')].map do |image|
      File.basename(image, '.tar.xz')
    end
  else
    []
  end
end

Instance Method Details

#activateObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sle2docker/prebuilt_image.rb', line 32

def activate
  unless File.exist?(File.join(IMAGES_DIR, "#{@image_name}.tar.xz"))
    fail PrebuiltImageNotFoundError,
         "Cannot find pre-built image #{@image_name}"
  end

  verify_image

  tmp_dir = prepare_docker_build_root
  puts 'Activating image'
  image = Docker::Image.build_from_dir(tmp_dir)
  image.tag('repo' =>  @repository, 'tag' => @tag)
  image.tag('repo' =>  @repository, 'tag' => 'latest')
ensure
  FileUtils.rm_rf(tmp_dir) if tmp_dir && File.exist?(tmp_dir)
end

#activated?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/sle2docker/prebuilt_image.rb', line 28

def activated?
  Docker::Image.exist?(image_id)
end

#copy_prebuilt_image(tmp_dir) ⇒ Object

rubocop:enable Lint/UselessAssignment



70
71
72
73
74
# File 'lib/sle2docker/prebuilt_image.rb', line 70

def copy_prebuilt_image(tmp_dir)
  prebuilt_image = File.join(IMAGES_DIR, "#{@image_name}.tar.xz")
  destination = File.join(tmp_dir, "#{@image_name}.tar.xz")
  FileUtils.cp(prebuilt_image, destination)
end

#create_dockerfile(tmp_dir) ⇒ Object

rubocop:disable Lint/UselessAssignment



58
59
60
61
62
63
64
65
66
67
# File 'lib/sle2docker/prebuilt_image.rb', line 58

def create_dockerfile(tmp_dir)
  prebuilt_image = @image_name + '.tar.xz'

  template = ERB.new(File.read(DOCKERFILE_TEMPLATE), nil, '<>')
             .result(binding)

  File.open(File.join(tmp_dir, 'Dockerfile'), 'w') do |file|
    file.write(template)
  end
end

#prepare_docker_build_rootObject



49
50
51
52
53
54
55
# File 'lib/sle2docker/prebuilt_image.rb', line 49

def prepare_docker_build_root
  tmp_dir = Dir.mktmpdir("sle2docker-#{@image_name}-dockerfile")

  create_dockerfile(tmp_dir)
  copy_prebuilt_image(tmp_dir)
  tmp_dir
end

#rpm_package_nameObject



76
77
78
79
80
81
82
83
84
# File 'lib/sle2docker/prebuilt_image.rb', line 76

def rpm_package_name
  file = File.join(IMAGES_DIR, "#{@image_name}.tar.xz")
  package_name = `rpm -qf #{file}`
  if $CHILD_STATUS.exitstatus != 0
    fail PrebuiltImageVerificationError,
         "Cannot find rpm package providing #{file}: #{package_name}"
  end
  package_name
end

#verify_imageObject



86
87
88
89
90
91
92
93
94
95
# File 'lib/sle2docker/prebuilt_image.rb', line 86

def verify_image
  puts 'Verifying integrity of the pre-built image'
  package_name = rpm_package_name
  verification = `rpm --verify #{package_name}`
  if $CHILD_STATUS.exitstatus != 0
    fail PrebuiltImageVerificationError,
         "Verification of #{package_name} failed: #{verification}"
  end
  true
end