summaryrefslogtreecommitdiff
path: root/src/lib/config.js
blob: f3a9e611883610519fcaa223765a6ce2854373af (plain)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
const fs = require('fs')
const ini = require('ini')
const {isNumeric} = require('./util')

let config = {}

function readFile(file) {
    if (!fs.existsSync(file))
        throw new Error(`file ${file} not found`)

    return ini.parse(fs.readFileSync(file, 'utf-8'))
}

function processScheme(source, scheme) {
    const result = {}

    for (let key in scheme) {
        let opts = scheme[key]
        let ne = !(key in source) || !source[key]
        if (opts.required === true && ne)
            throw new Error(`'${key}' is not defined`)

        let value = source[key] ?? opts.default ?? null

        if (value !== null) {
            switch (opts.type) {
                case 'int':
                    if (!isNumeric(value))
                        throw new Error(`'${key}' must be an integer`)
                    value = parseInt(value, 10)
                    break

                case 'float':
                    if (!isNumeric(value))
                        throw new Error(`'${key}' must be a float`)
                    value = parseFloat(value)
                    break

                case 'object':
                    if (typeof value !== 'object')
                        throw new Error(`'${key}' must be an object`)
                    break

                case 'boolean':
                    if (typeof value === 'string') {
                        value = value.trim()
                        value = ['true', '1'].includes(value)
                    } else {
                        value = !!value
                    }
                    break

                case 'map':
                    value = {}
                    for (let key1 in source) {
                        if (key1.startsWith(`${key}.`))
                            value[key1.substring(key.length+1)] = source[key1]
                    }
                    break
            }
        }

        result[key] = value
    }

    return result
}

function parseWorkerConfig(file) {
    config = {}

    const raw = readFile(file)
    const scheme = {
        host:     {required: true},
        port:     {required: true, type: 'int'},
        password: {},
        always_allow_localhost: {type: 'boolean', default: false},

        master_host: {},
        master_port: {type: 'int', default: 0},
        master_reconnect_timeout: {type: 'int', default: 10},

        log_file:          {},
        log_level_file:    {default: 'warn'},
        log_level_console: {default: 'warn'},

        mysql_host:        {required: true},
        mysql_port:        {required: true, type: 'int'},
        mysql_user:        {required: true},
        mysql_password:    {required: true},
        mysql_database:    {required: true},
        mysql_table:       {required: true, default: 'jobs'},
        mysql_fetch_limit: {default: 100, type: 'int'},

        launcher:          {required: true},
        'launcher.cwd':    {default: process.cwd()},
        'launcher.env':    {type: 'map', default: {}},
        max_output_buffer: {default: 1024*1024, type: 'int'},
        targets:           {required: true, type: 'object'},
    }
    Object.assign(config, processScheme(raw, scheme))

    config.targets = {}
    for (let target in raw.targets) {
        if (target === 'null')
            throw new Error('word \'null\' is reserved, please don\'t use it as a target name')

        if (!isNumeric(raw.targets[target]))
            throw new Error(`value of target '${target}' must be a number`)

        let value = parseInt(raw.targets[target], 10)
        if (value < 1)
            throw new Error(`target '${target}' has invalid value`)

        config.targets[target] = value
    }
}

function parseMasterConfig(file) {
    config = {}

    const raw = readFile(file)
    const scheme = {
        host:     {required: true},
        port:     {required: true, type: 'int'},
        password: {},
        always_allow_localhost: {type: 'boolean', default: false},

        ping_interval:          {default: 30, type: 'int'},
        poke_throttle_interval: {default: 0.5, type: 'float'},

        log_file:          {},
        log_level_file:    {default: 'warn'},
        log_level_console: {default: 'warn'},
    }
    Object.assign(config, processScheme(raw, scheme))
}

/**
 * @param {string} file
 */
function parseJobctlConfig(file) {
    config = {}
    const raw = readFile(file)

    Object.assign(config, processScheme(raw, {
        master: {type: 'boolean'},
        password: {},
        log_level: {default: 'warn'},
    }))

    // if (inputOptions.master)
    //     config.master = inputOptions.master
    Object.assign(config, processScheme(raw, {
        host:     {default: '127.0.0.1'},
        port:     {/*default: config.master ? 7081 : 7080,*/ type: 'int'}
    }))

    // for (let key of ['log_level', 'host', 'port']) {
    //     if (inputOptions[key])
    //         config[key] = inputOptions[key]
    // }

    // console.log('parseJobctlConfig [2]', config)
}

/**
 * @param {string|null} key
 * @return {string|number|object}
 */
function get(key = null) {
    if (key === null)
        return config

    return config[key] || null
}

/**
 * @param {object|string} key
 * @param value
 */
function set(key, value) {
    if (!config)
        config = {}
    if (typeof key === 'object') {
        Object.assign(config, key)
    } else {
        config[key] = value
    }
}

module.exports = {
    parseWorkerConfig,
    parseMasterConfig,
    parseJobctlConfig,
    get,
    set,
}