2021-12-06 13:47:11 +01:00
|
|
|
# Copyright 2021 Mattia Giambirtone & All Contributors
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2021-12-16 11:24:00 +01:00
|
|
|
import tables
|
2021-12-06 13:47:11 +01:00
|
|
|
import parsecfg
|
2021-12-16 11:24:00 +01:00
|
|
|
import strutils
|
|
|
|
import strformat
|
2021-12-06 13:47:11 +01:00
|
|
|
|
|
|
|
|
2022-01-09 22:46:25 +01:00
|
|
|
import logging
|
|
|
|
import ../core/fs
|
2021-12-06 13:47:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
type
|
2021-12-16 11:24:00 +01:00
|
|
|
NimDConfig* = ref object
|
2021-12-06 13:47:11 +01:00
|
|
|
## Configuration object
|
|
|
|
logLevel*: LogLevel
|
2022-01-09 22:46:25 +01:00
|
|
|
logFile*: string
|
|
|
|
workers*: int
|
2021-12-16 11:24:00 +01:00
|
|
|
directories*: seq[Directory]
|
|
|
|
symlinks*: seq[Symlink]
|
|
|
|
filesystems*: seq[Filesystem]
|
|
|
|
restartDelay*: int
|
2022-01-09 22:46:25 +01:00
|
|
|
sigtermDelay*: int
|
2021-12-16 11:24:00 +01:00
|
|
|
autoMount*: bool
|
|
|
|
autoUnmount*: bool
|
|
|
|
fstab*: string
|
|
|
|
sock*: string
|
2022-01-09 22:46:25 +01:00
|
|
|
onDependencyConflict*: string
|
|
|
|
setHostname*: bool
|
2021-12-16 11:24:00 +01:00
|
|
|
|
|
|
|
|
2022-01-09 22:46:25 +01:00
|
|
|
const defaultSections* = ["Misc", "Logging", "Filesystem"]
|
|
|
|
const defaultConfig* = {
|
2021-12-16 11:24:00 +01:00
|
|
|
"Logging": {
|
2022-01-09 22:46:25 +01:00
|
|
|
"logFile": "/var/log/nimd",
|
2021-12-16 11:24:00 +01:00
|
|
|
"level": "info"
|
|
|
|
}.toTable(),
|
|
|
|
"Misc": {
|
|
|
|
"controlSocket": "/var/run/nimd.sock",
|
2022-01-09 22:46:25 +01:00
|
|
|
"onDependencyConflict": "skip",
|
|
|
|
"setHostname": "true",
|
|
|
|
"workers": "1",
|
|
|
|
"sigtermDelay": "90",
|
|
|
|
"restartDelay": "30"
|
2021-12-16 11:24:00 +01:00
|
|
|
}.toTable(),
|
|
|
|
"Filesystem": {
|
|
|
|
"autoMount": "true",
|
|
|
|
"autoUnmount": "true",
|
|
|
|
"fstabPath": "/etc/fstab",
|
|
|
|
"createDirs": "",
|
|
|
|
"createSymlinks": "",
|
|
|
|
"virtualDisks": ""
|
|
|
|
}.toTable()
|
|
|
|
}.toTable()
|
2022-01-09 22:46:25 +01:00
|
|
|
|
|
|
|
const levels = {
|
|
|
|
"trace": Trace,
|
|
|
|
"debug": Debug,
|
|
|
|
"info": Info,
|
|
|
|
"warning": Warning,
|
|
|
|
"error": Error,
|
|
|
|
"critical": Critical,
|
|
|
|
"fatal": Fatal
|
|
|
|
}.toTable()
|
2021-12-16 11:24:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
proc parseConfig*(logger: Logger, file: string = "/etc/nimd/nimd.conf"): NimDConfig =
|
|
|
|
## Parses NimD's configuration file
|
|
|
|
## and returns a configuration object
|
|
|
|
|
|
|
|
# Yes this code is far from perfect, but I hate paesing
|
|
|
|
# config files :(
|
|
|
|
logger.debug(&"Reading config file at {file}")
|
|
|
|
let cfgObject = loadConfig(file)
|
|
|
|
var data = newTable[string, TableRef[string, string]]()
|
|
|
|
var existingSections: seq[string] = @[]
|
|
|
|
var directories: seq[Directory] = @[]
|
|
|
|
var filesystems: seq[Filesystem] = @[]
|
|
|
|
var symlinks: seq[Symlink] = @[]
|
|
|
|
var temp: seq[string] = @[]
|
|
|
|
for section in cfgObject.sections():
|
|
|
|
existingSections.add(section)
|
|
|
|
for section in defaultSections:
|
|
|
|
logger.debug(&"Parsing section '{section}'")
|
|
|
|
if section notin existingSections:
|
|
|
|
logger.warning(&"Missing section '{section}' from config file, falling back to defaults")
|
|
|
|
for (value, key) in defaultConfig[section].pairs():
|
|
|
|
data[section][value] = key
|
|
|
|
elif section notin defaultSections:
|
|
|
|
logger.warning(&"Unknown section '{section}' found in config file, skipping it")
|
|
|
|
else:
|
2022-01-09 22:46:25 +01:00
|
|
|
if not data.hasKey(section):
|
|
|
|
data[section] = newTable[string, string]()
|
2021-12-16 11:24:00 +01:00
|
|
|
for key in defaultConfig[section].keys():
|
|
|
|
data[section][key] = cfgObject.getSectionValue(section, key, defaultConfig[section][key])
|
|
|
|
for dirInfo in data["Filesystem"]["createDirs"].split(","):
|
|
|
|
temp = dirInfo.split(":")
|
2022-01-09 22:46:25 +01:00
|
|
|
directories.add(newDirectory(temp[0].strip(), uint64(parseInt(temp[1].strip()))))
|
2021-12-16 11:24:00 +01:00
|
|
|
for symInfo in data["Filesystem"]["createSymlinks"].split(","):
|
|
|
|
temp = symInfo.split(":")
|
|
|
|
symlinks.add(newSymlink(temp[0], temp[1]))
|
|
|
|
if data["Logging"]["level"] notin levels:
|
2022-01-09 22:46:25 +01:00
|
|
|
logger.warning(&"""Unknown logging level '{data["Logging"]["level"]}', defaulting to '{defaultConfig["Logging"]["level"]}'""")
|
2021-12-16 11:24:00 +01:00
|
|
|
data["Logging"]["level"] = defaultConfig["Logging"]["level"]
|
|
|
|
result = NimDConfig(logLevel: levels[data["Logging"]["level"]],
|
2022-01-09 22:46:25 +01:00
|
|
|
logFile: data["Logging"]["logFile"],
|
|
|
|
filesystems: filesystems,
|
|
|
|
symlinks: symlinks,
|
|
|
|
directories: directories,
|
|
|
|
autoMount: parseBool(data["Filesystem"]["autoMount"].toLowerAscii()),
|
|
|
|
autoUnmount: parseBool(data["Filesystem"]["autoUnmount"].toLowerAscii()),
|
|
|
|
fstab: data["Filesystem"]["fstabPath"],
|
|
|
|
sock: data["Misc"]["controlSocket"],
|
|
|
|
onDependencyConflict: data["Misc"]["onDependencyConflict"].toLowerAscii(),
|
|
|
|
restartDelay: parseInt(data["Misc"]["restartDelay"]),
|
|
|
|
sigtermDelay: parseInt(data["Misc"]["sigtermDelay"]),
|
|
|
|
workers: parseInt(data["Misc"]["workers"]),
|
|
|
|
setHostname: parseBool(data["Misc"]["setHostname"])
|
|
|
|
)
|