-
Notifications
You must be signed in to change notification settings - Fork 11
/
skinnywhale.sh
executable file
·148 lines (128 loc) · 3.19 KB
/
skinnywhale.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
#!/bin/bash
# Isolate the given top-level docker container
AUFS='/var/lib/docker/aufs/diff'
function usage {
#print usage and exit
echo "usage: skinnywhale.sh <container ID>"
exit 42
}
function error {
#print an error and exit
echo "ERROR: ${@}" 1>&2
exit 42
}
function debug {
[ "${DEBUG}" ] && echo "DEBUG:: ${@}" 1>&2
}
function getFullHash {
#return the full hash for a given container ID
basename $(find ${AUFS} -maxdepth 1 -name ${1}* | grep -v 'init')
}
function mountDiff {
# Copy the diff filesystem to our tmp
mkdir -p ${FS_DIFF}
cp -a ${1}/* ${FS_DIFF}
cd ${FS_DIFF}
rm -Rf var/cache
}
function mountFull {
# Copy the full filesystem to our tmp
mkdir -p ${FS_FULL}
cd ${FS_FULL}
docker export ${1} | tar -x
}
function findDep {
#Find $1 in the full filesystem
if [ -f "${FS_FULL}/${1}" ]
then
echo "${FS_FULL}/${1}"
fi
}
#lets kick this pig
debug "sanity checking"
[ "${1}" ] || usage
SD=$(docker info | grep Storage)
if ! echo ${SD} | grep -q aufs
then
echo "Sorry, docker needs to be running with aufs support"
exit 42
fi
OURTEMP="/tmp/skinnyRun${RANDOM}"
mkdir -p ${OURTEMP}
debug "rundir is ${OURTEMP}"
FS_DIFF=${OURTEMP}/DIFF
FS_FULL=${OURTEMP}/FULL
LAYER=$(getFullHash ${1})
debug "layer is ${LAYER}"
LPATH="${AUFS}/${LAYER}"
debug "LPATH is ${LPATH}"
DEPS=''
#synch our temp dir
mountDiff ${LPATH}
mountFull ${1}
# Ensure the ${FS_DIFF}/user/bin path exists
mkdir -p ${FS_DIFF}/usr/bin
#copy over the linker
LINKER=$(findDep /usr/bin/ldd)
debug "cp -nv ${LINKER} ${FS_DIFF}/usr/bin"
cp -nv ${LINKER} ${FS_DIFF}/usr/bin
#recursively grab the library dependencies for every binary
for FILE in `find ${FS_DIFF} -type f`
do
if file ${FILE} | grep -q 'dynamically linked'
then
debug "processing deps for ${FILE}"
D=$(ldd ${FILE} | sed -e 's/\(^[^/]\+\)\([^ ]\+\).*/\2/' | grep '/')
DEPS=$(printf "%s\n%s" "${DEPS}" "${D}" | sort | uniq)
fi
done
#copy in the library dependencies
debug "DEPENDENCIES is ${DEPS}"
for DEP in ${DEPS}
do
mkdir -p ${FS_DIFF}/$(dirname ${DEP})
SOURCE=$(findDep ${DEP})
if [ -z "${SOURCE}" ]
then
echo "ERROR!!!, can't find ${DEP} in the parent images"
#need an option to panic and die here
else
cp -nv ${SOURCE} ${FS_DIFF}/${DEP}
fi
done
#Copy in the lib dir if BRUTELIB is set
debug "BRUTELIB enabled! Copying over ${FS_FULL}/lib"
if [ "${BRUTELIB}" ]
then
cp -a ${FS_FULL}/lib ${FS_DIFF}
fi
debug "BRUTEUSRLIB enabled! Copying over ${FS_FULL}/lib"
if [ "${BRUTEUSRLIB}" ]
then
cp -a ${FS_FULL}/usr/lib ${FS_DIFF}
fi
#tar it all up and re-dockerize it
cd ${FS_DIFF}
debug "tar -cv . | docker import - skinny_${1}"
tar -c . --exclude=/var/cache | docker import - skinny_${1}
if [ $? -eq 0 ]
then
echo "--- Skinnywhale says: skinny_${1} is positively starving ---"
echo '
## .
## ## ## ==
## ## ## ## ===
/""""""""""""""""\___/ ===
/ rX
~~~{ /\ ~ ~~~ ~~~~ ~~ ~ / ===- ~~~
\______////////////__/
'
else
echo 'OH NOES CRITICAL ERROR. I couldnt write your image sorry'
fi
if [ "${NOCLEANUP}" ]
then
echo "not cleaning up my tempdir because NOCLEANUP is set (${OURTEMP}"
else
rm -Rf ${OURTEMP}
fi