|
| 1 | +# Copyright 2017 Google, Inc. All rights reserved. |
| 2 | + |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | + |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +def _impl(ctx): |
| 16 | + """Implementation of docker_diff""" |
| 17 | + |
| 18 | + container_diff_loction = ctx.executable._container_diff.short_path |
| 19 | + image_location = ctx.file.image.short_path |
| 20 | + |
| 21 | + # Shell script to execute container-diff with appropriate flags |
| 22 | + content = """\ |
| 23 | +#!/bin/bash |
| 24 | +set -e |
| 25 | +./%s diff %s %s""" % (container_diff_loction, image_location, ctx.attr.diff_base) |
| 26 | + if ctx.attr.diff_types: |
| 27 | + content += " --types=%s" % (",".join(ctx.attr.diff_types)) |
| 28 | + |
| 29 | + ctx.file_action( |
| 30 | + output = ctx.outputs.executable, |
| 31 | + content = content, |
| 32 | + ) |
| 33 | + |
| 34 | + return struct(runfiles=ctx.runfiles( |
| 35 | + files = [ |
| 36 | + ctx.executable._container_diff, |
| 37 | + ctx.file.image |
| 38 | + ]), |
| 39 | + ) |
| 40 | + |
| 41 | +# Diff a bazel image against an image in production with bazel run |
| 42 | +# Runs container-diff on the two images and prints the output. |
| 43 | +# Args: |
| 44 | +# name: name of the rule |
| 45 | +# image: bazel target to an image you have bazel built (must be a tar) |
| 46 | +# ex: image = "@//target/to:image.tar" |
| 47 | +# diff_base: Tag or digest in a remote registry you want to diff against |
| 48 | +# ex: diff_base = "gcr.io/google-appengine/debian8:latest" |
| 49 | +# diff_types: Types flag to pass to container diff |
| 50 | +# ex: ["pip", "file"] |
| 51 | + |
| 52 | +docker_diff = rule( |
| 53 | + attrs = { |
| 54 | + "image": attr.label( |
| 55 | + allow_files = [".tar"], |
| 56 | + single_file = True, |
| 57 | + mandatory = True, |
| 58 | + ), |
| 59 | + "diff_base": attr.string(mandatory = True), |
| 60 | + "diff_types": attr.string_list( |
| 61 | + allow_empty = True, |
| 62 | + ), |
| 63 | + "_container_diff": attr.label( |
| 64 | + default = Label("//:container-diff"), |
| 65 | + executable = True, |
| 66 | + cfg = "host", |
| 67 | + ), |
| 68 | + }, |
| 69 | + implementation = _impl, |
| 70 | + executable = True, |
| 71 | +) |
0 commit comments