1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
package main
import (
"context"
"os"
"slices"
"strings"
"time"
"github.com/lxc/incus/v6/internal/server/db/operationtype"
"github.com/lxc/incus/v6/internal/server/instance"
"github.com/lxc/incus/v6/internal/server/instance/instancetype"
"github.com/lxc/incus/v6/internal/server/operations"
"github.com/lxc/incus/v6/internal/server/project"
"github.com/lxc/incus/v6/internal/server/state"
"github.com/lxc/incus/v6/internal/server/task"
internalUtil "github.com/lxc/incus/v6/internal/util"
"github.com/lxc/incus/v6/shared/logger"
)
// This task function expires logs when executed. It's started by the Daemon
// and will run once every 24h.
func expireLogsTask(state *state.State) (task.Func, task.Schedule) {
f := func(ctx context.Context) {
opRun := func(op *operations.Operation) error {
return expireLogs(ctx, state)
}
op, err := operations.OperationCreate(state, "", operations.OperationClassTask, operationtype.LogsExpire, nil, nil, opRun, nil, nil, nil)
if err != nil {
logger.Error("Failed creating log files expiry operation", logger.Ctx{"err": err})
return
}
logger.Info("Expiring log files")
err = op.Start()
if err != nil {
logger.Error("Failed starting log files expiry operation", logger.Ctx{"err": err})
return
}
err = op.Wait(ctx)
if err != nil {
logger.Error("Failed expiring log files", logger.Ctx{"err": err})
return
}
logger.Info("Done expiring log files")
}
return f, task.Daily()
}
func expireLogs(ctx context.Context, state *state.State) error {
// List the instances.
instances, err := instance.LoadNodeAll(state, instancetype.Any)
if err != nil {
return err
}
// List the directory.
entries, err := os.ReadDir(state.OS.LogDir)
if err != nil {
return err
}
// Build the expected names.
names := []string{}
for _, inst := range instances {
names = append(names, project.Instance(inst.Project().Name, inst.Name()))
}
newestFile := func(path string, dir os.FileInfo) time.Time {
newest := dir.ModTime()
entries, err := os.ReadDir(path)
if err != nil {
return newest
}
for _, entry := range entries {
info, err := entry.Info()
if err != nil {
continue
}
if info.ModTime().After(newest) {
newest = info.ModTime()
}
}
return newest
}
for _, entry := range entries {
// At each iteration we check if we got cancelled in the meantime.
select {
case <-ctx.Done():
return nil
default:
}
// We only care about instance directories.
if !entry.IsDir() {
continue
}
// Skip if we are unable to read the file info, e.g. the file might
// be deleted.
fi, err := entry.Info()
if err != nil {
continue
}
// Check if the instance still exists.
if slices.Contains(names, fi.Name()) {
instDirEntries, err := os.ReadDir(internalUtil.LogPath(fi.Name()))
if err != nil {
return err
}
for _, instDirEntry := range instDirEntries {
path := internalUtil.LogPath(fi.Name(), instDirEntry.Name())
instInfo, err := instDirEntry.Info()
if err != nil {
continue
}
// Deal with directories (snapshots).
if instInfo.IsDir() {
newest := newestFile(path, instInfo)
if time.Since(newest).Hours() >= 48 {
err := os.RemoveAll(path)
if err != nil {
return err
}
}
continue
}
// Only remove old log files (keep other files, such as conf, pid, monitor etc).
if strings.HasSuffix(instInfo.Name(), ".log") || strings.HasSuffix(instInfo.Name(), ".log.old") {
// Remove any log file which wasn't modified in the past 48 hours.
if time.Since(instInfo.ModTime()).Hours() >= 48 {
err := os.Remove(path)
if err != nil {
return err
}
}
}
}
} else {
// Empty directory if unchanged in the past 24 hours.
path := internalUtil.LogPath(fi.Name())
newest := newestFile(path, fi)
if time.Since(newest).Hours() >= 24 {
err := os.RemoveAll(path)
if err != nil {
return err
}
}
}
}
return nil
}
|