Skip to content

Commit d5cf679

Browse files
committed
fix: support ossAuto
1 parent 00a1f17 commit d5cf679

6 files changed

Lines changed: 115 additions & 4 deletions

File tree

__tests__/e2e/nodejs/s_auto.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ resources:
2020
initializer:
2121
handler: index.initializer
2222
timeout: 10
23+
role: acs:ram::${config('AccountID')}:role/aliyunaliyunfcdefaultrole
2324

2425
vpcConfig: auto
2526
nasConfig: auto
2627
logConfig: auto
28+
ossMountConfig: auto
2729

2830
asyncInvokeConfig:
2931
destinationConfig:

package-lock.json

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"@serverless-cd/srm-aliyun-pop-core": "^0.0.8-beta.1",
3030
"@serverless-cd/srm-aliyun-ram20150501": "^0.0.2-beta.9",
3131
"@serverless-cd/srm-aliyun-sls20201230": "0.0.5-beta.3",
32+
"@serverless-cd/srm-aliyun-oss": "^0.0.1-beta.2",
3233
"@serverless-devs/diff": "^0.0.3-beta.6",
3334
"@serverless-devs/downloads": "^0.0.7",
3435
"@serverless-devs/load-component": "^0.0.9",

src/resources/fc/impl/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ export function computeLocalAuto(local) {
3636
const nasAuto = isAuto(local.nasConfig);
3737
const vpcAuto = isAutoVpcConfig(local.vpcConfig) || (!local.vpcConfig && nasAuto);
3838
const slsAuto = isAuto(local.logConfig);
39+
const ossAuto = isAuto(local.ossMountConfig);
3940
// auto 是在 preDeploy 和 plan 之间的阶段设置的,用于提示
4041
// 如果用户设置了 auto 会在 handlePreRun 方法变成 arn
4142
const roleAuto =
4243
isAuto(local.role) || (_.isNil(local.role) && !_.isEmpty(local?.ossMountConfig?.mountPoints));
43-
return { nasAuto, vpcAuto, slsAuto, roleAuto };
44+
return { nasAuto, vpcAuto, slsAuto, roleAuto, ossAuto };
4445
}
4546

4647
/**

src/resources/oss/index.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { ICredentials } from '@serverless-devs/component-interface';
2+
import Oss from '@serverless-cd/srm-aliyun-oss';
3+
import { Config } from '@alicloud/openapi-client';
4+
import { IRegion } from '../../interface';
5+
import logger from '../../logger';
6+
import { isAppCenter } from '../../utils/index';
7+
8+
export default class OSS {
9+
readonly client: Oss;
10+
private config;
11+
12+
constructor(private region: IRegion, credentials: ICredentials, ossEndpoint: string) {
13+
this.config = new Config({
14+
accountID: credentials.AccountID,
15+
accessKeyId: credentials.AccessKeyID,
16+
accessKeySecret: credentials.AccessKeySecret,
17+
securityToken: credentials.SecurityToken,
18+
endpoint: ossEndpoint,
19+
regionId: region,
20+
readTimeout: 60000,
21+
});
22+
this.client = new Oss();
23+
}
24+
25+
async deploy(): Promise<{ ossBucket: string }> {
26+
logger.debug(`init oss: ${JSON.stringify(this.config)}`);
27+
const { ossBucket } = await this.client.initOss(this.config);
28+
if (isAppCenter()) {
29+
logger.info(`created oss region: ${this.region};`);
30+
} else {
31+
logger.spin('creating', 'oss', `region: ${this.region}; ossBucket: ${ossBucket}`);
32+
}
33+
return { ossBucket };
34+
}
35+
}

src/subCommands/deploy/impl/function.ts

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import VPC_NAS from '../../../resources/vpc-nas';
1919
import Base from './base';
2020
import { ICredentials } from '@serverless-devs/component-interface';
2121
import { calculateCRC64, getFileSize } from '../../../utils';
22+
import OSS from '../../../resources/oss';
2223

2324
type IType = 'code' | 'config' | boolean;
2425
interface IOpts {
@@ -337,9 +338,9 @@ export default class Service extends Base {
337338
const { credential } = this.inputs;
338339
const { functionName } = this.local;
339340

340-
const { nasAuto, vpcAuto, slsAuto, roleAuto } = FC.computeLocalAuto(this.local);
341+
const { nasAuto, vpcAuto, slsAuto, roleAuto, ossAuto } = FC.computeLocalAuto(this.local);
341342
logger.debug(
342-
`Deploy auto compute local auto, nasAuto: ${nasAuto}; vpcAuto: ${vpcAuto}; slsAuto: ${slsAuto}; roleAuto: ${roleAuto}`,
343+
`Deploy auto compute local auto, nasAuto: ${nasAuto}; vpcAuto: ${vpcAuto}; slsAuto: ${slsAuto}; roleAuto: ${roleAuto}; ossAuto: ${ossAuto}`,
343344
);
344345

345346
if (slsAuto) {
@@ -364,6 +365,48 @@ logConfig:
364365
});
365366
}
366367

368+
if (ossAuto) {
369+
let ossEndpoint = `https://oss-${region}.aliyuncs.com`;
370+
if (process.env.FC_REGION === region) {
371+
ossEndpoint = `oss-${region}-internal.aliyuncs.com`;
372+
}
373+
if (region === 'cn-shanghai-finance-1') {
374+
if (process.env.FC_REGION === region) {
375+
ossEndpoint = `oss-${region}-pub-internal.aliyuncs.com`;
376+
} else {
377+
ossEndpoint = `oss-${region}-pub.aliyuncs.com`;
378+
}
379+
}
380+
if (region === 'cn-heyuan-acdr-1') {
381+
ossEndpoint = `oss-${region}-internal.aliyuncs.com`;
382+
}
383+
if (process.env.FC_CODE_TEMP_OSS_ENDPOINT) {
384+
ossEndpoint = process.env.FC_CODE_TEMP_OSS_ENDPOINT;
385+
}
386+
const oss = new OSS(region, credential as ICredentials, ossEndpoint);
387+
const { ossBucket } = await oss.deploy();
388+
logger.write(
389+
yellow(`Created oss resource succeeded, please replace ossMountConfig: auto in yaml with:
390+
ossMountConfig:
391+
mountPoints:
392+
- mountDir: /mnt/oss/${functionName}
393+
bucketName: ${ossBucket},
394+
endpoint: ${ossEndpoint},
395+
readOnly: false\n`),
396+
);
397+
this.createResource.oss = { ossBucket };
398+
_.set(this.local, 'ossMountConfig', {
399+
mountPoints: [
400+
{
401+
mountDir: `/mnt/oss/${functionName}`,
402+
bucketName: ossBucket,
403+
endpoint: ossEndpoint,
404+
readOnly: false,
405+
},
406+
],
407+
});
408+
}
409+
367410
if (roleAuto) {
368411
const client = new RamClient(credential as ICredentials);
369412
const arn = await client.initFcDefaultServiceRole();
@@ -406,7 +449,7 @@ vpcConfig:
406449
if (nasAuto) {
407450
const modelConfig = supplement?.modelConfig || annotations?.modelConfig;
408451
let serverAddr = `${mountTargetDomain}:/${functionName}${
409-
isEmpty(modelConfig) ? '' : `/${ modelConfig.id}`
452+
isEmpty(modelConfig) ? '' : `/${modelConfig.id}`
410453
}`;
411454
if (serverAddr.length > 128) {
412455
serverAddr = serverAddr.substring(0, 128);

0 commit comments

Comments
 (0)