forked from docker-archive/deploykit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
139 lines (112 loc) · 3.2 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
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"encoding/json"
"fmt"
"net/url"
"os"
"path/filepath"
"time"
log "github.com/Sirupsen/logrus"
"github.com/docker/infrakit/discovery"
"github.com/docker/infrakit/plugin/group"
"github.com/docker/infrakit/plugin/util"
"github.com/docker/infrakit/spi/flavor"
flavor_client "github.com/docker/infrakit/spi/http/flavor"
group_server "github.com/docker/infrakit/spi/http/group"
instance_client "github.com/docker/infrakit/spi/http/instance"
"github.com/docker/infrakit/spi/instance"
"github.com/spf13/cobra"
)
var (
// PluginName is the name of the plugin in the Docker Hub / registry
PluginName = "GroupPlugin"
// PluginType is the type / interface it supports
PluginType = "infra.GroupPlugin/1.0"
// Version is the build release identifier.
Version = "Unspecified"
// Revision is the build source control revision.
Revision = "Unspecified"
)
func main() {
logLevel := len(log.AllLevels) - 2
listen := "unix:///run/infrakit/plugins/group.sock"
pollInterval := 10 * time.Second
cmd := &cobra.Command{
Use: os.Args[0],
Short: "Group server",
RunE: func(c *cobra.Command, args []string) error {
if logLevel > len(log.AllLevels)-1 {
logLevel = len(log.AllLevels) - 1
} else if logLevel < 0 {
logLevel = 0
}
log.SetLevel(log.AllLevels[logLevel])
if c.Use == "version" {
return nil
}
// parse the listen string
listenURL, err := url.Parse(listen)
if err != nil {
return err
}
log.Infoln("Starting discovery")
pluginDir, err := discovery.NewDir(filepath.Dir(listenURL.Path))
if err != nil {
return err
}
instancePluginLookup := func(n string) (instance.Plugin, error) {
callable, err := pluginDir.PluginByName(n)
if err != nil {
return nil, err
}
return instance_client.PluginClient(callable), nil
}
flavorPluginLookup := func(n string) (flavor.Plugin, error) {
callable, err := pluginDir.PluginByName(n)
if err != nil {
return nil, err
}
return flavor_client.PluginClient(callable), nil
}
log.Infoln("Starting plugin")
log.Infoln("Starting")
log.Infoln("Listening on:", listen)
_, stopped, err := util.StartServer(listen, group_server.PluginServer(
group.NewGroupPlugin(
instancePluginLookup,
flavorPluginLookup,
pollInterval)))
if err != nil {
log.Error(err)
}
<-stopped // block until done
log.Infoln("Server stopped")
return nil
},
}
cmd.AddCommand(&cobra.Command{
Use: "version",
Short: "print build version information",
RunE: func(cmd *cobra.Command, args []string) error {
buff, err := json.MarshalIndent(map[string]interface{}{
"name": PluginName,
"type": PluginType,
"version": Version,
"revision": Revision,
}, " ", " ")
if err != nil {
return err
}
fmt.Println(string(buff))
return nil
},
})
cmd.Flags().StringVar(&listen, "listen", listen, "listen address (unix or tcp) for the control endpoint")
cmd.Flags().IntVar(&logLevel, "log", logLevel, "Logging level. 0 is least verbose. Max is 5")
cmd.Flags().DurationVar(&pollInterval, "poll-interval", pollInterval, "Group polling interval")
err := cmd.Execute()
if err != nil {
log.Error(err)
os.Exit(1)
}
}