forked from Symantec/Dominator
/
api.go
113 lines (98 loc) · 3.92 KB
/
api.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
/*
Package filegen manages the generation of computed files.
Package filegen may be used to implement a computed file server. It
registers a FileGenerator server with the lib/srpc package. The application
may register multiple file generators.
A generator for the /etc/mdb.json pathname is automatically registered.
*/
package filegen
import (
"io"
"sync"
"time"
"github.com/Symantec/Dominator/lib/hash"
"github.com/Symantec/Dominator/lib/log"
"github.com/Symantec/Dominator/lib/mdb"
"github.com/Symantec/Dominator/lib/objectserver/memory"
proto "github.com/Symantec/Dominator/proto/filegenerator"
)
// FileGenerator is the interface that wraps the Generate method.
//
// Generate computes file data from the provided machine information.
// The logger may be used to log problems.
// It returns the data, a time.Time indicating when the data are valid until
// (a zero time indicates the data are valid forever) and an error.
type FileGenerator interface {
Generate(machine mdb.Machine, logger log.Logger) (
data []byte, validUntil time.Time, err error)
}
type expiringHash struct {
hash hash.Hash
length uint64
validUntil time.Time
}
type pathManager struct {
generator hashGenerator
rwMutex sync.RWMutex
// Protected by lock.
machineHashes map[string]expiringHash
}
type Manager struct {
rwMutex sync.RWMutex
// Protected by lock.
pathManagers map[string]*pathManager
machineData map[string]mdb.Machine
clients map[<-chan *proto.ServerMessage]chan<- *proto.ServerMessage
// Not protected by lock.
objectServer *memory.ObjectServer
logger log.Logger
}
// New creates a new *Manager. Only one should be created per application.
// The logger will be used to log problems.
func New(logger log.Logger) *Manager {
return newManager(logger)
}
// GetRegisteredPaths returns a slice of filenames which have generators.
func (m *Manager) GetRegisteredPaths() []string {
return m.getRegisteredPaths()
}
// RegisterFileForPath registers a source file for a specific pathname. The
// source file is used as the data source. If the source file changes, the data
// are re-read.
func (m *Manager) RegisterFileForPath(pathname string, sourceFile string) {
m.registerFileForPath(pathname, sourceFile)
}
// RegisterGeneratorForPath registers a FileGenerator for a specific pathname.
// It returns a channel to which notification messages may be sent indicating
// that the data should be regenerated, even if the machine data have not
// changed. If the empty string is sent to the channel, it indicates that data
// should be regenerated for all machines, otherwise it indicates that data
// should be regenerated for a specific machine.
// An internal goroutine reads from the channel, which terminates if the channel
// is closed. The channel should be closed if the data should only be
// regenerated if the machine data change.
func (m *Manager) RegisterGeneratorForPath(pathname string,
gen FileGenerator) chan<- string {
return m.registerDataGeneratorForPath(pathname, gen)
}
// RegisterMdbGeneratorForPath registers a generator for pathname which yields
// the MDB data in JSON format for a machine.
func (m *Manager) RegisterMdbGeneratorForPath(pathname string) {
m.registerMdbGeneratorForPath(pathname)
}
// RegisterTemplateFileForPath registers a template file for a specific
// pathname.
// The template file is used to generate the data, modified by the machine data.
// If the template file changes and watchForUpdates is true, the template file
// is re-read and the data are regenerated.
// The template file syntax is defined by the text/template standard package.
func (m *Manager) RegisterTemplateFileForPath(pathname string,
templateFile string, watchForUpdates bool) error {
return m.registerTemplateFileForPath(pathname, templateFile,
watchForUpdates)
}
// WriteHtml will write status information about the Manager to w, with
// appropriate HTML markups.
func (m *Manager) WriteHtml(writer io.Writer) {
m.writeHtml(writer)
}