aboutsummaryrefslogtreecommitdiffstats
path: root/webapp/src/app/services/config.service.ts
blob: 138e0fb47a29545cfc509cc48200485a66dd7b36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import { Injectable, OnInit } from '@angular/core';
import { Http, Headers, RequestOptionsArgs, Response } from '@angular/http';
import { Location } from '@angular/common';
import { CookieService } from 'ngx-cookie';
import { Observable } from 'rxjs/Observable';
import { Subscriber } from 'rxjs/Subscriber';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';

// Import RxJs required methods
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/mergeMap';


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_PATHMAP = 1,
    SYNCTHING = 2
}

export var ProjectTypes = [
    { value: ProjectType.NATIVE_PATHMAP, display: "Path mapping" },
    { value: ProjectType.SYNCTHING, display: "Cloud Sync" }
];

export var ProjectStatus = {
    ErrorConfig: "ErrorConfig",
    Disable: "Disable",
    Enable: "Enable",
    Pause: "Pause",
    Syncing: "Syncing"
};

export interface IProject {
    id?: string;
    label: string;
    pathClient: string;
    pathServer?: string;
    type: ProjectType;
    status?: string;
    isInSync?: boolean;
    isUsable?: boolean;
    serverPrjDef?: IXDSFolderConfig;
    isExpanded?: boolean;
    visible?: boolean;
    defaultSdkID?: string;
}

export interface IXDSAgentConfig {
    URL: string;
    retry: number;
}

export interface ILocalSTConfig {
    ID: string;
    URL: string;
    retry: number;
    tilde: string;
}

export interface IxdsAgentPackage {
    os: string;
    arch: string;
    version: string;
    url: string;
}

export interface IConfig {
    xdsServerURL: string;
    xdsAgent: IXDSAgentConfig;
    xdsAgentPackages: IxdsAgentPackage[];
    projectsRootDir: string;
    projects: IProject[];
    localSThg: ILocalSTConfig;
}

@Injectable()
export class ConfigService {

    public conf: Observable<IConfig>;

    private confSubject: BehaviorSubject<IConfig>;
    private confStore: IConfig;
    private AgentConnectObs = null;
    private stConnectObs = null;

    constructor(private _window: Window,
        private cookie: CookieService,
        private xdsServerSvr: XDSServerService,
        private xdsAgentSvr: XDSAgentService,
        private stSvr: SyncthingService,
        private alert: AlertService,
        private utils: UtilsService,
    ) {
        this.load();
        this.confSubject = <BehaviorSubject<IConfig>>new BehaviorSubject(this.confStore);
        this.conf = this.confSubject.asObservable();

        // force to load projects
        this.loadProjects();
    }

    // Load config
    load() {
        // Try to retrieve previous config from cookie
        let cookConf = this.cookie.getObject("xds-config");
        if (cookConf != null) {
            this.confStore = <IConfig>cookConf;
        } else {
            // Set default config
            this.confStore = {
                xdsServerURL: this._window.location.origin + '/api/v1',
                xdsAgent: {
                    URL: 'http://localhost:8010',
                    retry: 10,
                },
                xdsAgentPackages: [],
                projectsRootDir: "",
                projects: [],
                localSThg: {
                    ID: null,
                    URL: "http://localhost:8384",
                    retry: 10,    // 10 seconds
                    tilde: "",
                }
            };
        }

        // Update XDS Agent tarball url
        this.xdsServerSvr.getXdsAgentInfo().subscribe(nfo => {
            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));
        });

        // Update Project data
        this.xdsServerSvr.FolderStateChange$.subscribe(prj => {
            let i = this._getProjectIdx(prj.id);
            if (i >= 0) {
                // XXX for now, only isInSync and status may change
                this.confStore.projects[i].isInSync = prj.isInSync;
                this.confStore.projects[i].status = prj.status;
                this.confStore.projects[i].isUsable = this._isUsableProject(prj);
                this.confSubject.next(Object.assign({}, this.confStore));
            }
        });
    }

    // Save config into cookie
    save() {
        // Notify subscribers
        this.confSubject.next(Object.assign({}, this.confStore));

        // Don't save projects in cookies (too big!)
        let cfg = Object.assign({}, this.confStore);
        delete (cfg.projects);
        this.cookie.putObject("xds-config", cfg);
    }

    loadProjects() {
        // Setup connection with local XDS agent
        if (this.AgentConnectObs) {
            try {
                this.AgentConnectObs.unsubscribe();
            } catch (err) { }
            this.AgentConnectObs = null;
        }

        let cfg = this.confStore.xdsAgent;
        this.AgentConnectObs = this.xdsAgentSvr.connect(cfg.retry, cfg.URL)
            .subscribe((sts) => {
                //console.log("Agent sts", sts);
                // FIXME: load projects from local XDS Agent and
                //  not directly from local syncthing
                this._loadProjectFromLocalST();

            }, error => {
                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>";

                    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);
                } else {
                    this.alert.error(error);
                }
            });
    }

    private _loadProjectFromLocalST() {
        // Remove previous subscriber if existing
        if (this.stConnectObs) {
            try {
                this.stConnectObs.unsubscribe();
            } catch (err) { }
            this.stConnectObs = null;
        }

        // FIXME: move this code and all logic about syncthing inside XDS Agent
        // Setup connection with local SyncThing
        let retry = this.confStore.localSThg.retry;
        let url = this.confStore.localSThg.URL;
        this.stConnectObs = this.stSvr.connect(retry, url).subscribe((sts) => {
            this.confStore.localSThg.ID = sts.ID;
            this.confStore.localSThg.tilde = sts.tilde;
            if (this.confStore.projectsRootDir === "") {
                this.confStore.projectsRootDir = sts.tilde;
            }

            // Rebuild projects definition from local and remote syncthing
            this.confStore.projects = [];

            this.xdsServerSvr.getProjects().subscribe(remotePrj => {
                this.stSvr.getProjects().subscribe(localPrj => {
                    remotePrj.forEach(rPrj => {
                        let lPrj = localPrj.filter(item => item.id === rPrj.id);
                        if (lPrj.length > 0 || rPrj.type === ProjectType.NATIVE_PATHMAP) {
                            this._addProject(rPrj, true);
                        }
                    });
                    this.confSubject.next(Object.assign({}, this.confStore));
                }), error => this.alert.error('Could not load initial state of local projects.');
            }), error => this.alert.error('Could not load initial state of remote projects.');

        }, error => {
            if (error.indexOf("Syncthing local daemon not responding") !== -1) {
                let msg = "<span><strong>" + error + "<br></strong>";
                msg += "Please check that local XDS-Agent is running.<br>";
                msg += "</span>";
                this.alert.error(msg);
            } else {
                this.alert.error(error);
            }
        });
    }

    set syncToolURL(url: string) {
        this.confStore.localSThg.URL = url;
        this.save();
    }

    set xdsAgentRetry(r: number) {
        this.confStore.localSThg.retry = r;
        this.confStore.xdsAgent.retry = r;
        this.save();
    }

    set xdsAgentUrl(url: string) {
        this.confStore.xdsAgent.URL = url;
        this.save();
    }


    set projectsRootDir(p: string) {
        if (p.charAt(0) === '~') {
            p = this.confStore.localSThg.tilde + p.substring(1);
        }
        this.confStore.projectsRootDir = p;
        this.save();
    }

    getLabelRootName(): string {
        let id = this.confStore.localSThg.ID;
        if (!id || id === "") {
            return null;
        }
        return id.slice(0, 15);
    }

    addProject(prj: IProject): Observable<IProject> {
        // Substitute tilde with to user home path
        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 (!((pathCli.charAt(0) === '/') ||
            (pathCli.charAt(1) === ':' && (pathCli.charAt(2) === '\\' || pathCli.charAt(2) === '/')))) {
            pathCli = this.confStore.projectsRootDir + '/' + pathCli;
        }

        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;
        return this.xdsServerSvr.addProject(xdsPrj)
            .flatMap(resStRemotePrj => {
                xdsPrj = resStRemotePrj;
                if (xdsPrj.type === ProjectType.SYNCTHING) {
                    // FIXME REWORK local ST config
                    //  move logic to server side tunneling-back by WS
                    let stData = xdsPrj.dataCloudSync;

                    // Now setup local config
                    let stLocPrj: ISyncThingProject = {
                        id: xdsPrj.id,
                        label: xdsPrj.label,
                        path: xdsPrj.path,
                        serverSyncThingID: stData.builderSThgID
                    };

                    // Set local Syncthing config
                    return this.stSvr.addProject(stLocPrj);

                } else {
                    return Observable.of(null);
                }
            })
            .map(resStLocalPrj => {
                this._addProject(xdsPrj);
                return newPrj;
            });
    }

    deleteProject(prj: IProject): Observable<IProject> {
        let idx = this._getProjectIdx(prj.id);
        let delPrj = prj;
        if (idx === -1) {
            throw new Error("Invalid project id (id=" + prj.id + ")");
        }
        return this.xdsServerSvr.deleteProject(prj.id)
            .flatMap(res => {
                if (prj.type === ProjectType.SYNCTHING) {
                    return this.stSvr.deleteProject(prj.id);
                }
                return Observable.of(null);
            })
            .map(res => {
                this.confStore.projects.splice(idx, 1);
                return delPrj;
            });
    }

    syncProject(prj: IProject): Observable<string> {
        let idx = this._getProjectIdx(prj.id);
        if (idx === -1) {
            throw new Error("Invalid project id (id=" + prj.id + ")");
        }
        return this.xdsServerSvr.syncProject(prj.id);
    }

    private _isUsableProject(p) {
        return p && p.isInSync &&
            (p.status === ProjectStatus.Enable) &&
            (p.status !== ProjectStatus.Syncing);
    }

    private _getProjectIdx(id: string): number {
        return this.confStore.projects.findIndex((item) => item.id === id);
    }

    private _addProject(rPrj: IXDSFolderConfig, noNext?: boolean) {

        // Convert XDSFolderConfig to IProject
        let pp: IProject = {
            id: rPrj.id,
            label: rPrj.label,
            pathClient: rPrj.path,
            pathServer: rPrj.dataPathMap.serverPath,
            type: rPrj.type,
            status: rPrj.status,
            isInSync: rPrj.isInSync,
            isUsable: this._isUsableProject(rPrj),
            defaultSdkID: rPrj.defaultSdkID,
            serverPrjDef: Object.assign({}, rPrj),  // do a copy
        };

        // add new project
        this.confStore.projects.push(pp);

        // sort project array
        this.confStore.projects.sort((a, b) => {
            if (a.label < b.label) {
                return -1;
            }
            if (a.label > b.label) {
                return 1;
            }
            return 0;
        });

        // FIXME: maybe reduce subject to only .project
        //this.confSubject.next(Object.assign({}, this.confStore).project);
        if (!noNext) {
            this.confSubject.next(Object.assign({}, this.confStore));
        }
    }
}