blob: 1283894d47782316d39386037af385d65c34f370 [file] [log] [blame]
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +01001#!/bin/bash
2set -x
Deniscaa72dd2021-06-04 17:13:05 +03003set -e
Dennis Dmitrievb12d2342020-03-12 19:36:24 +02004# allow access to the local variables from prepare-metadata.py
5set -a
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +01006
Vasyl Saienko9824df42021-03-17 15:43:02 +02007# ensure we don't re-source this in the same environment
Vasyl Saienko4a2832d2024-05-16 09:00:03 +03008[[ -z "$_FUNCTIONS_SCRIPT" ]] || return 0
9declare -r -g _FUNCTIONS_SCRIPT=1
Vasyl Saienko9824df42021-03-17 15:43:02 +020010
Oleh Hryhorovca772092020-02-13 11:15:17 +020011PUBLIC_INTERFACE_NETMASK=$(echo ${PUBLIC_INTERFACE_CIDR} | cut -d'/' -f2)
Oleh Hryhorovdccb1cd2020-03-04 15:52:55 +020012STORAGE_BACKEND_NETWORK_NETMASK=$(echo ${STORAGE_BACKEND_NETWORK} | cut -d'/' -f2)
Oleh Hryhorovdccb1cd2020-03-04 15:52:55 +020013STORAGE_FRONTEND_NETWORK_NETMASK=$(echo ${STORAGE_FRONTEND_NETWORK} | cut -d'/' -f2)
Vasyl Saienko552250d2021-03-19 12:07:23 +020014IRONIC_BAREMETAL_INTERFACE_IP=${IRONIC_BAREMETAL_INTERFACE_IP:-$ironic_baremetal_interface_ip}
Vasyl Saienko4c468192020-05-19 11:51:13 +030015IRONIC_BAREMETAL_NETWORK_NETMASK=$(echo ${IRONIC_BAREMETAL_NETWORK} | cut -d'/' -f2)
Vasyl Saienkod3964202023-09-24 15:09:48 +000016IRONIC_BAREMETAL_INTERFACE=$(ip route |grep ${IRONIC_BAREMETAL_NETWORK} | awk '/ src / {print $3}')
Vasyl Saienko3dd0c272020-11-16 17:20:07 +020017TUNNEL_INTERFACE_NETWORK_NETMASK=$(ip -o addr show |grep -w ${TUNNEL_INTERFACE_IP} | awk '{print $4}' |awk -F '/' '{print $2}')
18TUNNEL_INTERFACE=$(ip -o addr show |grep -w ${TUNNEL_INTERFACE_IP}/${TUNNEL_INTERFACE_NETWORK_NETMASK} | awk '{print $2}')
Michael Polenchuk486cadf2020-08-24 16:25:51 +040019IRONIC_BAREMETAL_NETWORK_PREFIX=$(sed 's/[0-9]*\/[0-9]*$//' <<< $IRONIC_BAREMETAL_NETWORK)
20IRONIC_BAREMETAL_TUNNEL_NETWORK_PREFIX=$(sed 's/[0-9]*\/[0-9]*$//' <<< $IRONIC_BAREMETAL_TUNNEL_NETWORK)
Vasyl Saienko4a2832d2024-05-16 09:00:03 +030021STORAGE_FRONTEND_NETWORK_NETMASK=$(echo ${STORAGE_FRONTEND_NETWORK} | cut -d'/' -f2)
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +010022DOCKER_DEFAULT_ADDRESS_POOL=${DOCKER_DEFAULT_ADDRESS_POOL:-10.10.1.0/16}
23# DOCKER_DEFAULT_ADDRESS_SIZE have to be less then netmask in DOCKER_DEFAULT_ADDRESS_POOL because
24# to the fact that actual netmask for docker_gwbridge is given from it
25DOCKER_DEFAULT_ADDRESS_SIZE=${DOCKER_DEFAULT_ADDRESS_SIZE:-24}
Mykyta Karpin5ed5fd82020-01-27 18:23:29 +020026DOCKER_EE_RELEASE=${DOCKER_EE_RELEASE:-stable-19.03}
Vasyl Saienkofa714102024-03-22 09:30:25 +020027DOCKER_EE_PACKAGES=${DOCKER_EE_PACKAGES:-'docker-ee'}
Vasyl Saienkoa94bf182023-04-01 10:42:17 +000028BINARY_BASE_URL=${BINARY_BASE_URL:-"http://binary.mirantis.com"}
dbiletskiy090662a2024-07-08 14:27:26 +020029CODENAME=$(cat /etc/*release | awk -F '=' '/_CODENAME/ {print $2}' | head -1)
Vasyl Saienko7c595922021-03-17 13:58:13 +020030
31### COMMON FUNCTIONS ###
32function get_interface_prefix {
33 local interface=$1
34 local interface_ip
35 local interface_mask
36
37 interface_ip=$(ip addr show dev ${interface} | grep -Po 'inet \K[\d.]+' | egrep -v "127.0.|172.17")
38 interface_mask=$(ip addr show dev ${interface} | grep -Po 'inet \K[\d.]+\/[\d]+' | egrep -v "127.0.|172.17" | cut -d'/' -f2)
39 echo "${interface_ip}/${interface_mask}"
40}
41### END COMMON FUNCTIONS ###
42
Oleh Hryhorovf4677af2021-04-26 18:00:15 +030043DEFAULT_INTERFACE=${DEFAULT_INTERFACE:-$(ip route show |awk '/default/ {print $5}')}
Vasyl Saienko7c595922021-03-17 13:58:13 +020044
Vasyl Saienko390c53a2021-03-18 12:45:11 +020045if [[ -n ${CONTROL_NETWORK_CIDR} ]]; then
Vasyl Saienkod3964202023-09-24 15:09:48 +000046 CONTROL_IP_ADDRESS=$(ip route |grep ${CONTROL_NETWORK_CIDR} | head -n1 | fgrep -v ' via ' | awk '/ src / {print $9}')
Vasyl Saienko7c595922021-03-17 13:58:13 +020047else
48 CONTROL_IP_ADDRESS=$(get_interface_prefix ${DEFAULT_INTERFACE} | awk -F '/' '{print $1}')
Vasyl Saienkoe9677062021-03-19 13:31:32 +020049 CONTROL_NETWORK_CIDR=$(ip route show dev ${DEFAULT_INTERFACE} | awk '/kernel/ {print $1}')
Vasyl Saienko7c595922021-03-17 13:58:13 +020050fi
51
52
Oleh Hryhorovb58460e2020-02-05 15:53:48 +020053PUBLIC_INTERFACE=${PUBLIC_INTERFACE:-ens4}
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +010054UCP_USERNAME=${UCP_USERNAME:-admin}
55UCP_PASSWORD=${UCP_PASSWORD:-administrator}
56OS_CODENAME=$(lsb_release -c -s)
Vasyl Saienko3ab29c92020-01-03 10:39:03 +020057NODE_DEPLOYMENT_RETRIES=${NODE_DEPLOYMENT_RETRIES:-15}
Vasyl Saienko7d7acea2020-01-03 16:59:05 +020058FLOATING_NETWORK_PREFIXES=${FLOATING_NETWORK_PREFIXES:-10.11.12.0/24}
Oleh Hryhorov404e9572020-01-21 18:57:22 +020059PUBLIC_INTERFACE=${PUBLIC_INTERFACE:-ens4}
Vasyl Saienko0fa6f192020-03-06 16:08:51 +020060UCP_MASTER_HOST=${UCP_MASTER_HOST:-${CONTROL_IP_ADDRESS}}
61UCP_IP_ADDRESS=${UCP_IP_ADDRESS:-$CONTROL_IP_ADDRESS}
Vasyl Saienkocfbedc72021-02-15 18:02:56 +020062# Change default VXLAN port, need for https://mirantis.jira.com/browse/PRODX-11679
63UCP_DOCKER_SWARM_DATA_PORT=${UCP_DOCKER_SWARM_DATA_PORT:-4789}
Vasyl Saienko06c7afe2022-07-01 17:53:56 +000064UCP_DOCKER_CALICO_VXLAN_PORT=${UCP_DOCKER_CALICO_VXLAN_PORT:-${UCP_DOCKER_SWARM_DATA_PORT}}
Vasyl Saienko08166342020-02-28 12:20:51 +020065NTP_SERVERS=${NTP_SERVERS:-"ldap.scc.mirantis.net ldap.bud.mirantis.net"}
Michael Polenchuk486cadf2020-08-24 16:25:51 +040066IRONIC_BAREMETAL_VXLAN_INTERFACE='vxlan10'
Mykyta Karpin6a153c52020-11-03 15:21:38 +020067HUGE_PAGES_1G_SIZE=$(echo "${HUGE_PAGES}" | awk -F ',' '{print $1}')
68HUGE_PAGES_2Mb_SIZE=$(echo "${HUGE_PAGES}" | awk -F ',' '{print $2}')
69CONFIGURE_HUGE_PAGES=false
Vasyl Saienko3dd0c272020-11-16 17:20:07 +020070TUNNEL_INTERFACE_NETPLAN_MANAGE=true
Anton Samoylov1d140ee2020-11-23 22:40:13 +040071TUNGSTENFABRIC_ENABLED=${TUNGSTENFABRIC_ENABLED:-false}
Mykyta Karpin6a153c52020-11-03 15:21:38 +020072if [[ "${HUGE_PAGES_1G_SIZE}" -gt 0 ]] || [[ "${HUGE_PAGES_2Mb_SIZE}" -gt 0 ]]; then
73 CONFIGURE_HUGE_PAGES=true
Anton Samoylov1d140ee2020-11-23 22:40:13 +040074 if [[ "${TUNGSTENFABRIC_ENABLED,,}" == false ]]; then
75 TUNNEL_INTERFACE_NETPLAN_MANAGE=false
76 fi
Mykyta Karpin6a153c52020-11-03 15:21:38 +020077fi
Oleh Hryhorov43bbc8c2020-12-03 15:30:09 +020078SINGLE_NODE=${SINGLE_NODE:-true}
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +010079
Vasyl Saienko25bb75b2021-02-15 12:22:33 +020080#FRR Options
81FRR_BGP_AS_NUMBER=${FRR_BGP_AS_NUMBER:-64512}
82FRR_ROUTER_ID=${TUNNEL_INTERFACE_IP}
Vasyl Saienko846c6342021-02-24 17:01:36 +020083FRR_BGP_SOURCE_ADDRESS=${TUNNEL_INTERFACE_IP}
84FRR_BGP_NEIGHBORS=${FRR_BGP_NEIGHBORS:-}
Vasyl Saienko5f953782021-02-25 10:30:32 +020085FRR_EVPN_TUNNELS_RANGE=${FRR_EVPN_TUNNELS_RANGE:-'20 50'}
Vasyl Saienkob3b8c4e2021-02-26 10:54:27 +020086FRR_EVPN_VXLAN_DST_PORT=${FRR_EVPN_VXLAN_DST_PORT:-4790}
Vasyl Saienko25bb75b2021-02-15 12:22:33 +020087
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +020088LVM_LOOP_DEVICE_SIZE=${LVM_LOOP_DEVICE_SIZE:-0}
Vasyl Saienkocd302062021-04-21 22:25:18 +030089CINDER_LVM_LOOP_DEVICE_SIZE=${CINDER_LVM_LOOP_DEVICE_SIZE:-0}
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +020090
Vasyl Saienko8d34ad02021-07-26 11:55:23 +030091_KUBECTL_CMD="kubectl --kubeconfig /etc/kubernetes/admin.conf "
92
Vasyl Saienkoa51d1ca2020-01-02 15:59:16 +020093function retry {
94 local retries=$1
95 shift
96 local msg="$1"
97 shift
98
99 local count=0
100 until "$@"; do
101 exit=$?
102 wait=$((2 ** $count))
103 count=$(($count + 1))
104 if [ $count -lt $retries ]; then
105 echo "Retry $count/$retries exited $exit, retrying in $wait seconds..."
106 sleep $wait
107 else
108 echo "Retry $count/$retries exited $exit, no more retries left."
109 echo "$msg"
110 return $exit
111 fi
112 done
113 return 0
114}
115
Vasyl Saienko300bb0b2023-04-19 06:52:53 +0000116function install_pkgs {
117 local pkgs="$@"
118 function _retry_install {
Vasyl Saienko92133c02024-05-16 10:18:51 +0300119 apt update && DEBIAN_FRONTEND=noninteractive apt install -o DPkg::Options::=--force-confdef -y $pkgs
Vasyl Saienko300bb0b2023-04-19 06:52:53 +0000120 }
121 retry 10 "Labeling node failed" _retry_install
122}
123
Vasyl Saienko806d5dc2022-02-01 14:18:09 +0200124function setup_bind_mounts {
125 mkdir -p /var/lib/persistant-data
Vasyl Saienkoe3107252023-06-06 07:02:58 +0000126 for component in nova glance cinder docker; do
Vasyl Saienko3cc01ca2023-09-25 12:23:43 +0000127 if [[ -d /var/lib/${component} ]]; then
128 continue
129 fi
Vasyl Saienko806d5dc2022-02-01 14:18:09 +0200130 mkdir -p /var/lib/data/${component} /var/lib/${component}
Vasyl Saienko3cc01ca2023-09-25 12:23:43 +0000131 echo "/var/lib/data/${component} /var/lib/${component} none bind 0 0" >> /etc/fstab
Vasyl Saienko806d5dc2022-02-01 14:18:09 +0200132 done
Vasyl Saienkoe3107252023-06-06 07:02:58 +0000133
134 if [[ "${KUBERNETES_INSTALLER}" == "k0s" ]]; then
135 mkdir -p /var/lib/k0s/kubelet
136 mkdir -p /var/lib/kubelet
137 echo "/var/lib/k0s/kubelet /var/lib/kubelet none bind 0 0" >> /etc/fstab
138 fi
139
Vasyl Saienko806d5dc2022-02-01 14:18:09 +0200140 mount -a
141}
142
Vasyl Saienko392dc712020-04-07 15:56:10 +0300143function configure_atop {
144 sed -i 's/INTERVAL=600/INTERVAL=60/' /usr/share/atop/atop.daily
145 systemctl restart atop
146}
147
dbiletskiy70af8192024-06-19 09:27:10 +0200148function disable_unattended_upgr {
Vasyl Saienkoaadd2f82020-08-07 09:35:03 +0300149 # Disable unattended upgrades
150 sed -i 's/1/0/' /etc/apt/apt.conf.d/20auto-upgrades
Oleh Hryhorovbe7bdf72021-04-05 18:21:22 +0300151 sed -i 's/APT::Periodic::Update-Package-Lists "1"/APT::Periodic::Update-Package-Lists "0"/g' /etc/apt/apt.conf.d/*
dbiletskiy70af8192024-06-19 09:27:10 +0200152}
153
Vasyl Saienkof9813512024-07-02 12:30:25 +0300154function configure_logind_conf {
155
156 cat <<EOF >/etc/systemd/logind.conf
157[Login]
158#NAutoVTs=6
159#ReserveVT=6
160#KillUserProcesses=no
161#KillOnlyUsers=
162#KillExcludeUsers=root
163InhibitDelayMaxSec=600
164#UserStopDelaySec=10
165#HandlePowerKey=poweroff
166#HandleSuspendKey=suspend
167#HandleHibernateKey=hibernate
168#HandleLidSwitch=suspend
169#HandleLidSwitchExternalPower=suspend
170#HandleLidSwitchDocked=ignore
171#HandleRebootKey=reboot
172#PowerKeyIgnoreInhibited=no
173#SuspendKeyIgnoreInhibited=no
174#HibernateKeyIgnoreInhibited=no
175#LidSwitchIgnoreInhibited=yes
176#RebootKeyIgnoreInhibited=no
177#HoldoffTimeoutSec=30s
178#IdleAction=ignore
179#IdleActionSec=30min
180#RuntimeDirectorySize=10%
181#RuntimeDirectoryInodesMax=400k
182#RemoveIPC=yes
183#InhibitorsMax=8192
184#SessionsMax=8192
185
186EOF
187
188 systemctl daemon-reload
189
190}
191
dbiletskiy70af8192024-06-19 09:27:10 +0200192function install_required_packages {
Mykyta Karpind6cde5e2022-07-05 12:06:06 +0300193 local pkg_list="apt-transport-https ca-certificates curl software-properties-common jq unzip atop iptables-persistent socat ntp rng-tools open-iscsi nfs-common"
Vasyl Saienkoaadd2f82020-08-07 09:35:03 +0300194
Vasyl Saienkoa7cfc322020-11-06 16:52:22 +0200195 if [[ "${CONFIGURE_HUGE_PAGES}" == true ]]; then
dbiletskiy090662a2024-07-08 14:27:26 +0200196 # Since jammy the igb_uio kernel module contains in the dpdk-kmods-dkms package
197 if [[ ${CODENAME} == "bionic" ]] || [[ ${CODENAME} == "focal" ]]; then
198 pkg_list="$pkg_list dpdk-igb-uio-dkms"
199 else
200 pkg_list="$pkg_list dpdk-kmods-dkms"
201 fi
Anton Samoylovec019d52020-11-19 18:55:47 +0400202 # linux-image-extra-$(uname -r) provides uio_pci_generic module, used by TF DPDK as default driver
203 if [[ "${TUNGSTENFABRIC_ENABLED,,}" == true ]]; then
204 pkg_list="$pkg_list linux-modules-extra-$(uname -r)"
205 fi
Vasyl Saienkoa7cfc322020-11-06 16:52:22 +0200206 fi
Vasyl Saienko300bb0b2023-04-19 06:52:53 +0000207 install_pkgs $pkg_list
208 # Since version 4 yq uses another syntax
Oleksandr Kononenko231933b2024-02-05 16:46:35 +0200209 if [[ ! -f /usr/bin/yq ]]; then
210 curl --retry 6 --retry-delay 5 -L ${BINARY_BASE_URL}/openstack/bin/utils/yq/yq-v3.3.2 -o /usr/bin/yq
211 chmod +x /usr/bin/yq
212 fi
Mykyta Karpin5ed5fd82020-01-27 18:23:29 +0200213}
214
Oleksii Grudev4399e232021-02-24 19:08:25 +0200215function enable_iscsi {
216 systemctl enable iscsid
217 systemctl restart iscsid
218}
Mykyta Karpin5ed5fd82020-01-27 18:23:29 +0200219
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +0200220function configure_lvm {
Vasyl Saienkocd302062021-04-21 22:25:18 +0300221 function _setup_lvm {
222 local device_num=$1
223 local device_size=$2
224 local vg_name=$3
Denis Biletskiy3b21a822022-05-10 10:29:29 +0300225 if vgdisplay | grep ${vg_name}; then
226 echo "Volume group ${vg_name} is already initialize"
Deniscaa72dd2021-06-04 17:13:05 +0300227 else
228 #configure lvm only on compute nodes
229 if [[ ${NODE_METADATA} == *"openstack-compute-node"* ]]; then
230 truncate --size ${device_size}G /srv/disk${device_num}
231 cat <<EOF > /etc/systemd/system/setup-loopback-loop${device_num}.service
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +0200232[Unit]
Vasyl Saienkocd302062021-04-21 22:25:18 +0300233Description=Setup loop${device_num} device
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +0200234DefaultDependencies=no
235After=systemd-udev-settle.service
236Before=lvm2-activation-early.service
237Wants=systemd-udev-settle.service
238
239[Service]
Vasyl Saienkocd302062021-04-21 22:25:18 +0300240ExecStart=-/sbin/losetup /dev/loop${device_num} /srv/disk${device_num}
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +0200241RemainAfterExit=true
242Type=oneshot
243
244[Install]
245WantedBy=local-fs.target
246EOF
Deniscaa72dd2021-06-04 17:13:05 +0300247 systemctl enable setup-loopback-loop${device_num}
248 systemctl start setup-loopback-loop${device_num}
249 #adding short sleep to give time for service to start
250 sleep 3
251 pvcreate /dev/loop${device_num}
252 vgcreate ${vg_name} /dev/loop${device_num}
253 fi
Vasyl Saienkocd302062021-04-21 22:25:18 +0300254 fi
255 }
256
257 if [[ "${LVM_LOOP_DEVICE_SIZE}" -gt 0 ]]; then
Denis Biletskiy3b21a822022-05-10 10:29:29 +0300258 _setup_lvm 10 ${LVM_LOOP_DEVICE_SIZE} nova-vol
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +0200259 fi
Vasyl Saienkocd302062021-04-21 22:25:18 +0300260 if [[ "${CINDER_LVM_LOOP_DEVICE_SIZE}" -gt 0 ]]; then
Denis Biletskiy3b21a822022-05-10 10:29:29 +0300261 _setup_lvm 11 ${CINDER_LVM_LOOP_DEVICE_SIZE} cinder-vol
Vasyl Saienkocd302062021-04-21 22:25:18 +0300262 fi
263
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +0200264}
265
Mykyta Karpin5ed5fd82020-01-27 18:23:29 +0200266function install_docker {
267 function install_retry {
Vasyl Saienkofa714102024-03-22 09:30:25 +0200268 curl --retry 6 --retry-delay 5 -fsSL "${DOCKER_EE_URL}/gpg" | sudo apt-key add -
269 add-apt-repository "deb [arch=amd64] ${DOCKER_EE_URL}/ ${OS_CODENAME} ${DOCKER_EE_RELEASE}"
270 install_pkgs ${DOCKER_EE_PACKAGES}
Vasyl Saienko92133c02024-05-16 10:18:51 +0300271 if [[ $KUBERNETES_CONTAINER_RUNTIME == "crio" ]]; then
272 NERDCTL_VERSION=1.7.6
273 curl --retry 6 --retry-delay 5 -fsSL https://github.com/containerd/nerdctl/releases/download/v${NERDCTL_VERSION}/nerdctl-${NERDCTL_VERSION}-linux-amd64.tar.gz --output nerdctl-${NERDCTL_VERSION}-linux-amd64.tar.gz
274 tar zxvf nerdctl-${NERDCTL_VERSION}-linux-amd64.tar.gz -C /usr/local/bin
275 fi
Mykyta Karpin5ed5fd82020-01-27 18:23:29 +0200276 }
277 retry 10 "Failed to install docker" install_retry
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100278}
279
Mykyta Karpin7abaea02020-07-27 15:43:07 +0300280function prepare_docker_config {
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100281 mkdir -p /etc/docker
282 cat <<EOF > /etc/docker/daemon.json
283{
Vasyl Saienkoa37de442024-05-02 17:15:47 +0300284 "default-cgroupns-mode": "host",
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100285 "default-address-pools": [
286 { "base": "${DOCKER_DEFAULT_ADDRESS_POOL}", "size": ${DOCKER_DEFAULT_ADDRESS_SIZE} }
Mykyta Karpin7abaea02020-07-27 15:43:07 +0300287 ],
288 "log-driver": "json-file",
289 "log-opts": {
290 "max-size": "1g",
291 "max-file": "3"
292 }
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100293}
294EOF
295
Vasyl Saienko92133c02024-05-16 10:18:51 +0300296 if [[ $KUBERNETES_CONTAINER_RUNTIME == "crio" ]]; then
297 mkdir -p /etc/containerd/
298cat <<EOF> /etc/containerd/config.toml
Vasyl Saienko05edc402024-06-11 11:17:21 +0300299version = 2
300[plugins]
301[plugins."io.containerd.grpc.v1.cri".containerd.runtimes]
302 [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
303 runtime_type = "io.containerd.runc.v2"
304 [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
305 SystemdCgroup = true
Vasyl Saienko92133c02024-05-16 10:18:51 +0300306EOF
Vasyl Saienko05edc402024-06-11 11:17:21 +0300307 systemctl restart containerd || /bin/true
Vasyl Saienko92133c02024-05-16 10:18:51 +0300308 fi
309
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100310}
311
Vasyl Saienkoe32453e2020-09-16 09:30:34 +0300312function pull_image {
313 local image="$1"
314 function pull_image_retry {
Vasyl Saienko92133c02024-05-16 10:18:51 +0300315 if [[ $KUBERNETES_CONTAINER_RUNTIME == "crio" ]]; then
316 nerdctl --address unix:///run/containerd/containerd.sock -n k8s.io pull $image
317 fi
Vasyl Saienkoe32453e2020-09-16 09:30:34 +0300318 docker pull ${image}
319 }
320 retry 10 "Can't pool docker image $image" pull_image_retry
321}
322
Vasyl Saienko84ee4fd2020-09-02 10:00:18 +0300323function cache_images {
324
Vasyl Saienkoe32453e2020-09-16 09:30:34 +0300325 # Cache UCP image with retries
326 pull_image "${DOCKER_UCP_IMAGE}"
Vasyl Saienko84ee4fd2020-09-02 10:00:18 +0300327
Vasyl Saienkoe32453e2020-09-16 09:30:34 +0300328 local image_prefix=${DOCKER_UCP_IMAGE%/*}
Oleksandr Kononenkobf4ecf82024-02-01 11:02:53 +0200329 for image in $(docker container run --rm --name ucp -v /var/run/docker.sock:/var/run/docker.sock ${DOCKER_UCP_IMAGE} images --list); do
330 pull_image "${image_prefix}/${image##*/}"
Vasyl Saienko92133c02024-05-16 10:18:51 +0300331 if [[ $KUBERNETES_CONTAINER_RUNTIME == "crio" ]]; then
332 nerdctl --address unix:///run/containerd/containerd.sock -n k8s.io tag ${image_prefix}/${image##*/} mirantis/${image##*/}
333 fi
Oleksandr Kononenkobf4ecf82024-02-01 11:02:53 +0200334 docker tag ${image_prefix}/${image##*/} mirantis/${image##*/}
335 done
Vasyl Saienko84ee4fd2020-09-02 10:00:18 +0300336}
337
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100338function install_ucp {
Deniscaa72dd2021-06-04 17:13:05 +0300339 if docker ps --all | grep ucp-controller; then
340 echo "Docker UCP container is running"
341 else
342 local tmpd
343 tmpd=$(mktemp -d)
344 function docker_run_retry {
345 docker container run --rm --name ucp \
346 -v /var/run/docker.sock:/var/run/docker.sock \
347 ${DOCKER_UCP_IMAGE} install \
348 --host-address $UCP_IP_ADDRESS \
349 --admin-username $UCP_USERNAME \
350 --admin-password $UCP_PASSWORD \
351 --existing-config
352 }
353 retry 10 "Can't bring up docker UCP container" docker_run_retry
354 fi
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100355}
356
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300357function download_k0s {
358 curl -sSLf https://get.k0s.sh | sudo sh
359}
360
361function install_k0s {
Vasyl Saienko59e5b2a2024-09-09 08:57:18 +0300362 local install_flags="--enable-worker"
363 if [[ "${SINGLE_NODE}" == true ]]; then
364 install_flags="$install_flags --single"
365 fi
Vasyl Saienkoe3107252023-06-06 07:02:58 +0000366 mkdir -p /etc/k0s
367 k0s config create > /etc/k0s/k0s.yaml
Vasyl Saienko59e5b2a2024-09-09 08:57:18 +0300368 k0s install controller -c /etc/k0s/k0s.yaml $install_flags
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300369 k0s start
370
Vasyl Saienkoe3107252023-06-06 07:02:58 +0000371 sleep 60
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300372
Vasyl Saienkoe2c68dd2022-01-27 17:10:12 +0200373 mkdir -p /etc/kubernetes /root/.kube/
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300374 k0s kubeconfig admin > /etc/kubernetes/admin.conf
375 cp /etc/kubernetes/admin.conf /root/.kube/config
376
377 mkdir -p /etc/k0s
378 k0s kubeconfig admin > /etc/k0s/admin.conf
Vasyl Saienko59e5b2a2024-09-09 08:57:18 +0300379 if [[ "${SINGLE_NODE}" != true ]]; then
380 k0s token create --role=worker > /etc/k0s/worker_token.yaml
381 k0s token create --role=controller > /etc/k0s/controller_token.yaml
382 fi
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300383
Vasyl Saienko300bb0b2023-04-19 06:52:53 +0000384 install_pkgs nginx
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300385 rm -f /etc/nginx/sites-enabled/default
386 ln -s /etc/k0s/ /var/www/k0s
387 cat << EOF > /etc/nginx/sites-enabled/k0s
388server {
389 listen *:80;
390 root /var/www;
391
392 location /k0s {
393 autoindex on;
394 }
395}
396EOF
397 systemctl restart nginx
398}
399
Oleh Hryhorovcd1a3bd2020-12-04 13:21:06 +0200400function get_authtoken_retry {
401# Download the bundle https://docs.docker.com/ee/ucp/user-access/cli/
402# Create an environment variable with the user security token
403 AUTHTOKEN=$(curl --retry 6 --retry-delay 5 -sk -d '{"username":"'$UCP_USERNAME'","password":"'$UCP_PASSWORD'"}' https://${UCP_MASTER_HOST}/auth/login | jq -r .auth_token)
404 if [ -z ${AUTHTOKEN} ]; then
405 return -1
406 fi
407}
408
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100409function download_bundles {
410 local tmpd
411 tmpd=$(mktemp -d)
Oleh Hryhorova0754872020-01-24 16:45:05 +0200412
413 function download_bundles_retry {
414 # Download the client certificate bundle
Vasyl Saienkof3dba8c2020-02-14 10:07:23 +0200415 curl --retry 6 --retry-delay 5 -k -H "Authorization: Bearer $AUTHTOKEN" https://${UCP_MASTER_HOST}/api/clientbundle -o ${tmpd}/bundle.zip
Oleh Hryhorova0754872020-01-24 16:45:05 +0200416 }
Oleh Hryhorovb58460e2020-02-05 15:53:48 +0200417
Vasyl Saienko9b688992020-02-13 13:50:53 +0200418 retry 10 "Can't get AUTHTOKEN from master." get_authtoken_retry
419 retry 10 "Can't download bundle file from master." download_bundles_retry
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100420
421 pushd $tmpd
422 # Unzip the bundle.
423 unzip bundle.zip
424
425 # Run the utility script.
426 eval "$(<env.sh)"
Vasyl Saienkof9f668c2020-01-04 11:36:35 +0200427 mkdir -p /etc/kubernetes /root/.kube/
Vasyl Saienko5b379422020-01-04 12:24:08 +0200428 cp kube.yml /etc/kubernetes/admin.conf
429 cp kube.yml /root/.kube/config
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100430 popd
431}
432
Vasyl Saienko3ab29c92020-01-03 10:39:03 +0200433function wait_for_node {
434 function retry_wait {
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300435 ${_KUBECTL_CMD} get nodes |grep -w Ready |awk '{print $1}' |grep -q $(hostname)
Vasyl Saienko3ab29c92020-01-03 10:39:03 +0200436 }
437 retry $NODE_DEPLOYMENT_RETRIES "The node didn't come up." retry_wait
438}
439
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300440function download_k8s_metadata {
441 mkdir -p /etc/k0s/
442 mkdir -p /etc/kubernetes
443 for f in worker_token.yaml controller_token.yaml admin.conf; do
444 curl --retry 6 --retry-delay 5 -L http://${UCP_MASTER_HOST}/k0s/${f} -o /etc/k0s/${f}
445 done
446 cp /etc/k0s/admin.conf /etc/kubernetes/admin.conf
447 mkdir -p /root/.kube/
448 cp /etc/k0s/admin.conf /root/.kube/config
449}
450
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100451function join_node {
Deniscaa72dd2021-06-04 17:13:05 +0300452 if kubectl --kubeconfig /etc/kubernetes/admin.conf get nodes |grep -w Ready |awk '{print $1}' |grep -q $(hostname); then
453 echo "This node is already join"
454 else
455 local type=${1}
456 function retry_join_node {
457 env -i $(docker swarm join-token $type |grep 'docker swarm join' | xargs)
Mykyta Karpine2d2fda2023-11-23 10:41:51 +0200458 sleep 20 && systemctl restart docker && sleep 20
Deniscaa72dd2021-06-04 17:13:05 +0300459 }
460 retry 10 "Failed to join node to swarm" retry_join_node
461 fi
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100462}
463
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300464function join_k0s_node {
465 k0s install worker --token-file /etc/k0s/worker_token.yaml
466 k0s start
467}
468
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100469function create_ucp_config {
Oleh Hryhorovcd1a3bd2020-12-04 13:21:06 +0200470 if [[ "${SINGLE_NODE}" == true ]]; then
471 max_pods="kubelet_max_pods = 220"
472 fi
Michael Polenchuk06bfa152022-02-15 17:50:04 +0400473 if [[ "${SECURE_OVERLAY_ENABLED,,}" == true ]]; then
474 secure_overlay="secure_overlay = true
475 calico_vxlan = false"
476 fi
Vasyl Saienko92133c02024-05-16 10:18:51 +0300477 kubelet_custom_flags=""
478 if [[ $KUBERNETES_CONTAINER_RUNTIME == "crio" ]]; then
Vasyl Saienko05edc402024-06-11 11:17:21 +0300479 kubelet_custom_flags='custom_kubelet_flags=["--cgroup-driver=systemd", "--container-runtime-endpoint=unix:///run/containerd/containerd.sock"]'
Vasyl Saienko92133c02024-05-16 10:18:51 +0300480 fi
Deniscaa72dd2021-06-04 17:13:05 +0300481 if docker config ls | grep com.docker.ucp.config ; then
482 echo "Config com.docker.ucp.config already exists"
483 else
484 echo "
Vasyl Saienko2fcc70b2020-01-02 10:09:08 +0200485[scheduling_configuration]
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100486 enable_admin_ucp_scheduling = true
Vasyl Saienko2fcc70b2020-01-02 10:09:08 +0200487 default_node_orchestrator = \"kubernetes\"
Vasyl Saienkoccd3a752020-01-02 10:21:54 +0200488[cluster_config]
489 dns = [\"172.18.208.44\"]
Vasyl Saienko06c7afe2022-07-01 17:53:56 +0000490 calico_vxlan_port = \"$UCP_DOCKER_CALICO_VXLAN_PORT\"
Oleh Hryhorovcd1a3bd2020-12-04 13:21:06 +0200491 ${max_pods}
Michael Polenchuk06bfa152022-02-15 17:50:04 +0400492 ${secure_overlay}
Vasyl Saienko92133c02024-05-16 10:18:51 +0300493 ${kubelet_custom_flags}
Vasyl Saienkoccd3a752020-01-02 10:21:54 +0200494" | docker config create com.docker.ucp.config -
Deniscaa72dd2021-06-04 17:13:05 +0300495 fi
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100496}
497
498function swarm_init {
Deniscaa72dd2021-06-04 17:13:05 +0300499 if docker node ls | grep $HOSTNAME; then
500 echo "This node is already part of a swarm"
501 else
502 docker swarm init --advertise-addr ${UCP_IP_ADDRESS} --data-path-addr ${UCP_IP_ADDRESS} --listen-addr ${UCP_IP_ADDRESS} --data-path-port ${UCP_DOCKER_SWARM_DATA_PORT}
Vasyl Saienko80b4c592023-10-13 14:24:00 +0000503 sleep 5 && systemctl restart docker && sleep 5
Deniscaa72dd2021-06-04 17:13:05 +0300504 fi
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100505}
506
507function rm_ucp_config {
508 docker config rm com.docker.ucp.config
509}
510
Vasyl Saienkob58c7a52020-01-02 09:14:40 +0200511function install_kubectl {
Michael Polenchuk92ca1b32020-11-13 15:58:38 +0400512 local kubectl=/usr/local/bin/kubectl
Vasyl Saienkoa94bf182023-04-01 10:42:17 +0000513 curl --retry 6 --retry-delay 5 -L ${BINARY_BASE_URL}/openstack/bin/utils/kubectl/kubectl-${KUBECTL_VERSION}-linux -o ${kubectl}
Michael Polenchuk92ca1b32020-11-13 15:58:38 +0400514 chmod +x ${kubectl}
Ilya Menkov2d22bd02020-02-14 15:01:29 +0400515cat << EOF >> ~/.bashrc
516source /usr/share/bash-completion/bash_completion
517source <(kubectl completion bash)
518EOF
Vasyl Saienkob58c7a52020-01-02 09:14:40 +0200519}
520
Vasyl Saienko08166342020-02-28 12:20:51 +0200521function configure_ntp {
Vasyl Saienkoe2c68dd2022-01-27 17:10:12 +0200522 echo "" > /etc/ntp.conf
Vasyl Saienko08166342020-02-28 12:20:51 +0200523
Anton Samoylov6d2cf7b2020-09-01 11:35:11 +0400524 for server in $NTP_SERVERS; do
525cat << EOF >> /etc/ntp.conf
526server ${server} iburst
527EOF
528 done
529
530 cat << EOF >> /etc/ntp.conf
531# Set general access to this service
532restrict -4 default kod nomodify notrap nopeer noquery
533restrict -6 default kod nomodify notrap nopeer noquery
534
535# Allow access from localhost
536restrict 127.0.0.1
537restrict ::1
538
539# Do not listen on any interface address by default
540interface ignore wildcard
541interface listen lo
542interface listen ${DEFAULT_INTERFACE}
543
544# Location of drift file
545driftfile /var/lib/ntp/ntp.drift
546EOF
547
ibumarskov35370042022-04-19 14:27:02 +0400548 systemctl disable systemd-timesyncd
549 systemctl enable ntp
Anton Samoylov6d2cf7b2020-09-01 11:35:11 +0400550 systemctl restart ntp
Vasyl Saienko08166342020-02-28 12:20:51 +0200551}
552
Vasyl Saienko5af97c32022-01-25 09:45:45 +0200553function wait_for_external_network {
554 function _check_access {
555 curl --connect-timeout 10 ${DOCKER_UCP_IMAGE%%/*} || (sleep 1; /bin/false)
556 }
557 retry 180 "Failed to wait for external networks reachable." _check_access
558}
559
Vasyl Saienko2fcc70b2020-01-02 10:09:08 +0200560function prepare_network {
Dennis Dmitrievea894212020-03-12 21:36:43 +0200561 if [ -z "${CONTROL_IP_ADDRESS}" ]; then
Vasyl Saienko7c595922021-03-17 13:58:13 +0200562 wait_condition_send "FAILURE" "CONTROL_IP_ADDRESS is not found."
Dennis Dmitrievea894212020-03-12 21:36:43 +0200563 exit 1
564 fi
565
Vasyl Saienko2fcc70b2020-01-02 10:09:08 +0200566 systemctl restart systemd-resolved
Vasyl Saienkoc14c43d2020-01-08 13:06:18 +0200567 # Make sure local hostname is present in /etc/hosts
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300568 sed -i "s/127.0.0.1 localhost/127.0.0.1 localhost\n${CONTROL_IP_ADDRESS} $(hostname -s).cluster.local $(hostname -s)/" /etc/hosts
Vasyl Saienko2fcc70b2020-01-02 10:09:08 +0200569}
570
Vasyl Saienko7d7acea2020-01-03 16:59:05 +0200571function workaround_default_forward_policy {
Vasyl Saienkod67e35e2023-09-27 14:44:01 +0000572 local should_run=0
573 for net in $FLOATING_NETWORK_PREFIXES; do
574 if ! iptables -nvL DOCKER-USER |grep $net; then
575 should_run=1
576 break
577 fi
578 done
579 if [[ $should_run == 0 ]]; then
580 echo "Iptables is already configured"
Deniscaa72dd2021-06-04 17:13:05 +0300581 else
582 cat << EOF > /etc/iptables/rules.v4
Vasyl Saienkobd34d2a2020-03-10 16:25:17 +0200583*filter
584:DOCKER-USER - [0:0]
585EOF
Deniscaa72dd2021-06-04 17:13:05 +0300586 for net in $FLOATING_NETWORK_PREFIXES; do
Vasyl Saienkobd34d2a2020-03-10 16:25:17 +0200587cat << EOF >> /etc/iptables/rules.v4
588-A DOCKER-USER -d ${net} -j ACCEPT
589-A DOCKER-USER -s ${net} -j ACCEPT
Vasyl Saienkobd34d2a2020-03-10 16:25:17 +0200590EOF
Deniscaa72dd2021-06-04 17:13:05 +0300591 done
Vasyl Saienkobd34d2a2020-03-10 16:25:17 +0200592
593cat << EOF >> /etc/iptables/rules.v4
594-A DOCKER-USER -j RETURN
Vasyl Saienko1785c352020-03-11 11:39:40 +0200595COMMIT
Vasyl Saienkobd34d2a2020-03-10 16:25:17 +0200596EOF
Vasyl Saienko02df39a2020-05-20 16:39:35 +0300597
598#Allow access to Internet from VMs for virtualized environment.
599cat << EOF >> /etc/iptables/rules.v4
600*nat
601:POSTROUTING ACCEPT - [0:0]
602EOF
Deniscaa72dd2021-06-04 17:13:05 +0300603 for net in $FLOATING_NETWORK_PREFIXES; do
Vasyl Saienko02df39a2020-05-20 16:39:35 +0300604cat << EOF >> /etc/iptables/rules.v4
605-A POSTROUTING -s ${net} -o ${DEFAULT_INTERFACE} -j MASQUERADE
606EOF
Deniscaa72dd2021-06-04 17:13:05 +0300607 done
Vasyl Saienko02df39a2020-05-20 16:39:35 +0300608
609cat << EOF >> /etc/iptables/rules.v4
610COMMIT
611EOF
Deniscaa72dd2021-06-04 17:13:05 +0300612 sudo netfilter-persistent reload
613 fi
Vasyl Saienko7d7acea2020-01-03 16:59:05 +0200614}
615
Dennis Dmitrievea894212020-03-12 21:36:43 +0200616function disable_rp_filter {
617 # Run this func before "network_config" to create new interfaces with the default rp_filter value
618 cat << EOF > /etc/sysctl.d/99-disable-rp-filter.conf
619net.ipv4.conf.all.rp_filter=0
620net.ipv4.conf.default.rp_filter=0
621EOF
Vasyl Saienkoe61a98c2021-10-19 21:42:19 +0300622 for iface in $(find /proc/sys/net/ipv4/conf/ -name "enp*" -o -name "ens*" -o -name "eth*"); do
623 echo 0 > $iface/rp_filter
Oleh Hryhorov207137d2021-04-13 12:03:39 +0300624 done
625
Dennis Dmitrievea894212020-03-12 21:36:43 +0200626 sysctl -p /etc/sysctl.d/99-disable-rp-filter.conf
627}
628
Vasyl Saienko45280eb2024-06-24 11:45:13 +0000629function configure_sysctl_limits {
630 cat << EOF > /etc/sysctl.d/99-limits.conf
631fs.inotify.max_user_instances = 8192
632EOF
633 sysctl -p /etc/sysctl.d/99-limits.conf
634}
635
Vasyl Saienko83f309e2020-05-06 11:14:28 +0300636function configure_contrack {
637 cat << EOF > /etc/sysctl.d/100-contrackd.conf
638net.netfilter.nf_conntrack_log_invalid=255
639net.netfilter.nf_conntrack_tcp_be_liberal=1
640EOF
641
642 sysctl -p /etc/sysctl.d/100-contrackd.conf
643}
644
Vasyl Saienkob377de02020-05-25 14:51:31 +0300645# NOTE(vsaienko): disable calling iptables when sending packets via bridge.
646function disable_iptables_for_bridges {
647 cat << EOF > /etc/sysctl.d/101-bridge-nf-call-iptables.conf
648net.bridge.bridge-nf-call-ip6tables=0
649net.bridge.bridge-nf-call-iptables=0
650net.bridge.bridge-nf-call-arptables=0
651EOF
652
653 sysctl -p /etc/sysctl.d/101-bridge-nf-call-iptables.conf
654}
655
Oleh Hryhorovb58460e2020-02-05 15:53:48 +0200656function network_config {
Vasyl Saienko9b688992020-02-13 13:50:53 +0200657 PUBLIC_NODE_IP_ADDRESS=${PUBLIC_INTERFACE_IP:-$(ip addr show dev ${PUBLIC_INTERFACE} | grep -Po 'inet \K[\d.]+' | egrep -v "127.0.|172.17")}
658 PUBLIC_NODE_IP_NETMASK=${PUBLIC_INTERFACE_NETMASK:-$(ip addr show dev ${PUBLIC_INTERFACE} | grep -Po 'inet \K[\d.]+\/[\d]+' | egrep -v "127.0.|172.17" | cut -d'/' -f2)}
659
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200660 local public_interface=${1:-${PUBLIC_INTERFACE}}
661 local cloud_netplan_cfg="/etc/netplan/50-cloud-init.yaml"
662 local match_ip_line
Vasyl Saienko9b688992020-02-13 13:50:53 +0200663
Vasyl Saienko300bb0b2023-04-19 06:52:53 +0000664 install_pkgs bridge-utils
Mykyta Karpin5ed5fd82020-01-27 18:23:29 +0200665
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200666cat << EOF > /etc/systemd/network/10-veth-phy-br.netdev
667[NetDev]
668Name=veth-phy
669Kind=veth
670[Peer]
671Name=veth-br
672EOF
Vasyl Saienko9b688992020-02-13 13:50:53 +0200673
Vasyl Saienko66522982020-05-25 09:34:43 +0300674if [[ -n ${IRONIC_BAREMETAL_INTERFACE} ]]; then
675
Vasyl Saienko4c468192020-05-19 11:51:13 +0300676cat << EOF > /etc/systemd/network/11-veth-bm.netdev
677[NetDev]
678Name=veth-bm
679Kind=veth
680[Peer]
681Name=veth-bm-br
682EOF
683
Vasyl Saienko1233afd2020-05-21 14:55:21 +0300684cat << EOF > /etc/systemd/network/12-veth-vbmc.netdev
685[NetDev]
686Name=veth-vbmc
687Kind=veth
688[Peer]
689Name=veth-vbmc-br
690EOF
Vasyl Saienko66522982020-05-25 09:34:43 +0300691fi
Vasyl Saienko1233afd2020-05-21 14:55:21 +0300692
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200693 sed -i 's/.*ethernets:.*/&\n veth-phy: {}/' ${cloud_netplan_cfg}
694 sed -i 's/.*ethernets:.*/&\n veth-br: {}/' ${cloud_netplan_cfg}
Vasyl Saienko66522982020-05-25 09:34:43 +0300695if [[ -n ${IRONIC_BAREMETAL_INTERFACE} ]]; then
Vasyl Saienko4c468192020-05-19 11:51:13 +0300696 sed -i 's/.*ethernets:.*/&\n veth-bm: {}/' ${cloud_netplan_cfg}
697 sed -i 's/.*ethernets:.*/&\n veth-bm-br: {}/' ${cloud_netplan_cfg}
Vasyl Saienko1233afd2020-05-21 14:55:21 +0300698 sed -i 's/.*ethernets:.*/&\n veth-vbmc: {}/' ${cloud_netplan_cfg}
699 sed -i 's/.*ethernets:.*/&\n veth-vbmc-br: {}/' ${cloud_netplan_cfg}
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200700
Michael Polenchuk486cadf2020-08-24 16:25:51 +0400701 if [[ "${IRONIC_MT_ENABLED,,}" == true ]] ; then
702 sed -i "s/.*ethernets:.*/&\n ${IRONIC_BAREMETAL_VXLAN_INTERFACE}: {}/" ${cloud_netplan_cfg}
703 sed -i 's/.*ethernets:.*/&\n veth-pxe: {}/' ${cloud_netplan_cfg}
704 sed -i 's/.*ethernets:.*/&\n veth-pxe-br: {}/' ${cloud_netplan_cfg}
705cat << EOF > /etc/systemd/network/13-veth-pxe.netdev
706[NetDev]
707Name=veth-pxe
708Kind=veth
709[Peer]
710Name=veth-pxe-br
711EOF
712 sed -i "s/- ${IRONIC_BAREMETAL_NETWORK_PREFIX}\([0-9]*\)/- ${IRONIC_BAREMETAL_TUNNEL_NETWORK_PREFIX}\1/" ${cloud_netplan_cfg}
713cat << EOF >> ${cloud_netplan_cfg}
714 vlans:
715 pxe.1000:
716 id: 1000
717 link: veth-pxe
718 addresses:
719 - ${IRONIC_BAREMETAL_INTERFACE_IP}/${IRONIC_BAREMETAL_NETWORK_NETMASK}
720EOF
721 else
722 ironic_baremetal_address_match_ip_line=$(grep -nm1 "${IRONIC_BAREMETAL_INTERFACE_IP}/${IRONIC_BAREMETAL_NETWORK_NETMASK}" ${cloud_netplan_cfg} | cut -d: -f1)
723 sed -i "$((${ironic_baremetal_address_match_ip_line}-1)),$((${ironic_baremetal_address_match_ip_line}))d" ${cloud_netplan_cfg}
724 fi
Vasyl Saienko66522982020-05-25 09:34:43 +0300725fi
Vasyl Saienko4c468192020-05-19 11:51:13 +0300726
Vasyl Saienko66522982020-05-25 09:34:43 +0300727 public_address_match_ip_line=$(grep -nm1 "${PUBLIC_NODE_IP_ADDRESS}/${PUBLIC_NODE_IP_NETMASK}" ${cloud_netplan_cfg} | cut -d: -f1)
Denis20740c22021-06-17 18:09:46 +0300728 if [ -n "${public_address_match_ip_line}" ] ; then
729 sed -i "$((${public_address_match_ip_line}-1)),$((${public_address_match_ip_line}))d" ${cloud_netplan_cfg}
730 fi
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200731
732cat << EOF >> ${cloud_netplan_cfg}
733 bridges:
734 br-public:
735 dhcp4: false
736 interfaces:
737 - ${PUBLIC_INTERFACE}
738 - veth-br
739 addresses:
740 - ${PUBLIC_NODE_IP_ADDRESS}/${PUBLIC_NODE_IP_NETMASK}
Vasyl Saienko66522982020-05-25 09:34:43 +0300741EOF
Vasyl Saienko5b6d70a2023-04-10 13:55:31 +0000742# Assign more ips for neutron dynamic routing PRODX-31417
743if [[ ${NODE_METADATA} == *"tempest"* ]]; then
744 for i in {71..76}; do
745cat << EOF >> ${cloud_netplan_cfg}
Vasyl Saienkoc7639002023-04-11 07:40:29 +0000746 - ${PUBLIC_NODE_IP_ADDRESS%.*}.${i}/${PUBLIC_NODE_IP_NETMASK}
Vasyl Saienko5b6d70a2023-04-10 13:55:31 +0000747EOF
748 done
749fi
Vasyl Saienko66522982020-05-25 09:34:43 +0300750
Vasyl Saienkof79d0942020-11-17 14:33:55 +0200751# Remove Tunnel interface from netplan
752if [[ $TUNNEL_INTERFACE_NETPLAN_MANAGE == false ]]; then
753 sed -i "/ ${TUNNEL_INTERFACE}/,/ set-name: ${TUNNEL_INTERFACE}/d" ${cloud_netplan_cfg}
754fi
755
Vasyl Saienko66522982020-05-25 09:34:43 +0300756if [[ -n ${IRONIC_BAREMETAL_INTERFACE} ]]; then
757cat << EOF >> ${cloud_netplan_cfg}
Vasyl Saienko4c468192020-05-19 11:51:13 +0300758 br-baremetal:
759 dhcp4: false
760 interfaces:
Vasyl Saienko4c468192020-05-19 11:51:13 +0300761 - veth-bm-br
Vasyl Saienko1233afd2020-05-21 14:55:21 +0300762 - veth-vbmc-br
Michael Polenchuk486cadf2020-08-24 16:25:51 +0400763EOF
764 if [[ "${IRONIC_MT_ENABLED,,}" != true ]] ; then
765cat << EOF >> ${cloud_netplan_cfg}
766 - ${IRONIC_BAREMETAL_INTERFACE}
Vasyl Saienko4c468192020-05-19 11:51:13 +0300767 addresses:
768 - ${IRONIC_BAREMETAL_INTERFACE_IP}/${IRONIC_BAREMETAL_NETWORK_NETMASK}
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200769EOF
Michael Polenchuk486cadf2020-08-24 16:25:51 +0400770 else
771cat << EOF >> ${cloud_netplan_cfg}
772 - ${IRONIC_BAREMETAL_VXLAN_INTERFACE}
773 - veth-pxe-br
774EOF
Vasyl Saienko3dd0c272020-11-16 17:20:07 +0200775
Michael Polenchuk486cadf2020-08-24 16:25:51 +0400776cat << EOF > /etc/systemd/system/ironic-vxlan-tunnel.service
777[Unit]
778Description=Ironic VXLAN tunnel
779After=network.target
780
781[Service]
782Type=oneshot
783RemainAfterExit=true
784ExecStart=/sbin/ip link add ${IRONIC_BAREMETAL_VXLAN_INTERFACE} type vxlan id 10 group 239.1.1.10 dstport 0 dev ${IRONIC_BAREMETAL_INTERFACE}
785ExecStart=/sbin/ip link set ${IRONIC_BAREMETAL_VXLAN_INTERFACE} up
786
787[Install]
788WantedBy=multi-user.target
789EOF
790
791 systemctl enable ironic-vxlan-tunnel.service
792 systemctl start ironic-vxlan-tunnel.service
793 fi
Vasyl Saienko66522982020-05-25 09:34:43 +0300794fi
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200795 netplan --debug apply
Vasyl Saienko9b688992020-02-13 13:50:53 +0200796
797 # NOTE(vsaienko): give some time to apply changes
798 sleep 15
Vasyl Saienko16c81c02020-11-17 17:10:55 +0200799
800# Remove Tunnel interface from netplan
801if [[ $TUNNEL_INTERFACE_NETPLAN_MANAGE == false ]]; then
802 ip addr flush ${TUNNEL_INTERFACE}
803 ip link set ${TUNNEL_INTERFACE} up
804fi
805
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200806}
807
Oleh Hryhorovb58460e2020-02-05 15:53:48 +0200808$functions_override
809
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200810function set_node_labels {
Vasyl Saienko2ae2eec2020-02-20 10:11:32 +0200811 function set_node_labels_retry {
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300812 ${_KUBECTL_CMD} patch node $(hostname) -p "{\"metadata\": $(echo $NODE_METADATA | jq -c ".")}"
Vasyl Saienko2ae2eec2020-02-20 10:11:32 +0200813 }
Vasyl Saienko08024582020-02-20 11:45:34 +0200814 retry 10 "Labeling node failed" set_node_labels_retry
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200815}
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100816
Vasyl Saienko1eb06d62020-11-16 10:16:22 +0200817HW_METADATA='{}'
Vasyl Saienkof9ee1582020-03-02 16:53:41 +0200818function prepare_metadata_files {
Dennis Dmitrievea894212020-03-12 21:36:43 +0200819 /usr/sbin/prepare-metadata.py --metadata-file /usr/share/metadata/lab-metadata.yaml
820}
821
822function collect_ceph_metadata {
Oleh Hryhorovf14fc922020-03-10 12:11:10 +0200823 local ceph_osd_node
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300824 ceph_osd_node=$(${_KUBECTL_CMD} get nodes -l role=ceph-osd-node -o jsonpath={.items[?\(@.metadata.name==\"$(hostname)\"\)].metadata.name})
Oleh Hryhorovf14fc922020-03-10 12:11:10 +0200825
Oleh Hryhorovf14fc922020-03-10 12:11:10 +0200826 if [[ -f /usr/share/metadata/ceph.yaml && ${ceph_osd_node} ]]; then
Vasyl Saienkof9ee1582020-03-02 16:53:41 +0200827 HW_METADATA="{\"ceph\": {\"$(hostname)\": \"$(base64 -w 0 /usr/share/metadata/ceph.yaml)\"}}"
Oleh Hryhorov39fbfe52020-03-12 15:26:53 +0200828 ceph_store_drive=$(cat /usr/share/metadata/ceph.yaml | egrep '\- name\: vd?' | awk '{print $3}')
829 if [[ -b /dev/${ceph_store_drive} ]]; then
830 sgdisk --zap-all /dev/${ceph_store_drive}
831 fi
Vasyl Saienkof9ee1582020-03-02 16:53:41 +0200832 fi
833}
834
Oleh Hryhorove7feab42020-11-11 18:00:29 +0200835function collect_interfaces_metadata {
Vasyl Saienko3dd0c272020-11-16 17:20:07 +0200836
Oleh Hryhorove7feab42020-11-11 18:00:29 +0200837 local if_metadata_file="/usr/share/metadata/interfaces.yaml"
Denisd2635bf2021-06-15 13:35:36 +0300838 if [ -z "${TUNNEL_INTERFACE}" ] ; then
839 echo "Tunnel interface is empty"
840 else
841 pci_id=$(ethtool -i ${TUNNEL_INTERFACE} |grep bus-info | awk '{print $2}')
842 mac=$(cat $(find /sys/bus/pci/devices/${pci_id}/ -name net)/${TUNNEL_INTERFACE}/address)
843 ip=${TUNNEL_INTERFACE_IP}/${TUNNEL_INTERFACE_NETWORK_NETMASK}
Oleh Hryhorove7feab42020-11-11 18:00:29 +0200844
Denisd2635bf2021-06-15 13:35:36 +0300845 cat << EOF > ${if_metadata_file}
Vasyl Saienko592730c2020-11-17 11:36:46 +0200846${TUNNEL_INTERFACE}:
Vasyl Saienko1152e982020-11-16 16:23:32 +0200847 pci_id: "${pci_id}"
848 ip_addr: "${ip}"
849 mac: "${mac}"
Oleh Hryhorove7feab42020-11-11 18:00:29 +0200850EOF
851
Denisd2635bf2021-06-15 13:35:36 +0300852 fi
Oleh Hryhorove7feab42020-11-11 18:00:29 +0200853 HW_METADATA=$(echo ${HW_METADATA} | jq -cr ". += {\"interfaces\": {\"$(hostname)\": \"$(base64 -w 0 ${if_metadata_file})\"}}")
854
855}
856
Oleh Hryhorovab3793f2020-06-04 18:37:22 +0300857function nested_virt_config {
858
Vasyl Saienkoe61a98c2021-10-19 21:42:19 +0300859if grep --color vmx /proc/cpuinfo; then
860 cat << EOF > /etc/modprobe.d/qemu-system-x86.conf
Oleh Hryhorovab3793f2020-06-04 18:37:22 +0300861options kvm_intel nested=1
862options kvm_intel enable_apicv=1
863options kvm_intel ept=1
864EOF
865
866 modprobe -r kvm_intel || true
867 modprobe kvm_intel nested=1
Vasyl Saienkoe61a98c2021-10-19 21:42:19 +0300868else
869 echo "Nested virtualization is not supported"
870fi
Oleh Hryhorovab3793f2020-06-04 18:37:22 +0300871
872}
Vasyl Saienkof9ee1582020-03-02 16:53:41 +0200873
Mykyta Karpin6a153c52020-11-03 15:21:38 +0200874function configure_huge_pages {
875 local nr_2Mb
876 local nr_1G
877 local apply_grub=false
878
879 if [[ ! $(cat /etc/default/grub | grep -E ^GRUB_CMDLINE_LINUX=".*hugepagesz=2M.*") ]] && [[ "${HUGE_PAGES_2Mb_SIZE}" -gt 0 ]]; then
880 nr_2Mb=$(( "${HUGE_PAGES_2Mb_SIZE}" / 2 ))
881 echo "Configure GRUB kernel cmd to allocate ${nr_2Mb} 2Mb huge pages"
882 echo "GRUB_CMDLINE_LINUX=\"\$GRUB_CMDLINE_LINUX hugepagesz=2M hugepages=$nr_2Mb\"" >> /etc/default/grub
883 apply_grub=true
884 fi
885
886 if [[ ! $(cat /etc/default/grub | grep -E ^GRUB_CMDLINE_LINUX=".*hugepagesz=1G.*") ]] && [[ "${HUGE_PAGES_1G_SIZE}" -gt 0 ]]; then
887 echo "Configure GRUB kernel cmd to allocate ${HUGE_PAGES_1G_SIZE} 1G huge pages"
888 echo "GRUB_CMDLINE_LINUX=\"\$GRUB_CMDLINE_LINUX hugepagesz=1G hugepages=${HUGE_PAGES_1G_SIZE}\"" >> /etc/default/grub
889 apply_grub=true
890 fi
891
892 if [[ "${apply_grub}" == true ]]; then
893 update-grub
894 cloud-init clean -r
895 else
896 echo "GRUB already configured with huge pages"
897 fi
898}
899
Oleh Hryhorov43bbc8c2020-12-03 15:30:09 +0200900function disable_master_taint {
901
Oleh Hryhorovcd1a3bd2020-12-04 13:21:06 +0200902 function disable_master_taint_retry {
903 # Enable "Allow all authenticated users, including service accounts to schedule on all nodes, including UCP managers."
904 curl --retry 6 --retry-delay 5 -k -H "Authorization: Bearer $AUTHTOKEN" -k -X PUT https://${UCP_MASTER_HOST}/collectionGrants/authenticated/swarm/scheduler
905 }
906
907 retry 10 "Can't enable permission to schedule all pods on UCP master." disable_master_taint_retry
Oleh Hryhorov43bbc8c2020-12-03 15:30:09 +0200908
909}
910
Vasyl Saienko94843cb2021-02-26 16:43:34 +0200911function disable_ipv6 {
912 cat << EOF > /etc/sysctl.d/11-ipv6-disable.conf
913net.ipv6.conf.all.disable_ipv6 = 1
914net.ipv6.conf.default.disable_ipv6 = 1
915net.ipv6.conf.lo.disable_ipv6 = 1
916EOF
917 sysctl -p /etc/sysctl.d/11-ipv6-disable.conf
918}
919
Vasyl Saienko846c6342021-02-24 17:01:36 +0200920function setup_evpn {
921
922cat << EOF > /etc/netplan/60-evpn.yaml
923network:
924 version: 2
925 ethernets:
Vasyl Saienko5f953782021-02-25 10:30:32 +0200926EOF
927for vni in $(seq ${FRR_EVPN_TUNNELS_RANGE}); do
928cat << EOF >> /etc/netplan/60-evpn.yaml
929 vxlan$vni: {}
930EOF
931done
932cat << EOF >> /etc/netplan/60-evpn.yaml
Vasyl Saienko846c6342021-02-24 17:01:36 +0200933 bridges:
Vasyl Saienko5f953782021-02-25 10:30:32 +0200934EOF
935for vni in $(seq ${FRR_EVPN_TUNNELS_RANGE}); do
936cat << EOF >> /etc/netplan/60-evpn.yaml
937 br$vni:
Vasyl Saienko846c6342021-02-24 17:01:36 +0200938 dhcp4: false
Vasyl Saienkof18b4782021-03-03 14:15:40 +0200939 link-local: []
Vasyl Saienko846c6342021-02-24 17:01:36 +0200940 parameters:
941 stp: false
Vasyl Saienko5f953782021-02-25 10:30:32 +0200942 interfaces:
943 - vxlan$vni
Vasyl Saienko846c6342021-02-24 17:01:36 +0200944 addresses:
Vasyl Saienko5f953782021-02-25 10:30:32 +0200945 - 192.168.$vni.254/24
Vasyl Saienko846c6342021-02-24 17:01:36 +0200946EOF
Vasyl Saienko5f953782021-02-25 10:30:32 +0200947done
Vasyl Saienko846c6342021-02-24 17:01:36 +0200948
Vasyl Saienko5f953782021-02-25 10:30:32 +0200949for vni in $(seq ${FRR_EVPN_TUNNELS_RANGE}); do
950cat << EOF > /etc/systemd/system/evpn-vxlan-tunnel-${vni}.service
Vasyl Saienko846c6342021-02-24 17:01:36 +0200951[Unit]
Vasyl Saienko5f953782021-02-25 10:30:32 +0200952Description=EVPN tunnel ${vni}
Vasyl Saienko846c6342021-02-24 17:01:36 +0200953After=network.target
954
955[Service]
956Type=oneshot
957RemainAfterExit=true
Vasyl Saienkob3b8c4e2021-02-26 10:54:27 +0200958ExecStart=/sbin/ip link add vxlan${vni} type vxlan id ${vni} local ${TUNNEL_INTERFACE_IP} nolearning dstport ${FRR_EVPN_VXLAN_DST_PORT}
Vasyl Saienko5f953782021-02-25 10:30:32 +0200959ExecStart=/sbin/ip link set vxlan${vni} up
Vasyl Saienko846c6342021-02-24 17:01:36 +0200960
961[Install]
962WantedBy=multi-user.target
963EOF
Vasyl Saienko5f953782021-02-25 10:30:32 +0200964 systemctl enable evpn-vxlan-tunnel-${vni}.service
965 systemctl start evpn-vxlan-tunnel-${vni}.service
966done
Vasyl Saienko846c6342021-02-24 17:01:36 +0200967
968 netplan --debug apply
969 # NOTE(vsaienko): give some time to apply changes
970 sleep 15
Vasyl Saienko846c6342021-02-24 17:01:36 +0200971}
972
Vasyl Saienko25bb75b2021-02-15 12:22:33 +0200973function install_frr {
974 function _install_frr_packages {
Vasyl Saienko25bb75b2021-02-15 12:22:33 +0200975 FRRVER="frr-stable"
Oleh Hryhorov6243c9f2023-09-21 09:53:09 +0000976 curl -s https://mirror.mirantis.com/.snapshots/frr-stable-latest/keys.asc | sudo apt-key add -
Oleh Hryhorov2ae1db82023-09-21 07:39:50 +0000977 add-apt-repository "deb [arch=amd64] https://mirror.mirantis.com/.snapshots/frr-stable-latest/ ${OS_CODENAME} ${FRRVER}"
Vasyl Saienko300bb0b2023-04-19 06:52:53 +0000978 install_pkgs frr frr-pythontools
Vasyl Saienko25bb75b2021-02-15 12:22:33 +0200979 }
980 retry 3 "Can't install frr packages" _install_frr_packages
981
982 sed -i 's/^bgpd=no/bgpd=yes/g' /etc/frr/daemons
983
984 cat << EOF > /etc/frr/frr.conf
985log syslog informational
986no ipv6 forwarding
987service integrated-vtysh-config
988!
989router bgp ${FRR_BGP_AS_NUMBER}
990 bgp router-id ${FRR_ROUTER_ID}
991 no bgp default ipv4-unicast
992 bgp cluster-id ${FRR_ROUTER_ID}
993 coalesce-time 1000
Vasyl Saienko846c6342021-02-24 17:01:36 +0200994 neighbor rr peer-group
Vasyl Saienkob3b8c4e2021-02-26 10:54:27 +0200995 neighbor rr capability extended-nexthop
996 neighbor rr remote-as ${FRR_BGP_AS_NUMBER}
Vasyl Saienko846c6342021-02-24 17:01:36 +0200997 neighbor rr update-source ${FRR_BGP_SOURCE_ADDRESS}
998EOF
999
1000for neighbor_ip in $(echo $FRR_BGP_NEIGHBORS | tr "," "\n"); do
1001 cat << EOF >> /etc/frr/frr.conf
1002 neighbor $neighbor_ip peer-group rr
1003EOF
1004
1005done
1006 cat << EOF >> /etc/frr/frr.conf
Vasyl Saienko25bb75b2021-02-15 12:22:33 +02001007 !
1008 address-family ipv4 unicast
Vasyl Saienko846c6342021-02-24 17:01:36 +02001009 neighbor rr activate
Vasyl Saienko25bb75b2021-02-15 12:22:33 +02001010 exit-address-family
1011 !
1012 address-family ipv4 vpn
Vasyl Saienko846c6342021-02-24 17:01:36 +02001013 neighbor rr activate
Vasyl Saienko25bb75b2021-02-15 12:22:33 +02001014 exit-address-family
1015 !
1016 address-family l2vpn evpn
Vasyl Saienko846c6342021-02-24 17:01:36 +02001017 neighbor rr activate
Vasyl Saienko25bb75b2021-02-15 12:22:33 +02001018 advertise-all-vni
1019 advertise-default-gw
1020 exit-address-family
1021!
1022line vty
1023!
1024EOF
1025
1026 systemctl restart frr
Vasyl Saienko846c6342021-02-24 17:01:36 +02001027
Vasyl Saienko94843cb2021-02-26 16:43:34 +02001028 disable_ipv6
Vasyl Saienko846c6342021-02-24 17:01:36 +02001029 setup_evpn
Vasyl Saienko25bb75b2021-02-15 12:22:33 +02001030}
1031
Mykyta Karpinbac78b42021-12-07 11:25:03 +02001032function cron_disable_calico_offloading {
Denis Biletskiy49b91ea2022-06-09 11:41:39 +02001033 # The workaround is to disable offloading on vxlan interface
1034 if [ -f /etc/cron.d/disable_calico_offloading ]; then
1035 echo "Cronjob for disable callico offloading already exists"
1036 else
1037 script_path="/usr/sbin/calico_disable_offloading.sh"
1038 cat << 'EOF' >> $script_path
1039#!/usr/bin/env bash
1040if /sbin/ethtool --show-offload vxlan.calico | grep -E '^.x-checksumming:' | grep -q ': on'; then
1041 /sbin/ethtool --offload vxlan.calico rx off tx off > /dev/null
1042fi
Mykyta Karpinbac78b42021-12-07 11:25:03 +02001043EOF
Denis Biletskiy49b91ea2022-06-09 11:41:39 +02001044 chmod +x $script_path
1045 cat << EOF >> /etc/cron.d/disable_calico_offloading
1046* * * * * root $script_path 2>&1 | /usr/bin/logger -t calico_disable_offloading
1047EOF
1048 fi
Mykyta Karpinbac78b42021-12-07 11:25:03 +02001049}
1050
Vasyl Saienko114333f2022-05-24 09:34:51 +00001051function increase_iscsi_timeout {
1052 cat <<EOF > /etc/udev/rules.d/50-iscsi-timeout.rules
1053ACTION=="add", SUBSYSTEM=="scsi" , SYSFS{type}=="0|7|14",
1054RUN+="/bin/sh -c 'echo 120 > /sys/block/%k/device/timeout'"
1055EOF
1056 udevadm control --reload-rules && udevadm trigger
1057}