-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.go
145 lines (123 loc) · 3.39 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
package main
import (
"context"
"io/ioutil"
"os"
"strings"
"time"
"github.com/lxc/lxd/lxd/db"
"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"
log "github.com/lxc/lxd/shared/log15"
)
// 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, db.OperationLogsExpire, nil, nil, opRun, nil, nil)
if err != nil {
logger.Error("Failed to start log expiry operation", log.Ctx{"err": err})
return
}
logger.Infof("Expiring log files")
_, err = op.Run()
if err != nil {
logger.Error("Failed to expire logs", log.Ctx{"err": err})
}
logger.Infof("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 := ioutil.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(), inst.Name()))
}
newestFile := func(path string, dir os.FileInfo) time.Time {
newest := dir.ModTime()
entries, err := ioutil.ReadDir(path)
if err != nil {
return newest
}
for _, entry := range entries {
if entry.ModTime().After(newest) {
newest = entry.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
}
// Check if the instance still exists.
if shared.StringInSlice(entry.Name(), names) {
instDirEntries, err := ioutil.ReadDir(shared.LogPath(entry.Name()))
if err != nil {
return err
}
for _, instDirEntry := range instDirEntries {
path := shared.LogPath(entry.Name(), instDirEntry.Name())
// Deal with directories (snapshots).
if instDirEntry.IsDir() {
newest := newestFile(path, instDirEntry)
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(instDirEntry.Name(), ".log") || strings.HasSuffix(instDirEntry.Name(), ".log.old") {
// Remove any log file which wasn't modified in the past 48 hours.
if time.Since(instDirEntry.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(entry.Name())
newest := newestFile(path, entry)
if time.Since(newest).Hours() >= 24 {
err := os.RemoveAll(path)
if err != nil {
return err
}
}
}
}
return nil
}