blob: 64c23c2881d3404aec19169a24be39bd7d07f4eb [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
21 echo "Installing Docker CE ..."
22 sudo apt-get -qq update
23 sudo apt-get install -y apt-transport-https ca-certificates software-properties-common
24 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE=1 apt-key add -
garciadeblase4022242023-05-30 14:10:46 +020025 sudo add-apt-repository -y "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
garciadeblas0bc87522021-10-20 22:16:17 +020026 sudo apt-get -qq update
27 sudo apt-get install -y docker-ce
28# echo "Reconfiguring Docker to use systemd as cgroup driver"
29# if [ ! -f /etc/docker/daemon.json ]; then
30# sudo bash -c "cat <<EOF > /etc/docker/daemon.json
31#{
32# \"exec-opts\": [\"native.cgroupdriver=systemd\"],
33# \"log-driver\": \"json-file\",
34# \"log-opts\": {
35# \"max-size\": \"100m\"
36# },
37# \"storage-driver\": \"overlay2\"
38#}
39#EOF"
40# else
41# sudo sed -i "s|native.cgroupdriver=cgroupfs|native.cgroupdriver=systemd|" /etc/docker/daemon.json
42# fi
43 echo "Adding user to group 'docker'"
44 sudo groupadd -f docker
45 sudo usermod -aG docker $USER
46 sleep 2
47 #sudo systemctl enable docker
48 #sudo systemctl daemon-reload
49 #sudo systemctl restart docker
50 sudo service docker restart
51 echo "... restarted Docker service"
52 if [ -n "${DOCKER_PROXY_URL}" ]; then
53 echo "Configuring docker proxy ..."
54 if [ -f /etc/docker/daemon.json ]; then
55 if grep -q registry-mirrors /etc/docker/daemon.json; then
56 sudo sed -i "s|registry-mirrors.*|registry-mirrors\": [\"${DOCKER_PROXY_URL}\"] |" /etc/docker/daemon.json
57 else
58 sudo sed -i "s|^{|{\n \"registry-mirrors\": [\"${DOCKER_PROXY_URL}\"],|" /etc/docker/daemon.json
59 fi
60 else
61 sudo bash -c "cat << EOF > /etc/docker/daemon.json
62{
63 \"registry-mirrors\": [\"${DOCKER_PROXY_URL}\"]
64}
65EOF"
66 fi
garciadeblasfa3eb332022-11-15 14:11:56 +010067 fi
68 if [ -n "${OSM_BEHIND_PROXY}" ] ; then
69 if ! [ -f /etc/systemd/system/docker.service.d/http-proxy.conf ] ; then
70 sudo mkdir -p /etc/systemd/system/docker.service.d
71 cat << EOF | sudo tee -a /etc/systemd/system/docker.service.d/http-proxy.conf
72[Service]
73EOF
74 fi
75 [ -n "${HTTP_PROXY}" ] && sudo bash -c "cat <<EOF >> /etc/systemd/system/docker.service.d/http-proxy.conf
76Environment=\"HTTP_PROXY=${HTTP_PROXY}\"
77EOF"
78 [ -n "${HTTPS_PROXY}" ] && sudo bash -c "cat <<EOF >> /etc/systemd/system/docker.service.d/http-proxy.conf
79Environment=\"HTTPS_PROXY=${HTTPS_PROXY}\"
80EOF"
81 [ -n "${NO_PROXY}" ] && sudo bash -c "cat <<EOF >> /etc/systemd/system/docker.service.d/http-proxy.conf
82Environment=\"NO_PROXY=${NO_PROXY}\"
83EOF"
84 fi
85 if [ -n "${DOCKER_PROXY_URL}" ] || [ -n "${OSM_BEHIND_PROXY}" ] ; then
garciadeblas0bc87522021-10-20 22:16:17 +020086 #sudo systemctl enable docker
87 sudo systemctl daemon-reload
88 #sudo systemctl restart docker
89 sudo service docker restart
90 echo "... restarted Docker service again"
91 fi
garciadeblas45331ff2023-06-07 12:53:25 +020092
93 echo "Configuring containerd"
94 sudo mv /etc/containerd/config.toml /etc/containerd/config.toml.orig 2>/dev/null
95 sudo bash -c "containerd config default > /etc/containerd/config.toml"
96 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
97 sudo diff /etc/containerd/config.toml.orig /etc/containerd/config.toml
98 sudo service containerd restart
99
garciadeblas0bc87522021-10-20 22:16:17 +0200100 [ -z "${DEBUG_INSTALL}" ] || ! echo "File: /etc/docker/daemon.json" || cat /etc/docker/daemon.json
101 sg docker -c "docker version" || FATAL "Docker installation failed"
102 echo "... Docker CE installation done"
103 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
104 return 0
105}
106
garciadeblasfa3eb332022-11-15 14:11:56 +0100107OSM_BEHIND_PROXY=""
108DOCKER_PROXY_URL=""
109
110while getopts ":D:p:-: P" o; do
garciadeblas0bc87522021-10-20 22:16:17 +0200111 case "${o}" in
112 D)
113 OSM_DEVOPS="${OPTARG}"
114 ;;
115 p)
116 DOCKER_PROXY_URL="${OPTARG}"
117 ;;
garciadeblasfa3eb332022-11-15 14:11:56 +0100118 P)
119 OSM_BEHIND_PROXY="y"
120 ;;
garciadeblas0bc87522021-10-20 22:16:17 +0200121 -)
122 [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL="y" && continue
123 echo -e "Invalid option: '--$OPTARG'\n" >&2
124 exit 1
125 ;;
126 :)
127 echo "Option -$OPTARG requires an argument" >&2
128 exit 1
129 ;;
130 \?)
131 echo -e "Invalid option: '-$OPTARG'\n" >&2
132 exit 1
133 ;;
134 *)
135 exit 1
136 ;;
137 esac
138done
139
140source $OSM_DEVOPS/common/logging
141
142echo "DEBUG_INSTALL=$DEBUG_INSTALL"
143echo "DOCKER_PROXY_URL=$DOCKER_PROXY_URL"
garciadeblasfa3eb332022-11-15 14:11:56 +0100144echo "OSM_BEHIND_PROXY=$OSM_BEHIND_PROXY"
garciadeblas0bc87522021-10-20 22:16:17 +0200145echo "USER=$USER"
146
147install_docker_ce
148