From aa18ef32243cb7e4bd1264b415ef82cbca7d5b54 Mon Sep 17 00:00:00 2001 From: Guillaume Date: Thu, 8 Jun 2023 19:38:17 +0200 Subject: [PATCH] Add proper Cleanup script. Chore following such addition. --- scripts/.cleanup.sh | 52 ++++++++++++++++++++++++++++++++++++++++++ scripts/.common | 30 +++++++++++++++++++++--- scripts/0.start_dls.sh | 4 +--- scripts/1.qemu.sh | 22 ++++-------------- 4 files changed, 85 insertions(+), 23 deletions(-) create mode 100755 scripts/.cleanup.sh diff --git a/scripts/.cleanup.sh b/scripts/.cleanup.sh new file mode 100755 index 0000000..1ad108a --- /dev/null +++ b/scripts/.cleanup.sh @@ -0,0 +1,52 @@ +#!/bin/bash + +stopdls() { + if docker ps &>/dev/null | grep -q " $DLS_CTR_NAME$"; then + pinfo "Stopping Direktil Local Server..." + docker stop $DLS_CTR_NAME + fi +} + +destroyvms() { + for host in ${!hosts[*]}; do + if test -f $ctxdir/data/$host/pid ; then + pid=$(cat $ctxdir/data/$host/pid) + pinfo "Cleaning VM $host with PID $pid..." + kill $pid && sleep 1 + fi + done +} + +cleanuppaths() { + PATHS="data secrets kubeconfig cache dist" + cd $ctxdir + for path in $PATHS; do + if test -e $path; then + pinfo "Removing path $path ..." + rm -rf $path + fi + done +} + +cleanupnetwork() { + if iptables -L |grep -q $QEMU_BR_NAME; then + pinfo "Cleaning iptables rules..." + iptables -t nat -D POSTROUTING -j MASQUERADE -s $QEMU_BR_IP/$QEMU_BR_MASK \! -o $QEMU_BR_NAME + iptables -D FORWARD -o $QEMU_BR_NAME -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT + iptables -D FORWARD -j ACCEPT -i $QEMU_BR_NAME + fi + if ip li show $QEMU_BR_NAME ; then + pinfo "Cleaning existing interfaces..." + ip li set $QEMU_BR_NAME down + ip li del $QEMU_BR_NAME + fi +} + +source $(dirname $0)/.common +check_root +prereqs +stopdls +declare -A hosts +get_hosts +destroyvms +cleanuppaths diff --git a/scripts/.common b/scripts/.common index 7016ee2..d9f045a 100644 --- a/scripts/.common +++ b/scripts/.common @@ -1,11 +1,26 @@ #!/bin/bash ## Vars +# Docker container name for dir2config +D2C_CTR_NAME=dir2config +# Docker container name for direktil local server +DLS_CTR_NAME=dls +# QEMU local bridge name. If you specificy a custom name, you may have to configure qemu bridge helper to allow it +QEMU_BR_NAME=virbr0 +# QEMU VM default disk size +QEMU_DISK_SIZE=30G +# Allocated CPUs to QEMU VMs +QEMU_VM_CPU=4 +# Allocated Memory to QEMU VMs +QEMU_VM_MEM=8096 +################ + +# Token file for dls tknfile=".dls_adm_token" -## Helper funcs +## Helper funcs pinfo() { echo -e "\e[32m$@\e[39m" @@ -42,7 +57,7 @@ check_root() { } prereqs() { - set -ex + # set -ex cd "$(dirname $0)/.." ctxdir="$PWD" } @@ -86,7 +101,7 @@ get_hosts() { ip=$(grep ip: $ctxdir/hosts/${h}.yaml|awk '{print $2}') hosts[$h]="$ip" done - cd - + cd - &>/dev/null } get_parts() { @@ -102,6 +117,15 @@ get_parts() { done } +destroyvms() { + for host in ${!hosts[*]}; do + host=$1 + if test -f $ctxdir/data/$host/pid ; then + pinfo "Detected a pid file, killing process in case VM was already started" + kill $(cat $ctxdir/data/$host/pid) && sleep 1 + fi + done +} create_kubeconfig() { if test -f $ctxdir/kubeconfig; then diff --git a/scripts/0.start_dls.sh b/scripts/0.start_dls.sh index ca03825..03d1a14 100755 --- a/scripts/0.start_dls.sh +++ b/scripts/0.start_dls.sh @@ -1,7 +1,5 @@ #!/bin/bash # -D2C_CTR_NAME=dir2config -DLS_CTR_NAME=dls # Admin token to unlock the DLS store DLS_UNLOCK_TOKEN=changeme # @@ -24,7 +22,7 @@ dir2config() { } start_store() { - if docker ps|grep " $DLS_CTR_NAME$" ; then + if docker ps|grep " $DLS_CTR_NAME$" &>/dev/null; then pinfo "Container $DLS_CTR_NAME seems already running" return fi diff --git a/scripts/1.qemu.sh b/scripts/1.qemu.sh index 645e318..889947e 100755 --- a/scripts/1.qemu.sh +++ b/scripts/1.qemu.sh @@ -3,16 +3,7 @@ # This collection of scripts aims to install a NOVIT cluster easily, with help of QEMU # # -# QEMU local bridge name. If you specificy a custom name, you may have to configure qemu bridge helper to allow it -QEMU_BR_NAME=virbr0 # -# QEMU VM default disk size -QEMU_DISK_SIZE=30G -# Allocated CPUs to QEMU VMs -QEMU_VM_CPU=4 -# Allocated Memory to QEMU VMs -QEMU_VM_MEM=8096 -################ ## QEMU functions @@ -35,15 +26,15 @@ setup_network_qemu() { pinfo "Using detected gateway IP $QEMU_BR_IP for bridge $QEMU_BR_NAME" if ! ip a show dev $QEMU_BR_NAME | grep $QEMU_BR_IP ; then ip a add $QEMU_BR_IP/$QEMU_BR_MASK dev $QEMU_BR_NAME - sudo iptables -t nat -I POSTROUTING -j MASQUERADE -s $QEMU_BR_IP/$QEMU_BR_MASK \! -o $QEMU_BR_NAME - sudo iptables -I FORWARD -o $QEMU_BR_NAME -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT - sudo iptables -I FORWARD -j ACCEPT -i $QEMU_BR_NAME + iptables -t nat -I POSTROUTING -j MASQUERADE -s $QEMU_BR_IP/$QEMU_BR_MASK \! -o $QEMU_BR_NAME + iptables -I FORWARD -o $QEMU_BR_NAME -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT + iptables -I FORWARD -j ACCEPT -i $QEMU_BR_NAME fi if ! test -d /etc/qemu; then mkdir -p /etc/qemu fi if ! grep -q "allow $QEMU_BR_NAME" /etc/qemu/bridge.conf; then - echo "allow $QEMU_BR_NAME" > /etc/qemu/bridge.conf + echo "allow $QEMU_BR_NAME" >> /etc/qemu/bridge.conf fi } run_qemu() { @@ -51,10 +42,6 @@ run_qemu() { for host in ${!hosts[*]}; do ip route show |grep "${hosts[$host]} dev $QEMU_BR_NAME" ||\ ip route add ${hosts[$host]} dev $QEMU_BR_NAME - if test -f $ctxdir/data/$host/pid ; then - pinfo "Detected a pid file, killing process in case VM was already started" - kill $(cat $ctxdir/data/$host/pid) && sleep 1 - fi pinfo "Starting host $host with ip ${hosts[$host]}" qemu-system-x86_64 -enable-kvm -smp $QEMU_VM_CPU -m $QEMU_VM_MEM \ -nic bridge,br=$QEMU_BR_NAME,mac=42:42:42:42:42:0${id} \ @@ -77,6 +64,7 @@ declare -A hosts setup_network_qemu get_hosts get_parts +destroyvms run_qemu #clean