diff options
Diffstat (limited to 'packages/peertube-runner/shared/ipc/ipc-client.ts')
-rw-r--r-- | packages/peertube-runner/shared/ipc/ipc-client.ts | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/packages/peertube-runner/shared/ipc/ipc-client.ts b/packages/peertube-runner/shared/ipc/ipc-client.ts new file mode 100644 index 000000000..7f5951157 --- /dev/null +++ b/packages/peertube-runner/shared/ipc/ipc-client.ts | |||
@@ -0,0 +1,74 @@ | |||
1 | import CliTable3 from 'cli-table3' | ||
2 | import { ensureDir } from 'fs-extra' | ||
3 | import { Client as NetIPC } from 'net-ipc' | ||
4 | import { ConfigManager } from '../config-manager' | ||
5 | import { IPCReponse, IPCReponseData, IPCRequest } from './shared' | ||
6 | |||
7 | export class IPCClient { | ||
8 | private netIPC: NetIPC | ||
9 | |||
10 | async run () { | ||
11 | await ensureDir(ConfigManager.Instance.getSocketDirectory()) | ||
12 | |||
13 | const socketPath = ConfigManager.Instance.getSocketPath() | ||
14 | this.netIPC = new NetIPC({ path: socketPath }) | ||
15 | await this.netIPC.connect() | ||
16 | } | ||
17 | |||
18 | async askRegister (options: { | ||
19 | url: string | ||
20 | registrationToken: string | ||
21 | runnerName: string | ||
22 | runnerDescription?: string | ||
23 | }) { | ||
24 | const req: IPCRequest = { | ||
25 | type: 'register', | ||
26 | ...options | ||
27 | } | ||
28 | |||
29 | const { success, error } = await this.netIPC.request(req) as IPCReponse | ||
30 | |||
31 | if (success) console.log('PeerTube instance registered') | ||
32 | else console.error('Could not register PeerTube instance on runner server side', error) | ||
33 | } | ||
34 | |||
35 | async askUnregister (options: { | ||
36 | url: string | ||
37 | }) { | ||
38 | const req: IPCRequest = { | ||
39 | type: 'unregister', | ||
40 | ...options | ||
41 | } | ||
42 | |||
43 | const { success, error } = await this.netIPC.request(req) as IPCReponse | ||
44 | |||
45 | if (success) console.log('PeerTube instance unregistered') | ||
46 | else console.error('Could not unregister PeerTube instance on runner server side', error) | ||
47 | } | ||
48 | |||
49 | async askListRegistered () { | ||
50 | const req: IPCRequest = { | ||
51 | type: 'list-registered' | ||
52 | } | ||
53 | |||
54 | const { success, error, data } = await this.netIPC.request(req) as IPCReponse<IPCReponseData> | ||
55 | if (!success) { | ||
56 | console.error('Could not list registered PeerTube instances', error) | ||
57 | return | ||
58 | } | ||
59 | |||
60 | const table = new CliTable3({ | ||
61 | head: [ 'instance', 'runner name', 'runner description' ] | ||
62 | }) | ||
63 | |||
64 | for (const server of data.servers) { | ||
65 | table.push([ server.url, server.runnerName, server.runnerDescription ]) | ||
66 | } | ||
67 | |||
68 | console.log(table.toString()) | ||
69 | } | ||
70 | |||
71 | stop () { | ||
72 | this.netIPC.destroy() | ||
73 | } | ||
74 | } | ||