-
Notifications
You must be signed in to change notification settings - Fork 317
/
workspace.go
221 lines (182 loc) · 4.72 KB
/
workspace.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package agent
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/loft-sh/devpod/pkg/command"
"github.com/loft-sh/devpod/pkg/config"
"github.com/mitchellh/go-homedir"
)
var extraSearchLocations = []string{"/home/devpod/.devpod/agent", "/opt/devpod/agent", "/var/lib/devpod/agent", "/var/devpod/agent"}
var ErrFindAgentHomeFolder = fmt.Errorf("couldn't find devpod home directory")
func GetAgentDaemonLogFolder(agentFolder string) (string, error) {
return FindAgentHomeFolder(agentFolder)
}
func findDir(agentFolder string, validate func(path string) bool) string {
// get agent folder
if agentFolder != "" {
if !validate(agentFolder) {
return ""
}
return agentFolder
}
// check environment
homeFolder := os.Getenv(config.DEVPOD_HOME)
if homeFolder != "" {
homeFolder = filepath.Join(homeFolder, "agent")
if !validate(homeFolder) {
return ""
}
return homeFolder
}
// check home folder first
homeDir, _ := homedir.Dir()
if homeDir != "" {
homeDir = filepath.Join(homeDir, ".devpod", "agent")
if validate(homeDir) {
return homeDir
}
}
// check root folder
homeDir, _ = command.GetHome("root")
if homeDir != "" {
homeDir = filepath.Join(homeDir, ".devpod", "agent")
if validate(homeDir) {
return homeDir
}
}
// check current directory
execDir, _ := os.Executable()
if execDir != "" {
execDir = filepath.Join(filepath.Dir(execDir), "agent")
if validate(execDir) {
return execDir
}
}
// check other folders
for _, dir := range extraSearchLocations {
if validate(dir) {
return dir
}
}
return ""
}
func FindAgentHomeFolder(agentFolder string) (string, error) {
homeDir := findDir(agentFolder, isDevPodHome)
if homeDir != "" {
return homeDir, nil
}
return "", ErrFindAgentHomeFolder
}
func isDevPodHome(dir string) bool {
_, err := os.Stat(filepath.Join(dir, "contexts"))
return err == nil
}
func PrepareAgentHomeFolder(agentFolder string) (string, error) {
// try to find agent home folder first
homeFolder, err := FindAgentHomeFolder(agentFolder)
if err == nil {
return homeFolder, nil
}
// try to find an executable directory
homeDir := findDir(agentFolder, func(path string) bool {
ok, _ := isDirExecutable(path)
return ok
})
if homeDir != "" {
return homeDir, nil
}
// check if agentFolder is set
if agentFolder != "" {
_, err := isDirExecutable(agentFolder)
return "", err
}
// return generic error here
return "", fmt.Errorf("couldn't find an executable directory")
}
func isDirExecutable(dir string) (bool, error) {
if !filepath.IsAbs(dir) {
var err error
dir, err = filepath.Abs(dir)
if err != nil {
return false, err
}
}
err := os.MkdirAll(dir, 0777)
if err != nil {
return false, err
}
testFile := filepath.Join(dir, "devpod_test.sh")
err = os.WriteFile(testFile, []byte(`#!/bin/sh
echo DevPod
`), 0755)
if err != nil {
return false, err
}
defer os.Remove(testFile)
if runtime.GOOS != "linux" {
return true, nil
}
// try to execute
out, err := exec.Command(testFile).Output()
if err != nil {
return false, err
} else if strings.TrimSpace(string(out)) != "DevPod" {
return false, fmt.Errorf("received %s, expected DevPod", strings.TrimSpace(string(out)))
}
return true, nil
}
func GetAgentWorkspaceContentDir(workspaceDir string) string {
return filepath.Join(workspaceDir, "content")
}
func GetAgentBinariesDir(agentFolder, context, workspaceID string) (string, error) {
homeFolder, err := FindAgentHomeFolder(agentFolder)
if err != nil {
return "", err
}
if context == "" {
context = config.DefaultContext
}
// workspace folder
workspaceDir := filepath.Join(homeFolder, "contexts", context, "workspaces", workspaceID)
// check if it already exists
_, err = os.Stat(workspaceDir)
if err == nil {
return filepath.Join(workspaceDir, "binaries"), nil
}
return "", os.ErrNotExist
}
func GetAgentWorkspaceDir(agentFolder, context, workspaceID string) (string, error) {
homeFolder, err := FindAgentHomeFolder(agentFolder)
if err != nil {
return "", err
}
if context == "" {
context = config.DefaultContext
}
// workspace folder
workspaceDir := filepath.Join(homeFolder, "contexts", context, "workspaces", workspaceID)
// check if it already exists
_, err = os.Stat(workspaceDir)
if err == nil {
return workspaceDir, nil
}
return "", os.ErrNotExist
}
func CreateAgentWorkspaceDir(agentFolder, context, workspaceID string) (string, error) {
homeFolder, err := PrepareAgentHomeFolder(agentFolder)
if err != nil {
return "", err
}
// workspace folder
workspaceDir := filepath.Join(homeFolder, "contexts", context, "workspaces", workspaceID)
// create workspace folder
err = os.MkdirAll(workspaceDir, 0755)
if err != nil {
return "", err
}
return workspaceDir, nil
}