提交 80743f17 编写于 作者: J Jon Mayo

Merge branch 'uninstall' into 'master'

Fix uninstall path

See merge request nvidia/container-toolkit/nvidia-container-runtime!25
......@@ -8,11 +8,14 @@
DOCKERFILE ?= $(CURDIR)/docker/Dockerfile
DOCKERDEVEL ?= $(CURDIR)/docker/Dockerfile.builder
IMAGE ?= nvidia/container-toolkit:docker19.03
BUILDER ?= nvidia/container-toolkit:builder
REGISTRY ?= nvidia
IMAGE ?= $(REGISTRY)/container-toolkit:docker19.03
BUILDER ?= $(REGISTRY)/container-toolkit:builder
##### Public rules #####
all: build
build:
......
#! /bin/bash
# Copyright (c) 2018-2019, NVIDIA CORPORATION. All rights reserved.
# Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# shellcheck disable=SC2015
[ -t 2 ] && readonly LOG_TTY=1 || readonly LOG_NO_TTY=1
if [ "${LOG_TTY-0}" -eq 1 ] && [ "$(tput colors)" -ge 15 ]; then
readonly FMT_CLEAR=$(tput sgr0)
readonly FMT_BOLD=$(tput bold)
readonly FMT_RED=$(tput setaf 1)
readonly FMT_YELLOW=$(tput setaf 3)
readonly FMT_BLUE=$(tput setaf 12)
readonly FMT_CLEAR=$(tput sgr0)
fi
log() {
......
......@@ -5,7 +5,7 @@ readonly DOCKER_CONFIG="/etc/docker/daemon.json"
docker::info() {
local -r docker_socket="${1:-/var/run/docker.sock}"
curl --unix-socket "${docker_socket}" 'http://v1.40/info' | jq '.Runtimes.nvidia.path'
curl --unix-socket "${docker_socket}" 'http://v1.40/info' | jq -r '.Runtimes.nvidia.path'
}
docker::ensure::mounted() {
......@@ -34,8 +34,10 @@ docker::config::backup() {
}
docker::config::restore() {
if [[ -f "${DOCKER_CONFIG}" ]]; then
if [[ -f "${DOCKER_CONFIG}.bak" ]]; then
mv "${DOCKER_CONFIG}.bak" "${DOCKER_CONFIG}"
else
rm "${DOCKER_CONFIG}"
fi
}
......@@ -58,6 +60,11 @@ docker::config::refresh() {
pkill -SIGHUP dockerd
}
docker::config::restart() {
log INFO "restarting the docker daemon"
pkill -SIGTERM dockerd
}
docker::config::get_nvidia_runtime() {
cat - | jq -r '.runtimes | keys[0]'
}
......@@ -72,12 +79,13 @@ docker::setup() {
local -r docker_socket="${2:-"/var/run/docker.socket"}"
local config=$(docker::config)
log INFO "current config: ${config}"
local -r nvidia_runtime="$(with_retry 5 2s docker::info "${docker_socket}")"
# This is a no-op
if [[ "${nvidia_runtime}" = "${destination}/nvidia-container-runtime" ]]; then
log INFO "Noop, docker is arlready setup with the runtime container"
return
fi
......@@ -96,3 +104,11 @@ docker::setup() {
log INFO "after: $(docker::config | jq .)"
docker::config::refresh
}
docker::uninstall() {
docker::config::restore
# Restart docker: this is because docker has a bug where when you
# remove a runtime it doesn't see it until the next restart
docker::config::restart
}
#! /bin/bash
# Copyright (c) 2018-2019, NVIDIA CORPORATION. All rights reserved.
# Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -euxo pipefail
shopt -s lastpipe
......@@ -13,21 +25,54 @@ source "${basedir}/common.sh"
source "${basedir}/toolkit.sh"
source "${basedir}/docker.sh"
UNINSTALL=0
DAEMON=0
main() {
local -r destination="${1:-"${RUN_DIR}"}/toolkit"
local -r docker_socket="${2:-"/var/run/docker.socket"}"
shift 2
while [ $# -gt 0 ]; do
case "$1" in
--no-uninstall)
UNINSTALL=1
shift
;;
--no-daemon)
DAEMON=1
shift
;;
*)
echo "Unknown argument $@"
exit 1
esac
done
toolkit::setup "${destination}"
docker::setup "${destination}" "${docker_socket}"
echo "docker info: $(docker::info "${docker_socket}")"
local uninstall_instructions="toolkit::uninstall ${destination} && docker::uninstall"
if [[ "${UNINSTALL}" -ne 0 ]]; then
uninstall_instructions="true"
fi
if [[ "$DAEMON" -ne 0 ]]; then
# clear other handlers
trap - EXIT
eval "${uninstall_instructions}"
exit 0
fi
echo "Done, now waiting for signal"
sleep infinity &
# shellcheck disable=SC2064
# We want the expand to happen now rather than at trap time
# Setup a new signal handler and reset the EXIT signal handler
trap "echo 'Caught signal'; toolkit::uninstall && { kill $!; exit 0; }" HUP INT QUIT PIPE TERM
trap "echo 'Caught signal'; ${uninstall_instructions} && { kill $!; exit 0; }" HUP INT QUIT PIPE TERM
trap - EXIT
while true; do wait $! || continue; done
exit 0
......
......@@ -31,7 +31,7 @@ toolkit::uninstall() {
log INFO "${FUNCNAME[0]} $*"
if findmnt -r -o TARGET | grep "${destination}" > /dev/null; then
umount -l -R "${destination}"
umount -l -R $(findmnt -r -o TARGET | grep "${destination}")
fi
}
......
#! /bin/bash
# Copyright (c) 2018-2019, NVIDIA CORPORATION. All rights reserved.
# Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -eEux
set -euxo pipefail
shopt -s lastpipe
readonly dind_name="nvidia-container-runtime-installer"
readonly basedir="$(dirname "$(realpath "$0")")"
readonly dind_name="nvidia-container-runtime-dind"
source "${basedir}/../src/common.sh"
# TODO move rm -rf shared to cleanup
testing::cleanup() {
docker run -it --privileged -v "${shared_dir}:/shared" alpine:latest chmod -R 777 /shared
rm -rf "${shared_dir}" || true
......@@ -23,31 +38,58 @@ testing::setup() {
mkdir -p "${shared_dir}"/etc/nvidia-container-runtime
}
testing::main() {
local image="${1:-"nvidia/container-toolkit:docker19.03"}"
testing::setup
testing::dind() {
# Docker creates /etc/docker when starting
# by default there isn't any config in this directory (even after the daemon starts)
docker run --privileged \
-v "${shared_dir}/etc/docker:/etc/docker" \
-v "${shared_dir}/run/nvidia:/run/nvidia:shared" \
--name "${dind_name}" -d docker:stable-dind -H unix://run/nvidia/docker.sock
}
testing::dind::alpine() {
docker exec -it "${dind_name}" sh -c "docker run -it alpine echo foo"
}
testing::toolkit() {
# Share the volumes so that we can edit the config file and point to the new runtime
# Share the pid so that we can ask docker to reload its config
docker run -it --privileged \
--volumes-from "${dind_name}" \
--pid "container:${dind_name}" \
"${image}" \
bash -x -c "/work/run.sh /run/nvidia /run/nvidia/docker.sock"
"${tool_image}" \
bash -x -c "/work/run.sh /run/nvidia /run/nvidia/docker.sock $*"
}
testing::main() {
local -r tool_image="${1:-"nvidia/container-toolkit:docker19.03"}"
testing::setup
testing::dind
testing::toolkit --no-uninstall --no-daemon
# Ensure that we haven't broken non GPU containers
testing::dind::alpine
# Uninstall
testing::toolkit --no-daemon
# ensure that uninstall killed the daemon
sleep 3
[[ "$(docker inspect -f '{{.State.Running}}' $dind_name)" = "false" ]]
docker rm "${dind_name}" || true &> /dev/null
# Restart dind with the backed up configuration
testing::dind
with_retry 5 3s testing::dind::alpine
testing::cleanup
}
readonly shared_dir="${1:-"./shared"}"
shift
shift 1
trap testing::cleanup ERR
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册