Increase timeout for Syncthing startup.
[src/xds/xds-agent.git] / main.go
1 // TODO add Doc
2 //
3 package main
4
5 import (
6         "fmt"
7         "log"
8         "os"
9         "time"
10
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"
17 )
18
19 const (
20         appName        = "xds-agent"
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"
24 )
25
26 var appAuthors = []cli.Author{
27         cli.Author{Name: "Sebastien Douheret", Email: "sebastien@iot.bzh"},
28 }
29
30 // AppVersion is the version of this application
31 var AppVersion = "?.?.?"
32
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"
36
37 // xdsAgent main routine
38 func xdsAgent(cliCtx *cli.Context) error {
39         var err error
40
41         // Create Agent context
42         ctx := agent.NewAgent(cliCtx)
43
44         // Load config
45         ctx.Config, err = xdsconfig.Init(cliCtx, ctx.Log)
46         if err != nil {
47                 return cli.NewExitError(err, 2)
48         }
49
50         // Start local instance of Syncthing and Syncthing-notify
51         ctx.SThg = st.NewSyncThing(ctx.Config, ctx.Log)
52
53         ctx.Log.Infof("Starting Syncthing...")
54         ctx.SThgCmd, err = ctx.SThg.Start()
55         if err != nil {
56                 return cli.NewExitError(err, 2)
57         }
58         fmt.Printf("Syncthing started (PID %d)\n", ctx.SThgCmd.Process.Pid)
59
60         ctx.Log.Infof("Starting Syncthing-inotify...")
61         ctx.SThgInotCmd, err = ctx.SThg.StartInotify()
62         if err != nil {
63                 return cli.NewExitError(err, 2)
64         }
65         fmt.Printf("Syncthing-inotify started (PID %d)\n", ctx.SThgInotCmd.Process.Pid)
66
67         // Establish connection with local Syncthing (retry if connection fail)
68         time.Sleep(3 * time.Second)
69         maxRetry := 30
70         retry := maxRetry
71         for retry > 0 {
72                 if err := ctx.SThg.Connect(); err == nil {
73                         break
74                 }
75                 ctx.Log.Infof("Establishing connection to Syncthing (retry %d/%d", retry, maxRetry)
76                 time.Sleep(time.Second)
77                 retry--
78         }
79         if err != nil || retry == 0 {
80                 return cli.NewExitError(err, 2)
81         }
82
83         // Retrieve Syncthing config
84         id, err := ctx.SThg.IDGet()
85         if err != nil {
86                 return cli.NewExitError(err, 2)
87         }
88         ctx.Log.Infof("Local Syncthing ID: %s", id)
89
90         // Create and start Web Server
91         ctx.WWWServer = webserver.New(ctx.Config, ctx.Log)
92         if err = ctx.WWWServer.Serve(); err != nil {
93                 log.Println(err)
94                 return cli.NewExitError(err, 3)
95         }
96
97         return cli.NewExitError("Program exited ", 4)
98 }
99
100 // main
101 func main() {
102
103         // Create a new instance of the logger
104         log := logrus.New()
105
106         // Create a new App instance
107         app := cli.NewApp()
108         app.Name = appName
109         app.Description = appDescription
110         app.Usage = appUsage
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
118
119         app.Flags = []cli.Flag{
120                 cli.StringFlag{
121                         Name:   "config, c",
122                         Usage:  "JSON config file to use\n\t",
123                         EnvVar: "XDS_CONFIGFILE",
124                 },
125                 cli.StringFlag{
126                         Name:   "log, l",
127                         Value:  "error",
128                         Usage:  "logging level (supported levels: panic, fatal, error, warn, info, debug)\n\t",
129                         EnvVar: "LOG_LEVEL",
130                 },
131         }
132
133         // only one action
134         app.Action = xdsAgent
135
136         app.Run(os.Args)
137 }