11 "github.com/Sirupsen/logrus"
12 "github.com/codegangsta/cli"
13 "github.com/iotbzh/xds-agent/lib/agent"
14 "github.com/iotbzh/xds-agent/lib/syncthing"
15 "github.com/iotbzh/xds-agent/lib/webserver"
16 "github.com/iotbzh/xds-agent/lib/xdsconfig"
21 appDescription = "X(cross) Development System Agent is a web server that allows to remotely cross build applications."
22 appCopyright = "Apache-2.0"
23 appUsage = "X(cross) Development System Agent"
26 var appAuthors = []cli.Author{
27 cli.Author{Name: "Sebastien Douheret", Email: "sebastien@iot.bzh"},
30 // AppVersion is the version of this application
31 var AppVersion = "?.?.?"
33 // AppSubVersion is the git tag id added to version string
34 // Should be set by compilation -ldflags "-X main.AppSubVersion=xxx"
35 var AppSubVersion = "unknown-dev"
37 // xdsAgent main routine
38 func xdsAgent(cliCtx *cli.Context) error {
41 // Create Agent context
42 ctx := agent.NewAgent(cliCtx)
45 ctx.Config, err = xdsconfig.Init(cliCtx, ctx.Log)
47 return cli.NewExitError(err, 2)
50 // Start local instance of Syncthing and Syncthing-notify
51 ctx.SThg = st.NewSyncThing(ctx.Config, ctx.Log)
53 ctx.Log.Infof("Starting Syncthing...")
54 ctx.SThgCmd, err = ctx.SThg.Start()
56 return cli.NewExitError(err, 2)
58 fmt.Printf("Syncthing started (PID %d)\n", ctx.SThgCmd.Process.Pid)
60 ctx.Log.Infof("Starting Syncthing-inotify...")
61 ctx.SThgInotCmd, err = ctx.SThg.StartInotify()
63 return cli.NewExitError(err, 2)
65 fmt.Printf("Syncthing-inotify started (PID %d)\n", ctx.SThgInotCmd.Process.Pid)
67 // Establish connection with local Syncthing (retry if connection fail)
68 time.Sleep(3 * time.Second)
72 if err := ctx.SThg.Connect(); err == nil {
75 ctx.Log.Infof("Establishing connection to Syncthing (retry %d/%d", retry, maxRetry)
76 time.Sleep(time.Second)
79 if err != nil || retry == 0 {
80 return cli.NewExitError(err, 2)
83 // Retrieve Syncthing config
84 id, err := ctx.SThg.IDGet()
86 return cli.NewExitError(err, 2)
88 ctx.Log.Infof("Local Syncthing ID: %s", id)
90 // Create and start Web Server
91 ctx.WWWServer = webserver.New(ctx.Config, ctx.Log)
92 if err = ctx.WWWServer.Serve(); err != nil {
94 return cli.NewExitError(err, 3)
97 return cli.NewExitError("Program exited ", 4)
103 // Create a new instance of the logger
106 // Create a new App instance
109 app.Description = appDescription
111 app.Version = AppVersion + " (" + AppSubVersion + ")"
112 app.Authors = appAuthors
113 app.Copyright = appCopyright
114 app.Metadata = make(map[string]interface{})
115 app.Metadata["version"] = AppVersion
116 app.Metadata["git-tag"] = AppSubVersion
117 app.Metadata["logger"] = log
119 app.Flags = []cli.Flag{
122 Usage: "JSON config file to use\n\t",
123 EnvVar: "XDS_CONFIGFILE",
128 Usage: "logging level (supported levels: panic, fatal, error, warn, info, debug)\n\t",
134 app.Action = xdsAgent