Skip to content

Commit f65ddc4

Browse files
committed
refactor: turn datacenter-manager to ts
1 parent 9a18394 commit f65ddc4

3 files changed

Lines changed: 116 additions & 123 deletions

File tree

‎src/core/services/datacenter-manager.js‎

Lines changed: 0 additions & 120 deletions
This file was deleted.
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
import { logger } from 'kv-logger';
4+
import _ from 'lodash';
5+
import { AppError } from '../app-error';
6+
import { config } from '../config';
7+
import { createEmptyFolder, copy } from '../utils/common';
8+
import { calcAllFileSha256, packageHashSync } from '../utils/security';
9+
10+
const manifestFilename = 'manifest.json';
11+
const contentsName = 'contents';
12+
13+
class DataCenterManager {
14+
getDataDir() {
15+
return config.common.dataDir;
16+
}
17+
18+
hasPackageStoreSync(packageHash: string) {
19+
const dataDir = this.getDataDir();
20+
const packageHashPath = path.join(dataDir, packageHash);
21+
const manifestFile = path.join(packageHashPath, manifestFilename);
22+
const contentPath = path.join(packageHashPath, contentsName);
23+
return fs.existsSync(manifestFile) && fs.existsSync(contentPath);
24+
}
25+
26+
getPackageInfo(packageHash: string) {
27+
if (this.hasPackageStoreSync(packageHash)) {
28+
const dataDir = this.getDataDir();
29+
const packageHashPath = path.join(dataDir, packageHash);
30+
const manifestFile = path.join(packageHashPath, manifestFilename);
31+
const contentPath = path.join(packageHashPath, contentsName);
32+
return this.buildPackageInfo(packageHash, packageHashPath, contentPath, manifestFile);
33+
}
34+
throw new AppError("can't get PackageInfo");
35+
}
36+
37+
buildPackageInfo(
38+
packageHash: string,
39+
packageHashPath: string,
40+
contentPath: string,
41+
manifestFile: string,
42+
) {
43+
return {
44+
packageHash,
45+
path: packageHashPath,
46+
contentPath,
47+
manifestFilePath: manifestFile,
48+
};
49+
}
50+
51+
validateStore(providePackageHash: string) {
52+
const dataDir = this.getDataDir();
53+
const packageHashPath = path.join(dataDir, providePackageHash);
54+
const manifestFile = path.join(packageHashPath, manifestFilename);
55+
const contentPath = path.join(packageHashPath, contentsName);
56+
if (!this.hasPackageStoreSync(providePackageHash)) {
57+
logger.debug(`validateStore providePackageHash not exist`);
58+
return Promise.resolve(false);
59+
}
60+
return calcAllFileSha256(contentPath).then((manifestJson) => {
61+
const packageHash = packageHashSync(manifestJson);
62+
let manifestJsonLocal;
63+
try {
64+
manifestJsonLocal = JSON.parse(fs.readFileSync(manifestFile, { encoding: 'utf8' }));
65+
} catch (e) {
66+
logger.debug(`validateStore manifestFile contents invilad`);
67+
return false;
68+
}
69+
const packageHashLocal = packageHashSync(manifestJsonLocal);
70+
if (
71+
_.eq(providePackageHash, packageHash) &&
72+
_.eq(providePackageHash, packageHashLocal)
73+
) {
74+
logger.debug(`validateStore store files is ok`);
75+
return true;
76+
}
77+
logger.debug(`validateStore store files broken`);
78+
return false;
79+
});
80+
}
81+
82+
storePackage(sourceDst: string, force = false) {
83+
return calcAllFileSha256(sourceDst).then((manifestJson) => {
84+
const packageHash = packageHashSync(manifestJson);
85+
const dataDir = this.getDataDir();
86+
const packageHashPath = path.join(dataDir, packageHash);
87+
const manifestFile = path.join(packageHashPath, manifestFilename);
88+
const contentPath = path.join(packageHashPath, contentsName);
89+
return this.validateStore(packageHash).then((isValidate) => {
90+
if (!force && isValidate) {
91+
return this.buildPackageInfo(
92+
packageHash,
93+
packageHashPath,
94+
contentPath,
95+
manifestFile,
96+
);
97+
}
98+
return createEmptyFolder(packageHashPath).then(() => {
99+
return copy(sourceDst, contentPath).then(() => {
100+
const manifestString = JSON.stringify(manifestJson);
101+
fs.writeFileSync(manifestFile, manifestString);
102+
return this.buildPackageInfo(
103+
packageHash,
104+
packageHashPath,
105+
contentPath,
106+
manifestFile,
107+
);
108+
});
109+
});
110+
});
111+
});
112+
}
113+
}
114+
115+
export const dataCenterManager = new DataCenterManager();

‎src/core/services/package-manager.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import _ from 'lodash';
88
import { Op } from 'sequelize';
99
import slash from 'slash';
1010
import yazl from 'yazl';
11-
1211
import { Apps } from '../../models/apps';
1312
import { Deployments, generateDeploymentsLabelId } from '../../models/deployments';
1413
import { DeploymentsHistory } from '../../models/deployments_history';
@@ -41,8 +40,7 @@ import { sequelize } from '../utils/connections';
4140
import { qetag } from '../utils/qetag';
4241
import { randToken, uploadPackageType } from '../utils/security';
4342
import { uploadFileToStorage } from '../utils/storage';
44-
45-
const dataCenterManager = require('./datacenter-manager')();
43+
import { dataCenterManager } from './datacenter-manager';
4644

4745
class PackageManager {
4846
getMetricsbyPackageId(packageId) {

0 commit comments

Comments
 (0)