You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
165 lines
6.1 KiB
Python
165 lines
6.1 KiB
Python
"""Shared shell fragments for remote host reconciliation scripts."""
|
|
|
|
|
|
VPC_SYSCTL_VALIDATION_SH = r'''
|
|
vc_vpc_sysctl_artifact_owned() {
|
|
vc_sysctl_path=$1
|
|
vc_sysctl_vpc=$2
|
|
vc_sysctl_revision=$3
|
|
vc_sysctl_digest=$4
|
|
vc_sysctl_bridge=$5
|
|
case "$vc_sysctl_revision" in ''|*[!0-9]*) return 1 ;; esac
|
|
[ "$vc_sysctl_revision" -ge 1 ] || return 1
|
|
printf '%s\n' "$vc_sysctl_digest" | grep -Eq '^[0-9a-f]{64}$' || return 1
|
|
[ -f "$vc_sysctl_path" ] && [ ! -L "$vc_sysctl_path" ] || return 1
|
|
[ "$($STAT -c '%u:%a' "$vc_sysctl_path" 2>/dev/null)" = 0:600 ] || return 1
|
|
awk -v identity="# vpc=$vc_sysctl_vpc revision=$vc_sysctl_revision digest=$vc_sysctl_digest" \
|
|
-v bridge="$vc_sysctl_bridge" '
|
|
NR==1 && $0=="# virt-cloud-vpc-dataplane-v1" {owner++; next}
|
|
NR==2 && $0==identity {identity_seen++; next}
|
|
NR==3 && $0=="net.ipv4.conf." bridge ".rp_filter = 2" {rp++; next}
|
|
NR==4 && $0=="net.ipv4.conf." bridge ".proxy_arp = 0" {proxy++; next}
|
|
NR==5 && $0=="net.ipv4.conf." bridge ".proxy_arp_pvlan = 1" {pvlan++; next}
|
|
{bad=1}
|
|
END {
|
|
exit (!bad && NR==5 && owner==1 && identity_seen==1 && rp==1 &&
|
|
proxy==1 && pvlan==1) ? 0 : 1
|
|
}
|
|
' "$vc_sysctl_path"
|
|
}
|
|
vc_vpc_sysctl_artifacts_equal() {
|
|
vc_sysctl_live=$1
|
|
vc_sysctl_desired=$2
|
|
vc_sysctl_vpc=$3
|
|
vc_sysctl_revision=$4
|
|
vc_sysctl_digest=$5
|
|
vc_sysctl_bridge=$6
|
|
[ -n "${CMP:-}" ] || return 1
|
|
vc_vpc_sysctl_artifact_owned "$vc_sysctl_live" "$vc_sysctl_vpc" \
|
|
"$vc_sysctl_revision" "$vc_sysctl_digest" "$vc_sysctl_bridge" || return 1
|
|
vc_vpc_sysctl_artifact_owned "$vc_sysctl_desired" "$vc_sysctl_vpc" \
|
|
"$vc_sysctl_revision" "$vc_sysctl_digest" "$vc_sysctl_bridge" || return 1
|
|
"$CMP" -s "$vc_sysctl_live" "$vc_sysctl_desired"
|
|
}
|
|
'''.strip()
|
|
|
|
|
|
# ``ip rule`` has no ``replace`` operation. Policy rules therefore use a
|
|
# two-phase contract: callers preflight every priority before mutating any of
|
|
# them, then add only missing rules and verify the exact readback. Existing
|
|
# matching rules are left untouched, so an idempotent replay has no routing
|
|
# gap. The caller supplies separate validation and command-failure exit codes
|
|
# because VPC apply and boot repair expose different error classes.
|
|
IP_RULE_CONVERGENCE_SH = r'''vc_ip_rule_read() {
|
|
vc_rule_pref=$1
|
|
vc_rule_iif=$2
|
|
vc_rule_to=$3
|
|
vc_rule_table=$4
|
|
vc_rule_invalid=$5
|
|
vc_rule_command_failed=$6
|
|
if ! VC_IP_RULE_EXISTING=$("$IP" -o -4 -N rule show pref "$vc_rule_pref" 2>/dev/null); then
|
|
return "$vc_rule_command_failed"
|
|
fi
|
|
if [ -z "$VC_IP_RULE_EXISTING" ]; then
|
|
VC_IP_RULE_PRESENT=no
|
|
return 0
|
|
fi
|
|
vc_rule_count=$(printf '%s\n' "$VC_IP_RULE_EXISTING" | awk 'NF {count++} END {print count + 0}')
|
|
[ "$vc_rule_count" = 1 ] || return "$vc_rule_invalid"
|
|
if [ "$vc_rule_iif" = - ]; then
|
|
vc_rule_expected="$vc_rule_pref: from all to $vc_rule_to lookup $vc_rule_table"
|
|
else
|
|
vc_rule_expected="$vc_rule_pref: from all to $vc_rule_to iif $vc_rule_iif lookup $vc_rule_table"
|
|
fi
|
|
vc_rule_normalized=$(printf '%s\n' "$VC_IP_RULE_EXISTING" | awk '{$1=$1; print}')
|
|
[ "$vc_rule_normalized" = "$vc_rule_expected" ] || return "$vc_rule_invalid"
|
|
VC_IP_RULE_PRESENT=yes
|
|
return 0
|
|
}
|
|
|
|
vc_ip_rule_preflight() {
|
|
vc_rule_snapshot=$7
|
|
vc_ip_rule_read "$1" "$2" "$3" "$4" "$5" "$6" || return $?
|
|
if [ "$VC_IP_RULE_PRESENT" = yes ] && [ "$vc_rule_snapshot" != - ]; then
|
|
printf '%s|%s|%s|%s\n' "$1" "$2" "$3" "$4" >> "$vc_rule_snapshot" || return "$6"
|
|
fi
|
|
return 0
|
|
}
|
|
|
|
vc_ip_rule_scope_check() {
|
|
vc_rule_scope_to=$1
|
|
vc_rule_scope_table=$2
|
|
vc_rule_scope_base_pref=$3
|
|
vc_rule_scope_iif=$4
|
|
vc_rule_scope_mode=$5
|
|
vc_rule_scope_invalid=$6
|
|
vc_rule_scope_command_failed=$7
|
|
case "$vc_rule_scope_mode" in
|
|
managed|empty) : ;;
|
|
*) return "$vc_rule_scope_invalid" ;;
|
|
esac
|
|
if ! VC_IP_RULE_SCOPE=$("$IP" -o -4 -N rule show 2>/dev/null); then
|
|
return "$vc_rule_scope_command_failed"
|
|
fi
|
|
if ! printf '%s\n' "$VC_IP_RULE_SCOPE" | awk \
|
|
-v expected_to="$vc_rule_scope_to" \
|
|
-v expected_table="$vc_rule_scope_table" \
|
|
-v base_pref="$vc_rule_scope_base_pref" \
|
|
-v expected_iif="$vc_rule_scope_iif" \
|
|
-v mode="$vc_rule_scope_mode" '
|
|
NF {
|
|
$1=$1
|
|
normalized=$0
|
|
destination=""
|
|
table=""
|
|
for (i=1; i<=NF; i++) {
|
|
if ($i == "to" && i < NF) destination=$(i+1)
|
|
if ($i == "lookup" && i < NF) table=$(i+1)
|
|
}
|
|
if (destination != expected_to && table != expected_table) next
|
|
if (mode == "empty") { bad=1; exit }
|
|
expected_base=base_pref ": from all to " expected_to \
|
|
" lookup " expected_table
|
|
expected_fabric=(base_pref + 1) ": from all to " expected_to \
|
|
" iif " expected_iif " lookup " expected_table
|
|
if (normalized == expected_base) {
|
|
if (++seen_base > 1) { bad=1; exit }
|
|
next
|
|
}
|
|
if (normalized == expected_fabric) {
|
|
if (++seen_fabric > 1) { bad=1; exit }
|
|
next
|
|
}
|
|
bad=1
|
|
exit
|
|
}
|
|
END {exit bad ? 1 : 0}
|
|
'; then
|
|
return "$vc_rule_scope_invalid"
|
|
fi
|
|
return 0
|
|
}
|
|
|
|
vc_ip_rule_ensure() {
|
|
vc_rule_attempted=$7
|
|
vc_rule_created=$8
|
|
vc_ip_rule_read "$1" "$2" "$3" "$4" "$5" "$6" || return $?
|
|
if [ "$VC_IP_RULE_PRESENT" = no ]; then
|
|
if [ "$vc_rule_attempted" != - ]; then
|
|
printf '%s|%s|%s|%s\n' "$1" "$2" "$3" "$4" >> "$vc_rule_attempted" || return "$6"
|
|
fi
|
|
if [ "$2" = - ]; then
|
|
"$IP" -4 rule add pref "$1" to "$3" lookup "$4" || return "$6"
|
|
else
|
|
"$IP" -4 rule add pref "$1" iif "$2" to "$3" lookup "$4" || return "$6"
|
|
fi
|
|
vc_ip_rule_read "$1" "$2" "$3" "$4" "$5" "$6" || return $?
|
|
[ "$VC_IP_RULE_PRESENT" = yes ] || return "$5"
|
|
if [ "$vc_rule_created" != - ]; then
|
|
printf '%s|%s|%s|%s\n' "$1" "$2" "$3" "$4" >> "$vc_rule_created" || return "$6"
|
|
fi
|
|
fi
|
|
return 0
|
|
}
|
|
'''
|