Move lcm certificate to lcm folder in OSM helm chart
[osm/devops.git] / installers / install_docker_ce.sh
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 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
48 function install_docker_ce() {
49 # installs and configures Docker CE
50 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
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
53 echo "Installing Docker CE ..."
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
63 sudo apt-get install -y docker-ce
64
65 echo "Adding user to group 'docker'"
66 sudo groupadd -f docker
67 sudo usermod -aG docker $USER
68
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
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
103 sudo systemctl daemon-reload
104 sudo systemctl restart docker
105 echo "... restarted Docker service"
106 fi
107
108 configure_containerd
109
110 [ -z "${DEBUG_INSTALL}" ] || ! echo "File: /etc/docker/daemon.json" || cat /etc/docker/daemon.json
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"
114 echo "... Docker CE installation done"
115 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
116 return 0
117 }
118
119 OSM_BEHIND_PROXY=""
120 DOCKER_PROXY_URL=""
121
122 while getopts ":D:p:-: P" o; do
123 case "${o}" in
124 D)
125 OSM_DEVOPS="${OPTARG}"
126 ;;
127 p)
128 DOCKER_PROXY_URL="${OPTARG}"
129 ;;
130 P)
131 OSM_BEHIND_PROXY="y"
132 ;;
133 -)
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
150 done
151
152 source $OSM_DEVOPS/common/logging
153
154 echo "DEBUG_INSTALL=$DEBUG_INSTALL"
155 echo "DOCKER_PROXY_URL=$DOCKER_PROXY_URL"
156 echo "OSM_BEHIND_PROXY=$OSM_BEHIND_PROXY"
157 echo "USER=$USER"
158
159 install_docker_ce
160