WIP: upgrade_server: Add /version endpoint

This commit is contained in:
Orsiris de Jong 2023-01-31 23:42:24 +01:00
parent 86b177f2c9
commit 3851d1d77a
4 changed files with 56 additions and 1 deletions

View file

@ -8,7 +8,9 @@ http_server:
upgrades:
# Build dir should contain the following structure
# /platform/arch
# /VERSION
# VERSION is a file containing a single line with the currently built NPBackup version, example: 2.2.0
# /{platform}/{arch}/{binary}
# Current platforms are 'windows', 'linux'
# Current arches are 'x64', 'x86'
# In each folder there should be a npbackup or npbackup.exe binary depending on the platform

View file

@ -16,6 +16,7 @@ from fastapi import FastAPI, HTTPException, Response, Depends, status
from fastapi.security import HTTPBasic, HTTPBasicCredentials
from fastapi_offline import FastAPIOffline
from upgrade_server.models.files import FileGet, FileSend, Platform, Arch
from upgrade_server.models.oper import CurrentVersion
import upgrade_server.crud as crud
import upgrade_server.configuration as configuration
@ -61,6 +62,26 @@ async def api_root(auth = Depends(get_current_username)):
}
@app.get("/current_version", response_model=CurrentVersion, status_code=200)
async def current_version(auth = Depends(get_current_username)):
try:
result = crud.get_current_version()
if not result:
raise HTTPException(
status_code=404,
detail="Not found"
)
return result
except HTTPException:
raise
except Exception as exc:
logger.debug("Cannot get file: {}".format(exc), exc_info=True)
raise HTTPException(
status_code=400,
detail="Cannot get file: {}".format(exc),
)
@app.get("/upgrades/{platform}/{arch}", response_model=FileSend, status_code=200)
async def upgrades(platform: Platform, arch: Arch, auth = Depends(get_current_username)):

View file

@ -15,6 +15,7 @@ from typing import Optional, Union
from logging import getLogger
import hashlib
from upgrade_server.models.files import FileGet, FileSend
from upgrade_server.models.oper import CurrentVersion
import upgrade_server.configuration as configuration
@ -36,6 +37,20 @@ def is_enabled() -> bool:
return not os.path.isfile("DISABLED")
def get_current_version() -> Optional[CurrentVersion]:
try:
path = os.path.join(config_dict['upgrades']['data_root'], 'VERSION')
print(path)
if os.path.isfile(path):
with open(path, 'r') as fh:
ver =fh.readline()
return(CurrentVersion(version=ver))
except OSError:
logger.error("Cannot get current version")
except Exception:
logger.error("Version seems to be bogus in VERSION file")
def get_file(file: FileGet, content: bool = False) -> Optional[Union[FileSend, bytes]]:
possible_filename = 'npbackup{}'.format(
'.exe' if file.platform.value == 'windows' else ''

View file

@ -0,0 +1,17 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# This file is part of npbackup
__intname__ = "npbackup.upgrade_server.models.oper"
__author__ = "Orsiris de Jong"
__copyright__ = "Copyright (C) 2023 NetInvent"
__license__ = "GPL-3.0-only"
__build__ = "202303101"
from pydantic import BaseModel
class CurrentVersion(BaseModel):
version: str