]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - packages/peertube-runner/shared/ipc/ipc-client.ts
db8d7f1bf6c4e405ca48fae43cb24d2697a73dc8
[github/Chocobozzz/PeerTube.git] / packages / peertube-runner / shared / ipc / ipc-client.ts
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
15 this.netIPC = new NetIPC({ path: socketPath })
16
17 try {
18 await this.netIPC.connect()
19 } catch (err) {
20 if (err.code === 'ECONNREFUSED') {
21 throw new Error(
22 'This runner is not currently running in server mode on this system. ' +
23 'Please run it using the `server` command first (in another terminal for example) and then retry your command.'
24 )
25 }
26
27 throw err
28 }
29 }
30
31 async askRegister (options: {
32 url: string
33 registrationToken: string
34 runnerName: string
35 runnerDescription?: string
36 }) {
37 const req: IPCRequest = {
38 type: 'register',
39 ...options
40 }
41
42 const { success, error } = await this.netIPC.request(req) as IPCReponse
43
44 if (success) console.log('PeerTube instance registered')
45 else console.error('Could not register PeerTube instance on runner server side', error)
46 }
47
48 async askUnregister (options: {
49 url: string
50 }) {
51 const req: IPCRequest = {
52 type: 'unregister',
53 ...options
54 }
55
56 const { success, error } = await this.netIPC.request(req) as IPCReponse
57
58 if (success) console.log('PeerTube instance unregistered')
59 else console.error('Could not unregister PeerTube instance on runner server side', error)
60 }
61
62 async askListRegistered () {
63 const req: IPCRequest = {
64 type: 'list-registered'
65 }
66
67 const { success, error, data } = await this.netIPC.request(req) as IPCReponse<IPCReponseData>
68 if (!success) {
69 console.error('Could not list registered PeerTube instances', error)
70 return
71 }
72
73 const table = new CliTable3({
74 head: [ 'instance', 'runner name', 'runner description' ]
75 })
76
77 for (const server of data.servers) {
78 table.push([ server.url, server.runnerName, server.runnerDescription ])
79 }
80
81 console.log(table.toString())
82 }
83
84 stop () {
85 this.netIPC.destroy()
86 }
87 }