-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-archive
More file actions
executable file
·203 lines (176 loc) · 4.14 KB
/
docker-archive
File metadata and controls
executable file
·203 lines (176 loc) · 4.14 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/bin/bash
#
# Copyright (c) 2017 Gaël PORTAY
#
# SPDX-License-Identifier: MIT
#
set -e
set -o pipefail
# tarcat($tarfile [taropts])
#
# Concatenate tar to stdout
tarcat() {
tar xOf "$@"
}
# image_get_id($TAR)
#
# Extract images from manifest
manifest_get_images() {
local imageids=
eval "$(tarcat "$TAR" "manifest.json" | \
jq -r '@sh "imageids+=\\ \(.[].RepoTags[])"')"
echo $imageids
}
# manifest_get_config($TAR)
#
# Extract config from manifest
manifest_get_config() {
local key=
eval "$(tarcat "$TAR" "manifest.json" | \
jq -r '@sh "key+=\\ \(.[].Config)"')"
echo $key
}
# manifest_get_repotags($TAR, $n)
#
# Extract config n from manifest
manifest_get_repotags() {
local key=
eval "$(tarcat "$TAR" "manifest.json" | \
jq -r '@sh "key+=\\ \(.['"$1"'].RepoTags[])"')"
echo $key
}
# manifest_get_layers($TAR, $n)
#
# Extract layers n from manifest
manifest_get_layers() {
local key=
eval "$(tarcat "$TAR" "manifest.json" | \
jq -r '@sh "key+=\\ \(.['"$1"'].Layers[])"')"
echo $key
}
# image_get_id($TAR, $repo:tag)
#
# Extract id from repositories
image_get_id() {
local repo="${1%:*}"
local tag="${1##*:}"
local key=
eval "$(tarcat "$TAR" "repositories" | \
jq -r "@sh \"key=\(.\"$repo\".\"$tag\")\"")"
echo $key
}
# config_get_key($TAR, $id, $key)
#
# Extract $key from $id config
config_get_key() {
local key=
eval "$(tarcat "$TAR" "$1.json" | \
jq -r "@sh \"key=\(.\"$2\")\"")"
echo $key
}
# layer_get_key($TAR, $layer, $key)
#
# Extract $key from $layer
layer_get_key() {
local key=
eval "$(tarcat "$TAR" "$1/json" | \
jq -r "@sh \"key=\(.\"$2\")\"")"
echo $key
}
jsoncat() {
if $opt_json; then
echo
tarcat "$@" | jq . | sed -e 's,^,\t,'
echo
fi
}
log() {
local json_file config_files repotags id parent p i j
# Pre-parse to remember existing parents.
json_file="manifest.json"
config_files="$(manifest_get_config)"
declare -A parents
i=0
for config in $config_files; do
repotags="$(manifest_get_repotags "$i")"
for repotag in $repotags; do
id="$(image_get_id "$repotag")"
parent="$(layer_get_key "$id" "parent")"
parents[$parent]="$repotag"
parents[$id]="$repotag"
done
i="$((i+1))"
done
# Display manifest
echo -e "\e[33m$1 (\e[31;1m${config_files/ /, }\e[0;33m)\e[0m"
for key in "${!parents[@]}"; do
echo "${key:0:12}: ${parents[$key]}"
done
# Display json manifest content
jsoncat "$1" "$json_file"
# Display every manifest's config
i=0
for config in $config_files; do
repotags="$(manifest_get_repotags "$i")"
# Display every config's repository:tag
for repotag in $repotags; do
id="$(image_get_id "$repotag")"
json_file="$id/json"
# Display config
echo -e "\e[33m${config:0:12} \e[36;1m$repotag -> \e[32m$json_file\e[0m"
echo -n "Version: "; tarcat "$TAR" "$id/VERSION" && echo
echo "Id: ${id:0:12}"
parent="$id"
j=0
while parent="$(layer_get_key "$parent" "parent")" &&
[ "$parent" != "null" ]; do
echo -n "Parent-$((++j)): ${parent:0:12}"
p="${parents[$parent]}"
if [ -z "$p" ] || [ "$p" = "$repotag" ]; then
echo ""
else
echo " -> $p"
fi
done
echo -n "Created: "; tarcat "$TAR" "$json_file" | jq -r ".created" | xargs date -R --date
echo -n "Container: "; tarcat "$TAR" "$json_file" | jq -r ".container"
echo -n "Docker-version: "; tarcat "$TAR" "$json_file" | jq -r ".docker_version"
# Display json content
jsoncat "$TAR" "$json_file"
done
i="$((i+1))"
done
}
usage() {
cat <<EOF
Usage: ${0##*/} [OPTIONS] TAR...
Display à la git-log the content of docker archive to stdout.
Options:
-j or --json Display json content.
-h or --help Print usage.
EOF
}
parse_arguments() {
tar_files=()
opt_json=false
while [ "$#" -ne 0 ]; do
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
usage
exit 0
elif [ "$1" = "-j" ] || [ "$1" = "--json" ]; then
opt_json=true
else
tar_files+=( "$1" )
fi
shift
done
}
parse_arguments "$@"
if [ "${#tar_files}" -eq 0 ]; then
usage
echo "Too few arguments!" >&2
exit 1
fi
for TAR in "${tar_files[@]}"; do
log "$TAR"
done | ${PAGER:-less -r}