upgrade_server: Update possible server responses

This commit is contained in:
deajan 2024-09-17 14:56:19 +02:00
parent 8ad39e2379
commit 5919e476d7
6 changed files with 102 additions and 24 deletions

View file

@ -16,7 +16,6 @@ import tempfile
from logging import getLogger
from npbackup.upgrade_client.upgrader import auto_upgrader, _check_new_version
import npbackup.configuration
from npbackup.__version__ import __version__ as npbackup_version
from npbackup.path_helper import CURRENT_DIR
@ -120,7 +119,6 @@ def run_upgrade(full_config: dict, ignore_errors: bool = False) -> bool:
username=username,
password=password,
auto_upgrade_host_identity=auto_upgrade_host_identity,
installed_version=npbackup_version,
group=group,
ignore_errors=ignore_errors,
)

View file

@ -114,7 +114,6 @@ def auto_upgrader(
username: str,
password: str,
auto_upgrade_host_identity: str = None,
installed_version: str = None,
group: str = None,
ignore_errors: bool = False,
) -> bool:
@ -143,11 +142,12 @@ def auto_upgrader(
requestor.create_session(authenticated=True)
# We'll check python_arch instead of os_arch since we build 32 bit python executables for compat reasons
platform_and_arch = "{}/{}".format(get_os(), python_arch()).lower() + "-legacy" if IS_LEGACY else ""
platform_and_arch = "{}/{}".format(get_os(), python_arch()).lower()
if IS_LEGACY:
platform_and_arch += "-legacy"
try:
host_id = "{}/{}/{}".format(
auto_upgrade_host_identity, installed_version, group
auto_upgrade_host_identity, npbackup_version, group
)
id_record = "{}/{}".format(platform_and_arch, host_id)
except TypeError:

View file

@ -3,12 +3,12 @@
#
# This file is part of npbackup
__intname__ = "npbackup.upgrade_server.upgrade_server"
__appname__ = "npbackup_upgrade_server"
__author__ = "Orsiris de Jong"
__copyright__ = "Copyright (C) 2023-2024 NetInvent"
__license__ = "GPL-3.0-only"
__build__ = "2023020601"
__version__ = "1.4.0"
__build__ = "2024091701"
__version__ = "3.0.0"
import sys
@ -18,13 +18,16 @@ from argparse import ArgumentParser
from upgrade_server import configuration
from ofunctions.logger_utils import logger_get_logger
import upgrade_server.api
from upgrade_server.__debug__ import _DEBUG
logger = logger_get_logger(__appname__ + ".log", debug=_DEBUG)
if __name__ == "__main__":
_DEV = os.environ.get("_DEV", False)
parser = ArgumentParser(
prog="{} {} - {}".format(__intname__, __copyright__, __license__),
prog="{} {} - {}".format(__appname__, __copyright__, __license__),
description="""NPBackup Upgrade server""",
)
@ -51,6 +54,11 @@ if __name__ == "__main__":
else:
config_dict = configuration.load_config()
try:
logger = logger_get_logger(config_dict["http_server"]["log_file"], debug=_DEBUG)
except (AttributeError, KeyError, IndexError, TypeError):
pass
try:
listen = config_dict["http_server"]["listen"]
except (TypeError, KeyError):

View file

@ -0,0 +1,72 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# This file is part of npbackup
__intname__ = "npbackup.__debug__"
__author__ = "Orsiris de Jong"
__site__ = "https://www.netperfect.fr/npbackup"
__description__ = "NetPerfect Backup Client"
__copyright__ = "Copyright (C) 2023-2024 NetInvent"
__build__ = "2024081901"
import sys
import os
from typing import Callable
from functools import wraps
from logging import getLogger
import json
logger = getLogger()
# If set, debugging will be enabled by setting envrionment variable to __SPECIAL_DEBUG_STRING content
# Else, a simple true or false will suffice
__SPECIAL_DEBUG_STRING = ""
__debug_os_env = os.environ.get("_DEBUG", "False").strip("'\"")
if not __SPECIAL_DEBUG_STRING:
if "--debug" in sys.argv:
_DEBUG = True
sys.argv.pop(sys.argv.index("--debug"))
if not "_DEBUG" in globals():
_DEBUG = False
if __SPECIAL_DEBUG_STRING:
if __debug_os_env == __SPECIAL_DEBUG_STRING:
_DEBUG = True
elif __debug_os_env.capitalize() == "True":
_DEBUG = True
def catch_exceptions(fn: Callable):
"""
Catch any exception and log it so we don't loose exceptions in thread
"""
@wraps(fn)
def wrapper(self, *args, **kwargs):
try:
# pylint: disable=E1102 (not-callable)
return fn(self, *args, **kwargs)
except Exception as exc:
# pylint: disable=E1101 (no-member)
operation = fn.__name__
logger.error(f"General catcher: Function {operation} failed with: {exc}")
logger.error("Trace:", exc_info=True)
return None
return wrapper
def fmt_json(js: dict):
"""
Just a quick and dirty shorthand for pretty print which doesn't require pprint
to be loaded
"""
js = json.dumps(js, indent=4)
return js

View file

@ -7,11 +7,11 @@ __intname__ = "npbackup.upgrade_server.api"
__author__ = "Orsiris de Jong"
__copyright__ = "Copyright (C) 2023-2024 NetInvent"
__license__ = "GPL-3.0-only"
__build__ = "2023030301"
__build__ = "2024091701"
__appname__ = "npbackup.upgrader"
from typing import Literal, Optional
from typing import Optional, Union
import logging
import secrets
from argparse import ArgumentParser
@ -124,20 +124,20 @@ async def current_version(
)
@app.get("/upgrades/{platform}/{arch}", response_model=FileSend, status_code=200)
@app.get("/upgrades/{platform}/{arch}", response_model=Union[FileSend, dict], status_code=200)
@app.get(
"/upgrades/{platform}/{arch}/{auto_upgrade_host_identity}",
response_model=FileSend,
response_model=Union[FileSend, dict],
status_code=200,
)
@app.get(
"/upgrades/{platform}/{arch}/{auto_upgrade_host_identity}/{installed_version}",
response_model=FileSend,
response_model=Union[FileSend, dict],
status_code=200,
)
@app.get(
"/upgrades/{platform}/{arch}/{auto_upgrade_host_identity}/{installed_version}/{group}",
response_model=FileSend,
response_model=Union[FileSend, dict],
status_code=200,
)
async def upgrades(

View file

@ -86,7 +86,7 @@ def get_current_version() -> Optional[CurrentVersion]:
logger.error("Trace:", exc_info=True)
def get_file(file: FileGet, content: bool = False) -> Optional[Union[FileSend, bytes]]:
def get_file(file: FileGet, content: bool = False) -> Optional[Union[FileSend, bytes, dict]]:
possible_filename = "npbackup.zip"
path = os.path.join(
config_dict["upgrades"]["data_root"],
@ -97,13 +97,13 @@ def get_file(file: FileGet, content: bool = False) -> Optional[Union[FileSend, b
logger.info("Searching for %s", path)
if not os.path.isfile(path):
logger.info(f"No upgrade file found in {path}")
return FileSend(
arch=file.arch.value,
platform=file.platform.value,
sha256sum=None,
filename=None,
file_length=0,
)
return {
"arch": file.arch.value,
"platform": file.platform.value,
"sha256sum": None,
"filename": None,
"file_length": 0,
}
with open(path, "rb") as fh:
bytes = fh.read()