forked from docker-archive/deploykit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
126 lines (107 loc) · 3.6 KB
/
main.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
package main
import (
"os"
"time"
log "github.com/Sirupsen/logrus"
"github.com/docker/infrakit/pkg/cli"
"github.com/docker/infrakit/pkg/discovery/local"
"github.com/docker/infrakit/pkg/plugin"
"github.com/docker/infrakit/pkg/plugin/group"
metadata_plugin "github.com/docker/infrakit/pkg/plugin/metadata"
flavor_client "github.com/docker/infrakit/pkg/rpc/flavor"
group_server "github.com/docker/infrakit/pkg/rpc/group"
instance_client "github.com/docker/infrakit/pkg/rpc/instance"
metadata_rpc "github.com/docker/infrakit/pkg/rpc/metadata"
"github.com/docker/infrakit/pkg/run"
"github.com/docker/infrakit/pkg/spi/flavor"
"github.com/docker/infrakit/pkg/spi/instance"
"github.com/docker/infrakit/pkg/types"
"github.com/spf13/cobra"
)
func main() {
cmd := &cobra.Command{
Use: os.Args[0],
Short: "Group server",
}
name := cmd.Flags().String("name", "group", "Plugin name to advertise for discovery")
logLevel := cmd.Flags().Int("log", cli.DefaultLogLevel, "Logging level. 0 is least verbose. Max is 5")
pollInterval := cmd.Flags().Duration("poll-interval", 10*time.Second, "Group polling interval")
maxParallelNum := cmd.Flags().Uint("max-parallel", 0, "Max number of parallel instance operation (create, delete). (Default: 0 = no limit)")
cmd.RunE = func(c *cobra.Command, args []string) error {
cli.SetLogLevel(*logLevel)
plugins, err := local.NewPluginDiscovery()
if err != nil {
return err
}
instancePluginLookup := func(n plugin.Name) (instance.Plugin, error) {
endpoint, err := plugins.Find(n)
if err != nil {
return nil, err
}
return instance_client.NewClient(n, endpoint.Address)
}
flavorPluginLookup := func(n plugin.Name) (flavor.Plugin, error) {
endpoint, err := plugins.Find(n)
if err != nil {
return nil, err
}
return flavor_client.NewClient(n, endpoint.Address)
}
groupPlugin := group.NewGroupPlugin(instancePluginLookup, flavorPluginLookup, *pollInterval, *maxParallelNum)
// Start a poller to load the snapshot and make that available as metadata
updateSnapshot := make(chan func(map[string]interface{}))
stopSnapshot := make(chan struct{})
go func() {
tick := time.Tick(1 * time.Second)
tick30 := time.Tick(30 * time.Second)
for {
select {
case <-tick:
// load the specs for the groups
snapshot := map[string]interface{}{}
if specs, err := groupPlugin.InspectGroups(); err == nil {
for _, spec := range specs {
snapshot[string(spec.ID)] = spec
}
} else {
snapshot["err"] = err
}
updateSnapshot <- func(view map[string]interface{}) {
types.Put([]string{"specs"}, snapshot, view)
}
case <-tick30:
snapshot := map[string]interface{}{}
// describe the groups and expose info as metadata
if specs, err := groupPlugin.InspectGroups(); err == nil {
for _, spec := range specs {
if description, err := groupPlugin.DescribeGroup(spec.ID); err == nil {
snapshot[string(spec.ID)] = description
} else {
snapshot[string(spec.ID)] = err
}
}
} else {
snapshot["err"] = err
}
updateSnapshot <- func(view map[string]interface{}) {
types.Put([]string{"groups"}, snapshot, view)
}
case <-stopSnapshot:
log.Infoln("Snapshot updater stopped")
return
}
}
}()
run.Plugin(plugin.DefaultTransport(*name),
metadata_rpc.Server(metadata_plugin.NewPluginFromChannel(updateSnapshot)),
group_server.PluginServer(groupPlugin))
close(stopSnapshot)
return nil
}
cmd.AddCommand(cli.VersionCommand())
err := cmd.Execute()
if err != nil {
log.Error(err)
os.Exit(1)
}
}