-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathupgrade.go
68 lines (62 loc) · 1.5 KB
/
upgrade.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
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"github.com/WeTrustPlatform/blockform/cloudinit"
"github.com/WeTrustPlatform/blockform/model"
"github.com/WeTrustPlatform/blockform/sshcmd"
"goji.io/pat"
)
// UpgradeNode upgrades the geth to latest version
func upgradeNode(ctx context.Context, node model.Node, callback func(error)) {
_, _, err := sshcmd.Exec(
os.Getenv("PRIV_KEY"),
os.Getenv("PASSPHRASE"),
"blockform",
node.DomainName,
fmt.Sprintf(`
wget --no-cache -qO- %s | bash \
&& sudo systemctl stop geth \
&& sudo cp geth /usr/bin/ \
&& sudo systemctl start geth \
`,
cloudinit.DownloadGethSh),
)
if err != nil {
log.Println(err)
// if there are errors,
// geth might not be running
sshcmd.Exec(
os.Getenv("PRIV_KEY"),
os.Getenv("PASSPHRASE"),
"blockform",
node.DomainName,
"sudo systemctl restart geth",
)
}
callback(err)
}
func handleUpgrade(w http.ResponseWriter, r *http.Request) {
ID := pat.Param(r, "id")
node := model.Node{}
db.Find(&node, ID)
log.Println("Upgrading geth", node.Name)
go upgradeNode(context.Background(), node, func(err error) {
var title = "The node has been upgraded to the latest geth"
var eventType = model.Fine
if err != nil {
title = "Upgrading geth failed"
eventType = model.Issue
}
db.Create(&model.Event{
NodeID: node.ID,
Type: eventType,
Title: title,
})
log.Println("Done upgrading node " + node.Name)
})
http.Redirect(w, r, "/node/"+ID+"/activity", http.StatusSeeOther)
}