10 "github.com/Sirupsen/logrus"
11 "github.com/codegangsta/cli"
12 "github.com/iotbzh/xds-agent/lib/syncthing"
13 "github.com/iotbzh/xds-agent/lib/xdsconfig"
14 "github.com/iotbzh/xds-agent/lib/webserver"
17 // Context holds the Agent context structure
20 Config *xdsconfig.Config
25 WWWServer *webserver.ServerService
29 // NewAgent Create a new instance of Agent
30 func NewAgent(cliCtx *cli.Context) *Context {
33 // Set logger level and formatter
34 log := cliCtx.App.Metadata["logger"].(*logrus.Logger)
36 logLevel := cliCtx.GlobalString("log")
38 logLevel = "error" // FIXME get from Config DefaultLogLevel
40 if log.Level, err = logrus.ParseLevel(logLevel); err != nil {
41 fmt.Printf("Invalid log level : \"%v\"\n", logLevel)
44 log.Formatter = &logrus.TextFormatter{}
46 // Define default configuration
48 ProgName: cliCtx.App.Name,
50 Exit: make(chan os.Signal, 1),
53 // register handler on SIGTERM / exit
54 signal.Notify(ctx.Exit, os.Interrupt, syscall.SIGTERM)
55 go handlerSigTerm(&ctx)
60 // Handle exit and properly stop/close all stuff
61 func handlerSigTerm(ctx *Context) {
64 ctx.Log.Infof("Stoping Syncthing... (PID %d)",
65 ctx.SThgCmd.Process.Pid)
66 ctx.Log.Infof("Stoping Syncthing-inotify... (PID %d)",
67 ctx.SThgInotCmd.Process.Pid)
69 ctx.SThg.StopInotify()
71 if ctx.WWWServer != nil {
72 ctx.Log.Infof("Stoping Web server...")