blob: f09e6eaa703b5e7a020623ec5f9cb8365a6c3a9c [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"}
Vasyl Saienko7c595922021-03-17 13:58:13 +020029
30### COMMON FUNCTIONS ###
31function get_interface_prefix {
32 local interface=$1
33 local interface_ip
34 local interface_mask
35
36 interface_ip=$(ip addr show dev ${interface} | grep -Po 'inet \K[\d.]+' | egrep -v "127.0.|172.17")
37 interface_mask=$(ip addr show dev ${interface} | grep -Po 'inet \K[\d.]+\/[\d]+' | egrep -v "127.0.|172.17" | cut -d'/' -f2)
38 echo "${interface_ip}/${interface_mask}"
39}
40### END COMMON FUNCTIONS ###
41
Oleh Hryhorovf4677af2021-04-26 18:00:15 +030042DEFAULT_INTERFACE=${DEFAULT_INTERFACE:-$(ip route show |awk '/default/ {print $5}')}
Vasyl Saienko7c595922021-03-17 13:58:13 +020043
Vasyl Saienko390c53a2021-03-18 12:45:11 +020044if [[ -n ${CONTROL_NETWORK_CIDR} ]]; then
Vasyl Saienkod3964202023-09-24 15:09:48 +000045 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 +020046else
47 CONTROL_IP_ADDRESS=$(get_interface_prefix ${DEFAULT_INTERFACE} | awk -F '/' '{print $1}')
Vasyl Saienkoe9677062021-03-19 13:31:32 +020048 CONTROL_NETWORK_CIDR=$(ip route show dev ${DEFAULT_INTERFACE} | awk '/kernel/ {print $1}')
Vasyl Saienko7c595922021-03-17 13:58:13 +020049fi
50
51
Oleh Hryhorovb58460e2020-02-05 15:53:48 +020052PUBLIC_INTERFACE=${PUBLIC_INTERFACE:-ens4}
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +010053UCP_USERNAME=${UCP_USERNAME:-admin}
54UCP_PASSWORD=${UCP_PASSWORD:-administrator}
55OS_CODENAME=$(lsb_release -c -s)
Vasyl Saienko3ab29c92020-01-03 10:39:03 +020056NODE_DEPLOYMENT_RETRIES=${NODE_DEPLOYMENT_RETRIES:-15}
Vasyl Saienko7d7acea2020-01-03 16:59:05 +020057FLOATING_NETWORK_PREFIXES=${FLOATING_NETWORK_PREFIXES:-10.11.12.0/24}
Oleh Hryhorov404e9572020-01-21 18:57:22 +020058PUBLIC_INTERFACE=${PUBLIC_INTERFACE:-ens4}
Vasyl Saienko0fa6f192020-03-06 16:08:51 +020059UCP_MASTER_HOST=${UCP_MASTER_HOST:-${CONTROL_IP_ADDRESS}}
60UCP_IP_ADDRESS=${UCP_IP_ADDRESS:-$CONTROL_IP_ADDRESS}
Vasyl Saienkocfbedc72021-02-15 18:02:56 +020061# Change default VXLAN port, need for https://mirantis.jira.com/browse/PRODX-11679
62UCP_DOCKER_SWARM_DATA_PORT=${UCP_DOCKER_SWARM_DATA_PORT:-4789}
Vasyl Saienko06c7afe2022-07-01 17:53:56 +000063UCP_DOCKER_CALICO_VXLAN_PORT=${UCP_DOCKER_CALICO_VXLAN_PORT:-${UCP_DOCKER_SWARM_DATA_PORT}}
Vasyl Saienko08166342020-02-28 12:20:51 +020064NTP_SERVERS=${NTP_SERVERS:-"ldap.scc.mirantis.net ldap.bud.mirantis.net"}
Michael Polenchuk486cadf2020-08-24 16:25:51 +040065IRONIC_BAREMETAL_VXLAN_INTERFACE='vxlan10'
Mykyta Karpin6a153c52020-11-03 15:21:38 +020066HUGE_PAGES_1G_SIZE=$(echo "${HUGE_PAGES}" | awk -F ',' '{print $1}')
67HUGE_PAGES_2Mb_SIZE=$(echo "${HUGE_PAGES}" | awk -F ',' '{print $2}')
68CONFIGURE_HUGE_PAGES=false
Vasyl Saienko3dd0c272020-11-16 17:20:07 +020069TUNNEL_INTERFACE_NETPLAN_MANAGE=true
Anton Samoylov1d140ee2020-11-23 22:40:13 +040070TUNGSTENFABRIC_ENABLED=${TUNGSTENFABRIC_ENABLED:-false}
Mykyta Karpin6a153c52020-11-03 15:21:38 +020071if [[ "${HUGE_PAGES_1G_SIZE}" -gt 0 ]] || [[ "${HUGE_PAGES_2Mb_SIZE}" -gt 0 ]]; then
72 CONFIGURE_HUGE_PAGES=true
Anton Samoylov1d140ee2020-11-23 22:40:13 +040073 if [[ "${TUNGSTENFABRIC_ENABLED,,}" == false ]]; then
74 TUNNEL_INTERFACE_NETPLAN_MANAGE=false
75 fi
Mykyta Karpin6a153c52020-11-03 15:21:38 +020076fi
Oleh Hryhorov43bbc8c2020-12-03 15:30:09 +020077SINGLE_NODE=${SINGLE_NODE:-true}
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +010078
Vasyl Saienko25bb75b2021-02-15 12:22:33 +020079#FRR Options
80FRR_BGP_AS_NUMBER=${FRR_BGP_AS_NUMBER:-64512}
81FRR_ROUTER_ID=${TUNNEL_INTERFACE_IP}
Vasyl Saienko846c6342021-02-24 17:01:36 +020082FRR_BGP_SOURCE_ADDRESS=${TUNNEL_INTERFACE_IP}
83FRR_BGP_NEIGHBORS=${FRR_BGP_NEIGHBORS:-}
Vasyl Saienko5f953782021-02-25 10:30:32 +020084FRR_EVPN_TUNNELS_RANGE=${FRR_EVPN_TUNNELS_RANGE:-'20 50'}
Vasyl Saienkob3b8c4e2021-02-26 10:54:27 +020085FRR_EVPN_VXLAN_DST_PORT=${FRR_EVPN_VXLAN_DST_PORT:-4790}
Vasyl Saienko25bb75b2021-02-15 12:22:33 +020086
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +020087LVM_LOOP_DEVICE_SIZE=${LVM_LOOP_DEVICE_SIZE:-0}
Vasyl Saienkocd302062021-04-21 22:25:18 +030088CINDER_LVM_LOOP_DEVICE_SIZE=${CINDER_LVM_LOOP_DEVICE_SIZE:-0}
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +020089
Vasyl Saienko8d34ad02021-07-26 11:55:23 +030090_KUBECTL_CMD="kubectl --kubeconfig /etc/kubernetes/admin.conf "
91
Vasyl Saienkoa51d1ca2020-01-02 15:59:16 +020092function retry {
93 local retries=$1
94 shift
95 local msg="$1"
96 shift
97
98 local count=0
99 until "$@"; do
100 exit=$?
101 wait=$((2 ** $count))
102 count=$(($count + 1))
103 if [ $count -lt $retries ]; then
104 echo "Retry $count/$retries exited $exit, retrying in $wait seconds..."
105 sleep $wait
106 else
107 echo "Retry $count/$retries exited $exit, no more retries left."
108 echo "$msg"
109 return $exit
110 fi
111 done
112 return 0
113}
114
Vasyl Saienko300bb0b2023-04-19 06:52:53 +0000115function install_pkgs {
116 local pkgs="$@"
117 function _retry_install {
Vasyl Saienko92133c02024-05-16 10:18:51 +0300118 apt update && DEBIAN_FRONTEND=noninteractive apt install -o DPkg::Options::=--force-confdef -y $pkgs
Vasyl Saienko300bb0b2023-04-19 06:52:53 +0000119 }
120 retry 10 "Labeling node failed" _retry_install
121}
122
Vasyl Saienko806d5dc2022-02-01 14:18:09 +0200123function setup_bind_mounts {
124 mkdir -p /var/lib/persistant-data
Vasyl Saienkoe3107252023-06-06 07:02:58 +0000125 for component in nova glance cinder docker; do
Vasyl Saienko3cc01ca2023-09-25 12:23:43 +0000126 if [[ -d /var/lib/${component} ]]; then
127 continue
128 fi
Vasyl Saienko806d5dc2022-02-01 14:18:09 +0200129 mkdir -p /var/lib/data/${component} /var/lib/${component}
Vasyl Saienko3cc01ca2023-09-25 12:23:43 +0000130 echo "/var/lib/data/${component} /var/lib/${component} none bind 0 0" >> /etc/fstab
Vasyl Saienko806d5dc2022-02-01 14:18:09 +0200131 done
Vasyl Saienkoe3107252023-06-06 07:02:58 +0000132
133 if [[ "${KUBERNETES_INSTALLER}" == "k0s" ]]; then
134 mkdir -p /var/lib/k0s/kubelet
135 mkdir -p /var/lib/kubelet
136 echo "/var/lib/k0s/kubelet /var/lib/kubelet none bind 0 0" >> /etc/fstab
137 fi
138
Vasyl Saienko806d5dc2022-02-01 14:18:09 +0200139 mount -a
140}
141
Vasyl Saienko392dc712020-04-07 15:56:10 +0300142function configure_atop {
143 sed -i 's/INTERVAL=600/INTERVAL=60/' /usr/share/atop/atop.daily
144 systemctl restart atop
145}
146
dbiletskiy70af8192024-06-19 09:27:10 +0200147function disable_unattended_upgr {
Vasyl Saienkoaadd2f82020-08-07 09:35:03 +0300148 # Disable unattended upgrades
149 sed -i 's/1/0/' /etc/apt/apt.conf.d/20auto-upgrades
Oleh Hryhorovbe7bdf72021-04-05 18:21:22 +0300150 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 +0200151}
152
Vasyl Saienkof9813512024-07-02 12:30:25 +0300153function configure_logind_conf {
154
155 cat <<EOF >/etc/systemd/logind.conf
156[Login]
157#NAutoVTs=6
158#ReserveVT=6
159#KillUserProcesses=no
160#KillOnlyUsers=
161#KillExcludeUsers=root
162InhibitDelayMaxSec=600
163#UserStopDelaySec=10
164#HandlePowerKey=poweroff
165#HandleSuspendKey=suspend
166#HandleHibernateKey=hibernate
167#HandleLidSwitch=suspend
168#HandleLidSwitchExternalPower=suspend
169#HandleLidSwitchDocked=ignore
170#HandleRebootKey=reboot
171#PowerKeyIgnoreInhibited=no
172#SuspendKeyIgnoreInhibited=no
173#HibernateKeyIgnoreInhibited=no
174#LidSwitchIgnoreInhibited=yes
175#RebootKeyIgnoreInhibited=no
176#HoldoffTimeoutSec=30s
177#IdleAction=ignore
178#IdleActionSec=30min
179#RuntimeDirectorySize=10%
180#RuntimeDirectoryInodesMax=400k
181#RemoveIPC=yes
182#InhibitorsMax=8192
183#SessionsMax=8192
184
185EOF
186
187 systemctl daemon-reload
188
189}
190
dbiletskiy70af8192024-06-19 09:27:10 +0200191function install_required_packages {
Mykyta Karpind6cde5e2022-07-05 12:06:06 +0300192 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 +0300193
Vasyl Saienkoa7cfc322020-11-06 16:52:22 +0200194 if [[ "${CONFIGURE_HUGE_PAGES}" == true ]]; then
195 pkg_list="$pkg_list dpdk-igb-uio-dkms"
Anton Samoylovec019d52020-11-19 18:55:47 +0400196 # linux-image-extra-$(uname -r) provides uio_pci_generic module, used by TF DPDK as default driver
197 if [[ "${TUNGSTENFABRIC_ENABLED,,}" == true ]]; then
198 pkg_list="$pkg_list linux-modules-extra-$(uname -r)"
199 fi
Vasyl Saienkoa7cfc322020-11-06 16:52:22 +0200200 fi
Vasyl Saienko300bb0b2023-04-19 06:52:53 +0000201 install_pkgs $pkg_list
202 # Since version 4 yq uses another syntax
Oleksandr Kononenko231933b2024-02-05 16:46:35 +0200203 if [[ ! -f /usr/bin/yq ]]; then
204 curl --retry 6 --retry-delay 5 -L ${BINARY_BASE_URL}/openstack/bin/utils/yq/yq-v3.3.2 -o /usr/bin/yq
205 chmod +x /usr/bin/yq
206 fi
Mykyta Karpin5ed5fd82020-01-27 18:23:29 +0200207}
208
Oleksii Grudev4399e232021-02-24 19:08:25 +0200209function enable_iscsi {
210 systemctl enable iscsid
211 systemctl restart iscsid
212}
Mykyta Karpin5ed5fd82020-01-27 18:23:29 +0200213
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +0200214function configure_lvm {
Vasyl Saienkocd302062021-04-21 22:25:18 +0300215 function _setup_lvm {
216 local device_num=$1
217 local device_size=$2
218 local vg_name=$3
Denis Biletskiy3b21a822022-05-10 10:29:29 +0300219 if vgdisplay | grep ${vg_name}; then
220 echo "Volume group ${vg_name} is already initialize"
Deniscaa72dd2021-06-04 17:13:05 +0300221 else
222 #configure lvm only on compute nodes
223 if [[ ${NODE_METADATA} == *"openstack-compute-node"* ]]; then
224 truncate --size ${device_size}G /srv/disk${device_num}
225 cat <<EOF > /etc/systemd/system/setup-loopback-loop${device_num}.service
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +0200226[Unit]
Vasyl Saienkocd302062021-04-21 22:25:18 +0300227Description=Setup loop${device_num} device
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +0200228DefaultDependencies=no
229After=systemd-udev-settle.service
230Before=lvm2-activation-early.service
231Wants=systemd-udev-settle.service
232
233[Service]
Vasyl Saienkocd302062021-04-21 22:25:18 +0300234ExecStart=-/sbin/losetup /dev/loop${device_num} /srv/disk${device_num}
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +0200235RemainAfterExit=true
236Type=oneshot
237
238[Install]
239WantedBy=local-fs.target
240EOF
Deniscaa72dd2021-06-04 17:13:05 +0300241 systemctl enable setup-loopback-loop${device_num}
242 systemctl start setup-loopback-loop${device_num}
243 #adding short sleep to give time for service to start
244 sleep 3
245 pvcreate /dev/loop${device_num}
246 vgcreate ${vg_name} /dev/loop${device_num}
247 fi
Vasyl Saienkocd302062021-04-21 22:25:18 +0300248 fi
249 }
250
251 if [[ "${LVM_LOOP_DEVICE_SIZE}" -gt 0 ]]; then
Denis Biletskiy3b21a822022-05-10 10:29:29 +0300252 _setup_lvm 10 ${LVM_LOOP_DEVICE_SIZE} nova-vol
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +0200253 fi
Vasyl Saienkocd302062021-04-21 22:25:18 +0300254 if [[ "${CINDER_LVM_LOOP_DEVICE_SIZE}" -gt 0 ]]; then
Denis Biletskiy3b21a822022-05-10 10:29:29 +0300255 _setup_lvm 11 ${CINDER_LVM_LOOP_DEVICE_SIZE} cinder-vol
Vasyl Saienkocd302062021-04-21 22:25:18 +0300256 fi
257
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +0200258}
259
Mykyta Karpin5ed5fd82020-01-27 18:23:29 +0200260function install_docker {
261 function install_retry {
Vasyl Saienkofa714102024-03-22 09:30:25 +0200262 curl --retry 6 --retry-delay 5 -fsSL "${DOCKER_EE_URL}/gpg" | sudo apt-key add -
263 add-apt-repository "deb [arch=amd64] ${DOCKER_EE_URL}/ ${OS_CODENAME} ${DOCKER_EE_RELEASE}"
264 install_pkgs ${DOCKER_EE_PACKAGES}
Vasyl Saienko92133c02024-05-16 10:18:51 +0300265 if [[ $KUBERNETES_CONTAINER_RUNTIME == "crio" ]]; then
266 NERDCTL_VERSION=1.7.6
267 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
268 tar zxvf nerdctl-${NERDCTL_VERSION}-linux-amd64.tar.gz -C /usr/local/bin
269 fi
Mykyta Karpin5ed5fd82020-01-27 18:23:29 +0200270 }
271 retry 10 "Failed to install docker" install_retry
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100272}
273
Mykyta Karpin7abaea02020-07-27 15:43:07 +0300274function prepare_docker_config {
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100275 mkdir -p /etc/docker
276 cat <<EOF > /etc/docker/daemon.json
277{
Vasyl Saienkoa37de442024-05-02 17:15:47 +0300278 "default-cgroupns-mode": "host",
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100279 "default-address-pools": [
280 { "base": "${DOCKER_DEFAULT_ADDRESS_POOL}", "size": ${DOCKER_DEFAULT_ADDRESS_SIZE} }
Mykyta Karpin7abaea02020-07-27 15:43:07 +0300281 ],
282 "log-driver": "json-file",
283 "log-opts": {
284 "max-size": "1g",
285 "max-file": "3"
286 }
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100287}
288EOF
289
Vasyl Saienko92133c02024-05-16 10:18:51 +0300290 if [[ $KUBERNETES_CONTAINER_RUNTIME == "crio" ]]; then
291 mkdir -p /etc/containerd/
292cat <<EOF> /etc/containerd/config.toml
Vasyl Saienko05edc402024-06-11 11:17:21 +0300293version = 2
294[plugins]
295[plugins."io.containerd.grpc.v1.cri".containerd.runtimes]
296 [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
297 runtime_type = "io.containerd.runc.v2"
298 [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
299 SystemdCgroup = true
Vasyl Saienko92133c02024-05-16 10:18:51 +0300300EOF
Vasyl Saienko05edc402024-06-11 11:17:21 +0300301 systemctl restart containerd || /bin/true
Vasyl Saienko92133c02024-05-16 10:18:51 +0300302 fi
303
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100304}
305
Vasyl Saienkoe32453e2020-09-16 09:30:34 +0300306function pull_image {
307 local image="$1"
308 function pull_image_retry {
Vasyl Saienko92133c02024-05-16 10:18:51 +0300309 if [[ $KUBERNETES_CONTAINER_RUNTIME == "crio" ]]; then
310 nerdctl --address unix:///run/containerd/containerd.sock -n k8s.io pull $image
311 fi
Vasyl Saienkoe32453e2020-09-16 09:30:34 +0300312 docker pull ${image}
313 }
314 retry 10 "Can't pool docker image $image" pull_image_retry
315}
316
Vasyl Saienko84ee4fd2020-09-02 10:00:18 +0300317function cache_images {
318
Vasyl Saienkoe32453e2020-09-16 09:30:34 +0300319 # Cache UCP image with retries
320 pull_image "${DOCKER_UCP_IMAGE}"
Vasyl Saienko84ee4fd2020-09-02 10:00:18 +0300321
Vasyl Saienkoe32453e2020-09-16 09:30:34 +0300322 local image_prefix=${DOCKER_UCP_IMAGE%/*}
Oleksandr Kononenkobf4ecf82024-02-01 11:02:53 +0200323 for image in $(docker container run --rm --name ucp -v /var/run/docker.sock:/var/run/docker.sock ${DOCKER_UCP_IMAGE} images --list); do
324 pull_image "${image_prefix}/${image##*/}"
Vasyl Saienko92133c02024-05-16 10:18:51 +0300325 if [[ $KUBERNETES_CONTAINER_RUNTIME == "crio" ]]; then
326 nerdctl --address unix:///run/containerd/containerd.sock -n k8s.io tag ${image_prefix}/${image##*/} mirantis/${image##*/}
327 fi
Oleksandr Kononenkobf4ecf82024-02-01 11:02:53 +0200328 docker tag ${image_prefix}/${image##*/} mirantis/${image##*/}
329 done
Vasyl Saienko84ee4fd2020-09-02 10:00:18 +0300330}
331
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100332function install_ucp {
Deniscaa72dd2021-06-04 17:13:05 +0300333 if docker ps --all | grep ucp-controller; then
334 echo "Docker UCP container is running"
335 else
336 local tmpd
337 tmpd=$(mktemp -d)
338 function docker_run_retry {
339 docker container run --rm --name ucp \
340 -v /var/run/docker.sock:/var/run/docker.sock \
341 ${DOCKER_UCP_IMAGE} install \
342 --host-address $UCP_IP_ADDRESS \
343 --admin-username $UCP_USERNAME \
344 --admin-password $UCP_PASSWORD \
345 --existing-config
346 }
347 retry 10 "Can't bring up docker UCP container" docker_run_retry
348 fi
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100349}
350
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300351function download_k0s {
352 curl -sSLf https://get.k0s.sh | sudo sh
353}
354
355function install_k0s {
Vasyl Saienkoe3107252023-06-06 07:02:58 +0000356 mkdir -p /etc/k0s
357 k0s config create > /etc/k0s/k0s.yaml
358 k0s install controller -c /etc/k0s/k0s.yaml
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300359 k0s start
360
Vasyl Saienkoe3107252023-06-06 07:02:58 +0000361 sleep 60
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300362
Vasyl Saienkoe2c68dd2022-01-27 17:10:12 +0200363 mkdir -p /etc/kubernetes /root/.kube/
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300364 k0s kubeconfig admin > /etc/kubernetes/admin.conf
365 cp /etc/kubernetes/admin.conf /root/.kube/config
366
367 mkdir -p /etc/k0s
368 k0s kubeconfig admin > /etc/k0s/admin.conf
369 k0s token create --role=worker > /etc/k0s/worker_token.yaml
370 k0s token create --role=controller > /etc/k0s/controller_token.yaml
371
Vasyl Saienko300bb0b2023-04-19 06:52:53 +0000372 install_pkgs nginx
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300373 rm -f /etc/nginx/sites-enabled/default
374 ln -s /etc/k0s/ /var/www/k0s
375 cat << EOF > /etc/nginx/sites-enabled/k0s
376server {
377 listen *:80;
378 root /var/www;
379
380 location /k0s {
381 autoindex on;
382 }
383}
384EOF
385 systemctl restart nginx
386}
387
Oleh Hryhorovcd1a3bd2020-12-04 13:21:06 +0200388function get_authtoken_retry {
389# Download the bundle https://docs.docker.com/ee/ucp/user-access/cli/
390# Create an environment variable with the user security token
391 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)
392 if [ -z ${AUTHTOKEN} ]; then
393 return -1
394 fi
395}
396
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100397function download_bundles {
398 local tmpd
399 tmpd=$(mktemp -d)
Oleh Hryhorova0754872020-01-24 16:45:05 +0200400
401 function download_bundles_retry {
402 # Download the client certificate bundle
Vasyl Saienkof3dba8c2020-02-14 10:07:23 +0200403 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 +0200404 }
Oleh Hryhorovb58460e2020-02-05 15:53:48 +0200405
Vasyl Saienko9b688992020-02-13 13:50:53 +0200406 retry 10 "Can't get AUTHTOKEN from master." get_authtoken_retry
407 retry 10 "Can't download bundle file from master." download_bundles_retry
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100408
409 pushd $tmpd
410 # Unzip the bundle.
411 unzip bundle.zip
412
413 # Run the utility script.
414 eval "$(<env.sh)"
Vasyl Saienkof9f668c2020-01-04 11:36:35 +0200415 mkdir -p /etc/kubernetes /root/.kube/
Vasyl Saienko5b379422020-01-04 12:24:08 +0200416 cp kube.yml /etc/kubernetes/admin.conf
417 cp kube.yml /root/.kube/config
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100418 popd
419}
420
Vasyl Saienko3ab29c92020-01-03 10:39:03 +0200421function wait_for_node {
422 function retry_wait {
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300423 ${_KUBECTL_CMD} get nodes |grep -w Ready |awk '{print $1}' |grep -q $(hostname)
Vasyl Saienko3ab29c92020-01-03 10:39:03 +0200424 }
425 retry $NODE_DEPLOYMENT_RETRIES "The node didn't come up." retry_wait
426}
427
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300428function download_k8s_metadata {
429 mkdir -p /etc/k0s/
430 mkdir -p /etc/kubernetes
431 for f in worker_token.yaml controller_token.yaml admin.conf; do
432 curl --retry 6 --retry-delay 5 -L http://${UCP_MASTER_HOST}/k0s/${f} -o /etc/k0s/${f}
433 done
434 cp /etc/k0s/admin.conf /etc/kubernetes/admin.conf
435 mkdir -p /root/.kube/
436 cp /etc/k0s/admin.conf /root/.kube/config
437}
438
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100439function join_node {
Deniscaa72dd2021-06-04 17:13:05 +0300440 if kubectl --kubeconfig /etc/kubernetes/admin.conf get nodes |grep -w Ready |awk '{print $1}' |grep -q $(hostname); then
441 echo "This node is already join"
442 else
443 local type=${1}
444 function retry_join_node {
445 env -i $(docker swarm join-token $type |grep 'docker swarm join' | xargs)
Mykyta Karpine2d2fda2023-11-23 10:41:51 +0200446 sleep 20 && systemctl restart docker && sleep 20
Deniscaa72dd2021-06-04 17:13:05 +0300447 }
448 retry 10 "Failed to join node to swarm" retry_join_node
449 fi
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100450}
451
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300452function join_k0s_node {
453 k0s install worker --token-file /etc/k0s/worker_token.yaml
454 k0s start
455}
456
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100457function create_ucp_config {
Oleh Hryhorovcd1a3bd2020-12-04 13:21:06 +0200458 if [[ "${SINGLE_NODE}" == true ]]; then
459 max_pods="kubelet_max_pods = 220"
460 fi
Michael Polenchuk06bfa152022-02-15 17:50:04 +0400461 if [[ "${SECURE_OVERLAY_ENABLED,,}" == true ]]; then
462 secure_overlay="secure_overlay = true
463 calico_vxlan = false"
464 fi
Vasyl Saienko92133c02024-05-16 10:18:51 +0300465 kubelet_custom_flags=""
466 if [[ $KUBERNETES_CONTAINER_RUNTIME == "crio" ]]; then
Vasyl Saienko05edc402024-06-11 11:17:21 +0300467 kubelet_custom_flags='custom_kubelet_flags=["--cgroup-driver=systemd", "--container-runtime-endpoint=unix:///run/containerd/containerd.sock"]'
Vasyl Saienko92133c02024-05-16 10:18:51 +0300468 fi
Deniscaa72dd2021-06-04 17:13:05 +0300469 if docker config ls | grep com.docker.ucp.config ; then
470 echo "Config com.docker.ucp.config already exists"
471 else
472 echo "
Vasyl Saienko2fcc70b2020-01-02 10:09:08 +0200473[scheduling_configuration]
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100474 enable_admin_ucp_scheduling = true
Vasyl Saienko2fcc70b2020-01-02 10:09:08 +0200475 default_node_orchestrator = \"kubernetes\"
Vasyl Saienkoccd3a752020-01-02 10:21:54 +0200476[cluster_config]
477 dns = [\"172.18.208.44\"]
Vasyl Saienko06c7afe2022-07-01 17:53:56 +0000478 calico_vxlan_port = \"$UCP_DOCKER_CALICO_VXLAN_PORT\"
Oleh Hryhorovcd1a3bd2020-12-04 13:21:06 +0200479 ${max_pods}
Michael Polenchuk06bfa152022-02-15 17:50:04 +0400480 ${secure_overlay}
Vasyl Saienko92133c02024-05-16 10:18:51 +0300481 ${kubelet_custom_flags}
Vasyl Saienkoccd3a752020-01-02 10:21:54 +0200482" | docker config create com.docker.ucp.config -
Deniscaa72dd2021-06-04 17:13:05 +0300483 fi
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100484}
485
486function swarm_init {
Deniscaa72dd2021-06-04 17:13:05 +0300487 if docker node ls | grep $HOSTNAME; then
488 echo "This node is already part of a swarm"
489 else
490 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 +0000491 sleep 5 && systemctl restart docker && sleep 5
Deniscaa72dd2021-06-04 17:13:05 +0300492 fi
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100493}
494
495function rm_ucp_config {
496 docker config rm com.docker.ucp.config
497}
498
Vasyl Saienkob58c7a52020-01-02 09:14:40 +0200499function install_kubectl {
Michael Polenchuk92ca1b32020-11-13 15:58:38 +0400500 local kubectl=/usr/local/bin/kubectl
Vasyl Saienkoa94bf182023-04-01 10:42:17 +0000501 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 +0400502 chmod +x ${kubectl}
Ilya Menkov2d22bd02020-02-14 15:01:29 +0400503cat << EOF >> ~/.bashrc
504source /usr/share/bash-completion/bash_completion
505source <(kubectl completion bash)
506EOF
Vasyl Saienkob58c7a52020-01-02 09:14:40 +0200507}
508
Vasyl Saienko08166342020-02-28 12:20:51 +0200509function configure_ntp {
Vasyl Saienkoe2c68dd2022-01-27 17:10:12 +0200510 echo "" > /etc/ntp.conf
Vasyl Saienko08166342020-02-28 12:20:51 +0200511
Anton Samoylov6d2cf7b2020-09-01 11:35:11 +0400512 for server in $NTP_SERVERS; do
513cat << EOF >> /etc/ntp.conf
514server ${server} iburst
515EOF
516 done
517
518 cat << EOF >> /etc/ntp.conf
519# Set general access to this service
520restrict -4 default kod nomodify notrap nopeer noquery
521restrict -6 default kod nomodify notrap nopeer noquery
522
523# Allow access from localhost
524restrict 127.0.0.1
525restrict ::1
526
527# Do not listen on any interface address by default
528interface ignore wildcard
529interface listen lo
530interface listen ${DEFAULT_INTERFACE}
531
532# Location of drift file
533driftfile /var/lib/ntp/ntp.drift
534EOF
535
ibumarskov35370042022-04-19 14:27:02 +0400536 systemctl disable systemd-timesyncd
537 systemctl enable ntp
Anton Samoylov6d2cf7b2020-09-01 11:35:11 +0400538 systemctl restart ntp
Vasyl Saienko08166342020-02-28 12:20:51 +0200539}
540
Vasyl Saienko5af97c32022-01-25 09:45:45 +0200541function wait_for_external_network {
542 function _check_access {
543 curl --connect-timeout 10 ${DOCKER_UCP_IMAGE%%/*} || (sleep 1; /bin/false)
544 }
545 retry 180 "Failed to wait for external networks reachable." _check_access
546}
547
Vasyl Saienko2fcc70b2020-01-02 10:09:08 +0200548function prepare_network {
Dennis Dmitrievea894212020-03-12 21:36:43 +0200549 if [ -z "${CONTROL_IP_ADDRESS}" ]; then
Vasyl Saienko7c595922021-03-17 13:58:13 +0200550 wait_condition_send "FAILURE" "CONTROL_IP_ADDRESS is not found."
Dennis Dmitrievea894212020-03-12 21:36:43 +0200551 exit 1
552 fi
553
Vasyl Saienko2fcc70b2020-01-02 10:09:08 +0200554 systemctl restart systemd-resolved
Vasyl Saienkoc14c43d2020-01-08 13:06:18 +0200555 # Make sure local hostname is present in /etc/hosts
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300556 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 +0200557}
558
Vasyl Saienko7d7acea2020-01-03 16:59:05 +0200559function workaround_default_forward_policy {
Vasyl Saienkod67e35e2023-09-27 14:44:01 +0000560 local should_run=0
561 for net in $FLOATING_NETWORK_PREFIXES; do
562 if ! iptables -nvL DOCKER-USER |grep $net; then
563 should_run=1
564 break
565 fi
566 done
567 if [[ $should_run == 0 ]]; then
568 echo "Iptables is already configured"
Deniscaa72dd2021-06-04 17:13:05 +0300569 else
570 cat << EOF > /etc/iptables/rules.v4
Vasyl Saienkobd34d2a2020-03-10 16:25:17 +0200571*filter
572:DOCKER-USER - [0:0]
573EOF
Deniscaa72dd2021-06-04 17:13:05 +0300574 for net in $FLOATING_NETWORK_PREFIXES; do
Vasyl Saienkobd34d2a2020-03-10 16:25:17 +0200575cat << EOF >> /etc/iptables/rules.v4
576-A DOCKER-USER -d ${net} -j ACCEPT
577-A DOCKER-USER -s ${net} -j ACCEPT
Vasyl Saienkobd34d2a2020-03-10 16:25:17 +0200578EOF
Deniscaa72dd2021-06-04 17:13:05 +0300579 done
Vasyl Saienkobd34d2a2020-03-10 16:25:17 +0200580
581cat << EOF >> /etc/iptables/rules.v4
582-A DOCKER-USER -j RETURN
Vasyl Saienko1785c352020-03-11 11:39:40 +0200583COMMIT
Vasyl Saienkobd34d2a2020-03-10 16:25:17 +0200584EOF
Vasyl Saienko02df39a2020-05-20 16:39:35 +0300585
586#Allow access to Internet from VMs for virtualized environment.
587cat << EOF >> /etc/iptables/rules.v4
588*nat
589:POSTROUTING ACCEPT - [0:0]
590EOF
Deniscaa72dd2021-06-04 17:13:05 +0300591 for net in $FLOATING_NETWORK_PREFIXES; do
Vasyl Saienko02df39a2020-05-20 16:39:35 +0300592cat << EOF >> /etc/iptables/rules.v4
593-A POSTROUTING -s ${net} -o ${DEFAULT_INTERFACE} -j MASQUERADE
594EOF
Deniscaa72dd2021-06-04 17:13:05 +0300595 done
Vasyl Saienko02df39a2020-05-20 16:39:35 +0300596
597cat << EOF >> /etc/iptables/rules.v4
598COMMIT
599EOF
Deniscaa72dd2021-06-04 17:13:05 +0300600 sudo netfilter-persistent reload
601 fi
Vasyl Saienko7d7acea2020-01-03 16:59:05 +0200602}
603
Dennis Dmitrievea894212020-03-12 21:36:43 +0200604function disable_rp_filter {
605 # Run this func before "network_config" to create new interfaces with the default rp_filter value
606 cat << EOF > /etc/sysctl.d/99-disable-rp-filter.conf
607net.ipv4.conf.all.rp_filter=0
608net.ipv4.conf.default.rp_filter=0
609EOF
Vasyl Saienkoe61a98c2021-10-19 21:42:19 +0300610 for iface in $(find /proc/sys/net/ipv4/conf/ -name "enp*" -o -name "ens*" -o -name "eth*"); do
611 echo 0 > $iface/rp_filter
Oleh Hryhorov207137d2021-04-13 12:03:39 +0300612 done
613
Dennis Dmitrievea894212020-03-12 21:36:43 +0200614 sysctl -p /etc/sysctl.d/99-disable-rp-filter.conf
615}
616
Vasyl Saienko45280eb2024-06-24 11:45:13 +0000617function configure_sysctl_limits {
618 cat << EOF > /etc/sysctl.d/99-limits.conf
619fs.inotify.max_user_instances = 8192
620EOF
621 sysctl -p /etc/sysctl.d/99-limits.conf
622}
623
Vasyl Saienko83f309e2020-05-06 11:14:28 +0300624function configure_contrack {
625 cat << EOF > /etc/sysctl.d/100-contrackd.conf
626net.netfilter.nf_conntrack_log_invalid=255
627net.netfilter.nf_conntrack_tcp_be_liberal=1
628EOF
629
630 sysctl -p /etc/sysctl.d/100-contrackd.conf
631}
632
Vasyl Saienkob377de02020-05-25 14:51:31 +0300633# NOTE(vsaienko): disable calling iptables when sending packets via bridge.
634function disable_iptables_for_bridges {
635 cat << EOF > /etc/sysctl.d/101-bridge-nf-call-iptables.conf
636net.bridge.bridge-nf-call-ip6tables=0
637net.bridge.bridge-nf-call-iptables=0
638net.bridge.bridge-nf-call-arptables=0
639EOF
640
641 sysctl -p /etc/sysctl.d/101-bridge-nf-call-iptables.conf
642}
643
Oleh Hryhorovb58460e2020-02-05 15:53:48 +0200644function network_config {
Vasyl Saienko9b688992020-02-13 13:50:53 +0200645 PUBLIC_NODE_IP_ADDRESS=${PUBLIC_INTERFACE_IP:-$(ip addr show dev ${PUBLIC_INTERFACE} | grep -Po 'inet \K[\d.]+' | egrep -v "127.0.|172.17")}
646 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)}
647
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200648 local public_interface=${1:-${PUBLIC_INTERFACE}}
649 local cloud_netplan_cfg="/etc/netplan/50-cloud-init.yaml"
650 local match_ip_line
Vasyl Saienko9b688992020-02-13 13:50:53 +0200651
Vasyl Saienko300bb0b2023-04-19 06:52:53 +0000652 install_pkgs bridge-utils
Mykyta Karpin5ed5fd82020-01-27 18:23:29 +0200653
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200654cat << EOF > /etc/systemd/network/10-veth-phy-br.netdev
655[NetDev]
656Name=veth-phy
657Kind=veth
658[Peer]
659Name=veth-br
660EOF
Vasyl Saienko9b688992020-02-13 13:50:53 +0200661
Vasyl Saienko66522982020-05-25 09:34:43 +0300662if [[ -n ${IRONIC_BAREMETAL_INTERFACE} ]]; then
663
Vasyl Saienko4c468192020-05-19 11:51:13 +0300664cat << EOF > /etc/systemd/network/11-veth-bm.netdev
665[NetDev]
666Name=veth-bm
667Kind=veth
668[Peer]
669Name=veth-bm-br
670EOF
671
Vasyl Saienko1233afd2020-05-21 14:55:21 +0300672cat << EOF > /etc/systemd/network/12-veth-vbmc.netdev
673[NetDev]
674Name=veth-vbmc
675Kind=veth
676[Peer]
677Name=veth-vbmc-br
678EOF
Vasyl Saienko66522982020-05-25 09:34:43 +0300679fi
Vasyl Saienko1233afd2020-05-21 14:55:21 +0300680
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200681 sed -i 's/.*ethernets:.*/&\n veth-phy: {}/' ${cloud_netplan_cfg}
682 sed -i 's/.*ethernets:.*/&\n veth-br: {}/' ${cloud_netplan_cfg}
Vasyl Saienko66522982020-05-25 09:34:43 +0300683if [[ -n ${IRONIC_BAREMETAL_INTERFACE} ]]; then
Vasyl Saienko4c468192020-05-19 11:51:13 +0300684 sed -i 's/.*ethernets:.*/&\n veth-bm: {}/' ${cloud_netplan_cfg}
685 sed -i 's/.*ethernets:.*/&\n veth-bm-br: {}/' ${cloud_netplan_cfg}
Vasyl Saienko1233afd2020-05-21 14:55:21 +0300686 sed -i 's/.*ethernets:.*/&\n veth-vbmc: {}/' ${cloud_netplan_cfg}
687 sed -i 's/.*ethernets:.*/&\n veth-vbmc-br: {}/' ${cloud_netplan_cfg}
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200688
Michael Polenchuk486cadf2020-08-24 16:25:51 +0400689 if [[ "${IRONIC_MT_ENABLED,,}" == true ]] ; then
690 sed -i "s/.*ethernets:.*/&\n ${IRONIC_BAREMETAL_VXLAN_INTERFACE}: {}/" ${cloud_netplan_cfg}
691 sed -i 's/.*ethernets:.*/&\n veth-pxe: {}/' ${cloud_netplan_cfg}
692 sed -i 's/.*ethernets:.*/&\n veth-pxe-br: {}/' ${cloud_netplan_cfg}
693cat << EOF > /etc/systemd/network/13-veth-pxe.netdev
694[NetDev]
695Name=veth-pxe
696Kind=veth
697[Peer]
698Name=veth-pxe-br
699EOF
700 sed -i "s/- ${IRONIC_BAREMETAL_NETWORK_PREFIX}\([0-9]*\)/- ${IRONIC_BAREMETAL_TUNNEL_NETWORK_PREFIX}\1/" ${cloud_netplan_cfg}
701cat << EOF >> ${cloud_netplan_cfg}
702 vlans:
703 pxe.1000:
704 id: 1000
705 link: veth-pxe
706 addresses:
707 - ${IRONIC_BAREMETAL_INTERFACE_IP}/${IRONIC_BAREMETAL_NETWORK_NETMASK}
708EOF
709 else
710 ironic_baremetal_address_match_ip_line=$(grep -nm1 "${IRONIC_BAREMETAL_INTERFACE_IP}/${IRONIC_BAREMETAL_NETWORK_NETMASK}" ${cloud_netplan_cfg} | cut -d: -f1)
711 sed -i "$((${ironic_baremetal_address_match_ip_line}-1)),$((${ironic_baremetal_address_match_ip_line}))d" ${cloud_netplan_cfg}
712 fi
Vasyl Saienko66522982020-05-25 09:34:43 +0300713fi
Vasyl Saienko4c468192020-05-19 11:51:13 +0300714
Vasyl Saienko66522982020-05-25 09:34:43 +0300715 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 +0300716 if [ -n "${public_address_match_ip_line}" ] ; then
717 sed -i "$((${public_address_match_ip_line}-1)),$((${public_address_match_ip_line}))d" ${cloud_netplan_cfg}
718 fi
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200719
720cat << EOF >> ${cloud_netplan_cfg}
721 bridges:
722 br-public:
723 dhcp4: false
724 interfaces:
725 - ${PUBLIC_INTERFACE}
726 - veth-br
727 addresses:
728 - ${PUBLIC_NODE_IP_ADDRESS}/${PUBLIC_NODE_IP_NETMASK}
Vasyl Saienko66522982020-05-25 09:34:43 +0300729EOF
Vasyl Saienko5b6d70a2023-04-10 13:55:31 +0000730# Assign more ips for neutron dynamic routing PRODX-31417
731if [[ ${NODE_METADATA} == *"tempest"* ]]; then
732 for i in {71..76}; do
733cat << EOF >> ${cloud_netplan_cfg}
Vasyl Saienkoc7639002023-04-11 07:40:29 +0000734 - ${PUBLIC_NODE_IP_ADDRESS%.*}.${i}/${PUBLIC_NODE_IP_NETMASK}
Vasyl Saienko5b6d70a2023-04-10 13:55:31 +0000735EOF
736 done
737fi
Vasyl Saienko66522982020-05-25 09:34:43 +0300738
Vasyl Saienkof79d0942020-11-17 14:33:55 +0200739# Remove Tunnel interface from netplan
740if [[ $TUNNEL_INTERFACE_NETPLAN_MANAGE == false ]]; then
741 sed -i "/ ${TUNNEL_INTERFACE}/,/ set-name: ${TUNNEL_INTERFACE}/d" ${cloud_netplan_cfg}
742fi
743
Vasyl Saienko66522982020-05-25 09:34:43 +0300744if [[ -n ${IRONIC_BAREMETAL_INTERFACE} ]]; then
745cat << EOF >> ${cloud_netplan_cfg}
Vasyl Saienko4c468192020-05-19 11:51:13 +0300746 br-baremetal:
747 dhcp4: false
748 interfaces:
Vasyl Saienko4c468192020-05-19 11:51:13 +0300749 - veth-bm-br
Vasyl Saienko1233afd2020-05-21 14:55:21 +0300750 - veth-vbmc-br
Michael Polenchuk486cadf2020-08-24 16:25:51 +0400751EOF
752 if [[ "${IRONIC_MT_ENABLED,,}" != true ]] ; then
753cat << EOF >> ${cloud_netplan_cfg}
754 - ${IRONIC_BAREMETAL_INTERFACE}
Vasyl Saienko4c468192020-05-19 11:51:13 +0300755 addresses:
756 - ${IRONIC_BAREMETAL_INTERFACE_IP}/${IRONIC_BAREMETAL_NETWORK_NETMASK}
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200757EOF
Michael Polenchuk486cadf2020-08-24 16:25:51 +0400758 else
759cat << EOF >> ${cloud_netplan_cfg}
760 - ${IRONIC_BAREMETAL_VXLAN_INTERFACE}
761 - veth-pxe-br
762EOF
Vasyl Saienko3dd0c272020-11-16 17:20:07 +0200763
Michael Polenchuk486cadf2020-08-24 16:25:51 +0400764cat << EOF > /etc/systemd/system/ironic-vxlan-tunnel.service
765[Unit]
766Description=Ironic VXLAN tunnel
767After=network.target
768
769[Service]
770Type=oneshot
771RemainAfterExit=true
772ExecStart=/sbin/ip link add ${IRONIC_BAREMETAL_VXLAN_INTERFACE} type vxlan id 10 group 239.1.1.10 dstport 0 dev ${IRONIC_BAREMETAL_INTERFACE}
773ExecStart=/sbin/ip link set ${IRONIC_BAREMETAL_VXLAN_INTERFACE} up
774
775[Install]
776WantedBy=multi-user.target
777EOF
778
779 systemctl enable ironic-vxlan-tunnel.service
780 systemctl start ironic-vxlan-tunnel.service
781 fi
Vasyl Saienko66522982020-05-25 09:34:43 +0300782fi
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200783 netplan --debug apply
Vasyl Saienko9b688992020-02-13 13:50:53 +0200784
785 # NOTE(vsaienko): give some time to apply changes
786 sleep 15
Vasyl Saienko16c81c02020-11-17 17:10:55 +0200787
788# Remove Tunnel interface from netplan
789if [[ $TUNNEL_INTERFACE_NETPLAN_MANAGE == false ]]; then
790 ip addr flush ${TUNNEL_INTERFACE}
791 ip link set ${TUNNEL_INTERFACE} up
792fi
793
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200794}
795
Oleh Hryhorovb58460e2020-02-05 15:53:48 +0200796$functions_override
797
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200798function set_node_labels {
Vasyl Saienko2ae2eec2020-02-20 10:11:32 +0200799 function set_node_labels_retry {
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300800 ${_KUBECTL_CMD} patch node $(hostname) -p "{\"metadata\": $(echo $NODE_METADATA | jq -c ".")}"
Vasyl Saienko2ae2eec2020-02-20 10:11:32 +0200801 }
Vasyl Saienko08024582020-02-20 11:45:34 +0200802 retry 10 "Labeling node failed" set_node_labels_retry
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200803}
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100804
Vasyl Saienko1eb06d62020-11-16 10:16:22 +0200805HW_METADATA='{}'
Vasyl Saienkof9ee1582020-03-02 16:53:41 +0200806function prepare_metadata_files {
Dennis Dmitrievea894212020-03-12 21:36:43 +0200807 /usr/sbin/prepare-metadata.py --metadata-file /usr/share/metadata/lab-metadata.yaml
808}
809
810function collect_ceph_metadata {
Oleh Hryhorovf14fc922020-03-10 12:11:10 +0200811 local ceph_osd_node
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300812 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 +0200813
Oleh Hryhorovf14fc922020-03-10 12:11:10 +0200814 if [[ -f /usr/share/metadata/ceph.yaml && ${ceph_osd_node} ]]; then
Vasyl Saienkof9ee1582020-03-02 16:53:41 +0200815 HW_METADATA="{\"ceph\": {\"$(hostname)\": \"$(base64 -w 0 /usr/share/metadata/ceph.yaml)\"}}"
Oleh Hryhorov39fbfe52020-03-12 15:26:53 +0200816 ceph_store_drive=$(cat /usr/share/metadata/ceph.yaml | egrep '\- name\: vd?' | awk '{print $3}')
817 if [[ -b /dev/${ceph_store_drive} ]]; then
818 sgdisk --zap-all /dev/${ceph_store_drive}
819 fi
Vasyl Saienkof9ee1582020-03-02 16:53:41 +0200820 fi
821}
822
Oleh Hryhorove7feab42020-11-11 18:00:29 +0200823function collect_interfaces_metadata {
Vasyl Saienko3dd0c272020-11-16 17:20:07 +0200824
Oleh Hryhorove7feab42020-11-11 18:00:29 +0200825 local if_metadata_file="/usr/share/metadata/interfaces.yaml"
Denisd2635bf2021-06-15 13:35:36 +0300826 if [ -z "${TUNNEL_INTERFACE}" ] ; then
827 echo "Tunnel interface is empty"
828 else
829 pci_id=$(ethtool -i ${TUNNEL_INTERFACE} |grep bus-info | awk '{print $2}')
830 mac=$(cat $(find /sys/bus/pci/devices/${pci_id}/ -name net)/${TUNNEL_INTERFACE}/address)
831 ip=${TUNNEL_INTERFACE_IP}/${TUNNEL_INTERFACE_NETWORK_NETMASK}
Oleh Hryhorove7feab42020-11-11 18:00:29 +0200832
Denisd2635bf2021-06-15 13:35:36 +0300833 cat << EOF > ${if_metadata_file}
Vasyl Saienko592730c2020-11-17 11:36:46 +0200834${TUNNEL_INTERFACE}:
Vasyl Saienko1152e982020-11-16 16:23:32 +0200835 pci_id: "${pci_id}"
836 ip_addr: "${ip}"
837 mac: "${mac}"
Oleh Hryhorove7feab42020-11-11 18:00:29 +0200838EOF
839
Denisd2635bf2021-06-15 13:35:36 +0300840 fi
Oleh Hryhorove7feab42020-11-11 18:00:29 +0200841 HW_METADATA=$(echo ${HW_METADATA} | jq -cr ". += {\"interfaces\": {\"$(hostname)\": \"$(base64 -w 0 ${if_metadata_file})\"}}")
842
843}
844
Oleh Hryhorovab3793f2020-06-04 18:37:22 +0300845function nested_virt_config {
846
Vasyl Saienkoe61a98c2021-10-19 21:42:19 +0300847if grep --color vmx /proc/cpuinfo; then
848 cat << EOF > /etc/modprobe.d/qemu-system-x86.conf
Oleh Hryhorovab3793f2020-06-04 18:37:22 +0300849options kvm_intel nested=1
850options kvm_intel enable_apicv=1
851options kvm_intel ept=1
852EOF
853
854 modprobe -r kvm_intel || true
855 modprobe kvm_intel nested=1
Vasyl Saienkoe61a98c2021-10-19 21:42:19 +0300856else
857 echo "Nested virtualization is not supported"
858fi
Oleh Hryhorovab3793f2020-06-04 18:37:22 +0300859
860}
Vasyl Saienkof9ee1582020-03-02 16:53:41 +0200861
Mykyta Karpin6a153c52020-11-03 15:21:38 +0200862function configure_huge_pages {
863 local nr_2Mb
864 local nr_1G
865 local apply_grub=false
866
867 if [[ ! $(cat /etc/default/grub | grep -E ^GRUB_CMDLINE_LINUX=".*hugepagesz=2M.*") ]] && [[ "${HUGE_PAGES_2Mb_SIZE}" -gt 0 ]]; then
868 nr_2Mb=$(( "${HUGE_PAGES_2Mb_SIZE}" / 2 ))
869 echo "Configure GRUB kernel cmd to allocate ${nr_2Mb} 2Mb huge pages"
870 echo "GRUB_CMDLINE_LINUX=\"\$GRUB_CMDLINE_LINUX hugepagesz=2M hugepages=$nr_2Mb\"" >> /etc/default/grub
871 apply_grub=true
872 fi
873
874 if [[ ! $(cat /etc/default/grub | grep -E ^GRUB_CMDLINE_LINUX=".*hugepagesz=1G.*") ]] && [[ "${HUGE_PAGES_1G_SIZE}" -gt 0 ]]; then
875 echo "Configure GRUB kernel cmd to allocate ${HUGE_PAGES_1G_SIZE} 1G huge pages"
876 echo "GRUB_CMDLINE_LINUX=\"\$GRUB_CMDLINE_LINUX hugepagesz=1G hugepages=${HUGE_PAGES_1G_SIZE}\"" >> /etc/default/grub
877 apply_grub=true
878 fi
879
880 if [[ "${apply_grub}" == true ]]; then
881 update-grub
882 cloud-init clean -r
883 else
884 echo "GRUB already configured with huge pages"
885 fi
886}
887
Oleh Hryhorov43bbc8c2020-12-03 15:30:09 +0200888function disable_master_taint {
889
Oleh Hryhorovcd1a3bd2020-12-04 13:21:06 +0200890 function disable_master_taint_retry {
891 # Enable "Allow all authenticated users, including service accounts to schedule on all nodes, including UCP managers."
892 curl --retry 6 --retry-delay 5 -k -H "Authorization: Bearer $AUTHTOKEN" -k -X PUT https://${UCP_MASTER_HOST}/collectionGrants/authenticated/swarm/scheduler
893 }
894
895 retry 10 "Can't enable permission to schedule all pods on UCP master." disable_master_taint_retry
Oleh Hryhorov43bbc8c2020-12-03 15:30:09 +0200896
897}
898
Vasyl Saienko94843cb2021-02-26 16:43:34 +0200899function disable_ipv6 {
900 cat << EOF > /etc/sysctl.d/11-ipv6-disable.conf
901net.ipv6.conf.all.disable_ipv6 = 1
902net.ipv6.conf.default.disable_ipv6 = 1
903net.ipv6.conf.lo.disable_ipv6 = 1
904EOF
905 sysctl -p /etc/sysctl.d/11-ipv6-disable.conf
906}
907
Vasyl Saienko846c6342021-02-24 17:01:36 +0200908function setup_evpn {
909
910cat << EOF > /etc/netplan/60-evpn.yaml
911network:
912 version: 2
913 ethernets:
Vasyl Saienko5f953782021-02-25 10:30:32 +0200914EOF
915for vni in $(seq ${FRR_EVPN_TUNNELS_RANGE}); do
916cat << EOF >> /etc/netplan/60-evpn.yaml
917 vxlan$vni: {}
918EOF
919done
920cat << EOF >> /etc/netplan/60-evpn.yaml
Vasyl Saienko846c6342021-02-24 17:01:36 +0200921 bridges:
Vasyl Saienko5f953782021-02-25 10:30:32 +0200922EOF
923for vni in $(seq ${FRR_EVPN_TUNNELS_RANGE}); do
924cat << EOF >> /etc/netplan/60-evpn.yaml
925 br$vni:
Vasyl Saienko846c6342021-02-24 17:01:36 +0200926 dhcp4: false
Vasyl Saienkof18b4782021-03-03 14:15:40 +0200927 link-local: []
Vasyl Saienko846c6342021-02-24 17:01:36 +0200928 parameters:
929 stp: false
Vasyl Saienko5f953782021-02-25 10:30:32 +0200930 interfaces:
931 - vxlan$vni
Vasyl Saienko846c6342021-02-24 17:01:36 +0200932 addresses:
Vasyl Saienko5f953782021-02-25 10:30:32 +0200933 - 192.168.$vni.254/24
Vasyl Saienko846c6342021-02-24 17:01:36 +0200934EOF
Vasyl Saienko5f953782021-02-25 10:30:32 +0200935done
Vasyl Saienko846c6342021-02-24 17:01:36 +0200936
Vasyl Saienko5f953782021-02-25 10:30:32 +0200937for vni in $(seq ${FRR_EVPN_TUNNELS_RANGE}); do
938cat << EOF > /etc/systemd/system/evpn-vxlan-tunnel-${vni}.service
Vasyl Saienko846c6342021-02-24 17:01:36 +0200939[Unit]
Vasyl Saienko5f953782021-02-25 10:30:32 +0200940Description=EVPN tunnel ${vni}
Vasyl Saienko846c6342021-02-24 17:01:36 +0200941After=network.target
942
943[Service]
944Type=oneshot
945RemainAfterExit=true
Vasyl Saienkob3b8c4e2021-02-26 10:54:27 +0200946ExecStart=/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 +0200947ExecStart=/sbin/ip link set vxlan${vni} up
Vasyl Saienko846c6342021-02-24 17:01:36 +0200948
949[Install]
950WantedBy=multi-user.target
951EOF
Vasyl Saienko5f953782021-02-25 10:30:32 +0200952 systemctl enable evpn-vxlan-tunnel-${vni}.service
953 systemctl start evpn-vxlan-tunnel-${vni}.service
954done
Vasyl Saienko846c6342021-02-24 17:01:36 +0200955
956 netplan --debug apply
957 # NOTE(vsaienko): give some time to apply changes
958 sleep 15
Vasyl Saienko846c6342021-02-24 17:01:36 +0200959}
960
Vasyl Saienko25bb75b2021-02-15 12:22:33 +0200961function install_frr {
962 function _install_frr_packages {
Vasyl Saienko25bb75b2021-02-15 12:22:33 +0200963 FRRVER="frr-stable"
Oleh Hryhorov6243c9f2023-09-21 09:53:09 +0000964 curl -s https://mirror.mirantis.com/.snapshots/frr-stable-latest/keys.asc | sudo apt-key add -
Oleh Hryhorov2ae1db82023-09-21 07:39:50 +0000965 add-apt-repository "deb [arch=amd64] https://mirror.mirantis.com/.snapshots/frr-stable-latest/ ${OS_CODENAME} ${FRRVER}"
Vasyl Saienko300bb0b2023-04-19 06:52:53 +0000966 install_pkgs frr frr-pythontools
Vasyl Saienko25bb75b2021-02-15 12:22:33 +0200967 }
968 retry 3 "Can't install frr packages" _install_frr_packages
969
970 sed -i 's/^bgpd=no/bgpd=yes/g' /etc/frr/daemons
971
972 cat << EOF > /etc/frr/frr.conf
973log syslog informational
974no ipv6 forwarding
975service integrated-vtysh-config
976!
977router bgp ${FRR_BGP_AS_NUMBER}
978 bgp router-id ${FRR_ROUTER_ID}
979 no bgp default ipv4-unicast
980 bgp cluster-id ${FRR_ROUTER_ID}
981 coalesce-time 1000
Vasyl Saienko846c6342021-02-24 17:01:36 +0200982 neighbor rr peer-group
Vasyl Saienkob3b8c4e2021-02-26 10:54:27 +0200983 neighbor rr capability extended-nexthop
984 neighbor rr remote-as ${FRR_BGP_AS_NUMBER}
Vasyl Saienko846c6342021-02-24 17:01:36 +0200985 neighbor rr update-source ${FRR_BGP_SOURCE_ADDRESS}
986EOF
987
988for neighbor_ip in $(echo $FRR_BGP_NEIGHBORS | tr "," "\n"); do
989 cat << EOF >> /etc/frr/frr.conf
990 neighbor $neighbor_ip peer-group rr
991EOF
992
993done
994 cat << EOF >> /etc/frr/frr.conf
Vasyl Saienko25bb75b2021-02-15 12:22:33 +0200995 !
996 address-family ipv4 unicast
Vasyl Saienko846c6342021-02-24 17:01:36 +0200997 neighbor rr activate
Vasyl Saienko25bb75b2021-02-15 12:22:33 +0200998 exit-address-family
999 !
1000 address-family ipv4 vpn
Vasyl Saienko846c6342021-02-24 17:01:36 +02001001 neighbor rr activate
Vasyl Saienko25bb75b2021-02-15 12:22:33 +02001002 exit-address-family
1003 !
1004 address-family l2vpn evpn
Vasyl Saienko846c6342021-02-24 17:01:36 +02001005 neighbor rr activate
Vasyl Saienko25bb75b2021-02-15 12:22:33 +02001006 advertise-all-vni
1007 advertise-default-gw
1008 exit-address-family
1009!
1010line vty
1011!
1012EOF
1013
1014 systemctl restart frr
Vasyl Saienko846c6342021-02-24 17:01:36 +02001015
Vasyl Saienko94843cb2021-02-26 16:43:34 +02001016 disable_ipv6
Vasyl Saienko846c6342021-02-24 17:01:36 +02001017 setup_evpn
Vasyl Saienko25bb75b2021-02-15 12:22:33 +02001018}
1019
Mykyta Karpinbac78b42021-12-07 11:25:03 +02001020function cron_disable_calico_offloading {
Denis Biletskiy49b91ea2022-06-09 11:41:39 +02001021 # The workaround is to disable offloading on vxlan interface
1022 if [ -f /etc/cron.d/disable_calico_offloading ]; then
1023 echo "Cronjob for disable callico offloading already exists"
1024 else
1025 script_path="/usr/sbin/calico_disable_offloading.sh"
1026 cat << 'EOF' >> $script_path
1027#!/usr/bin/env bash
1028if /sbin/ethtool --show-offload vxlan.calico | grep -E '^.x-checksumming:' | grep -q ': on'; then
1029 /sbin/ethtool --offload vxlan.calico rx off tx off > /dev/null
1030fi
Mykyta Karpinbac78b42021-12-07 11:25:03 +02001031EOF
Denis Biletskiy49b91ea2022-06-09 11:41:39 +02001032 chmod +x $script_path
1033 cat << EOF >> /etc/cron.d/disable_calico_offloading
1034* * * * * root $script_path 2>&1 | /usr/bin/logger -t calico_disable_offloading
1035EOF
1036 fi
Mykyta Karpinbac78b42021-12-07 11:25:03 +02001037}
1038
Vasyl Saienko114333f2022-05-24 09:34:51 +00001039function increase_iscsi_timeout {
1040 cat <<EOF > /etc/udev/rules.d/50-iscsi-timeout.rules
1041ACTION=="add", SUBSYSTEM=="scsi" , SYSFS{type}=="0|7|14",
1042RUN+="/bin/sh -c 'echo 120 > /sys/block/%k/device/timeout'"
1043EOF
1044 udevadm control --reload-rules && udevadm trigger
1045}