Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions samples/aliyun-poc-domain.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
version: 0.0.1

provider:
name: aliyun
region: cn-hongkong


service: insight-poc-domain

tags:
owner: geek-fun

buckets:
insight_poc_bucket:
name: insight-poc-domain
website:
code: dist
domain: meke-ui.serverlessinsight.com
index: index.html
error_page: 404.html
error_code: 404
3 changes: 2 additions & 1 deletion samples/aliyun-poc-fc-gpu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ stages:
dev:
node_env: development
prod:
region: cn-shanghai
region: cn-hangzhou

service: insight-poc-gpu

Expand All @@ -28,6 +28,7 @@ functions:
cmd: "npm start"
port: 9000
memory: 512
gpu: TESLA_8
timeout: 10
network:
vpc_id: vpc-2vc8v9btc8470laqui9bk
Expand Down
18 changes: 14 additions & 4 deletions src/commands/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import { deployStack } from '../stack';
import { constructActionContext, logger } from '../common';
import { constructActionContext, getIacLocation, logger } from '../common';
import { parseYaml } from '../parser';

export const deploy = async (
stackName: string,
options: { location: string; parameters: { [key: string]: string }; stage: string | undefined },
options: {
location: string;
parameters?: { [key: string]: string };
stage?: string;
region?: string;
provider?: string;
accessKeyId?: string;
accessKeySecret?: string;
securityToken?: string;
},
) => {
const context = constructActionContext({ ...options, stackName });
logger.info('Validating yaml...');
const iac = parseYaml(context.iacLocation);
const iac = parseYaml(getIacLocation(options.location));
logger.info('Yaml is valid! 🎉');

const context = constructActionContext({ ...options, stackName, iacProvider: iac.provider });

logger.info('Deploying stack...');
await deployStack(stackName, iac, context);

Expand Down
27 changes: 23 additions & 4 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,19 @@ program
.option('-s, --stage <stage>', 'specify the stage')
.action((stackName, { file, stage }) => {
logger.debug('log command info');
validate(file, stage);
validate(stackName, { stage, location: file });
});

program
.command('deploy <stackName>')
.description('deploy serverless Iac yaml')
.option('-f, --file <path>', 'specify the yaml file')
.option('-s, --stage <stage>', 'specify the stage')
.option('-r, --region <region>', 'specify the region')
.option('-pr, --provider <provider>', 'specify the provider')
.option('-ak, --accessKeyId <accessKeyId>', 'specify the AccessKeyId')
.option('-as, --accessKeySecret <accessKeySecret>', 'specify the AccessKeySecret')
.option('-at, --securityToken <securityToken>', 'specify the SecurityToken')
.option(
'-p, --parameter <key=value>',
'override parameters',
Expand All @@ -46,9 +51,23 @@ program
},
{},
)
.action(async (stackName, { file, parameter, stage }) => {
await deploy(stackName, { location: file, parameters: parameter, stage });
});
.action(
async (
stackName,
{ stage, parameter, file, region, provider, accessKeyId, accessKeySecret, securityToken },
) => {
await deploy(stackName, {
stage,
parameters: parameter,
location: file,
region,
provider,
accessKeyId,
accessKeySecret,
securityToken,
});
},
);

program
.command('template <stackName>')
Expand Down
7 changes: 5 additions & 2 deletions src/commands/validate.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { constructActionContext, logger } from '../common';
import { parseYaml } from '../parser';

export const validate = (location: string | undefined, stage: string | undefined) => {
const context = constructActionContext({ location, stage });
export const validate = (
stackName: string,
options: { location: string | undefined; stage: string | undefined },
) => {
const context = constructActionContext({ stackName, ...options });
parseYaml(context.iacLocation);
logger.info('Yaml is valid! 🎉');
};
32 changes: 18 additions & 14 deletions src/common/actionContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ActionContext } from '../types';
import { ActionContext, ServerlessIac } from '../types';
import path from 'node:path';
import { ProviderEnum } from './providerEnum';

Expand All @@ -12,28 +12,32 @@ export const getIacLocation = (location?: string): string => {
path.resolve(projectRoot, 'serverless-insight.yml');
};

export const constructActionContext = (config?: {
export const constructActionContext = (config: {
stage?: string;
stackName?: string;
region?: string;
provider?: string;
account?: string;
accessKeyId?: string;
accessKeySecret?: string;
securityToken?: string;
location?: string;
parameters?: { [key: string]: string };
stage?: string;
stackName?: string;
iacProvider?: ServerlessIac['provider'];
}): ActionContext => {
return {
stage: config?.stage ?? 'default',
stackName: config?.stackName ?? '',
stage: config.stage ?? 'default',
stackName: config.stackName ?? '',
provider: (config.provider ?? config.iacProvider?.name ?? ProviderEnum.ALIYUN) as ProviderEnum,
region:
config?.region ?? process.env.ROS_REGION_ID ?? process.env.ALIYUN_REGION ?? 'cn-hangzhou',
accessKeyId: config?.accessKeyId ?? (process.env.ALIYUN_ACCESS_KEY_ID as string),
accessKeySecret: config?.accessKeySecret ?? (process.env.ALIYUN_ACCESS_KEY_SECRET as string),
securityToken: config?.securityToken ?? process.env.ALIYUN_SECURITY_TOKEN,
iacLocation: getIacLocation(config?.location),
parameters: Object.entries(config?.parameters ?? {}).map(([key, value]) => ({ key, value })),
provider: config?.provider as ProviderEnum,
config.region ??
config.iacProvider?.region ??
process.env.ROS_REGION_ID ??
process.env.ALIYUN_REGION ??
'cn-hangzhou',
accessKeyId: config.accessKeyId ?? (process.env.ALIYUN_ACCESS_KEY_ID as string),
accessKeySecret: config.accessKeySecret ?? (process.env.ALIYUN_ACCESS_KEY_SECRET as string),
securityToken: config.securityToken ?? process.env.ALIYUN_SECURITY_TOKEN,
iacLocation: getIacLocation(config.location),
parameters: Object.entries(config.parameters ?? {}).map(([key, value]) => ({ key, value })),
};
};
3 changes: 2 additions & 1 deletion src/parser/functionParser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FunctionDomain, FunctionRaw, NasStorageClassEnum } from '../types';
import { FunctionDomain, FunctionGpuEnum, FunctionRaw, NasStorageClassEnum } from '../types';
import { isEmpty } from 'lodash';

export const parseFunction = (functions?: {
Expand All @@ -13,6 +13,7 @@ export const parseFunction = (functions?: {
code: func.code,
container: func.container,
memory: func.memory,
gpu: func.gpu as FunctionGpuEnum,
timeout: func.timeout,
environment: func.environment,
log: func.log,
Expand Down
30 changes: 28 additions & 2 deletions src/stack/rosStack/function.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { ActionContext, FunctionDomain, NasStorageClassEnum, ServerlessIac } from '../../types';
import {
ActionContext,
FunctionDomain,
FunctionGpuEnum,
NasStorageClassEnum,
ServerlessIac,
} from '../../types';
import {
CODE_ZIP_SIZE_LIMIT,
encodeBase64ForRosId,
Expand All @@ -25,13 +31,25 @@ const storageClassMap = {
[NasStorageClassEnum.EXTREME_STANDARD]: { fileSystemType: 'extreme', storageType: 'standard' },
[NasStorageClassEnum.EXTREME_ADVANCE]: { fileSystemType: 'extreme', storageType: 'advance' },
};

const securityGroupRangeMap: { [key: string]: string } = {
TCP: '1/65535',
UDP: '1/65535',
ICMP: '-1/-1',
GRE: '-1/-1',
ALL: '-1/-1',
};
const gpuConfigMap = {
[FunctionGpuEnum.TESLA_8]: { gpuMemorySize: 8192, gpuType: 'fc.gpu.tesla.1' },
[FunctionGpuEnum.TESLA_12]: { gpuMemorySize: 12288, gpuType: 'fc.gpu.tesla.1' },
[FunctionGpuEnum.TESLA_16]: { gpuMemorySize: 16384, gpuType: 'fc.gpu.tesla.1' },
[FunctionGpuEnum.AMPERE_8]: { gpuMemorySize: 8192, gpuType: 'fc.gpu.ampere.1' },
[FunctionGpuEnum.AMPERE_12]: { gpuMemorySize: 12288, gpuType: 'fc.gpu.ampere.1' },
[FunctionGpuEnum.AMPERE_16]: { gpuMemorySize: 16384, gpuType: 'fc.gpu.ampere.1' },
[FunctionGpuEnum.AMPERE_24]: { gpuMemorySize: 24576, gpuType: 'fc.gpu.ampere.1' },
[FunctionGpuEnum.ADA_48]: { gpuMemorySize: 49152, gpuType: 'fc.gpu.ada.1' },
};

const transformSecurityRules = (rules: Array<string>, ruleType: 'INGRESS' | 'EGRESS') => {
return rules.map((rule) => {
const [protocol, cidrIp, portRange] = rule.split(':');
Expand All @@ -49,6 +67,13 @@ const transformSecurityRules = (rules: Array<string>, ruleType: 'INGRESS' | 'EGR
});
};

const transformGpuConfig = (gpu: FunctionDomain['gpu']) => {
if (!gpu) {
return undefined;
}

return gpuConfigMap[gpu];
};
export const resolveFunctions = (
scope: ros.Construct,
functions: Array<FunctionDomain> | undefined,
Expand Down Expand Up @@ -248,8 +273,9 @@ export const resolveFunctions = (
{
functionName: replaceReference(fnc.name, context),
memorySize: replaceReference(fnc.memory, context),
timeout: replaceReference(fnc.timeout, context),
diskSize: fnc.storage?.disk,
gpuConfig: transformGpuConfig(fnc.gpu),
timeout: replaceReference(fnc.timeout, context),
environmentVariables: replaceReference(fnc.environment, context),
logConfig,
vpcConfig,
Expand Down
13 changes: 13 additions & 0 deletions src/types/domains/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type FunctionRaw = {
port: number;
};
memory: number;
gpu: string;
timeout: number;
log?: boolean;
environment?: {
Expand Down Expand Up @@ -48,6 +49,7 @@ export type FunctionDomain = {
port: number;
};
memory: number;
gpu?: FunctionGpuEnum;
timeout: number;
log?: boolean;
environment?: {
Expand Down Expand Up @@ -77,3 +79,14 @@ export enum NasStorageClassEnum {
EXTREME_STANDARD = 'EXTREME_STANDARD',
EXTREME_ADVANCE = 'EXTREME_ADVANCE',
}

export enum FunctionGpuEnum {
TESLA_8 = 'TESLA_8',
TESLA_12 = 'TESLA_12',
TESLA_16 = 'TESLA_16',
AMPERE_8 = 'AMPERE_8',
AMPERE_12 = 'AMPERE_12',
AMPERE_16 = 'AMPERE_16',
AMPERE_24 = 'AMPERE_24',
ADA_48 = 'ADA_48',
}
13 changes: 13 additions & 0 deletions src/validator/functionSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ export const functionSchema = {
},
},
memory: { type: 'number' },
gpu: {
type: 'string',
enum: [
'TESLA_8',
'TESLA_12',
'TESLA_16',
'AMPERE_8',
'AMPERE_12',
'AMPERE_16',
'AMPERE_24',
'ADA_48',
],
},
timeout: { type: 'number' },
log: { type: 'boolean' },
environment: {
Expand Down
5 changes: 4 additions & 1 deletion tests/commands/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ describe('unit test for deploy command', () => {
});

expect(mockedDeployStack).toHaveBeenCalledTimes(1);
expect(mockedDeployStack).toHaveBeenCalledWith(stackName, expect.any(Object), defaultContext);
expect(mockedDeployStack).toHaveBeenCalledWith(stackName, expect.any(Object), {
...defaultContext,
region: 'cn-chengdu',
});
});
});
27 changes: 27 additions & 0 deletions tests/fixtures/deployFixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,32 @@ export const oneFcWithContainerRos = {
},
};

export const oneFcWithGpuIac = {
...oneFcIac,
functions: [
{
...((oneFcIac.functions && oneFcIac.functions[0]) ?? {}),
gpu: 'TESLA_8',
},
],
} as ServerlessIac;
export const oneFcWithGpuRos = {
...oneFcRos,
Resources: {
...oneFcRos.Resources,
hello_fn: {
...oneFcRos.Resources.hello_fn,
Properties: {
...oneFcRos.Resources.hello_fn.Properties,
GpuConfig: {
GpuMemorySize: 8192,
GpuType: 'fc.gpu.tesla.1',
},
},
},
},
};

export const largeCodeRos = {
Description: 'my-demo-service stack',
Mappings: {
Expand Down Expand Up @@ -1381,6 +1407,7 @@ export const defaultContext = {
iacLocation: expect.stringContaining('tests/fixtures/serverless-insight.yml'),
parameters: [],
region: 'cn-hangzhou',
provider: 'aliyun',
securityToken: 'account id',
stackName: 'my-demo-stack',
stage: 'default',
Expand Down
Loading