@@ -27,23 +27,32 @@ func DefaultBinExists() bool {
27
27
28
28
// CreateDefaultEnvFile creates the default environment file for gpud systemd service.
29
29
// Assume systemdctl is already installed, and runs on the linux system.
30
- func CreateDefaultEnvFile () error {
31
- return writeEnvFile (DefaultEnvFile )
30
+ func CreateDefaultEnvFile (endpoint string ) error {
31
+ return writeEnvFile (DefaultEnvFile , endpoint )
32
32
}
33
33
34
34
const defaultEnvFileContent = `# gpud environment variables are set here
35
35
FLAGS="--log-level=info --log-file=/var/log/gpud.log"
36
36
`
37
37
38
- func writeEnvFile (file string ) error {
38
+ func createDefaultEnvFileContent (endpoint string ) string {
39
+ if endpoint == "" {
40
+ return defaultEnvFileContent
41
+ }
42
+ return fmt .Sprintf (`# gpud environment variables are set here
43
+ FLAGS="--log-level=info --log-file=/var/log/gpud.log --endpoint=%s"
44
+ ` , endpoint )
45
+ }
46
+
47
+ func writeEnvFile (file string , endpoint string ) error {
39
48
if _ , err := os .Stat (file ); err == nil {
40
- return addLogFileFlagIfExists (file )
49
+ return updateFlagsFromExistingEnvFile (file , endpoint )
41
50
}
42
- return atomicfile .WriteFile (file , []byte (defaultEnvFileContent ), 0644 )
51
+ return atomicfile .WriteFile (file , []byte (createDefaultEnvFileContent ( endpoint ) ), 0644 )
43
52
}
44
53
45
- func addLogFileFlagIfExists (file string ) error {
46
- lines , err := processEnvFileLines (file )
54
+ func updateFlagsFromExistingEnvFile (file string , endpoint string ) error {
55
+ lines , err := processEnvFileLines (file , endpoint )
47
56
if err != nil {
48
57
return err
49
58
}
@@ -52,7 +61,7 @@ func addLogFileFlagIfExists(file string) error {
52
61
53
62
// processEnvFileLines reads all lines from the environment file and processes each line,
54
63
// adding the log-file flag to the FLAGS variable if it doesn't already exist.
55
- func processEnvFileLines (file string ) ([]string , error ) {
64
+ func processEnvFileLines (file string , endpoint string ) ([]string , error ) {
56
65
readFile , err := os .OpenFile (file , os .O_RDONLY , 0644 )
57
66
if err != nil {
58
67
return nil , err
@@ -70,15 +79,22 @@ func processEnvFileLines(file string) ([]string, error) {
70
79
continue
71
80
}
72
81
73
- // FLAGS already contains --log-file flag
74
- if strings .Contains (line , "--log-file=" ) {
82
+ // FLAGS already contains --log-file flag and --endpoint flag
83
+ if strings .Contains (line , "--log-file=" ) && ( endpoint != "" && strings . Contains ( line , "--endpoint=" )) {
75
84
lines = append (lines , line )
76
85
continue
77
86
}
78
87
79
88
// remove the trailing " character
80
89
line = strings .TrimSuffix (line , "\" " )
81
- line = fmt .Sprintf ("%s --log-file=/var/log/gpud.log\" " , line )
90
+
91
+ if ! strings .Contains (line , "--log-file=" ) {
92
+ line = fmt .Sprintf ("%s --log-file=/var/log/gpud.log\" " , line )
93
+ }
94
+
95
+ if endpoint != "" && ! strings .Contains (line , "--endpoint=" ) {
96
+ line = fmt .Sprintf ("%s --endpoint=%s\" " , line , endpoint )
97
+ }
82
98
83
99
lines = append (lines , line )
84
100
}
0 commit comments