utils: move config backup methods to confighelper
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
parent
6749d3836e
commit
f6d8de2cee
|
@ -8,6 +8,10 @@ from __future__ import annotations
|
||||||
import configparser
|
import configparser
|
||||||
import os
|
import os
|
||||||
import hashlib
|
import hashlib
|
||||||
|
import shutil
|
||||||
|
import filecmp
|
||||||
|
import pathlib
|
||||||
|
import logging
|
||||||
from utils import SentinelClass
|
from utils import SentinelClass
|
||||||
from components.gpio import GpioOutputPin
|
from components.gpio import GpioOutputPin
|
||||||
from components.template import JinjaTemplate
|
from components.template import JinjaTemplate
|
||||||
|
@ -423,3 +427,22 @@ def get_configuration(server: Server,
|
||||||
raise ConfigError("No section [server] in config")
|
raise ConfigError("No section [server] in config")
|
||||||
orig_sections = config.sections()
|
orig_sections = config.sections()
|
||||||
return ConfigHelper(server, config, 'server', orig_sections)
|
return ConfigHelper(server, config, 'server', orig_sections)
|
||||||
|
|
||||||
|
def backup_config(cfg_path: str) -> None:
|
||||||
|
cfg = pathlib.Path(cfg_path).expanduser().resolve()
|
||||||
|
backup = cfg.parent.joinpath(f".{cfg.name}.bkp")
|
||||||
|
try:
|
||||||
|
if backup.exists() and filecmp.cmp(cfg, backup):
|
||||||
|
# Backup already exists and is current
|
||||||
|
return
|
||||||
|
shutil.copy2(cfg, backup)
|
||||||
|
logging.info(f"Backing up last working configuration to '{backup}'")
|
||||||
|
except Exception:
|
||||||
|
logging.exception("Failed to create a backup")
|
||||||
|
|
||||||
|
def find_config_backup(cfg_path: str) -> Optional[str]:
|
||||||
|
cfg = pathlib.Path(cfg_path).expanduser().resolve()
|
||||||
|
backup = cfg.parent.joinpath(f".{cfg.name}.bkp")
|
||||||
|
if backup.is_file():
|
||||||
|
return str(backup)
|
||||||
|
return None
|
||||||
|
|
|
@ -193,7 +193,8 @@ class Server:
|
||||||
|
|
||||||
if not self.warnings:
|
if not self.warnings:
|
||||||
cfg_file = self.app_args['config_file']
|
cfg_file = self.app_args['config_file']
|
||||||
await self.event_loop.run_in_thread(utils.backup_config, cfg_file)
|
await self.event_loop.run_in_thread(
|
||||||
|
confighelper.backup_config, cfg_file)
|
||||||
|
|
||||||
# Start HTTP Server
|
# Start HTTP Server
|
||||||
logging.info(
|
logging.info(
|
||||||
|
@ -855,7 +856,8 @@ def main() -> None:
|
||||||
"-n", "--nologfile", action='store_true',
|
"-n", "--nologfile", action='store_true',
|
||||||
help="disable logging to a file")
|
help="disable logging to a file")
|
||||||
cmd_line_args = parser.parse_args()
|
cmd_line_args = parser.parse_args()
|
||||||
app_args = {'config_file': cmd_line_args.configfile}
|
cfg_file = cmd_line_args.configfile
|
||||||
|
app_args = {'config_file': cfg_file}
|
||||||
|
|
||||||
# Setup Logging
|
# Setup Logging
|
||||||
version = utils.get_software_version()
|
version = utils.get_software_version()
|
||||||
|
@ -885,7 +887,7 @@ def main() -> None:
|
||||||
try:
|
try:
|
||||||
server = Server(app_args, file_logger, event_loop)
|
server = Server(app_args, file_logger, event_loop)
|
||||||
except confighelper.ConfigError as e:
|
except confighelper.ConfigError as e:
|
||||||
backup_cfg = utils.find_config_backup(app_args['config_file'])
|
backup_cfg = confighelper.find_config_backup(cfg_file)
|
||||||
if alt_config_loaded or backup_cfg is None:
|
if alt_config_loaded or backup_cfg is None:
|
||||||
logging.exception("Server Config Error")
|
logging.exception("Server Config Error")
|
||||||
estatus = 1
|
estatus = 1
|
||||||
|
@ -915,7 +917,7 @@ def main() -> None:
|
||||||
# Restore the original config and clear the warning
|
# Restore the original config and clear the warning
|
||||||
# before the server restarts
|
# before the server restarts
|
||||||
if alt_config_loaded:
|
if alt_config_loaded:
|
||||||
app_args['config_file'] = cmd_line_args.configfile
|
app_args['config_file'] = cfg_file
|
||||||
app_args.pop('config_warning', None)
|
app_args.pop('config_warning', None)
|
||||||
alt_config_loaded = False
|
alt_config_loaded = False
|
||||||
event_loop.close()
|
event_loop.close()
|
||||||
|
|
|
@ -18,8 +18,6 @@ import hashlib
|
||||||
import json
|
import json
|
||||||
import shlex
|
import shlex
|
||||||
import re
|
import re
|
||||||
import shutil
|
|
||||||
import filecmp
|
|
||||||
from queue import SimpleQueue as Queue
|
from queue import SimpleQueue as Queue
|
||||||
|
|
||||||
# Annotation imports
|
# Annotation imports
|
||||||
|
@ -232,22 +230,3 @@ def load_system_module(name: str) -> ModuleType:
|
||||||
else:
|
else:
|
||||||
raise ServerError(f"Unable to import module {name}")
|
raise ServerError(f"Unable to import module {name}")
|
||||||
return module
|
return module
|
||||||
|
|
||||||
def backup_config(cfg_path: str) -> None:
|
|
||||||
cfg = pathlib.Path(cfg_path).expanduser().resolve()
|
|
||||||
backup = cfg.parent.joinpath(f".{cfg.name}.bkp")
|
|
||||||
try:
|
|
||||||
if backup.exists() and filecmp.cmp(cfg, backup):
|
|
||||||
# Backup already exists and is current
|
|
||||||
return
|
|
||||||
shutil.copy2(cfg, backup)
|
|
||||||
logging.info(f"Backing up last working configuration to '{backup}'")
|
|
||||||
except Exception:
|
|
||||||
logging.exception("Failed to create a backup")
|
|
||||||
|
|
||||||
def find_config_backup(cfg_path: str) -> Optional[str]:
|
|
||||||
cfg = pathlib.Path(cfg_path).expanduser().resolve()
|
|
||||||
backup = cfg.parent.joinpath(f".{cfg.name}.bkp")
|
|
||||||
if backup.is_file():
|
|
||||||
return str(backup)
|
|
||||||
return None
|
|
||||||
|
|
Loading…
Reference in New Issue