-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathdefault.go
More file actions
158 lines (131 loc) · 3.84 KB
/
Copy pathdefault.go
File metadata and controls
158 lines (131 loc) · 3.84 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
package config
import (
"context"
"fmt"
stdos "os"
"path/filepath"
"strconv"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
nvidiacommon "github.com/leptonai/gpud/pkg/config/common"
)
const (
DefaultAPIVersion = "v1"
DefaultDataDir = "/var/lib/gpud"
DefaultGPUdPort = 15132
)
var (
DefaultRefreshPeriod = metav1.Duration{Duration: time.Minute}
// keep the metrics only for the last 3 hours
DefaultRetentionPeriod = metav1.Duration{Duration: 3 * time.Hour}
// compact/vacuum is disruptive to existing queries (including reads)
// but necessary to keep the state database from growing indefinitely
// TODO: disabled for now, until we have a better way to detect the performance issue
DefaultCompactPeriod = metav1.Duration{Duration: 0}
)
func DefaultConfig(ctx context.Context, opts ...OpOption) (*Config, error) {
options := &Op{}
if err := options.ApplyOpts(opts); err != nil {
return nil, err
}
dataDir, err := ResolveDataDir(options.DataDir)
if err != nil {
return nil, err
}
cfg := &Config{
APIVersion: DefaultAPIVersion,
Address: fmt.Sprintf(":%d", GPUdPortNumber()),
DataDir: dataDir,
RetentionPeriod: DefaultRetentionPeriod,
CompactPeriod: DefaultCompactPeriod,
Pprof: false,
EnableAutoUpdate: true,
NvidiaToolOverwrites: nvidiacommon.ToolOverwrites{
InfinibandClassRootDir: options.InfinibandClassRootDir,
},
FailureInjector: options.FailureInjector,
DBInMemory: options.DBInMemory,
SessionToken: options.SessionToken,
SessionMachineID: options.SessionMachineID,
SessionEndpoint: options.SessionEndpoint,
}
if cfg.State == "" {
cfg.State = StateFilePath(cfg.DataDir)
}
return cfg, nil
}
// ResolveDataDir resolves and validates a data directory path.
// If dataDir is empty or matches DefaultDataDir, it uses platform-specific logic:
// - For root users (or when /var/lib exists): /var/lib/gpud
// - For non-root users: $HOME/.gpud
//
// For non-empty custom paths, it ensures the directory exists and is writable.
// The directory is created with 0755 permissions if it doesn't exist.
func ResolveDataDir(dataDir string) (string, error) {
if dataDir == "" {
return setupDefaultDir()
}
if err := stdos.MkdirAll(dataDir, 0755); err != nil {
return "", err
}
return dataDir, nil
}
func setupDefaultDir() (string, error) {
asRoot := stdos.Geteuid() == 0 // running as root
d := DefaultDataDir
_, err := stdos.Stat(filepath.Dir(d))
if !asRoot || stdos.IsNotExist(err) {
homeDir, err := stdos.UserHomeDir()
if err != nil {
return "", err
}
d = filepath.Join(homeDir, ".gpud")
}
if _, err := stdos.Stat(d); stdos.IsNotExist(err) {
if err = stdos.MkdirAll(d, 0755); err != nil {
return "", err
}
}
return d, nil
}
func DefaultStateFile() (string, error) {
dir, err := ResolveDataDir("")
if err != nil {
return "", err
}
return StateFilePath(dir), nil
}
func DefaultFifoFile() (string, error) {
dir, err := ResolveDataDir("")
if err != nil {
return "", err
}
return FifoFilePath(dir), nil
}
// StateFilePath returns the state DB file path under the dataDir.
func StateFilePath(dataDir string) string {
return filepath.Join(dataDir, "gpud.state")
}
// FifoFilePath returns the FIFO pipe path under the dataDir.
func FifoFilePath(dataDir string) string {
return filepath.Join(dataDir, "gpud.fifo")
}
// PackagesDir returns the packages directory under the dataDir.
func PackagesDir(dataDir string) string {
return filepath.Join(dataDir, "packages")
}
// VersionFilePath returns the version file path under the dataDir.
func VersionFilePath(dataDir string) string {
return filepath.Join(dataDir, "target_version")
}
func GPUdPortNumber() int {
portStr, found := stdos.LookupEnv("GPUD_PORT")
if !found {
return DefaultGPUdPort
}
port, err := strconv.Atoi(portStr)
if err != nil {
return DefaultGPUdPort
}
return port
}