Skip to content

Commit

Permalink
Add reset command to uninstall k3s from nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Gu1llaum-3 committed Jul 12, 2024
1 parent 752c22a commit 067619f
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
107 changes: 107 additions & 0 deletions cmd/reset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package cmd

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"

"github.com/spf13/cobra"
)

type Node struct {
Hostname string `json:"hostname"`
IP string `json:"ip"`
}

func MakeReset() *cobra.Command {
var user, ip, sshKey, plan string

cmd := &cobra.Command{
Use: "reset",
Short: "Uninstall k3s on specified nodes",
RunE: func(cmd *cobra.Command, args []string) error {
if user == "" || (ip == "" && plan == "") {
return fmt.Errorf("Usage: %s", cmd.UsageString())
}

if plan != "" {
return uninstallK3sFromPlan(user, sshKey, plan)
}
return uninstallK3s(user, sshKey, ip)
},
}

cmd.Flags().StringVarP(&user, "user", "u", "", "Username for SSH connection")
cmd.Flags().StringVarP(&ip, "ip", "i", "", "IP address of the host")
cmd.Flags().StringVar(&sshKey, "ssh-key", os.Getenv("HOME")+"/.ssh/id_rsa", "Path to the private SSH key")
cmd.Flags().StringVar(&plan, "plan", "", "JSON file containing the list of nodes")

return cmd
}

func uninstallK3s(user, sshKey, ip string) error {
fmt.Printf("Uninstalling k3s on host %s\n", ip)
cmd := exec.Command("ssh", "-i", sshKey, "-o", "BatchMode=yes", "-o", "StrictHostKeyChecking=no", "-o", "ConnectTimeout=10", fmt.Sprintf("%s@%s", user, ip), "bash -s")
cmd.Stdin = strings.NewReader(`
if [ -f /usr/local/bin/k3s-uninstall.sh ]; then
/usr/local/bin/k3s-uninstall.sh
echo "k3s server uninstalled successfully."
elif [ -f /usr/local/bin/k3s-agent-uninstall.sh ]; then
/usr/local/bin/k3s-agent-uninstall.sh
echo "k3s agent uninstalled successfully."
else
echo "Neither k3s-uninstall.sh nor k3s-agent-uninstall.sh found."
exit 1
fi
`)
output, err := cmd.CombinedOutput()
fmt.Printf("%s\n", output)
if err != nil {
return fmt.Errorf("failed to execute script on %s: %v", ip, err)
}
return nil
}

func uninstallK3sFromPlan(user, sshKey, plan string) error {
data, err := ioutil.ReadFile(plan)
if err != nil {
return fmt.Errorf("unable to read JSON file %s: %v", plan, err)
}

var nodes []Node
if err := json.Unmarshal(data, &nodes); err != nil {
return fmt.Errorf("error parsing JSON file %s: %v", plan, err)
}

var successNodes []Node
var failedNodes []Node

for _, node := range nodes {
fmt.Printf("Uninstalling k3s on %s (%s)\n", node.Hostname, node.IP)
if err := uninstallK3s(user, sshKey, node.IP); err != nil {
fmt.Printf("Error: %v\n", err)
failedNodes = append(failedNodes, node)
} else {
fmt.Printf("k3s successfully uninstalled on %s (%s)\n", node.Hostname, node.IP)
successNodes = append(successNodes, node)
}
}

fmt.Println("\nSummary of uninstallation:")
fmt.Println("Successful:")
for _, node := range successNodes {
fmt.Printf("- %s (%s)\n", node.Hostname, node.IP)
}

if len(failedNodes) > 0 {
fmt.Println("Failed:")
for _, node := range failedNodes {
fmt.Printf("- %s (%s)\n", node.Hostname, node.IP)
}
}

return nil
}
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func main() {
cmdReady := cmd.MakeReady()
cmdPlan := cmd.MakePlan()
cmdNodeToken := cmd.MakeNodeToken()
cmdReset := cmd.MakeReset()

printk3supASCIIArt := cmd.PrintK3supASCIIArt

Expand Down Expand Up @@ -58,6 +59,7 @@ func main() {
rootCmd.AddCommand(cmdReady)
rootCmd.AddCommand(cmdPlan)
rootCmd.AddCommand(cmdNodeToken)
rootCmd.AddCommand(cmdReset)

if err := rootCmd.Execute(); err != nil {
os.Exit(1)
Expand Down

0 comments on commit 067619f

Please sign in to comment.