From 7d17190b1643bf89c7cfe035fa76f0772dfe97ac Mon Sep 17 00:00:00 2001 From: StasPlov Date: Fri, 31 May 2024 04:07:50 +1000 Subject: [PATCH] update: unlock add step --- unlock.sh | 62 +++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 49 insertions(+), 13 deletions(-) diff --git a/unlock.sh b/unlock.sh index 77f1527..4ecd240 100755 --- a/unlock.sh +++ b/unlock.sh @@ -1,15 +1,51 @@ #!/bin/bash -json_content='{ - "registry-mirrors" : [ - "https://mirror.gcr.io", - "https://daocloud.io", - "https://c.163.com", - "https://huecker.io", - "https://registry.docker-cn.com" - ] -}' - -echo "$json_content" | sudo tee /etc/docker/daemon.json > /dev/null - -sudo systemctl restart docker \ No newline at end of file +# New JSON content to add +new_mirrors=( + "https://dockerhub.timeweb.cloud". + "https://mirror.gcr.io" + "https://daocloud.io" + "https://c.163.com" + "https://huecker.io" + "https://registry.docker-cn.com" +) + +if [ -f /etc/docker/daemon.json ]; then + echo "File /etc/docker/daemon.json exists. Updating the content." + current_mirrors=$(grep -oP '(?<="registry-mirrors": \[)[^]]*' /etc/docker/daemon.json | tr -d ' \n' | tr ',' '\n' | tr -d '"') +else + echo "File /etc/docker/daemon.json does not exist. Creating a new file." + current_mirrors="" +fi + +combined_mirrors=$(echo -e "$current_mirrors\n${new_mirrors[*]}" | tr ' ' '\n' | sort | uniq) + +new_content="{\n \"registry-mirrors\": [\n" +for mirror in $combined_mirrors; do + new_content+=" \"$mirror\",\n" +done + +new_content=$(echo -e "$new_content" | sed '$ s/,$//') +new_content=$(echo -e "$new_content" | sed '$ s/,$//') +new_content="${new_content}\n ]\n}" + +echo -e "$new_content" | sudo tee /etc/docker/daemon.json > /dev/null + +# ANSI color codes +GREEN='\033[0;32m' +YELLOW='\033[0;33m' +NOCOLOR='\033[0m' + +while true; do + read -p "Restart Docker now? (y/N): " + if [[ $REPLY =~ ^[Yy]$ ]]; then + sudo systemctl restart docker + echo + echo -e "${GREEN}Docker restarted.${NOCOLOR}" + break + elif [[ $REPLY =~ ^[Nn]$ ]] || [[ -z $REPLY ]]; then + echo + echo -e "${YELLOW}Docker was not restarted. Please restart it manually to apply changes.${NOCOLOR}" + break + fi +done