summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.vscode/launch.json8
-rw-r--r--.vscode/settings.json2
-rw-r--r--glide.yaml2
-rw-r--r--main.go64
-rw-r--r--utils.go36
5 files changed, 84 insertions, 28 deletions
diff --git a/.vscode/launch.json b/.vscode/launch.json
index d85db0e..aadccfd 100644
--- a/.vscode/launch.json
+++ b/.vscode/launch.json
@@ -9,7 +9,7 @@
"env": {
"GOPATH": "${workspaceRoot}/../../../..:${env:GOPATH}",
"XDS_APPNAME": "xds-cli",
- "XDS_SERVER_URL": "localhost:8800",
+ "XDS_AGENT_URL": "localhost:8800",
"XDS_LOGLEVEL": "debug"
},
"args": ["misc", "version"],
@@ -24,7 +24,7 @@
"env": {
"GOPATH": "${workspaceRoot}/../../../..:${env:GOPATH}",
"XDS_APPNAME": "xds-cli",
- "XDS_SERVER_URL": "localhost:8800",
+ "XDS_AGENT_URL": "localhost:8800",
"XDS_LOGLEVEL": "debug"
},
"args": ["sdks", "list"],
@@ -39,7 +39,7 @@
"env": {
"GOPATH": "${workspaceRoot}/../../../..:${env:GOPATH}",
"XDS_APPNAME": "xds-cli",
- "XDS_SERVER_URL": "localhost:8800",
+ "XDS_AGENT_URL": "localhost:8800",
"XDS_LOGLEVEL": "debug"
},
"args": ["prj", "add",
@@ -58,7 +58,7 @@
"env": {
"GOPATH": "${workspaceRoot}/../../../..:${env:GOPATH}",
"XDS_APPNAME": "xds-cli",
- "XDS_SERVER_URL": "localhost:8800",
+ "XDS_AGENT_URL": "localhost:8800",
"XDS_LOGLEVEL": "debug"
},
"args": ["exec",
diff --git a/.vscode/settings.json b/.vscode/settings.json
index 7e90d15..6afb506 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -24,6 +24,6 @@
"cSpell.words": [
"apiv", "iosk", "zhouhui", "ldflags", "socketio", "xdsconfig", "sdkid",
"godotenv", "crosssdk", "prjs", "xaapiv", "urfave", "sebd", "golib",
- "joho", "XDSAGENT"
+ "joho", "XDSAGENT", "reflectme", "franciscocpg"
]
}
diff --git a/glide.yaml b/glide.yaml
index 830ac5d..681160e 100644
--- a/glide.yaml
+++ b/glide.yaml
@@ -23,3 +23,5 @@ import:
version: ^1.1.0
subpackages:
- cmd/godotenv
+- package: github.com/franciscocpg/reflectme
+ version: ^0.1.9
diff --git a/main.go b/main.go
index 1c0d886..197541d 100644
--- a/main.go
+++ b/main.go
@@ -29,6 +29,7 @@ import (
"text/tabwriter"
"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"
@@ -99,9 +100,9 @@ func main() {
Setting of global options is driven either by environment variables or by command
line options or using a config file knowning that the following priority order is used:
1. use option value (for example --url option),
- 2. else use variable 'XDS_xxx' (for example 'XDS_SERVER_URL' variable) when a
+ 2. else use variable 'XDS_xxx' (for example 'XDS_AGENT_URL' variable) when a
config file is specified with '--config|-c' option,
- 3. else use 'XDS_xxx' (for example 'XDS_SERVER_URL') environment variable.
+ 3. else use 'XDS_xxx' (for example 'XDS_AGENT_URL') environment variable.
Examples:
# Get help of 'projects' sub-command
@@ -163,9 +164,15 @@ func main() {
},
cli.StringFlag{
Name: "url, u",
+ EnvVar: "XDS_AGENT_URL",
+ Value: "localhost:8800",
+ Usage: "local XDS agent url",
+ },
+ cli.StringFlag{
+ Name: "url-server, us",
EnvVar: "XDS_SERVER_URL",
- Value: "localhost:8000",
- Usage: "remote XDS server url",
+ Value: "",
+ Usage: "overwrite remote XDS server url (default value set in xds-agent-config.json file)",
},
cli.BoolFlag{
Name: "timestamp, ts",
@@ -265,20 +272,26 @@ func XdsConnInit(ctx *cli.Context) error {
var err error
// Define HTTP and WS url
- baseURL := ctx.String("url")
+ agentURL := ctx.String("url")
+ serverURL := ctx.String("url-server")
// Allow to only set port number
- if match, _ := regexp.MatchString("^([0-9]+)$", baseURL); match {
- baseURL = "http://localhost:" + ctx.String("url")
+ if match, _ := regexp.MatchString("^([0-9]+)$", agentURL); match {
+ agentURL = "http://localhost:" + ctx.String("url")
+ }
+ if match, _ := regexp.MatchString("^([0-9]+)$", serverURL); match {
+ serverURL = "http://localhost:" + ctx.String("url-server")
}
-
// Add http prefix if missing
- if !strings.HasPrefix(baseURL, "http://") {
- baseURL = "http://" + baseURL
+ if agentURL != "" && !strings.HasPrefix(agentURL, "http://") {
+ agentURL = "http://" + agentURL
+ }
+ if serverURL != "" && !strings.HasPrefix(serverURL, "http://") {
+ serverURL = "http://" + serverURL
}
// Create HTTP client
- Log.Debugln("Connect HTTP client on ", baseURL)
+ Log.Debugln("Connect HTTP client on ", agentURL)
conf := common.HTTPClientConfig{
URLPrefix: "/api/v1",
HeaderClientKeyName: "Xds-Agent-Sid",
@@ -288,12 +301,12 @@ func XdsConnInit(ctx *cli.Context) error {
LogLevel: common.HTTPLogLevelWarning,
}
- HTTPCli, err = common.HTTPNewClient(baseURL, conf)
+ HTTPCli, err = common.HTTPNewClient(agentURL, conf)
if err != nil {
errmsg := err.Error()
if m, err := regexp.MatchString("Get http.?://", errmsg); m && err == nil {
i := strings.LastIndex(errmsg, ":")
- errmsg = "Cannot connection to " + baseURL + errmsg[i:]
+ errmsg = "Cannot connection to " + agentURL + errmsg[i:]
}
return cli.NewExitError(errmsg, 1)
}
@@ -301,7 +314,7 @@ func XdsConnInit(ctx *cli.Context) error {
Log.Infoln("HTTP session ID : ", HTTPCli.GetClientID())
// Create io Websocket client
- Log.Debugln("Connecting IO.socket client on ", baseURL)
+ Log.Debugln("Connecting IO.socket client on ", agentURL)
opts := &socketio_client.Options{
Transport: "websocket",
@@ -309,7 +322,7 @@ func XdsConnInit(ctx *cli.Context) error {
}
opts.Header["XDS-AGENT-SID"] = []string{HTTPCli.GetClientID()}
- IOsk, err = socketio_client.NewClient(baseURL, opts)
+ IOsk, err = socketio_client.NewClient(agentURL, opts)
if err != nil {
return cli.NewExitError("IO.socket connection error: "+err.Error(), 1)
}
@@ -321,6 +334,27 @@ func XdsConnInit(ctx *cli.Context) error {
ctx.App.Metadata["httpCli"] = HTTPCli
ctx.App.Metadata["ioskCli"] = IOsk
+ // Display version in logs (debug helpers)
+ ver := xaapiv1.XDSVersion{}
+ if err := XdsVersionGet(&ver); err != nil {
+ return cli.NewExitError("ERROR while retrieving XDS version: "+err.Error(), 1)
+ }
+ Log.Infof("XDS Agent/Server version: %v", ver)
+
+ // Get current config and update connection to server when needed
+ xdsConf := xaapiv1.APIConfig{}
+ if err := XdsConfigGet(&xdsConf); err != nil {
+ return cli.NewExitError("ERROR while getting XDS config: "+err.Error(), 1)
+ }
+ svrCfg := xdsConf.Servers[XdsServerIndexGet()]
+ if serverURL != "" && (svrCfg.URL != serverURL || !svrCfg.Connected) {
+ svrCfg.URL = serverURL
+ svrCfg.ConnRetry = 10
+ if err := XdsConfigSet(xdsConf); err != nil {
+ return cli.NewExitError("ERROR while updating XDS server URL: "+err.Error(), 1)
+ }
+ }
+
return nil
}
diff --git a/utils.go b/utils.go
index f70d5da..d632415 100644
--- a/utils.go
+++ b/utils.go
@@ -20,28 +20,48 @@ package main
import (
"encoding/json"
+ "fmt"
+ "github.com/franciscocpg/reflectme"
"github.com/iotbzh/xds-agent/lib/xaapiv1"
"github.com/urfave/cli"
)
var cacheXdsVersion *xaapiv1.XDSVersion
+var cacheData = xaapiv1.XDSVersion{}
// XdsVersionGet Get version of XDS agent & server
func XdsVersionGet(ver *xaapiv1.XDSVersion) error {
- // Use cached data
- if cacheXdsVersion != nil {
- ver = cacheXdsVersion
- return nil
+ // Update cached data
+ if cacheXdsVersion == nil {
+ if err := HTTPCli.Get("/version", &cacheData); err != nil {
+ return err
+ }
}
+ reflectme.Copy(&cacheData, ver)
+ return nil
+}
+
+// XdsConfigGet Get current XDS Agent config
+func XdsConfigGet(cfg *xaapiv1.APIConfig) error {
+ return HTTPCli.Get("/config", cfg)
+}
- dataVer := xaapiv1.XDSVersion{}
- if err := HTTPCli.Get("/version", &dataVer); err != nil {
+// XdsConfigSet Set XDS Agent config
+func XdsConfigSet(cfg xaapiv1.APIConfig) error {
+ // clear cache
+ cacheXdsVersion = nil
+
+ newCfg := xaapiv1.APIConfig{}
+ if err := HTTPCli.Post("/config", cfg, &newCfg); err != nil {
return err
}
- cacheXdsVersion = &dataVer
- *ver = dataVer
+ idx := XdsServerIndexGet()
+ if !newCfg.Servers[idx].Connected {
+ return fmt.Errorf("XDS server %s still not connected", cfg.Servers[idx].URL)
+ }
+
return nil
}