-
Notifications
You must be signed in to change notification settings - Fork 697
/
Copy pathimage.bzl
163 lines (142 loc) · 5.18 KB
/
image.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# Copyright 2017 Google Inc. 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.
"""A rule for creating a Node.js container image.
The signature of this rule is compatible with nodejs_binary.
"""
load("@bazel_skylib//lib:dicts.bzl", "dicts")
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary")
load("@build_bazel_rules_nodejs//:providers.bzl", "NodeRuntimeDepsInfo", "NpmPackageInfo")
load(
"//container:container.bzl",
"container_pull",
)
load(
"//lang:image.bzl",
"app_layer",
lang_image = "image",
)
load(
"//repositories:go_repositories.bzl",
_go_deps = "go_deps",
)
# Load the resolved digests.
load(":nodejs.bzl", "DIGESTS")
def repositories():
"""Import the dependencies of the nodejs_image rule.
Call the core "go_deps" function to reduce boilerplate. This is
idempotent if folks call it themselves.
"""
_go_deps()
excludes = native.existing_rules().keys()
if "nodejs_image_base" not in excludes:
container_pull(
name = "nodejs_image_base",
registry = "gcr.io",
repository = "google-appengine/debian9",
digest = DIGESTS["latest"],
)
if "nodejs_debug_image_base" not in excludes:
container_pull(
name = "nodejs_debug_image_base",
registry = "gcr.io",
repository = "google-appengine/debian9",
digest = DIGESTS["debug"],
)
DEFAULT_BASE = select({
"@io_bazel_rules_docker//:debug": "@nodejs_debug_image_base//image",
"@io_bazel_rules_docker//:fastbuild": "@nodejs_image_base//image",
"@io_bazel_rules_docker//:optimized": "@nodejs_image_base//image",
"//conditions:default": "@nodejs_debug_image_base//image",
})
def _runfiles(dep):
return depset(transitive = [dep[DefaultInfo].default_runfiles.files, dep[DefaultInfo].data_runfiles.files, dep.files])
def _emptyfiles(dep):
return depset(transitive = [dep[DefaultInfo].default_runfiles.empty_filenames, dep[DefaultInfo].data_runfiles.empty_filenames])
def _dep_layer_impl(ctx):
return lang_image.implementation(ctx, runfiles = _runfiles, emptyfiles = _emptyfiles)
_dep_layer = rule(
attrs = dicts.add(lang_image.attrs, {
"dep": attr.label(
mandatory = True,
allow_files = True, # override
),
}),
executable = True,
outputs = lang_image.outputs,
toolchains = lang_image.toolchains,
implementation = _dep_layer_impl,
)
def _npm_deps_runfiles(dep):
"""
Collects the npm package sources of all the transitive deps of the dep
"""
depsets = []
for pkg in dep[NodeRuntimeDepsInfo].pkgs:
if NpmPackageInfo in pkg:
depsets.append(pkg[NpmPackageInfo].sources)
return depset(transitive = depsets)
def _npm_deps_layer_impl(ctx):
return lang_image.implementation(ctx, runfiles = _npm_deps_runfiles, emptyfiles = _emptyfiles)
_npm_deps_layer = rule(
attrs = lang_image.attrs,
executable = True,
outputs = lang_image.outputs,
toolchains = lang_image.toolchains,
implementation = _npm_deps_layer_impl,
)
def nodejs_image(
name,
base = None,
data = [],
layers = [],
binary = None,
**kwargs):
"""Constructs a container image wrapping a nodejs_binary target.
Args:
name: Name of the nodejs_image target.
base: Base image to use for the nodejs_image.
data: Runtime dependencies of the nodejs_image.
layers: Augments "deps" with dependencies that should be put into
their own layers.
binary: An alternative binary target to use instead of generating one.
**kwargs: See nodejs_binary.
"""
if not binary:
binary = name + ".binary"
nodejs_binary(
name = binary,
data = data + layers,
**kwargs
)
visibility = kwargs.get("visibility", None)
tags = kwargs.get("tags", None)
# TODO(mattmoor): Consider making the directory into which the app
# is placed configurable.
base = base or DEFAULT_BASE
for index, dep in enumerate(layers):
this_name = "%s.%d" % (name, index)
_dep_layer(name = this_name, base = base, dep = dep, binary = binary, testonly = kwargs.get("testonly"), visibility = visibility, tags = tags)
base = this_name
npm_deps_layer_name = "%s.npm_deps" % name
_npm_deps_layer(name = npm_deps_layer_name, base = base, binary = binary, testonly = kwargs.get("testonly"), visibility = visibility, tags = tags)
app_layer(
name = name,
base = npm_deps_layer_name,
binary = binary,
visibility = visibility,
tags = tags,
args = kwargs.get("args"),
data = data,
testonly = kwargs.get("testonly"),
)