| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 1 | #!/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 | |
| 16 | set +eux |
| 17 | |
| 18 | function install_docker_ce() { |
| 19 | # installs and configures Docker CE |
| 20 | [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function |
| garciadeblas | 80b2e17 | 2023-06-01 18:38:13 +0200 | [diff] [blame] | 21 | 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 |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 23 | echo "Installing Docker CE ..." |
| garciadeblas | 80b2e17 | 2023-06-01 18:38:13 +0200 | [diff] [blame] | 24 | 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 |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 33 | sudo apt-get install -y docker-ce |
| garciadeblas | 80b2e17 | 2023-06-01 18:38:13 +0200 | [diff] [blame] | 34 | |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 35 | echo "Adding user to group 'docker'" |
| 36 | sudo groupadd -f docker |
| 37 | sudo usermod -aG docker $USER |
| garciadeblas | 80b2e17 | 2023-06-01 18:38:13 +0200 | [diff] [blame] | 38 | |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 39 | 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 | } |
| 52 | EOF" |
| 53 | fi |
| garciadeblas | fa3eb33 | 2022-11-15 14:11:56 +0100 | [diff] [blame] | 54 | 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] |
| 60 | EOF |
| 61 | fi |
| 62 | [ -n "${HTTP_PROXY}" ] && sudo bash -c "cat <<EOF >> /etc/systemd/system/docker.service.d/http-proxy.conf |
| 63 | Environment=\"HTTP_PROXY=${HTTP_PROXY}\" |
| 64 | EOF" |
| 65 | [ -n "${HTTPS_PROXY}" ] && sudo bash -c "cat <<EOF >> /etc/systemd/system/docker.service.d/http-proxy.conf |
| 66 | Environment=\"HTTPS_PROXY=${HTTPS_PROXY}\" |
| 67 | EOF" |
| 68 | [ -n "${NO_PROXY}" ] && sudo bash -c "cat <<EOF >> /etc/systemd/system/docker.service.d/http-proxy.conf |
| 69 | Environment=\"NO_PROXY=${NO_PROXY}\" |
| 70 | EOF" |
| 71 | fi |
| 72 | if [ -n "${DOCKER_PROXY_URL}" ] || [ -n "${OSM_BEHIND_PROXY}" ] ; then |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 73 | sudo systemctl daemon-reload |
| garciadeblas | 80b2e17 | 2023-06-01 18:38:13 +0200 | [diff] [blame] | 74 | sudo systemctl restart docker |
| 75 | echo "... restarted Docker service" |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 76 | fi |
| garciadeblas | 45331ff | 2023-06-07 12:53:25 +0200 | [diff] [blame] | 77 | |
| garciadeblas | 80b2e17 | 2023-06-01 18:38:13 +0200 | [diff] [blame] | 78 | 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 |
| garciadeblas | 1f2576e | 2023-06-07 23:19:00 +0200 | [diff] [blame] | 82 | if [ -n "${DOCKER_PROXY_URL}" ]; then |
| garciadeblas | 1f2576e | 2023-06-07 23:19:00 +0200 | [diff] [blame] | 83 | 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 |
| garciadeblas | 1f2576e | 2023-06-07 23:19:00 +0200 | [diff] [blame] | 84 | fi |
| garciadeblas | 80b2e17 | 2023-06-01 18:38:13 +0200 | [diff] [blame] | 85 | sudo systemctl restart containerd |
| garciadeblas | 45331ff | 2023-06-07 12:53:25 +0200 | [diff] [blame] | 86 | |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 87 | [ -z "${DEBUG_INSTALL}" ] || ! echo "File: /etc/docker/daemon.json" || cat /etc/docker/daemon.json |
| garciadeblas | 80b2e17 | 2023-06-01 18:38:13 +0200 | [diff] [blame] | 88 | 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" |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 91 | echo "... Docker CE installation done" |
| 92 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 93 | return 0 |
| 94 | } |
| 95 | |
| garciadeblas | fa3eb33 | 2022-11-15 14:11:56 +0100 | [diff] [blame] | 96 | OSM_BEHIND_PROXY="" |
| 97 | DOCKER_PROXY_URL="" |
| 98 | |
| 99 | while getopts ":D:p:-: P" o; do |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 100 | case "${o}" in |
| 101 | D) |
| 102 | OSM_DEVOPS="${OPTARG}" |
| 103 | ;; |
| 104 | p) |
| 105 | DOCKER_PROXY_URL="${OPTARG}" |
| 106 | ;; |
| garciadeblas | fa3eb33 | 2022-11-15 14:11:56 +0100 | [diff] [blame] | 107 | P) |
| 108 | OSM_BEHIND_PROXY="y" |
| 109 | ;; |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 110 | -) |
| 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 |
| 127 | done |
| 128 | |
| 129 | source $OSM_DEVOPS/common/logging |
| 130 | |
| 131 | echo "DEBUG_INSTALL=$DEBUG_INSTALL" |
| 132 | echo "DOCKER_PROXY_URL=$DOCKER_PROXY_URL" |
| garciadeblas | fa3eb33 | 2022-11-15 14:11:56 +0100 | [diff] [blame] | 133 | echo "OSM_BEHIND_PROXY=$OSM_BEHIND_PROXY" |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 134 | echo "USER=$USER" |
| 135 | |
| 136 | install_docker_ce |
| 137 | |