From 87abd81d86ae2dd8e9886c71ea3b162bc6a0a612 Mon Sep 17 00:00:00 2001 From: dlorenc Date: Sat, 13 May 2017 09:47:16 -0700 Subject: [PATCH] Add a rule for generating passwd files. --- .../layer.tar | Bin 0 -> 10240 bytes docker/BUILD | 1 + docker/build_test.sh | 18 +++++ docker/contrib/passwd.bzl | 70 ++++++++++++++++++ docker/testdata/BUILD | 17 +++++ 5 files changed, 106 insertions(+) create mode 100644 621fe329a78d65d90d34d6dc277ccac2249bba4c8228222271418bf6b07c4dec/layer.tar create mode 100644 docker/contrib/passwd.bzl diff --git a/621fe329a78d65d90d34d6dc277ccac2249bba4c8228222271418bf6b07c4dec/layer.tar b/621fe329a78d65d90d34d6dc277ccac2249bba4c8228222271418bf6b07c4dec/layer.tar new file mode 100644 index 0000000000000000000000000000000000000000..abe2dbf7e875afabf6b2f0a03ec9ba621192b9d8 GIT binary patch literal 10240 zcmeIx-3o#*6u|La_Y`{q-7*dKHdYe_&2UBe_HFcmbQ2+~i~a{>L*$(KJ7-BMqKERa z&T|n~AFu25-F?5#)KbVi`pJBOlRK7!l(S2Pllyr7-0XzkNsy%WxQ#pm!R4d=Yo+|S z(El%tz^(s>(z)|4R=KzRFN(*t37-n3b*2g-RjdO|^Zxn$uljyd4(4L?GFut{ z&JO#sv(3KhP1=oh-`QO=gsrP>+b*Vn>=8f!0R#|0009ILKmY**5I_I{1Q0*~0R#|0 U009ILKmY**5I_I{1b!)S1xhSMqW}N^ literal 0 HcmV?d00001 diff --git a/docker/BUILD b/docker/BUILD index d325f2a3f..64049c486 100644 --- a/docker/BUILD +++ b/docker/BUILD @@ -40,6 +40,7 @@ TEST_TARGETS = [ "workdir_with_tar_base", "link_with_files_base", "build_with_tag", + "with_passwd", ] TEST_DATA = [ diff --git a/docker/build_test.sh b/docker/build_test.sh index 81b0d1f6b..f4e831565 100755 --- a/docker/build_test.sh +++ b/docker/build_test.sh @@ -547,6 +547,24 @@ function test_build_with_tag() { "[\"gcr.io/build/with:tag\"]" } +function test_build_with_passwd() { + # We should have a tag in our manifest containing the name + # specified via the tag kwarg. + + local layer="621fe329a78d65d90d34d6dc277ccac2249bba4c8228222271418bf6b07c4dec" + check_layers "with_passwd" "${layer}" + + check_eq "$(get_layer_listing "with_passwd" "${layer}")" \ + './ +./etc/ +./etc/passwd' + + local test_data="${TEST_DATA_DIR}/with_passwd.tar" + echo $test_data + passwd_contents=$(tar xOf "${test_data}" "${layer}/layer.tar" | tar xO "./etc/passwd") + check_eq ${passwd_contents} "foobar:x:1234:2345:myusernameinfo:/myhomedir:/myshell" +} + tests=$(grep "^function test_" "${BASH_SOURCE[0]}" \ | cut -d' ' -f 2 | cut -d'(' -f 1) diff --git a/docker/contrib/passwd.bzl b/docker/contrib/passwd.bzl new file mode 100644 index 000000000..257e8bf35 --- /dev/null +++ b/docker/contrib/passwd.bzl @@ -0,0 +1,70 @@ +# Copyright 2017 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar") + +def _impl(ctx): + """Core implementation of docker_push.""" + + f = "%s:x:%s:%s:%s:%s:%s\n" % ( + ctx.attr.username, + ctx.attr.uid, + ctx.attr.gid, + ctx.attr.info, + ctx.attr.home, + ctx.attr.shell + ) + ctx.file_action( + output = ctx.outputs.out, + content = f, + executable=False + ) + build_tar = ctx.executable.build_tar + args = [ + "--output=" + ctx.outputs.tar.path, + "--file=%s=/etc/passwd" % ctx.outputs.out.path + ] + arg_file = ctx.new_file(ctx.attr.name + ".args") + ctx.file_action(arg_file, "\n".join(args)) + + ctx.action( + executable = build_tar, + arguments = ["--flagfile=" + arg_file.path], + inputs = [ctx.outputs.out, arg_file], + outputs = [ctx.outputs.tar], + use_default_shell_env = True + ) + +passwd_file = rule( + attrs = { + "username": attr.string(mandatory = True), + "uid": attr.int(default = 1000), + "gid": attr.int(default = 1000), + "info": attr.string(default = "user"), + "home": attr.string(default = "/home"), + "shell": attr.string(default = "/bin/bash"), + "build_tar": attr.label( + default = Label("@bazel_tools//tools/build_defs/pkg:build_tar"), + cfg = "host", + executable = True, + allow_files = True, + ), + }, + executable = False, + outputs = { + "out": "%{name}.passwd", + "tar": "%{name}.passwd.tar", + }, + implementation = _impl, +) diff --git a/docker/testdata/BUILD b/docker/testdata/BUILD index 1a0691073..435406e54 100644 --- a/docker/testdata/BUILD +++ b/docker/testdata/BUILD @@ -296,3 +296,20 @@ docker_build_with_tag( files = ["foo"], tag = "gcr.io/build/with:tag", ) + +load("//docker/contrib:passwd.bzl", "passwd_file") + +passwd_file( + name = "testpasswd", + gid = 2345, + home = "/myhomedir", + info = "myusernameinfo", + shell = "/myshell", + uid = 1234, + username = "foobar", +) + +docker_build( + name = "with_passwd", + tars = [":testpasswd.passwd.tar"], +)