12 "github.com/Sirupsen/logrus"
13 "github.com/codegangsta/cli"
14 "github.com/iotbzh/xds-agent/lib/agent"
15 "github.com/iotbzh/xds-agent/lib/syncthing"
16 "github.com/iotbzh/xds-agent/lib/xdsconfig"
17 "github.com/iotbzh/xds-agent/lib/xdsserver"
22 appDescription = "X(cross) Development System Agent is a web server that allows to remotely cross build applications."
23 appCopyright = "Apache-2.0"
24 appUsage = "X(cross) Development System Agent"
27 var appAuthors = []cli.Author{
28 cli.Author{Name: "Sebastien Douheret", Email: "sebastien@iot.bzh"},
31 // AppVersion is the version of this application
32 var AppVersion = "?.?.?"
34 // AppSubVersion is the git tag id added to version string
35 // Should be set by compilation -ldflags "-X main.AppSubVersion=xxx"
36 var AppSubVersion = "unknown-dev"
38 // xdsAgent main routine
39 func xdsAgent(cliCtx *cli.Context) error {
41 // Create Agent context
42 ctx := agent.NewAgent(cliCtx)
45 cfg, err := xdsconfig.Init(cliCtx, ctx.Log)
47 return cli.NewExitError(err, 2)
51 // Start local instance of Syncthing and Syncthing-notify
52 ctx.SThg = st.NewSyncThing(ctx.Config.FileConf.SThgConf, ctx.Log)
54 ctx.Log.Infof("Starting Syncthing...")
55 ctx.SThgCmd, err = ctx.SThg.Start()
57 return cli.NewExitError(err, 2)
59 ctx.Log.Infof("Syncthing started (PID %d)", ctx.SThgCmd.Process.Pid)
61 ctx.Log.Infof("Starting Syncthing-inotify...")
62 ctx.SThgInotCmd, err = ctx.SThg.StartInotify()
64 return cli.NewExitError(err, 2)
66 ctx.Log.Infof("Syncthing-inotify started (PID %d)", ctx.SThgInotCmd.Process.Pid)
68 // Establish connection with local Syncthing (retry if connection fail)
69 time.Sleep(3 * time.Second)
72 if err := ctx.SThg.Connect(); err == nil {
75 ctx.Log.Infof("Establishing connection to Syncthing (retry %d/5)", retry)
76 time.Sleep(time.Second)
80 err = fmt.Errorf("ERROR: cannot connect to Syncthing (url: %s)", ctx.SThg.BaseURL)
81 return cli.NewExitError(err, 2)
84 // Retrieve Syncthing config
85 id, err := ctx.SThg.IDGet()
87 return cli.NewExitError(err, 2)
89 ctx.Log.Infof("Local Syncthing ID: %s", id)
91 // Create and start Web Server
92 ctx.WWWServer = xdsserver.NewServer(ctx.Config, ctx.Log)
93 if err = ctx.WWWServer.Serve(); err != nil {
95 return cli.NewExitError(err, 3)
98 return cli.NewExitError("Program exited ", 4)
104 // Create a new instance of the logger
107 // Create a new App instance
110 app.Description = appDescription
112 app.Version = AppVersion + " (" + AppSubVersion + ")"
113 app.Authors = appAuthors
114 app.Copyright = appCopyright
115 app.Metadata = make(map[string]interface{})
116 app.Metadata["version"] = AppVersion
117 app.Metadata["git-tag"] = AppSubVersion
118 app.Metadata["logger"] = log
120 app.Flags = []cli.Flag{
123 Usage: "JSON config file to use\n\t",
124 EnvVar: "XDS_CONFIGFILE",
129 Usage: "logging level (supported levels: panic, fatal, error, warn, info, debug)\n\t",
135 app.Action = xdsAgent