From f32faf1f78c3a691b02e383cb3e14cefdeb4a606 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=A3rebe=20-=20Romain=20GERARD?= Date: Tue, 30 Apr 2024 10:44:18 +0200 Subject: [PATCH] feat(demo): Add destroy command --- cmd/demo.go | 18 ++++++- cmd/demo_scripts/create_qovery_demo.sh | 4 +- cmd/demo_scripts/destroy_qovery_demo.sh | 69 +++++++++++++++++++++++++ pkg/version.go | 2 +- 4 files changed, 89 insertions(+), 4 deletions(-) create mode 100755 cmd/demo_scripts/destroy_qovery_demo.sh diff --git a/cmd/demo.go b/cmd/demo.go index ad8747aa..126e9694 100644 --- a/cmd/demo.go +++ b/cmd/demo.go @@ -60,10 +60,23 @@ var demoCmd = &cobra.Command{ } if args[0] == "destroy" { - panic("Destroy is not yet implemented") + err := os.WriteFile("destroy_demo_cluster.sh", demoScriptsDestroy, 0700) + if err != nil { + log.Errorf("Cannot write file to disk: %s", err) + os.Exit(1) + } + + cmd := exec.Command("/bin/sh", "destroy_demo_cluster.sh", demoClusterName, organizationId, string(token)) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + if err := cmd.Run(); err != nil { + log.Errorf("Error executing the command %s", err) + } + os.Exit(0) } log.Errorf("Unknown command %s. Only `up` and `destroy` are supported", args[0]) + os.Exit(1) }, } var ( @@ -73,6 +86,9 @@ var ( //go:embed demo_scripts/create_qovery_demo.sh var demoScriptsCreate []byte +//go:embed demo_scripts/destroy_qovery_demo.sh +var demoScriptsDestroy []byte + func init() { var userName string currentUser, err := user.Current() diff --git a/cmd/demo_scripts/create_qovery_demo.sh b/cmd/demo_scripts/create_qovery_demo.sh index e5dd4695..7e624dda 100755 --- a/cmd/demo_scripts/create_qovery_demo.sh +++ b/cmd/demo_scripts/create_qovery_demo.sh @@ -82,7 +82,7 @@ setup_network() { if [ "$(uname -s)" = 'Darwin' ]; then # MacOs set -x - sudo ifconfig lo0 alias 172.42.0.3/32 up || exit 1 + sudo ifconfig lo0 alias 172.42.0.3/32 up || true elif grep -qi microsoft /proc/version; then # Wsl echo '******** PLEASE READ ********' @@ -90,7 +90,7 @@ setup_network() { echo 'netsh interface ipv4 add address name="Loopback Pseudo-Interface 1" address=172.42.0.3 mask=255.255.255.255 skipassource=true' echo '******** PLEASE READ ********' set -x - sudo ip addr add 172.42.0.3/32 dev lo || exit 1 + sudo ip addr add 172.42.0.3/32 dev lo || true fi set +x } diff --git a/cmd/demo_scripts/destroy_qovery_demo.sh b/cmd/demo_scripts/destroy_qovery_demo.sh new file mode 100755 index 00000000..f61bbcc6 --- /dev/null +++ b/cmd/demo_scripts/destroy_qovery_demo.sh @@ -0,0 +1,69 @@ +#!/bin/sh + +set -eu + +CLUSTER_NAME=$1 +ORGANIZATION_ID=$2 +#case $2 in +# qov_*) +# AUTHORIZATION_HEADER="Authorization: Token $3" +# ;; +# +# *) +# AUTHORIZATION_HEADER="Authorization: Bearer $3" +# ;; +#esac + +delete_cluster() { + clusterName=$1 + clusterExist=$(k3d cluster list -o json | jq '.[] | select(.name=="'"$clusterName"'") | .name') + if [ -n "$clusterExist" ] + then + k3d cluster delete "$clusterName" || true + fi + docker network rm "k3d-${clusterName}" || true +} + +teardown_network() { + if [ "$(uname -s)" = 'Darwin' ]; then + # MacOs + set -x + sudo ifconfig lo0 -alias 172.42.0.3/32 up || true + elif grep -qi microsoft /proc/version; then + # Wsl + echo '******** PLEASE READ ********' + echo 'You must run this command from an administrator terminal to finish the cleanup' + echo 'netsh interface ipv4 delete address name="Loopback Pseudo-Interface 1" address=172.42.0.3' + echo '******** PLEASE READ ********' + set -x + sudo ip addr del 172.42.0.3/32 dev lo || true + fi + set +x +} + + +echo '' +echo '""""""""""""""""""""""""""""""""""""""""""""' +echo 'Removing Qovery helm repositories' +echo '""""""""""""""""""""""""""""""""""""""""""""' +helm repo remove qovery || true + +echo '' +echo '""""""""""""""""""""""""""""""""""""""""""""' +echo "Removing $CLUSTER_NAME kube cluster" +echo '""""""""""""""""""""""""""""""""""""""""""""' +delete_cluster "$CLUSTER_NAME" + +echo '' +echo '""""""""""""""""""""""""""""""""""""""""""""' +echo 'Removing network config' +echo '""""""""""""""""""""""""""""""""""""""""""""' +teardown_network + + +echo '' +echo '""""""""""""""""""""""""""""""""""""""""""""' +echo "Qovery local demo cluster is now deleted" +echo "Your created environments still exits !" +echo "Go to https://console.qovery.com/organization/${ORGANIZATION_ID}/clusters/general to delete Qovery cluster config" +echo '""""""""""""""""""""""""""""""""""""""""""""' diff --git a/pkg/version.go b/pkg/version.go index 6df16055..a92e421c 100644 --- a/pkg/version.go +++ b/pkg/version.go @@ -11,7 +11,7 @@ import ( ) func GetCurrentVersion() string { - return "0.90.4" // ci-version-check + return "0.90.5" // ci-version-check } func GetLatestOnlineVersionUrl() (string, error) {