-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #164 from boostcampwm2023/feature-be-#140
[BE]feat#140 ์ฑ์ ํ ์ ์๋ ๊ธฐ๋ณธ ๋ชจ๋์ ๋ง๋ ๋ค
- Loading branch information
Showing
12 changed files
with
239 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { Client } from 'ssh2'; | ||
import 'dotenv/config'; | ||
|
||
async function getSSH( | ||
host: string, | ||
port: number, | ||
username: string, | ||
password: string, | ||
): Promise<Client> { | ||
const conn = new Client(); | ||
|
||
await new Promise<void>((resolve, reject) => { | ||
conn | ||
.on('ready', () => resolve()) | ||
.on('error', reject) | ||
.connect({ | ||
host, | ||
port, | ||
username, | ||
password, | ||
}); | ||
}); | ||
|
||
return conn; | ||
} | ||
export async function executeSSHCommand( | ||
command: string, | ||
): Promise<{ stdoutData: string; stderrData: string }> { | ||
const conn: Client = await getSSH( | ||
process.env.CONTAINER_SSH_HOST, | ||
Number(process.env.CONTAINER_SSH_PORT), | ||
process.env.CONTAINER_SSH_USERNAME, | ||
process.env.CONTAINER_SSH_PASSWORD, | ||
); | ||
|
||
return new Promise((resolve, reject) => { | ||
conn.exec(command, (err, stream) => { | ||
if (err) { | ||
reject(new Error('SSH command execution Server error')); | ||
return; | ||
} | ||
let stdoutData = ''; | ||
let stderrData = ''; | ||
stream | ||
.on('close', () => { | ||
conn.end(); | ||
resolve({ stdoutData, stderrData }); | ||
}) | ||
.on('data', (chunk) => { | ||
stdoutData += chunk; | ||
}); | ||
|
||
stream.stderr.on('data', (chunk) => { | ||
stderrData += chunk; | ||
}); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { QuizWizardService } from './quiz-wizard.service'; | ||
|
||
@Module({ | ||
providers: [QuizWizardService], | ||
exports: [QuizWizardService], | ||
}) | ||
export class QuizWizardModule {} |
18 changes: 18 additions & 0 deletions
18
packages/backend/src/quiz-wizard/quiz-wizard.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { QuizWizardService } from './quiz-wizard.service'; | ||
|
||
describe('QuizWizardService', () => { | ||
let service: QuizWizardService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [QuizWizardService], | ||
}).compile(); | ||
|
||
service = module.get<QuizWizardService>(QuizWizardService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import fs from 'fs'; | ||
import * as path from 'path'; | ||
import { promisify } from 'util'; | ||
import { exec } from 'child_process'; | ||
|
||
@Injectable() | ||
export class QuizWizardService { | ||
async submit(containerId, quizId) { | ||
const execAsync = promisify(exec); | ||
|
||
console.log('ํ์ฌ ๋๋ ํ ๋ฆฌ ์์น:', process.cwd()); | ||
try { | ||
await execAsync( | ||
`yarn run jest ${path.resolve( | ||
process.cwd(), | ||
'src', | ||
'quiz-wizard', | ||
'tests', | ||
`${quizId}.spec.ts`, | ||
)} ${containerId}`, | ||
); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
checkDirectoryExists(directoryPath) { | ||
const doesExist = fs.existsSync(directoryPath); | ||
describe('Directory Existence Check', () => { | ||
it('should check if a directory exists', () => { | ||
expect(doesExist).toBe(true); | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { executeSSHCommand } from '../../common/ssh.util'; | ||
|
||
export async function isDirectoryExist( | ||
container: string, | ||
path: string, | ||
): Promise<boolean> { | ||
const { stdoutData } = await executeSSHCommand( | ||
`docker exec -w /home/quizzer/quiz/ -u quizzer ${container} /usr/local/bin/sh "ls -l ${path} | grep ^d"`, | ||
); | ||
|
||
return stdoutData !== ''; | ||
} | ||
|
||
export async function getConfig( | ||
container: string, | ||
key: string, | ||
): Promise<string> { | ||
const { stdoutData } = await executeSSHCommand( | ||
`docker exec -u quizzer -w /home/quizzer/quiz ${container} git -C /home/quizzer/quiz config user.${key}`, | ||
); | ||
|
||
return stdoutData; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsBoolean, IsString } from 'class-validator'; | ||
|
||
export class Success { | ||
@ApiProperty({ example: true }) | ||
@IsBoolean() | ||
solved = true; | ||
|
||
@ApiProperty({ example: 'gitchallenge.com/api/v1/quizzes/shared?answer=โโ' }) | ||
@IsString() | ||
link: string; | ||
|
||
constructor(link: string) { | ||
this.link = link; | ||
} | ||
} | ||
|
||
export class Fail { | ||
@ApiProperty({ example: false }) | ||
solved = false; | ||
} | ||
|
||
export type SubmitDto = Success | Fail; |
Oops, something went wrong.