94 lines
2.8 KiB
Go
94 lines
2.8 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
func (m *storageManager) inventory(ctx context.Context) (storageInventory, error) {
|
|
config, err := m.inventoryConfig(ctx)
|
|
if err != nil {
|
|
return storageInventory{}, err
|
|
}
|
|
scan := m.scan
|
|
if scan == nil {
|
|
scan = inspectStorage
|
|
}
|
|
inventory, err := scan(ctx, config)
|
|
if err != nil {
|
|
return inventory, err
|
|
}
|
|
// Keep discovered repository identities after deleting the last referencing
|
|
// run/worktree; otherwise the next total could silently lose sibling trees.
|
|
var observed []string
|
|
for _, tree := range inventory.Worktrees {
|
|
observed = append(observed, tree.Path, tree.Repository)
|
|
}
|
|
if len(observed) > 0 && ctx.Err() == nil {
|
|
if err := m.store.RememberStorageWorkspaces(ctx, observed); err != nil {
|
|
return inventory, err
|
|
}
|
|
}
|
|
return inventory, nil
|
|
}
|
|
func (m *storageManager) inventoryConfig(ctx context.Context) (storageInventoryConfig, error) {
|
|
config := storageInventoryConfig{DataRoot: m.store.Directory(), ArtifactsRoot: m.runner.artifactsRoot}
|
|
// The database path is obtained from the store, never supplied by a client.
|
|
config.DatabasePath = m.store.DatabasePath()
|
|
var err error
|
|
if m.roots != nil {
|
|
config = *m.roots
|
|
config.DatabasePath = m.store.DatabasePath()
|
|
config.DataRoot = m.store.Directory()
|
|
} else {
|
|
cache, cacheErr := os.UserCacheDir()
|
|
if cacheErr != nil {
|
|
return config, cacheErr
|
|
}
|
|
config.SessionsRoot = filepath.Join(cache, "pipelines", "pi-sessions")
|
|
config.AttachmentsRoot, err = chatAttachmentDir()
|
|
if err != nil {
|
|
return config, err
|
|
}
|
|
}
|
|
config.ProtectedWorkspaces = append(config.ProtectedWorkspaces, config.DataRoot, config.ArtifactsRoot, config.AttachmentsRoot, config.SessionsRoot)
|
|
runs, err := m.store.ListRuns(ctx)
|
|
if err != nil {
|
|
return config, err
|
|
}
|
|
for _, run := range runs {
|
|
config.WorkspaceRoots = append(config.WorkspaceRoots, run.Workspace)
|
|
if !storageTerminal(run.Status) || run.CleanupUnconfirmed {
|
|
config.ProtectedWorkspaces = append(config.ProtectedWorkspaces, run.Workspace)
|
|
}
|
|
}
|
|
directories, err := m.store.StorageOperatorDirectories(ctx)
|
|
if err != nil {
|
|
return config, err
|
|
}
|
|
config.WorkspaceRoots = append(config.WorkspaceRoots, directories...)
|
|
config.ProtectedWorkspaces = append(config.ProtectedWorkspaces, directories...)
|
|
if err := m.store.RememberStorageWorkspaces(ctx, config.WorkspaceRoots); err != nil {
|
|
return config, err
|
|
}
|
|
remembered, err := m.store.StorageWorkspacePaths(ctx)
|
|
if err != nil {
|
|
return config, err
|
|
}
|
|
config.WorkspaceRoots = append(config.WorkspaceRoots, remembered...)
|
|
seen := map[string]bool{}
|
|
unique := config.WorkspaceRoots[:0]
|
|
for _, path := range config.WorkspaceRoots {
|
|
if path != "" {
|
|
path = filepath.Clean(path)
|
|
if !seen[path] {
|
|
unique = append(unique, path)
|
|
seen[path] = true
|
|
}
|
|
}
|
|
}
|
|
config.WorkspaceRoots = unique
|
|
return config, nil
|
|
}
|