-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnetclient.go
64 lines (61 loc) · 2.14 KB
/
netclient.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package cmd
import (
"os"
"runtime"
"strings"
"github.com/bitfield/script"
"github.com/pterm/pterm"
)
func installNetclient() {
newInstall := false
existingVersion, _ := script.Exec("netclient version").String()
pterm.Println("checking for installed netclient ", "existing version", existingVersion, "latest", latest)
if strings.TrimSpace(existingVersion) == latest {
pterm.Println("latest version of netclient already installed, skipping...")
return
} else if strings.Contains(existingVersion, "file not found") {
newInstall = true
}
pterm.Println("netclient new install:", newInstall)
pterm.Println("retrieving the latest version of netclient")
arch := runtime.GOARCH
baseURL := "https://github.com/gravitl/netclient/releases/download/" + latest + "/netclient-linux-" + arch
getFile(baseURL, "", "/tmp/netclient")
os.Chmod("/tmp/netclient", 0700)
if _, err := script.Exec("/tmp/netclient install").Stdout(); err != nil {
panic(err)
}
if newInstall {
pterm.Println("latest version of netclient installed")
} else {
pterm.Println("netclient updated to latest version")
// since this was an update don't contine
return
}
pterm.Println("joining network with token", token)
if token != "" {
if _, err := script.Exec("netclient join -t " + token).Stdout(); err != nil {
panic(err)
}
} else {
pterm.Println("enrollment key not defined")
}
nodeID, err := script.Exec("/tmp/nmctl node list --output json").JQ(".[]").JQ(".id").String()
if err != nil {
pterm.Println("error reading netclient config:", err)
return
}
hostID, err := script.Exec("/tmp/nmctl node list --output json").JQ(".[]").JQ(".hostid").String()
if err != nil {
pterm.Println("error reading netclient config:", err)
return
}
pterm.Println("setting default node (nmctl host update " + hostID + " --default)")
script.Exec("/tmp/nmctl host update " + hostID + " --default").Stdout()
pterm.Println("creating ingress gateway (nmctl node create_ingress netmaker " + nodeID + ")")
if hostID == "" {
pterm.Println("something went wrong joining network, unable to create ingress")
return
}
script.Exec("/tmp/nmctl node create_ingress netmaker " + nodeID).Stdout()
}