diff options
Diffstat (limited to 'apps/peertube-runner/src/peertube-runner.ts')
-rw-r--r-- | apps/peertube-runner/src/peertube-runner.ts | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/apps/peertube-runner/src/peertube-runner.ts b/apps/peertube-runner/src/peertube-runner.ts new file mode 100644 index 000000000..67ca0e0ac --- /dev/null +++ b/apps/peertube-runner/src/peertube-runner.ts | |||
@@ -0,0 +1,91 @@ | |||
1 | #!/usr/bin/env node | ||
2 | |||
3 | import { Command, InvalidArgumentError } from '@commander-js/extra-typings' | ||
4 | import { listRegistered, registerRunner, unregisterRunner } from './register/index.js' | ||
5 | import { RunnerServer } from './server/index.js' | ||
6 | import { ConfigManager, logger } from './shared/index.js' | ||
7 | |||
8 | const program = new Command() | ||
9 | .version(process.env.PACKAGE_VERSION) | ||
10 | .option( | ||
11 | '--id <id>', | ||
12 | 'Runner server id, so you can run multiple PeerTube server runners with different configurations on the same machine', | ||
13 | 'default' | ||
14 | ) | ||
15 | .option('--verbose', 'Run in verbose mode') | ||
16 | .hook('preAction', thisCommand => { | ||
17 | const options = thisCommand.opts() | ||
18 | |||
19 | ConfigManager.Instance.init(options.id) | ||
20 | |||
21 | if (options.verbose === true) { | ||
22 | logger.level = 'debug' | ||
23 | } | ||
24 | }) | ||
25 | |||
26 | program.command('server') | ||
27 | .description('Run in server mode, to execute remote jobs of registered PeerTube instances') | ||
28 | .action(async () => { | ||
29 | try { | ||
30 | await RunnerServer.Instance.run() | ||
31 | } catch (err) { | ||
32 | logger.error(err, 'Cannot run PeerTube runner as server mode') | ||
33 | process.exit(-1) | ||
34 | } | ||
35 | }) | ||
36 | |||
37 | program.command('register') | ||
38 | .description('Register a new PeerTube instance to process runner jobs') | ||
39 | .requiredOption('--url <url>', 'PeerTube instance URL', parseUrl) | ||
40 | .requiredOption('--registration-token <token>', 'Runner registration token (can be found in PeerTube instance administration') | ||
41 | .requiredOption('--runner-name <name>', 'Runner name') | ||
42 | .option('--runner-description <description>', 'Runner description') | ||
43 | .action(async options => { | ||
44 | try { | ||
45 | await registerRunner(options) | ||
46 | } catch (err) { | ||
47 | console.error('Cannot register this PeerTube runner.') | ||
48 | console.error(err) | ||
49 | process.exit(-1) | ||
50 | } | ||
51 | }) | ||
52 | |||
53 | program.command('unregister') | ||
54 | .description('Unregister the runner from PeerTube instance') | ||
55 | .requiredOption('--url <url>', 'PeerTube instance URL', parseUrl) | ||
56 | .requiredOption('--runner-name <name>', 'Runner name') | ||
57 | .action(async options => { | ||
58 | try { | ||
59 | await unregisterRunner(options) | ||
60 | } catch (err) { | ||
61 | console.error('Cannot unregister this PeerTube runner.') | ||
62 | console.error(err) | ||
63 | process.exit(-1) | ||
64 | } | ||
65 | }) | ||
66 | |||
67 | program.command('list-registered') | ||
68 | .description('List registered PeerTube instances') | ||
69 | .action(async () => { | ||
70 | try { | ||
71 | await listRegistered() | ||
72 | } catch (err) { | ||
73 | console.error('Cannot list registered PeerTube instances.') | ||
74 | console.error(err) | ||
75 | process.exit(-1) | ||
76 | } | ||
77 | }) | ||
78 | |||
79 | program.parse() | ||
80 | |||
81 | // --------------------------------------------------------------------------- | ||
82 | // Private | ||
83 | // --------------------------------------------------------------------------- | ||
84 | |||
85 | function parseUrl (url: string) { | ||
86 | if (url.startsWith('http://') !== true && url.startsWith('https://') !== true) { | ||
87 | throw new InvalidArgumentError('URL should start with a http:// or https://') | ||
88 | } | ||
89 | |||
90 | return url | ||
91 | } | ||