| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
# Copyright The containerd Authors. |
| 4 |
|
| 5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
# you may not use this file except in compliance with the License. |
| 7 |
# You may obtain a copy of the License at |
| 8 |
|
| 9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
|
| 11 |
# Unless required by applicable law or agreed to in writing, software |
| 12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
# See the License for the specific language governing permissions and |
| 15 |
# limitations under the License. |
| 16 |
|
| 17 |
set -euo pipefail |
| 18 |
|
| 19 |
REPO=$GOPATH/src/github.com/containerd/stargz-snapshotter |
| 20 |
CONTAINERD_CONFIG_DIR=/etc/containerd/ |
| 21 |
CONTAINERD_ROOT=/var/lib/containerd/ |
| 22 |
REMOTE_SNAPSHOTTER_CONFIG_DIR=/etc/containerd-stargz-grpc/ |
| 23 |
REMOTE_SNAPSHOTTER_ROOT=/var/lib/containerd-stargz-grpc/ |
| 24 |
REMOTE_SNAPSHOTTER_SOCKET=/run/containerd-stargz-grpc/containerd-stargz-grpc.sock |
| 25 |
CNI_CONFIG_DIR=/etc/cni/net.d/ |
| 26 |
|
| 27 |
RETRYNUM=30 |
| 28 |
RETRYINTERVAL=1 |
| 29 |
TIMEOUTSEC=180 |
| 30 |
function retry { |
| 31 |
local SUCCESS=false |
| 32 |
for i in $(seq ${RETRYNUM}) ; do |
| 33 |
if eval "timeout ${TIMEOUTSEC} ${@}" ; then |
| 34 |
SUCCESS=true |
| 35 |
break |
| 36 |
fi |
| 37 |
echo "Fail(${i}). Retrying..." |
| 38 |
sleep ${RETRYINTERVAL} |
| 39 |
done |
| 40 |
if [ "${SUCCESS}" == "true" ] ; then |
| 41 |
return 0 |
| 42 |
else |
| 43 |
return 1 |
| 44 |
fi |
| 45 |
} |
| 46 |
|
| 47 |
function kill_all { |
| 48 |
if [ "${1}" != "" ] ; then |
| 49 |
ps aux | grep "${1}" | grep -v grep | sed -E 's/ +/ /g' | cut -f 2 -d ' ' | xargs -I{} kill -9 {} || true |
| 50 |
fi |
| 51 |
} |
| 52 |
|
| 53 |
function cleanup { |
| 54 |
rm -rf "${CONTAINERD_ROOT}"* |
| 55 |
if [ -f "${REMOTE_SNAPSHOTTER_SOCKET}" ] ; then |
| 56 |
rm "${REMOTE_SNAPSHOTTER_SOCKET}" |
| 57 |
fi |
| 58 |
if [ -d "${REMOTE_SNAPSHOTTER_ROOT}snapshotter/snapshots/" ] ; then |
| 59 |
find "${REMOTE_SNAPSHOTTER_ROOT}snapshotter/snapshots/" \ |
| 60 |
-maxdepth 1 -mindepth 1 -type d -exec umount "{}/fs" \; |
| 61 |
fi |
| 62 |
rm -rf "${REMOTE_SNAPSHOTTER_ROOT}"* |
| 63 |
} |
| 64 |
|
| 65 |
echo "copying config from repo..." |
| 66 |
mkdir -p "${CONTAINERD_CONFIG_DIR}" "${REMOTE_SNAPSHOTTER_CONFIG_DIR}" "${CNI_CONFIG_DIR}" && \ |
| 67 |
cp "${REPO}/script/demo/config.containerd.toml" "${CONTAINERD_CONFIG_DIR}config.toml" && \ |
| 68 |
cp "${REPO}/script/demo/config.stargz.toml" "${REMOTE_SNAPSHOTTER_CONFIG_DIR}config.toml" && \ |
| 69 |
cp "${REPO}/script/demo/config.cni.conflist" "${CNI_CONFIG_DIR}optimizer.conflist" |
| 70 |
|
| 71 |
echo "cleaning up the environment..." |
| 72 |
kill_all "containerd" |
| 73 |
kill_all "containerd-stargz-grpc" |
| 74 |
cleanup |
| 75 |
|
| 76 |
echo "preparing commands..." |
| 77 |
( cd "${REPO}" && PREFIX=/tmp/out/ make clean && \ |
| 78 |
PREFIX=/tmp/out/ make -j2 && \ |
| 79 |
PREFIX=/tmp/out/ make install ) |
| 80 |
|
| 81 |
echo "running remote snaphsotter..." |
| 82 |
containerd-stargz-grpc --log-level=debug \ |
| 83 |
--address="${REMOTE_SNAPSHOTTER_SOCKET}" \ |
| 84 |
--config="${REMOTE_SNAPSHOTTER_CONFIG_DIR}config.toml" & |
| 85 |
retry ls "${REMOTE_SNAPSHOTTER_SOCKET}" |
| 86 |
|
| 87 |
echo "running containerd..." |
| 88 |
containerd --config="${CONTAINERD_CONFIG_DIR}config.toml" $@ & |