Skip to content

Commit

Permalink
fix: review
Browse files Browse the repository at this point in the history
Signed-off-by: Toma Puljak <[email protected]>
  • Loading branch information
Tpuljak committed Jan 20, 2025
1 parent 134f14b commit 4359f9d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 26 deletions.
7 changes: 4 additions & 3 deletions pkg/agent/toolbox/process/session/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"fmt"
"net/http"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -47,13 +46,15 @@ func SessionExecuteCommand(configDir string) func(c *gin.Context) {
}
session.commands[*cmdId] = command

err := os.MkdirAll(filepath.Join(configDir, "sessions", sessionId, *cmdId), 0755)
logFilePath := command.LogFilePath(session.Dir(configDir))

err := os.MkdirAll(logFilePath, 0755)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}

logFile, err = os.Create(filepath.Join(configDir, "sessions", sessionId, *cmdId, "output.log"))
logFile, err = os.Create(logFilePath)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
Expand Down
7 changes: 3 additions & 4 deletions pkg/agent/toolbox/process/session/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"errors"
"net/http"
"os"
"path/filepath"

"github.com/daytonaio/daytona/internal/util"
"github.com/daytonaio/daytona/pkg/api/controllers/log"
Expand All @@ -20,19 +19,19 @@ func GetSessionCommandLogs(configDir string) func(c *gin.Context) {
sessionId := c.Param("sessionId")
cmdId := c.Param("commandId")

_, ok := sessions[sessionId]
session, ok := sessions[sessionId]
if !ok {
c.AbortWithError(http.StatusNotFound, errors.New("session not found"))
return
}

_, ok = sessions[sessionId].commands[cmdId]
command, ok := sessions[sessionId].commands[cmdId]
if !ok {
c.AbortWithError(http.StatusNotFound, errors.New("command not found"))
return
}

path := filepath.Join(configDir, "sessions", sessionId, cmdId, "output.log")
path := command.LogFilePath(session.Dir(configDir))

if c.Request.Header.Get("Upgrade") == "websocket" {
logFile, err := os.Open(path)
Expand Down
19 changes: 10 additions & 9 deletions pkg/agent/toolbox/process/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"net/http"
"os"
"os/exec"
"path/filepath"
"slices"

"github.com/daytonaio/daytona/pkg/common"
Expand Down Expand Up @@ -48,17 +47,19 @@ func CreateSession(projectDir, configDir string) func(c *gin.Context) {
return
}

err = os.MkdirAll(filepath.Join(configDir, "sessions", request.SessionId), 0755)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}

sessions[request.SessionId] = &session{
session := &session{
id: request.SessionId,
cmd: cmd,
stdinWriter: stdinWriter,
commands: map[string]*Command{},
}
sessions[request.SessionId] = session

err = os.MkdirAll(session.Dir(configDir), 0755)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}

c.Status(http.StatusCreated)
}
Expand All @@ -82,7 +83,7 @@ func DeleteSession(configDir string) func(c *gin.Context) {

delete(sessions, sessionId)

err = os.RemoveAll(filepath.Join(configDir, "sessions", sessionId))
err = os.RemoveAll(session.Dir(configDir))
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
Expand Down
10 changes: 10 additions & 0 deletions pkg/agent/toolbox/process/session/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package session
import (
"io"
"os/exec"
"path/filepath"
)

type CreateSessionRequest struct {
Expand All @@ -29,13 +30,22 @@ type Session struct {
} // @name Session

type session struct {
id string
cmd *exec.Cmd
stdinWriter io.Writer
commands map[string]*Command
}

func (s *session) Dir(configDir string) string {
return filepath.Join(configDir, "sessions", s.id)
}

type Command struct {
Id string `json:"id" validate:"required"`
Command string `json:"command" validate:"required"`
ExitCode *int `json:"exitCode,omitempty" validate:"optional"`
} // @name Command

func (c *Command) LogFilePath(sessionDir string) string {
return filepath.Join(sessionDir, c.Id, "output.log")
}
20 changes: 10 additions & 10 deletions pkg/api/controllers/log/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var upgrader = websocket.Upgrader{
},
}

func WriteToWs(ws *websocket.Conn, c chan []byte, errChan chan error) {
func writeToWs(ws *websocket.Conn, c chan []byte, errChan chan error) {
for {
err := ws.WriteMessage(websocket.TextMessage, <-c)
if err != nil {
Expand All @@ -35,7 +35,7 @@ func WriteToWs(ws *websocket.Conn, c chan []byte, errChan chan error) {
}
}

func WriteJSONToWs(ws *websocket.Conn, c chan interface{}, errChan chan error) {
func writeJSONToWs(ws *websocket.Conn, c chan interface{}, errChan chan error) {
for {
err := ws.WriteJSON(<-c)
if err != nil {
Expand Down Expand Up @@ -123,7 +123,7 @@ func ReadServerLog(ginCtx *gin.Context) {
return
}
if err == nil {
ReadLog(ginCtx, reader, util.ReadLog, WriteToWs)
ReadLog(ginCtx, reader, util.ReadLog, writeToWs)
return
}
time.Sleep(TIMEOUT)
Expand All @@ -140,7 +140,7 @@ func ReadServerLog(ginCtx *gin.Context) {
return
}

ReadLog(ginCtx, reader, util.ReadLog, WriteToWs)
ReadLog(ginCtx, reader, util.ReadLog, writeToWs)
}

func ReadWorkspaceLog(ginCtx *gin.Context) {
Expand All @@ -154,7 +154,7 @@ func ReadWorkspaceLog(ginCtx *gin.Context) {
for {
wsLogReader, err := server.WorkspaceService.GetWorkspaceLogReader(workspaceId)
if err == nil {
ReadLog(ginCtx, wsLogReader, util.ReadJSONLog, WriteJSONToWs)
ReadLog(ginCtx, wsLogReader, util.ReadJSONLog, writeJSONToWs)
return
}
time.Sleep(TIMEOUT)
Expand All @@ -167,7 +167,7 @@ func ReadWorkspaceLog(ginCtx *gin.Context) {
return
}

ReadLog(ginCtx, wsLogReader, util.ReadJSONLog, WriteJSONToWs)
ReadLog(ginCtx, wsLogReader, util.ReadJSONLog, writeJSONToWs)
}

func ReadProjectLog(ginCtx *gin.Context) {
Expand All @@ -182,7 +182,7 @@ func ReadProjectLog(ginCtx *gin.Context) {
for {
projectLogReader, err := server.WorkspaceService.GetProjectLogReader(workspaceId, projectName)
if err == nil {
ReadLog(ginCtx, projectLogReader, util.ReadJSONLog, WriteJSONToWs)
ReadLog(ginCtx, projectLogReader, util.ReadJSONLog, writeJSONToWs)
return
}
time.Sleep(TIMEOUT)
Expand All @@ -195,7 +195,7 @@ func ReadProjectLog(ginCtx *gin.Context) {
return
}

ReadLog(ginCtx, projectLogReader, util.ReadJSONLog, WriteJSONToWs)
ReadLog(ginCtx, projectLogReader, util.ReadJSONLog, writeJSONToWs)
}

func ReadBuildLog(ginCtx *gin.Context) {
Expand All @@ -210,7 +210,7 @@ func ReadBuildLog(ginCtx *gin.Context) {
buildLogReader, err := server.BuildService.GetBuildLogReader(buildId)

if err == nil {
ReadLog(ginCtx, buildLogReader, util.ReadJSONLog, WriteJSONToWs)
ReadLog(ginCtx, buildLogReader, util.ReadJSONLog, writeJSONToWs)
return
}
time.Sleep(TIMEOUT)
Expand All @@ -223,5 +223,5 @@ func ReadBuildLog(ginCtx *gin.Context) {
return
}

ReadLog(ginCtx, buildLogReader, util.ReadJSONLog, WriteJSONToWs)
ReadLog(ginCtx, buildLogReader, util.ReadJSONLog, writeJSONToWs)
}

0 comments on commit 4359f9d

Please sign in to comment.