blob: f3d5e39f3714ee7c684a8033af4d53ddccc77a44 [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}
Mykyta Karpin9df830a2024-09-12 14:25:01 +020062UCP_AUDIT_LOG_LEVEL=${UCP_AUDIT_LOG_LEVEL:-''}
Vasyl Saienkocfbedc72021-02-15 18:02:56 +020063# Change default VXLAN port, need for https://mirantis.jira.com/browse/PRODX-11679
64UCP_DOCKER_SWARM_DATA_PORT=${UCP_DOCKER_SWARM_DATA_PORT:-4789}
Vasyl Saienko06c7afe2022-07-01 17:53:56 +000065UCP_DOCKER_CALICO_VXLAN_PORT=${UCP_DOCKER_CALICO_VXLAN_PORT:-${UCP_DOCKER_SWARM_DATA_PORT}}
Vasyl Saienko08166342020-02-28 12:20:51 +020066NTP_SERVERS=${NTP_SERVERS:-"ldap.scc.mirantis.net ldap.bud.mirantis.net"}
Michael Polenchuk486cadf2020-08-24 16:25:51 +040067IRONIC_BAREMETAL_VXLAN_INTERFACE='vxlan10'
Mykyta Karpin6a153c52020-11-03 15:21:38 +020068HUGE_PAGES_1G_SIZE=$(echo "${HUGE_PAGES}" | awk -F ',' '{print $1}')
69HUGE_PAGES_2Mb_SIZE=$(echo "${HUGE_PAGES}" | awk -F ',' '{print $2}')
70CONFIGURE_HUGE_PAGES=false
Vasyl Saienko3dd0c272020-11-16 17:20:07 +020071TUNNEL_INTERFACE_NETPLAN_MANAGE=true
Anton Samoylov1d140ee2020-11-23 22:40:13 +040072TUNGSTENFABRIC_ENABLED=${TUNGSTENFABRIC_ENABLED:-false}
Mykyta Karpin6a153c52020-11-03 15:21:38 +020073if [[ "${HUGE_PAGES_1G_SIZE}" -gt 0 ]] || [[ "${HUGE_PAGES_2Mb_SIZE}" -gt 0 ]]; then
74 CONFIGURE_HUGE_PAGES=true
Anton Samoylov1d140ee2020-11-23 22:40:13 +040075 if [[ "${TUNGSTENFABRIC_ENABLED,,}" == false ]]; then
76 TUNNEL_INTERFACE_NETPLAN_MANAGE=false
77 fi
Mykyta Karpin6a153c52020-11-03 15:21:38 +020078fi
Oleh Hryhorov43bbc8c2020-12-03 15:30:09 +020079SINGLE_NODE=${SINGLE_NODE:-true}
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +010080
Vasyl Saienko25bb75b2021-02-15 12:22:33 +020081#FRR Options
82FRR_BGP_AS_NUMBER=${FRR_BGP_AS_NUMBER:-64512}
83FRR_ROUTER_ID=${TUNNEL_INTERFACE_IP}
Vasyl Saienko846c6342021-02-24 17:01:36 +020084FRR_BGP_SOURCE_ADDRESS=${TUNNEL_INTERFACE_IP}
85FRR_BGP_NEIGHBORS=${FRR_BGP_NEIGHBORS:-}
Vasyl Saienko5f953782021-02-25 10:30:32 +020086FRR_EVPN_TUNNELS_RANGE=${FRR_EVPN_TUNNELS_RANGE:-'20 50'}
Vasyl Saienkob3b8c4e2021-02-26 10:54:27 +020087FRR_EVPN_VXLAN_DST_PORT=${FRR_EVPN_VXLAN_DST_PORT:-4790}
Vasyl Saienko25bb75b2021-02-15 12:22:33 +020088
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +020089LVM_LOOP_DEVICE_SIZE=${LVM_LOOP_DEVICE_SIZE:-0}
Vasyl Saienkocd302062021-04-21 22:25:18 +030090CINDER_LVM_LOOP_DEVICE_SIZE=${CINDER_LVM_LOOP_DEVICE_SIZE:-0}
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +020091
Vasyl Saienko8d34ad02021-07-26 11:55:23 +030092_KUBECTL_CMD="kubectl --kubeconfig /etc/kubernetes/admin.conf "
93
Vasyl Saienkoa51d1ca2020-01-02 15:59:16 +020094function retry {
95 local retries=$1
96 shift
97 local msg="$1"
98 shift
99
100 local count=0
101 until "$@"; do
102 exit=$?
103 wait=$((2 ** $count))
104 count=$(($count + 1))
105 if [ $count -lt $retries ]; then
106 echo "Retry $count/$retries exited $exit, retrying in $wait seconds..."
107 sleep $wait
108 else
109 echo "Retry $count/$retries exited $exit, no more retries left."
110 echo "$msg"
111 return $exit
112 fi
113 done
114 return 0
115}
116
Vasyl Saienko300bb0b2023-04-19 06:52:53 +0000117function install_pkgs {
118 local pkgs="$@"
119 function _retry_install {
Vasyl Saienko92133c02024-05-16 10:18:51 +0300120 apt update && DEBIAN_FRONTEND=noninteractive apt install -o DPkg::Options::=--force-confdef -y $pkgs
Vasyl Saienko300bb0b2023-04-19 06:52:53 +0000121 }
122 retry 10 "Labeling node failed" _retry_install
123}
124
Vasyl Saienko806d5dc2022-02-01 14:18:09 +0200125function setup_bind_mounts {
126 mkdir -p /var/lib/persistant-data
Vasyl Saienkoe3107252023-06-06 07:02:58 +0000127 for component in nova glance cinder docker; do
Vasyl Saienko3cc01ca2023-09-25 12:23:43 +0000128 if [[ -d /var/lib/${component} ]]; then
129 continue
130 fi
Vasyl Saienko806d5dc2022-02-01 14:18:09 +0200131 mkdir -p /var/lib/data/${component} /var/lib/${component}
Vasyl Saienko3cc01ca2023-09-25 12:23:43 +0000132 echo "/var/lib/data/${component} /var/lib/${component} none bind 0 0" >> /etc/fstab
Vasyl Saienko806d5dc2022-02-01 14:18:09 +0200133 done
Vasyl Saienkoe3107252023-06-06 07:02:58 +0000134
135 if [[ "${KUBERNETES_INSTALLER}" == "k0s" ]]; then
136 mkdir -p /var/lib/k0s/kubelet
137 mkdir -p /var/lib/kubelet
138 echo "/var/lib/k0s/kubelet /var/lib/kubelet none bind 0 0" >> /etc/fstab
139 fi
140
Vasyl Saienko806d5dc2022-02-01 14:18:09 +0200141 mount -a
142}
143
Vasyl Saienko392dc712020-04-07 15:56:10 +0300144function configure_atop {
145 sed -i 's/INTERVAL=600/INTERVAL=60/' /usr/share/atop/atop.daily
146 systemctl restart atop
147}
148
dbiletskiy70af8192024-06-19 09:27:10 +0200149function disable_unattended_upgr {
Vasyl Saienkoaadd2f82020-08-07 09:35:03 +0300150 # Disable unattended upgrades
151 sed -i 's/1/0/' /etc/apt/apt.conf.d/20auto-upgrades
Oleh Hryhorovbe7bdf72021-04-05 18:21:22 +0300152 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 +0200153}
154
Vasyl Saienkof9813512024-07-02 12:30:25 +0300155function configure_logind_conf {
156
157 cat <<EOF >/etc/systemd/logind.conf
158[Login]
159#NAutoVTs=6
160#ReserveVT=6
161#KillUserProcesses=no
162#KillOnlyUsers=
163#KillExcludeUsers=root
164InhibitDelayMaxSec=600
165#UserStopDelaySec=10
166#HandlePowerKey=poweroff
167#HandleSuspendKey=suspend
168#HandleHibernateKey=hibernate
169#HandleLidSwitch=suspend
170#HandleLidSwitchExternalPower=suspend
171#HandleLidSwitchDocked=ignore
172#HandleRebootKey=reboot
173#PowerKeyIgnoreInhibited=no
174#SuspendKeyIgnoreInhibited=no
175#HibernateKeyIgnoreInhibited=no
176#LidSwitchIgnoreInhibited=yes
177#RebootKeyIgnoreInhibited=no
178#HoldoffTimeoutSec=30s
179#IdleAction=ignore
180#IdleActionSec=30min
181#RuntimeDirectorySize=10%
182#RuntimeDirectoryInodesMax=400k
183#RemoveIPC=yes
184#InhibitorsMax=8192
185#SessionsMax=8192
186
187EOF
188
189 systemctl daemon-reload
190
191}
192
dbiletskiy70af8192024-06-19 09:27:10 +0200193function install_required_packages {
Mykyta Karpind6cde5e2022-07-05 12:06:06 +0300194 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 +0300195
Vasyl Saienkoa7cfc322020-11-06 16:52:22 +0200196 if [[ "${CONFIGURE_HUGE_PAGES}" == true ]]; then
dbiletskiy090662a2024-07-08 14:27:26 +0200197 # Since jammy the igb_uio kernel module contains in the dpdk-kmods-dkms package
198 if [[ ${CODENAME} == "bionic" ]] || [[ ${CODENAME} == "focal" ]]; then
199 pkg_list="$pkg_list dpdk-igb-uio-dkms"
200 else
201 pkg_list="$pkg_list dpdk-kmods-dkms"
202 fi
Anton Samoylovec019d52020-11-19 18:55:47 +0400203 # linux-image-extra-$(uname -r) provides uio_pci_generic module, used by TF DPDK as default driver
204 if [[ "${TUNGSTENFABRIC_ENABLED,,}" == true ]]; then
205 pkg_list="$pkg_list linux-modules-extra-$(uname -r)"
206 fi
Vasyl Saienkoa7cfc322020-11-06 16:52:22 +0200207 fi
Vasyl Saienko300bb0b2023-04-19 06:52:53 +0000208 install_pkgs $pkg_list
209 # Since version 4 yq uses another syntax
Oleksandr Kononenko231933b2024-02-05 16:46:35 +0200210 if [[ ! -f /usr/bin/yq ]]; then
211 curl --retry 6 --retry-delay 5 -L ${BINARY_BASE_URL}/openstack/bin/utils/yq/yq-v3.3.2 -o /usr/bin/yq
212 chmod +x /usr/bin/yq
213 fi
Mykyta Karpin5ed5fd82020-01-27 18:23:29 +0200214}
215
Oleksii Grudev4399e232021-02-24 19:08:25 +0200216function enable_iscsi {
217 systemctl enable iscsid
218 systemctl restart iscsid
219}
Mykyta Karpin5ed5fd82020-01-27 18:23:29 +0200220
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +0200221function configure_lvm {
Vasyl Saienkocd302062021-04-21 22:25:18 +0300222 function _setup_lvm {
223 local device_num=$1
224 local device_size=$2
225 local vg_name=$3
Denis Biletskiy3b21a822022-05-10 10:29:29 +0300226 if vgdisplay | grep ${vg_name}; then
227 echo "Volume group ${vg_name} is already initialize"
Deniscaa72dd2021-06-04 17:13:05 +0300228 else
229 #configure lvm only on compute nodes
230 if [[ ${NODE_METADATA} == *"openstack-compute-node"* ]]; then
231 truncate --size ${device_size}G /srv/disk${device_num}
232 cat <<EOF > /etc/systemd/system/setup-loopback-loop${device_num}.service
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +0200233[Unit]
Vasyl Saienkocd302062021-04-21 22:25:18 +0300234Description=Setup loop${device_num} device
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +0200235DefaultDependencies=no
236After=systemd-udev-settle.service
237Before=lvm2-activation-early.service
238Wants=systemd-udev-settle.service
239
240[Service]
Vasyl Saienkocd302062021-04-21 22:25:18 +0300241ExecStart=-/sbin/losetup /dev/loop${device_num} /srv/disk${device_num}
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +0200242RemainAfterExit=true
243Type=oneshot
244
245[Install]
246WantedBy=local-fs.target
247EOF
Deniscaa72dd2021-06-04 17:13:05 +0300248 systemctl enable setup-loopback-loop${device_num}
249 systemctl start setup-loopback-loop${device_num}
250 #adding short sleep to give time for service to start
251 sleep 3
252 pvcreate /dev/loop${device_num}
253 vgcreate ${vg_name} /dev/loop${device_num}
254 fi
Vasyl Saienkocd302062021-04-21 22:25:18 +0300255 fi
256 }
257
258 if [[ "${LVM_LOOP_DEVICE_SIZE}" -gt 0 ]]; then
Denis Biletskiy3b21a822022-05-10 10:29:29 +0300259 _setup_lvm 10 ${LVM_LOOP_DEVICE_SIZE} nova-vol
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +0200260 fi
Vasyl Saienkocd302062021-04-21 22:25:18 +0300261 if [[ "${CINDER_LVM_LOOP_DEVICE_SIZE}" -gt 0 ]]; then
Denis Biletskiy3b21a822022-05-10 10:29:29 +0300262 _setup_lvm 11 ${CINDER_LVM_LOOP_DEVICE_SIZE} cinder-vol
Vasyl Saienkocd302062021-04-21 22:25:18 +0300263 fi
264
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +0200265}
266
Mykyta Karpin5ed5fd82020-01-27 18:23:29 +0200267function install_docker {
268 function install_retry {
Vasyl Saienkofa714102024-03-22 09:30:25 +0200269 curl --retry 6 --retry-delay 5 -fsSL "${DOCKER_EE_URL}/gpg" | sudo apt-key add -
270 add-apt-repository "deb [arch=amd64] ${DOCKER_EE_URL}/ ${OS_CODENAME} ${DOCKER_EE_RELEASE}"
271 install_pkgs ${DOCKER_EE_PACKAGES}
Vasyl Saienko92133c02024-05-16 10:18:51 +0300272 if [[ $KUBERNETES_CONTAINER_RUNTIME == "crio" ]]; then
273 NERDCTL_VERSION=1.7.6
274 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
275 tar zxvf nerdctl-${NERDCTL_VERSION}-linux-amd64.tar.gz -C /usr/local/bin
276 fi
Mykyta Karpin5ed5fd82020-01-27 18:23:29 +0200277 }
278 retry 10 "Failed to install docker" install_retry
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100279}
280
Mykyta Karpin7abaea02020-07-27 15:43:07 +0300281function prepare_docker_config {
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100282 mkdir -p /etc/docker
283 cat <<EOF > /etc/docker/daemon.json
284{
Vasyl Saienkoa37de442024-05-02 17:15:47 +0300285 "default-cgroupns-mode": "host",
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100286 "default-address-pools": [
287 { "base": "${DOCKER_DEFAULT_ADDRESS_POOL}", "size": ${DOCKER_DEFAULT_ADDRESS_SIZE} }
Mykyta Karpin7abaea02020-07-27 15:43:07 +0300288 ],
289 "log-driver": "json-file",
290 "log-opts": {
291 "max-size": "1g",
292 "max-file": "3"
293 }
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100294}
295EOF
296
Vasyl Saienko92133c02024-05-16 10:18:51 +0300297 if [[ $KUBERNETES_CONTAINER_RUNTIME == "crio" ]]; then
298 mkdir -p /etc/containerd/
299cat <<EOF> /etc/containerd/config.toml
Vasyl Saienko05edc402024-06-11 11:17:21 +0300300version = 2
301[plugins]
302[plugins."io.containerd.grpc.v1.cri".containerd.runtimes]
303 [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
304 runtime_type = "io.containerd.runc.v2"
305 [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
306 SystemdCgroup = true
Vasyl Saienko92133c02024-05-16 10:18:51 +0300307EOF
Vasyl Saienko05edc402024-06-11 11:17:21 +0300308 systemctl restart containerd || /bin/true
Vasyl Saienko92133c02024-05-16 10:18:51 +0300309 fi
310
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100311}
312
Vasyl Saienkoe32453e2020-09-16 09:30:34 +0300313function pull_image {
314 local image="$1"
315 function pull_image_retry {
Vasyl Saienko92133c02024-05-16 10:18:51 +0300316 if [[ $KUBERNETES_CONTAINER_RUNTIME == "crio" ]]; then
317 nerdctl --address unix:///run/containerd/containerd.sock -n k8s.io pull $image
318 fi
Vasyl Saienkoe32453e2020-09-16 09:30:34 +0300319 docker pull ${image}
320 }
321 retry 10 "Can't pool docker image $image" pull_image_retry
322}
323
Vasyl Saienko84ee4fd2020-09-02 10:00:18 +0300324function cache_images {
325
Vasyl Saienkoe32453e2020-09-16 09:30:34 +0300326 # Cache UCP image with retries
327 pull_image "${DOCKER_UCP_IMAGE}"
Vasyl Saienko84ee4fd2020-09-02 10:00:18 +0300328
Vasyl Saienkoe32453e2020-09-16 09:30:34 +0300329 local image_prefix=${DOCKER_UCP_IMAGE%/*}
Oleksandr Kononenkobf4ecf82024-02-01 11:02:53 +0200330 for image in $(docker container run --rm --name ucp -v /var/run/docker.sock:/var/run/docker.sock ${DOCKER_UCP_IMAGE} images --list); do
331 pull_image "${image_prefix}/${image##*/}"
Vasyl Saienko92133c02024-05-16 10:18:51 +0300332 if [[ $KUBERNETES_CONTAINER_RUNTIME == "crio" ]]; then
333 nerdctl --address unix:///run/containerd/containerd.sock -n k8s.io tag ${image_prefix}/${image##*/} mirantis/${image##*/}
334 fi
Oleksandr Kononenkobf4ecf82024-02-01 11:02:53 +0200335 docker tag ${image_prefix}/${image##*/} mirantis/${image##*/}
336 done
Vasyl Saienko84ee4fd2020-09-02 10:00:18 +0300337}
338
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100339function install_ucp {
Deniscaa72dd2021-06-04 17:13:05 +0300340 if docker ps --all | grep ucp-controller; then
341 echo "Docker UCP container is running"
342 else
343 local tmpd
344 tmpd=$(mktemp -d)
345 function docker_run_retry {
346 docker container run --rm --name ucp \
347 -v /var/run/docker.sock:/var/run/docker.sock \
348 ${DOCKER_UCP_IMAGE} install \
349 --host-address $UCP_IP_ADDRESS \
350 --admin-username $UCP_USERNAME \
351 --admin-password $UCP_PASSWORD \
352 --existing-config
353 }
354 retry 10 "Can't bring up docker UCP container" docker_run_retry
355 fi
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100356}
357
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300358function download_k0s {
Vasyl Saienko3e8ba732024-09-09 14:07:49 +0300359 curl -sSLf https://get.k0s.sh -o /tmp/get.k0s.sh
360 sh /tmp/get.k0s.sh
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300361}
362
363function install_k0s {
Vasyl Saienko59e5b2a2024-09-09 08:57:18 +0300364 local install_flags="--enable-worker"
365 if [[ "${SINGLE_NODE}" == true ]]; then
366 install_flags="$install_flags --single"
367 fi
Vasyl Saienkoe3107252023-06-06 07:02:58 +0000368 mkdir -p /etc/k0s
369 k0s config create > /etc/k0s/k0s.yaml
Vasyl Saienko59e5b2a2024-09-09 08:57:18 +0300370 k0s install controller -c /etc/k0s/k0s.yaml $install_flags
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300371 k0s start
372
Vasyl Saienkoe3107252023-06-06 07:02:58 +0000373 sleep 60
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300374
Vasyl Saienkoe2c68dd2022-01-27 17:10:12 +0200375 mkdir -p /etc/kubernetes /root/.kube/
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300376 k0s kubeconfig admin > /etc/kubernetes/admin.conf
377 cp /etc/kubernetes/admin.conf /root/.kube/config
378
379 mkdir -p /etc/k0s
380 k0s kubeconfig admin > /etc/k0s/admin.conf
Vasyl Saienko59e5b2a2024-09-09 08:57:18 +0300381 if [[ "${SINGLE_NODE}" != true ]]; then
382 k0s token create --role=worker > /etc/k0s/worker_token.yaml
383 k0s token create --role=controller > /etc/k0s/controller_token.yaml
384 fi
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300385
Vasyl Saienko300bb0b2023-04-19 06:52:53 +0000386 install_pkgs nginx
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300387 rm -f /etc/nginx/sites-enabled/default
388 ln -s /etc/k0s/ /var/www/k0s
389 cat << EOF > /etc/nginx/sites-enabled/k0s
390server {
391 listen *:80;
392 root /var/www;
393
394 location /k0s {
395 autoindex on;
396 }
397}
398EOF
399 systemctl restart nginx
400}
401
Oleh Hryhorovcd1a3bd2020-12-04 13:21:06 +0200402function get_authtoken_retry {
403# Download the bundle https://docs.docker.com/ee/ucp/user-access/cli/
404# Create an environment variable with the user security token
405 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)
406 if [ -z ${AUTHTOKEN} ]; then
407 return -1
408 fi
409}
410
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100411function download_bundles {
412 local tmpd
413 tmpd=$(mktemp -d)
Oleh Hryhorova0754872020-01-24 16:45:05 +0200414
415 function download_bundles_retry {
416 # Download the client certificate bundle
Vasyl Saienkof3dba8c2020-02-14 10:07:23 +0200417 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 +0200418 }
Oleh Hryhorovb58460e2020-02-05 15:53:48 +0200419
Vasyl Saienko9b688992020-02-13 13:50:53 +0200420 retry 10 "Can't get AUTHTOKEN from master." get_authtoken_retry
421 retry 10 "Can't download bundle file from master." download_bundles_retry
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100422
423 pushd $tmpd
424 # Unzip the bundle.
425 unzip bundle.zip
426
427 # Run the utility script.
428 eval "$(<env.sh)"
Vasyl Saienkof9f668c2020-01-04 11:36:35 +0200429 mkdir -p /etc/kubernetes /root/.kube/
Vasyl Saienko5b379422020-01-04 12:24:08 +0200430 cp kube.yml /etc/kubernetes/admin.conf
431 cp kube.yml /root/.kube/config
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100432 popd
433}
434
Vasyl Saienko3ab29c92020-01-03 10:39:03 +0200435function wait_for_node {
436 function retry_wait {
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300437 ${_KUBECTL_CMD} get nodes |grep -w Ready |awk '{print $1}' |grep -q $(hostname)
Vasyl Saienko3ab29c92020-01-03 10:39:03 +0200438 }
439 retry $NODE_DEPLOYMENT_RETRIES "The node didn't come up." retry_wait
440}
441
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300442function download_k8s_metadata {
443 mkdir -p /etc/k0s/
444 mkdir -p /etc/kubernetes
445 for f in worker_token.yaml controller_token.yaml admin.conf; do
446 curl --retry 6 --retry-delay 5 -L http://${UCP_MASTER_HOST}/k0s/${f} -o /etc/k0s/${f}
447 done
448 cp /etc/k0s/admin.conf /etc/kubernetes/admin.conf
449 mkdir -p /root/.kube/
450 cp /etc/k0s/admin.conf /root/.kube/config
451}
452
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100453function join_node {
Deniscaa72dd2021-06-04 17:13:05 +0300454 if kubectl --kubeconfig /etc/kubernetes/admin.conf get nodes |grep -w Ready |awk '{print $1}' |grep -q $(hostname); then
455 echo "This node is already join"
456 else
457 local type=${1}
458 function retry_join_node {
459 env -i $(docker swarm join-token $type |grep 'docker swarm join' | xargs)
Mykyta Karpine2d2fda2023-11-23 10:41:51 +0200460 sleep 20 && systemctl restart docker && sleep 20
Deniscaa72dd2021-06-04 17:13:05 +0300461 }
462 retry 10 "Failed to join node to swarm" retry_join_node
463 fi
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100464}
465
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300466function join_k0s_node {
467 k0s install worker --token-file /etc/k0s/worker_token.yaml
468 k0s start
469}
470
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100471function create_ucp_config {
Oleh Hryhorovcd1a3bd2020-12-04 13:21:06 +0200472 if [[ "${SINGLE_NODE}" == true ]]; then
473 max_pods="kubelet_max_pods = 220"
474 fi
Michael Polenchuk06bfa152022-02-15 17:50:04 +0400475 if [[ "${SECURE_OVERLAY_ENABLED,,}" == true ]]; then
476 secure_overlay="secure_overlay = true
477 calico_vxlan = false"
478 fi
Vasyl Saienko92133c02024-05-16 10:18:51 +0300479 kubelet_custom_flags=""
480 if [[ $KUBERNETES_CONTAINER_RUNTIME == "crio" ]]; then
Vasyl Saienko05edc402024-06-11 11:17:21 +0300481 kubelet_custom_flags='custom_kubelet_flags=["--cgroup-driver=systemd", "--container-runtime-endpoint=unix:///run/containerd/containerd.sock"]'
Vasyl Saienko92133c02024-05-16 10:18:51 +0300482 fi
Deniscaa72dd2021-06-04 17:13:05 +0300483 if docker config ls | grep com.docker.ucp.config ; then
484 echo "Config com.docker.ucp.config already exists"
485 else
486 echo "
Vasyl Saienko2fcc70b2020-01-02 10:09:08 +0200487[scheduling_configuration]
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100488 enable_admin_ucp_scheduling = true
Vasyl Saienko2fcc70b2020-01-02 10:09:08 +0200489 default_node_orchestrator = \"kubernetes\"
Vasyl Saienkoccd3a752020-01-02 10:21:54 +0200490[cluster_config]
491 dns = [\"172.18.208.44\"]
Vasyl Saienko06c7afe2022-07-01 17:53:56 +0000492 calico_vxlan_port = \"$UCP_DOCKER_CALICO_VXLAN_PORT\"
Oleh Hryhorovcd1a3bd2020-12-04 13:21:06 +0200493 ${max_pods}
Michael Polenchuk06bfa152022-02-15 17:50:04 +0400494 ${secure_overlay}
Vasyl Saienko92133c02024-05-16 10:18:51 +0300495 ${kubelet_custom_flags}
Mykyta Karpin9df830a2024-09-12 14:25:01 +0200496[audit_log_configuration]
497 level = \"${UCP_AUDIT_LOG_LEVEL}\"
498 support_dump_include_audit_logs = false
Vasyl Saienkoccd3a752020-01-02 10:21:54 +0200499" | docker config create com.docker.ucp.config -
Deniscaa72dd2021-06-04 17:13:05 +0300500 fi
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100501}
502
503function swarm_init {
Deniscaa72dd2021-06-04 17:13:05 +0300504 if docker node ls | grep $HOSTNAME; then
505 echo "This node is already part of a swarm"
506 else
507 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 +0000508 sleep 5 && systemctl restart docker && sleep 5
Deniscaa72dd2021-06-04 17:13:05 +0300509 fi
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100510}
511
512function rm_ucp_config {
513 docker config rm com.docker.ucp.config
514}
515
Vasyl Saienkob58c7a52020-01-02 09:14:40 +0200516function install_kubectl {
Michael Polenchuk92ca1b32020-11-13 15:58:38 +0400517 local kubectl=/usr/local/bin/kubectl
Vasyl Saienkoa94bf182023-04-01 10:42:17 +0000518 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 +0400519 chmod +x ${kubectl}
Ilya Menkov2d22bd02020-02-14 15:01:29 +0400520cat << EOF >> ~/.bashrc
521source /usr/share/bash-completion/bash_completion
522source <(kubectl completion bash)
523EOF
Vasyl Saienkob58c7a52020-01-02 09:14:40 +0200524}
525
Vasyl Saienko08166342020-02-28 12:20:51 +0200526function configure_ntp {
Vasyl Saienkoe2c68dd2022-01-27 17:10:12 +0200527 echo "" > /etc/ntp.conf
Vasyl Saienko08166342020-02-28 12:20:51 +0200528
Anton Samoylov6d2cf7b2020-09-01 11:35:11 +0400529 for server in $NTP_SERVERS; do
530cat << EOF >> /etc/ntp.conf
531server ${server} iburst
532EOF
533 done
534
535 cat << EOF >> /etc/ntp.conf
536# Set general access to this service
537restrict -4 default kod nomodify notrap nopeer noquery
538restrict -6 default kod nomodify notrap nopeer noquery
539
540# Allow access from localhost
541restrict 127.0.0.1
542restrict ::1
543
544# Do not listen on any interface address by default
545interface ignore wildcard
546interface listen lo
547interface listen ${DEFAULT_INTERFACE}
548
549# Location of drift file
550driftfile /var/lib/ntp/ntp.drift
551EOF
552
ibumarskov35370042022-04-19 14:27:02 +0400553 systemctl disable systemd-timesyncd
554 systemctl enable ntp
Anton Samoylov6d2cf7b2020-09-01 11:35:11 +0400555 systemctl restart ntp
Vasyl Saienko08166342020-02-28 12:20:51 +0200556}
557
Vasyl Saienko5af97c32022-01-25 09:45:45 +0200558function wait_for_external_network {
559 function _check_access {
560 curl --connect-timeout 10 ${DOCKER_UCP_IMAGE%%/*} || (sleep 1; /bin/false)
561 }
562 retry 180 "Failed to wait for external networks reachable." _check_access
563}
564
Vasyl Saienko2fcc70b2020-01-02 10:09:08 +0200565function prepare_network {
Dennis Dmitrievea894212020-03-12 21:36:43 +0200566 if [ -z "${CONTROL_IP_ADDRESS}" ]; then
Vasyl Saienko7c595922021-03-17 13:58:13 +0200567 wait_condition_send "FAILURE" "CONTROL_IP_ADDRESS is not found."
Dennis Dmitrievea894212020-03-12 21:36:43 +0200568 exit 1
569 fi
570
Vasyl Saienko2fcc70b2020-01-02 10:09:08 +0200571 systemctl restart systemd-resolved
Vasyl Saienkoc14c43d2020-01-08 13:06:18 +0200572 # Make sure local hostname is present in /etc/hosts
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300573 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 +0200574}
575
Vasyl Saienko7d7acea2020-01-03 16:59:05 +0200576function workaround_default_forward_policy {
Vasyl Saienkod67e35e2023-09-27 14:44:01 +0000577 local should_run=0
578 for net in $FLOATING_NETWORK_PREFIXES; do
579 if ! iptables -nvL DOCKER-USER |grep $net; then
580 should_run=1
581 break
582 fi
583 done
584 if [[ $should_run == 0 ]]; then
585 echo "Iptables is already configured"
Deniscaa72dd2021-06-04 17:13:05 +0300586 else
587 cat << EOF > /etc/iptables/rules.v4
Vasyl Saienkobd34d2a2020-03-10 16:25:17 +0200588*filter
589:DOCKER-USER - [0:0]
590EOF
Deniscaa72dd2021-06-04 17:13:05 +0300591 for net in $FLOATING_NETWORK_PREFIXES; do
Vasyl Saienkobd34d2a2020-03-10 16:25:17 +0200592cat << EOF >> /etc/iptables/rules.v4
593-A DOCKER-USER -d ${net} -j ACCEPT
594-A DOCKER-USER -s ${net} -j ACCEPT
Vasyl Saienkobd34d2a2020-03-10 16:25:17 +0200595EOF
Deniscaa72dd2021-06-04 17:13:05 +0300596 done
Vasyl Saienkobd34d2a2020-03-10 16:25:17 +0200597
598cat << EOF >> /etc/iptables/rules.v4
599-A DOCKER-USER -j RETURN
Vasyl Saienko1785c352020-03-11 11:39:40 +0200600COMMIT
Vasyl Saienkobd34d2a2020-03-10 16:25:17 +0200601EOF
Vasyl Saienko02df39a2020-05-20 16:39:35 +0300602
603#Allow access to Internet from VMs for virtualized environment.
604cat << EOF >> /etc/iptables/rules.v4
605*nat
606:POSTROUTING ACCEPT - [0:0]
607EOF
Deniscaa72dd2021-06-04 17:13:05 +0300608 for net in $FLOATING_NETWORK_PREFIXES; do
Vasyl Saienko02df39a2020-05-20 16:39:35 +0300609cat << EOF >> /etc/iptables/rules.v4
610-A POSTROUTING -s ${net} -o ${DEFAULT_INTERFACE} -j MASQUERADE
611EOF
Deniscaa72dd2021-06-04 17:13:05 +0300612 done
Vasyl Saienko02df39a2020-05-20 16:39:35 +0300613
614cat << EOF >> /etc/iptables/rules.v4
615COMMIT
616EOF
Deniscaa72dd2021-06-04 17:13:05 +0300617 sudo netfilter-persistent reload
618 fi
Vasyl Saienko7d7acea2020-01-03 16:59:05 +0200619}
620
Dennis Dmitrievea894212020-03-12 21:36:43 +0200621function disable_rp_filter {
622 # Run this func before "network_config" to create new interfaces with the default rp_filter value
623 cat << EOF > /etc/sysctl.d/99-disable-rp-filter.conf
624net.ipv4.conf.all.rp_filter=0
625net.ipv4.conf.default.rp_filter=0
626EOF
Vasyl Saienkoe61a98c2021-10-19 21:42:19 +0300627 for iface in $(find /proc/sys/net/ipv4/conf/ -name "enp*" -o -name "ens*" -o -name "eth*"); do
628 echo 0 > $iface/rp_filter
Oleh Hryhorov207137d2021-04-13 12:03:39 +0300629 done
630
Dennis Dmitrievea894212020-03-12 21:36:43 +0200631 sysctl -p /etc/sysctl.d/99-disable-rp-filter.conf
632}
633
Vasyl Saienko45280eb2024-06-24 11:45:13 +0000634function configure_sysctl_limits {
635 cat << EOF > /etc/sysctl.d/99-limits.conf
636fs.inotify.max_user_instances = 8192
637EOF
638 sysctl -p /etc/sysctl.d/99-limits.conf
639}
640
Vasyl Saienko83f309e2020-05-06 11:14:28 +0300641function configure_contrack {
642 cat << EOF > /etc/sysctl.d/100-contrackd.conf
643net.netfilter.nf_conntrack_log_invalid=255
644net.netfilter.nf_conntrack_tcp_be_liberal=1
645EOF
646
647 sysctl -p /etc/sysctl.d/100-contrackd.conf
648}
649
Vasyl Saienkob377de02020-05-25 14:51:31 +0300650# NOTE(vsaienko): disable calling iptables when sending packets via bridge.
651function disable_iptables_for_bridges {
652 cat << EOF > /etc/sysctl.d/101-bridge-nf-call-iptables.conf
653net.bridge.bridge-nf-call-ip6tables=0
654net.bridge.bridge-nf-call-iptables=0
655net.bridge.bridge-nf-call-arptables=0
656EOF
657
658 sysctl -p /etc/sysctl.d/101-bridge-nf-call-iptables.conf
659}
660
Oleh Hryhorovb58460e2020-02-05 15:53:48 +0200661function network_config {
Vasyl Saienko9b688992020-02-13 13:50:53 +0200662 PUBLIC_NODE_IP_ADDRESS=${PUBLIC_INTERFACE_IP:-$(ip addr show dev ${PUBLIC_INTERFACE} | grep -Po 'inet \K[\d.]+' | egrep -v "127.0.|172.17")}
663 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)}
664
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200665 local public_interface=${1:-${PUBLIC_INTERFACE}}
666 local cloud_netplan_cfg="/etc/netplan/50-cloud-init.yaml"
667 local match_ip_line
Vasyl Saienko9b688992020-02-13 13:50:53 +0200668
Vasyl Saienko300bb0b2023-04-19 06:52:53 +0000669 install_pkgs bridge-utils
Mykyta Karpin5ed5fd82020-01-27 18:23:29 +0200670
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200671cat << EOF > /etc/systemd/network/10-veth-phy-br.netdev
672[NetDev]
673Name=veth-phy
674Kind=veth
675[Peer]
676Name=veth-br
677EOF
Vasyl Saienko9b688992020-02-13 13:50:53 +0200678
Vasyl Saienko66522982020-05-25 09:34:43 +0300679if [[ -n ${IRONIC_BAREMETAL_INTERFACE} ]]; then
680
Vasyl Saienko4c468192020-05-19 11:51:13 +0300681cat << EOF > /etc/systemd/network/11-veth-bm.netdev
682[NetDev]
683Name=veth-bm
684Kind=veth
685[Peer]
686Name=veth-bm-br
687EOF
688
Vasyl Saienko1233afd2020-05-21 14:55:21 +0300689cat << EOF > /etc/systemd/network/12-veth-vbmc.netdev
690[NetDev]
691Name=veth-vbmc
692Kind=veth
693[Peer]
694Name=veth-vbmc-br
695EOF
Vasyl Saienko66522982020-05-25 09:34:43 +0300696fi
Vasyl Saienko1233afd2020-05-21 14:55:21 +0300697
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200698 sed -i 's/.*ethernets:.*/&\n veth-phy: {}/' ${cloud_netplan_cfg}
699 sed -i 's/.*ethernets:.*/&\n veth-br: {}/' ${cloud_netplan_cfg}
Vasyl Saienko66522982020-05-25 09:34:43 +0300700if [[ -n ${IRONIC_BAREMETAL_INTERFACE} ]]; then
Vasyl Saienko4c468192020-05-19 11:51:13 +0300701 sed -i 's/.*ethernets:.*/&\n veth-bm: {}/' ${cloud_netplan_cfg}
702 sed -i 's/.*ethernets:.*/&\n veth-bm-br: {}/' ${cloud_netplan_cfg}
Vasyl Saienko1233afd2020-05-21 14:55:21 +0300703 sed -i 's/.*ethernets:.*/&\n veth-vbmc: {}/' ${cloud_netplan_cfg}
704 sed -i 's/.*ethernets:.*/&\n veth-vbmc-br: {}/' ${cloud_netplan_cfg}
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200705
Michael Polenchuk486cadf2020-08-24 16:25:51 +0400706 if [[ "${IRONIC_MT_ENABLED,,}" == true ]] ; then
707 sed -i "s/.*ethernets:.*/&\n ${IRONIC_BAREMETAL_VXLAN_INTERFACE}: {}/" ${cloud_netplan_cfg}
708 sed -i 's/.*ethernets:.*/&\n veth-pxe: {}/' ${cloud_netplan_cfg}
709 sed -i 's/.*ethernets:.*/&\n veth-pxe-br: {}/' ${cloud_netplan_cfg}
710cat << EOF > /etc/systemd/network/13-veth-pxe.netdev
711[NetDev]
712Name=veth-pxe
713Kind=veth
714[Peer]
715Name=veth-pxe-br
716EOF
717 sed -i "s/- ${IRONIC_BAREMETAL_NETWORK_PREFIX}\([0-9]*\)/- ${IRONIC_BAREMETAL_TUNNEL_NETWORK_PREFIX}\1/" ${cloud_netplan_cfg}
718cat << EOF >> ${cloud_netplan_cfg}
719 vlans:
720 pxe.1000:
721 id: 1000
722 link: veth-pxe
723 addresses:
724 - ${IRONIC_BAREMETAL_INTERFACE_IP}/${IRONIC_BAREMETAL_NETWORK_NETMASK}
725EOF
726 else
727 ironic_baremetal_address_match_ip_line=$(grep -nm1 "${IRONIC_BAREMETAL_INTERFACE_IP}/${IRONIC_BAREMETAL_NETWORK_NETMASK}" ${cloud_netplan_cfg} | cut -d: -f1)
728 sed -i "$((${ironic_baremetal_address_match_ip_line}-1)),$((${ironic_baremetal_address_match_ip_line}))d" ${cloud_netplan_cfg}
729 fi
Vasyl Saienko66522982020-05-25 09:34:43 +0300730fi
Vasyl Saienko4c468192020-05-19 11:51:13 +0300731
Vasyl Saienko66522982020-05-25 09:34:43 +0300732 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 +0300733 if [ -n "${public_address_match_ip_line}" ] ; then
734 sed -i "$((${public_address_match_ip_line}-1)),$((${public_address_match_ip_line}))d" ${cloud_netplan_cfg}
735 fi
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200736
737cat << EOF >> ${cloud_netplan_cfg}
738 bridges:
739 br-public:
740 dhcp4: false
741 interfaces:
742 - ${PUBLIC_INTERFACE}
743 - veth-br
744 addresses:
745 - ${PUBLIC_NODE_IP_ADDRESS}/${PUBLIC_NODE_IP_NETMASK}
Vasyl Saienko66522982020-05-25 09:34:43 +0300746EOF
Vasyl Saienko5b6d70a2023-04-10 13:55:31 +0000747# Assign more ips for neutron dynamic routing PRODX-31417
748if [[ ${NODE_METADATA} == *"tempest"* ]]; then
749 for i in {71..76}; do
750cat << EOF >> ${cloud_netplan_cfg}
Vasyl Saienkoc7639002023-04-11 07:40:29 +0000751 - ${PUBLIC_NODE_IP_ADDRESS%.*}.${i}/${PUBLIC_NODE_IP_NETMASK}
Vasyl Saienko5b6d70a2023-04-10 13:55:31 +0000752EOF
753 done
754fi
Vasyl Saienko66522982020-05-25 09:34:43 +0300755
Vasyl Saienkof79d0942020-11-17 14:33:55 +0200756# Remove Tunnel interface from netplan
757if [[ $TUNNEL_INTERFACE_NETPLAN_MANAGE == false ]]; then
758 sed -i "/ ${TUNNEL_INTERFACE}/,/ set-name: ${TUNNEL_INTERFACE}/d" ${cloud_netplan_cfg}
759fi
760
Vasyl Saienko66522982020-05-25 09:34:43 +0300761if [[ -n ${IRONIC_BAREMETAL_INTERFACE} ]]; then
762cat << EOF >> ${cloud_netplan_cfg}
Vasyl Saienko4c468192020-05-19 11:51:13 +0300763 br-baremetal:
764 dhcp4: false
765 interfaces:
Vasyl Saienko4c468192020-05-19 11:51:13 +0300766 - veth-bm-br
Vasyl Saienko1233afd2020-05-21 14:55:21 +0300767 - veth-vbmc-br
Michael Polenchuk486cadf2020-08-24 16:25:51 +0400768EOF
769 if [[ "${IRONIC_MT_ENABLED,,}" != true ]] ; then
770cat << EOF >> ${cloud_netplan_cfg}
771 - ${IRONIC_BAREMETAL_INTERFACE}
Vasyl Saienko4c468192020-05-19 11:51:13 +0300772 addresses:
773 - ${IRONIC_BAREMETAL_INTERFACE_IP}/${IRONIC_BAREMETAL_NETWORK_NETMASK}
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200774EOF
Michael Polenchuk486cadf2020-08-24 16:25:51 +0400775 else
776cat << EOF >> ${cloud_netplan_cfg}
777 - ${IRONIC_BAREMETAL_VXLAN_INTERFACE}
778 - veth-pxe-br
779EOF
Vasyl Saienko3dd0c272020-11-16 17:20:07 +0200780
Michael Polenchuk486cadf2020-08-24 16:25:51 +0400781cat << EOF > /etc/systemd/system/ironic-vxlan-tunnel.service
782[Unit]
783Description=Ironic VXLAN tunnel
784After=network.target
785
786[Service]
787Type=oneshot
788RemainAfterExit=true
789ExecStart=/sbin/ip link add ${IRONIC_BAREMETAL_VXLAN_INTERFACE} type vxlan id 10 group 239.1.1.10 dstport 0 dev ${IRONIC_BAREMETAL_INTERFACE}
790ExecStart=/sbin/ip link set ${IRONIC_BAREMETAL_VXLAN_INTERFACE} up
791
792[Install]
793WantedBy=multi-user.target
794EOF
795
796 systemctl enable ironic-vxlan-tunnel.service
797 systemctl start ironic-vxlan-tunnel.service
798 fi
Vasyl Saienko66522982020-05-25 09:34:43 +0300799fi
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200800 netplan --debug apply
Vasyl Saienko9b688992020-02-13 13:50:53 +0200801
802 # NOTE(vsaienko): give some time to apply changes
803 sleep 15
Vasyl Saienko16c81c02020-11-17 17:10:55 +0200804
805# Remove Tunnel interface from netplan
806if [[ $TUNNEL_INTERFACE_NETPLAN_MANAGE == false ]]; then
807 ip addr flush ${TUNNEL_INTERFACE}
808 ip link set ${TUNNEL_INTERFACE} up
809fi
810
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200811}
812
Oleh Hryhorovb58460e2020-02-05 15:53:48 +0200813$functions_override
814
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200815function set_node_labels {
Vasyl Saienko2ae2eec2020-02-20 10:11:32 +0200816 function set_node_labels_retry {
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300817 ${_KUBECTL_CMD} patch node $(hostname) -p "{\"metadata\": $(echo $NODE_METADATA | jq -c ".")}"
Vasyl Saienko2ae2eec2020-02-20 10:11:32 +0200818 }
Vasyl Saienko08024582020-02-20 11:45:34 +0200819 retry 10 "Labeling node failed" set_node_labels_retry
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200820}
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100821
Vasyl Saienko1eb06d62020-11-16 10:16:22 +0200822HW_METADATA='{}'
Vasyl Saienkof9ee1582020-03-02 16:53:41 +0200823function prepare_metadata_files {
Dennis Dmitrievea894212020-03-12 21:36:43 +0200824 /usr/sbin/prepare-metadata.py --metadata-file /usr/share/metadata/lab-metadata.yaml
825}
826
827function collect_ceph_metadata {
Oleh Hryhorovf14fc922020-03-10 12:11:10 +0200828 local ceph_osd_node
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300829 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 +0200830
Oleh Hryhorovf14fc922020-03-10 12:11:10 +0200831 if [[ -f /usr/share/metadata/ceph.yaml && ${ceph_osd_node} ]]; then
Vasyl Saienkof9ee1582020-03-02 16:53:41 +0200832 HW_METADATA="{\"ceph\": {\"$(hostname)\": \"$(base64 -w 0 /usr/share/metadata/ceph.yaml)\"}}"
Oleh Hryhorov39fbfe52020-03-12 15:26:53 +0200833 ceph_store_drive=$(cat /usr/share/metadata/ceph.yaml | egrep '\- name\: vd?' | awk '{print $3}')
834 if [[ -b /dev/${ceph_store_drive} ]]; then
835 sgdisk --zap-all /dev/${ceph_store_drive}
836 fi
Vasyl Saienkof9ee1582020-03-02 16:53:41 +0200837 fi
838}
839
Oleh Hryhorove7feab42020-11-11 18:00:29 +0200840function collect_interfaces_metadata {
Vasyl Saienko3dd0c272020-11-16 17:20:07 +0200841
Oleh Hryhorove7feab42020-11-11 18:00:29 +0200842 local if_metadata_file="/usr/share/metadata/interfaces.yaml"
Denisd2635bf2021-06-15 13:35:36 +0300843 if [ -z "${TUNNEL_INTERFACE}" ] ; then
844 echo "Tunnel interface is empty"
845 else
846 pci_id=$(ethtool -i ${TUNNEL_INTERFACE} |grep bus-info | awk '{print $2}')
847 mac=$(cat $(find /sys/bus/pci/devices/${pci_id}/ -name net)/${TUNNEL_INTERFACE}/address)
848 ip=${TUNNEL_INTERFACE_IP}/${TUNNEL_INTERFACE_NETWORK_NETMASK}
Oleh Hryhorove7feab42020-11-11 18:00:29 +0200849
Denisd2635bf2021-06-15 13:35:36 +0300850 cat << EOF > ${if_metadata_file}
Vasyl Saienko592730c2020-11-17 11:36:46 +0200851${TUNNEL_INTERFACE}:
Vasyl Saienko1152e982020-11-16 16:23:32 +0200852 pci_id: "${pci_id}"
853 ip_addr: "${ip}"
854 mac: "${mac}"
Oleh Hryhorove7feab42020-11-11 18:00:29 +0200855EOF
856
Denisd2635bf2021-06-15 13:35:36 +0300857 fi
Oleh Hryhorove7feab42020-11-11 18:00:29 +0200858 HW_METADATA=$(echo ${HW_METADATA} | jq -cr ". += {\"interfaces\": {\"$(hostname)\": \"$(base64 -w 0 ${if_metadata_file})\"}}")
859
860}
861
Oleh Hryhorovab3793f2020-06-04 18:37:22 +0300862function nested_virt_config {
863
Vasyl Saienkoe61a98c2021-10-19 21:42:19 +0300864if grep --color vmx /proc/cpuinfo; then
865 cat << EOF > /etc/modprobe.d/qemu-system-x86.conf
Oleh Hryhorovab3793f2020-06-04 18:37:22 +0300866options kvm_intel nested=1
867options kvm_intel enable_apicv=1
868options kvm_intel ept=1
869EOF
870
871 modprobe -r kvm_intel || true
872 modprobe kvm_intel nested=1
Vasyl Saienkoe61a98c2021-10-19 21:42:19 +0300873else
874 echo "Nested virtualization is not supported"
875fi
Oleh Hryhorovab3793f2020-06-04 18:37:22 +0300876
877}
Vasyl Saienkof9ee1582020-03-02 16:53:41 +0200878
Mykyta Karpin6a153c52020-11-03 15:21:38 +0200879function configure_huge_pages {
880 local nr_2Mb
881 local nr_1G
882 local apply_grub=false
883
884 if [[ ! $(cat /etc/default/grub | grep -E ^GRUB_CMDLINE_LINUX=".*hugepagesz=2M.*") ]] && [[ "${HUGE_PAGES_2Mb_SIZE}" -gt 0 ]]; then
885 nr_2Mb=$(( "${HUGE_PAGES_2Mb_SIZE}" / 2 ))
886 echo "Configure GRUB kernel cmd to allocate ${nr_2Mb} 2Mb huge pages"
887 echo "GRUB_CMDLINE_LINUX=\"\$GRUB_CMDLINE_LINUX hugepagesz=2M hugepages=$nr_2Mb\"" >> /etc/default/grub
888 apply_grub=true
889 fi
890
891 if [[ ! $(cat /etc/default/grub | grep -E ^GRUB_CMDLINE_LINUX=".*hugepagesz=1G.*") ]] && [[ "${HUGE_PAGES_1G_SIZE}" -gt 0 ]]; then
892 echo "Configure GRUB kernel cmd to allocate ${HUGE_PAGES_1G_SIZE} 1G huge pages"
893 echo "GRUB_CMDLINE_LINUX=\"\$GRUB_CMDLINE_LINUX hugepagesz=1G hugepages=${HUGE_PAGES_1G_SIZE}\"" >> /etc/default/grub
894 apply_grub=true
895 fi
896
897 if [[ "${apply_grub}" == true ]]; then
898 update-grub
899 cloud-init clean -r
900 else
901 echo "GRUB already configured with huge pages"
902 fi
903}
904
Oleh Hryhorov43bbc8c2020-12-03 15:30:09 +0200905function disable_master_taint {
906
Oleh Hryhorovcd1a3bd2020-12-04 13:21:06 +0200907 function disable_master_taint_retry {
908 # Enable "Allow all authenticated users, including service accounts to schedule on all nodes, including UCP managers."
909 curl --retry 6 --retry-delay 5 -k -H "Authorization: Bearer $AUTHTOKEN" -k -X PUT https://${UCP_MASTER_HOST}/collectionGrants/authenticated/swarm/scheduler
910 }
911
912 retry 10 "Can't enable permission to schedule all pods on UCP master." disable_master_taint_retry
Oleh Hryhorov43bbc8c2020-12-03 15:30:09 +0200913
914}
915
Vasyl Saienko94843cb2021-02-26 16:43:34 +0200916function disable_ipv6 {
917 cat << EOF > /etc/sysctl.d/11-ipv6-disable.conf
918net.ipv6.conf.all.disable_ipv6 = 1
919net.ipv6.conf.default.disable_ipv6 = 1
920net.ipv6.conf.lo.disable_ipv6 = 1
921EOF
922 sysctl -p /etc/sysctl.d/11-ipv6-disable.conf
923}
924
Vasyl Saienko846c6342021-02-24 17:01:36 +0200925function setup_evpn {
926
927cat << EOF > /etc/netplan/60-evpn.yaml
928network:
929 version: 2
930 ethernets:
Vasyl Saienko5f953782021-02-25 10:30:32 +0200931EOF
932for vni in $(seq ${FRR_EVPN_TUNNELS_RANGE}); do
933cat << EOF >> /etc/netplan/60-evpn.yaml
934 vxlan$vni: {}
935EOF
936done
937cat << EOF >> /etc/netplan/60-evpn.yaml
Vasyl Saienko846c6342021-02-24 17:01:36 +0200938 bridges:
Vasyl Saienko5f953782021-02-25 10:30:32 +0200939EOF
940for vni in $(seq ${FRR_EVPN_TUNNELS_RANGE}); do
941cat << EOF >> /etc/netplan/60-evpn.yaml
942 br$vni:
Vasyl Saienko846c6342021-02-24 17:01:36 +0200943 dhcp4: false
Vasyl Saienkof18b4782021-03-03 14:15:40 +0200944 link-local: []
Vasyl Saienko846c6342021-02-24 17:01:36 +0200945 parameters:
946 stp: false
Vasyl Saienko5f953782021-02-25 10:30:32 +0200947 interfaces:
948 - vxlan$vni
Vasyl Saienko846c6342021-02-24 17:01:36 +0200949 addresses:
Vasyl Saienko5f953782021-02-25 10:30:32 +0200950 - 192.168.$vni.254/24
Vasyl Saienko846c6342021-02-24 17:01:36 +0200951EOF
Vasyl Saienko5f953782021-02-25 10:30:32 +0200952done
Vasyl Saienko846c6342021-02-24 17:01:36 +0200953
Vasyl Saienko5f953782021-02-25 10:30:32 +0200954for vni in $(seq ${FRR_EVPN_TUNNELS_RANGE}); do
955cat << EOF > /etc/systemd/system/evpn-vxlan-tunnel-${vni}.service
Vasyl Saienko846c6342021-02-24 17:01:36 +0200956[Unit]
Vasyl Saienko5f953782021-02-25 10:30:32 +0200957Description=EVPN tunnel ${vni}
Vasyl Saienko846c6342021-02-24 17:01:36 +0200958After=network.target
959
960[Service]
961Type=oneshot
962RemainAfterExit=true
Vasyl Saienkob3b8c4e2021-02-26 10:54:27 +0200963ExecStart=/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 +0200964ExecStart=/sbin/ip link set vxlan${vni} up
Vasyl Saienko846c6342021-02-24 17:01:36 +0200965
966[Install]
967WantedBy=multi-user.target
968EOF
Vasyl Saienko5f953782021-02-25 10:30:32 +0200969 systemctl enable evpn-vxlan-tunnel-${vni}.service
970 systemctl start evpn-vxlan-tunnel-${vni}.service
971done
Vasyl Saienko846c6342021-02-24 17:01:36 +0200972
973 netplan --debug apply
974 # NOTE(vsaienko): give some time to apply changes
975 sleep 15
Vasyl Saienko846c6342021-02-24 17:01:36 +0200976}
977
Vasyl Saienko25bb75b2021-02-15 12:22:33 +0200978function install_frr {
979 function _install_frr_packages {
Vasyl Saienko25bb75b2021-02-15 12:22:33 +0200980 FRRVER="frr-stable"
Oleh Hryhorov6243c9f2023-09-21 09:53:09 +0000981 curl -s https://mirror.mirantis.com/.snapshots/frr-stable-latest/keys.asc | sudo apt-key add -
Oleh Hryhorov2ae1db82023-09-21 07:39:50 +0000982 add-apt-repository "deb [arch=amd64] https://mirror.mirantis.com/.snapshots/frr-stable-latest/ ${OS_CODENAME} ${FRRVER}"
Vasyl Saienko300bb0b2023-04-19 06:52:53 +0000983 install_pkgs frr frr-pythontools
Vasyl Saienko25bb75b2021-02-15 12:22:33 +0200984 }
985 retry 3 "Can't install frr packages" _install_frr_packages
986
987 sed -i 's/^bgpd=no/bgpd=yes/g' /etc/frr/daemons
988
989 cat << EOF > /etc/frr/frr.conf
990log syslog informational
991no ipv6 forwarding
992service integrated-vtysh-config
993!
994router bgp ${FRR_BGP_AS_NUMBER}
995 bgp router-id ${FRR_ROUTER_ID}
996 no bgp default ipv4-unicast
997 bgp cluster-id ${FRR_ROUTER_ID}
998 coalesce-time 1000
Vasyl Saienko846c6342021-02-24 17:01:36 +0200999 neighbor rr peer-group
Vasyl Saienkob3b8c4e2021-02-26 10:54:27 +02001000 neighbor rr capability extended-nexthop
1001 neighbor rr remote-as ${FRR_BGP_AS_NUMBER}
Vasyl Saienko846c6342021-02-24 17:01:36 +02001002 neighbor rr update-source ${FRR_BGP_SOURCE_ADDRESS}
1003EOF
1004
1005for neighbor_ip in $(echo $FRR_BGP_NEIGHBORS | tr "," "\n"); do
1006 cat << EOF >> /etc/frr/frr.conf
1007 neighbor $neighbor_ip peer-group rr
1008EOF
1009
1010done
1011 cat << EOF >> /etc/frr/frr.conf
Vasyl Saienko25bb75b2021-02-15 12:22:33 +02001012 !
1013 address-family ipv4 unicast
Vasyl Saienko846c6342021-02-24 17:01:36 +02001014 neighbor rr activate
Vasyl Saienko25bb75b2021-02-15 12:22:33 +02001015 exit-address-family
1016 !
1017 address-family ipv4 vpn
Vasyl Saienko846c6342021-02-24 17:01:36 +02001018 neighbor rr activate
Vasyl Saienko25bb75b2021-02-15 12:22:33 +02001019 exit-address-family
1020 !
1021 address-family l2vpn evpn
Vasyl Saienko846c6342021-02-24 17:01:36 +02001022 neighbor rr activate
Vasyl Saienko25bb75b2021-02-15 12:22:33 +02001023 advertise-all-vni
1024 advertise-default-gw
1025 exit-address-family
1026!
1027line vty
1028!
1029EOF
1030
1031 systemctl restart frr
Vasyl Saienko846c6342021-02-24 17:01:36 +02001032
Vasyl Saienko94843cb2021-02-26 16:43:34 +02001033 disable_ipv6
Vasyl Saienko846c6342021-02-24 17:01:36 +02001034 setup_evpn
Vasyl Saienko25bb75b2021-02-15 12:22:33 +02001035}
1036
Mykyta Karpinbac78b42021-12-07 11:25:03 +02001037function cron_disable_calico_offloading {
Denis Biletskiy49b91ea2022-06-09 11:41:39 +02001038 # The workaround is to disable offloading on vxlan interface
1039 if [ -f /etc/cron.d/disable_calico_offloading ]; then
1040 echo "Cronjob for disable callico offloading already exists"
1041 else
1042 script_path="/usr/sbin/calico_disable_offloading.sh"
1043 cat << 'EOF' >> $script_path
1044#!/usr/bin/env bash
1045if /sbin/ethtool --show-offload vxlan.calico | grep -E '^.x-checksumming:' | grep -q ': on'; then
1046 /sbin/ethtool --offload vxlan.calico rx off tx off > /dev/null
1047fi
Mykyta Karpinbac78b42021-12-07 11:25:03 +02001048EOF
Denis Biletskiy49b91ea2022-06-09 11:41:39 +02001049 chmod +x $script_path
1050 cat << EOF >> /etc/cron.d/disable_calico_offloading
1051* * * * * root $script_path 2>&1 | /usr/bin/logger -t calico_disable_offloading
1052EOF
1053 fi
Mykyta Karpinbac78b42021-12-07 11:25:03 +02001054}
1055
Vasyl Saienko114333f2022-05-24 09:34:51 +00001056function increase_iscsi_timeout {
1057 cat <<EOF > /etc/udev/rules.d/50-iscsi-timeout.rules
1058ACTION=="add", SUBSYSTEM=="scsi" , SYSFS{type}=="0|7|14",
1059RUN+="/bin/sh -c 'echo 120 > /sys/block/%k/device/timeout'"
1060EOF
1061 udevadm control --reload-rules && udevadm trigger
1062}