From 533f1c03cbe5d87e4971b2e27b678eb2ba0188b5 Mon Sep 17 00:00:00 2001 From: seven Date: Sat, 21 Sep 2024 18:15:17 +0800 Subject: [PATCH] feat: deploy action create lambda OK Signed-off-by: seven --- .gitignore | 1 + .npmignore | 4 ++ package-lock.json | 4 +- package.json | 2 +- src/common/index.ts | 1 + src/iac/parse.ts | 2 +- src/iac/resource.ts | 28 ------------- src/stack/deploy.ts | 57 +++++++++++++++++---------- src/types.ts | 9 ++--- tests/fixtures/serverless-insignt.yml | 2 +- 10 files changed, 51 insertions(+), 59 deletions(-) delete mode 100644 src/iac/resource.ts diff --git a/.gitignore b/.gitignore index 96aa5b4..8fac46d 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ /coverage/ /.vscode/ .DS_Store +/artifacts/ diff --git a/.npmignore b/.npmignore index 258fed0..4d11c5a 100644 --- a/.npmignore +++ b/.npmignore @@ -2,13 +2,17 @@ .prettierrc jest.config.js tsconfig.json +Dockerfile .idea/ .github/ +.husky/ coverage/ src/ tests/ docs/ +artifacts/ +scripts/ # Ignore all .d.ts files except index.d.ts *.d.ts !index.d.ts diff --git a/package-lock.json b/package-lock.json index fab15d5..76f1fab 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@geek-fun/serverlessinsight", - "version": "0.0.1", + "version": "0.0.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@geek-fun/serverlessinsight", - "version": "0.0.1", + "version": "0.0.2", "license": "Apache-2.0", "dependencies": { "@alicloud/openapi-client": "^0.4.11", diff --git a/package.json b/package.json index 528bfcc..c009407 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@geek-fun/serverlessinsight", - "version": "0.0.1", + "version": "0.0.2", "description": "Full life cycle cross providers serverless application management for your fast-growing business.", "homepage": "https://serverlessinsight.geekfun.club", "main": "dist/src/index.js", diff --git a/src/common/index.ts b/src/common/index.ts index dd7f9ba..d127481 100644 --- a/src/common/index.ts +++ b/src/common/index.ts @@ -3,3 +3,4 @@ export * from './provider'; export * from './logger'; export * from './getVersion'; export * from './rosClient'; +export * from './actionContext'; diff --git a/src/iac/parse.ts b/src/iac/parse.ts index ea1d59e..06eb401 100644 --- a/src/iac/parse.ts +++ b/src/iac/parse.ts @@ -5,7 +5,7 @@ import { validateYaml } from './iacSchema'; const mapToArr = (obj: Record | string>) => { return Object.entries(obj).map(([key, value]) => - typeof value === 'string' ? { [key]: value } : { id: key, ...value }, + typeof value === 'string' ? { [key]: value } : { key, ...value }, ); }; diff --git a/src/iac/resource.ts b/src/iac/resource.ts deleted file mode 100644 index 03eed99..0000000 --- a/src/iac/resource.ts +++ /dev/null @@ -1,28 +0,0 @@ -// import * as ros from '@alicloud/ros-cdk-core'; -// import * as fc from '@alicloud/ros-cdk-fc'; -// -// export class IacStack extends ros.Stack { -// constructor(scope: ros.Construct, id: string, props?: ros.StackProps) { -// super(scope, id, props); -// new ros.RosInfo(this, ros.RosInfo.description, 'This is the simple ros cdk app example.'); -// -// const functionCompute = new fc.RosFunction( -// this, -// 'MyFunction', -// { -// functionName: 'my-function', -// serviceName: service.attrServiceName, -// handler: 'index.handler', -// runtime: 'nodejs14', -// code: { -// zipFile: -// 'exports.handler = function(event, context, callback) { callback(null, "Hello World"); }', -// }, -// }, -// false, -// ); -// console.log(functionCompute); -// } -// } -// -// export const defineResource = (name: string, type: string, properties: Record) => {}; diff --git a/src/stack/deploy.ts b/src/stack/deploy.ts index 5008814..a615fe7 100644 --- a/src/stack/deploy.ts +++ b/src/stack/deploy.ts @@ -2,32 +2,49 @@ import * as ros from '@alicloud/ros-cdk-core'; import * as fc from '@alicloud/ros-cdk-fc'; import { ActionContext, ServerlessIac } from '../types'; import { printer, rosStackDeploy } from '../common'; +import path from 'node:path'; +import * as fs from 'node:fs'; + +const resolveCode = (location: string): string => { + const filePath = path.resolve(process.cwd(), location); + const fileContent = fs.readFileSync(filePath); + + return fileContent.toString('base64'); +}; export class IacStack extends ros.Stack { constructor(scope: ros.Construct, iac: ServerlessIac, props?: ros.StackProps) { super(scope, iac.service, props); new ros.RosInfo(this, ros.RosInfo.description, 'This is the simple ros cdk app example.'); - iac.functions.map( - (fnc) => - new fc.RosFunction( - this, - fnc.name, - { - functionName: fnc.name, - serviceName: `${fnc.name}-service`, - handler: fnc.handler, - runtime: fnc.runtime, - memorySize: fnc.memory, - timeout: fnc.timeout, - environmentVariables: fnc.environment, - code: { - zipFile: - 'exports.handler = function(event, context, callback) { callback(null, "Hello World"); }', - }, - }, - false, - ), + const service = new fc.RosService( + this, + `${iac.service}-service`, + { + serviceName: `${iac.service}-service`, + }, + true, ); + + iac.functions.forEach((fnc) => { + const func = new fc.RosFunction( + this, + fnc.key, + { + functionName: fnc.name, + serviceName: service.serviceName, + handler: fnc.handler, + runtime: fnc.runtime, + memorySize: fnc.memory, + timeout: fnc.timeout, + environmentVariables: fnc.environment, + code: { + zipFile: resolveCode(fnc.code), + }, + }, + true, + ); + func.addDependsOn(service); + }); } } diff --git a/src/types.ts b/src/types.ts index 345c35c..05e4eb3 100644 --- a/src/types.ts +++ b/src/types.ts @@ -12,9 +12,10 @@ type Stages = { export type IacFunction = { name: string; + key: string; runtime: string; handler: string; - zip: string; + code: string; memory: number; timeout: number; environment: { @@ -22,10 +23,6 @@ export type IacFunction = { }; }; -type Functions = { - [key: string]: IacFunction; -}; - export type Event = { type: string; source: string; @@ -50,7 +47,7 @@ export type RawServerlessIac = { stages: Stages; service: string; tags: Tags; - functions: Functions; + functions: { [key: string]: IacFunction }; events: Events; }; diff --git a/tests/fixtures/serverless-insignt.yml b/tests/fixtures/serverless-insignt.yml index 0080029..7f80074 100644 --- a/tests/fixtures/serverless-insignt.yml +++ b/tests/fixtures/serverless-insignt.yml @@ -20,7 +20,7 @@ functions: name: insight-poc-fn runtime: nodejs14 handler: index.handler - code: artifact.zip + code: artifacts/artifact.zip memory: 512 timeout: 10 environment: