From 6b9c5e64b80ccf6747870a6100c89f0209350cb3 Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Tue, 6 May 2025 23:43:53 +0200 Subject: [PATCH] add vpc Signed-off-by: GnomeZworc --- agent.sh | 24 +++++++++++++++++++++--- delete_vpc.sh | 5 +++++ lib/netns.sh | 23 +++++++++++++++++++++++ lib/vpc.sh | 21 ++++++++++++++++----- 4 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 delete_vpc.sh create mode 100644 lib/netns.sh diff --git a/agent.sh b/agent.sh index b606817..284eb60 100644 --- a/agent.sh +++ b/agent.sh @@ -1,10 +1,13 @@ #!/bin/bash +[[ -f ./libs/shflags ]] && . ./libs/shflags || eval "$(curl --silent https://git.g3e.fr/H6N/tools/raw/branch/main/libs/shflags)" + . ./lib/vpc.sh -function start_vm { - [[ -f ./libs/shflags ]] && . ./libs/shflags || eval "$(curl --silent https://git.g3e.fr/H6N/tools/raw/branch/main/libs/shflags)" +DRY_RUN="1" +SIMULATION="1" +function start_vm { DEFINE_string 'interface' 'eno1' 'KVM Interface' 'i' DEFINE_string 'vxlan_id' '-' 'VXLAN ID' 'v' DEFINE_string 'gateway_ip' '-' 'Gatewat IP' 'g' @@ -44,7 +47,19 @@ function start_vm { echo echo - create_vpc "${FLAGS_vpc_name}" + create_vpc "${FLAGS_dryrun}" "${FLAGS_vpc_name}" +} + +function stop_vm { + DEFINE_string 'vm_name' '-' 'VM NAME' 'm' + DEFINE_string 'vpc_name' '-' 'VPC NAME' 'c' + DEFINE_boolean 'dryrun' false 'Enable dry-run mode' 'd' + + FLAGS "$@" || exit $? + eval set -- "${FLAGS_ARGV}" + + delete_vpc "${FLAGS_vpc_name}" + } function main { @@ -55,6 +70,9 @@ function main { "StartVm") start_vm "$@" ;; + "StopVm") + stop_vm "$@" + ;; *) echo "action : ${fonction} not known !" echo "avalable action :" diff --git a/delete_vpc.sh b/delete_vpc.sh new file mode 100644 index 0000000..81acab2 --- /dev/null +++ b/delete_vpc.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +. ./lib/vpc.sh + +delete_vpc "${1}" \ No newline at end of file diff --git a/lib/netns.sh b/lib/netns.sh new file mode 100644 index 0000000..763ccf2 --- /dev/null +++ b/lib/netns.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +function create_netns { + netns_name="${1}" + + ip netns add "${netns_name}" + +} + +function check_netns { + netns_name="${1}" + + ip netns | grep "${netns_name}" \ + && return 0 \ + || return 1 +} + +function delete_netns { + netns_name="${1}" + + ip netns delete "${netns_name}" + +} \ No newline at end of file diff --git a/lib/vpc.sh b/lib/vpc.sh index 8ae2ba4..bca59f7 100644 --- a/lib/vpc.sh +++ b/lib/vpc.sh @@ -1,6 +1,7 @@ #!/bin/bash . ./lib/db.sh +. ./lib/netns.sh function check_vpc_exist { vpc_name=${1} @@ -10,20 +11,30 @@ function check_vpc_exist { echo " -> check in linux" check_in_db vpc "${vpc_name}" - [ "$?" -eq "0" ] && return 0 || return 1 + [ "$?" -eq "0" ] || return 1 + check_netns "${vpc_name}" + [ "$?" -eq "0" ] || return 1 + return 0 } function create_vpc { - vpc_name="${1}" + dryrun="${1}" + vpc_name="${2}" check_vpc_exist "${vpc_name}" - [ "$?" -eq "0" ] || add_in_db "vpc" "${vpc_name}" + [ "$?" -eq "0" ] || \ + { + add_in_db "vpc" "${vpc_name}" + create_netns "${vpc_name}" + } + } function delete_vpc { - - vpc_name="${1}" + dryrun="${1}" + vpc_name="${2}" delete_in_db "vpc" "${vpc_name}" + delete_netns "${vpc_name}" } \ No newline at end of file