-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.go
161 lines (136 loc) · 3.64 KB
/
logging.go
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
package main
import (
"context"
"os"
"strings"
"time"
"github.com/lxc/lxd/lxd/db/operationtype"
"github.com/lxc/lxd/lxd/instance"
"github.com/lxc/lxd/lxd/instance/instancetype"
"github.com/lxc/lxd/lxd/operations"
"github.com/lxc/lxd/lxd/project"
"github.com/lxc/lxd/lxd/state"
"github.com/lxc/lxd/lxd/task"
"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/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 to start log expiry operation", logger.Ctx{"err": err})
return
}
logger.Info("Expiring log files")
err = op.Start()
if err != nil {
logger.Error("Failed to expire logs", logger.Ctx{"err": err})
}
_, _ = op.Wait(ctx)
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 shared.StringInSlice(fi.Name(), names) {
instDirEntries, err := os.ReadDir(shared.LogPath(fi.Name()))
if err != nil {
return err
}
for _, instDirEntry := range instDirEntries {
path := shared.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 := shared.LogPath(fi.Name())
newest := newestFile(path, fi)
if time.Since(newest).Hours() >= 24 {
err := os.RemoveAll(path)
if err != nil {
return err
}
}
}
}
return nil
}