-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
84 lines (75 loc) · 1.83 KB
/
main.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
package main
import (
"fmt"
"log"
"os"
"os/signal"
"time"
)
func main() {
killSignal := make(chan os.Signal, 1)
signal.Notify(killSignal, os.Interrupt, os.Kill)
mainLoop:
for {
client, agentConn, err := setupSshClient()
if err != nil {
log.Println("Ssh connection error, trying again in five seconds", err)
agentConn.Close()
time.Sleep(5 * time.Second)
continue
}
// Setup ssh tunnels to the remote host
quit := make(chan struct{})
err = setupTunnel(quit, client, Config.LocalNfsdPort, Config.RemoteNfsdPort)
if err != nil {
log.Println("Unable to create tunnel, trying again in five seconds", err)
client.Close()
agentConn.Close()
close(quit)
time.Sleep(5 * time.Second)
continue
}
err = setupTunnel(quit, client, Config.LocalMountdPort, Config.RemoteMountdPort)
if err != nil {
log.Println("Unable to create tunnel, trying again in five seconds", err)
client.Close()
agentConn.Close()
close(quit)
time.Sleep(5 * time.Second)
continue
}
if !nfsIsMounted() {
mountNfs()
}
// Create a channel we can select on for when the ssh connection closes
clientWaitChan := make(chan struct{})
go func() {
client.Wait()
close(clientWaitChan)
}()
go func() {
for {
waiter := time.After(1 * time.Second)
select {
case <-waiter:
case <-quit:
return
}
fmt.Printf("\r[METER] 10s avg. throughput: %s/second. 1min avg. throughput: %s/second.", Meter.GetHumanReadablePer10Seconds(), Meter.GetHumanReadablePerMinute())
}
}()
select {
case <-killSignal:
if nfsIsMounted() {
unmountNfs()
}
break mainLoop
case <-clientWaitChan:
log.Println("Ssh connection closed, restarting in five seconds")
agentConn.Close()
close(quit)
time.Sleep(5 * time.Second)
}
}
log.Fatalln("Interrupted! Unmounted the NFS and quiting..")
}