Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/content-service/pkg/logs/logs.go
2500 views
1
// Copyright (c) 2021 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package logs
6
7
import (
8
"context"
9
"errors"
10
"fmt"
11
"io/fs"
12
"os"
13
"path/filepath"
14
"strings"
15
16
"golang.org/x/xerrors"
17
)
18
19
const (
20
// TerminalStoreLocation is the path in the workspace where terminal related data like logs or hist-files are
21
// persisted in the file system
22
TerminalStoreLocation = "/workspace/.gitpod"
23
24
prebuildLogFilePrefix = "prebuild-log-"
25
26
legacyTerminalStoreLocation = "/workspace"
27
legacyPrebuildLogFilePrefix = ".prebuild-log-"
28
29
// UploadedHeadlessLogPathPrefix is the prefix under which headless logs are stored inside an instance
30
UploadedHeadlessLogPathPrefix = "logs"
31
)
32
33
// UploadedHeadlessLogPath returns the path relative to the workspace instance
34
func UploadedHeadlessLogPath(taskID string) string {
35
return fmt.Sprintf("%s/%s", UploadedHeadlessLogPathPrefix, taskID)
36
}
37
38
// PrebuildLogFileName is the absolute path to the file containing the output of the prebuild log for the given task in recent workspaces
39
func PrebuildLogFileName(storeLocation string, taskId string) string {
40
return storeLocation + "/" + prebuildLogFilePrefix + taskId
41
}
42
43
// LegacyPrebuildLogFileName is the absolute path to the file containing the output of the prebuild log for the given
44
// task in older workspaces
45
func LegacyPrebuildLogFileName(taskId string) string {
46
return legacyTerminalStoreLocation + "/" + legacyPrebuildLogFilePrefix + taskId
47
}
48
49
// ListPrebuildLogFiles lists all log files in the workspace. Location is assumed to be the base dir of the workspace session
50
func ListPrebuildLogFiles(ctx context.Context, location string) (filePaths []string, err error) {
51
listLogFiles := func(relativeLocation, prefix string) (logFiles []string, errr error) {
52
absDirPath := filepath.Join(location, relativeLocation)
53
files, err := os.ReadDir(absDirPath)
54
if err != nil && !errors.Is(err, fs.ErrNotExist) {
55
return nil, err
56
}
57
58
for _, file := range files {
59
filename := file.Name()
60
if strings.HasPrefix(filename, prefix) {
61
absPath := filepath.Join(absDirPath, filename)
62
logFiles = append(logFiles, absPath)
63
}
64
}
65
return logFiles, nil
66
}
67
// list log files in `location` first
68
filePaths, err = listLogFiles("", prebuildLogFilePrefix)
69
if err != nil {
70
return nil, err
71
}
72
if len(filePaths) == 0 {
73
filePaths, err = listLogFiles(strings.TrimPrefix(TerminalStoreLocation, "/workspace"), prebuildLogFilePrefix)
74
if err != nil {
75
return nil, err
76
}
77
}
78
if len(filePaths) == 0 {
79
filePaths, err = listLogFiles(strings.TrimPrefix(legacyTerminalStoreLocation, "/workspace"), legacyPrebuildLogFilePrefix)
80
if err != nil {
81
return nil, err
82
}
83
}
84
return filePaths, nil
85
}
86
87
// ParseTaskIDFromPrebuildLogFilePath tries to parse the streamID from the given file name path
88
func ParseTaskIDFromPrebuildLogFilePath(filePath string) (string, error) {
89
fileName := filepath.Base(filePath)
90
91
var streamID string
92
if strings.HasPrefix(fileName, legacyPrebuildLogFilePrefix) {
93
streamID = strings.TrimPrefix(fileName, legacyPrebuildLogFilePrefix)
94
} else if strings.HasPrefix(fileName, prebuildLogFilePrefix) {
95
streamID = strings.TrimPrefix(fileName, prebuildLogFilePrefix)
96
}
97
if streamID == "" {
98
return "", xerrors.Errorf("cannot parse stream ID from filePath: '%s'", fileName)
99
}
100
101
return streamID, nil
102
}
103
104