-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
55 lines (45 loc) · 998 Bytes
/
server.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
package main
import (
"bufio"
"bytes"
"io"
"os"
"strings"
)
type Server struct {
Name string
Devices []GPU
}
func ReadDefaultSSHConfig() (io.Reader, error) {
read, err := os.ReadFile(os.Getenv("HOME") + "/.ssh/config")
if err != nil {
return nil, err
}
return bytes.NewReader(read), nil
}
func GetHosts(sshConfigReader io.Reader) ([]string, error) {
// Read the hosts.
var hosts []string
scanner := bufio.NewScanner(sshConfigReader)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if strings.HasPrefix(line, "Host ") {
hosts = append(hosts, strings.TrimSpace(strings.TrimPrefix(line, "Host ")))
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return hosts, nil
}
func GetServers(info map[string]string) []Server {
var servers []Server
for host, gpuInfo := range info {
server := Server{
Name: host,
Devices: ExtractGPUInfo(strings.TrimSpace(gpuInfo)),
}
servers = append(servers, server)
}
return servers
}