-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathdepend.sh
executable file
·165 lines (140 loc) · 3.07 KB
/
depend.sh
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
164
165
#!/bin/bash
usage() {
echo "$0 {-d|-g|-p {tag}|-x} {target image Dockerfile} [known image tags]" >&2
}
find_parent() {
awk '
BEGIN {
ec = 1;
}
$1 == "FROM" && parent {
ec = 2;
exit;
}
$1 == "FROM" {
split($0, a);
parent = $2;
ec = 0
print parent;
}
END {
exit ec
}' "$1"
}
contains() {
needle=$1
shift
echo "$@" | grep -q -E "\<$needle\>"
}
#
# You cannot use a colon (:) in a target or prerequisite name in a
# Makefile. Change colons to @-signs, which docker does not allow as part
# of an name or tag. The Makefile will have to translate them back before
# invoking `docker pull' to fetch the base images.
#
make_friendly_name() {
echo "$1" | sed 's/:/@/g'
}
untag() {
echo "${1%:*}"
}
noop() {
:
}
depfiles_own_image() {
local target_image=$1
local make_friendly_parent=$(make_friendly_name "$2")
local untagged_parent=$(untag "$2")
echo "$target_image@latest: $make_friendly_parent"
echo ".PHONY: $target_image.dependants $untagged_parent.dependants"
echo "$untagged_parent.dependants: $target_image"
echo "$untagged_parent.dependants: $target_image.dependants"
}
depfiles_ext_image() {
local target_image="$1"
local make_friendly_parent=$(make_friendly_name "$2")
echo "$target_image@latest: $make_friendly_parent"
}
list_ext_image() {
local make_friendly_parent=$(make_friendly_name "$2")
echo "$make_friendly_parent"
}
graph_own_image() {
local untagged_parent=$(untag "$2")
cat <<-EOF
"$1" [shape=box]
"$untagged_parent" [shape=box]
"$1" -> "$untagged_parent"
EOF
}
graph_ext_image() {
cat <<-EOF
"$1" [shape=box]
"$2" [shape=house; style=filled; fillcolor="#a0a0a0"]
"$1" -> "$2"
EOF
}
require_parent_tag() {
local target_image=$1
local parent_image=$2
if ! echo "$parent_image" | grep ":${required_parent_tag}\$"; then
echo "FROM in Dockerfile for $target_image must specify a parent with the tag '$required_parent_tag'" >&2
exit 1
fi
}
while getopts ":dgp:x" c; do
case $c in
d)
own_image_function=depfiles_own_image
ext_image_function=depfiles_ext_image
;;
x)
own_image_function=noop
ext_image_function=list_ext_image
;;
g)
own_image_function=graph_own_image
ext_image_function=graph_ext_image
;;
p)
own_image_function=require_parent_tag
required_parent_tag=$OPTARG
ext_image_function=noop
;;
\?)
echo "Unrecognized option -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument" >&2
exit 1
;;
esac
done
shift "$(dc -e"$OPTIND 1 - p")"
if [ -z "$own_image_function" ] || [ $# -lt 2 ]; then
usage
exit 1
fi
target_dockerfile=$1
target_image=$(dirname "$target_dockerfile")
shift
known_images="$*"
parent_image_tag=$(find_parent "$target_dockerfile")
ec=$?
case $ec in
0) ;;
1)
echo "Failed to find a parent docker image in $target_dockerfile" >&2
exit $ec
;;
2)
echo "Found multiple parent docker images in $target_dockerfile" >&2
exit $ec
;;
esac
if contains "$parent_image_tag" "$known_images"; then
$own_image_function "$target_image" "$parent_image_tag"
else
$ext_image_function "$target_image" "$parent_image_tag"
fi