Compare commits

..

No commits in common. "master" and "feat_the_progress_of_tasks" have entirely different histories.

43 changed files with 8176 additions and 11749 deletions

1
.gitignore vendored
View File

@ -34,4 +34,3 @@ lerna-debug.log*
!.vscode/extensions.json !.vscode/extensions.json
/config.yml /config.yml
tsconfig.build.tsbuildinfo

View File

@ -1,3 +0,0 @@
{
"recommendations": []
}

View File

@ -16,9 +16,5 @@ db:
prefix: fennec prefix: fennec
rabbitmq: rabbitmq:
uri: 'amqp://fennec:fennec@192.168.31.194:5672' uri: 'amqp://fennec:fennec@192.168.31.194:5672'
etcd:
hosts:
- 'http://192.168.31.194:2379'
workspaces: workspaces:
root: '/Users/ivanli/Projects/fennec/workspaces' root: '/Users/ivanli/Projects/fennec/workspaces'

View File

@ -1,14 +0,0 @@
module.exports = {
apps: [
{
name: 'fennec-be',
script: 'npm',
args: 'run start:prod',
watch: false,
ignore_watch: ['node_modules'],
log_date_format: 'MM-DD HH:mm:ss.SSS Z',
env: {},
max_restarts: 5,
},
],
};

19317
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,8 +1,8 @@
{ {
"name": "fennec-be", "name": "fennec-be",
"version": "0.1.1", "version": "0.1.0",
"description": "a ci/cd project.", "description": "",
"author": "Ivan Li\b<ivanli2048@gmail.com>", "author": "",
"private": true, "private": true,
"license": "UNLICENSED", "license": "UNLICENSED",
"scripts": { "scripts": {
@ -22,7 +22,7 @@
}, },
"dependencies": { "dependencies": {
"@golevelup/nestjs-rabbitmq": "^1.16.1", "@golevelup/nestjs-rabbitmq": "^1.16.1",
"@nestjs-lib/auth": "^0.2.1", "@nestjs/bull": "^0.3.1",
"@nestjs/common": "^7.5.1", "@nestjs/common": "^7.5.1",
"@nestjs/config": "^0.6.2", "@nestjs/config": "^0.6.2",
"@nestjs/core": "^7.5.1", "@nestjs/core": "^7.5.1",
@ -30,25 +30,24 @@
"@nestjs/platform-express": "^7.5.1", "@nestjs/platform-express": "^7.5.1",
"@nestjs/typeorm": "^7.1.5", "@nestjs/typeorm": "^7.1.5",
"@types/amqplib": "^0.8.0", "@types/amqplib": "^0.8.0",
"@types/bull": "^3.15.0",
"@types/ramda": "^0.27.38", "@types/ramda": "^0.27.38",
"apollo-server-express": "^2.19.2", "apollo-server-express": "^2.19.2",
"bcrypt": "^5.0.0", "bcrypt": "^5.0.0",
"body-parser": "^1.19.0", "body-parser": "^1.19.0",
"bull": "^3.20.1",
"class-transformer": "^0.3.2", "class-transformer": "^0.3.2",
"class-validator": "^0.13.1", "class-validator": "^0.13.1",
"debug": "^4.3.1", "debug": "^4.3.1",
"graphql": "^15.5.0", "graphql": "^15.5.0",
"graphql-tools": "^8.1.0", "graphql-tools": "^7.0.2",
"ioredis": "^4.25.0", "ioredis": "^4.25.0",
"jose": "^3.14.0",
"js-yaml": "^4.0.0", "js-yaml": "^4.0.0",
"nestjs-etcd": "^0.2.0",
"nestjs-pino": "^1.4.0", "nestjs-pino": "^1.4.0",
"nestjs-redis": "^1.2.8", "nestjs-redis": "^1.2.8",
"observable-to-async-generator": "^1.0.1-rc", "observable-to-async-generator": "^1.0.1-rc",
"pg": "^8.5.1", "pg": "^8.5.1",
"pino-pretty": "^4.7.1", "pino-pretty": "^4.7.1",
"pm2": "^5.1.0",
"ramda": "^0.27.1", "ramda": "^0.27.1",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2", "rimraf": "^3.0.2",
@ -57,7 +56,7 @@
"typeorm": "^0.2.30" "typeorm": "^0.2.30"
}, },
"devDependencies": { "devDependencies": {
"@nestjs/cli": "^7.6.0", "@nestjs/cli": "^7.5.7",
"@nestjs/schematics": "^7.1.3", "@nestjs/schematics": "^7.1.3",
"@nestjs/testing": "^7.5.1", "@nestjs/testing": "^7.5.1",
"@types/body-parser": "^1.19.0", "@types/body-parser": "^1.19.0",
@ -97,9 +96,6 @@
"collectCoverageFrom": [ "collectCoverageFrom": [
"**/*.(t|j)s" "**/*.(t|j)s"
], ],
"moduleNameMapper": {
"^jose/(.*)$": "<rootDir>/../node_modules/jose/dist/node/cjs/$1"
},
"coverageDirectory": "../coverage", "coverageDirectory": "../coverage",
"testEnvironment": "node" "testEnvironment": "node"
} }

View File

@ -1,4 +1,3 @@
import { CommonsModule } from './commons/commons.module';
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common'; import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config'; import { ConfigModule, ConfigService } from '@nestjs/config';
import { GraphQLModule } from '@nestjs/graphql'; import { GraphQLModule } from '@nestjs/graphql';
@ -13,13 +12,13 @@ import { PipelineTasksModule } from './pipeline-tasks/pipeline-tasks.module';
import configuration from './commons/config/configuration'; import configuration from './commons/config/configuration';
import { RedisModule } from 'nestjs-redis'; import { RedisModule } from 'nestjs-redis';
import { WebhooksModule } from './webhooks/webhooks.module'; import { WebhooksModule } from './webhooks/webhooks.module';
import { RawBodyMiddleware } from './commons/middleware/raw-body.middleware'; import { RawBodyMiddleware } from './commons/middlewares/raw-body.middleware';
import { GiteaWebhooksController } from './webhooks/gitea-webhooks.controller'; import { GiteaWebhooksController } from './webhooks/gitea-webhooks.controller';
import { ParseBodyMiddleware } from './commons/middleware/parse-body.middleware'; import { ParseBodyMiddleware } from './commons/middlewares/parse-body.middleware';
import { BullModule } from '@nestjs/bull';
import { LoggerModule } from 'nestjs-pino'; import { LoggerModule } from 'nestjs-pino';
import { EtcdModule } from 'nestjs-etcd';
import pinoPretty from 'pino-pretty'; import pinoPretty from 'pino-pretty';
import { fromPairs, map, pipe, toPairs } from 'ramda';
@Module({ @Module({
imports: [ imports: [
@ -64,21 +63,16 @@ import { fromPairs, map, pipe, toPairs } from 'ramda';
playground: true, playground: true,
autoSchemaFile: true, autoSchemaFile: true,
installSubscriptionHandlers: true, installSubscriptionHandlers: true,
context: ({ req, connection, ...args }) => { }),
return connection ? { req: connection.context } : { req }; inject: [ConfigService],
}, }),
subscriptions: { BullModule.forRootAsync({
onConnect: (connectionParams: Record<string, string>) => { imports: [ConfigModule],
const connectionParamsWithLowerKeys = pipe( useFactory: (configService: ConfigService) => ({
toPairs, redis: {
map(([key, value]) => [key.toLowerCase(), value]), host: configService.get<string>('db.redis.host', 'localhost'),
fromPairs, port: configService.get<number>('db.redis.port', undefined),
)(connectionParams); password: configService.get<string>('db.redis.password', undefined),
return {
headers: connectionParamsWithLowerKeys,
};
},
}, },
}), }),
inject: [ConfigService], inject: [ConfigService],
@ -97,15 +91,7 @@ import { fromPairs, map, pipe, toPairs } from 'ramda';
}), }),
inject: [ConfigService], inject: [ConfigService],
}), }),
EtcdModule.forRootAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
hosts: configService.get<string>('db.etcd.hosts', 'localhost:2379'),
}),
inject: [ConfigService],
}),
WebhooksModule, WebhooksModule,
CommonsModule,
], ],
controllers: [AppController], controllers: [AppController],
providers: [AppService, AppResolver], providers: [AppService, AppResolver],

View File

@ -1,11 +1,10 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { PasswordConverter } from './services/password-converter'; import { PasswordConverter } from './services/password-converter';
import { RedisMutexModule } from './redis-mutex/redis-mutex.module'; import { RedisMutexModule } from './redis-mutex/redis-mutex.module';
import { AuthModule } from '@nestjs-lib/auth';
@Module({ @Module({
imports: [RedisMutexModule, AuthModule],
providers: [PasswordConverter], providers: [PasswordConverter],
exports: [PasswordConverter, RedisMutexModule, AuthModule], exports: [PasswordConverter, RedisMutexModule],
imports: [RedisMutexModule],
}) })
export class CommonsModule {} export class CommonsModule {}

View File

@ -6,18 +6,13 @@ import {
HttpStatus, HttpStatus,
} from '@nestjs/common'; } from '@nestjs/common';
import { ApolloError } from 'apollo-server-errors'; import { ApolloError } from 'apollo-server-errors';
import { PinoLogger, InjectPinoLogger } from 'nestjs-pino';
@Catch(HttpException) @Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter { export class HttpExceptionFilter implements ExceptionFilter {
constructor(
@InjectPinoLogger(HttpExceptionFilter.name)
private readonly logger: PinoLogger,
) {}
catch(exception: HttpException, host: ArgumentsHost) { catch(exception: HttpException, host: ArgumentsHost) {
switch (host.getType<'http' | 'graphql' | string>()) { switch (host.getType<'http' | 'graphql' | string>()) {
case 'graphql': { case 'graphql': {
const errorName = exception.message; const message = exception.message;
const extensions: Record<string, any> = {}; const extensions: Record<string, any> = {};
const err = exception.getResponse(); const err = exception.getResponse();
if (typeof err === 'string') { if (typeof err === 'string') {
@ -26,10 +21,8 @@ export class HttpExceptionFilter implements ExceptionFilter {
Object.assign(extensions, (err as any).extension); Object.assign(extensions, (err as any).extension);
extensions.message = (err as any).message; extensions.message = (err as any).message;
} }
extensions.error = errorName;
this.logger.error(extensions);
return new ApolloError( return new ApolloError(
extensions.message, message,
exception.getStatus().toString(), exception.getStatus().toString(),
extensions, extensions,
); );

View File

@ -1,5 +1,4 @@
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { RedisService } from 'nestjs-redis';
import { RedisMutexService } from './redis-mutex.service'; import { RedisMutexService } from './redis-mutex.service';
describe('RedisMutexService', () => { describe('RedisMutexService', () => {
@ -7,13 +6,7 @@ describe('RedisMutexService', () => {
beforeEach(async () => { beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({ const module: TestingModule = await Test.createTestingModule({
providers: [ providers: [RedisMutexService],
RedisMutexService,
{
provide: RedisService,
useValue: {},
},
],
}).compile(); }).compile();
service = module.get<RedisMutexService>(RedisMutexService); service = module.get<RedisMutexService>(RedisMutexService);

View File

@ -1,4 +1,3 @@
import { PinoLogger } from 'nestjs-pino';
import { ValidationPipe } from '@nestjs/common'; import { ValidationPipe } from '@nestjs/common';
import { ConfigService } from '@nestjs/config'; import { ConfigService } from '@nestjs/config';
import { NestFactory } from '@nestjs/core'; import { NestFactory } from '@nestjs/core';
@ -15,8 +14,7 @@ async function bootstrap() {
transform: true, transform: true,
}), }),
); );
const httpExceptionFilterLogger = await app.resolve(PinoLogger); app.useGlobalFilters(new HttpExceptionFilter());
app.useGlobalFilters(new HttpExceptionFilter(httpExceptionFilterLogger));
await app.listen(configService.get<number>('http.port')); await app.listen(configService.get<number>('http.port'));
} }
bootstrap(); bootstrap();

View File

@ -1,6 +1,4 @@
import { Field, InputType, Int, ObjectType } from '@nestjs/graphql'; import { Field, InputType, Int, ObjectType } from '@nestjs/graphql';
import { Type } from 'class-transformer';
import { IsInstance, isInstance, ValidateNested } from 'class-validator';
import { WorkUnit } from './work-unit.model'; import { WorkUnit } from './work-unit.model';
@InputType('WorkUnitMetadataInput') @InputType('WorkUnitMetadataInput')
@ -8,9 +6,5 @@ import { WorkUnit } from './work-unit.model';
export class WorkUnitMetadata { export class WorkUnitMetadata {
@Field(() => Int) @Field(() => Int)
version = 1; version = 1;
@Type(() => WorkUnit)
@IsInstance(WorkUnit, { each: true })
@ValidateNested({ each: true })
units: WorkUnit[]; units: WorkUnit[];
} }

View File

@ -1,5 +1,4 @@
import { Field, InputType, ObjectType } from '@nestjs/graphql'; import { Field, InputType, ObjectType } from '@nestjs/graphql';
import { IsNotEmpty } from 'class-validator';
import { import {
PipelineUnits, PipelineUnits,
PipelineUnits as PipelineUnitTypes, PipelineUnits as PipelineUnitTypes,
@ -10,7 +9,5 @@ import {
export class WorkUnit { export class WorkUnit {
@Field(() => PipelineUnits) @Field(() => PipelineUnits)
type: PipelineUnitTypes; type: PipelineUnitTypes;
@IsNotEmpty({ each: true })
scripts: string[]; scripts: string[];
} }

View File

@ -42,9 +42,9 @@ describe('PipelineTaskRunner', () => {
setTimeout(() => { setTimeout(() => {
logger.handleEvent(event); logger.handleEvent(event);
}); });
await expect( expect(message$.pipe(take(1), timeout(100)).toPromise()).rejects.toMatch(
message$.pipe(take(1), timeout(100)).toPromise(), 'timeout',
).rejects.toThrow(/timeout/i); );
}); });
it('multiple subscribers', async () => { it('multiple subscribers', async () => {
const event = new PipelineTaskEvent(); const event = new PipelineTaskEvent();
@ -53,16 +53,13 @@ describe('PipelineTaskRunner', () => {
const message2$ = logger.getMessage$('test'); const message2$ = logger.getMessage$('test');
setTimeout(() => { setTimeout(() => {
logger.handleEvent(event); logger.handleEvent(event);
logger.handleEvent(event);
}); });
await Promise.all([ expect(message$.pipe(take(1), timeout(100)).toPromise()).resolves.toEqual(
expect( event,
message$.pipe(take(1), timeout(100)).toPromise(), );
).resolves.toEqual(event),
expect( expect(
message2$.pipe(take(1), timeout(100)).toPromise(), message2$.pipe(take(1), timeout(100)).toPromise(),
).resolves.toEqual(event), ).resolves.toEqual(event);
]);
}); });
}); });

View File

@ -13,8 +13,7 @@ import {
@Injectable() @Injectable()
export class PipelineTaskLogger implements OnModuleDestroy { export class PipelineTaskLogger implements OnModuleDestroy {
private readonly messageSubject = new Subject<PipelineTaskEvent>(); private readonly messageSubject = new Subject<PipelineTaskEvent>();
private readonly message$: Observable<PipelineTaskEvent> = private readonly message$: Observable<PipelineTaskEvent> = this.messageSubject.pipe();
this.messageSubject.pipe();
@RabbitSubscribe({ @RabbitSubscribe({
exchange: EXCHANGE_PIPELINE_TASK_FANOUT, exchange: EXCHANGE_PIPELINE_TASK_FANOUT,

View File

@ -1,4 +1,3 @@
import { DeployByPm2Service } from './runners/deploy-by-pm2/deploy-by-pm2.service';
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { ReposService } from '../repos/repos.service'; import { ReposService } from '../repos/repos.service';
import { PipelineUnits } from './enums/pipeline-units.enum'; import { PipelineUnits } from './enums/pipeline-units.enum';
@ -12,7 +11,7 @@ import { WorkUnitMetadata } from './models/work-unit-metadata.model';
import { AmqpConnection } from '@golevelup/nestjs-rabbitmq'; import { AmqpConnection } from '@golevelup/nestjs-rabbitmq';
describe('PipelineTaskRunner', () => { describe('PipelineTaskRunner', () => {
let runner: PipelineTaskRunner; let runner: PipelineTaskRunner;
let deployByPM2Service: DeployByPm2Service; let reposService: ReposService;
beforeEach(async () => { beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({ const module: TestingModule = await Test.createTestingModule({
@ -37,18 +36,11 @@ describe('PipelineTaskRunner', () => {
provide: AmqpConnection, provide: AmqpConnection,
useValue: {}, useValue: {},
}, },
{
provide: DeployByPm2Service,
useValue: {
deploy: () => Promise.resolve(),
},
},
], ],
}).compile(); }).compile();
module.get(ReposService); reposService = module.get(ReposService);
runner = module.get(PipelineTaskRunner); runner = module.get(PipelineTaskRunner);
deployByPM2Service = module.get(DeployByPm2Service);
}); });
it('should be defined', () => { it('should be defined', () => {
@ -73,7 +65,7 @@ describe('PipelineTaskRunner', () => {
beforeEach(() => { beforeEach(() => {
emitEvent = jest emitEvent = jest
.spyOn(runner, 'emitEvent') .spyOn(runner, 'emitEvent')
.mockImplementation(() => Promise.resolve()); .mockImplementation((..._) => Promise.resolve());
}); });
describe('doTask', () => { describe('doTask', () => {
@ -83,10 +75,10 @@ describe('PipelineTaskRunner', () => {
beforeEach(() => { beforeEach(() => {
checkout = jest checkout = jest
.spyOn(runner, 'checkout') .spyOn(runner, 'checkout')
.mockImplementation(() => Promise.resolve('/null')); .mockImplementation((..._) => Promise.resolve('/null'));
doTaskUnit = jest doTaskUnit = jest
.spyOn(runner, 'doTaskUnit') .spyOn(runner, 'doTaskUnit')
.mockImplementation(() => Promise.resolve()); .mockImplementation((..._) => Promise.resolve());
}); });
it('only checkout', async () => { it('only checkout', async () => {
@ -153,7 +145,7 @@ describe('PipelineTaskRunner', () => {
await runner.doTask(task); await runner.doTask(task);
expect(checkout).toBeCalledTimes(1); expect(checkout).toBeCalledTimes(1);
expect(doTaskUnit).toBeCalledTimes(1); expect(doTaskUnit).toBeCalledTimes(2);
expect(emitEvent).toBeCalledTimes(2); expect(emitEvent).toBeCalledTimes(2);
}); });
@ -179,7 +171,9 @@ describe('PipelineTaskRunner', () => {
doTaskUnit = jest doTaskUnit = jest
.spyOn(runner, 'doTaskUnit') .spyOn(runner, 'doTaskUnit')
.mockImplementation(() => Promise.reject(new Error('test error'))); .mockImplementation((..._) =>
Promise.reject(new Error('test error')),
);
await runner.doTask(task); await runner.doTask(task);
expect(checkout).toBeCalledTimes(1); expect(checkout).toBeCalledTimes(1);
@ -195,7 +189,7 @@ describe('PipelineTaskRunner', () => {
it('success', async () => { it('success', async () => {
const runScript = jest const runScript = jest
.spyOn(runner, 'runScript') .spyOn(runner, 'runScript')
.mockImplementation(() => Promise.resolve()); .mockImplementation((..._) => Promise.resolve());
const task = new PipelineTask(); const task = new PipelineTask();
const unit = PipelineUnits.test; const unit = PipelineUnits.test;
@ -216,7 +210,9 @@ describe('PipelineTaskRunner', () => {
it('failed', async () => { it('failed', async () => {
const runScript = jest const runScript = jest
.spyOn(runner, 'runScript') .spyOn(runner, 'runScript')
.mockImplementation(() => Promise.reject(new Error('test error'))); .mockImplementation((..._) =>
Promise.reject(new Error('test error')),
);
const task = new PipelineTask(); const task = new PipelineTask();
const unit = PipelineUnits.test; const unit = PipelineUnits.test;
@ -234,7 +230,7 @@ describe('PipelineTaskRunner', () => {
describe('runScript', () => { describe('runScript', () => {
it('normal', async () => { it('normal', async () => {
const spawn = jest.fn<any, any>(() => ({ const spawn = jest.fn((..._: any[]) => ({
stdout: { stdout: {
on: () => undefined, on: () => undefined,
}, },
@ -260,7 +256,7 @@ describe('PipelineTaskRunner', () => {
}); });
}); });
it('failed', async () => { it('failed', async () => {
const spawn = jest.fn(() => ({ const spawn = jest.fn((..._: any[]) => ({
stdout: { stdout: {
on: () => undefined, on: () => undefined,
}, },
@ -295,7 +291,7 @@ describe('PipelineTaskRunner', () => {
}, 10); }, 10);
}, 10); }, 10);
}); });
const spawn = jest.fn(() => ({ const spawn = jest.fn((..._: any[]) => ({
stdout: { stdout: {
on, on,
}, },
@ -308,7 +304,7 @@ describe('PipelineTaskRunner', () => {
})); }));
let emitSuccessCount = 0; let emitSuccessCount = 0;
jest.spyOn(runner, 'emitEvent').mockImplementation(() => { jest.spyOn(runner, 'emitEvent').mockImplementation((..._: any[]) => {
return new Promise((resolve) => { return new Promise((resolve) => {
setTimeout(() => { setTimeout(() => {
emitSuccessCount++; emitSuccessCount++;
@ -327,25 +323,4 @@ describe('PipelineTaskRunner', () => {
}); });
}); });
}); });
describe('tryRunDeployScript', () => {
it('should be call deploy with right args', async () => {
const deploy = jest.spyOn(deployByPM2Service, 'deploy');
await expect(
runner['tryRunDeployScript'](
'/test/dir',
'@@DEPLOY ecosystem.config.js',
),
).resolves.toBe(true);
expect(deploy.mock.calls[0][0]).toEqual('/test/dir/ecosystem.config.js');
});
it('should return false', async () => {
await expect(
runner['tryRunDeployScript'](
'/test/dir',
'pm2 start ecosystem.config.js',
),
).resolves.toBe(false);
});
});
}); });

View File

@ -1,4 +1,3 @@
import { DeployByPm2Service } from './runners/deploy-by-pm2/deploy-by-pm2.service';
import { ReposService } from '../repos/repos.service'; import { ReposService } from '../repos/repos.service';
import { spawn, ChildProcessWithoutNullStreams } from 'child_process'; import { spawn, ChildProcessWithoutNullStreams } from 'child_process';
import { PipelineTask } from './pipeline-task.entity'; import { PipelineTask } from './pipeline-task.entity';
@ -28,9 +27,6 @@ import {
getSelfInstanceQueueKey, getSelfInstanceQueueKey,
getSelfInstanceRouteKey, getSelfInstanceRouteKey,
} from '../commons/utils/rabbit-mq'; } from '../commons/utils/rabbit-mq';
import { rm } from 'fs/promises';
import { rename } from 'fs/promises';
import { join } from 'path';
type Spawn = typeof spawn; type Spawn = typeof spawn;
@ -45,7 +41,6 @@ export class PipelineTaskRunner {
@Inject('spawn') @Inject('spawn')
private readonly spawn: Spawn, private readonly spawn: Spawn,
private readonly amqpConnection: AmqpConnection, private readonly amqpConnection: AmqpConnection,
private readonly deployByPM2Service: DeployByPm2Service,
) {} ) {}
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'new-pipeline-task', exchange: 'new-pipeline-task',
@ -115,7 +110,7 @@ export class PipelineTaskRunner {
this.logger.info('running task [%s].', task.id); this.logger.info('running task [%s].', task.id);
try { try {
let workspaceRoot = await this.checkout(task); const workspaceRoot = await this.checkout(task);
const units = task.units const units = task.units
.filter((unit) => unit !== PipelineUnits.checkout) .filter((unit) => unit !== PipelineUnits.checkout)
.map( .map(
@ -126,22 +121,6 @@ export class PipelineTaskRunner {
); );
this.logger.info({ units }, 'begin run units.'); this.logger.info({ units }, 'begin run units.');
for (const unit of units) { for (const unit of units) {
if (unit.type === PipelineUnits.deploy) {
const oldRoot = workspaceRoot;
workspaceRoot = this.reposService.getDeployRoot(task);
if (oldRoot !== workspaceRoot) {
await rm(workspaceRoot, { force: true, recursive: true });
await rename(oldRoot, workspaceRoot);
}
await this.emitEvent(
task,
unit.type,
TaskStatuses.success,
`[deploy] change deploy folder content success`,
'stdout',
);
}
await this.doTaskUnit(unit.type, unit.scripts, task, workspaceRoot); await this.doTaskUnit(unit.type, unit.scripts, task, workspaceRoot);
} }
await this.emitEvent( await this.emitEvent(
@ -234,7 +213,6 @@ export class PipelineTaskRunner {
'checkout failed.', 'checkout failed.',
'stderr', 'stderr',
); );
throw err;
} }
} }
@ -277,9 +255,6 @@ export class PipelineTaskRunner {
unit: PipelineUnits, unit: PipelineUnits,
): Promise<void> { ): Promise<void> {
await this.emitEvent(task, unit, TaskStatuses.working, script, 'stdin'); await this.emitEvent(task, unit, TaskStatuses.working, script, 'stdin');
if (await this.tryRunDeployScript(workspaceRoot, script)) {
return;
}
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const sub = this.spawn(script, { const sub = this.spawn(script, {
shell: true, shell: true,
@ -323,17 +298,4 @@ export class PipelineTaskRunner {
}); });
}); });
} }
private async tryRunDeployScript(workspaceRoot: string, script: string) {
const match = /^@@DEPLOY\s+(\S*)/.exec(script);
if (match) {
await this.deployByPM2Service.deploy(
join(workspaceRoot, match[1]),
workspaceRoot,
);
return true;
} else {
return false;
}
}
} }

View File

@ -7,5 +7,3 @@ export const ROUTE_PIPELINE_TASK_DONE = 'pipeline-task-done';
export const QUEUE_PIPELINE_TASK_DONE = 'pipeline-task-done'; export const QUEUE_PIPELINE_TASK_DONE = 'pipeline-task-done';
export const ROUTE_PIPELINE_TASK_KILL = 'pipeline-task-kill'; export const ROUTE_PIPELINE_TASK_KILL = 'pipeline-task-kill';
export const QUEUE_PIPELINE_TASK_KILL = 'pipeline-task-kill'; export const QUEUE_PIPELINE_TASK_KILL = 'pipeline-task-kill';
export const PM2_TOKEN = Symbol('pm2-token');

View File

@ -16,12 +16,9 @@ import {
} from './pipeline-tasks.constants'; } from './pipeline-tasks.constants';
import { PipelineTaskLogger } from './pipeline-task.logger'; import { PipelineTaskLogger } from './pipeline-task.logger';
import { PipelineTaskFlushService } from './pipeline-task-flush.service'; import { PipelineTaskFlushService } from './pipeline-task-flush.service';
import { CommonsModule } from '../commons/commons.module';
import { DeployByPm2Service } from './runners/deploy-by-pm2/deploy-by-pm2.service';
@Module({ @Module({
imports: [ imports: [
CommonsModule,
TypeOrmModule.forFeature([PipelineTask, Pipeline]), TypeOrmModule.forFeature([PipelineTask, Pipeline]),
RedisModule, RedisModule,
ReposModule, ReposModule,
@ -69,7 +66,6 @@ import { DeployByPm2Service } from './runners/deploy-by-pm2/deploy-by-pm2.servic
useValue: spawn, useValue: spawn,
}, },
PipelineTaskFlushService, PipelineTaskFlushService,
DeployByPm2Service,
], ],
exports: [PipelineTasksService], exports: [PipelineTasksService],
}) })

View File

@ -1,4 +1,3 @@
import { JwtService } from '@nestjs-lib/auth';
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { PipelineTaskLogger } from './pipeline-task.logger'; import { PipelineTaskLogger } from './pipeline-task.logger';
import { PipelineTasksResolver } from './pipeline-tasks.resolver'; import { PipelineTasksResolver } from './pipeline-tasks.resolver';
@ -19,10 +18,6 @@ describe('PipelineTasksResolver', () => {
provide: PipelineTaskLogger, provide: PipelineTaskLogger,
useValue: {}, useValue: {},
}, },
{
provide: JwtService,
useValue: {},
},
], ],
}).compile(); }).compile();

View File

@ -7,9 +7,7 @@ import { plainToClass } from 'class-transformer';
import { PipelineTaskLogger } from './pipeline-task.logger'; import { PipelineTaskLogger } from './pipeline-task.logger';
import { observableToAsyncIterable } from '@graphql-tools/utils'; import { observableToAsyncIterable } from '@graphql-tools/utils';
import { PipelineTaskEvent } from './models/pipeline-task-event'; import { PipelineTaskEvent } from './models/pipeline-task-event';
import { Roles, AccountRole } from '@nestjs-lib/auth';
@Roles(AccountRole.admin, AccountRole.super)
@Resolver() @Resolver()
export class PipelineTasksResolver { export class PipelineTasksResolver {
constructor( constructor(

View File

@ -1,117 +0,0 @@
import { Test, TestingModule } from '@nestjs/testing';
import { getLoggerToken, PinoLogger } from 'nestjs-pino';
import { join } from 'path';
import { DeployByPm2Service } from './deploy-by-pm2.service';
describe('DeployByPm2Service', () => {
let service: DeployByPm2Service;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
DeployByPm2Service,
{
provide: getLoggerToken(DeployByPm2Service.name),
useValue: new PinoLogger({
pinoHttp: {
level: 'silent',
},
}),
},
],
}).compile();
service = module.get<DeployByPm2Service>(DeployByPm2Service);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
describe('getAppsSn', () => {
it('should return right value', () => {
expect(
service['getAppsSn']([
{ name: 'app' },
{ name: 'app#4' },
{ name: 'app#1' },
]),
).toEqual(4 + 1);
});
it('should return 1 when no match', () => {
expect(
service['getAppsSn']([
{ name: 'bar' },
{ name: 'foo#4' },
{ name: 'foo#1' },
]),
).toEqual(4 + 1);
});
});
describe('filterOldApps', () => {
it('should return right value', () => {
expect(
service['filterOldApps'](
[{ name: 'app' }],
[
{ name: 'app' },
{ name: 'app#4' },
{ name: 'foo#2' },
{ name: 'bar' },
],
),
).toEqual([{ name: 'app' }, { name: 'app#4' }]);
});
it('should return [] when no match', () => {
expect(
service['filterOldApps'](
[{ name: 'app' }],
[{ name: 'foo#2' }, { name: 'bar' }],
),
).toEqual([]);
});
});
describe('replaceAppName', () => {
it('should be replaced with right value', () => {
const getAppsSn = jest
.spyOn(service, 'getAppsSn' as any)
.mockImplementation(() => 1);
const options = [{ name: 'app' }, { name: 'foo' }];
service['replaceAppName'](options, []);
expect(options).toEqual([{ name: 'app#1' }, { name: 'foo#1' }]);
expect(getAppsSn).toBeCalledTimes(1);
expect(getAppsSn.mock.calls[0][0]).toEqual([]);
});
});
describe('deploy', () => {
it('should be success', async () => {
const filterOldApps = jest
.spyOn(service, 'filterOldApps' as any)
.mockImplementation(() => [{ name: 'app#1' }, { name: 'app#2' }]);
const replaceAppName = jest
.spyOn(service, 'replaceAppName' as any)
.mockImplementation((options) => (options[0].name = 'app#2'));
const stopApps = jest
.spyOn(service, 'stopApps' as any)
.mockImplementation(() => Promise.resolve());
await expect(
service['deploy'](
join(
__dirname,
'../../../../test/__mocks__/deploy-service/ecosystem.config.js',
),
join(__dirname, '../../../../test/__mocks__/deploy-service'),
),
).resolves.toBeFalsy();
expect(filterOldApps).toBeCalledTimes(1);
expect(replaceAppName).toBeCalledTimes(1);
expect(stopApps).toBeCalledTimes(1);
stopApps.mockReset();
await service['stopApps']([{ name: 'app#2' }]);
}, 10_000);
});
});

View File

@ -1,98 +0,0 @@
import { InjectPinoLogger, PinoLogger } from 'nestjs-pino';
import { Injectable } from '@nestjs/common';
import { promisify } from 'util';
import * as pm2 from 'pm2';
import { Proc, ProcessDescription, StartOptions } from 'pm2';
import { clone, last } from 'ramda';
@Injectable()
export class DeployByPm2Service {
constructor(
@InjectPinoLogger(DeployByPm2Service.name)
private readonly logger: PinoLogger,
) {}
async deploy(filePath: string, workspace: string) {
const baseConfig: { apps: StartOptions[] } = await import(filePath);
const appOptionsList: StartOptions[] = clone(baseConfig.apps);
await promisify<void>(pm2.connect.bind(pm2))();
const allApps = await promisify(pm2.list.bind(pm2))();
try {
if (!Array.isArray(baseConfig.apps)) {
this.logger.error(
'the "apps" in the PM2 ecosystem configuration is not array',
);
throw new Error('apps is not array');
}
const oldApps = this.filterOldApps(appOptionsList, allApps);
this.replaceAppName(appOptionsList, oldApps);
for (const appOptions of appOptionsList) {
const proc = await promisify<StartOptions, Proc>(pm2.start.bind(pm2))({
...appOptions,
cwd: workspace,
});
this.logger.info({ proc }, `start ${appOptions.name}`);
}
await this.stopApps(oldApps);
} catch (err) {
await this.stopApps(appOptionsList);
throw err;
} finally {
pm2.disconnect();
}
}
private async stopApps(apps: ProcessDescription[] | StartOptions[]) {
await Promise.all(
apps.map(async (app: ProcessDescription | StartOptions) => {
let procAtStop: ProcessDescription;
let procAtDelete: ProcessDescription;
try {
const idOrName = 'pm_id' in app ? app.pm_id : app.name;
procAtStop = await promisify(pm2.stop.bind(pm2))(idOrName);
procAtDelete = await promisify(pm2.delete.bind(pm2))(idOrName);
this.logger.info('stop & delete %s success', app.name);
} catch (error) {
this.logger.error(
{ error, procAtStop, procAtDelete },
'stop & delete %s error',
app.name,
);
}
}),
);
}
private replaceAppName(
optionsList: StartOptions[],
oldApps: ProcessDescription[],
) {
const appSn = this.getAppsSn(oldApps);
optionsList.forEach((options) => {
if (!options.name) {
this.logger.error('please give a name for application');
throw new Error('app name is not given');
}
options.name = `${options.name}#${appSn}`;
});
}
private filterOldApps(
optionsList: StartOptions[],
apps: ProcessDescription[],
) {
return apps.filter((app) =>
optionsList.some((options) => app.name.split('#')[0] === options.name),
);
}
private getAppsSn(oldApps: ProcessDescription[]) {
const appsSn: number[] = oldApps.map(
(app) => +(app.name.split('#')?.[1] ?? 0),
);
return (last(appsSn.sort()) ?? 0) + 1;
}
}

View File

@ -1,4 +1,3 @@
import { JwtService } from '@nestjs-lib/auth';
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { PipelineTasksService } from '../pipeline-tasks/pipeline-tasks.service'; import { PipelineTasksService } from '../pipeline-tasks/pipeline-tasks.service';
import { CommitLogsResolver } from './commit-logs.resolver'; import { CommitLogsResolver } from './commit-logs.resolver';
@ -19,10 +18,6 @@ describe('CommitLogsResolver', () => {
provide: PipelineTasksService, provide: PipelineTasksService,
useValue: {}, useValue: {},
}, },
{
provide: JwtService,
useValue: {},
},
], ],
}).compile(); }).compile();

View File

@ -1,4 +1,3 @@
import { Roles, AccountRole } from '@nestjs-lib/auth';
import { Query } from '@nestjs/graphql'; import { Query } from '@nestjs/graphql';
import { import {
Args, Args,
@ -11,7 +10,6 @@ import { PipelineTasksService } from '../pipeline-tasks/pipeline-tasks.service';
import { Commit, LogFields } from '../repos/dtos/log-list.model'; import { Commit, LogFields } from '../repos/dtos/log-list.model';
import { PipelinesService } from './pipelines.service'; import { PipelinesService } from './pipelines.service';
@Roles(AccountRole.admin, AccountRole.super)
@Resolver(() => Commit) @Resolver(() => Commit)
export class CommitLogsResolver { export class CommitLogsResolver {
constructor( constructor(

View File

@ -1,13 +1,11 @@
import { Type } from 'class-transformer';
import { InputType } from '@nestjs/graphql'; import { InputType } from '@nestjs/graphql';
import { WorkUnitMetadata } from '../../pipeline-tasks/models/work-unit-metadata.model'; import { WorkUnitMetadata } from '../../pipeline-tasks/models/work-unit-metadata.model';
import { import {
IsInstance, IsObject,
IsOptional, IsOptional,
IsString, IsString,
IsUUID, IsUUID,
MaxLength, MaxLength,
ValidateNested,
} from 'class-validator'; } from 'class-validator';
@InputType({ isAbstract: true }) @InputType({ isAbstract: true })
@ -23,9 +21,7 @@ export class CreatePipelineInput {
@MaxLength(32) @MaxLength(32)
name: string; name: string;
@Type(() => WorkUnitMetadata)
@IsOptional() @IsOptional()
@ValidateNested() @IsObject()
@IsInstance(WorkUnitMetadata)
workUnitMetadata: WorkUnitMetadata; workUnitMetadata: WorkUnitMetadata;
} }

View File

@ -8,11 +8,9 @@ import { PipelineTasksModule } from '../pipeline-tasks/pipeline-tasks.module';
import { ReposModule } from '../repos/repos.module'; import { ReposModule } from '../repos/repos.module';
import { RabbitMQModule } from '@golevelup/nestjs-rabbitmq'; import { RabbitMQModule } from '@golevelup/nestjs-rabbitmq';
import { ConfigModule, ConfigService } from '@nestjs/config'; import { ConfigModule, ConfigService } from '@nestjs/config';
import { CommonsModule } from '../commons/commons.module';
@Module({ @Module({
imports: [ imports: [
CommonsModule,
TypeOrmModule.forFeature([Pipeline]), TypeOrmModule.forFeature([Pipeline]),
PipelineTasksModule, PipelineTasksModule,
RabbitMQModule.forRootAsync(RabbitMQModule, { RabbitMQModule.forRootAsync(RabbitMQModule, {

View File

@ -1,4 +1,3 @@
import { JwtService } from '@nestjs-lib/auth';
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { PipelinesResolver } from './pipelines.resolver'; import { PipelinesResolver } from './pipelines.resolver';
import { PipelinesService } from './pipelines.service'; import { PipelinesService } from './pipelines.service';
@ -14,10 +13,6 @@ describe('PipelinesResolver', () => {
provide: PipelinesService, provide: PipelinesService,
useValue: {}, useValue: {},
}, },
{
provide: JwtService,
useValue: {},
},
], ],
}).compile(); }).compile();

View File

@ -4,9 +4,7 @@ import { UpdatePipelineInput } from './dtos/update-pipeline.input';
import { Pipeline } from './pipeline.entity'; import { Pipeline } from './pipeline.entity';
import { PipelinesService } from './pipelines.service'; import { PipelinesService } from './pipelines.service';
import { ListPipelineArgs } from './dtos/list-pipelines.args'; import { ListPipelineArgs } from './dtos/list-pipelines.args';
import { Roles, AccountRole } from '@nestjs-lib/auth';
@Roles(AccountRole.admin, AccountRole.super)
@Resolver() @Resolver()
export class PipelinesResolver { export class PipelinesResolver {
constructor(private readonly service: PipelinesService) {} constructor(private readonly service: PipelinesService) {}

View File

@ -6,11 +6,9 @@ import { Project } from './project.entity';
import { RabbitMQModule } from '@golevelup/nestjs-rabbitmq'; import { RabbitMQModule } from '@golevelup/nestjs-rabbitmq';
import { ConfigModule, ConfigService } from '@nestjs/config'; import { ConfigModule, ConfigService } from '@nestjs/config';
import { EXCHANGE_PROJECT_FANOUT } from './projects.constants'; import { EXCHANGE_PROJECT_FANOUT } from './projects.constants';
import { CommonsModule } from '../commons/commons.module';
@Module({ @Module({
imports: [ imports: [
CommonsModule,
TypeOrmModule.forFeature([Project]), TypeOrmModule.forFeature([Project]),
RabbitMQModule.forRootAsync(RabbitMQModule, { RabbitMQModule.forRootAsync(RabbitMQModule, {
imports: [ConfigModule], imports: [ConfigModule],

View File

@ -1,4 +1,3 @@
import { JwtService } from '@nestjs-lib/auth';
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { ProjectsResolver } from './projects.resolver'; import { ProjectsResolver } from './projects.resolver';
import { ProjectsService } from './projects.service'; import { ProjectsService } from './projects.service';
@ -14,10 +13,6 @@ describe('ProjectsResolver', () => {
provide: ProjectsService, provide: ProjectsService,
useValue: {}, useValue: {},
}, },
{
provide: JwtService,
useValue: {},
},
], ],
}).compile(); }).compile();

View File

@ -1,11 +1,9 @@
import { AccountRole, Roles } from '@nestjs-lib/auth';
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql'; import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
import { CreateProjectInput } from './dtos/create-project.input'; import { CreateProjectInput } from './dtos/create-project.input';
import { UpdateProjectInput } from './dtos/update-project.input'; import { UpdateProjectInput } from './dtos/update-project.input';
import { Project } from './project.entity'; import { Project } from './project.entity';
import { ProjectsService } from './projects.service'; import { ProjectsService } from './projects.service';
@Roles(AccountRole.admin, AccountRole.super)
@Resolver(() => Project) @Resolver(() => Project)
export class ProjectsResolver { export class ProjectsResolver {
constructor(private readonly service: ProjectsService) {} constructor(private readonly service: ProjectsService) {}
@ -22,7 +20,7 @@ export class ProjectsResolver {
@Mutation(() => Project) @Mutation(() => Project)
async createProject( async createProject(
@Args('project', { type: () => CreateProjectInput }) @Args('project', { type: () => CreateProjectInput })
dto: CreateProjectInput, dto: UpdateProjectInput,
) { ) {
return await this.service.create(dto); return await this.service.create(dto);
} }

View File

@ -12,7 +12,6 @@ import { readFile } from 'fs/promises';
import { getLoggerToken, PinoLogger } from 'nestjs-pino'; import { getLoggerToken, PinoLogger } from 'nestjs-pino';
import { Nack } from '@golevelup/nestjs-rabbitmq'; import { Nack } from '@golevelup/nestjs-rabbitmq';
import { getInstanceName } from '../commons/utils/rabbit-mq'; import { getInstanceName } from '../commons/utils/rabbit-mq';
import { RedisMutexService } from '../commons/redis-mutex/redis-mutex.service';
const getTest1Project = () => const getTest1Project = () =>
({ ({
@ -53,14 +52,6 @@ describe('ReposService', () => {
provide: getLoggerToken(ReposService.name), provide: getLoggerToken(ReposService.name),
useValue: new PinoLogger({}), useValue: new PinoLogger({}),
}, },
{
provide: RedisMutexService,
useValue: {
lock: jest.fn(() =>
Promise.resolve(() => Promise.resolve(undefined)),
),
},
},
], ],
}).compile(); }).compile();

View File

@ -56,14 +56,6 @@ export class ReposService {
); );
} }
getDeployRoot(task: PipelineTask) {
return join(
this.configService.get<string>('workspaces.root'),
encodeURIComponent(task.pipeline.project.name),
encodeURIComponent(`deploy-${task.pipeline.name}`),
);
}
async getGit( async getGit(
project: Project, project: Project,
workspaceRoot?: string, workspaceRoot?: string,

View File

@ -1,15 +0,0 @@
module.exports = {
apps: [
{
name: 'app',
script: __dirname + '/index.js',
watch: false,
ignore_watch: ['node_modules'],
log_date_format: 'MM-DD HH:mm:ss.SSS Z',
env: {},
max_restarts: 5,
kill_timeout: 10_000,
wait_ready: true,
},
],
};

View File

@ -1,23 +0,0 @@
import { createServer } from 'http';
var app = createServer(function (req, res) {
res.writeHead(200);
setTimeout(() => {
res.end('hey');
}, 2000);
});
var listener = app.listen(0, function () {
console.log('Listening on port ' + listener.address().port);
setTimeout(() => {
// Here we send the ready signal to PM2
process.send('ready');
}, 5000);
});
process.on('SIGINT', function () {
listener.close();
setTimeout(() => {
process.exit(0);
}, 2000);
});

View File

@ -1,10 +0,0 @@
{
"name": "deploy-service",
"version": "1.0.0",
"description": "For Test",
"main": "index.js",
"type": "module",
"scripts": {},
"author": "Ivan Li",
"license": "ISC"
}

View File

@ -1,16 +0,0 @@
import { createServer } from 'http';
var app = createServer(function (req, res) {
res.writeHead(200);
setTimeout(() => {
res.end('pm2');
}, 2000);
});
var listener = app.listen(33333, function () {
console.log('Listening on port ' + listener.address().port);
setTimeout(() => {
// Here we send the ready signal to PM2
process.send('ready');
}, 5000);
});

View File

@ -7,10 +7,9 @@
"experimentalDecorators": true, "experimentalDecorators": true,
"allowSyntheticDefaultImports": true, "allowSyntheticDefaultImports": true,
"target": "es2017", "target": "es2017",
"lib": ["ES2021"],
"sourceMap": true, "sourceMap": true,
"outDir": "./dist", "outDir": "./dist",
"rootDir": "./src",
"baseUrl": "./", "baseUrl": "./",
}, "incremental": true
}
} }