aboutsummaryrefslogtreecommitdiffhomepage
path: root/packages/peertube-runner/shared/ipc/ipc-client.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-04-21 15:05:27 +0200
committerChocobozzz <chocobozzz@cpy.re>2023-05-09 08:57:34 +0200
commit1772b383de490cf406fe93ef3aa3a941f6db513c (patch)
tree7cecc404c8d71951c22079e9bf5180095981b7f9 /packages/peertube-runner/shared/ipc/ipc-client.ts
parent118626c8752bee7b05c4e0b668852e1aba2416f1 (diff)
downloadPeerTube-1772b383de490cf406fe93ef3aa3a941f6db513c.tar.gz
PeerTube-1772b383de490cf406fe93ef3aa3a941f6db513c.tar.zst
PeerTube-1772b383de490cf406fe93ef3aa3a941f6db513c.zip
Add peertube runner cli
Diffstat (limited to 'packages/peertube-runner/shared/ipc/ipc-client.ts')
-rw-r--r--packages/peertube-runner/shared/ipc/ipc-client.ts74
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 @@
1import CliTable3 from 'cli-table3'
2import { ensureDir } from 'fs-extra'
3import { Client as NetIPC } from 'net-ipc'
4import { ConfigManager } from '../config-manager'
5import { IPCReponse, IPCReponseData, IPCRequest } from './shared'
6
7export 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}