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"
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"
25 var appAuthors = []cli.Author{
26 cli.Author{Name: "Sebastien Douheret", Email: "sebastien@iot.bzh"},
29 // AppVersion is the version of this application
30 var AppVersion = "?.?.?"
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"
36 // xdsAgent main routine
37 func xdsAgent(cliCtx *cli.Context) error {
40 // Create Agent context
41 ctx := agent.NewAgent(cliCtx)
44 ctx.Config, err = xdsconfig.Init(cliCtx, ctx.Log)
46 return cli.NewExitError(err, 2)
49 // Start local instance of Syncthing and Syncthing-notify
50 ctx.SThg = st.NewSyncThing(ctx.Config, ctx.Log)
52 ctx.Log.Infof("Starting Syncthing...")
53 ctx.SThgCmd, err = ctx.SThg.Start()
55 return cli.NewExitError(err, 2)
57 ctx.Log.Infof("Syncthing started (PID %d)", ctx.SThgCmd.Process.Pid)
59 ctx.Log.Infof("Starting Syncthing-inotify...")
60 ctx.SThgInotCmd, err = ctx.SThg.StartInotify()
62 return cli.NewExitError(err, 2)
64 ctx.Log.Infof("Syncthing-inotify started (PID %d)", ctx.SThgInotCmd.Process.Pid)
66 // Establish connection with local Syncthing (retry if connection fail)
67 time.Sleep(3 * time.Second)
70 if err := ctx.SThg.Connect(); err == nil {
73 ctx.Log.Infof("Establishing connection to Syncthing (retry %d/10)", retry)
74 time.Sleep(time.Second)
77 if err != nil || retry == 0 {
78 return cli.NewExitError(err, 2)
81 // Retrieve Syncthing config
82 id, err := ctx.SThg.IDGet()
84 return cli.NewExitError(err, 2)
86 ctx.Log.Infof("Local Syncthing ID: %s", id)
88 // Create and start Web Server
89 ctx.WWWServer = xdsserver.NewServer(ctx.Config, ctx.Log)
90 if err = ctx.WWWServer.Serve(); err != nil {
92 return cli.NewExitError(err, 3)
95 return cli.NewExitError("Program exited ", 4)
101 // Create a new instance of the logger
104 // Create a new App instance
107 app.Description = appDescription
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
117 app.Flags = []cli.Flag{
120 Usage: "JSON config file to use\n\t",
121 EnvVar: "XDS_CONFIGFILE",
126 Usage: "logging level (supported levels: panic, fatal, error, warn, info, debug)\n\t",
132 app.Action = xdsAgent