2016-02-10 19:54:34 +00:00
|
|
|
package enterprise
|
|
|
|
|
|
|
|
import (
|
2016-02-10 21:05:17 +00:00
|
|
|
"fmt"
|
2016-02-10 19:54:34 +00:00
|
|
|
"log"
|
2016-02-10 21:48:00 +00:00
|
|
|
"net"
|
2016-02-10 21:05:17 +00:00
|
|
|
"net/http"
|
2016-02-10 19:54:34 +00:00
|
|
|
"os"
|
2016-02-10 21:48:00 +00:00
|
|
|
"time"
|
2016-02-10 19:54:34 +00:00
|
|
|
|
|
|
|
"github.com/influxdata/enterprise-client/v2"
|
2016-02-10 21:05:17 +00:00
|
|
|
"github.com/influxdata/enterprise-client/v2/admin"
|
2016-02-10 19:54:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2016-02-10 21:05:17 +00:00
|
|
|
AdminPort uint16
|
|
|
|
Hosts []*client.Host
|
2016-02-10 19:54:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Service struct {
|
2016-02-10 21:05:17 +00:00
|
|
|
hosts []*client.Host
|
|
|
|
logger *log.Logger
|
|
|
|
hostname string
|
|
|
|
adminPort string
|
2016-02-10 21:48:00 +00:00
|
|
|
|
|
|
|
shutdown chan struct{}
|
2016-02-10 19:54:34 +00:00
|
|
|
}
|
|
|
|
|
2016-02-10 21:48:00 +00:00
|
|
|
func NewEnterprise(c Config, hostname string, shutdown chan struct{}) *Service {
|
2016-02-10 19:54:34 +00:00
|
|
|
return &Service{
|
2016-02-10 21:05:17 +00:00
|
|
|
hosts: c.Hosts,
|
|
|
|
hostname: hostname,
|
|
|
|
logger: log.New(os.Stdout, "[enterprise]", log.Ldate|log.Ltime),
|
|
|
|
adminPort: fmt.Sprintf(":%d", c.AdminPort),
|
2016-02-10 21:48:00 +00:00
|
|
|
shutdown: shutdown,
|
2016-02-10 19:54:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) Open() {
|
2016-02-11 19:11:36 +00:00
|
|
|
if len(s.hosts) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-02-10 19:54:34 +00:00
|
|
|
cl, err := client.New(s.hosts)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Printf("Unable to contact one or more Enterprise hosts. err: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
2016-02-10 21:48:00 +00:00
|
|
|
go func() {
|
|
|
|
token, secret, err := s.registerProduct(cl)
|
|
|
|
if err == nil {
|
|
|
|
s.startAdminInterface(token, secret)
|
|
|
|
}
|
|
|
|
}()
|
2016-02-10 19:54:34 +00:00
|
|
|
}
|
|
|
|
|
2016-02-10 21:48:00 +00:00
|
|
|
func (s *Service) registerProduct(cl *client.Client) (token string, secret string, err error) {
|
2016-02-10 19:54:34 +00:00
|
|
|
p := client.Product{
|
2016-02-11 19:12:18 +00:00
|
|
|
ProductID: "4815162342",
|
2016-02-10 20:42:07 +00:00
|
|
|
Host: s.hostname,
|
2016-02-11 19:12:18 +00:00
|
|
|
ClusterID: "8675309",
|
|
|
|
Name: "telegraf",
|
|
|
|
Version: "0.10.1.dev",
|
|
|
|
AdminURL: "http://" + s.hostname + s.adminPort,
|
2016-02-10 19:54:34 +00:00
|
|
|
}
|
|
|
|
|
2016-02-10 21:48:00 +00:00
|
|
|
_, err = cl.Register(&p)
|
2016-02-10 19:54:34 +00:00
|
|
|
if err != nil {
|
|
|
|
s.logger.Println("Unable to register Telegraf with Enterprise")
|
2016-02-10 21:48:00 +00:00
|
|
|
return
|
2016-02-10 19:54:34 +00:00
|
|
|
}
|
2016-02-10 21:48:00 +00:00
|
|
|
|
|
|
|
for _, host := range cl.Hosts {
|
|
|
|
if host.Primary {
|
|
|
|
token = host.Token
|
|
|
|
secret = host.SecretKey
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2016-02-10 19:54:34 +00:00
|
|
|
}
|
2016-02-10 21:05:17 +00:00
|
|
|
|
2016-02-10 21:48:00 +00:00
|
|
|
func (s *Service) startAdminInterface(token, secret string) {
|
|
|
|
srv := &http.Server{
|
|
|
|
ReadTimeout: 5 * time.Second,
|
|
|
|
WriteTimeout: 5 * time.Second,
|
|
|
|
Handler: admin.App(token, []byte(secret)),
|
|
|
|
}
|
|
|
|
l, err := net.Listen("tcp", s.adminPort)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Printf("Unable to bind to admin interface port: err: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
go srv.Serve(l)
|
|
|
|
select {
|
|
|
|
case <-s.shutdown:
|
|
|
|
s.logger.Printf("Shutting down enterprise admin interface")
|
|
|
|
l.Close()
|
|
|
|
}
|
|
|
|
return
|
2016-02-10 21:05:17 +00:00
|
|
|
}
|