-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmarten
executable file
·152 lines (142 loc) · 3.74 KB
/
marten
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
#!/bin/sh
# marten : Manager of image archives and temporary-containers
set -e -u
MINC=`dirname $0`/minc
LIBEXEC=`dirname $0`/libexec
MINCFARM=$LIBEXEC/minc-farm
MINCSMGL=$LIBEXEC/minc-smuggler
usage() { # [error messages]
test $# -ne 0 && echo "$*"
echo "$0 - Manager of image archives and temporary-containers"
echo "Usage: $0 [action] <command> [argument...]"
echo " commands:"
echo " lc or list List containers"
echo " li or images List images"
echo " rm UUID Remove specified container"
echo " rmi UUID Remove specified image"
echo " pull [USER/]NAME[:TAG] Import a docker image from Dockerhub"
echo " import DIR|IMAGE Import DIR or Dokcer IMAGE as an image"
echo " commit UUID Commit specified container to image"
echo " rename UUID NAME Rename given UUID container to NAME"
echo " renamei UUID NAME Rename given UUID image to NAME"
echo " tag UUID NAME Alias of renamei"
echo " options:"
echo " -h or --help Show this help"
echo " --long Show long ID with li/lc commands"
exit $#
}
do_list() { # command [opts..]
cmd=$1
shift 1
export MINC_LONG_ID=
while [ $# -ne 0 ]; do
case $1 in
--long|-l)
export MINC_LONG_ID=1
;;
esac
shift 1
done
$MINCFARM $cmd
}
import_docker() {
FILE=$1
OPT=
case $FILE in
*tar.gz) OPT="-z" ;;
*tar.bz) OPT="-j" ;;
esac
NAME=`tar $OPT -xf $FILE repositories -O | jq -r "keys[0]" `
IMAGE=`tar $OPT -xf $FILE repositories -O | jq -r ".[] | .[]" `
if [ -z "$NAME" -o -z "$IMAGE" ]; then
usage "$FILE is not a docker image"
fi
echo "Importing image: $NAME"
IDS=
ID=$IMAGE
while [ "$ID" -a "$ID" != "null" ]; do
IDS="$ID $IDS"
ID=`tar $OPT -xf $FILE $ID/json -O | jq -r ".parent"`
done
for id in $IDS; do
tmproot=`mktemp -d`
tar $OPT -xf $FILE $id/layer.tar -O | tar -x -C $tmproot
export MINC_IMPORT_UUID=$id
if [ $id = $IMAGE ]; then
export MINC_IMPORT_NAME=$NAME
fi
$MINCFARM import $tmproot
export MINC_BASE_UUID=$id
rm -rf $tmproot
done
exit 0
}
pull_docker() {
TAG=${1##*:}
NAME=${1%%:*}
USER=${NAME%%/*}
test -z "$NAME" && usage
test -z "$TAG" -o "$TAG" = "$NAME" && TAG=latest
test -z "$USER" -o "$USER" = "$NAME" && NAME="library/$NAME"
echo "Trying to pull $NAME:$TAG"
TMPDIR=`mktemp -d backyard-XXXXXX`
$MINCSMGL $NAME $TAG $TMPDIR
echo "Pulled. Importing image: $NAME"
LASTID=`cat $TMPDIR/manifest.json | jq -r .layers[].digest | tail -n 1`
LASTID=${LASTID##*:}
cat $TMPDIR/manifest.json | jq -r .layers[].digest | while read d; do
tmproot=`mktemp -d $TMPDIR/layer-root-XXXXXX`
ID=${d##*:}
tar --force-local -xf $TMPDIR/$d -C $tmproot
export MINC_IMPORT_UUID=$ID
test "$ID" = "$LASTID" && export MINC_IMPORT_NAME=${NAME##*/}
$MINCFARM import $tmproot
export MINC_BASE_UUID=$ID
rm -rf $tmproot
done
rm -rf $TMPDIR
exit 0
}
test $# -eq 0 && usage
if [ "$1" = "--debug" ] ; then
export MINC_DEBUG=1
set -x
shift 1
fi
test $# -eq 0 && usage
cmd=$1
shift 1
case $cmd in
lc|list|li|images)
do_list $cmd $*
;;
pull)
pull_docker $1
;;
import)
test -d $1 || import_docker $1
$MINCFARM import $1
;;
commit)
$MINCFARM commit $1
;;
rm|rmi)
$MINCFARM $cmd $1
;;
rename|renamei|tag)
test $cmd = "rename" && cmd="dir" || cmd="idir"
UUID=$1
NAME=$2
test -z "$UUID" -o -z "$NAME" && usage "No uuid/name was given."
DIR=`$MINCFARM $cmd $UUID`
if [ -d "$DIR" ]; then
$MINCFARM $cmd $NAME > /dev/null && usage "$NAME is already used."
test $cmd = "idir" && ( echo "$NAME" > $DIR/name ) || \
( echo "$NAME" > $DIR/utsname)
else
usage "$UUID is not found."
fi
;;
*)
usage
esac