aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/runners
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-04-21 14:55:10 +0200
committerChocobozzz <chocobozzz@cpy.re>2023-05-09 08:57:34 +0200
commit0c9668f77901e7540e2c7045eb0f2974a4842a69 (patch)
tree226d3dd1565b0bb56588897af3b8530e6216e96b /server/middlewares/validators/runners
parent6bcb854cdea8688a32240bc5719c7d139806e00b (diff)
downloadPeerTube-0c9668f77901e7540e2c7045eb0f2974a4842a69.tar.gz
PeerTube-0c9668f77901e7540e2c7045eb0f2974a4842a69.tar.zst
PeerTube-0c9668f77901e7540e2c7045eb0f2974a4842a69.zip
Implement remote runner jobs in server
Move ffmpeg functions to @shared
Diffstat (limited to 'server/middlewares/validators/runners')
-rw-r--r--server/middlewares/validators/runners/index.ts3
-rw-r--r--server/middlewares/validators/runners/job-files.ts27
-rw-r--r--server/middlewares/validators/runners/jobs.ts156
-rw-r--r--server/middlewares/validators/runners/registration-token.ts37
-rw-r--r--server/middlewares/validators/runners/runners.ts95
5 files changed, 318 insertions, 0 deletions
diff --git a/server/middlewares/validators/runners/index.ts b/server/middlewares/validators/runners/index.ts
new file mode 100644
index 000000000..9a9629a80
--- /dev/null
+++ b/server/middlewares/validators/runners/index.ts
@@ -0,0 +1,3 @@
1export * from './jobs'
2export * from './registration-token'
3export * from './runners'
diff --git a/server/middlewares/validators/runners/job-files.ts b/server/middlewares/validators/runners/job-files.ts
new file mode 100644
index 000000000..56afa39aa
--- /dev/null
+++ b/server/middlewares/validators/runners/job-files.ts
@@ -0,0 +1,27 @@
1import express from 'express'
2import { HttpStatusCode } from '@shared/models'
3import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared'
4
5const tags = [ 'runner' ]
6
7export const runnerJobGetVideoTranscodingFileValidator = [
8 isValidVideoIdParam('videoId'),
9
10 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
11 if (areValidationErrors(req, res)) return
12
13 if (!await doesVideoExist(req.params.videoId, res, 'all')) return
14
15 const runnerJob = res.locals.runnerJob
16
17 if (runnerJob.privatePayload.videoUUID !== res.locals.videoAll.uuid) {
18 return res.fail({
19 status: HttpStatusCode.FORBIDDEN_403,
20 message: 'Job is not associated to this video',
21 tags: [ ...tags, res.locals.videoAll.uuid ]
22 })
23 }
24
25 return next()
26 }
27]
diff --git a/server/middlewares/validators/runners/jobs.ts b/server/middlewares/validators/runners/jobs.ts
new file mode 100644
index 000000000..8cb87e946
--- /dev/null
+++ b/server/middlewares/validators/runners/jobs.ts
@@ -0,0 +1,156 @@
1import express from 'express'
2import { body, param } from 'express-validator'
3import { isUUIDValid } from '@server/helpers/custom-validators/misc'
4import {
5 isRunnerJobAbortReasonValid,
6 isRunnerJobErrorMessageValid,
7 isRunnerJobProgressValid,
8 isRunnerJobSuccessPayloadValid,
9 isRunnerJobTokenValid,
10 isRunnerJobUpdatePayloadValid
11} from '@server/helpers/custom-validators/runners/jobs'
12import { isRunnerTokenValid } from '@server/helpers/custom-validators/runners/runners'
13import { cleanUpReqFiles } from '@server/helpers/express-utils'
14import { RunnerJobModel } from '@server/models/runner/runner-job'
15import { HttpStatusCode, RunnerJobState, RunnerJobSuccessBody, RunnerJobUpdateBody, ServerErrorCode } from '@shared/models'
16import { areValidationErrors } from '../shared'
17
18const tags = [ 'runner' ]
19
20export const acceptRunnerJobValidator = [
21 (req: express.Request, res: express.Response, next: express.NextFunction) => {
22 if (res.locals.runnerJob.state !== RunnerJobState.PENDING) {
23 return res.fail({
24 status: HttpStatusCode.BAD_REQUEST_400,
25 message: 'This runner job is not in pending state',
26 tags
27 })
28 }
29
30 return next()
31 }
32]
33
34export const abortRunnerJobValidator = [
35 body('reason').custom(isRunnerJobAbortReasonValid),
36
37 (req: express.Request, res: express.Response, next: express.NextFunction) => {
38 if (areValidationErrors(req, res, { tags })) return
39
40 return next()
41 }
42]
43
44export const updateRunnerJobValidator = [
45 body('progress').optional().custom(isRunnerJobProgressValid),
46
47 (req: express.Request, res: express.Response, next: express.NextFunction) => {
48 if (areValidationErrors(req, res, { tags })) return cleanUpReqFiles(req)
49
50 const body = req.body as RunnerJobUpdateBody
51
52 if (isRunnerJobUpdatePayloadValid(body.payload, res.locals.runnerJob.type, req.files) !== true) {
53 cleanUpReqFiles(req)
54
55 return res.fail({
56 status: HttpStatusCode.BAD_REQUEST_400,
57 message: 'Payload is invalid',
58 tags
59 })
60 }
61
62 return next()
63 }
64]
65
66export const errorRunnerJobValidator = [
67 body('message').custom(isRunnerJobErrorMessageValid),
68
69 (req: express.Request, res: express.Response, next: express.NextFunction) => {
70 if (areValidationErrors(req, res, { tags })) return
71
72 return next()
73 }
74]
75
76export const successRunnerJobValidator = [
77 (req: express.Request, res: express.Response, next: express.NextFunction) => {
78 const body = req.body as RunnerJobSuccessBody
79
80 if (isRunnerJobSuccessPayloadValid(body.payload, res.locals.runnerJob.type, req.files) !== true) {
81 cleanUpReqFiles(req)
82
83 return res.fail({
84 status: HttpStatusCode.BAD_REQUEST_400,
85 message: 'Payload is invalid',
86 tags
87 })
88 }
89
90 return next()
91 }
92]
93
94export const runnerJobGetValidator = [
95 param('jobUUID').custom(isUUIDValid),
96
97 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
98 if (areValidationErrors(req, res, { tags })) return
99
100 const runnerJob = await RunnerJobModel.loadWithRunner(req.params.jobUUID)
101
102 if (!runnerJob) {
103 return res.fail({
104 status: HttpStatusCode.NOT_FOUND_404,
105 message: 'Unknown runner job',
106 tags
107 })
108 }
109
110 res.locals.runnerJob = runnerJob
111
112 return next()
113 }
114]
115
116export const jobOfRunnerGetValidator = [
117 param('jobUUID').custom(isUUIDValid),
118
119 body('runnerToken').custom(isRunnerTokenValid),
120 body('jobToken').custom(isRunnerJobTokenValid),
121
122 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
123 if (areValidationErrors(req, res, { tags })) return cleanUpReqFiles(req)
124
125 const runnerJob = await RunnerJobModel.loadByRunnerAndJobTokensWithRunner({
126 uuid: req.params.jobUUID,
127 runnerToken: req.body.runnerToken,
128 jobToken: req.body.jobToken
129 })
130
131 if (!runnerJob) {
132 cleanUpReqFiles(req)
133
134 return res.fail({
135 status: HttpStatusCode.NOT_FOUND_404,
136 message: 'Unknown runner job',
137 tags
138 })
139 }
140
141 if (runnerJob.state !== RunnerJobState.PROCESSING) {
142 cleanUpReqFiles(req)
143
144 return res.fail({
145 status: HttpStatusCode.BAD_REQUEST_400,
146 type: ServerErrorCode.RUNNER_JOB_NOT_IN_PROCESSING_STATE,
147 message: 'Job is not in "processing" state',
148 tags
149 })
150 }
151
152 res.locals.runnerJob = runnerJob
153
154 return next()
155 }
156]
diff --git a/server/middlewares/validators/runners/registration-token.ts b/server/middlewares/validators/runners/registration-token.ts
new file mode 100644
index 000000000..cc31d4a7e
--- /dev/null
+++ b/server/middlewares/validators/runners/registration-token.ts
@@ -0,0 +1,37 @@
1import express from 'express'
2import { param } from 'express-validator'
3import { isIdValid } from '@server/helpers/custom-validators/misc'
4import { RunnerRegistrationTokenModel } from '@server/models/runner/runner-registration-token'
5import { forceNumber } from '@shared/core-utils'
6import { HttpStatusCode } from '@shared/models'
7import { areValidationErrors } from '../shared/utils'
8
9const tags = [ 'runner' ]
10
11const deleteRegistrationTokenValidator = [
12 param('id').custom(isIdValid),
13
14 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
15 if (areValidationErrors(req, res, { tags })) return
16
17 const registrationToken = await RunnerRegistrationTokenModel.load(forceNumber(req.params.id))
18
19 if (!registrationToken) {
20 return res.fail({
21 status: HttpStatusCode.NOT_FOUND_404,
22 message: 'Registration token not found',
23 tags
24 })
25 }
26
27 res.locals.runnerRegistrationToken = registrationToken
28
29 return next()
30 }
31]
32
33// ---------------------------------------------------------------------------
34
35export {
36 deleteRegistrationTokenValidator
37}
diff --git a/server/middlewares/validators/runners/runners.ts b/server/middlewares/validators/runners/runners.ts
new file mode 100644
index 000000000..71a1275d2
--- /dev/null
+++ b/server/middlewares/validators/runners/runners.ts
@@ -0,0 +1,95 @@
1import express from 'express'
2import { body, param } from 'express-validator'
3import { isIdValid } from '@server/helpers/custom-validators/misc'
4import {
5 isRunnerDescriptionValid,
6 isRunnerNameValid,
7 isRunnerRegistrationTokenValid,
8 isRunnerTokenValid
9} from '@server/helpers/custom-validators/runners/runners'
10import { RunnerModel } from '@server/models/runner/runner'
11import { RunnerRegistrationTokenModel } from '@server/models/runner/runner-registration-token'
12import { forceNumber } from '@shared/core-utils'
13import { HttpStatusCode, RegisterRunnerBody, ServerErrorCode } from '@shared/models'
14import { areValidationErrors } from '../shared/utils'
15
16const tags = [ 'runner' ]
17
18const registerRunnerValidator = [
19 body('registrationToken').custom(isRunnerRegistrationTokenValid),
20 body('name').custom(isRunnerNameValid),
21 body('description').optional().custom(isRunnerDescriptionValid),
22
23 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
24 if (areValidationErrors(req, res, { tags })) return
25
26 const body: RegisterRunnerBody = req.body
27
28 const runnerRegistrationToken = await RunnerRegistrationTokenModel.loadByRegistrationToken(body.registrationToken)
29
30 if (!runnerRegistrationToken) {
31 return res.fail({
32 status: HttpStatusCode.NOT_FOUND_404,
33 message: 'Registration token is invalid',
34 tags
35 })
36 }
37
38 res.locals.runnerRegistrationToken = runnerRegistrationToken
39
40 return next()
41 }
42]
43
44const deleteRunnerValidator = [
45 param('runnerId').custom(isIdValid),
46
47 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
48 if (areValidationErrors(req, res, { tags })) return
49
50 const runner = await RunnerModel.load(forceNumber(req.params.runnerId))
51
52 if (!runner) {
53 return res.fail({
54 status: HttpStatusCode.NOT_FOUND_404,
55 message: 'Runner not found',
56 tags
57 })
58 }
59
60 res.locals.runner = runner
61
62 return next()
63 }
64]
65
66const getRunnerFromTokenValidator = [
67 body('runnerToken').custom(isRunnerTokenValid),
68
69 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
70 if (areValidationErrors(req, res, { tags })) return
71
72 const runner = await RunnerModel.loadByToken(req.body.runnerToken)
73
74 if (!runner) {
75 return res.fail({
76 status: HttpStatusCode.NOT_FOUND_404,
77 message: 'Unknown runner token',
78 type: ServerErrorCode.UNKNOWN_RUNNER_TOKEN,
79 tags
80 })
81 }
82
83 res.locals.runner = runner
84
85 return next()
86 }
87]
88
89// ---------------------------------------------------------------------------
90
91export {
92 registerRunnerValidator,
93 deleteRunnerValidator,
94 getRunnerFromTokenValidator
95}