Skip to content

Commit

Permalink
feat: add deploy command
Browse files Browse the repository at this point in the history
Signed-off-by: seven <[email protected]>
  • Loading branch information
Blankll committed Sep 19, 2024
1 parent 1a46da1 commit e33b209
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 18 deletions.
13 changes: 13 additions & 0 deletions src/commands/deploy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { deployStack } from '../stack';
import { printer } from '../common';
import { parseYaml } from '../iac';

export const deploy = (location?: string) => {
printer.info('Validating yaml...');
const iac = parseYaml(location);
printer.success('Yaml is valid! 🎉');

printer.info('Deploying stack...');
deployStack(iac);
printer.success('Stack deployed! 🎉');
};
9 changes: 9 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Command } from 'commander';
import { lang } from '../lang';
import { logger, getVersion } from '../common';
import { validate } from './validate';
import { deploy } from './deploy';

const program = new Command();

Expand All @@ -29,4 +30,12 @@ program
validate(location);
});

program
.command('deploy [location]')
.description('deploy serverless Iac yaml')
.action((location) => {
logger.debug('log command info');
deploy(location);
});

program.parse();
8 changes: 1 addition & 7 deletions src/commands/validate.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { parseYaml } from '../iac';
import path from 'node:path';
import { printer } from '../common';

export const validate = (location?: string) => {
const projectRoot = path.resolve(process.cwd());
const yamlPath = location
? path.resolve(projectRoot, location)
: path.resolve(projectRoot, 'serverless-insight.yml');

parseYaml(yamlPath);
parseYaml(location);
printer.success('Yaml is valid! 🎉');
};
3 changes: 2 additions & 1 deletion src/common/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import chalk from 'chalk';

export const printer = {
success: (message: string) =>
console.log(`${chalk.bgYellow(chalk.black('ServerlessInsight'))}: ${chalk.green(message)}`),
console.log(`${chalk.blue('ServerlessInsight')}: ${chalk.green(message)}`),
info: (message: string) => console.log(`${chalk.blue('ServerlessInsight')}: ${message}`),
error: (message: string) =>
console.log(`${chalk.bgRed(chalk.black('ServerlessInsight'))}: ${chalk.red(message)}`),
};
4 changes: 4 additions & 0 deletions src/iac/iacSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ class IacSchemaErrors extends Error {
type: error.keyword,
}));
}

public get errors() {
return this.schemaErrors;
}
}

export const validateYaml = (iacJson: RawServerlessIac) => {
Expand Down
12 changes: 9 additions & 3 deletions src/iac/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { parse } from 'yaml';
import { existsSync, readFileSync } from 'node:fs';
import { IacFunction, RawServerlessIac, ServerlessIac, Event } from '../types';
import { validateYaml } from './iacSchema';
import path from 'node:path';

const mapToArr = (obj: Record<string, Record<string, unknown> | string>) => {
return Object.entries(obj).map(([key, value]) =>
Expand All @@ -28,10 +29,15 @@ const transformYaml = (iacJson: RawServerlessIac): ServerlessIac => {
};
};

export const parseYaml = (path: string): ServerlessIac => {
validateExistence(path);
export const parseYaml = (location?: string): ServerlessIac => {
const projectRoot = path.resolve(process.cwd());
const yamlPath = location
? path.resolve(projectRoot, location)
: path.resolve(projectRoot, 'serverless-insight.yml');

const yamlContent = readFileSync(path, 'utf8');
validateExistence(yamlPath);

const yamlContent = readFileSync(yamlPath, 'utf8');
const iacJson = parse(yamlContent) as RawServerlessIac;

validateYaml(iacJson);
Expand Down
28 changes: 28 additions & 0 deletions src/iac/resource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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<string, any>) => {};
38 changes: 38 additions & 0 deletions src/stack/deploy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as ros from '@alicloud/ros-cdk-core';
import * as fc from '@alicloud/ros-cdk-fc';
import { ServerlessIac } from '../types';

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,
),
);
}
}

export const deployStack = (iac: ServerlessIac) => {
console.log(`Deploying stack... ${JSON.stringify(iac)}`);
const app = new ros.App();
new IacStack(app, iac);
app.synth();
};
1 change: 1 addition & 0 deletions src/stack/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { deployStack } from './deploy';
10 changes: 4 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,16 @@ type Stages = {
[key: string]: Stage;
};

type FunctionEnvironment = {
NODE_ENV: string;
};

export type IacFunction = {
name: string;
runtime: string;
handler: string;
code: string;
zip: string;
memory: number;
timeout: number;
environment: FunctionEnvironment;
environment: {
[key: string]: string;
};
};

type Functions = {
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/serverless-insignt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ functions:
runtime: nodejs14
handler: index.handler
code: artifact.zip
memory: 128
memory: 512
timeout: 10
environment:
NODE_ENV: production
Expand Down

0 comments on commit e33b209

Please sign in to comment.