chore: add current provisioning state before migration

This commit is contained in:
Jesús Pérez 2025-09-22 23:11:41 +01:00
parent a9703b4748
commit 50745b0f22
660 changed files with 88126 additions and 0 deletions

View file

@ -0,0 +1,146 @@
#!/bin/bash
USAGE="alias_sdn.sh up(default)|down|check"
[ "$1" == "-h" ] && echo "$USAGE" && exit
[ -z "$1" ] && echo "Task not found" && exit 1
TASK="$1"
IP_LIST=$2
[ -z "$2" ] && echo "IP List not found" && exit 1
SETUP_MODE=${SETUP_MODE:-alias}
INTERFACE=${INTERFACE:-eth2:1}
DEV_INTERFACE=${DEV_INTERFACE:-eth2}
NETMASK=${NETMASK:-255.255.255.0}
ROOT_INTERFACES=${ROOT_INTERFACES:-/etc/network/interfaces}
BACKUP_INTERFACES=${BACKUP_INTERFACES:-/etc/network/_interfaces}
_ping_ip_host() {
[ -z "$1" ] && return 1
local str_wait=""
case $(uname -s) in
Darwin|darwin) str_wait="" ;;
*) str_wait="-w2"
esac
ping "$1" -c2 -q $str_wait >/dev/null 2>/dev/null
}
_add_interface() {
echo "
auto $INTERFACE
iface $INTERFACE inet static
address $IP
netmask $NETMASK
" >> "$ROOT_INTERFACES"
}
_add_as_alias() {
if _ping_ip_host "$IP" ; then
echo "$IP is alive"
return
fi
ip addr add "$IP"/24 dev "$DEV_INTERFACE" label "$INTERFACE"
}
_remove_as_alias() {
if ! _ping_ip_host "$IP" ; then
echo "$IP is not alive"
return
fi
ip addr delete "$IP"/24 dev "$DEV_INTERFACE" label "$INTERFACE"
}
_add_as_system() {
if _ping_ip_host "$IP" ; then
echo "$IP is alive"
return
fi
local has_ip=""
has_ip=$(grep "$IP" "$ROOT_INTERFACES")
if [ -z "$has_ip" ] ; then
[ ! -r "$BACKUP_INTERFACES" ] && cp "$ROOT_INTERFACES" "$BACKUP_INTERFACES"
_add_interface
fi
}
_remove_as_system() {
local has_ip=""
has_ip=$(grep "$IP" "$ROOT_INTERFACES")
if [ -n "$has_ip" ] ; then
[ -r "$BACKUP_INTERFACES" ] && cp "$BACKUP_INTERFACES" "$ROOT_INTERFACES"
has_ip=$(grep "$IP" "$ROOT_INTERFACES")
[ -n "$has_ip" ] && echo "Unable to remove $IP from $$ROOT_INTERFACES" && exit 1
fi
}
_check_interface() {
local ip_a
#ifaces_data=$(ip a | grep "inet " | grep dynamic | sed 's/inet //g' | awk '{print $7":"$1}' | grep "$INTERFACE")
ip_a=$(ip a | grep "inet " | grep "$INTERFACE" | awk '{print $2}' | cut -f1 -d"/" | grep "$IP")
if [ "$IP" != "$ip_a" ] ; then
echo "$IP for $INTERFACE not found"
IP_ACTIVE=""
else
echo "$IP active on $INTERFACE"
IP_ACTIVE="on"
fi
if _ping_ip_host "$IP" ; then
echo "$IP is alive"
fi
}
_restart_networking() {
systemctl restart networing
}
_on_ip() {
IP_ACTIVE=""
_check_interface
case "$TASK" in
up|u) [ -n "$IP_ACTIVE" ] && return
TASK="up"
;;
down|d) [ -z "$IP_ACTIVE" ] && return
TASK="down"
;;
check|c|status|s)
return
;;
ping|p|resp|r)
if _ping_ip_host "$IP" ; then
echo "$IP responding"
else
echo "$IP not responding"
fi
return
;;
*) echo "Option $TASK unknown"
exit 1
esac
case "$SETUP_MODE" in
system|sys)
if [ "$TASK" == "up" ] ; then
_add_as_system
else
_remove_as_system
fi
_restart_networking
_check_interface
;;
alias|a)
if [ "$TASK" == "up" ] ; then
_add_as_alias
else
_remove_as_alias
fi
_check_interface
;;
esac
}
if [ -r "$IP_LIST" ] ; then
TARGET_IPS=$(grep -v "^#" "$IP_LIST")
else
TARGET_IPS=$IP_LIST
fi
for it in $TARGET_IPS
do
IP="$it"
_on_ip
done

View file

@ -0,0 +1,55 @@
#!/bin/bash
ALIAS_SDN_BIN=./alias_sdn.sh
if [ ! -r "$ALIAS_SDN_BIN" ] ; then
echo "ALIAS_SDN_BIN not found in $ALIAS_SDN_BIN"
exit 1
fi
_check_resolution() {
local hostname="$1"
local ip=$2
local main_hostname=${3:-""}
local has_ip=""
has_ip=$(grep "$ip" /etc/hosts | grep -v "^#" | awk '{print $1}')
[ -z "$has_ip" ] && echo "$ip ${hostname}" | sudo tee -a /etc/hosts 2>/dev/null >/dev/null
if [ "$main_hostname" == "true" ] && [ "$hostname" != "$(cat /etc/hostname)" ] ; then
echo "$hostname" | sudo tee /etc/hostname 2>/dev/null >/dev/null
sudo hostname "$hostname"
fi
}
[ -r "./env-ip-aliases" ] && . ./env-ip-aliases
NET_INTERFACES=/etc/network/interfaces
{% if taskserv.aliases %}
{%- for ip in taskserv.aliases %}
has_ip=$(grep {{ip.address}} $NET_INTERFACES)
if [ -z "$has_ip" ] ; then
echo "
auto {{ip.dev_interface}}
iface {{ip.dev_interface}} inet static
address {{ip.address}}
netmask {{ ip.netmask }}
{% if ip.search and ip.nameservers != "" -%}
dns-nameserver {{it}}
{% endif %}
{% if ip.search and ip.search != "" -%}
search {{ ip.search }}
{% endif %}
" | sudo tee -a $NET_INTERFACES &>/dev/null
#export SETUP_MODE={{ ip.setup_mode }}
#export INTERFACE={{ ip.interface }}
#export DEV_INTERFACE={{ ip.dev_interface }}
#export NETMASK={{ ip.netmask }}
#$ALIAS_SDN_BIN up {{ ip.address }}
_check_resolution {{ ip.hostname }} {{ ip.address }} {{ ip.main_hostname }}
fi
{% endfor %}
sudo systemctl restart networking
{% endif %}
#sudo cp $ALIAS_SDN_BIN /etc

View file

@ -0,0 +1,2 @@
export ROOT_INTERFACES=${ROOT_INTERFACES:-/etc/network/interfaces}
export BACKUP_INTERFACES=${BACKUP_INTERFACES:-/etc/network/_interfaces}

View file

@ -0,0 +1,18 @@
#!/bin/bash
# Info: Script to install IP aliases packages and tools
# Author: JesusPerezLorenzo
# Release: 1.0
# Date: 4-08-2024
USAGE="install-ip-aliases.sh"
[ "$1" == "-h" ] && echo "$USAGE" && exit 1
#ORG=$(pwd)
[ -r "./env-ip-aliases" ] && . ./env-ip-aliases
if [ -r "create_alias.sh" ] ; then
chmod +x ./create_alias.sh
./create_alias.sh
fi