-
Notifications
You must be signed in to change notification settings - Fork 1
/
registry.go
122 lines (109 loc) · 3.33 KB
/
registry.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
package registry
import (
"errors"
"fmt"
"sync"
"os"
"runtime"
"github.com/oliverkofoed/dogo/commandtree"
"github.com/oliverkofoed/dogo/neaterror"
"github.com/oliverkofoed/dogo/registry/modules/docker"
"github.com/oliverkofoed/dogo/registry/modules/dogo"
"github.com/oliverkofoed/dogo/registry/modules/file"
"github.com/oliverkofoed/dogo/registry/modules/firewall"
"github.com/oliverkofoed/dogo/registry/resources/cloudflare"
"github.com/oliverkofoed/dogo/registry/resources/linode"
"github.com/oliverkofoed/dogo/registry/resources/linodeold"
"github.com/oliverkofoed/dogo/registry/resources/localhost"
"github.com/oliverkofoed/dogo/registry/resources/qemu"
"github.com/oliverkofoed/dogo/registry/resources/server"
"github.com/oliverkofoed/dogo/registry/resources/vagrant"
"github.com/oliverkofoed/dogo/schema"
"github.com/oliverkofoed/dogo/snobgob"
"github.com/oliverkofoed/dogo/version"
)
// ModuleManagers is the list of registered modules
var ModuleManagers = map[string]*schema.ModuleManager{
docker.Manager.Name: &docker.Manager,
firewall.Manager.Name: &firewall.Manager,
dogo.Manager.Name: &dogo.Manager,
file.Manager.Name: &file.Manager,
}
// ResourceManagers is the list of registered resource providers
var ResourceManagers = map[string]*schema.ResourceManager{
server.Manager.Name: &server.Manager,
vagrant.Manager.Name: &vagrant.Manager,
linode.Manager.Name: &linode.Manager,
linodeold.Manager.Name: &linodeold.Manager,
localhost.Manager.Name: &localhost.Manager,
cloudflare.Manager.Name: &cloudflare.Manager,
qemu.Manager.Name: &qemu.Manager,
//virtualbox.Manager.Name: &virtualbox.Manager,
}
func GobRegister() {
snobgob.Register(neaterror.Error{})
snobgob.Register(commandtree.RootCommand{})
snobgob.Register(GetStateCommand{})
snobgob.Register(schema.ServerState{})
snobgob.Register(errors.New("test"))
snobgob.Register(commandtree.NewExecCommand("", "", "", "", ""))
snobgob.Register(commandtree.NewBashCommands("", "", "", "", ""))
snobgob.Register(DefaultStateQuery{})
for _, m := range ModuleManagers {
if m.ModulePrototype != nil {
snobgob.Register(m.ModulePrototype)
}
if m.StatePrototype != nil {
snobgob.Register(m.StatePrototype)
}
if m.GobRegister != nil {
m.GobRegister()
}
}
}
type GetStateCommand struct {
commandtree.Command
Query map[string]interface{}
}
type DefaultStateQuery struct {
}
func (c *GetStateCommand) Execute() {
state := &schema.ServerState{
Version: version.Version,
OS: runtime.GOOS,
Arch: runtime.GOARCH,
UID: os.Getuid(),
Modules: make(map[string]interface{}),
}
// collect state from the requested modules
var wg sync.WaitGroup
var lock sync.RWMutex
for name, query := range c.Query {
manager := ModuleManagers[name]
wg.Add(1)
go func(name string, manager *schema.ModuleManager, query interface{}) {
defer func() {
if r := recover(); r != nil {
lock.Lock()
state.Modules[name] = fmt.Errorf("Panic: %v", r)
lock.Unlock()
wg.Done()
}
}()
moduleState, err := manager.GetState(query)
lock.Lock()
if err != nil {
if _, ok := err.(neaterror.Error); !ok {
err = neaterror.New(nil, err.Error())
}
state.Modules[name] = err
} else {
state.Modules[name] = moduleState
}
lock.Unlock()
wg.Done()
}(name, manager, query)
}
wg.Wait()
c.SetResult(state)
}