Add logsDir setting and more
[src/xds/xds-agent.git] / main.go
1 // TODO add Doc
2 //
3 package main
4
5 import (
6         "log"
7         "os"
8         "time"
9
10         "github.com/Sirupsen/logrus"
11         "github.com/codegangsta/cli"
12         "github.com/iotbzh/xds-agent/lib/agent"
13         "github.com/iotbzh/xds-agent/lib/syncthing"
14         "github.com/iotbzh/xds-agent/lib/xdsconfig"
15         "github.com/iotbzh/xds-agent/lib/xdsserver"
16 )
17
18 const (
19         appName        = "xds-agent"
20         appDescription = "X(cross) Development System Agent is a web server that allows to remotely cross build applications."
21         appCopyright   = "Apache-2.0"
22         appUsage       = "X(cross) Development System Agent"
23 )
24
25 var appAuthors = []cli.Author{
26         cli.Author{Name: "Sebastien Douheret", Email: "sebastien@iot.bzh"},
27 }
28
29 // AppVersion is the version of this application
30 var AppVersion = "?.?.?"
31
32 // AppSubVersion is the git tag id added to version string
33 // Should be set by compilation -ldflags "-X main.AppSubVersion=xxx"
34 var AppSubVersion = "unknown-dev"
35
36 // xdsAgent main routine
37 func xdsAgent(cliCtx *cli.Context) error {
38         var err error
39
40         // Create Agent context
41         ctx := agent.NewAgent(cliCtx)
42
43         // Load config
44         ctx.Config, err = xdsconfig.Init(cliCtx, ctx.Log)
45         if err != nil {
46                 return cli.NewExitError(err, 2)
47         }
48
49         // Start local instance of Syncthing and Syncthing-notify
50         ctx.SThg = st.NewSyncThing(ctx.Config, ctx.Log)
51
52         ctx.Log.Infof("Starting Syncthing...")
53         ctx.SThgCmd, err = ctx.SThg.Start()
54         if err != nil {
55                 return cli.NewExitError(err, 2)
56         }
57         ctx.Log.Infof("Syncthing started (PID %d)", ctx.SThgCmd.Process.Pid)
58
59         ctx.Log.Infof("Starting Syncthing-inotify...")
60         ctx.SThgInotCmd, err = ctx.SThg.StartInotify()
61         if err != nil {
62                 return cli.NewExitError(err, 2)
63         }
64         ctx.Log.Infof("Syncthing-inotify started (PID %d)", ctx.SThgInotCmd.Process.Pid)
65
66         // Establish connection with local Syncthing (retry if connection fail)
67         time.Sleep(3 * time.Second)
68         retry := 10
69         for retry > 0 {
70                 if err := ctx.SThg.Connect(); err == nil {
71                         break
72                 }
73                 ctx.Log.Infof("Establishing connection to Syncthing (retry %d/10)", retry)
74                 time.Sleep(time.Second)
75                 retry--
76         }
77         if err != nil || retry == 0 {
78                 return cli.NewExitError(err, 2)
79         }
80
81         // Retrieve Syncthing config
82         id, err := ctx.SThg.IDGet()
83         if err != nil {
84                 return cli.NewExitError(err, 2)
85         }
86         ctx.Log.Infof("Local Syncthing ID: %s", id)
87
88         // Create and start Web Server
89         ctx.WWWServer = xdsserver.NewServer(ctx.Config, ctx.Log)
90         if err = ctx.WWWServer.Serve(); err != nil {
91                 log.Println(err)
92                 return cli.NewExitError(err, 3)
93         }
94
95         return cli.NewExitError("Program exited ", 4)
96 }
97
98 // main
99 func main() {
100
101         // Create a new instance of the logger
102         log := logrus.New()
103
104         // Create a new App instance
105         app := cli.NewApp()
106         app.Name = appName
107         app.Description = appDescription
108         app.Usage = appUsage
109         app.Version = AppVersion + " (" + AppSubVersion + ")"
110         app.Authors = appAuthors
111         app.Copyright = appCopyright
112         app.Metadata = make(map[string]interface{})
113         app.Metadata["version"] = AppVersion
114         app.Metadata["git-tag"] = AppSubVersion
115         app.Metadata["logger"] = log
116
117         app.Flags = []cli.Flag{
118                 cli.StringFlag{
119                         Name:   "config, c",
120                         Usage:  "JSON config file to use\n\t",
121                         EnvVar: "XDS_CONFIGFILE",
122                 },
123                 cli.StringFlag{
124                         Name:   "log, l",
125                         Value:  "error",
126                         Usage:  "logging level (supported levels: panic, fatal, error, warn, info, debug)\n\t",
127                         EnvVar: "LOG_LEVEL",
128                 },
129         }
130
131         // only one action
132         app.Action = xdsAgent
133
134         app.Run(os.Args)
135 }