blob: 5010f7e3d41d126302e00a7e81da007484b75529 [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
garciadeblas73738a82023-11-02 07:56:50 +010018function configure_containerd() {
19 echo "Configuring containerd to expose CRI and use systemd cgroup"
20 sudo mv /etc/containerd/config.toml /etc/containerd/config.toml.orig 2>/dev/null
21 sudo bash -c "containerd config default > /etc/containerd/config.toml"
22 sudo sed -i 's/SystemdCgroup = false/SystemdCgroup = true/' /etc/containerd/config.toml
23 if [ -n "${DOCKER_PROXY_URL}" ]; then
24 echo "Configuring ${DOCKER_PROXY_URL} as registry mirror in /etc/containerd/config.toml"
25 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
26 fi
27 if [ -n "${OSM_BEHIND_PROXY}" ] ; then
28 echo "Configuring http proxies in /etc/systemd/system/containerd.service.d/http-proxy.conf"
29 if ! [ -f /etc/systemd/system/containerd.service.d/http-proxy.conf ] ; then
30 sudo mkdir -p /etc/systemd/system/containerd.service.d
31 cat << EOF | sudo tee -a /etc/systemd/system/containerd.service.d/http-proxy.conf
32[Service]
33EOF
34 fi
35 [ -n "${HTTP_PROXY}" ] && sudo bash -c "cat <<EOF >> /etc/systemd/system/containerd.service.d/http-proxy.conf
36Environment=\"HTTP_PROXY=${HTTP_PROXY}\"
37EOF"
38 [ -n "${HTTPS_PROXY}" ] && sudo bash -c "cat <<EOF >> /etc/systemd/system/containerd.service.d/http-proxy.conf
39Environment=\"HTTPS_PROXY=${HTTPS_PROXY}\"
40EOF"
41 [ -n "${NO_PROXY}" ] && sudo bash -c "cat <<EOF >> /etc/systemd/system/containerd.service.d/http-proxy.conf
42Environment=\"NO_PROXY=${NO_PROXY}\"
43EOF"
44 fi
45 sudo systemctl restart containerd
46}
47
garciadeblas0bc87522021-10-20 22:16:17 +020048function install_docker_ce() {
49 # installs and configures Docker CE
50 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
garciadeblas80b2e172023-06-01 18:38:13 +020051 echo "Removing previous installation of docker ..."
52 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 +020053 echo "Installing Docker CE ..."
garciadeblas80b2e172023-06-01 18:38:13 +020054 sudo apt-get -y update
55 sudo apt-get install -y apt-transport-https ca-certificates software-properties-common gnupg
56 sudo install -m 0755 -d /etc/apt/keyrings
57 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
58 sudo chmod a+r /etc/apt/keyrings/docker.gpg
59 echo "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
60 "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
61 sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
62 sudo apt-get -y update
garciadeblas0bc87522021-10-20 22:16:17 +020063 sudo apt-get install -y docker-ce
garciadeblas80b2e172023-06-01 18:38:13 +020064
garciadeblas0bc87522021-10-20 22:16:17 +020065 echo "Adding user to group 'docker'"
66 sudo groupadd -f docker
67 sudo usermod -aG docker $USER
garciadeblas80b2e172023-06-01 18:38:13 +020068
garciadeblas0bc87522021-10-20 22:16:17 +020069 if [ -n "${DOCKER_PROXY_URL}" ]; then
70 echo "Configuring docker proxy ..."
71 if [ -f /etc/docker/daemon.json ]; then
72 if grep -q registry-mirrors /etc/docker/daemon.json; then
73 sudo sed -i "s|registry-mirrors.*|registry-mirrors\": [\"${DOCKER_PROXY_URL}\"] |" /etc/docker/daemon.json
74 else
75 sudo sed -i "s|^{|{\n \"registry-mirrors\": [\"${DOCKER_PROXY_URL}\"],|" /etc/docker/daemon.json
76 fi
77 else
78 sudo bash -c "cat << EOF > /etc/docker/daemon.json
79{
80 \"registry-mirrors\": [\"${DOCKER_PROXY_URL}\"]
81}
82EOF"
83 fi
garciadeblasfa3eb332022-11-15 14:11:56 +010084 fi
85 if [ -n "${OSM_BEHIND_PROXY}" ] ; then
86 if ! [ -f /etc/systemd/system/docker.service.d/http-proxy.conf ] ; then
87 sudo mkdir -p /etc/systemd/system/docker.service.d
88 cat << EOF | sudo tee -a /etc/systemd/system/docker.service.d/http-proxy.conf
89[Service]
90EOF
91 fi
92 [ -n "${HTTP_PROXY}" ] && sudo bash -c "cat <<EOF >> /etc/systemd/system/docker.service.d/http-proxy.conf
93Environment=\"HTTP_PROXY=${HTTP_PROXY}\"
94EOF"
95 [ -n "${HTTPS_PROXY}" ] && sudo bash -c "cat <<EOF >> /etc/systemd/system/docker.service.d/http-proxy.conf
96Environment=\"HTTPS_PROXY=${HTTPS_PROXY}\"
97EOF"
98 [ -n "${NO_PROXY}" ] && sudo bash -c "cat <<EOF >> /etc/systemd/system/docker.service.d/http-proxy.conf
99Environment=\"NO_PROXY=${NO_PROXY}\"
100EOF"
101 fi
102 if [ -n "${DOCKER_PROXY_URL}" ] || [ -n "${OSM_BEHIND_PROXY}" ] ; then
garciadeblas0bc87522021-10-20 22:16:17 +0200103 sudo systemctl daemon-reload
garciadeblas80b2e172023-06-01 18:38:13 +0200104 sudo systemctl restart docker
105 echo "... restarted Docker service"
garciadeblas0bc87522021-10-20 22:16:17 +0200106 fi
garciadeblas45331ff2023-06-07 12:53:25 +0200107
garciadeblas73738a82023-11-02 07:56:50 +0100108 configure_containerd
garciadeblas45331ff2023-06-07 12:53:25 +0200109
garciadeblas0bc87522021-10-20 22:16:17 +0200110 [ -z "${DEBUG_INSTALL}" ] || ! echo "File: /etc/docker/daemon.json" || cat /etc/docker/daemon.json
garciadeblas80b2e172023-06-01 18:38:13 +0200111 echo "Testing Docker CE installation ..."
112 sg docker -c "docker version" || FATAL_TRACK docker_ce "Docker installation failed. Cannot run docker version"
113 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 +0200114 echo "... Docker CE installation done"
115 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
116 return 0
117}
118
garciadeblasfa3eb332022-11-15 14:11:56 +0100119OSM_BEHIND_PROXY=""
120DOCKER_PROXY_URL=""
121
122while getopts ":D:p:-: P" o; do
garciadeblas0bc87522021-10-20 22:16:17 +0200123 case "${o}" in
124 D)
125 OSM_DEVOPS="${OPTARG}"
126 ;;
127 p)
128 DOCKER_PROXY_URL="${OPTARG}"
129 ;;
garciadeblasfa3eb332022-11-15 14:11:56 +0100130 P)
131 OSM_BEHIND_PROXY="y"
132 ;;
garciadeblas0bc87522021-10-20 22:16:17 +0200133 -)
134 [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL="y" && continue
135 echo -e "Invalid option: '--$OPTARG'\n" >&2
136 exit 1
137 ;;
138 :)
139 echo "Option -$OPTARG requires an argument" >&2
140 exit 1
141 ;;
142 \?)
143 echo -e "Invalid option: '-$OPTARG'\n" >&2
144 exit 1
145 ;;
146 *)
147 exit 1
148 ;;
149 esac
150done
151
152source $OSM_DEVOPS/common/logging
153
154echo "DEBUG_INSTALL=$DEBUG_INSTALL"
155echo "DOCKER_PROXY_URL=$DOCKER_PROXY_URL"
garciadeblasfa3eb332022-11-15 14:11:56 +0100156echo "OSM_BEHIND_PROXY=$OSM_BEHIND_PROXY"
garciadeblas0bc87522021-10-20 22:16:17 +0200157echo "USER=$USER"
158
159install_docker_ce
160