Keep deployment logs findable after the container is recreated (closes #214)
Deployment logs were stored under a directory named after the container's hostname, and the path was worked out again from the current hostname on download. Docker gives a recreated container a new hostname, so every older log download returned 404. New logs now go to `logs/<appname>/` with no hostname in the path. Logs written by older versions under an old hostname directory are still found by looking one directory deeper, inside the same confined log root. Tests cover both the hostname-free path and downloading an old-layout log. Model: opus-5-5
This commit was merged in pull request #218.
This commit is contained in:
@@ -6,9 +6,11 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -640,7 +642,7 @@ func (h *Handlers) HandleDeploymentLogDownload() http.HandlerFunc {
|
||||
}
|
||||
defer func() { _ = root.Close() }()
|
||||
|
||||
file, openErr := root.Open(relPath)
|
||||
file, openErr := openDeploymentLog(root, relPath)
|
||||
if openErr != nil {
|
||||
http.NotFound(writer, request)
|
||||
|
||||
@@ -665,6 +667,24 @@ func (h *Handlers) HandleDeploymentLogDownload() http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// openDeploymentLog opens a deployment log file inside the log root.
|
||||
// Logs written by older versions sit one directory deeper, under the
|
||||
// hostname of the container that wrote them, so when the file is not at
|
||||
// relPath it is looked for under any directory directly below the root.
|
||||
func openDeploymentLog(root *os.Root, relPath string) (*os.File, error) {
|
||||
file, err := root.Open(relPath)
|
||||
if err == nil {
|
||||
return file, nil
|
||||
}
|
||||
|
||||
matches, globErr := fs.Glob(root.FS(), path.Join("*", filepath.ToSlash(relPath)))
|
||||
if globErr != nil || len(matches) == 0 {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return root.Open(matches[0])
|
||||
}
|
||||
|
||||
// containerLogsAPITail is the default number of log lines for the container logs API.
|
||||
const containerLogsAPITail = "100"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user