Skip to content

Commit

Permalink
update_deps in Go (#1357)
Browse files Browse the repository at this point in the history
* Bump go-containerregistry

* update_deps in Go

This removes one of two uses of the Python containerregistry

I wanted to use this in a repo of ours that uses rules_docker, and
didn't want anything that needs Python 2, so I thought moving this
to Go was the right approach.

Having a public binary for this would also be super useful for
encouraging folks to use digests instead of tags.

* Fix comment in BUILD file

* Remote tools:update_deps and the related tools/update_deps.py

* Consistent capitalisation of Go error messages

* Unused import
  • Loading branch information
zoidyzoidzoid authored and nlopezgi committed Jan 8, 2020
1 parent d960dff commit ff81e08
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 105 deletions.
23 changes: 17 additions & 6 deletions tools/BUILD → container/go/cmd/update_deps/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,25 @@
# 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.
package(default_visibility = ["//visibility:private"])
# ###
# Synthesize a .bzl file containing the digests for a given repository.

py_binary(
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")

go_binary(
name = "update_deps",
srcs = ["update_deps.py"],
legacy_create_init = False,
python_version = "PY2",
embed = [":go_default_library"],
visibility = ["//visibility:public"],
)

go_library(
name = "go_default_library",
srcs = ["update_deps.go"],
importpath = "github.com/bazelbuild/rules_docker",
visibility = ["//visibility:private"],
deps = [
"@containerregistry",
"//container/go/pkg/compat:go_default_library",
"@com_github_google_go_containerregistry//pkg/crane:go_default_library",
"@com_github_pkg_errors//:go_default_library",
],
)
117 changes: 117 additions & 0 deletions container/go/cmd/update_deps/update_deps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright 2015 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.
//////////////////////////////////////////////////////////////////////
// Synthesize a .bzl file containing the digests for a given repository.

package main

import (
"flag"
"log"
"os"
"strings"
"text/template"
"time"

"github.com/google/go-containerregistry/pkg/crane"
)

const digestTemplate = `# 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.
""" Generated file with dependencies for language rule."""
# !!!! THIS IS A GENERATED FILE TO NOT EDIT IT BY HAND !!!!
#
# To regenerate this file, run ./update_deps.sh from the root of the
# git repository.
DIGESTS = {
# "{{.Debug}}" circa {{.Date}}
"debug": "{{.DebugTag}}",
# "{{.Latest}}" circa {{.Date}}
"latest": "{{.LatestTag}}",
}
`

var (
repository = flag.String("repository", "", "The repository for which to resolve tags.")
output = flag.String("output", "", "The output file to which we write the values.")
)

type Data struct {
DebugTag, LatestTag, Debug, Latest, Date string
}

func main() {
flag.Parse()

if *repository == "" {
log.Fatalln("Required option -repository was not specified.")
}
if *output == "" {
log.Fatalln("Required option -output was not specified.")
}
options := []crane.Option{}

latest := *repository + ":latest"
latestDigest, err := crane.Digest(latest, options...)
if err != nil {
log.Fatalf("Computing digest for %s: %v", latest, err)
}

debug := *repository + ":debug"
debugDigest, err := crane.Digest(debug, options...)
if err != nil {
if !strings.HasPrefix(err.Error(), "MANIFEST_UNKNOWN") {
log.Fatalf("Computing digest for %s: %v", debug, err)
}
debugDigest = latestDigest
}

now := time.Now()
// Jan 2 15:04:05 2006 MST
date := now.Format("2006-01-02 15:04 -0700")

t := template.Must(template.New("digestTemplate").Parse(digestTemplate))

r := Data{
DebugTag: debugDigest,
LatestTag: latestDigest,
Debug: debug,
Latest: latest,
Date: date,
}

f, err := os.Create(*output)
if err != nil {
log.Fatalf("Failed to open file %s: %s", *output, err)
}
defer f.Close()

err = t.Execute(f, r)
if err != nil {
log.Fatalf("Executing template:", err)
}
}
2 changes: 1 addition & 1 deletion repositories/go_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def go_deps():
if "com_github_google_go_containerregistry" not in excludes:
go_repository(
name = "com_github_google_go_containerregistry",
commit = "68bc585818eeca751a710d0f83265e3966a0f56c",
commit = "b02f5c5c90539d88cec4eb4450c3d6f135457942",
importpath = "github.com/google/go-containerregistry",
)
if "com_github_pkg_errors" not in excludes:
Expand Down
90 changes: 0 additions & 90 deletions tools/update_deps.py

This file was deleted.

16 changes: 8 additions & 8 deletions update_deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
set -eu
set -o pipefail

bazel run tools:update_deps -- --repository=gcr.io/distroless/base --output=$PWD/go/go.bzl
bazel run tools:update_deps -- --repository=gcr.io/distroless/static --output=$PWD/go/static.bzl
bazel run tools:update_deps -- --repository=gcr.io/distroless/cc --output=$PWD/cc/cc.bzl
bazel run tools:update_deps -- --repository=gcr.io/distroless/python2.7 --output=$PWD/python/python.bzl
bazel run tools:update_deps -- --repository=gcr.io/distroless/python3 --output=$PWD/python3/python3.bzl
bazel run tools:update_deps -- --repository=gcr.io/distroless/java --output=$PWD/java/java.bzl
bazel run tools:update_deps -- --repository=gcr.io/distroless/java/jetty --output=$PWD/java/jetty.bzl
bazel run tools:update_deps -- --repository=gcr.io/google-appengine/debian9 --output=$PWD/nodejs/nodejs.bzl
bazel run //container/go/cmd/update_deps -- --repository=gcr.io/distroless/base --output=$PWD/go/go.bzl
bazel run //container/go/cmd/update_deps -- --repository=gcr.io/distroless/static --output=$PWD/go/static.bzl
bazel run //container/go/cmd/update_deps -- --repository=gcr.io/distroless/cc --output=$PWD/cc/cc.bzl
bazel run //container/go/cmd/update_deps -- --repository=gcr.io/distroless/python2.7 --output=$PWD/python/python.bzl
bazel run //container/go/cmd/update_deps -- --repository=gcr.io/distroless/python3 --output=$PWD/python3/python3.bzl
bazel run //container/go/cmd/update_deps -- --repository=gcr.io/distroless/java --output=$PWD/java/java.bzl
bazel run //container/go/cmd/update_deps -- --repository=gcr.io/distroless/java/jetty --output=$PWD/java/jetty.bzl
bazel run //container/go/cmd/update_deps -- --repository=gcr.io/google-appengine/debian9 --output=$PWD/nodejs/nodejs.bzl

0 comments on commit ff81e08

Please sign in to comment.