Auto detect XDS-Agent tarballs and fix URL.
authorSebastien Douheret <sebastien.douheret@iot.bzh>
Mon, 22 May 2017 20:22:05 +0000 (22:22 +0200)
committerSebastien Douheret <sebastien.douheret@iot.bzh>
Wed, 24 May 2017 22:15:10 +0000 (00:15 +0200)
Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
lib/apiv1/agent.go
webapp/src/app/common/xdsagent.service.ts
webapp/src/app/config/config.component.html

index 7434545..d8feb61 100644 (file)
@@ -2,15 +2,21 @@ package apiv1
 
 import (
        "net/http"
+       "path"
+       "strings"
 
        "path/filepath"
 
        "github.com/gin-gonic/gin"
+       "github.com/iotbzh/xds-agent/lib/common"
 )
 
 type XDSAgentTarball struct {
-       OS      string `json:"os"`
-       FileURL string `json:"fileUrl"`
+       OS         string `json:"os"`
+       Arch       string `json:"arch"`
+       Version    string `json:"version"`
+       RawVersion string `json:"raw-version"`
+       FileURL    string `json:"fileUrl"`
 }
 type XDSAgentInfo struct {
        Tarballs []XDSAgentTarball `json:"tarballs"`
@@ -18,20 +24,44 @@ type XDSAgentInfo struct {
 
 // getXdsAgentInfo : return various information about Xds Agent
 func (s *APIService) getXdsAgentInfo(c *gin.Context) {
-       // TODO: retrieve link dynamically by reading assets/xds-agent-tarballs
-       tarballDir := "assets/xds-agent-tarballs"
-       response := XDSAgentInfo{
-               Tarballs: []XDSAgentTarball{
-                       XDSAgentTarball{
-                               OS:      "linux",
-                               FileURL: filepath.Join(tarballDir, "xds-agent_linux-amd64-v0.0.1_3cdf92c.zip"),
-                       },
-                       XDSAgentTarball{
-                               OS:      "windows",
-                               FileURL: filepath.Join(tarballDir, "xds-agent_windows-386-v0.0.1_3cdf92c.zip"),
-                       },
-               },
+
+       res := XDSAgentInfo{}
+       tarballURL := "assets/xds-agent-tarballs"
+       tarballDir := filepath.Join(s.cfg.WebAppDir, "assets", "xds-agent-tarballs")
+       if common.Exists(tarballDir) {
+               files, err := filepath.Glob(path.Join(tarballDir, "xds-agent_*.zip"))
+               if err != nil {
+                       s.log.Debugf("Error while retrieving xds-agent tarballs: dir=%s, error=%v", tarballDir, err)
+               }
+               for _, ff := range files {
+                       file := filepath.Base(ff)
+                       // Assume that tarball name format is: xds-agent_OS-ARCH-RAWVERSION.zip
+                       fs := strings.TrimSuffix(strings.TrimPrefix(file, "xds-agent_"), ".zip")
+                       f := strings.Split(fs, "-")
+
+                       if len(f) >= 3 {
+                               vers := strings.Split(f[2], "_")
+                               ver := f[2]
+                               if len(vers) > 1 {
+                                       ver = vers[0]
+                               }
+
+                               newT := XDSAgentTarball{
+                                       OS:         f[0],
+                                       Arch:       f[1],
+                                       Version:    ver,
+                                       RawVersion: f[2],
+                                       FileURL:    filepath.Join(tarballURL, file),
+                               }
+
+                               s.log.Infof("Added XDS-Agent tarball: %s", file)
+                               res.Tarballs = append(res.Tarballs, newT)
+
+                       } else {
+                               s.log.Debugf("Error while retrieving xds-agent, decoding failure: file:%v", ff)
+                       }
+               }
        }
 
-       c.JSON(http.StatusOK, response)
+       c.JSON(http.StatusOK, res)
 }
index 4d9aadc..86f0336 100644 (file)
@@ -59,20 +59,12 @@ export class XDSAgentService {
         this.Status$ = this.statusSubject.asObservable();
 
         this.apikey = DEFAULT_API_KEY; // FIXME Add dynamic allocated key
-        this._status.baseURL = 'http://localhost:' + DEFAULT_PORT;
-        this.baseRestUrl = this._status.baseURL + '/api/' + API_VERSION;
-        let re = this._window.location.origin.match(/http[s]?:\/\/([^\/]*)[\/]?/);
-        if (re === null || re.length < 2) {
-            console.error('ERROR: cannot determine Websocket url');
-        } else {
-            this.wsUrl = 'ws://' + re[1];
-        }
+        this._initURLs('http://localhost:' + DEFAULT_PORT);
     }
 
     connect(retry: number, url?: string): Observable<IAgentStatus> {
         if (url) {
-            this._status.baseURL = url;
-            this.baseRestUrl = this._status.baseURL + '/api/' + API_VERSION;
+            this._initURLs(url);
         }
         //FIXME [XDS-Agent]: not implemented yet, set always as connected
         //this._status.connected = false;
@@ -103,6 +95,18 @@ export class XDSAgentService {
         });
     }
 
+    private _initURLs(url: string) {
+        this._status.baseURL = url;
+        this.baseRestUrl = this._status.baseURL + '/api/' + API_VERSION;
+        let re = this._status.baseURL.match(/http[s]?:\/\/([^\/]*)[\/]?/);
+        if (re === null || re.length < 2) {
+            this.wsUrl = '';
+            console.error('ERROR: cannot determine Websocket url');
+            return;
+        }
+        this.wsUrl = 'ws://' + re[1];
+    }
+
     private _WSState(sts: boolean) {
         this._status.WS_connected = sts;
         this.statusSubject.next(Object.assign({}, this._status));
index f707bbe..8737d08 100644 (file)
             <div class="col-xs-12">
                 <table class="table table-condensed">
                     <tbody>
-                        <!-- FIXME [XDS-Agent]
                         <tr [ngClass]="{'info': (agentStatus$ | async)?.connected, 'danger': !(agentStatus$ | async)?.connected}">
-                        -->
-                        <tr [ngClass]="{'info': (localSTStatus$ | async)?.connected, 'danger': !(localSTStatus$ | async)?.connected}">
                             <th><label>XDS local Agent URL</label></th>
                             <td> <input type="text" [(ngModel)]="xdsAgentUrl"></td>
                             <td>
                                 <button class="btn btn-link" (click)="xdsAgentRestartConn()"><span class="fa fa-refresh fa-size-x2"></span></button>
-                                <!-- FIXME [XDS-Agent]
-                                <button  *ngIf="!(agentStatus$ | async)?.connected"  -->
-                                <button  *ngIf="!(localSTStatus$ | async)?.connected" [disabled]="xdsAgentZipUrl==''" class="btn btn-link">
+                                <button [disabled]="xdsAgentZipUrl==''" class="btn btn-link">
                                     <a *ngIf="xdsAgentZipUrl!=''" class="fa fa-download fa-size-x2" [href]="xdsAgentZipUrl" target="_blank"></a>
                                     <a *ngIf="xdsAgentZipUrl==''" class="fa fa-download fa-size-x2" href="javascript:void(0);" target="_blank"></a>
                                 </button>