diff options
author | Chocobozzz <me@florianbigard.com> | 2023-04-21 15:05:27 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2023-05-09 08:57:34 +0200 |
commit | 1772b383de490cf406fe93ef3aa3a941f6db513c (patch) | |
tree | 7cecc404c8d71951c22079e9bf5180095981b7f9 /packages/peertube-runner/shared/config-manager.ts | |
parent | 118626c8752bee7b05c4e0b668852e1aba2416f1 (diff) | |
download | PeerTube-1772b383de490cf406fe93ef3aa3a941f6db513c.tar.gz PeerTube-1772b383de490cf406fe93ef3aa3a941f6db513c.tar.zst PeerTube-1772b383de490cf406fe93ef3aa3a941f6db513c.zip |
Add peertube runner cli
Diffstat (limited to 'packages/peertube-runner/shared/config-manager.ts')
-rw-r--r-- | packages/peertube-runner/shared/config-manager.ts | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/packages/peertube-runner/shared/config-manager.ts b/packages/peertube-runner/shared/config-manager.ts new file mode 100644 index 000000000..352bae1fa --- /dev/null +++ b/packages/peertube-runner/shared/config-manager.ts | |||
@@ -0,0 +1,139 @@ | |||
1 | import envPaths from 'env-paths' | ||
2 | import { ensureDir, pathExists, readFile, remove, writeFile } from 'fs-extra' | ||
3 | import { merge } from 'lodash' | ||
4 | import { logger } from 'packages/peertube-runner/shared/logger' | ||
5 | import { dirname, join } from 'path' | ||
6 | import { parse, stringify } from '@iarna/toml' | ||
7 | |||
8 | const paths = envPaths('peertube-runner') | ||
9 | |||
10 | type Config = { | ||
11 | jobs: { | ||
12 | concurrency: number | ||
13 | } | ||
14 | |||
15 | ffmpeg: { | ||
16 | threads: number | ||
17 | nice: number | ||
18 | } | ||
19 | |||
20 | registeredInstances: { | ||
21 | url: string | ||
22 | runnerToken: string | ||
23 | runnerName: string | ||
24 | runnerDescription?: string | ||
25 | }[] | ||
26 | } | ||
27 | |||
28 | export class ConfigManager { | ||
29 | private static instance: ConfigManager | ||
30 | |||
31 | private config: Config = { | ||
32 | jobs: { | ||
33 | concurrency: 2 | ||
34 | }, | ||
35 | ffmpeg: { | ||
36 | threads: 2, | ||
37 | nice: 20 | ||
38 | }, | ||
39 | registeredInstances: [] | ||
40 | } | ||
41 | |||
42 | private id: string | ||
43 | private configFilePath: string | ||
44 | |||
45 | private constructor () {} | ||
46 | |||
47 | init (id: string) { | ||
48 | this.id = id | ||
49 | this.configFilePath = join(this.getConfigDir(), 'config.toml') | ||
50 | } | ||
51 | |||
52 | async load () { | ||
53 | logger.info(`Using ${this.configFilePath} as configuration file`) | ||
54 | |||
55 | if (this.isTestInstance()) { | ||
56 | logger.info('Removing configuration file as we are using the "test" id') | ||
57 | await remove(this.configFilePath) | ||
58 | } | ||
59 | |||
60 | await ensureDir(dirname(this.configFilePath)) | ||
61 | |||
62 | if (!await pathExists(this.configFilePath)) { | ||
63 | await this.save() | ||
64 | } | ||
65 | |||
66 | const file = await readFile(this.configFilePath, 'utf-8') | ||
67 | |||
68 | this.config = merge(this.config, parse(file)) | ||
69 | } | ||
70 | |||
71 | save () { | ||
72 | return writeFile(this.configFilePath, stringify(this.config)) | ||
73 | } | ||
74 | |||
75 | // --------------------------------------------------------------------------- | ||
76 | |||
77 | async setRegisteredInstances (registeredInstances: { | ||
78 | url: string | ||
79 | runnerToken: string | ||
80 | runnerName: string | ||
81 | runnerDescription?: string | ||
82 | }[]) { | ||
83 | this.config.registeredInstances = registeredInstances | ||
84 | |||
85 | await this.save() | ||
86 | } | ||
87 | |||
88 | // --------------------------------------------------------------------------- | ||
89 | |||
90 | getConfig () { | ||
91 | return this.deepFreeze(this.config) | ||
92 | } | ||
93 | |||
94 | // --------------------------------------------------------------------------- | ||
95 | |||
96 | getTranscodingDirectory () { | ||
97 | return join(paths.cache, this.id, 'transcoding') | ||
98 | } | ||
99 | |||
100 | getSocketDirectory () { | ||
101 | return join(paths.data, this.id) | ||
102 | } | ||
103 | |||
104 | getSocketPath () { | ||
105 | return join(this.getSocketDirectory(), 'peertube-runner.sock') | ||
106 | } | ||
107 | |||
108 | getConfigDir () { | ||
109 | return join(paths.config, this.id) | ||
110 | } | ||
111 | |||
112 | // --------------------------------------------------------------------------- | ||
113 | |||
114 | isTestInstance () { | ||
115 | return this.id === 'test' | ||
116 | } | ||
117 | |||
118 | // --------------------------------------------------------------------------- | ||
119 | |||
120 | // Thanks: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze | ||
121 | private deepFreeze <T extends object> (object: T) { | ||
122 | const propNames = Reflect.ownKeys(object) | ||
123 | |||
124 | // Freeze properties before freezing self | ||
125 | for (const name of propNames) { | ||
126 | const value = object[name] | ||
127 | |||
128 | if ((value && typeof value === 'object') || typeof value === 'function') { | ||
129 | this.deepFreeze(value) | ||
130 | } | ||
131 | } | ||
132 | |||
133 | return Object.freeze({ ...object }) | ||
134 | } | ||
135 | |||
136 | static get Instance () { | ||
137 | return this.instance || (this.instance = new this()) | ||
138 | } | ||
139 | } | ||