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/xdsconfig"
16 "github.com/iotbzh/xds-agent/lib/xdsserver"
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)
71 if err := ctx.SThg.Connect(); err == nil {
74 ctx.Log.Infof("Establishing connection to Syncthing (retry %d/10)", retry)
75 time.Sleep(time.Second)
78 if err != nil || retry == 0 {
79 return cli.NewExitError(err, 2)
82 // Retrieve Syncthing config
83 id, err := ctx.SThg.IDGet()
85 return cli.NewExitError(err, 2)
87 ctx.Log.Infof("Local Syncthing ID: %s", id)
89 // Create and start Web Server
90 ctx.WWWServer = xdsserver.NewServer(ctx.Config, ctx.Log)
91 if err = ctx.WWWServer.Serve(); err != nil {
93 return cli.NewExitError(err, 3)
96 return cli.NewExitError("Program exited ", 4)
102 // Create a new instance of the logger
105 // Create a new App instance
108 app.Description = appDescription
110 app.Version = AppVersion + " (" + AppSubVersion + ")"
111 app.Authors = appAuthors
112 app.Copyright = appCopyright
113 app.Metadata = make(map[string]interface{})
114 app.Metadata["version"] = AppVersion
115 app.Metadata["git-tag"] = AppSubVersion
116 app.Metadata["logger"] = log
118 app.Flags = []cli.Flag{
121 Usage: "JSON config file to use\n\t",
122 EnvVar: "XDS_CONFIGFILE",
127 Usage: "logging level (supported levels: panic, fatal, error, warn, info, debug)\n\t",
133 app.Action = xdsAgent