Skip to content

Commit

Permalink
include build number in tag
Browse files Browse the repository at this point in the history
* added new cli option --tag_with_build to include obs build number
  of kiwi generated images in docker tag

Signed-off-by: Frank Schreiner <schreiner@suse.de>
  • Loading branch information
M0ses committed Sep 23, 2016
1 parent dab99fe commit d577bab
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,3 +2,4 @@ pkg
Vagrantfile
package/sle2docker.8
package/sle2docker.8.html
sle2docker-*.gem
9 changes: 8 additions & 1 deletion lib/sle2docker/cli.rb
Expand Up @@ -22,6 +22,13 @@ def list
type: :boolean,
default: false,
aliases: '-a'

method_option :tag_with_build,
desc: 'Include kiwi build number into tag',
type: :boolean,
default: false,
aliases: '-b'

def activate(image_name = nil)
ensure_can_access_dockerd

Expand All @@ -48,7 +55,7 @@ def version

def ensure_can_access_dockerd
output = `docker info`
fail output if $CHILD_STATUS.exitstatus != 0
raise output if $CHILD_STATUS.exitstatus.nonzero?
end

def activate_images(images)
Expand Down
39 changes: 23 additions & 16 deletions lib/sle2docker/prebuilt_image.rb
Expand Up @@ -5,7 +5,8 @@ class PrebuiltImage
IMAGES_DIR = '/usr/share/suse-docker-images'.freeze
DOCKERFILE_TEMPLATE = File.join(
File.expand_path('../../templates/docker_build', __FILE__),
'dockerfile.erb')
'dockerfile.erb'
)

attr_reader :image_id

Expand Down Expand Up @@ -69,17 +70,22 @@ def copy_prebuilt_image(tmp_dir)
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}"
if $CHILD_STATUS.exitstatus.nonzero?
raise(
PrebuiltImageVerificationError,
"Cannot find rpm package providing #{file}: #{package_name}"
)
end
package_name
end

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

def verify_image
Expand All @@ -88,27 +94,28 @@ 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
raise(
PrebuiltImageVerificationError,
"Verification of #{package_name} failed: #{verification}"
) if $CHILD_STATUS.exitstatus.nonzero?
true
end

private

def compute_repository_and_tag
# example of image name: sles12-docker.x86_64-1.0.0-Build7.2
regexp = /\A(?<name>.*)-docker\..*-(?<version>\d+\.\d+\.\d+)/
regexp = /\A(?<name>.*)-docker\..*-(?<version>\d+\.\d+\.\d+)
(-Build(?<build>\d+\.\d+)?)?/x
match = regexp.match(@image_name)
if match.nil?
fail DockerTagError,
"Cannot calculate the Docker tag for #{@image_name}"
end
match.nil? &&
raise(DockerTagError,
"Cannot calculate the Docker tag for #{@image_name}")

@repository = "suse/#{match['name']}"
@tag = match['version']
@image_id = "#{@repository}:#{@tag}"
@options['tag_with_build'] && @tag << '-' + (match['build'] || '0.0')
@image_id = "#{@repository}:#{@tag}"
end
end
end
62 changes: 61 additions & 1 deletion test/prebuilt_image_test.rb
Expand Up @@ -4,7 +4,7 @@
class PrebuiltImageTest < MiniTest::Test
describe 'PrebuiltImage' do
before do
@options = { password: '' }
@options = { password: '', tag_with_build: false }
end

after do
Expand Down Expand Up @@ -40,6 +40,18 @@ class PrebuiltImageTest < MiniTest::Test
end
end
end
end
end

class PrebuiltImageTest < MiniTest::Test
describe 'PrebuiltImage' do
before do
@options = { password: '', tag_with_build: false }
end

after do
FakeFS::FileSystem.clear
end

describe 'activation' do
it 'creates a Dockerfile and builds the image' do
Expand Down Expand Up @@ -86,6 +98,54 @@ class PrebuiltImageTest < MiniTest::Test

prebuilt_image.activate
end

it 'triggers docker build and tags with build #' do
@options['tag_with_build'] = true
File.stubs(:exist?).returns(true)
tmp_dir = '/foo'
mocked_image = mock()
mocked_image.expects(:tag)
.with('repo' => 'suse/sles12', 'tag' => '1.0.0-7.2')
.once
mocked_image.expects(:tag)
.with('repo' => 'suse/sles12', 'tag' => 'latest')
.once

prebuilt_image = Sle2Docker::PrebuiltImage.new(
'sles12-docker.x86_64-1.0.0-Build7.2',
@options
)
prebuilt_image.expects(:prepare_docker_build_root).once.returns(tmp_dir)
prebuilt_image.expects(:verify_image).once
Docker::Image.expects(:build_from_dir).with(tmp_dir).once.returns(mocked_image)
FileUtils.expects(:rm_rf).with(tmp_dir).once

prebuilt_image.activate
end

it 'triggers docker build and tags with build# (build# empty)' do
@options['tag_with_build'] = true
File.stubs(:exist?).returns(true)
tmp_dir = '/foo'
mocked_image = mock()
mocked_image.expects(:tag)
.with('repo' => 'suse/sles12', 'tag' => '1.0.0-0.0')
.once
mocked_image.expects(:tag)
.with('repo' => 'suse/sles12', 'tag' => 'latest')
.once

prebuilt_image = Sle2Docker::PrebuiltImage.new(
'sles12-docker.x86_64-1.0.0-Build',
@options
)
prebuilt_image.expects(:prepare_docker_build_root).once.returns(tmp_dir)
prebuilt_image.expects(:verify_image).once
Docker::Image.expects(:build_from_dir).with(tmp_dir).once.returns(mocked_image)
FileUtils.expects(:rm_rf).with(tmp_dir).once

prebuilt_image.activate
end
end
end
end

0 comments on commit d577bab

Please sign in to comment.