HEX
Server: Apache/2.2.34 (Unix) mod_fastcgi/mod_fastcgi-SNAP-0910052141
System: Linux Kou-Etsu-Dou 4.4.59+ #25556 SMP PREEMPT Thu Mar 4 18:03:46 CST 2021 x86_64
User: hosam (1026)
PHP: 7.2.29
Disabled: NONE
Upload Files
File: //volume1/@appstore/MailPlus-Server/hook/MailPlusServerIFNameChange.sh
#!/bin/bash

if  [ ! -e "/var/packages/MailPlus-Server/target/" ]; then
	exit 0
fi
. /var/packages/MailPlus-Server/target/scripts/DsmVersionUtil.sh
. /var/packages/MailPlus-Server/target/backend_hook/hookUtils.conf

VlanAction()
{
	local host_if=$(${MAILPLUS_SERVER_BACKEND_BINARY} --getHostIF)
	local ori_if=${ORIGIN_IFNAME_1}
	local new_if=${IFNAME_1}

	if [ -z "${host_if}" ]; then
		log_err "[Vlan Action] empty host interface"
		return 1
	fi

	if [ "${host_if}" == "${ori_if}" ]; then
		local host_id=$(${MAILPLUS_SERVER_BACKEND_BINARY} --getHostID)

		if [ -z "${host_id}" ]; then
			err_log "[Vlan Action] empty host id"
			return 1
		fi

		if ! ${MAILPLUS_SERVER_BACKEND_BINARY} --setConfKeyVal "node_bind_if-${host_id}" "${new_if}"; then
			err_log "[Vlan Action] - Failed to set the new interface to [${new_if}]"
		fi

		bringDownMPSinterface "${new_if}"

		local new_ip="$(getInterfaceIP "${new_if}")"
		if [ -z "${new_ip}" ]; then
			err_log "[Vlan Action] cant not get ip of interface [${new_if}]"
			return 1
		fi

		## update backend config and mapping table
		if ! ${MAILPLUS_SERVER_BACKEND_BINARY} --mappingRegen "${new_ip}"; then
			err_log "[Vlan Action] - Failed to regen mailplus server id-ip mapping table for [${new_ip}]"
		fi
	fi
	return 0
}

CreateBondHook()
{
	local host_if=$(${MAILPLUS_SERVER_BACKEND_BINARY} --getHostIF)
	local bond=${IFNAME_1}
	local ori_if_idx=1
	local ori_if_cnt=$((ORIGIN_IFNAME_NUM + 1))
	local matched=

	if [ -z "${bond}" ]; then
		log_err "[MailPlusServerBondCreate] empty bond name"
		return 1
	fi

	while [ "${ori_if_idx}" != "${ori_if_cnt}" ]
	do
		eval "if_name=\$ORIGIN_IFNAME_$ori_if_idx"
		if [ "${if_name}" == ${host_if} ]; then
			matched=1
			break
		fi
		ori_if_idx=$((ori_if_idx + 1));
	done

	if [ "${matched}" == "1" ]; then
		local host_id=$(${MAILPLUS_SERVER_BACKEND_BINARY} --getHostID)
		if [ -z "${host_id}" ]; then
			err_log "[MailPlusServerBondCreate] empty host id"
			return 1
		fi

		if ! ${MAILPLUS_SERVER_BACKEND_BINARY} --setConfKeyVal "node_bind_if-${host_id}" "${bond}"; then
			err_log "[MailPlusServerBondCreate] - Failed to set the interface to [${bond}]"
		fi

		bringDownMPSinterface "${bond}"

		local new_ip="$(getInterfaceIP "${bond}")"
		if [ -z "${new_ip}" ]; then
			err_log "[MailPlusServerBondCreate] cant not get ip of interface [${bond}]"
			return 1
		fi
		## update backend config and mapping table
		if ! ${MAILPLUS_SERVER_BACKEND_BINARY} --mappingRegen "${new_ip}"; then
			err_log "[MailPlusServerBondCreate] - Failed to regen mailplus server id-ip mapping table for [$new_ip]"
		fi
	fi
	return 0
}

DeleteBondHook()
{
	local bond=${ORIGIN_IFNAME_1}

	if [ -z "${bond}" ]; then
		log_err "[MailPlusServerBondDelete] empty bond name"
		return 1
	fi

	local host_if=$(${MAILPLUS_SERVER_BACKEND_BINARY} --getHostIF)
	if [ "${bond}" == "${host_if}" ]; then
		local host_id=$(${MAILPLUS_SERVER_BACKEND_BINARY} --getHostID)
		if [ -z "${host_id}" ]; then
			err_log "[MailPlusServerBondDelete] empty host id"
			return 1
		fi
		local new_if=${IFNAME_1}

		if [ -z "${IFNAME_1}" ]; then
			err_log "[MailPlusServerBondDelete] - empty new interface name"
		fi

		if ! ${MAILPLUS_SERVER_BACKEND_BINARY} --setConfKeyVal "node_bind_if-${host_id}" "${new_if}"; then
			err_log "[MailPlusServerBondDelete] - Failed to set the new interface to [${new_if}]"
		fi

		bringDownMPSinterface "${new_if}"

		local new_ip="$(getInterfaceIP "${new_if}")"
		if [ -z "${new_ip}" ]; then
			err_log "[MailPlusServerBondDelete] cant not get ip of interface [${new_if}]"
			return 1
		fi
		## update backend config and mapping table
		if ! ${MAILPLUS_SERVER_BACKEND_BINARY} --mappingRegen "${new_ip}"; then
			err_log "[MailPlusServerBondDelete] - Failed to regen mailplus server id-ip mapping table for [${new_ip}]"
		fi
	fi
	return 0
}

case $1 in
	--sdk-mod-ver)
	#Print SDK support version
	echo "1.0";
	;;
	--name)
	#Print package name
	echo "MailPlus-Server";
	;;
	--pkg-ver)
	#Print package version
	echo "1.0";
	;;
	--vendor)
	#Print package vendor
	echo "Synology";
	;;
	--pre)
	;;
	--post)
		if [ "0" != "${RESULT}" ]; then
			exit 0
		fi

		if isPackageStarted && ${MAILPLUS_SERVER_BACKEND_BINARY} --isClusterInit; then
			if [ "${ORIGIN_IFNAME_NUM}" -eq 1 ]; then
				if [ "${IFNAME_NUM}" -eq 1 ]; then
					#vlan create/remove
					VlanAction
				elif [ "${IFNAME_NUM}" -gt 1 ]; then
					#bond delete
					DeleteBondHook
				else
					err_log "[MailPlusServerIFNameChange] unknown interface name change event ${ORIGIN_IFNAME_NUM} => ${IFNAME_NUM}"
				fi
			elif [ "${ORIGIN_IFNAME_NUM}" -gt 1 -a "${IFNAME_NUM}" -eq 1 ]; then
				#bond create
				CreateBondHook
			else
				err_log "[MailPlusServerIFNameChange] unknown interface name change event ${ORIGIN_IFNAME_NUM} => ${IFNAME_NUM}"
			fi
		fi
	;;
	*)
	echo "Usage: $0 --sdk-mod-ver|--name|--pkg-ver|--vendor|--pre|--post"
	;;
esac