sle2docker 0.4.2 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 003b2cf7fe50c4f0c0343c9001871408ca40250b
4
- data.tar.gz: 2bcebb608864099eb53090dfed2e27a5299acafc
3
+ metadata.gz: c12ca51c3ec19bc47d2f727a395123d06c18a1df
4
+ data.tar.gz: 39e0763011fe5c1bfc8ef1bb5069ab4bb461d121
5
5
  SHA512:
6
- metadata.gz: 955c42a3fef64be6211b2475866f88e1818df042a86d14b9ef8655b9d8935ba3c57e62c7881ac64d8fb71d09d5e9062844749475b5304b84561efeb537697274
7
- data.tar.gz: 8d9f18f7cc45828eae8354256efd26cab5501eee63a2d90e68b14b835ee11201fbb94ad989a76416fc38f3b52597f487f5d13c4e3559680e9504986674e339c2
6
+ metadata.gz: f8a0761f532912585ed7dc09e32922b7c5646fbbc38cc1b1a1b057658048993a8e14f5e75b68da2dee47e32be2d5fd74ccbad34886ef1032e3edb11348b9dc5c
7
+ data.tar.gz: 1a7880b5a9d09d8510ee7cd39c6b153d1785a33683873c642936c13e950e1c27291f291e7b0eedaf3e3e3e594bec59648b57fa902cd0354a155d48c1c37598b2
data/.gitignore CHANGED
@@ -2,3 +2,4 @@ pkg
2
2
  Vagrantfile
3
3
  package/sle2docker.8
4
4
  package/sle2docker.8.html
5
+ sle2docker-*.gem
@@ -3,3 +3,5 @@ AllCops:
3
3
  - Vagrantfile
4
4
  - sle2docker.gemspec
5
5
  - lib/templates/**/*
6
+ Metrics/MethodLength:
7
+ Max: 15
@@ -1,6 +1,13 @@
1
1
  language: ruby
2
+ sudo: false
2
3
  rvm:
3
- - 2.1.2
4
+ - 2.1.2
5
+ before_install:
6
+ - gem install bundler
7
+ script:
8
+ - bundle exec rake test
9
+ - bundle exec rubocop -V
10
+ - bundle exec rubocop -F
4
11
  addons:
5
12
  code_climate:
6
13
  repo_token:
data/Changelog CHANGED
@@ -1,3 +1,15 @@
1
+ Wed Oct 05 11:05:30 CET 2016 Miquel Sabaté <msabate@suse.com>
2
+
3
+ * Version 0.5.0:
4
+ - Added the --all option to the activate command. With this
5
+ options you can tell sle2docker to activate all the
6
+ available images (see 43558fdcc9a4bc2f66f01ea8e5d9d2734722d0a6).
7
+ - Added the --tag_with_build flag to include the obs build
8
+ number of kiwi generated images in the docker tag
9
+ (see d577bab99b427de339b91182e708eaaab8d9ae87).
10
+ - Clarified error message on missing Docker images
11
+ (see 591ac6c1aa60b089f6a09d608004514a59614882).
12
+
1
13
  Tue Nov 10 17:58:30 CET 2015 Flavio Castelli <fcastelli@suse.com>
2
14
 
3
15
  * Version 0.4.2:
@@ -9,5 +9,7 @@ require 'tmpdir'
9
9
 
10
10
  require_relative 'sle2docker/cli'
11
11
  require_relative 'sle2docker/exceptions'
12
- require_relative 'sle2docker/prebuilt_image'
12
+ require_relative 'sle2docker/image'
13
+ require_relative 'sle2docker/rootfs_image'
14
+ require_relative 'sle2docker/native_image'
13
15
  require_relative 'sle2docker/version'
@@ -1,32 +1,45 @@
1
1
  module Sle2Docker
2
2
  # Entry point of the command line interface
3
3
  class Cli < Thor
4
+ check_unknown_options!
5
+
4
6
  desc 'list', 'List available pre-built images'
5
7
  def list
6
8
  puts 'Available pre-built images:'
7
- prebuilt_images = PrebuiltImage.list
8
- if prebuilt_images.empty?
9
+ images = RootFSImage.list + NativeImage.list
10
+ if images.empty?
9
11
  puts 'No pre-built image found.'
10
12
  puts "\nPre-built images can be installed from SLE12 Update " \
11
13
  'repository using zypper:'
12
14
  puts ' zypper install \"sle*-docker-image\"'
13
15
  else
14
- prebuilt_images.each { |image| puts " - #{image}" }
16
+ images.each { |image| puts " - #{image}" }
15
17
  end
16
18
  end
17
19
 
18
20
  desc 'activate IMAGE_NAME', 'Activate a pre-built image'
19
- def activate(image_name)
20
- ensure_can_access_dockerd
21
+ method_option :all,
22
+ desc: 'Activate all the available pre-built images',
23
+ type: :boolean,
24
+ default: false,
25
+ aliases: '-a'
21
26
 
22
- prebuilt_image = Sle2Docker::PrebuiltImage.new(image_name, options)
23
- if prebuilt_image.activated?
24
- warn 'Image has already been activated. Exiting'
25
- exit(0)
26
- end
27
+ method_option :tag_with_build,
28
+ desc: 'Include kiwi build number into tag',
29
+ type: :boolean,
30
+ default: false,
31
+ aliases: '-b'
27
32
 
28
- prebuilt_image.activate
29
- puts "#{prebuilt_image.image_id} activated"
33
+ def activate(image_name = nil)
34
+ ensure_can_access_dockerd
35
+ if options['all']
36
+ activate_all(options)
37
+ elsif !image_name.nil?
38
+ activate_image(image_name, options)
39
+ else
40
+ puts 'You have to specify an image name.'
41
+ exit 1
42
+ end
30
43
  end
31
44
 
32
45
  map '-v' => :version
@@ -37,10 +50,38 @@ module Sle2Docker
37
50
 
38
51
  private
39
52
 
53
+ def activate_all(options)
54
+ images = RootFSImage
55
+ .list.map { |img| Sle2Docker::RootFSImage.new(img, options) }
56
+ images += NativeImage
57
+ .list.map { |img| Sle2Docker::NativeImage.new(img, options) }
58
+ activate_images(images)
59
+ end
60
+
61
+ def activate_image(image_name, options)
62
+ if RootFSImage.list.include?(image_name)
63
+ activate_images([Sle2Docker::RootFSImage.new(image_name, options)])
64
+ elsif NativeImage.list.include?(image_name)
65
+ activate_images([Sle2Docker::NativeImage.new(image_name, options)])
66
+ else
67
+ puts 'You have to specify an existing image name.'
68
+ exit 1
69
+ end
70
+ end
71
+
40
72
  def ensure_can_access_dockerd
41
- output = `docker info`
42
- if $CHILD_STATUS.exitstatus != 0
43
- raise output
73
+ output = `docker info 2>&1`
74
+ raise output if $CHILD_STATUS.exitstatus.nonzero?
75
+ end
76
+
77
+ def activate_images(images)
78
+ images.each do |image|
79
+ if image.activated?
80
+ warn "Image '#{image.image_id}' has already been activated."
81
+ else
82
+ image.activate
83
+ puts "#{image.image_id} activated"
84
+ end
44
85
  end
45
86
  end
46
87
  end
@@ -5,12 +5,12 @@ module Sle2Docker
5
5
  class ConfigNotFoundError < RuntimeError
6
6
  end
7
7
 
8
- class PrebuiltImageNotFoundError < RuntimeError
8
+ class ImageNotFoundError < RuntimeError
9
9
  end
10
10
 
11
11
  class DockerTagError < RuntimeError
12
12
  end
13
13
 
14
- class PrebuiltImageVerificationError < RuntimeError
14
+ class ImageVerificationError < RuntimeError
15
15
  end
16
16
  end
@@ -0,0 +1,43 @@
1
+ module Sle2Docker
2
+ # This class is the base class of native and prebulid images for
3
+ # SUSE Linux Enterprise
4
+ class Image
5
+ attr_reader :image_id
6
+
7
+ def activated?
8
+ Docker::Image.exist?(image_id)
9
+ end
10
+
11
+ def verify_image
12
+ check_image_exists
13
+
14
+ puts 'Verifying integrity of the pre-built image'
15
+ package_name = rpm_package_name
16
+ verification = `rpm --verify #{package_name}`
17
+ if $CHILD_STATUS.exitstatus.nonzero?
18
+ raise(ImageVerificationError,
19
+ "Verification of #{package_name} failed: #{verification}")
20
+ end
21
+ true
22
+ end
23
+
24
+ def rpm_package_name
25
+ image_file = File.join(self.class::IMAGES_DIR, "#{@image_name}.tar.xz")
26
+ package_name = `rpm -qf #{image_file}`
27
+ if $CHILD_STATUS.exitstatus.nonzero?
28
+ raise(
29
+ ImageVerificationError,
30
+ "Cannot find rpm package providing #{file}: #{package_name}"
31
+ )
32
+ end
33
+ package_name
34
+ end
35
+
36
+ def check_image_exists
37
+ msg = "Cannot find pre-built image #{@image_name}"
38
+ raise(ImageNotFoundError, msg) unless File.exist? File.join(
39
+ self.class::IMAGES_DIR, "#{@image_name}.tar.xz"
40
+ )
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,62 @@
1
+ module Sle2Docker
2
+ # This class takes care of handling the native loadable images for
3
+ # SUSE Linux Enterprise
4
+ class NativeImage < Image
5
+ IMAGES_DIR = '/usr/share/suse-docker-images/native'.freeze
6
+
7
+ def self.list
8
+ if File.exist?(NativeImage::IMAGES_DIR)
9
+ Dir[File.join(NativeImage::IMAGES_DIR, '*.tar.xz')].map do |image|
10
+ File.basename(image, '.tar.xz')
11
+ end
12
+ else
13
+ []
14
+ end
15
+ end
16
+
17
+ def initialize(image_name, options)
18
+ @image_name = image_name
19
+ @options = options
20
+ compute_metadata_file
21
+ end
22
+
23
+ def activate
24
+ verify_image
25
+
26
+ puts 'Loading image'
27
+ Docker::Image.load(File.join(IMAGES_DIR, "#{@image_name}.tar.xz"))
28
+ image = Docker::Image.get(@image_id)
29
+ image.tag('repo' => @repository, 'tag' => 'latest')
30
+ @options['tag_with_build'] &&
31
+ image.tag('repo' => @repository, 'tag' => "#{@tag}-#{@build}")
32
+ end
33
+
34
+ private
35
+
36
+ def compute_metadata_file
37
+ # example of image name and metadata file:
38
+ # kiwi >= 8.30
39
+ # sles12sp3-container.x86_64-2.0.1-Build2.3.docker (image basename)
40
+ # sles12sp3-container.x86_64-2.0.1.metadata
41
+ regexp = /(?<metadata_file>.*\d+\.\d+\.\d+)
42
+ (-Build(?<build>\d+\.\d+)\.docker)?/x
43
+ match = regexp.match(@image_name)
44
+ match.nil? &&
45
+ raise(DockerTagError,
46
+ "Docker image #{@image_name} not found. \
47
+ Run sle2docker list to check which docker images are available.")
48
+ @metadata = parse_metadata_file("#{match['metadata_file']}.metadata")
49
+ @repository = @metadata['image']['name']
50
+ @tag = @metadata['image']['tags'][0]
51
+ @build = match['build']
52
+ @image_id = "#{@repository}:#{@tag}"
53
+ end
54
+
55
+ def parse_metadata_file(metadata)
56
+ file = File.read(
57
+ File.join(NativeImage::IMAGES_DIR, metadata)
58
+ )
59
+ JSON.parse(file)
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,85 @@
1
+ module Sle2Docker
2
+ # This class takes care of handling the pre-build images for
3
+ # SUSE Linux Enterprise
4
+ class RootFSImage < Image
5
+ IMAGES_DIR = '/usr/share/suse-docker-images'.freeze
6
+ DOCKERFILE_TEMPLATE = File.join(
7
+ File.expand_path('../../templates/docker_build', __FILE__),
8
+ 'dockerfile.erb'
9
+ )
10
+
11
+ def self.list
12
+ if File.exist?(RootFSImage::IMAGES_DIR)
13
+ Dir[File.join(RootFSImage::IMAGES_DIR, '*.tar.xz')].map do |image|
14
+ File.basename(image, '.tar.xz')
15
+ end
16
+ else
17
+ []
18
+ end
19
+ end
20
+
21
+ def initialize(image_name, options)
22
+ @image_name = image_name
23
+ @options = options
24
+ compute_repository_and_tag
25
+ end
26
+
27
+ def activate
28
+ verify_image
29
+
30
+ tmp_dir = prepare_docker_build_root
31
+ puts 'Activating image'
32
+ image = Docker::Image.build_from_dir(tmp_dir)
33
+ image.tag('repo' => @repository, 'tag' => @tag)
34
+ image.tag('repo' => @repository, 'tag' => 'latest')
35
+ ensure
36
+ FileUtils.rm_rf(tmp_dir) if tmp_dir && File.exist?(tmp_dir)
37
+ end
38
+
39
+ def prepare_docker_build_root
40
+ tmp_dir = Dir.mktmpdir("sle2docker-#{@image_name}-dockerfile")
41
+
42
+ create_dockerfile(tmp_dir)
43
+ copy_prebuilt_image(tmp_dir)
44
+ tmp_dir
45
+ end
46
+
47
+ def create_dockerfile(tmp_dir)
48
+ prebuilt_image = @image_name + '.tar.xz'
49
+ template = ERB.new(File.read(DOCKERFILE_TEMPLATE), nil, '<>')
50
+ .result(binding)
51
+
52
+ File.open(File.join(tmp_dir, 'Dockerfile'), 'w') do |file|
53
+ file.write(template)
54
+ end
55
+ end
56
+
57
+ def copy_prebuilt_image(tmp_dir)
58
+ prebuilt_image = File.join(IMAGES_DIR, "#{@image_name}.tar.xz")
59
+ destination = File.join(tmp_dir, "#{@image_name}.tar.xz")
60
+ FileUtils.cp(prebuilt_image, destination)
61
+ end
62
+
63
+ private
64
+
65
+ def compute_repository_and_tag
66
+ # example of image name:
67
+ # kiwi < 8.0 :
68
+ # sles12-docker.x86_64-1.0.0-Build7.2
69
+ # kiwi >= 8.0
70
+ # obs-source-service.x86_64-1.0.0-Build3.1.docker
71
+ regexp = /\A(?<name>.*)(-docker)?\..*-(?<version>\d+\.\d+\.\d+)
72
+ (-Build(?<build>\d+\.\d+)?)?/x
73
+ match = regexp.match(@image_name)
74
+ match.nil? &&
75
+ raise(DockerTagError,
76
+ "Docker image #{@image_name} not found. \
77
+ Run sle2docker list to check which docker images are available.")
78
+
79
+ @repository = "suse/#{match['name']}"
80
+ @tag = match['version']
81
+ @options['tag_with_build'] && @tag << '-' + (match['build'] || '0.0')
82
+ @image_id = "#{@repository}:#{@tag}"
83
+ end
84
+ end
85
+ end
@@ -1,5 +1,3 @@
1
- # rubocop:disable Style/Documentation
2
-
3
1
  module Sle2Docker
4
- VERSION = '0.4.2'
2
+ VERSION = '0.5.1'.freeze
5
3
  end
@@ -39,6 +39,10 @@ To activate the pre-built image use the following command:
39
39
 
40
40
  `sle2docker activate IMAGE_NAME`
41
41
 
42
+ To activate all the available pre-built images use the following command:
43
+
44
+ `sle2docker activate --all` or `sle2docker activate -a`
45
+
42
46
  ## Customizing the images
43
47
 
44
48
  To create custom Docker images based on the official ones use
@@ -29,6 +29,7 @@ EOD
29
29
 
30
30
  s.add_runtime_dependency 'docker-api'
31
31
  s.add_runtime_dependency 'thor'
32
+ s.add_runtime_dependency 'json'
32
33
  s.add_development_dependency 'bundler'
33
34
  s.add_development_dependency 'fakefs'
34
35
  s.add_development_dependency 'minitest'
@@ -0,0 +1,15 @@
1
+ require_relative 'test_helper'
2
+
3
+ class CommandTest < MiniTest::Test
4
+ describe 'Wrong flag' do
5
+ it 'returns the proper name of the given flag' do
6
+ argv = ['--flag']
7
+ out, err = capture_io do
8
+ Sle2Docker::Cli.start(argv)
9
+ end
10
+
11
+ assert out.empty?
12
+ assert_equal "Unknown switches '--flag'\n", err
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,98 @@
1
+ require_relative 'test_helper'
2
+
3
+ # rubocop:disable Metrics/BlockLength
4
+ class NativeImageTest < MiniTest::Test
5
+ describe 'NativeImage' do
6
+ after do
7
+ FakeFS::FileSystem.clear
8
+ end
9
+
10
+ describe 'listing' do
11
+ it 'works when no pre-built image is available' do
12
+ actual = Sle2Docker::NativeImage.list
13
+ expected = []
14
+ assert_equal expected, actual
15
+ end
16
+
17
+ it 'lists the names of the available images' do
18
+ FakeFS do
19
+ expected = [
20
+ 'sles11sp3-docker.x86_64-1.0.0-Build1.3',
21
+ 'sles12-docker.x86_64-1.0.0-Build7.2'
22
+ ]
23
+
24
+ FileUtils.mkdir_p(Sle2Docker::NativeImage::IMAGES_DIR)
25
+ expected.each do |image|
26
+ FileUtils.touch(
27
+ File.join(
28
+ Sle2Docker::NativeImage::IMAGES_DIR,
29
+ "#{image}.tar.xz"
30
+ )
31
+ )
32
+ end
33
+
34
+ actual = Sle2Docker::NativeImage.list
35
+ assert_equal expected, actual
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ class NativeImageTest < MiniTest::Test
43
+ describe 'NativeImage' do
44
+ before do
45
+ @options = { tag_with_build: false }
46
+ end
47
+
48
+ describe 'activation' do
49
+ it 'triggers docker load' do
50
+ image_file = '/usr/share/suse-docker-images/native/'\
51
+ 'sles12sp3-container.x86_64-2.0.1-Build2.3.docker.tar.xz'
52
+ File.stubs(:exist?).returns(false)
53
+ File.stubs(:exist?).with(image_file).returns(true)
54
+ Docker::Image.expects(:load).with(image_file).once.returns(true)
55
+ File.stubs(:read).returns(
56
+ '{"image": {"name": "repo", "tags": ["tag1","tag2"]}}'
57
+ )
58
+ mocked_image = mock
59
+ mocked_image.expects(:tag)
60
+ .with('repo' => 'repo', 'tag' => 'latest')
61
+ .once
62
+ Docker::Image.expects(:get).with('repo:tag1').once.returns(mocked_image)
63
+ native_image = Sle2Docker::NativeImage.new(
64
+ 'sles12sp3-container.x86_64-2.0.1-Build2.3.docker',
65
+ @options
66
+ )
67
+ native_image.expects(:verify_image).once
68
+ native_image.activate
69
+ end
70
+
71
+ it 'triggers docker load and tags with build' do
72
+ @options['tag_with_build'] = true
73
+ image_file = '/usr/share/suse-docker-images/native/'\
74
+ 'sles12sp3-container.x86_64-2.0.1-Build2.3.docker.tar.xz'
75
+ File.stubs(:exist?).returns(false)
76
+ File.stubs(:exist?).with(image_file).returns(true)
77
+ Docker::Image.expects(:load).with(image_file).once.returns(true)
78
+ File.stubs(:read).returns(
79
+ '{"image": {"name": "repo", "tags": ["tag1","tag2"]}}'
80
+ )
81
+ mocked_image = mock
82
+ mocked_image.expects(:tag)
83
+ .with('repo' => 'repo', 'tag' => 'latest')
84
+ .once
85
+ mocked_image.expects(:tag)
86
+ .with('repo' => 'repo', 'tag' => 'tag1-2.3')
87
+ .once
88
+ Docker::Image.expects(:get).with('repo:tag1').once.returns(mocked_image)
89
+ native_image = Sle2Docker::NativeImage.new(
90
+ 'sles12sp3-container.x86_64-2.0.1-Build2.3.docker',
91
+ @options
92
+ )
93
+ native_image.expects(:verify_image).once
94
+ native_image.activate
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,158 @@
1
+ require_relative 'test_helper'
2
+
3
+ # rubocop:disable Metrics/LineLength, Metrics/BlockLength
4
+ class RootFSImageTest < MiniTest::Test
5
+ describe 'RootFSImage' do
6
+ before do
7
+ @options = { password: '', tag_with_build: false }
8
+ end
9
+
10
+ after do
11
+ FakeFS::FileSystem.clear
12
+ end
13
+
14
+ describe 'listing' do
15
+ it 'works when no pre-built image is available' do
16
+ actual = Sle2Docker::RootFSImage.list
17
+ expected = []
18
+ assert_equal expected, actual
19
+ end
20
+
21
+ it 'lists the names of the available images' do
22
+ FakeFS do
23
+ expected = [
24
+ 'sles11sp3-docker.x86_64-1.0.0-Build1.3',
25
+ 'sles12-docker.x86_64-1.0.0-Build7.2'
26
+ ]
27
+
28
+ FileUtils.mkdir_p(Sle2Docker::RootFSImage::IMAGES_DIR)
29
+ expected.each do |image|
30
+ FileUtils.touch(
31
+ File.join(
32
+ Sle2Docker::RootFSImage::IMAGES_DIR,
33
+ "#{image}.tar.xz"
34
+ )
35
+ )
36
+ end
37
+
38
+ actual = Sle2Docker::RootFSImage.list
39
+ assert_equal expected, actual
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ # rubocop:disable Style/IndentHeredoc
47
+ class RootFSImageTest < MiniTest::Test
48
+ describe 'RootFSImage' do
49
+ before do
50
+ @options = { password: '', tag_with_build: false }
51
+ end
52
+
53
+ after do
54
+ FakeFS::FileSystem.clear
55
+ end
56
+
57
+ describe 'activation' do
58
+ it 'creates a Dockerfile and builds the image' do
59
+ begin
60
+ image = 'sles12-docker.x86_64-1.0.0-Build7.2'
61
+ prebuilt_image = Sle2Docker::RootFSImage.new(image, @options)
62
+ expected = <<DOCKERFILE
63
+ FROM scratch
64
+ MAINTAINER "Flavio Castelli <fcastelli@suse.com>"
65
+
66
+ ADD sles12-docker.x86_64-1.0.0-Build7.2.tar.xz /
67
+ DOCKERFILE
68
+
69
+ tmp_dir = Dir.mktmpdir('sle2docker-test')
70
+ prebuilt_image.create_dockerfile(tmp_dir)
71
+ dockerfile = File.join(tmp_dir, 'Dockerfile')
72
+
73
+ assert File.exist?(dockerfile)
74
+ assert_equal(expected, File.read(dockerfile))
75
+ ensure
76
+ FileUtils.rm_rf(tmp_dir) if tmp_dir && File.exist?(tmp_dir)
77
+ end
78
+ end
79
+
80
+ it 'triggers docker build' do
81
+ File.stubs(:exist?).returns(true)
82
+ tmp_dir = '/foo'
83
+ mocked_image = mock
84
+ mocked_image.expects(:tag)
85
+ .with('repo' => 'suse/sles12-docker', 'tag' => '1.0.0')
86
+ .once
87
+ mocked_image.expects(:tag)
88
+ .with('repo' => 'suse/sles12-docker', 'tag' => 'latest')
89
+ .once
90
+
91
+ prebuilt_image = Sle2Docker::RootFSImage.new(
92
+ 'sles12-docker.x86_64-1.0.0-Build7.2',
93
+ @options
94
+ )
95
+ img_file = File.join(
96
+ Sle2Docker::RootFSImage::IMAGES_DIR,
97
+ 'sles12-docker.x86_64-1.0.0-Build7.2.tar.xz'
98
+ )
99
+ prebuilt_image.expects(:prepare_docker_build_root).once.returns(tmp_dir)
100
+ prebuilt_image.expects(:`).with("rpm -qf #{img_file}")
101
+ .once.returns('sles12-docker')
102
+ prebuilt_image.expects(:`).with('rpm --verify sles12-docker').once
103
+ Docker::Image.expects(:build_from_dir).with(tmp_dir).once.returns(mocked_image)
104
+ FileUtils.expects(:rm_rf).with(tmp_dir).once
105
+
106
+ prebuilt_image.activate
107
+ end
108
+
109
+ it 'triggers docker build and tags with build #' do
110
+ @options['tag_with_build'] = true
111
+ File.stubs(:exist?).returns(true)
112
+ tmp_dir = '/foo'
113
+ mocked_image = mock
114
+ mocked_image.expects(:tag)
115
+ .with('repo' => 'suse/sles12-docker', 'tag' => '1.0.0-7.2')
116
+ .once
117
+ mocked_image.expects(:tag)
118
+ .with('repo' => 'suse/sles12-docker', 'tag' => 'latest')
119
+ .once
120
+
121
+ prebuilt_image = Sle2Docker::RootFSImage.new(
122
+ 'sles12-docker.x86_64-1.0.0-Build7.2',
123
+ @options
124
+ )
125
+ prebuilt_image.expects(:prepare_docker_build_root).once.returns(tmp_dir)
126
+ prebuilt_image.expects(:verify_image).once
127
+ Docker::Image.expects(:build_from_dir).with(tmp_dir).once.returns(mocked_image)
128
+ FileUtils.expects(:rm_rf).with(tmp_dir).once
129
+
130
+ prebuilt_image.activate
131
+ end
132
+
133
+ it 'triggers docker build and tags with build# (build# empty)' do
134
+ @options['tag_with_build'] = true
135
+ File.stubs(:exist?).returns(true)
136
+ tmp_dir = '/foo'
137
+ mocked_image = mock
138
+ mocked_image.expects(:tag)
139
+ .with('repo' => 'suse/sles12-docker', 'tag' => '1.0.0-0.0')
140
+ .once
141
+ mocked_image.expects(:tag)
142
+ .with('repo' => 'suse/sles12-docker', 'tag' => 'latest')
143
+ .once
144
+
145
+ prebuilt_image = Sle2Docker::RootFSImage.new(
146
+ 'sles12-docker.x86_64-1.0.0-Build',
147
+ @options
148
+ )
149
+ prebuilt_image.expects(:prepare_docker_build_root).once.returns(tmp_dir)
150
+ prebuilt_image.expects(:verify_image).once
151
+ Docker::Image.expects(:build_from_dir).with(tmp_dir).once.returns(mocked_image)
152
+ FileUtils.expects(:rm_rf).with(tmp_dir).once
153
+
154
+ prebuilt_image.activate
155
+ end
156
+ end
157
+ end
158
+ end
@@ -1,5 +1,5 @@
1
- require 'codeclimate-test-reporter'
2
- CodeClimate::TestReporter.start
1
+ require 'simplecov'
2
+ SimpleCov.start
3
3
 
4
4
  require File.expand_path('../../lib/sle2docker', __FILE__)
5
5
 
@@ -7,8 +7,7 @@ require 'minitest/autorun'
7
7
  require 'stringio'
8
8
  require 'fakefs/safe'
9
9
 
10
- # rubocop:disable Style/Documentation, Lint/Eval
11
-
10
+ # rubocop:disable Security/Eval
12
11
  class Object
13
12
  def capture(stream)
14
13
  begin
@@ -22,6 +21,7 @@ class Object
22
21
  result
23
22
  end
24
23
  end
24
+ # rubocop:enable Security/Eval
25
25
 
26
26
  module MiniTest
27
27
  class Test
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sle2docker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Flavio Castelli
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-11 00:00:00.000000000 Z
11
+ date: 2017-10-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: docker-api
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -180,12 +194,16 @@ files:
180
194
  - lib/sle2docker.rb
181
195
  - lib/sle2docker/cli.rb
182
196
  - lib/sle2docker/exceptions.rb
183
- - lib/sle2docker/prebuilt_image.rb
197
+ - lib/sle2docker/image.rb
198
+ - lib/sle2docker/native_image.rb
199
+ - lib/sle2docker/rootfs_image.rb
184
200
  - lib/sle2docker/version.rb
185
201
  - lib/templates/docker_build/dockerfile.erb
186
202
  - package/sle2docker.8.ronn
187
203
  - sle2docker.gemspec
188
- - test/prebuilt_image_test.rb
204
+ - test/command_test.rb
205
+ - test/native_image_test.rb
206
+ - test/rootfs_image_test.rb
189
207
  - test/test_helper.rb
190
208
  homepage: https://github.com/SUSE/sle2docker
191
209
  licenses:
@@ -1,114 +0,0 @@
1
- module Sle2Docker
2
- # This class takes care of handling the pre-build images for
3
- # SUSE Linux Enterprise
4
- class PrebuiltImage
5
- IMAGES_DIR = '/usr/share/suse-docker-images'
6
- DOCKERFILE_TEMPLATE = File.join(
7
- File.expand_path('../../templates/docker_build', __FILE__),
8
- 'dockerfile.erb')
9
-
10
- attr_reader :image_id
11
-
12
- def self.list
13
- if File.exist?(PrebuiltImage::IMAGES_DIR)
14
- Dir[File.join(IMAGES_DIR, '*.tar.xz')].map do |image|
15
- File.basename(image, '.tar.xz')
16
- end
17
- else
18
- []
19
- end
20
- end
21
-
22
- def initialize(image_name, options)
23
- @image_name = image_name
24
- @options = options
25
- compute_repository_and_tag
26
- end
27
-
28
- def activated?
29
- Docker::Image.exist?(image_id)
30
- end
31
-
32
- def activate
33
- unless File.exist?(File.join(IMAGES_DIR, "#{@image_name}.tar.xz"))
34
- fail PrebuiltImageNotFoundError,
35
- "Cannot find pre-built image #{@image_name}"
36
- end
37
-
38
- verify_image
39
-
40
- tmp_dir = prepare_docker_build_root
41
- puts 'Activating image'
42
- image = Docker::Image.build_from_dir(tmp_dir)
43
- image.tag('repo' => @repository, 'tag' => @tag)
44
- image.tag('repo' => @repository, 'tag' => 'latest')
45
- ensure
46
- FileUtils.rm_rf(tmp_dir) if tmp_dir && File.exist?(tmp_dir)
47
- end
48
-
49
- def prepare_docker_build_root
50
- tmp_dir = Dir.mktmpdir("sle2docker-#{@image_name}-dockerfile")
51
-
52
- create_dockerfile(tmp_dir)
53
- copy_prebuilt_image(tmp_dir)
54
- tmp_dir
55
- end
56
-
57
- # rubocop:disable Lint/UselessAssignment
58
- def create_dockerfile(tmp_dir)
59
- prebuilt_image = @image_name + '.tar.xz'
60
-
61
- template = ERB.new(File.read(DOCKERFILE_TEMPLATE), nil, '<>')
62
- .result(binding)
63
-
64
- File.open(File.join(tmp_dir, 'Dockerfile'), 'w') do |file|
65
- file.write(template)
66
- end
67
- end
68
- # rubocop:enable Lint/UselessAssignment
69
-
70
- def copy_prebuilt_image(tmp_dir)
71
- prebuilt_image = File.join(IMAGES_DIR, "#{@image_name}.tar.xz")
72
- destination = File.join(tmp_dir, "#{@image_name}.tar.xz")
73
- FileUtils.cp(prebuilt_image, destination)
74
- end
75
-
76
- def rpm_package_name
77
- file = File.join(IMAGES_DIR, "#{@image_name}.tar.xz")
78
- package_name = `rpm -qf #{file}`
79
- if $CHILD_STATUS.exitstatus != 0
80
- fail PrebuiltImageVerificationError,
81
- "Cannot find rpm package providing #{file}: #{package_name}"
82
- end
83
- package_name
84
- end
85
-
86
- def verify_image
87
- puts 'Verifying integrity of the pre-built image'
88
- package_name = rpm_package_name
89
- verification = `rpm --verify #{package_name}`
90
- if $CHILD_STATUS.exitstatus != 0
91
- fail PrebuiltImageVerificationError,
92
- "Verification of #{package_name} failed: #{verification}"
93
- end
94
- true
95
- end
96
-
97
- private
98
-
99
- def compute_repository_and_tag
100
- # example of image name: sles12-docker.x86_64-1.0.0-Build7.2
101
- regexp = /\A(?<name>.*)-docker\..*-(?<version>\d+\.\d+\.\d+)/
102
- match = regexp.match(@image_name)
103
- if match.nil?
104
- fail DockerTagError,
105
- "Cannot calculate the Docker tag for #{@image_name}"
106
- end
107
-
108
- @repository = "suse/#{match['name']}"
109
- @tag = match['version']
110
- @image_id = "#{@repository}:#{@tag}"
111
- end
112
-
113
- end
114
- end
@@ -1,91 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- # rubocop:disable Metrics/ClassLength, Style/Documentation, Metrics/LineLength, Style/MethodCallParentheses:
4
- class PrebuiltImageTest < MiniTest::Test
5
- describe 'PrebuiltImage' do
6
- before do
7
- @options = { password: '' }
8
- end
9
-
10
- after do
11
- FakeFS::FileSystem.clear
12
- end
13
-
14
- describe 'listing' do
15
- it 'works when no pre-built image is available' do
16
- actual = Sle2Docker::PrebuiltImage.list
17
- expected = []
18
- assert_equal expected, actual
19
- end
20
-
21
- it 'lists the names of the available images' do
22
- FakeFS do
23
- expected = [
24
- 'sles11sp3-docker.x86_64-1.0.0-Build1.3',
25
- 'sles12-docker.x86_64-1.0.0-Build7.2'
26
- ]
27
-
28
- FileUtils.mkdir_p(Sle2Docker::PrebuiltImage::IMAGES_DIR)
29
- expected.each do |image|
30
- FileUtils.touch(
31
- File.join(
32
- Sle2Docker::PrebuiltImage::IMAGES_DIR,
33
- "#{image}.tar.xz"
34
- )
35
- )
36
- end
37
-
38
- actual = Sle2Docker::PrebuiltImage.list
39
- assert_equal expected, actual
40
- end
41
- end
42
- end
43
-
44
- describe 'activation' do
45
- it 'creates a Dockerfile and builds the image' do
46
- begin
47
- image = 'sles12-docker.x86_64-1.0.0-Build7.2'
48
- prebuilt_image = Sle2Docker::PrebuiltImage.new(image, @options)
49
- expected = <<EOF
50
- FROM scratch
51
- MAINTAINER "Flavio Castelli <fcastelli@suse.com>"
52
-
53
- ADD sles12-docker.x86_64-1.0.0-Build7.2.tar.xz /
54
- EOF
55
-
56
- tmp_dir = Dir.mktmpdir('sle2docker-test')
57
- prebuilt_image.create_dockerfile(tmp_dir)
58
- dockerfile = File.join(tmp_dir, 'Dockerfile')
59
-
60
- assert File.exist?(dockerfile)
61
- assert_equal(expected, File.read(dockerfile))
62
- ensure
63
- FileUtils.rm_rf(tmp_dir) if File.exist?(tmp_dir)
64
- end
65
- end
66
-
67
- it 'triggers docker build' do
68
- File.stubs(:exist?).returns(true)
69
- tmp_dir = '/foo'
70
- mocked_image = mock()
71
- mocked_image.expects(:tag)
72
- .with('repo' => 'suse/sles12', 'tag' => '1.0.0')
73
- .once
74
- mocked_image.expects(:tag)
75
- .with('repo' => 'suse/sles12', 'tag' => 'latest')
76
- .once
77
-
78
- prebuilt_image = Sle2Docker::PrebuiltImage.new(
79
- 'sles12-docker.x86_64-1.0.0-Build7.2',
80
- @options
81
- )
82
- prebuilt_image.expects(:prepare_docker_build_root).once.returns(tmp_dir)
83
- prebuilt_image.expects(:verify_image).once
84
- Docker::Image.expects(:build_from_dir).with(tmp_dir).once.returns(mocked_image)
85
- FileUtils.expects(:rm_rf).with(tmp_dir).once
86
-
87
- prebuilt_image.activate
88
- end
89
- end
90
- end
91
- end