-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnmctl.go
53 lines (49 loc) · 1.51 KB
/
nmctl.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
package cmd
import (
"net/http"
"os"
"runtime"
"github.com/bitfield/script"
"github.com/pterm/pterm"
)
func installNmctl() {
pterm.Println("installing nmctl")
arch := runtime.GOARCH
baseURL := "https://github.com/gravitl/netmaker/releases/download/" + latest + "/nmctl-linux-" + arch
getFile(baseURL, "", "/tmp/nmctl")
os.Chmod("/tmp/nmctl", 0700)
if _, err := script.Exec("/tmp/nmctl context set default --endpoint=https://api." + domain + " --master_key=" + masterkey).Stdout(); err != nil {
panic(err)
}
if _, err := script.Exec("/tmp/nmctl context use default").Stdout(); err != nil {
panic(err)
}
if _, err := script.Exec("/tmp/nmctl network list").Stdout(); err != nil {
panic(err)
}
}
func createNetwork() {
request, err := http.NewRequest(http.MethodGet, "https://api."+domain+"/api/networks", nil)
if err != nil {
panic(err)
}
request.Header.Set("Authorization", "Bearer "+masterkey)
resp, err := script.Do(request).String()
if err != nil {
panic(err)
}
if len(resp) > 5 {
pterm.Println("networks exist, skipping creation of new network")
return
}
pterm.Println("Creating netmaker network (10.101.0.0/16)")
if _, err := script.Exec("/tmp/nmctl network create --name netmaker --ipv4_addr 10.101.0.0/16").String(); err != nil {
panic(err)
}
pterm.Println("creating enrollmentkey")
token, err = script.Exec("/tmp/nmctl enrollment_key create --uses 1 --networks netmaker --tags netmaker").JQ(" .token").String()
if err != nil {
panic(err)
}
pterm.Println("enrollment token:", token)
}