diff options
Diffstat (limited to 'packages/server-commands/src/runners/runners-command.ts')
-rw-r--r-- | packages/server-commands/src/runners/runners-command.ts | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/packages/server-commands/src/runners/runners-command.ts b/packages/server-commands/src/runners/runners-command.ts new file mode 100644 index 000000000..376a1dff9 --- /dev/null +++ b/packages/server-commands/src/runners/runners-command.ts | |||
@@ -0,0 +1,85 @@ | |||
1 | import { pick } from '@peertube/peertube-core-utils' | ||
2 | import { | ||
3 | HttpStatusCode, | ||
4 | RegisterRunnerBody, | ||
5 | RegisterRunnerResult, | ||
6 | ResultList, | ||
7 | Runner, | ||
8 | UnregisterRunnerBody | ||
9 | } from '@peertube/peertube-models' | ||
10 | import { buildUUID } from '@peertube/peertube-node-utils' | ||
11 | import { unwrapBody } from '../requests/index.js' | ||
12 | import { AbstractCommand, OverrideCommandOptions } from '../shared/index.js' | ||
13 | |||
14 | export class RunnersCommand extends AbstractCommand { | ||
15 | |||
16 | list (options: OverrideCommandOptions & { | ||
17 | start?: number | ||
18 | count?: number | ||
19 | sort?: string | ||
20 | } = {}) { | ||
21 | const path = '/api/v1/runners' | ||
22 | |||
23 | return this.getRequestBody<ResultList<Runner>>({ | ||
24 | ...options, | ||
25 | |||
26 | path, | ||
27 | query: pick(options, [ 'start', 'count', 'sort' ]), | ||
28 | implicitToken: true, | ||
29 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
30 | }) | ||
31 | } | ||
32 | |||
33 | register (options: OverrideCommandOptions & RegisterRunnerBody) { | ||
34 | const path = '/api/v1/runners/register' | ||
35 | |||
36 | return unwrapBody<RegisterRunnerResult>(this.postBodyRequest({ | ||
37 | ...options, | ||
38 | |||
39 | path, | ||
40 | fields: pick(options, [ 'name', 'registrationToken', 'description' ]), | ||
41 | implicitToken: true, | ||
42 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
43 | })) | ||
44 | } | ||
45 | |||
46 | unregister (options: OverrideCommandOptions & UnregisterRunnerBody) { | ||
47 | const path = '/api/v1/runners/unregister' | ||
48 | |||
49 | return this.postBodyRequest({ | ||
50 | ...options, | ||
51 | |||
52 | path, | ||
53 | fields: pick(options, [ 'runnerToken' ]), | ||
54 | implicitToken: false, | ||
55 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
56 | }) | ||
57 | } | ||
58 | |||
59 | delete (options: OverrideCommandOptions & { | ||
60 | id: number | ||
61 | }) { | ||
62 | const path = '/api/v1/runners/' + options.id | ||
63 | |||
64 | return this.deleteRequest({ | ||
65 | ...options, | ||
66 | |||
67 | path, | ||
68 | implicitToken: true, | ||
69 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
70 | }) | ||
71 | } | ||
72 | |||
73 | // --------------------------------------------------------------------------- | ||
74 | |||
75 | async autoRegisterRunner () { | ||
76 | const { data } = await this.server.runnerRegistrationTokens.list({ sort: 'createdAt' }) | ||
77 | |||
78 | const { runnerToken } = await this.register({ | ||
79 | name: 'runner ' + buildUUID(), | ||
80 | registrationToken: data[0].registrationToken | ||
81 | }) | ||
82 | |||
83 | return runnerToken | ||
84 | } | ||
85 | } | ||