| 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 | |
| garciadeblas | 8c2b264 | 2023-11-02 07:56:50 +0100 | [diff] [blame] | 18 | function 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] |
| 33 | EOF |
| 34 | fi |
| 35 | [ -n "${HTTP_PROXY}" ] && sudo bash -c "cat <<EOF >> /etc/systemd/system/containerd.service.d/http-proxy.conf |
| 36 | Environment=\"HTTP_PROXY=${HTTP_PROXY}\" |
| 37 | EOF" |
| 38 | [ -n "${HTTPS_PROXY}" ] && sudo bash -c "cat <<EOF >> /etc/systemd/system/containerd.service.d/http-proxy.conf |
| 39 | Environment=\"HTTPS_PROXY=${HTTPS_PROXY}\" |
| 40 | EOF" |
| 41 | [ -n "${NO_PROXY}" ] && sudo bash -c "cat <<EOF >> /etc/systemd/system/containerd.service.d/http-proxy.conf |
| 42 | Environment=\"NO_PROXY=${NO_PROXY}\" |
| 43 | EOF" |
| 44 | fi |
| 45 | sudo systemctl restart containerd |
| 46 | } |
| 47 | |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 48 | function install_docker_ce() { |
| 49 | # installs and configures Docker CE |
| 50 | [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function |
| garciadeblas | 80b2e17 | 2023-06-01 18:38:13 +0200 | [diff] [blame] | 51 | 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 |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 53 | echo "Installing Docker CE ..." |
| garciadeblas | 80b2e17 | 2023-06-01 18:38:13 +0200 | [diff] [blame] | 54 | 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 |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 63 | sudo apt-get install -y docker-ce |
| garciadeblas | 80b2e17 | 2023-06-01 18:38:13 +0200 | [diff] [blame] | 64 | |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 65 | echo "Adding user to group 'docker'" |
| 66 | sudo groupadd -f docker |
| 67 | sudo usermod -aG docker $USER |
| garciadeblas | 80b2e17 | 2023-06-01 18:38:13 +0200 | [diff] [blame] | 68 | |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 69 | 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 | } |
| 82 | EOF" |
| 83 | fi |
| garciadeblas | fa3eb33 | 2022-11-15 14:11:56 +0100 | [diff] [blame] | 84 | 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] |
| 90 | EOF |
| 91 | fi |
| 92 | [ -n "${HTTP_PROXY}" ] && sudo bash -c "cat <<EOF >> /etc/systemd/system/docker.service.d/http-proxy.conf |
| 93 | Environment=\"HTTP_PROXY=${HTTP_PROXY}\" |
| 94 | EOF" |
| 95 | [ -n "${HTTPS_PROXY}" ] && sudo bash -c "cat <<EOF >> /etc/systemd/system/docker.service.d/http-proxy.conf |
| 96 | Environment=\"HTTPS_PROXY=${HTTPS_PROXY}\" |
| 97 | EOF" |
| 98 | [ -n "${NO_PROXY}" ] && sudo bash -c "cat <<EOF >> /etc/systemd/system/docker.service.d/http-proxy.conf |
| 99 | Environment=\"NO_PROXY=${NO_PROXY}\" |
| 100 | EOF" |
| 101 | fi |
| 102 | if [ -n "${DOCKER_PROXY_URL}" ] || [ -n "${OSM_BEHIND_PROXY}" ] ; then |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 103 | sudo systemctl daemon-reload |
| garciadeblas | 80b2e17 | 2023-06-01 18:38:13 +0200 | [diff] [blame] | 104 | sudo systemctl restart docker |
| 105 | echo "... restarted Docker service" |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 106 | fi |
| garciadeblas | 45331ff | 2023-06-07 12:53:25 +0200 | [diff] [blame] | 107 | |
| garciadeblas | 8c2b264 | 2023-11-02 07:56:50 +0100 | [diff] [blame] | 108 | configure_containerd |
| garciadeblas | 45331ff | 2023-06-07 12:53:25 +0200 | [diff] [blame] | 109 | |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 110 | [ -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] | 111 | 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" |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 114 | echo "... Docker CE installation done" |
| 115 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 116 | return 0 |
| 117 | } |
| 118 | |
| garciadeblas | fa3eb33 | 2022-11-15 14:11:56 +0100 | [diff] [blame] | 119 | while getopts ":D:p:-: P" o; do |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 120 | case "${o}" in |
| 121 | D) |
| 122 | OSM_DEVOPS="${OPTARG}" |
| 123 | ;; |
| 124 | p) |
| 125 | DOCKER_PROXY_URL="${OPTARG}" |
| 126 | ;; |
| garciadeblas | fa3eb33 | 2022-11-15 14:11:56 +0100 | [diff] [blame] | 127 | P) |
| 128 | OSM_BEHIND_PROXY="y" |
| 129 | ;; |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 130 | -) |
| 131 | [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL="y" && continue |
| 132 | echo -e "Invalid option: '--$OPTARG'\n" >&2 |
| 133 | exit 1 |
| 134 | ;; |
| 135 | :) |
| 136 | echo "Option -$OPTARG requires an argument" >&2 |
| 137 | exit 1 |
| 138 | ;; |
| 139 | \?) |
| 140 | echo -e "Invalid option: '-$OPTARG'\n" >&2 |
| 141 | exit 1 |
| 142 | ;; |
| 143 | *) |
| 144 | exit 1 |
| 145 | ;; |
| 146 | esac |
| 147 | done |
| 148 | |
| garciadeblas | 8298116 | 2024-07-23 15:24:00 +0200 | [diff] [blame] | 149 | DEBUG_INSTALL=${DEBUG_INSTALL:-} |
| 150 | DOCKER_PROXY_URL=${DOCKER_PROXY_URL:-} |
| 151 | OSM_BEHIND_PROXY=${OSM_BEHIND_PROXY:-} |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 152 | echo "DEBUG_INSTALL=$DEBUG_INSTALL" |
| 153 | echo "DOCKER_PROXY_URL=$DOCKER_PROXY_URL" |
| garciadeblas | fa3eb33 | 2022-11-15 14:11:56 +0100 | [diff] [blame] | 154 | echo "OSM_BEHIND_PROXY=$OSM_BEHIND_PROXY" |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 155 | echo "USER=$USER" |
| 156 | |
| garciadeblas | 8298116 | 2024-07-23 15:24:00 +0200 | [diff] [blame] | 157 | source $OSM_DEVOPS/common/logging |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 158 | |
| garciadeblas | 8298116 | 2024-07-23 15:24:00 +0200 | [diff] [blame] | 159 | install_docker_ce |