Add folder interface and support native pathmap folder type.
[src/xds/xds-server.git] / webapp / src / app / services / config.service.ts
index 9b9f5db..c65332f 100644 (file)
@@ -13,17 +13,22 @@ import 'rxjs/add/observable/throw';
 import 'rxjs/add/operator/mergeMap';
 
 
-import { XDSServerService, IXDSConfigProject } from "../services/xdsserver.service";
+import { XDSServerService, IXDSFolderConfig } from "../services/xdsserver.service";
 import { XDSAgentService } from "../services/xdsagent.service";
 import { SyncthingService, ISyncThingProject, ISyncThingStatus } from "../services/syncthing.service";
 import { AlertService, IAlert } from "../services/alert.service";
 import { UtilsService } from "../services/utils.service";
 
 export enum ProjectType {
-    NATIVE = 1,
+    NATIVE_PATHMAP = 1,
     SYNCTHING = 2
 }
 
+export var ProjectTypes = [
+    { value: ProjectType.NATIVE_PATHMAP, display: "Path mapping" },
+    { value: ProjectType.SYNCTHING, display: "Cloud Sync" }
+];
+
 export interface INativeProject {
     // TODO
 }
@@ -31,7 +36,8 @@ export interface INativeProject {
 export interface IProject {
     id?: string;
     label: string;
-    path: string;
+    pathClient: string;
+    pathServer?: string;
     type: ProjectType;
     remotePrjDef?: INativeProject | ISyncThingProject;
     localPrjDef?: any;
@@ -52,10 +58,17 @@ export interface ILocalSTConfig {
     tilde: string;
 }
 
+export interface IxdsAgentPackage {
+    os: string;
+    arch: string;
+    version: string;
+    url: string;
+}
+
 export interface IConfig {
     xdsServerURL: string;
     xdsAgent: IXDSAgentConfig;
-    xdsAgentZipUrl: string;
+    xdsAgentPackages: IxdsAgentPackage[];
     projectsRootDir: string;
     projects: IProject[];
     localSThg: ILocalSTConfig;
@@ -70,7 +83,6 @@ export class ConfigService {
     private confStore: IConfig;
     private AgentConnectObs = null;
     private stConnectObs = null;
-    private xdsAgentZipUrl = "";
 
     constructor(private _window: Window,
         private cookie: CookieService,
@@ -102,7 +114,7 @@ export class ConfigService {
                     URL: 'http://localhost:8000',
                     retry: 10,
                 },
-                xdsAgentZipUrl: "",
+                xdsAgentPackages: [],
                 projectsRootDir: "",
                 projects: [],
                 localSThg: {
@@ -115,14 +127,17 @@ export class ConfigService {
         }
 
         // Update XDS Agent tarball url
-        this.confStore.xdsAgentZipUrl = "";
         this.xdsServerSvr.getXdsAgentInfo().subscribe(nfo => {
-            let os = this.utils.getOSName(true);
-            let zurl = nfo.tarballs && nfo.tarballs.filter(elem => elem.os === os);
-            if (zurl && zurl.length) {
-                this.confStore.xdsAgentZipUrl = zurl[0].fileUrl;
-                this.confSubject.next(Object.assign({}, this.confStore));
-            }
+            this.confStore.xdsAgentPackages = [];
+            nfo.tarballs && nfo.tarballs.forEach(el =>
+                this.confStore.xdsAgentPackages.push({
+                    os: el.os,
+                    arch: el.arch,
+                    version: el.version,
+                    url: el.fileUrl
+                })
+            );
+            this.confSubject.next(Object.assign({}, this.confStore));
         });
     }
 
@@ -158,9 +173,12 @@ export class ConfigService {
                 if (error.indexOf("XDS local Agent not responding") !== -1) {
                     let msg = "<span><strong>" + error + "<br></strong>";
                     msg += "You may need to download and execute XDS-Agent.<br>";
-                    if (this.confStore.xdsAgentZipUrl !== "") {
-                        msg += "<a class=\"fa fa-download\" href=\"" + this.confStore.xdsAgentZipUrl + "\" target=\"_blank\"></a>";
-                        msg += " Download XDS-Agent tarball.";
+
+                    let os = this.utils.getOSName(true);
+                    let zurl = this.confStore.xdsAgentPackages && this.confStore.xdsAgentPackages.filter(elem => elem.os === os);
+                    if (zurl && zurl.length) {
+                        msg += " Download XDS-Agent tarball for " + zurl[0].os + " host OS ";
+                        msg += "<a class=\"fa fa-download\" href=\"" + zurl[0].url + "\" target=\"_blank\"></a>";
                     }
                     msg += "</span>";
                     this.alert.error(msg);
@@ -201,8 +219,9 @@ export class ConfigService {
                             let pp: IProject = {
                                 id: rPrj.id,
                                 label: rPrj.label,
-                                path: rPrj.path,
-                                type: ProjectType.SYNCTHING,    // FIXME support other types
+                                pathClient: rPrj.path,
+                                pathServer: rPrj.dataPathMap.serverPath,
+                                type: rPrj.type,
                                 remotePrjDef: Object.assign({}, rPrj),
                                 localPrjDef: Object.assign({}, lPrj[0]),
                             };
@@ -260,57 +279,46 @@ export class ConfigService {
 
     addProject(prj: IProject) {
         // Substitute tilde with to user home path
-        prj.path = prj.path.trim();
-        if (prj.path.charAt(0) === '~') {
-            prj.path = this.confStore.localSThg.tilde + prj.path.substring(1);
+        let pathCli = prj.pathClient.trim();
+        if (pathCli.charAt(0) === '~') {
+            pathCli = this.confStore.localSThg.tilde + pathCli.substring(1);
 
             // Must be a full path (on Linux or Windows)
-        } else if (!((prj.path.charAt(0) === '/') ||
-            (prj.path.charAt(1) === ':' && (prj.path.charAt(2) === '\\' || prj.path.charAt(2) === '/')))) {
-            prj.path = this.confStore.projectsRootDir + '/' + prj.path;
-        }
-
-        if (prj.id == null) {
-            // FIXME - must be done on server side
-            let prefix = this.getLabelRootName() || new Date().toISOString();
-            let splath = prj.path.split('/');
-            prj.id = prefix + "_" + splath[splath.length - 1];
+        } else if (!((pathCli.charAt(0) === '/') ||
+            (pathCli.charAt(1) === ':' && (pathCli.charAt(2) === '\\' || pathCli.charAt(2) === '/')))) {
+            pathCli = this.confStore.projectsRootDir + '/' + pathCli;
         }
 
-        if (this._getProjectIdx(prj.id) !== -1) {
-            this.alert.warning("Project already exist (id=" + prj.id + ")", true);
-            return;
-        }
-
-        // TODO - support others project types
-        if (prj.type !== ProjectType.SYNCTHING) {
-            this.alert.error('Project type not supported yet (type: ' + prj.type + ')');
-            return;
-        }
-
-        let sdkPrj: IXDSConfigProject = {
-            id: prj.id,
-            label: prj.label,
-            path: prj.path,
-            hostSyncThingID: this.confStore.localSThg.ID,
+        let xdsPrj: IXDSFolderConfig = {
+            id: "",
+            label: prj.label || "",
+            path: pathCli,
+            type: prj.type,
             defaultSdkID: prj.defaultSdkID,
+            dataPathMap: {
+                serverPath: prj.pathServer,
+            },
+            dataCloudSync: {
+                syncThingID: this.confStore.localSThg.ID,
+            }
         };
-
         // Send config to XDS server
         let newPrj = prj;
-        this.xdsServerSvr.addProject(sdkPrj)
+        this.xdsServerSvr.addProject(xdsPrj)
             .subscribe(resStRemotePrj => {
                 newPrj.remotePrjDef = resStRemotePrj;
+                newPrj.id = resStRemotePrj.id;
 
                 // FIXME REWORK local ST config
                 //  move logic to server side tunneling-back by WS
+                let stData = resStRemotePrj.dataCloudSync;
 
                 // Now setup local config
                 let stLocPrj: ISyncThingProject = {
-                    id: sdkPrj.id,
-                    label: sdkPrj.label,
-                    path: sdkPrj.path,
-                    remoteSyncThingID: resStRemotePrj.builderSThgID
+                    id: resStRemotePrj.id,
+                    label: xdsPrj.label,
+                    path: xdsPrj.path,
+                    serverSyncThingID: stData.builderSThgID
                 };
 
                 // Set local Syncthing config
@@ -354,4 +362,4 @@ export class ConfigService {
         return this.confStore.projects.findIndex((item) => item.id === id);
     }
 
-}
\ No newline at end of file
+}