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