Path: blob/main/components/content-service/pkg/logs/logs.go
2500 views
// Copyright (c) 2021 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package logs56import (7"context"8"errors"9"fmt"10"io/fs"11"os"12"path/filepath"13"strings"1415"golang.org/x/xerrors"16)1718const (19// TerminalStoreLocation is the path in the workspace where terminal related data like logs or hist-files are20// persisted in the file system21TerminalStoreLocation = "/workspace/.gitpod"2223prebuildLogFilePrefix = "prebuild-log-"2425legacyTerminalStoreLocation = "/workspace"26legacyPrebuildLogFilePrefix = ".prebuild-log-"2728// UploadedHeadlessLogPathPrefix is the prefix under which headless logs are stored inside an instance29UploadedHeadlessLogPathPrefix = "logs"30)3132// UploadedHeadlessLogPath returns the path relative to the workspace instance33func UploadedHeadlessLogPath(taskID string) string {34return fmt.Sprintf("%s/%s", UploadedHeadlessLogPathPrefix, taskID)35}3637// PrebuildLogFileName is the absolute path to the file containing the output of the prebuild log for the given task in recent workspaces38func PrebuildLogFileName(storeLocation string, taskId string) string {39return storeLocation + "/" + prebuildLogFilePrefix + taskId40}4142// LegacyPrebuildLogFileName is the absolute path to the file containing the output of the prebuild log for the given43// task in older workspaces44func LegacyPrebuildLogFileName(taskId string) string {45return legacyTerminalStoreLocation + "/" + legacyPrebuildLogFilePrefix + taskId46}4748// ListPrebuildLogFiles lists all log files in the workspace. Location is assumed to be the base dir of the workspace session49func ListPrebuildLogFiles(ctx context.Context, location string) (filePaths []string, err error) {50listLogFiles := func(relativeLocation, prefix string) (logFiles []string, errr error) {51absDirPath := filepath.Join(location, relativeLocation)52files, err := os.ReadDir(absDirPath)53if err != nil && !errors.Is(err, fs.ErrNotExist) {54return nil, err55}5657for _, file := range files {58filename := file.Name()59if strings.HasPrefix(filename, prefix) {60absPath := filepath.Join(absDirPath, filename)61logFiles = append(logFiles, absPath)62}63}64return logFiles, nil65}66// list log files in `location` first67filePaths, err = listLogFiles("", prebuildLogFilePrefix)68if err != nil {69return nil, err70}71if len(filePaths) == 0 {72filePaths, err = listLogFiles(strings.TrimPrefix(TerminalStoreLocation, "/workspace"), prebuildLogFilePrefix)73if err != nil {74return nil, err75}76}77if len(filePaths) == 0 {78filePaths, err = listLogFiles(strings.TrimPrefix(legacyTerminalStoreLocation, "/workspace"), legacyPrebuildLogFilePrefix)79if err != nil {80return nil, err81}82}83return filePaths, nil84}8586// ParseTaskIDFromPrebuildLogFilePath tries to parse the streamID from the given file name path87func ParseTaskIDFromPrebuildLogFilePath(filePath string) (string, error) {88fileName := filepath.Base(filePath)8990var streamID string91if strings.HasPrefix(fileName, legacyPrebuildLogFilePrefix) {92streamID = strings.TrimPrefix(fileName, legacyPrebuildLogFilePrefix)93} else if strings.HasPrefix(fileName, prebuildLogFilePrefix) {94streamID = strings.TrimPrefix(fileName, prebuildLogFilePrefix)95}96if streamID == "" {97return "", xerrors.Errorf("cannot parse stream ID from filePath: '%s'", fileName)98}99100return streamID, nil101}102103104