/*
- * Copyright (C) 2017 "IoT.bzh"
+ * Copyright (C) 2017-2018 "IoT.bzh"
* Author Sebastien Douheret <sebastien@iot.bzh>
*
* Licensed under the Apache License, Version 2.0 (the "License");
"strings"
"text/tabwriter"
+ "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent.git/lib/xaapiv1"
+ common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git/golib"
"github.com/Sirupsen/logrus"
- "github.com/iotbzh/xds-agent/lib/xaapiv1"
- common "github.com/iotbzh/xds-common/golib"
+
"github.com/joho/godotenv"
socketio_client "github.com/sebd71/go-socket.io-client"
"github.com/urfave/cli"
// Application details
const (
- appCopyright = "Copyright (C) 2017 IoT.bzh - Apache-2.0"
+ appCopyright = "Copyright (C) 2017-2018 IoT.bzh - Apache-2.0"
defaultLogLevel = "error"
)
// exitError exists this program with the specified error
func exitError(code int, f string, a ...interface{}) {
+ earlyDisplay()
err := fmt.Sprintf(f, a...)
fmt.Fprintf(os.Stderr, err+"\n")
os.Exit(code)
}
+// earlyDebug Used to log info before logger has been initialized
+var earlyDebug []string
+
+func earlyPrintf(format string, args ...interface{}) {
+ earlyDebug = append(earlyDebug, fmt.Sprintf(format, args...))
+}
+
+func earlyDisplay() {
+ for _, str := range earlyDebug {
+ Log.Infof("%s", str)
+ }
+ earlyDebug = []string{}
+}
+
// main
func main() {
- var earlyDebug []string
// Allow to set app name from cli (useful for debugging)
if AppName == "" {
// Load config file if requested
if confFile != "" {
- earlyDebug = append(earlyDebug, fmt.Sprintf("confFile detected: %v", confFile))
+ earlyPrintf("confFile detected: %v", confFile)
+ confFile, err := common.ResolveEnvVar(confFile)
+ if err != nil {
+ exitError(1, "Error while resolving confFile: %v", err)
+ }
+ earlyPrintf("Resolved confFile: %v", confFile)
if !common.Exists(confFile) {
exitError(1, "Error env config file not found")
}
// Load config file variables that will overwrite env variables
- err := godotenv.Overload(confFile)
+ err = godotenv.Overload(confFile)
if err != nil {
exitError(1, "Error loading env config file "+confFile)
}
if err != nil {
exitError(1, "Error reading env config file "+confFile)
}
- earlyDebug = append(earlyDebug, fmt.Sprintf("EnvConfFileMap: %v", EnvConfFileMap))
+ earlyPrintf("EnvConfFileMap: %v", EnvConfFileMap)
}
app.Before = func(ctx *cli.Context) error {
Log.Formatter = &logrus.TextFormatter{}
Log.Infof("%s version: %s", AppName, app.Version)
- for _, str := range earlyDebug {
- Log.Infof("%s", str)
- }
+ earlyDisplay()
Log.Debugf("\nEnvironment: %v\n", os.Environ())
if err = XdsConnInit(ctx); err != nil {
serverURL = "http://" + serverURL
}
+ lvl := common.HTTPLogLevelWarning
+ if Log.Level == logrus.DebugLevel {
+ lvl = common.HTTPLogLevelDebug
+ }
+
// Create HTTP client
Log.Debugln("Connect HTTP client on ", agentURL)
conf := common.HTTPClientConfig{
CsrfDisable: true,
LogOut: Log.Out,
LogPrefix: "XDSAGENT: ",
- LogLevel: common.HTTPLogLevelWarning,
+ LogLevel: lvl,
}
HTTPCli, err = common.HTTPNewClient(agentURL, conf)
if err != nil {
errmsg := err.Error()
- if m, err := regexp.MatchString("Get http.?://", errmsg); m && err == nil {
+ m, err := regexp.MatchString("Get http.?://", errmsg)
+ if (m && err == nil) || strings.Contains(errmsg, "Failed to get device ID") {
i := strings.LastIndex(errmsg, ":")
- errmsg = "Cannot connection to " + agentURL + errmsg[i:]
+ newErr := "Cannot connection to " + agentURL
+ if i > 0 {
+ newErr += " (" + strings.TrimSpace(errmsg[i+1:]) + ")"
+ } else {
+ newErr += " (" + strings.TrimSpace(errmsg) + ")"
+ }
+ errmsg = newErr
}
return cli.NewExitError(errmsg, 1)
}
if err := XdsConfigGet(&xdsConf); err != nil {
return cli.NewExitError("ERROR while getting XDS config: "+err.Error(), 1)
}
+ if len(xdsConf.Servers) < 1 {
+ return cli.NewExitError("No XDS Server connected", 1)
+ }
svrCfg := xdsConf.Servers[XdsServerIndexGet()]
if (serverURL != "" && svrCfg.URL != serverURL) || !svrCfg.Connected {
+ Log.Infof("Update XDS Server config: serverURL=%v, svrCfg=%v", serverURL, svrCfg)
if serverURL != "" {
svrCfg.URL = serverURL
}