-
Notifications
You must be signed in to change notification settings - Fork 6
/
core_delete.go
95 lines (86 loc) · 2.29 KB
/
core_delete.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
package object
import (
"context"
"os"
"path/filepath"
"github.com/opensvc/om3/core/actioncontext"
"github.com/opensvc/om3/util/file"
)
// DeleteSection deletes a resource section from object.
//
// TODO: Fix/verify following doc
// If no resource selector is set, remove all etc, var and log
// file belonging to the object.
//
// If a resource selector is set, only delete the corresponding
// sections in the configuration file.
func (t *core) DeleteSection(ctx context.Context, rids ...string) error {
ctx = actioncontext.WithProps(ctx, actioncontext.Delete)
unlock, err := t.lockAction(ctx)
if err != nil {
return err
}
defer unlock()
return t.config.DeleteSections(rids...)
}
func (t *core) Delete(ctx context.Context) error {
ctx = actioncontext.WithProps(ctx, actioncontext.Delete)
unlock, err := t.lockAction(ctx)
if err != nil {
return err
}
defer unlock()
return t.deleteInstance()
}
func (t *core) deleteInstance() error {
if err := t.deleteInstanceFiles(); err != nil {
return err
}
if err := t.deleteInstanceLogs(); err != nil {
return err
}
if err := t.setPurgeCollectorTag(); err != nil {
t.log.Warnf("%s", err)
return nil
}
return nil
}
func (t *core) deleteInstanceFiles() error {
patterns := []string{
filepath.Join(t.logDir(), t.path.Name+".log*"),
filepath.Join(t.logDir(), t.path.Name+".debug.log*"),
filepath.Join(t.logDir(), "."+t.path.Name+".log*"),
filepath.Join(t.logDir(), "."+t.path.Name+".debug.log*"),
filepath.Join(t.varDir()),
}
for _, pattern := range patterns {
matches, err := filepath.Glob(pattern)
if err != nil {
t.log.Attr("pattern", pattern).Warnf("expand glob for delete: %s", err)
continue
}
for _, fpath := range matches {
_ = t.tryDeleteInstanceFile(fpath)
}
}
t.tryDeleteInstanceFile(t.ConfigFile())
return nil
}
func (t *core) tryDeleteInstanceFile(fpath string) bool {
if file.IsProtected(fpath) {
t.log.Attr("path", fpath).Warnf("block attempt to remove the protected file %s", fpath)
return false
}
if err := os.RemoveAll(fpath); err != nil {
t.log.Attr("path", fpath).Warnf("removing %s: %s", fpath, err)
return false
}
t.log.Attr("path", fpath).Infof("removed %s", fpath)
return true
}
func (t *core) deleteInstanceLogs() error {
return nil
}
func (t *core) setPurgeCollectorTag() error {
return nil
}