-
Notifications
You must be signed in to change notification settings - Fork 28
/
app-manager.js
175 lines (147 loc) · 6.29 KB
/
app-manager.js
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
(function () {
const fse = require('fs-extra');
const path = require('path');
const express = require('express');
const _ = require('lodash');
require('json5/lib/register');
const cfg = require('./config.js');
const logger = cfg.log4js.getLogger('AppManager');
const archives = require('./service/archive-manager');
const fabricCLI = require('./fabric-cli');
const fileUtils = require('./util/fileUtils')
class AppManager {
async provisionWebApp(fileObj) {
let baseFileName = fileUtils.getFileBaseName(_.get(fileObj, 'originalname'));
const appFolderPath = path.join(cfg.WEBAPPS_DIR, baseFileName);
return archives.extract(fileObj.path, fileObj.originalname, cfg.WEBAPPS_DIR, true)
.then(() => {
return {context: baseFileName, folder: appFolderPath};
});
}
async getWebAppsList() {
return new Promise((resolve, reject) => {
fse.readdir(cfg.WEBAPPS_DIR, (err, fileList) => {
return err ? resolve([]) : resolve(fileList);
})
})
}
async provisionMiddleware(fileObj) {
const extractPath = cfg.MIDDLWARE_DIR;
if (!fse.existsSync(extractPath)) {
fse.mkdir(extractPath);
}
return new Promise((resolve, reject) => {
const destFile = path.resolve(extractPath, fileObj.originalname);
fse.copyFile(fileObj.path, destFile, err => {
return err ? reject(err) : resolve(destFile);
});
});
}
async getMiddlewareList() {
return new Promise((resolve, reject) => {
fse.readdir(cfg.MIDDLWARE_DIR, (err, fileList) => {
return err ? resolve([]) : resolve(fileList);
})
})
}
redeployWebapp(expressApp, appContext, appFolder) {
expressApp.use(`/${cfg.WEBAPPS_DIR}/${appContext}`, express.static(appFolder));
}
async redeployAllAppstoreApps(expressApp) {
let appsCfgs = await this.loadAppStoreConfig();
_.each(appsCfgs, cfg => {
this.deployAppstoreApp(cfg.name, cfg.folder, cfg.port);
})
}
async provisionAppstoreApp(expressApp, fileObj) {
try {
let baseFileName = fileUtils.getFileBaseName(_.get(fileObj, 'originalname'));
const appFolderPath = path.join(cfg.APPSTORE_DIR, baseFileName);
logger.debug("Provisioning Appstore app", appFolderPath, fileObj);
let extractPath = await archives.extract(fileObj.path, fileObj.originalname, appFolderPath, true);
let port = await this.assignPortAndSave(baseFileName, extractPath);
return await this.deployAppstoreApp(baseFileName, extractPath, port, expressApp);
} catch (e) {
console.error('Error deploying app:', e)
throw e
}
}
async deployAppstoreApp(appName, extractPath, port, app) {
let result = fabricCLI.execShellCommand("docker-compose up -d --force-recreate", extractPath, {
PORT: port
});
if (!result.isError()) {
await this.deployWebappIfPresent(extractPath, appName, app);
return result;
}
throw new Error(`Docker-compose up error. Return code: ${result.code}, Console output: ${result.output}`);
}
async assignPortAndSave(appName, extractPath) {
let appsCfgs = await this.loadAppStoreConfig();
let port = PortAssigner.assignAppOnPort(appsCfgs, appName);
logger.debug("Port assigned:", port);
await this.saveAppStoreConfig(_.assign(appsCfgs, {
[appName]: {
name: appName,
folder: extractPath,
port: port
}
}));
return port;
}
async deployWebappIfPresent(extractPath, baseFileName, app) {
const webappDir = path.join(extractPath, "webapp");
if (fse.existsSync(webappDir)) {
const appFolder = path.join(cfg.WEBAPPS_DIR, baseFileName);
await fse.copy(webappDir, appFolder);
this.redeployWebapp(app, baseFileName, appFolder);
}
}
async getAppstoreList() {
return this.loadAppStoreConfig()
.then(appsCfgs => _.map(appsCfgs, e => e));
}
async getAppStatus(appName) {
let result = fabricCLI.execShellCommand(`docker logs --tail 1000 ${appName}.${cfg.org}.${cfg.domain}`);
return {output: _.split(result.stdout, '\n')};
}
async loadAppStoreConfig() {
const appCfgFile = this.getAppCfgFile();
return fse.ensureFile(appCfgFile)
.then(() => fse.readJson(appCfgFile))
.catch(() => new Object({}));
}
async saveAppStoreConfig(appsCfg) {
return fse.outputJson(this.getAppCfgFile(), appsCfg);
}
getAppCfgFile() {
const appCfgFile = path.join(cfg.APPSTORE_DIR, "./deployed-apps.cfg");
return appCfgFile;
}
}
class PortAssigner {
static assignAppOnPort(appsCfgs, appName) {
let port = appsCfgs[appName]
? _.get(appsCfgs, `${appName}.port`)
: this.assignFreePort(appsCfgs);
return port;
}
static assignFreePort(appsCfgs) {
const portRangesSequence = _.split(cfg.CUSTOM_APP_PORTS, ',');
for (let i = 0; i < _.size(portRangesSequence); i++) {
const portsRange = _.split(portRangesSequence[i], '-');
let port = _.get(portsRange, "[0]");
const rangeEnd = _.get(portsRange, "[1]") || port;
while (port <= rangeEnd) {
if (!_.find(appsCfgs, item => item.port == port)) {
return port;
}
port++;
}
}
throw new Error("No free ports in port range")
}
}
module.exports = new AppManager();
})
();