aboutsummaryrefslogtreecommitdiffhomepage
path: root/packages/peertube-runner/shared/config-manager.ts
blob: 548eeab8573c21d3672b621cd8543cbf59dabbe6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import envPaths from 'env-paths'
import { ensureDir, pathExists, readFile, remove, writeFile } from 'fs-extra'
import { merge } from 'lodash'
import { logger } from 'packages/peertube-runner/shared/logger'
import { dirname, join } from 'path'
import { parse, stringify } from '@iarna/toml'

const paths = envPaths('peertube-runner')

type Config = {
  jobs: {
    concurrency: number
  }

  ffmpeg: {
    threads: number
    nice: number
  }

  registeredInstances: {
    url: string
    runnerToken: string
    runnerName: string
    runnerDescription?: string
  }[]
}

export class ConfigManager {
  private static instance: ConfigManager

  private config: Config = {
    jobs: {
      concurrency: 2
    },
    ffmpeg: {
      threads: 2,
      nice: 20
    },
    registeredInstances: []
  }

  private id: string
  private configFilePath: string

  private constructor () {}

  init (id: string) {
    this.id = id
    this.configFilePath = join(this.getConfigDir(), 'config.toml')
  }

  async load () {
    logger.info(`Using ${this.configFilePath} as configuration file`)

    if (this.isTestInstance()) {
      logger.info('Removing configuration file as we are using the "test" id')
      await remove(this.configFilePath)
    }

    await ensureDir(dirname(this.configFilePath))

    if (!await pathExists(this.configFilePath)) {
      await this.save()
    }

    const file = await readFile(this.configFilePath, 'utf-8')

    this.config = merge(this.config, parse(file))
  }

  save () {
    return writeFile(this.configFilePath, stringify(this.config))
  }

  // ---------------------------------------------------------------------------

  async setRegisteredInstances (registeredInstances: {
    url: string
    runnerToken: string
    runnerName: string
    runnerDescription?: string
  }[]) {
    this.config.registeredInstances = registeredInstances

    await this.save()
  }

  // ---------------------------------------------------------------------------

  getConfig () {
    return this.deepFreeze(this.config)
  }

  // ---------------------------------------------------------------------------

  getTranscodingDirectory () {
    return join(paths.cache, this.id, 'transcoding')
  }

  getSocketDirectory () {
    return join(paths.data, this.id)
  }

  getSocketPath () {
    return join(this.getSocketDirectory(), 'peertube-runner.sock')
  }

  getConfigDir () {
    return join(paths.config, this.id)
  }

  // ---------------------------------------------------------------------------

  isTestInstance () {
    return typeof this.id === 'string' && this.id.match(/^test-\d$/)
  }

  // ---------------------------------------------------------------------------

  // Thanks: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze
  private deepFreeze <T extends object> (object: T) {
    const propNames = Reflect.ownKeys(object)

    // Freeze properties before freezing self
    for (const name of propNames) {
      const value = object[name]

      if ((value && typeof value === 'object') || typeof value === 'function') {
        this.deepFreeze(value)
      }
    }

    return Object.freeze({ ...object })
  }

  static get Instance () {
    return this.instance || (this.instance = new this())
  }
}