orbiter/cmd/daemon.go
Gianluca Arbezzano db7f674976 Bootstrap autodetection and swarm zero conf
Fixed #9

This PR bootstrap the autodetection feature:

1. It works when the configuration file is not setup. Right know I am
not going to manage the merge of double sources (autodetection and
configuration file).

2. At the moment only Docker Swarm will support this feature.
2017-03-14 23:49:53 +00:00

96 lines
2.3 KiB
Go

package cmd
import (
"flag"
"io/ioutil"
"net/http"
"os"
"os/signal"
"path/filepath"
"strings"
"github.com/Sirupsen/logrus"
"github.com/gianarb/orbiter/api"
"github.com/gianarb/orbiter/core"
)
type DaemonCmd struct {
EventChannel chan *logrus.Entry
}
func (c *DaemonCmd) Run(args []string) int {
logrus.Info("orbiter started")
var port string
var configPath string
var debug bool
cmdFlags := flag.NewFlagSet("event", flag.ExitOnError)
cmdFlags.StringVar(&port, "port", ":8000", "port")
cmdFlags.StringVar(&configPath, "config", "", "config")
cmdFlags.BoolVar(&debug, "debug", false, "debug")
if err := cmdFlags.Parse(args); err != nil {
logrus.WithField("error", err).Warn("Problem to parse arguments.")
return 1
}
if debug == true {
logrus.SetLevel(logrus.DebugLevel)
logrus.Debug("Daemon started in debug mode")
}
var coreEngine core.Core
if configPath != "" {
config, err := readConfiguration(configPath)
if err != nil {
logrus.WithField("error", err).Warn("Configuration file malformed.")
return 1
}
logrus.Infof("Starting from configuration file located %s", configPath)
coreEngine, err = core.NewCoreByConfig(config.AutoscalersConf)
if err != nil {
logrus.WithField("error", err).Warn(err)
return 1
}
} else {
logrus.Info("Starting in auto-detection mode.")
coreEngine, err = core.Autodetect()
}
go func() {
sigchan := make(chan os.Signal, 10)
signal.Notify(sigchan, os.Interrupt)
<-sigchan
logrus.Info("Stopping and cleaning. Bye!")
os.Exit(0)
}()
router := api.GetRouter(coreEngine, c.EventChannel)
logrus.Infof("API Server run on port %s", port)
http.ListenAndServe(port, router)
return 0
}
func (c *DaemonCmd) Help() string {
helpText := `
Usage: start gourmet API handler.
Options:
-debug_level=info Servert port
-port=:8000 Servert port
-config=/etc/daemon.yml Configuration path
`
return strings.TrimSpace(helpText)
}
func (r *DaemonCmd) Synopsis() string {
return "Start core daemon"
}
func readConfiguration(path string) (core.Conf, error) {
var config core.Conf
filename, _ := filepath.Abs(path)
yamlFile, err := ioutil.ReadFile(filename)
if err != nil {
return config, err
}
config, err = core.ParseYAMLConfiguration(yamlFile)
if err != nil {
return config, err
}
return config, nil
}