-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
164 lines (140 loc) · 4.07 KB
/
Copy pathmain.go
File metadata and controls
164 lines (140 loc) · 4.07 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"runtime"
"syscall"
"time"
"github.com/rmcluster/linux-client/fscas"
"github.com/rmcluster/linux-client/openapi"
)
// time to wait after failed announcement
const retrySleep = time.Second
func getDataPath() string {
// Check environment variable first
if envPath := os.Getenv("RMCLUSTER_CLIENT_DATA_DIR"); envPath != "" {
return envPath
}
// Fallback based on OS
home, err := os.UserHomeDir()
if err != nil {
// Cannot determine a sane fallback
return ""
}
switch runtime.GOOS {
case "windows":
// Use %LOCALAPPDATA% on Windows
localAppData := os.Getenv("LOCALAPPDATA")
if localAppData == "" {
localAppData = filepath.Join(home, "AppData", "Local")
}
return filepath.Join(localAppData, "rmcluster-client")
default:
// Use XDG_DATA_HOME if present, otherwise ~/.local/share
if xdgDataHome := os.Getenv("XDG_DATA_HOME"); xdgDataHome != "" {
return filepath.Join(xdgDataHome, "rmcluster-client")
}
return filepath.Join(home, ".local", "share", "rmcluster-client")
}
}
func main() {
id := flag.String("id", "", "the id of the node")
tracker := flag.String("tracker", "127.0.0.1:4917", "ip:port of the tracker")
rpcPort := flag.Int("port", 1984, "port to run the RPC server on")
dataPath := flag.String("data-path", getDataPath(), "path to the data directory. CAS storage will be placed under 'storage' subdirectory. If empty, CAS is disabled.")
casPort := flag.Int("cas-port", 1985, "port to run the CAS server on")
rpcCommand := flag.String("cmd", "rpc-server", "command to run the RPC server")
nickname := flag.String("nickname", "", "nickname for the node")
flag.Parse()
if *id == "" {
config, err := LoadOrCreateConfig()
if err != nil {
log.Fatalf("Failed to load or create config: %v", err)
}
*id = config.ID
log.Printf("Generated new ID: %s", *id)
}
args := []string{
"--port", fmt.Sprint(*rpcPort),
}
args = append(args, flag.Args()...)
// start RPC server
cmd := exec.Command(*rpcCommand, args...)
cmd.Stdin = nil
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
panic(err)
}
// print command
log.Printf("Running command: %s %v\n", *rpcCommand, args)
if *dataPath != "" {
// start CAS server
log.Printf("Data directory: %s", *dataPath)
casStoragePath := *dataPath + "/storage"
cas := fscas.NewCAS(casStoragePath)
go func() {
log.Printf("Starting CAS server on %s", fmt.Sprintf("0.0.0.0:%d", *casPort))
if err := openapi.NewRouter(cas).Run(fmt.Sprintf("0.0.0.0:%d", *casPort)); err != nil {
log.Fatal(err)
}
}()
} else {
log.Println("CAS is disabled (no data directory specified)")
}
// start announcement loop
go func() {
query := make(url.Values)
query.Add("id", *id)
query.Add("port", fmt.Sprint(*rpcPort))
query.Add("max_size", fmt.Sprint(getTotalMemoryBytes()))
if *dataPath != "" {
query.Add("storage_port", fmt.Sprint(*casPort))
}
if *nickname != "" {
query.Add("nickname", fmt.Sprintf(*nickname))
}
announceUrl := url.URL{
Scheme: "http",
Host: *tracker,
Path: "/announce",
RawQuery: query.Encode(),
}
for {
// send announce request
log.Printf("Announcing: %s", announceUrl.String())
resp, err := http.Get(announceUrl.String())
if err != nil {
log.Printf("Failed to announce to tracker: %v\n", err)
time.Sleep(retrySleep)
continue
}
// parse response
var response announcementResponse
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
log.Printf("Failed to parse announcement response: %v\n", err)
time.Sleep(retrySleep)
continue
}
log.Printf("Announced to server, reannouncing in %v seconds\n", response.Interval)
// wait for next announcement time
time.Sleep(time.Duration(response.Interval * float64(time.Second)))
}
}()
cmd.Wait()
}
type announcementResponse struct {
Interval float64 `json:"interval"`
}
func getTotalMemoryBytes() uint64 {
var sysinfo syscall.Sysinfo_t
syscall.Sysinfo(&sysinfo)
return sysinfo.Totalram
}