-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathservices.go
85 lines (78 loc) · 1.92 KB
/
services.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"context"
"log"
"net/http"
"os"
"github.com/WeTrustPlatform/blockform/model"
"github.com/WeTrustPlatform/blockform/sshcmd"
"goji.io/pat"
)
func serviceStatus(ctx context.Context, node model.Node, unit string, callback func(string, string)) {
stdin, stderr, err := sshcmd.Exec(
os.Getenv("PRIV_KEY"),
os.Getenv("PASSPHRASE"),
"blockform",
node.DomainName,
"systemctl is-active "+unit,
)
if err != nil {
log.Println(err)
}
callback(stdin, stderr)
}
func serviceLogs(ctx context.Context, node model.Node, unit string, callback func(string, string)) {
stdin, stderr, err := sshcmd.Exec(
os.Getenv("PRIV_KEY"),
os.Getenv("PASSPHRASE"),
"blockform",
node.DomainName,
"journalctl -n 20 -q -o cat -u "+unit,
)
if err != nil {
log.Println(err)
}
callback(stdin, stderr)
}
func handleNodeLogs(w http.ResponseWriter, r *http.Request) {
ID := pat.Param(r, "id")
unit := pat.Param(r, "unit")
if unit != "geth" && unit != "nginx" && unit != "faucet" && unit != "certbot.timer" {
w.WriteHeader(401)
return
}
node := model.Node{}
db.Find(&node, ID)
serviceLogs(context.Background(), node, unit, func(stdin, stderr string) {
w.Write([]byte(stdin))
})
}
func handleNodeStatus(w http.ResponseWriter, r *http.Request) {
ID := pat.Param(r, "id")
unit := pat.Param(r, "unit")
if unit != "geth" && unit != "nginx" && unit != "faucet" && unit != "certbot.timer" {
w.WriteHeader(401)
return
}
node := model.Node{}
db.Find(&node, ID)
serviceStatus(context.Background(), node, unit, func(stdin, stderr string) {
w.Write([]byte(stdin))
})
}
func handleNodeVersion(w http.ResponseWriter, r *http.Request) {
ID := pat.Param(r, "id")
node := model.Node{}
db.Find(&node, ID)
stdin, _, err := sshcmd.Exec(
os.Getenv("PRIV_KEY"),
os.Getenv("PASSPHRASE"),
"blockform",
node.DomainName,
"geth version | grep ^Version",
)
if err != nil {
log.Println(err)
}
w.Write([]byte(stdin))
}