diff options
Diffstat (limited to 'server/controllers/api/runners/manage-runners.ts')
-rw-r--r-- | server/controllers/api/runners/manage-runners.ts | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/server/controllers/api/runners/manage-runners.ts b/server/controllers/api/runners/manage-runners.ts new file mode 100644 index 000000000..eb08c4b1d --- /dev/null +++ b/server/controllers/api/runners/manage-runners.ts | |||
@@ -0,0 +1,107 @@ | |||
1 | import express from 'express' | ||
2 | import { logger, loggerTagsFactory } from '@server/helpers/logger' | ||
3 | import { generateRunnerToken } from '@server/helpers/token-generator' | ||
4 | import { | ||
5 | asyncMiddleware, | ||
6 | authenticate, | ||
7 | ensureUserHasRight, | ||
8 | paginationValidator, | ||
9 | runnersSortValidator, | ||
10 | setDefaultPagination, | ||
11 | setDefaultSort | ||
12 | } from '@server/middlewares' | ||
13 | import { deleteRunnerValidator, getRunnerFromTokenValidator, registerRunnerValidator } from '@server/middlewares/validators/runners' | ||
14 | import { RunnerModel } from '@server/models/runner/runner' | ||
15 | import { HttpStatusCode, ListRunnersQuery, RegisterRunnerBody, UserRight } from '@shared/models' | ||
16 | |||
17 | const lTags = loggerTagsFactory('api', 'runner') | ||
18 | |||
19 | const manageRunnersRouter = express.Router() | ||
20 | |||
21 | manageRunnersRouter.post('/register', | ||
22 | asyncMiddleware(registerRunnerValidator), | ||
23 | asyncMiddleware(registerRunner) | ||
24 | ) | ||
25 | manageRunnersRouter.post('/unregister', | ||
26 | asyncMiddleware(getRunnerFromTokenValidator), | ||
27 | asyncMiddleware(unregisterRunner) | ||
28 | ) | ||
29 | |||
30 | manageRunnersRouter.delete('/:runnerId', | ||
31 | authenticate, | ||
32 | ensureUserHasRight(UserRight.MANAGE_RUNNERS), | ||
33 | asyncMiddleware(deleteRunnerValidator), | ||
34 | asyncMiddleware(deleteRunner) | ||
35 | ) | ||
36 | |||
37 | manageRunnersRouter.get('/', | ||
38 | authenticate, | ||
39 | ensureUserHasRight(UserRight.MANAGE_RUNNERS), | ||
40 | paginationValidator, | ||
41 | runnersSortValidator, | ||
42 | setDefaultSort, | ||
43 | setDefaultPagination, | ||
44 | asyncMiddleware(listRunners) | ||
45 | ) | ||
46 | |||
47 | // --------------------------------------------------------------------------- | ||
48 | |||
49 | export { | ||
50 | manageRunnersRouter | ||
51 | } | ||
52 | |||
53 | // --------------------------------------------------------------------------- | ||
54 | |||
55 | async function registerRunner (req: express.Request, res: express.Response) { | ||
56 | const body: RegisterRunnerBody = req.body | ||
57 | |||
58 | const runnerToken = generateRunnerToken() | ||
59 | |||
60 | const runner = new RunnerModel({ | ||
61 | runnerToken, | ||
62 | name: body.name, | ||
63 | description: body.description, | ||
64 | lastContact: new Date(), | ||
65 | ip: req.ip, | ||
66 | runnerRegistrationTokenId: res.locals.runnerRegistrationToken.id | ||
67 | }) | ||
68 | |||
69 | await runner.save() | ||
70 | |||
71 | logger.info('Registered new runner %s', runner.name, { ...lTags(runner.name) }) | ||
72 | |||
73 | return res.json({ id: runner.id, runnerToken }) | ||
74 | } | ||
75 | async function unregisterRunner (req: express.Request, res: express.Response) { | ||
76 | const runner = res.locals.runner | ||
77 | await runner.destroy() | ||
78 | |||
79 | logger.info('Unregistered runner %s', runner.name, { ...lTags(runner.name) }) | ||
80 | |||
81 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | ||
82 | } | ||
83 | |||
84 | async function deleteRunner (req: express.Request, res: express.Response) { | ||
85 | const runner = res.locals.runner | ||
86 | |||
87 | await runner.destroy() | ||
88 | |||
89 | logger.info('Deleted runner %s', runner.name, { ...lTags(runner.name) }) | ||
90 | |||
91 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | ||
92 | } | ||
93 | |||
94 | async function listRunners (req: express.Request, res: express.Response) { | ||
95 | const query: ListRunnersQuery = req.query | ||
96 | |||
97 | const resultList = await RunnerModel.listForApi({ | ||
98 | start: query.start, | ||
99 | count: query.count, | ||
100 | sort: query.sort | ||
101 | }) | ||
102 | |||
103 | return res.json({ | ||
104 | total: resultList.total, | ||
105 | data: resultList.data.map(d => d.toFormattedJSON()) | ||
106 | }) | ||
107 | } | ||