X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?p=src%2Fxds%2Fxds-agent.git;a=blobdiff_plain;f=main.go;h=a02eba5b823b1290573992bc04f5871908f37e41;hp=93d13a26ef9e4f18d8725f8cc1f04f924b369010;hb=c2558601c7ce2b7fd7eaf7c2336fb78242781e50;hpb=bfeab33538d50ee52750de4dd4c0e72b64f674f6 diff --git a/main.go b/main.go index 93d13a2..a02eba5 100644 --- a/main.go +++ b/main.go @@ -1,26 +1,38 @@ -// TODO add Doc -// +/* + * Copyright (C) 2017-2018 "IoT.bzh" + * Author Sebastien Douheret + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + * xds-agent: X(cross) Development System client running on developer/local host. + */ + package main import ( - "log" "os" - "time" - - "fmt" + "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent/lib/agent" + "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent/lib/xdsconfig" "github.com/Sirupsen/logrus" - "github.com/codegangsta/cli" - "github.com/iotbzh/xds-agent/lib/agent" - "github.com/iotbzh/xds-agent/lib/syncthing" - "github.com/iotbzh/xds-agent/lib/xdsconfig" - "github.com/iotbzh/xds-agent/lib/xdsserver" + "github.com/urfave/cli" ) const ( appName = "xds-agent" appDescription = "X(cross) Development System Agent is a web server that allows to remotely cross build applications." - appCopyright = "Apache-2.0" + appCopyright = "Copyright (C) 2017-2018 IoT.bzh - Apache-2.0" appUsage = "X(cross) Development System Agent" ) @@ -37,65 +49,21 @@ var AppSubVersion = "unknown-dev" // xdsAgent main routine func xdsAgent(cliCtx *cli.Context) error { + var err error // Create Agent context - ctx := agent.NewAgent(cliCtx) + ctxAgent := agent.NewAgent(cliCtx) // Load config - cfg, err := xdsconfig.Init(cliCtx, ctx.Log) + ctxAgent.Config, err = xdsconfig.Init(cliCtx, ctxAgent.Log) if err != nil { return cli.NewExitError(err, 2) } - ctx.Config = &cfg - - // Start local instance of Syncthing and Syncthing-notify - ctx.SThg = st.NewSyncThing(ctx.Config.FileConf.SThgConf, ctx.Log) - ctx.Log.Infof("Starting Syncthing...") - ctx.SThgCmd, err = ctx.SThg.Start() - if err != nil { - return cli.NewExitError(err, 2) - } - ctx.Log.Infof("Syncthing started (PID %d)", ctx.SThgCmd.Process.Pid) + // Run Agent (main loop) + errCode, err := ctxAgent.Run() - ctx.Log.Infof("Starting Syncthing-inotify...") - ctx.SThgInotCmd, err = ctx.SThg.StartInotify() - if err != nil { - return cli.NewExitError(err, 2) - } - ctx.Log.Infof("Syncthing-inotify started (PID %d)", ctx.SThgInotCmd.Process.Pid) - - // Establish connection with local Syncthing (retry if connection fail) - time.Sleep(3 * time.Second) - retry := 10 - for retry > 0 { - if err := ctx.SThg.Connect(); err == nil { - break - } - ctx.Log.Infof("Establishing connection to Syncthing (retry %d/5)", retry) - time.Sleep(time.Second) - retry-- - } - if ctx.SThg == nil { - err = fmt.Errorf("ERROR: cannot connect to Syncthing (url: %s)", ctx.SThg.BaseURL) - return cli.NewExitError(err, 2) - } - - // Retrieve Syncthing config - id, err := ctx.SThg.IDGet() - if err != nil { - return cli.NewExitError(err, 2) - } - ctx.Log.Infof("Local Syncthing ID: %s", id) - - // Create and start Web Server - ctx.WWWServer = xdsserver.NewServer(ctx.Config, ctx.Log) - if err = ctx.WWWServer.Serve(); err != nil { - log.Println(err) - return cli.NewExitError(err, 3) - } - - return cli.NewExitError("Program exited ", 4) + return cli.NewExitError(err, errCode) } // main @@ -127,7 +95,13 @@ func main() { Name: "log, l", Value: "error", Usage: "logging level (supported levels: panic, fatal, error, warn, info, debug)\n\t", - EnvVar: "LOG_LEVEL", + EnvVar: "XDS_LOGLEVEL", + }, + cli.StringFlag{ + Name: "logfile", + Value: "stdout", + Usage: "filename where logs will be redirected (default stdout)\n\t", + EnvVar: "XDS_LOGFILE", }, }