Skip to content

Commit

Permalink
Merge "Update network interfaces during upgrade bootstrap execution"
Browse files Browse the repository at this point in the history
  • Loading branch information
Zuul authored and openstack-gerrit committed Oct 10, 2023
2 parents 3dcd90d + 521f929 commit 2a96b5b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 84 deletions.
42 changes: 25 additions & 17 deletions puppet-manifests/src/bin/apply_network_config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ function array_diff {
function sysinv_agent_lock {
case $1 in
${ACQUIRE_LOCK})
log_it "Acquiring lock to synchronize with sysinv-agent audit"
local lock_file="/var/run/apply_network_config.lock"
# Lock file should be the same as defined in sysinv agent code
local lock_timeout=5
Expand All @@ -126,12 +127,14 @@ function sysinv_agent_lock {
fi
;;
${RELEASE_LOCK})
log_it "Releasing lock"
[[ ${LOCK_FD} -gt 0 ]] && flock -u ${LOCK_FD}
;;
esac
}

function update_interfaces {
skip_lock=$(($1))
upDown=()
changed=()
vlans=()
Expand Down Expand Up @@ -195,23 +198,24 @@ function update_interfaces {
done

current=()
if [ -x ${ETC_DIR}/auto ]; then
if [ -f ${ETC_DIR}/auto ]; then
auto_etc=( $(grep -v HEADER ${ETC_DIR}/auto) )
current=( ${auto_etc[@]:1} )
fi

active=( ${auto_puppet[@]} )

# synchronize with sysinv-agent audit
sysinv_agent_lock ${ACQUIRE_LOCK}
if [ ${skip_lock} -ne 0 ]; then
# synchronize with sysinv-agent audit
sysinv_agent_lock ${ACQUIRE_LOCK}
fi

remove=$(array_diff current[@] active[@])
for r in ${remove[@]}; do
for iface in ${remove[@]}; do
# Bring down interface before we execute network restart, interfaces
# that do not have an ifcfg are not managed by init script
iface=${r#ifcfg-}
do_if_down ${iface}
do_rm ${ETC_DIR}/${r}
do_rm ${ETC_DIR}/ifcfg-${iface}
done

# If a lower ethernet interface is being changed, the upper vlan interface(s) will lose
Expand All @@ -234,9 +238,9 @@ function update_interfaces {
# away unexpectedly).
for iftype in vlan ethernet; do
for cfg in ${upDown[@]}; do
ifcfg=${ETC_DIR}/${cfg}
ifcfg=${PUPPET_DIR}/${cfg}
if iftype_filter ${iftype} ${ifcfg}; then
do_if_down ${ifcfg#ifcfg-}
do_if_down ${cfg:6}
fi
done
done
Expand All @@ -259,13 +263,15 @@ function update_interfaces {
for cfg in ${upDown[@]}; do
ifcfg=${PUPPET_DIR}/${cfg}
if iftype_filter ${iftype} ${ifcfg}; then
do_if_up ${ifcfg#ifcfg-}
do_if_up ${cfg:6}
fi
done
done

# unlock: synchronize with sysinv-agent audit
sysinv_agent_lock ${RELEASE_LOCK}
if [ ${skip_lock} -ne 0 ]; then
# unlock: synchronize with sysinv-agent audit
sysinv_agent_lock ${RELEASE_LOCK}
fi

echo "${updated_ifs[@]}"
}
Expand Down Expand Up @@ -323,14 +329,16 @@ else

parse_interface_stanzas

if [[ ! -f /var/run/.network_upgrade_bootstrap ]]; then
ifaces=$(update_interfaces)
update_routes "${ifaces}"
else
log_it "Executing upgrade bootstrap, just add the config files into /etc/network/"
update_config
[ -f /var/run/.network_upgrade_bootstrap ]
upgr_bootstrap=$?

if [ ${upgr_bootstrap} -eq 0 ]; then
log_it "Upgrade bootstrap is in execution"
fi

ifaces=$(update_interfaces ${upgr_bootstrap})
update_routes "${ifaces}"

else
log_it "Not using sysconfig or ifupdown, cannot advance! Aborting..."
exit 1
Expand Down
82 changes: 15 additions & 67 deletions puppet-manifests/src/bin/network_ifupdown.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,53 +18,31 @@ export ETC_ROUTES_FILE="/etc/network/routes"
export ETC_DIR="/etc/network/interfaces.d/"

#
# Execute ifup script
# returns $(false) if argument $1 is invalid
# Sets interface to UP state
#
function do_if_up {
local cfg=$1
local search_file=''
local iface=''
local if_name=''
if [ -f ${cfg} ]; then
search_file=${cfg}
elif [ -f ${PUPPET_DIR}/${cfg} ]; then
search_file=${PUPPET_DIR}/${cfg}
elif [ -f ${PUPPET_DIR}/ifcfg-${cfg} ]; then
search_file=${PUPPET_DIR}/ifcfg-${cfg}
else
log_it "do_if_up: cannot process argument ${cfg}"
return $(false)
fi
iface=$( grep iface ${search_file} )
if_name=$( echo "${iface}" | awk '{print $2}' )
local if_name=$1

log_it "Bringing ${if_name} up"

/sbin/ifup ${if_name} || log_it "Failed bringing ${if_name} up"
}

#
# Execute ifdown script
# returns $(false) if argument $1 is invalid
# Sets interface to DOWN state
#
function do_if_down {
local cfg=$1
local search_file=''
local iface=''
local if_name=''
if [ -f ${cfg} ]; then
search_file=${cfg}
elif [ -f ${ETC_DIR}/${cfg} ]; then
search_file=${ETC_DIR}/${cfg}
elif [ -f ${ETC_DIR}/ifcfg-${cfg} ]; then
search_file=${ETC_DIR}/ifcfg-${cfg}
else
log_it "do_if_down: cannot process argument ${cfg}"
return $(false)
fi
iface=$( grep iface ${search_file} )
if_name=$( echo "${iface}" | awk '{print $2}' )
local if_name=$1

log_it "Bringing ${if_name} down"
/sbin/ifdown ${if_name} || log_it "Failed bringing ${if_name} down"

# ifdown may fail if the interface is not currently managed by ifupdown,
# or if the ifcfg- file was changed or erased
/sbin/ifdown ${if_name} > /dev/null 2>&1

# force link to down and erase any remaining IP addresses
/usr/sbin/ip link set down dev ${if_name} > /dev/null 2>&1
/usr/sbin/ip addr flush dev ${if_name} > /dev/null 2>&1
}

#
Expand Down Expand Up @@ -642,33 +620,3 @@ function update_routes {
do_cp ${PUPPET_ROUTES_FILE} ${ETC_ROUTES_FILE}
fi
}

function update_config {

# process interfaces
auto_puppet=( $(grep -v HEADER ${PUPPET_DIR}/auto) )
for auto_if in ${auto_puppet[@]:1}; do
cfg="ifcfg-${auto_if}"
do_cp ${PUPPET_DIR}/${cfg} ${ETC_DIR}/${cfg}
done
do_cp ${PUPPET_DIR}/auto ${ETC_DIR}/auto

# process routes
if [ -f ${PUPPET_ROUTES6_FILE} ]; then
log_it "add IPv6 routes generated in network.pp"
if [ -f ${PUPPET_ROUTES_FILE} ]; then
puppet_data=$(grep -v HEADER ${PUPPET_ROUTES6_FILE})
while read route6Line; do
route_exists=$( grep -E "${route6Line}" ${PUPPET_ROUTES_FILE} )
if [ "${route_exists}" == "" ]; then
echo "${route6Line}" >> ${PUPPET_ROUTES_FILE}
fi
done <<< ${puppet_data}
else
cat ${PUPPET_ROUTES6_FILE} >> ${PUPPET_ROUTES_FILE}
fi
fi
if [ -f ${PUPPET_ROUTES_FILE} ]; then
do_cp ${PUPPET_ROUTES_FILE} ${ETC_ROUTES_FILE}
fi
}

0 comments on commit 2a96b5b

Please sign in to comment.