Fix containerd configuration to be applied only if there is docker proxy
[osm/devops.git] / installers / install_docker_ce.sh
index 9d6387f..5c0cc6d 100755 (executable)
@@ -22,7 +22,7 @@ function install_docker_ce() {
     sudo apt-get -qq update
     sudo apt-get install -y apt-transport-https ca-certificates software-properties-common
     curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE=1 apt-key add -
-    sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
+    sudo add-apt-repository -y "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
     sudo apt-get -qq update
     sudo apt-get install -y docker-ce
 #    echo "Reconfiguring Docker to use systemd as cgroup driver"
@@ -64,12 +64,41 @@ function install_docker_ce() {
 }
 EOF"
         fi
+    fi
+    if [ -n "${OSM_BEHIND_PROXY}" ] ; then
+        if ! [ -f /etc/systemd/system/docker.service.d/http-proxy.conf ] ; then
+            sudo mkdir -p /etc/systemd/system/docker.service.d
+            cat << EOF | sudo tee -a /etc/systemd/system/docker.service.d/http-proxy.conf
+[Service]
+EOF
+        fi
+        [ -n "${HTTP_PROXY}" ] && sudo bash -c "cat <<EOF >> /etc/systemd/system/docker.service.d/http-proxy.conf
+Environment=\"HTTP_PROXY=${HTTP_PROXY}\"
+EOF"
+        [ -n "${HTTPS_PROXY}" ] && sudo bash -c "cat <<EOF >> /etc/systemd/system/docker.service.d/http-proxy.conf
+Environment=\"HTTPS_PROXY=${HTTPS_PROXY}\"
+EOF"
+        [ -n "${NO_PROXY}" ] && sudo bash -c "cat <<EOF >> /etc/systemd/system/docker.service.d/http-proxy.conf
+Environment=\"NO_PROXY=${NO_PROXY}\"
+EOF"
+    fi
+    if [ -n "${DOCKER_PROXY_URL}" ] || [ -n "${OSM_BEHIND_PROXY}" ] ; then
         #sudo systemctl enable docker
         sudo systemctl daemon-reload
         #sudo systemctl restart docker
         sudo service docker restart
         echo "... restarted Docker service again"
     fi
+
+    if [ -n "${DOCKER_PROXY_URL}" ]; then
+        echo "Configuring containerd"
+        sudo mv /etc/containerd/config.toml /etc/containerd/config.toml.orig 2>/dev/null
+        sudo bash -c "containerd config default > /etc/containerd/config.toml"
+        sudo sed -i "s#\[plugins.\"io.containerd.grpc.v1.cri\".registry.mirrors\]#\[plugins.\"io.containerd.grpc.v1.cri\".registry.mirrors\]\n        \[plugins.\"io.containerd.grpc.v1.cri\".registry.mirrors.\"docker.io\"\]\n          endpoint = \[\"${DOCKER_PROXY_URL}\"\]\n        \[plugins.\"io.containerd.grpc.v1.cri\".registry.mirrors.\"registry.hub.docker.com\"]\n          endpoint = \[\"${DOCKER_PROXY_URL}\"]#" /etc/containerd/config.toml
+        sudo diff /etc/containerd/config.toml.orig /etc/containerd/config.toml
+        sudo service containerd restart
+    fi
+
     [ -z "${DEBUG_INSTALL}" ] || ! echo "File: /etc/docker/daemon.json" || cat /etc/docker/daemon.json
     sg docker -c "docker version" || FATAL "Docker installation failed"
     echo "... Docker CE installation done"
@@ -77,7 +106,10 @@ EOF"
     return 0
 }
 
-while getopts ":D:p:-: " o; do
+OSM_BEHIND_PROXY=""
+DOCKER_PROXY_URL=""
+
+while getopts ":D:p:-: P" o; do
     case "${o}" in
         D)
             OSM_DEVOPS="${OPTARG}"
@@ -85,6 +117,9 @@ while getopts ":D:p:-: " o; do
         p)
             DOCKER_PROXY_URL="${OPTARG}"
             ;;
+        P)
+            OSM_BEHIND_PROXY="y"
+            ;;
         -)
             [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL="y" && continue
             echo -e "Invalid option: '--$OPTARG'\n" >&2
@@ -108,6 +143,7 @@ source $OSM_DEVOPS/common/logging
 
 echo "DEBUG_INSTALL=$DEBUG_INSTALL"
 echo "DOCKER_PROXY_URL=$DOCKER_PROXY_URL"
+echo "OSM_BEHIND_PROXY=$OSM_BEHIND_PROXY"
 echo "USER=$USER"
 
 install_docker_ce