You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.2 KiB

#!/usr/bin/env bash
# CONSTANTS
BASEDIR=$(dirname "$0")
CONTAINER=''
SYMPA_ARCHIVE=''
SYMPA_NET0=''
# FUNCTIONS
function usage {
echo "$(basename "$0") <CONTAINER_NAME>"
}
function parse_options {
while [[ $# -gt 0 ]]; do
case "$1" in
--help | -h)
usage && exit 0
;;
*)
if [[ -z $CONTAINER ]]; then
CONTAINER=$1
else
echo >&2 "Unknown option: $1" && usage && exit 2
fi
;;
esac
shift 1 # Move to the next argument
done
[[ -z $CONTAINER ]] && usage && exit 1 || true
}
function halt_on_error {
>&2 echo "ERROR: $1"
exit ${2:-1}
}
function assert_proxmox {
grep -q ^ID=debian /etc/os-release || halt_on_error 'distro debian required!' 2
command -v pct > /dev/null || halt_on_error 'command `pct` not found!' 3
}
function assert_container_sympa_debian9 {
miaou-exec $CONTAINER -- grep -q ^9 /etc/debian_version
miaou-exec $CONTAINER -- test -d /etc/sympa
}
function export_sympa {
miaou-exec $CONTAINER -- bash << EOF
rm sympa_{export,purge}.bash
wget https://git.artcode.re/cemea/monit-sympa/raw/branch/main/sympa/resources/tools/sympa_{export,purge}.bash
chmod +x sympa_{export,purge}.bash
./sympa_purge.bash
./sympa_export.bash
EOF
SYMPA_ARCHIVE=$(miaou-exec $CONTAINER -- find -type f -name "\*.sympa" | sort -rn | head -1)
miaou-pull $CONTAINER $SYMPA_ARCHIVE
}
function get_container_net0 {
OLD_VMID=$(pct-lookup $CONTAINER)
SYMPA_NET0=$(pct config $OLD_VMID | grep ^net0 | cut -d ' ' -f2)
echo "SYMPA_NET0=$SYMPA_NET0"
}
function rename_old_container {
pct-rename $CONTAINER $CONTAINER.9
pct set $OLD_VMID --delete net0
}
function create_new_container {
miaou-create $CONTAINER
local vmid=$(pct-lookup $CONTAINER)
local net1=$(pct config $vmid | grep ^net0 | cut -d ' ' -f2 | sed 's/name=eth0/name=eth1/')
pct set $vmid --net0 "$SYMPA_NET0"
pct set $vmid --net1 "$net1"
echo '------------------------------------'
echo "rebooting container $CONTAINER $vmid"
echo '------------------------------------'
pct reboot $vmid
}
function import_from_archive {
"$BASEDIR"/miaou-install-from-import-sympa $CONTAINER $SYMPA_ARCHIVE
}
# MAIN
set -Eue
parse_options "$@"
assert_proxmox
assert_container_sympa_debian9
export_sympa
get_container_net0
rename_old_container
create_new_container
import_from_archive