-
Notifications
You must be signed in to change notification settings - Fork 13
/
docker-clip
executable file
·233 lines (216 loc) · 6.55 KB
/
docker-clip
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/bin/bash
# Docker Client Plugin Manager (CLIP)
#
# (c) 2019 Łukasz Lach
# llach@llach.pl
# https://lach.dev
if [[ "$1" == "docker-cli-plugin-metadata" ]]; then
cat <<CUT
{
"SchemaVersion": "0.1.0",
"Vendor": "Łukasz Lach",
"Version": "v19.06.0",
"ShortDescription": "Docker Client Plugins Manager"
}
CUT
exit
fi
USER_LOCAL_DIR="$HOME/.docker/cli-plugins"
#
# docker clip build
#
cmd_build() {
while getopts ":t:c:" opt; do
case ${opt} in
t ) PLUGIN_TAG=$OPTARG ;;
c ) PLUGIN_COMMAND=$OPTARG ;;
\? )
echo "Invalid option: $OPTARG" 1>&2
break
;;
: )
echo "Invalid option: $OPTARG requires an argument" 1>&2
break
;;
esac
done
shift $((OPTIND -1))
PLUGIN_REPOSITORY=$(echo "$PLUGIN_TAG" | cut -d ':' -f 1)
PLUGIN_CONTEXT="$1"
if [ ! -e "$PLUGIN_CONTEXT/docker-$PLUGIN_COMMAND" ]; then
echo "Error: docker-$PLUGIN_COMMAND file does not exist in the build context"
exit 1
elif [ ! -x "$PLUGIN_CONTEXT/docker-$PLUGIN_COMMAND" ]; then
echo "Error: docker-$PLUGIN_COMMAND file is not executable"
exit 1
elif [ ! -e "$PLUGIN_CONTEXT/docker-$PLUGIN_COMMAND.json" ]; then
echo "Error: docker-$PLUGIN_COMMAND.json file does not exist in the build context"
exit 1
fi
BUILD_FILE=$(mktemp)
cat > "$BUILD_FILE" <<EOF
FROM scratch
LABEL com.docker.clip="1" \
com.docker.clip.tag="${PLUGIN_TAG}" \
com.docker.clip.repository="${PLUGIN_REPOSITORY}" \
com.docker.clip.command="${PLUGIN_COMMAND}" \
com.docker.clip.build_date="$(date +'%F %T')"
COPY . .
EOF
echo "Building $PLUGIN_TAG client plugin"
docker build -q --force-rm --no-cache -t "$PLUGIN_TAG" -f "$BUILD_FILE" "$PLUGIN_CONTEXT"
rm -f "$BUILD_FILE"
echo "Successfully built $PLUGIN_TAG client plugin"
}
#
# docker clip push
#
cmd_push() {
PLUGIN_TAG="$1"
echo "Pushing $PLUGIN_TAG client plugin"
docker push "$PLUGIN_TAG"
}
#
# docker clip add
#
cmd_add() {
PLUGIN_TAG="$1"
PLUGIN_REPOSITORY=$(echo "$PLUGIN_TAG" | cut -d ':' -f 1)
IMAGE_TAG=$(echo "$PLUGIN_TAG:" | cut -d ':' -f 2)
if [ -z "$IMAGE_TAG" ]; then PLUGIN_TAG="$PLUGIN_TAG:latest"; fi
echo "Installing client plugin from $PLUGIN_TAG"
REMOVE_PLUGIN_IMAGE=0
if ! docker images --format '{{.Repository}}:{{.Tag}}' | grep -e "^$PLUGIN_TAG$" &>/dev/null; then
docker pull "$PLUGIN_TAG"
REMOVE_PLUGIN_IMAGE=1
fi
PLUGIN_FLAG=$(docker image inspect --format '{{index .Config.Labels "com.docker.clip"}}' "$PLUGIN_TAG")
PLUGIN_COMMAND=$(docker image inspect --format '{{index .Config.Labels "com.docker.clip.command"}}' "$PLUGIN_TAG")
PLUGIN_BIN="$USER_LOCAL_DIR/docker-$PLUGIN_COMMAND"
if [ "$PLUGIN_FLAG" != "1" ]; then
echo "Error: Image $PLUGIN_TAG is not a valid plugin image"
exit 1
fi
if [ -e "$PLUGIN_BIN" ]; then
echo "Error: $PLUGIN_TAG exports '$PLUGIN_COMMAND' client command that already exists"
exit 1
fi
PLUGIN_LOCAL_DIR="$USER_LOCAL_DIR/.command/$PLUGIN_COMMAND"
rm -rf "$PLUGIN_LOCAL_DIR"
mkdir -p "$PLUGIN_LOCAL_DIR"
IMAGE_DIR=$(mktemp -d)
docker save "$PLUGIN_TAG" | tar -C "$IMAGE_DIR" -x
find "$IMAGE_DIR" -name 'layer.tar' -exec tar -C "$PLUGIN_LOCAL_DIR" -xf {} \;
rm -rf "$IMAGE_DIR"
echo "$PLUGIN_TAG" > "$PLUGIN_LOCAL_DIR/.plugin-image"
if [[ "$REMOVE_PLUGIN_IMAGE" == "1" ]]; then
docker rmi "$PLUGIN_TAG" >/dev/null
fi
# Install command
cat > "$USER_LOCAL_DIR/docker-$PLUGIN_COMMAND" <<EOF
#!/usr/bin/env bash
exec docker clip run "$PLUGIN_COMMAND" "\$@"
EOF
chmod +x "$USER_LOCAL_DIR/docker-$PLUGIN_COMMAND"
echo "Successfully installed $PLUGIN_TAG client plugin"
echo "New client command available: 'docker $PLUGIN_COMMAND'"
}
#
# docker clip update
#
cmd_update() {
PLUGIN_COMMAND="$1"
PLUGIN_LOCAL_DIR="$USER_LOCAL_DIR/.command/$PLUGIN_COMMAND"
PLUGIN_TAG=$(cat "$PLUGIN_LOCAL_DIR/.plugin-image")
echo "Updating the $PLUGIN_COMMAND client command from $PLUGIN_TAG"
cmd_rm "$PLUGIN_COMMAND"
docker rmi "$PLUGIN_TAG" >/dev/null || true
cmd_install "$PLUGIN_TAG"
echo "Successfully updated the $PLUGIN_COMMAND client command"
}
#
# docker clip rm
#
cmd_rm() {
PLUGIN_COMMAND="$1"
if [ -z "$PLUGIN_COMMAND" ]; then
echo "Error: Command name was not specified"
exit 1
fi
PLUGIN_LOCAL_DIR="$USER_LOCAL_DIR/.command/$PLUGIN_COMMAND"
if [ -d "$PLUGIN_LOCAL_DIR" ]; then
rm -rf "$PLUGIN_LOCAL_DIR"
rm -f "$USER_LOCAL_DIR/docker-$PLUGIN_COMMAND"
echo "Successfully removed '$PLUGIN_COMMAND' client command"
else
if [ -e "$USER_LOCAL_DIR/docker-$PLUGIN_COMMAND" ]; then
echo "Error: Client command '$PLUGIN_COMMAND' is not maintained by docker-clip"
exit 1
fi
echo "Error: Client command '$PLUGIN_COMMAND' is not installed"
exit 1
fi
}
#
# docker clip ls
#
cmd_ls() {
printf "%-20s %s\n" COMMAND IMAGE
while read PLUGIN_IMAGE_FILE; do
PLUGIN_TAG=$(cat "$PLUGIN_IMAGE_FILE")
PLUGIN_COMMAND=$(basename $(dirname "$PLUGIN_IMAGE_FILE"))
printf "%-20s %s\n" "$PLUGIN_COMMAND" "$PLUGIN_TAG"
done < <(find "$USER_LOCAL_DIR/.command" -name .plugin-image)
}
#
# docker clip search
#
cmd_search() {
PATTERN="$1"
printf "COMMAND IMAGE DESCRIPTION\n"
curl -sSfL https://raw.githubusercontent.com/lukaszlach/clip/master/clips/catalog | \
grep --color=always -ie "$PATTERN"
exit $?
}
#
# docker clip run
# Used internally only
#
cmd_run() {
PLUGIN_COMMAND="$1"
PLUGIN_LOCAL_DIR="$USER_LOCAL_DIR/.command/$PLUGIN_COMMAND"
shift
if [[ "$1" == "docker-cli-plugin-metadata" ]]; then
cat "$PLUGIN_LOCAL_DIR/docker-$PLUGIN_COMMAND.json"
exit 0
fi
exec "$PLUGIN_LOCAL_DIR/docker-$PLUGIN_COMMAND" "$@"
exit 0
}
usage() {
cat <<EOF
Usage: docker clip COMMAND
Commands:
add Install a new plugin by an image name
build Build a new plugin
ls List installed plugins
push Push local plugin to a Docker registry
rm Remove an installed plugin
search Search plugins from the remote catalog
update Reinstall a plugin to its latest version
EOF
}
shift
COMMAND="$1"
shift
if [ -z "$COMMAND" ] || [ "$COMMAND" == "-h" ] || [ "$COMMAND" == "--help" ]; then
usage
exit 0
fi
set -e
case "$COMMAND" in
build | push | add | update | rm | ls | search | run)
eval "cmd_${COMMAND}" "$@"
;;
esac
exit 0