blob: e5c2f44d6751d06999055911f51051ac6f733590 [file] [log] [blame]
garciadeblas0bc87522021-10-20 22:16:17 +02001#!/bin/bash
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14#
15
16set +eux
17
18function install_docker_ce() {
19 # installs and configures Docker CE
20 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
garciadeblas80b2e172023-06-01 18:38:13 +020021 echo "Removing previous installation of docker ..."
22 for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove $pkg; done
garciadeblas0bc87522021-10-20 22:16:17 +020023 echo "Installing Docker CE ..."
garciadeblas80b2e172023-06-01 18:38:13 +020024 sudo apt-get -y update
25 sudo apt-get install -y apt-transport-https ca-certificates software-properties-common gnupg
26 sudo install -m 0755 -d /etc/apt/keyrings
27 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
28 sudo chmod a+r /etc/apt/keyrings/docker.gpg
29 echo "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
30 "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
31 sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
32 sudo apt-get -y update
garciadeblas0bc87522021-10-20 22:16:17 +020033 sudo apt-get install -y docker-ce
garciadeblas80b2e172023-06-01 18:38:13 +020034
garciadeblas0bc87522021-10-20 22:16:17 +020035 echo "Adding user to group 'docker'"
36 sudo groupadd -f docker
37 sudo usermod -aG docker $USER
garciadeblas80b2e172023-06-01 18:38:13 +020038
garciadeblas0bc87522021-10-20 22:16:17 +020039 if [ -n "${DOCKER_PROXY_URL}" ]; then
40 echo "Configuring docker proxy ..."
41 if [ -f /etc/docker/daemon.json ]; then
42 if grep -q registry-mirrors /etc/docker/daemon.json; then
43 sudo sed -i "s|registry-mirrors.*|registry-mirrors\": [\"${DOCKER_PROXY_URL}\"] |" /etc/docker/daemon.json
44 else
45 sudo sed -i "s|^{|{\n \"registry-mirrors\": [\"${DOCKER_PROXY_URL}\"],|" /etc/docker/daemon.json
46 fi
47 else
48 sudo bash -c "cat << EOF > /etc/docker/daemon.json
49{
50 \"registry-mirrors\": [\"${DOCKER_PROXY_URL}\"]
51}
52EOF"
53 fi
garciadeblasfa3eb332022-11-15 14:11:56 +010054 fi
55 if [ -n "${OSM_BEHIND_PROXY}" ] ; then
56 if ! [ -f /etc/systemd/system/docker.service.d/http-proxy.conf ] ; then
57 sudo mkdir -p /etc/systemd/system/docker.service.d
58 cat << EOF | sudo tee -a /etc/systemd/system/docker.service.d/http-proxy.conf
59[Service]
60EOF
61 fi
62 [ -n "${HTTP_PROXY}" ] && sudo bash -c "cat <<EOF >> /etc/systemd/system/docker.service.d/http-proxy.conf
63Environment=\"HTTP_PROXY=${HTTP_PROXY}\"
64EOF"
65 [ -n "${HTTPS_PROXY}" ] && sudo bash -c "cat <<EOF >> /etc/systemd/system/docker.service.d/http-proxy.conf
66Environment=\"HTTPS_PROXY=${HTTPS_PROXY}\"
67EOF"
68 [ -n "${NO_PROXY}" ] && sudo bash -c "cat <<EOF >> /etc/systemd/system/docker.service.d/http-proxy.conf
69Environment=\"NO_PROXY=${NO_PROXY}\"
70EOF"
71 fi
72 if [ -n "${DOCKER_PROXY_URL}" ] || [ -n "${OSM_BEHIND_PROXY}" ] ; then
garciadeblas0bc87522021-10-20 22:16:17 +020073 sudo systemctl daemon-reload
garciadeblas80b2e172023-06-01 18:38:13 +020074 sudo systemctl restart docker
75 echo "... restarted Docker service"
garciadeblas0bc87522021-10-20 22:16:17 +020076 fi
garciadeblas45331ff2023-06-07 12:53:25 +020077
garciadeblas80b2e172023-06-01 18:38:13 +020078 echo "Configuring containerd to expose CRI, use systemd cgroup and use DOCKER_PROXY_URL as registry mirror"
79 sudo mv /etc/containerd/config.toml /etc/containerd/config.toml.orig 2>/dev/null
80 sudo bash -c "containerd config default > /etc/containerd/config.toml"
81 sudo sed -i 's/SystemdCgroup = false/SystemdCgroup = true/' /etc/containerd/config.toml
garciadeblas1f2576e2023-06-07 23:19:00 +020082 if [ -n "${DOCKER_PROXY_URL}" ]; then
garciadeblas1f2576e2023-06-07 23:19:00 +020083 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
garciadeblas1f2576e2023-06-07 23:19:00 +020084 fi
garciadeblas80b2e172023-06-01 18:38:13 +020085 sudo systemctl restart containerd
garciadeblas45331ff2023-06-07 12:53:25 +020086
garciadeblas0bc87522021-10-20 22:16:17 +020087 [ -z "${DEBUG_INSTALL}" ] || ! echo "File: /etc/docker/daemon.json" || cat /etc/docker/daemon.json
garciadeblas80b2e172023-06-01 18:38:13 +020088 echo "Testing Docker CE installation ..."
89 sg docker -c "docker version" || FATAL_TRACK docker_ce "Docker installation failed. Cannot run docker version"
90 sg docker -c "docker run --rm hello-world" || FATAL_TRACK docker_ce "Docker installation failed. Cannot run hello-world"
garciadeblas0bc87522021-10-20 22:16:17 +020091 echo "... Docker CE installation done"
92 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
93 return 0
94}
95
garciadeblasfa3eb332022-11-15 14:11:56 +010096OSM_BEHIND_PROXY=""
97DOCKER_PROXY_URL=""
98
99while getopts ":D:p:-: P" o; do
garciadeblas0bc87522021-10-20 22:16:17 +0200100 case "${o}" in
101 D)
102 OSM_DEVOPS="${OPTARG}"
103 ;;
104 p)
105 DOCKER_PROXY_URL="${OPTARG}"
106 ;;
garciadeblasfa3eb332022-11-15 14:11:56 +0100107 P)
108 OSM_BEHIND_PROXY="y"
109 ;;
garciadeblas0bc87522021-10-20 22:16:17 +0200110 -)
111 [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL="y" && continue
112 echo -e "Invalid option: '--$OPTARG'\n" >&2
113 exit 1
114 ;;
115 :)
116 echo "Option -$OPTARG requires an argument" >&2
117 exit 1
118 ;;
119 \?)
120 echo -e "Invalid option: '-$OPTARG'\n" >&2
121 exit 1
122 ;;
123 *)
124 exit 1
125 ;;
126 esac
127done
128
129source $OSM_DEVOPS/common/logging
130
131echo "DEBUG_INSTALL=$DEBUG_INSTALL"
132echo "DOCKER_PROXY_URL=$DOCKER_PROXY_URL"
garciadeblasfa3eb332022-11-15 14:11:56 +0100133echo "OSM_BEHIND_PROXY=$OSM_BEHIND_PROXY"
garciadeblas0bc87522021-10-20 22:16:17 +0200134echo "USER=$USER"
135
136install_docker_ce
137