Skip to content

Commit d4cd3fa

Browse files
authored
Merge pull request #11 from yanglong1010/alibaba-dragonwell-support
3 enhancements
2 parents 108aab8 + 427de32 commit d4cd3fa

File tree

2 files changed

+85
-27
lines changed

2 files changed

+85
-27
lines changed

src/javaIndex.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ export class JavaStartupAcceleration {
154154
info("oss endpoint: " + this.ossEndpoint)
155155
}
156156
await this.genDump();
157-
info("completed");
157+
info("all completed");
158158
}
159159

160160
async enableQuickStart() {
@@ -220,6 +220,7 @@ export class JavaStartupAcceleration {
220220
error(e.stderr.toString());
221221
error(e.stdout.toString());
222222
} finally {
223+
info("start to clean");
223224
/* delete local temp files */
224225
await remove(tmpDir);
225226
await remove(tmpZipFilePath);
@@ -256,15 +257,16 @@ export class JavaStartupAcceleration {
256257

257258
let result = await fcClient.post(`/proxy/${tmpServiceName}/${tmpFunctionName}/action`, body, null);
258259
let data = result.data;
259-
info("server messages: " + data)
260+
info("server messages: " + data);
260261
if (data.indexOf("success") == 0) {
261-
info("dumped successfully")
262+
info("dumped successfully");
262263
} else {
263264
throw new Error("dump encountered error");
264265
}
265266
}
266267

267268
private async downloadAccelerationFiles(fcClient, tmpServiceName: string, tmpFunctionName: string) {
269+
info("downloading");
268270
let sharedDir = join(this.artifactPath, this.sharedDirName);
269271
await ensureDir(sharedDir);
270272
let localFile = join(sharedDir, ARCHIVE_NAME);
@@ -278,9 +280,11 @@ export class JavaStartupAcceleration {
278280

279281
await this.extractTar(sharedDir, localFile);
280282
removeSync(localFile);
283+
info("download completed");
281284
}
282285

283286
private async createTempFunction(fcClient, tmpServiceName: string, tmpFunctionName: string, tmpZipFilePath: string) {
287+
info("assistant function [" + tmpFunctionName + "] creating");
284288
await fcClient.createFunction(tmpServiceName, {
285289
code: {
286290
zipFile: readFileSync(tmpZipFilePath, 'base64'),
@@ -300,10 +304,11 @@ export class JavaStartupAcceleration {
300304
SRPATH: this.tmpSrpath
301305
}
302306
});
303-
info("assistant function created")
307+
info("assistant function [" + tmpFunctionName + "] created");
304308
}
305309

306310
private static async createTempTrigger(fcClient, tmpServiceName: string, tmpFunctionName: string, tmpTriggerName: string) {
311+
info("assistant trigger [" + tmpTriggerName + "] creating");
307312
await fcClient.createTrigger(tmpServiceName, tmpFunctionName, {
308313
invocationRole: '',
309314
qualifier: 'LATEST',
@@ -312,10 +317,11 @@ export class JavaStartupAcceleration {
312317
triggerName: tmpTriggerName,
313318
triggerType: 'http'
314319
});
315-
info("assistant trigger created")
320+
info("assistant trigger [" + tmpTriggerName + "] created");
316321
}
317322

318323
private async createTempService(fcClient, tmpServiceName) {
324+
info("assistant service [" + tmpServiceName + "] creating");
319325
await fcClient.createService(tmpServiceName, {
320326
description: '用于 Alibaba Dragonwell Acceleration Cache 生成',
321327
serviceName: tmpServiceName,
@@ -324,7 +330,7 @@ export class JavaStartupAcceleration {
324330
nasConfig: this.nasConfig,
325331
vpcConfig: this.vpcConfig,
326332
});
327-
info("assistant service created")
333+
info("assistant service [" + tmpServiceName + "] created");
328334
}
329335

330336
private async getFCClient() {
@@ -350,11 +356,13 @@ export class JavaStartupAcceleration {
350356
}
351357

352358
private async genZip(dir: string, zipFilePath: string) {
359+
info("zip file creating");
353360
await this.makeZip(dir, zipFilePath);
354361
info("zip file created");
355362
}
356363

357364
private async createZipAndUploadToOSS() {
365+
info("start to upload");
358366
const tmpZipFilePath = join(tmpdir(), this.ossKey);
359367

360368
await this.genZip(this.artifactPath, tmpZipFilePath);

src/javaMain.ts

Lines changed: 71 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,24 @@ export default class JavaStartupAccelerationComponent {
1414
}
1515

1616
async index(params) {
17+
await this.checkSBuildArtifacts();
18+
1719
let args = await this.parseArgs(params.argsObj);
1820
let debug = args.debug;
1921
if (debug) {
2022
info("index args: " + JSON.stringify(params));
2123
}
2224
info("parsed args: " + JSON.stringify(args));
2325

26+
let fcEndpoint = await this.getFCEndpoint();
2427
let moduleName = args.moduleName;
2528
let serviceName = await this.getServiceConfig(moduleName, 'name');
2629
let functionName = await this.getFunctionConfig(moduleName, 'name');
27-
let fcEndpoint = await this.getFCEndpoint();
2830
let initializer = await this.getFunctionConfig(moduleName, 'initializer');
2931
let runtime = await this.getFunctionConfig(moduleName, 'runtime');
3032
let region = await this.getModulesProps(moduleName, 'region');
31-
let role = await this.getServiceConfig(moduleName, 'role');
32-
let logConfig = await this.getServiceConfig(moduleName, 'logConfig');
33+
let role = await this.getServiceConfig(moduleName, 'role', false);
34+
let logConfig = await this.getServiceConfig(moduleName, 'logConfig', false);
3335
const access = params?.project?.access || this.defaultAccess;
3436
const credential = await this.getCredential(access);
3537
let codeUri = await this.getFunctionConfig(moduleName, 'codeUri');
@@ -39,12 +41,11 @@ export default class JavaStartupAccelerationComponent {
3941
}
4042
srpath = this.removeStrSuffix(srpath, "/");
4143
let sharedDirName = path.basename(srpath);
42-
4344
let downloader = args.downloader;
4445
let uploader = args.uploader;
4546
let ossUtilUrl, ossEndpoint;
4647
if (downloader == OSS) {
47-
ossUtilUrl = await this.getGlobalConfig('ossUtilUrl');
48+
ossUtilUrl = await this.getGlobalConfig('ossUtilUrl', false);
4849
ossEndpoint = await this.getModulesProps(moduleName, 'ossEndpoint', false);
4950
}
5051

@@ -96,6 +97,7 @@ export default class JavaStartupAccelerationComponent {
9697
let instanceType = args.instanceType;
9798
let features = args.features;
9899
let funcEnvVars = await this.getFunctionEnvVars(moduleName);
100+
99101
const instance = new JavaStartupAcceleration(process.cwd(), {
100102
region,
101103
fcEndpoint,
@@ -149,64 +151,103 @@ export default class JavaStartupAccelerationComponent {
149151
}
150152
}
151153

152-
async getGlobalConfig(key: string) {
154+
async getGlobalConfig(key: string, errorIfNotExist: boolean = true) {
153155
const yaml = await this.getConfig();
154156
if (yaml) {
155157
try {
156-
return yaml[key];
158+
let value = await this.replaceReference(yaml[key]);
159+
if (value === undefined) {
160+
throw new Error("key " + key + " does not exist");
161+
}
162+
return value;
157163
} catch (e) {
158-
error('read global config [' + key + '] error');
159-
throw e;
164+
error('read global config [' + key + '] error: ' + e.message);
165+
if (errorIfNotExist) {
166+
throw e;
167+
}
160168
}
161169
}
170+
return null;
162171
}
163172

164173
async getModulesProps(moduleName: string, key: string, errorIfNotExist: boolean = true) {
165174
const yaml = await this.getConfig();
166175
if (yaml) {
167176
try {
168-
return yaml['services'][moduleName]['props'][key];
177+
let value = await this.replaceReference(yaml['services'][moduleName]['props'][key]);
178+
if (value === undefined) {
179+
throw new Error("key " + key + " does not exist");
180+
}
181+
return value;
169182
} catch (e) {
183+
info('read module prop [' + key + '] error: ' + e.message);
170184
if (errorIfNotExist) {
171185
throw e;
172-
} else {
173-
info('read module prop [' + key + '] error');
174186
}
175187
}
176188
}
189+
return null;
177190
}
178191

179-
async getServiceConfig(moduleName: string, key: string) {
192+
async getServiceConfig(moduleName: string, key: string, errorIfNotExist: boolean = true) {
180193
const yaml = await this.getConfig();
181194
if (yaml) {
182195
try {
183-
return yaml['services'][moduleName]['props']['service'][key];
196+
let value = await this.replaceReference(yaml['services'][moduleName]['props']['service'][key]);
197+
if (value === undefined) {
198+
throw new Error("key " + key + " does not exist");
199+
}
200+
return value;
184201
} catch (e) {
185-
error('read module config [' + key + '] error');
186-
throw e;
202+
error('read module config [' + key + '] error: ' + e.message);
203+
if (errorIfNotExist) {
204+
throw e;
205+
}
187206
}
188207
}
208+
return null;
189209
}
190210

191211
async getFunctionConfig(moduleName: string, key: string, errorIfNotExist: boolean = true) {
192212
const yaml = await this.getConfig();
193213

194-
let value = null;
195214
if (yaml) {
196215
try {
197-
value = yaml['services'][moduleName]['props']['function'][key];
216+
let value = await this.replaceReference(yaml['services'][moduleName]['props']['function'][key]);
217+
if (value === undefined) {
218+
throw new Error("key " + key + " does not exist");
219+
}
220+
return value;
198221
} catch (e) {
222+
error('read function config [' + key + '] error: ' + e.message);
199223
if (errorIfNotExist) {
200224
throw e;
201-
} else {
202-
info('function config [' + key + '] does not exist:' + e.message);
203225
}
204226
}
205227
}
206228

207-
return value;
229+
return null;
208230
}
209231

232+
async replaceReference(value) {
233+
if (typeof value !== 'string') {
234+
return value;
235+
}
236+
let myRegexp = new RegExp("\\${([a-zA-Z_.0-9]+)}", "g");
237+
let match = myRegexp.exec(value);
238+
if (match == null) {
239+
return value;
240+
}
241+
242+
let ref = match[1].split('.');
243+
let config = await this.getConfig();
244+
for (let i = 0; i < ref.length; i++) {
245+
config = config[ref[i]];
246+
}
247+
return config;
248+
}
249+
250+
210251
async getFunctionEnvVar(moduleName: string, name: string) {
211252
const environmentVariables = await this.getFunctionConfig(moduleName, "environmentVariables", false);
212253

@@ -227,6 +268,15 @@ export default class JavaStartupAccelerationComponent {
227268
return YAML.load(yamlContent);
228269
}
229270

271+
async checkSBuildArtifacts() {
272+
const path = join(process.cwd(), ".s", "build", "artifacts");
273+
if (existsSync(path)) {
274+
throw new Error("s build artifacts dir [" + path + "] should be deleted");
275+
} else {
276+
info("check s build artifacts: ok");
277+
}
278+
}
279+
230280
async readFileContent(fileName: string) {
231281
const path = join(process.cwd(), fileName);
232282
const isExists = existsSync(path);

0 commit comments

Comments
 (0)