Skip to content

Commit 8861d70

Browse files
committed
Add reset command to uninstall k3s from nodes
Signed-off-by: Gu1llaum-3 <[email protected]>
1 parent 752c22a commit 8861d70

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

cmd/reset.go

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package cmd
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io/ioutil"
7+
"os"
8+
"os/exec"
9+
"strings"
10+
11+
"github.com/spf13/cobra"
12+
)
13+
14+
type Node struct {
15+
Hostname string `json:"hostname"`
16+
IP string `json:"ip"`
17+
}
18+
19+
func MakeReset() *cobra.Command {
20+
var user, ip, sshKey, plan string
21+
22+
cmd := &cobra.Command{
23+
Use: "reset",
24+
Short: "Uninstall k3s on specified nodes",
25+
RunE: func(cmd *cobra.Command, args []string) error {
26+
if user == "" || (ip == "" && plan == "") {
27+
return fmt.Errorf("Usage: %s", cmd.UsageString())
28+
}
29+
30+
if plan != "" {
31+
return uninstallK3sFromPlan(user, sshKey, plan)
32+
}
33+
return uninstallK3s(user, sshKey, ip)
34+
},
35+
}
36+
37+
cmd.Flags().StringVarP(&user, "user", "u", "", "Username for SSH connection")
38+
cmd.Flags().StringVarP(&ip, "ip", "i", "", "IP address of the host")
39+
cmd.Flags().StringVar(&sshKey, "ssh-key", os.Getenv("HOME")+"/.ssh/id_rsa", "Path to the private SSH key")
40+
cmd.Flags().StringVar(&plan, "plan", "", "JSON file containing the list of nodes")
41+
42+
return cmd
43+
}
44+
45+
func uninstallK3s(user, sshKey, ip string) error {
46+
fmt.Printf("Uninstalling k3s on host %s\n", ip)
47+
cmd := exec.Command("ssh", "-i", sshKey, "-o", "BatchMode=yes", "-o", "StrictHostKeyChecking=no", "-o", "ConnectTimeout=10", fmt.Sprintf("%s@%s", user, ip), "bash -s")
48+
cmd.Stdin = strings.NewReader(`
49+
if [ -f /usr/local/bin/k3s-uninstall.sh ]; then
50+
/usr/local/bin/k3s-uninstall.sh
51+
echo "k3s server uninstalled successfully."
52+
elif [ -f /usr/local/bin/k3s-agent-uninstall.sh ]; then
53+
/usr/local/bin/k3s-agent-uninstall.sh
54+
echo "k3s agent uninstalled successfully."
55+
else
56+
echo "Neither k3s-uninstall.sh nor k3s-agent-uninstall.sh found."
57+
exit 1
58+
fi
59+
`)
60+
output, err := cmd.CombinedOutput()
61+
fmt.Printf("%s\n", output)
62+
if err != nil {
63+
return fmt.Errorf("failed to execute script on %s: %v", ip, err)
64+
}
65+
return nil
66+
}
67+
68+
func uninstallK3sFromPlan(user, sshKey, plan string) error {
69+
data, err := ioutil.ReadFile(plan)
70+
if err != nil {
71+
return fmt.Errorf("unable to read JSON file %s: %v", plan, err)
72+
}
73+
74+
var nodes []Node
75+
if err := json.Unmarshal(data, &nodes); err != nil {
76+
return fmt.Errorf("error parsing JSON file %s: %v", plan, err)
77+
}
78+
79+
var successNodes []Node
80+
var failedNodes []Node
81+
82+
for _, node := range nodes {
83+
fmt.Printf("Uninstalling k3s on %s (%s)\n", node.Hostname, node.IP)
84+
if err := uninstallK3s(user, sshKey, node.IP); err != nil {
85+
fmt.Printf("Error: %v\n", err)
86+
failedNodes = append(failedNodes, node)
87+
} else {
88+
fmt.Printf("k3s successfully uninstalled on %s (%s)\n", node.Hostname, node.IP)
89+
successNodes = append(successNodes, node)
90+
}
91+
}
92+
93+
fmt.Println("\nSummary of uninstallation:")
94+
fmt.Println("Successful:")
95+
for _, node := range successNodes {
96+
fmt.Printf("- %s (%s)\n", node.Hostname, node.IP)
97+
}
98+
99+
if len(failedNodes) > 0 {
100+
fmt.Println("Failed:")
101+
for _, node := range failedNodes {
102+
fmt.Printf("- %s (%s)\n", node.Hostname, node.IP)
103+
}
104+
}
105+
106+
return nil
107+
}

main.go

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func main() {
1717
cmdReady := cmd.MakeReady()
1818
cmdPlan := cmd.MakePlan()
1919
cmdNodeToken := cmd.MakeNodeToken()
20+
cmdReset := cmd.MakeReset()
2021

2122
printk3supASCIIArt := cmd.PrintK3supASCIIArt
2223

@@ -58,6 +59,7 @@ func main() {
5859
rootCmd.AddCommand(cmdReady)
5960
rootCmd.AddCommand(cmdPlan)
6061
rootCmd.AddCommand(cmdNodeToken)
62+
rootCmd.AddCommand(cmdReset)
6163

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

0 commit comments

Comments
 (0)