diff --git a/setup-docker b/setup-docker index df73e34..07b237a 100755 --- a/setup-docker +++ b/setup-docker @@ -15,41 +15,58 @@ # [1] rootless docker's documentation: https://docs.docker.com/engine/security/rootless/ # [2] CMLab's rootless docker guideline: https://hackmd.io/@snsd0805/SkWMyB1ER -echo -e -n "\033[44m[CML INFO]\033[0m " -echo "Start setting up Docker service" + +# utils functions + +function print_info { + local msg="$1" + echo -e -n "\033[44m[CML INFO]\033[0m " + echo "$msg" +} + +function print_err { + local msg="$1" + echo -e -n "\033[41m[CML INFO]\033[0m " + echo "$msg" +} + +function run_command_check { + local status="$1" + local success_prefix="$2" + local error_prefix="$3" + if [ $status -eq 0 ]; then + print_info "${success_prefix}" + else + print_err "${error_prefix}" + exit 1 + fi +} + +# start +print_info "Start setting up Docker service" # insert the username into /etc/subuid & /etc/subuid # setup-uid's source code is on https://gitea.snsd0805.com/snsd0805/CMLAB-Docker-Builder/src/branch/master/tool/setup_uid.c setup-uid - -if [ $? -eq 0 ]; then - echo -e -n "\033[44m[CML INFO]\033[0m " - echo "Inserted $(whoami) into /etc/subuid & /etc/subgid" -else - echo -e -n "\033[41m[CML ERROR]\033[0m " - echo "failed to set subuid & subgid. Please contact us: unix_manager@cmlab.csie.ntu.edu.tw" - exit 1 -fi - +run_command_check $? "Inserted $(whoami) into /etc/subuid & /etc/subgid" "failed to set subuid & subgid. Please contact us: unix_manager@cmlab.csie.ntu.edu.tw" # Run install script provided by docker -curl -fsSL https://get.docker.com/rootless | DOCKER_BIN=/tmp2/$(whoami)/docker/bin sh -if [ $? -eq 0 ]; then - echo -e -n "\033[44m[CML INFO]\033[0m " - echo "Docker installed." +if [ ! -d "/tmp2/$(whoami)/docker/bin" ]; then + curl -fsSL https://get.docker.com/rootless | DOCKER_BIN=/tmp2/$(whoami)/docker/bin sh + run_command_check $? "Docker installed." "failed to install rootless docker. Please contact us: unix_manager@cmlab.csie.ntu.edu.tw" + export PATH="/tmp2/$(whoami)/docker/bin:$PATH" + export DOCKER_HOST="unix:///run/user/$(id -u)/docker.sock" + echo "export PATH=/tmp2/$(whoami)/docker/bin:\$PATH" >> ~/.bashrc + echo "export DOCKER_HOST=unix:///run/user/$(id -u)/docker.sock" >> ~/.bashrc + print_info "We have add some environment variables in your ~/.bashrc" else - echo -e -n "\033[41m[CML ERROR]\033[0m " - echo "failed to install rootless docker. Please contact us: unix_manager@cmlab.csie.ntu.edu.tw" - exit 1 + print_info "Docker has been installed. We skip this step." fi -echo "export PATH=/tmp2/$(whoami)/docker/bin:\$PATH" >> ~/.bashrc -echo "export DOCKER_HOST=unix:///run/user/$(id -u)/docker.sock" >> ~/.bashrc -echo -e -n "\033[44m[CML INFO]\033[0m " -echo "We have some environment variables in your ~/.bashrc" - # Link NVIDIA Container Runtime to Docker & change docker image's path -mkdir /tmp2/$(whoami)/docker-data +if [ ! -d /tmp2/$(whoami)/docker-data ]; then + mkdir /tmp2/$(whoami)/docker-data +fi cat << EOF > $HOME/.config/docker/daemon.json { "runtimes": { @@ -61,28 +78,25 @@ cat << EOF > $HOME/.config/docker/daemon.json "data-root": "/tmp2/$(whoami)/docker-data" } EOF -if [ $? -eq 0 ]; then - echo -e -n "\033[44m[CML INFO]\033[0m " - echo "Linked NVIDIA Container Runtime & changed the data path." -else - echo -e -n "\033[41m[CML ERROR]\033[0m " - echo "failed to link NVIDIA Container Runtime to Docker. Please contact us: unix_manager@cmlab.csie.ntu.edu.tw" - exit 1 -fi +run_command_check $? "Linked NVIDIA Container Runtime & changed the data path." "failed to link NVIDIA Container Runtime to Docker. Please contact us: unix_manager@cmlab.csie.ntu.edu.tw" # restart docker -echo -e -n "\033[44m[CML INFO]\033[0m " -echo "Restarting Docker..." - +print_info "Restarting Docker & test..." systemctl --user restart docker && docker pull ubuntu && docker run --rm --runtime=nvidia --gpus '"device=0,1"' ubuntu:22.04 nvidia-smi -echo -e -n "\033[44m[CML INFO]\033[0m " -echo "These are 2 GPUs on this server." - - - - - - +print_info "These are 2 GPUs on this server." +print_info "Complete all install steps." +print_info "If you have any problems, please contact us!" +echo +echo 'CMLab Unix Manager' +echo 'CMLab, National Taiwan University' +echo 'Email: unix_manager@cmlab.csie.ntu.edu.tw' +echo ' __ __ _ ' +echo ' / / / /__ (_)_ __ ' +echo ' __ ___/ /_/ / _ \/ /\ \ / ' +echo ' / |/ /\____/_//_/_//_\_\____ ____' +echo ' / /|_/ / _ `/ _ \/ _ `/ _ `/ -_) __/' +echo '/_/ /_/\_,_/_//_/\_,_/\_, /\__/_/ ' +echo ' /___/ '