mirror of
https://github.com/arcan1s/ahriman.git
synced 2026-04-07 11:03:37 +00:00
Compare commits
15 Commits
5e090cebdb
...
feature/ss
| Author | SHA1 | Date | |
|---|---|---|---|
| a170e43073 | |||
| 190b6665de | |||
| 71f9044f27 | |||
| a69e3338b1 | |||
| 96ebb3793d | |||
| 3265bb913f | |||
| af8e2c9e9b | |||
| 1c312bb528 | |||
| e39194e9f6 | |||
| 21cc029c18 | |||
| 40671b99d5 | |||
| 3ad2c494af | |||
| 34014d1cdd | |||
| 93ed2b864b | |||
| cca931ccd0 |
@@ -1,7 +1,7 @@
|
||||
version: 2
|
||||
|
||||
build:
|
||||
os: ubuntu-20.04
|
||||
os: ubuntu-lts-latest
|
||||
tools:
|
||||
python: "3.12"
|
||||
apt_packages:
|
||||
|
||||
@@ -29,17 +29,16 @@ RUN pacman -S --noconfirm --asdeps \
|
||||
python-filelock \
|
||||
python-inflection \
|
||||
python-pyelftools \
|
||||
python-requests \
|
||||
&& \
|
||||
pacman -S --noconfirm --asdeps \
|
||||
python-requests
|
||||
RUN pacman -S --noconfirm --asdeps \
|
||||
base-devel \
|
||||
python-build \
|
||||
python-flit \
|
||||
python-installer \
|
||||
python-setuptools \
|
||||
python-tox \
|
||||
python-wheel \
|
||||
&& \
|
||||
pacman -S --noconfirm --asdeps \
|
||||
python-wheel
|
||||
RUN pacman -S --noconfirm --asdeps \
|
||||
git \
|
||||
python-aiohttp \
|
||||
python-aiohttp-openmetrics \
|
||||
@@ -48,9 +47,8 @@ RUN pacman -S --noconfirm --asdeps \
|
||||
python-cryptography \
|
||||
python-jinja \
|
||||
python-systemd \
|
||||
rsync \
|
||||
&& \
|
||||
runuser -u build -- install-aur-package \
|
||||
rsync
|
||||
RUN runuser -u build -- install-aur-package \
|
||||
python-aioauth-client \
|
||||
python-sphinx-typlog-theme \
|
||||
python-webargs \
|
||||
@@ -59,6 +57,7 @@ RUN pacman -S --noconfirm --asdeps \
|
||||
python-aiohttp-jinja2 \
|
||||
python-aiohttp-session \
|
||||
python-aiohttp-security \
|
||||
python-aiohttp-sse-git \
|
||||
python-requests-unixsocket2
|
||||
|
||||
# install ahriman
|
||||
|
||||
@@ -12,6 +12,14 @@ ahriman.core.status.client module
|
||||
:no-undoc-members:
|
||||
:show-inheritance:
|
||||
|
||||
ahriman.core.status.event\_bus module
|
||||
-------------------------------------
|
||||
|
||||
.. automodule:: ahriman.core.status.event_bus
|
||||
:members:
|
||||
:no-undoc-members:
|
||||
:show-inheritance:
|
||||
|
||||
ahriman.core.status.local\_client module
|
||||
----------------------------------------
|
||||
|
||||
|
||||
@@ -12,6 +12,14 @@ ahriman.web.middlewares.auth\_handler module
|
||||
:no-undoc-members:
|
||||
:show-inheritance:
|
||||
|
||||
ahriman.web.middlewares.etag\_handler module
|
||||
--------------------------------------------
|
||||
|
||||
.. automodule:: ahriman.web.middlewares.etag_handler
|
||||
:members:
|
||||
:no-undoc-members:
|
||||
:show-inheritance:
|
||||
|
||||
ahriman.web.middlewares.exception\_handler module
|
||||
-------------------------------------------------
|
||||
|
||||
|
||||
@@ -92,6 +92,14 @@ ahriman.web.schemas.error\_schema module
|
||||
:no-undoc-members:
|
||||
:show-inheritance:
|
||||
|
||||
ahriman.web.schemas.event\_bus\_filter\_schema module
|
||||
-----------------------------------------------------
|
||||
|
||||
.. automodule:: ahriman.web.schemas.event_bus_filter_schema
|
||||
:members:
|
||||
:no-undoc-members:
|
||||
:show-inheritance:
|
||||
|
||||
ahriman.web.schemas.event\_schema module
|
||||
----------------------------------------
|
||||
|
||||
@@ -356,6 +364,14 @@ ahriman.web.schemas.search\_schema module
|
||||
:no-undoc-members:
|
||||
:show-inheritance:
|
||||
|
||||
ahriman.web.schemas.sse\_schema module
|
||||
--------------------------------------
|
||||
|
||||
.. automodule:: ahriman.web.schemas.sse_schema
|
||||
:members:
|
||||
:no-undoc-members:
|
||||
:show-inheritance:
|
||||
|
||||
ahriman.web.schemas.status\_schema module
|
||||
-----------------------------------------
|
||||
|
||||
|
||||
@@ -4,6 +4,14 @@ ahriman.web.views.v1.auditlog package
|
||||
Submodules
|
||||
----------
|
||||
|
||||
ahriman.web.views.v1.auditlog.event\_bus module
|
||||
-----------------------------------------------
|
||||
|
||||
.. automodule:: ahriman.web.views.v1.auditlog.event_bus
|
||||
:members:
|
||||
:no-undoc-members:
|
||||
:show-inheritance:
|
||||
|
||||
ahriman.web.views.v1.auditlog.events module
|
||||
-------------------------------------------
|
||||
|
||||
|
||||
@@ -235,6 +235,18 @@ Remove packages
|
||||
|
||||
This flow removes package from filesystem, updates repository database and also runs synchronization and reporting methods.
|
||||
|
||||
Rollback packages
|
||||
^^^^^^^^^^^^^^^^^
|
||||
|
||||
This flow restores a package to a previously built version:
|
||||
|
||||
#. Load the current package definition from the repository database.
|
||||
#. Replace its version with the requested rollback target.
|
||||
#. Search the archive directory for built artifacts (packages and signatures) matching the target version.
|
||||
#. Add the found artifacts to the repository via the same path as ``package-add`` with ``PackageSource.Archive``.
|
||||
#. Trigger an immediate update to process the added packages.
|
||||
#. If ``--hold`` is enabled (the default), mark the package as held in the database to prevent automatic updates from overriding the rollback.
|
||||
|
||||
Check outdated packages
|
||||
^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
|
||||
@@ -180,10 +180,15 @@ Web server settings. This feature requires ``aiohttp`` libraries to be installed
|
||||
|
||||
* ``address`` - optional address in form ``proto://host:port`` (``port`` can be omitted in case of default ``proto`` ports), will be used instead of ``http://{host}:{port}`` in case if set, string, optional. This option is required in case if ``OAuth`` provider is used.
|
||||
* ``autorefresh_intervals`` - enable page auto refresh options, space separated list of integers, optional. The first defined interval will be used as default. If no intervals set, the auto refresh buttons will be disabled. If first element of the list equals ``0``, auto refresh will be disabled by default.
|
||||
* ``cors_allow_headers`` - allowed CORS headers, space separated list of strings, optional.
|
||||
* ``cors_allow_methods`` - allowed CORS methods, space separated list of strings, optional.
|
||||
* ``cors_allow_origins`` - allowed CORS origins, space separated list of strings, optional, default ``*``.
|
||||
* ``cors_expose_headers`` - exposed CORS headers, space separated list of strings, optional.
|
||||
* ``enable_archive_upload`` - allow to upload packages via HTTP (i.e. call of ``/api/v1/service/upload`` uri), boolean, optional, default ``no``.
|
||||
* ``host`` - host to bind, string, optional.
|
||||
* ``index_url`` - full URL of the repository index page, string, optional.
|
||||
* ``max_body_size`` - max body size in bytes to be validated for archive upload, integer, optional. If not set, validation will be disabled.
|
||||
* ``max_queue_size`` - max queue size for server sent event streams, integer, optional, default ``0``. If set to ``0``, queue is unlimited.
|
||||
* ``port`` - port to bind, integer, optional.
|
||||
* ``service_only`` - disable status routes (including logs), boolean, optional, default ``no``.
|
||||
* ``static_path`` - path to directory with static files, string, required.
|
||||
@@ -191,7 +196,7 @@ Web server settings. This feature requires ``aiohttp`` libraries to be installed
|
||||
* ``templates`` - path to templates directories, space separated list of paths, required.
|
||||
* ``unix_socket`` - path to the listening unix socket, string, optional. If set, server will create the socket on the specified address which can (and will) be used by application. Note, that unlike usual host/port configuration, unix socket allows to perform requests without authorization.
|
||||
* ``unix_socket_unsafe`` - set unsafe (o+w) permissions to unix socket, boolean, optional, default ``yes``. This option is enabled by default, because it is supposed that unix socket is created in safe environment (only web service is supposed to be used in unsafe), but it can be disabled by configuration.
|
||||
* ``wait_timeout`` - wait timeout in seconds, maximum amount of time to be waited before lock will be free, integer, optional.
|
||||
* ``wait_timeout`` - wait timeout in seconds, maximum amount of time to be waited before lock will be free, integer, optional. If set to ``0``, wait infinitely.
|
||||
|
||||
``archive`` group
|
||||
-----------------
|
||||
|
||||
@@ -33,3 +33,28 @@ The service provides several commands aim to do easy repository backup and resto
|
||||
.. code-block:: shell
|
||||
|
||||
sudo -u ahriman ahriman repo-rebuild --from-database
|
||||
|
||||
Package rollback
|
||||
================
|
||||
|
||||
If the ``archive.keep_built_packages`` option is enabled, the service keeps previously built package files in the archive directory. These archives can be used to rollback a package to a previous successfully built version.
|
||||
|
||||
#.
|
||||
List available archive versions for a package:
|
||||
|
||||
.. code-block:: shell
|
||||
|
||||
ahriman package-archives ahriman
|
||||
|
||||
#.
|
||||
Rollback the package to the desired version:
|
||||
|
||||
.. code-block:: shell
|
||||
|
||||
sudo -u ahriman ahriman package-rollback ahriman 2.19.0-1
|
||||
|
||||
By default, the ``--hold`` flag is enabled, which prevents the package from being automatically updated on subsequent ``repo-update`` runs. To rollback without holding the package use:
|
||||
|
||||
.. code-block:: shell
|
||||
|
||||
sudo -u ahriman ahriman package-rollback ahriman 2.19.0-1 --no-hold
|
||||
|
||||
@@ -7,10 +7,13 @@ aiohttp==3.11.18
|
||||
# ahriman (pyproject.toml)
|
||||
# aiohttp-cors
|
||||
# aiohttp-jinja2
|
||||
# aiohttp-sse
|
||||
aiohttp-cors==0.8.1
|
||||
# via ahriman (pyproject.toml)
|
||||
aiohttp-jinja2==1.6
|
||||
# via ahriman (pyproject.toml)
|
||||
aiohttp-sse==2.2.0
|
||||
# via ahriman (pyproject.toml)
|
||||
aiosignal==1.3.2
|
||||
# via aiohttp
|
||||
alabaster==1.0.0
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import js from "@eslint/js";
|
||||
import stylistic from "@stylistic/eslint-plugin";
|
||||
import react from "eslint-plugin-react";
|
||||
import reactHooks from "eslint-plugin-react-hooks";
|
||||
import reactRefresh from "eslint-plugin-react-refresh";
|
||||
import simpleImportSort from "eslint-plugin-simple-import-sort";
|
||||
@@ -8,7 +9,11 @@ import tseslint from "typescript-eslint";
|
||||
export default tseslint.config(
|
||||
{ ignores: ["dist"] },
|
||||
{
|
||||
extends: [js.configs.recommended, ...tseslint.configs.recommendedTypeChecked],
|
||||
extends: [
|
||||
js.configs.recommended,
|
||||
react.configs.flat.recommended,
|
||||
...tseslint.configs.recommendedTypeChecked,
|
||||
],
|
||||
files: ["src/**/*.{ts,tsx}"],
|
||||
languageOptions: {
|
||||
parserOptions: {
|
||||
@@ -17,13 +22,14 @@ export default tseslint.config(
|
||||
},
|
||||
},
|
||||
plugins: {
|
||||
"@stylistic": stylistic,
|
||||
"react-hooks": reactHooks,
|
||||
"react-refresh": reactRefresh,
|
||||
"simple-import-sort": simpleImportSort,
|
||||
"@stylistic": stylistic,
|
||||
},
|
||||
rules: {
|
||||
...reactHooks.configs.recommended.rules,
|
||||
"react/react-in-jsx-scope": "off",
|
||||
"react-refresh/only-export-components": ["warn", { allowConstantExport: true }],
|
||||
|
||||
// imports
|
||||
@@ -33,7 +39,7 @@ export default tseslint.config(
|
||||
// core
|
||||
"curly": "error",
|
||||
"eqeqeq": "error",
|
||||
"no-console": "error",
|
||||
"no-console": ["warn", { allow: ["warn", "error"] }],
|
||||
"no-eval": "error",
|
||||
|
||||
// stylistic
|
||||
@@ -68,6 +74,7 @@ export default tseslint.config(
|
||||
"@typescript-eslint/consistent-type-imports": ["error", { prefer: "type-imports" }],
|
||||
"@typescript-eslint/explicit-function-return-type": ["error", { allowExpressions: true }],
|
||||
"@typescript-eslint/no-deprecated": "error",
|
||||
"@typescript-eslint/no-floating-promises": "error",
|
||||
"@typescript-eslint/no-unused-vars": ["error", { argsIgnorePattern: "^_" }],
|
||||
"@typescript-eslint/prefer-nullish-coalescing": "error",
|
||||
"@typescript-eslint/prefer-optional-chain": "error",
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
"react": ">=19.2.0 <19.3.0",
|
||||
"react-chartjs-2": ">=5.3.0 <5.4.0",
|
||||
"react-dom": ">=19.2.0 <19.3.0",
|
||||
"react-error-boundary": ">=6.1.0 <6.2.0",
|
||||
"react-syntax-highlighter": ">=16.1.0 <16.2.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
@@ -20,6 +21,7 @@
|
||||
"@types/react-syntax-highlighter": ">=15.5.0 <15.6.0",
|
||||
"@vitejs/plugin-react": ">=6.0.0 <6.1.0",
|
||||
"eslint": ">=9.39.0 <9.40.0",
|
||||
"eslint-plugin-react": ">=7.37.0 <7.38.0",
|
||||
"eslint-plugin-react-hooks": ">=7.0.0 <7.1.0",
|
||||
"eslint-plugin-react-refresh": ">=0.5.0 <0.6.0",
|
||||
"eslint-plugin-simple-import-sort": ">=12.1.0 <12.2.0",
|
||||
|
||||
55
frontend/src/components/common/ErrorBoundary.tsx
Normal file
55
frontend/src/components/common/ErrorBoundary.tsx
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (c) 2021-2026 ahriman team.
|
||||
*
|
||||
* This file is part of ahriman
|
||||
* (see https://github.com/arcan1s/ahriman).
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import { Box, Button, Typography } from "@mui/material";
|
||||
import type React from "react";
|
||||
import type { FallbackProps } from "react-error-boundary";
|
||||
|
||||
interface ErrorDetails {
|
||||
message: string;
|
||||
stack: string | undefined;
|
||||
}
|
||||
|
||||
export default function ErrorFallback({ error }: FallbackProps): React.JSX.Element {
|
||||
|
||||
const details: ErrorDetails = error instanceof Error
|
||||
? { message: error.message, stack: error.stack }
|
||||
: { message: String(error), stack: undefined };
|
||||
|
||||
return <Box role="alert" sx={{ color: "text.primary", minHeight: "100vh", p: 6 }}>
|
||||
<Typography sx={{ fontWeight: 700 }} variant="h4">
|
||||
Something went wrong
|
||||
</Typography>
|
||||
|
||||
<Typography color="error" sx={{ fontFamily: "monospace", mt: 2 }}>
|
||||
{details.message}
|
||||
</Typography>
|
||||
|
||||
{details.stack && <Typography
|
||||
component="pre"
|
||||
sx={{ color: "text.secondary", fontFamily: "monospace", fontSize: "0.75rem", mt: 3, whiteSpace: "pre-wrap", wordBreak: "break-word" }}
|
||||
>
|
||||
{details.stack}
|
||||
</Typography>}
|
||||
|
||||
<Box sx={{ display: "flex", gap: 2, mt: 4 }}>
|
||||
<Button onClick={() => window.location.reload()} variant="outlined">Reload page</Button>
|
||||
</Box>
|
||||
</Box>;
|
||||
}
|
||||
@@ -21,11 +21,18 @@ import "chartSetup";
|
||||
import "utils";
|
||||
|
||||
import App from "App";
|
||||
import ErrorFallback from "components/common/ErrorBoundary";
|
||||
import { StrictMode } from "react";
|
||||
import { createRoot } from "react-dom/client";
|
||||
import { ErrorBoundary } from "react-error-boundary";
|
||||
|
||||
createRoot(document.getElementById("root")!).render(
|
||||
<StrictMode>
|
||||
<App />
|
||||
<ErrorBoundary
|
||||
FallbackComponent={ErrorFallback}
|
||||
onError={(error, info) => console.error("Uncaught error:", error, info.componentStack)}
|
||||
>
|
||||
<App />
|
||||
</ErrorBoundary>
|
||||
</StrictMode>,
|
||||
);
|
||||
|
||||
@@ -4,18 +4,18 @@
|
||||
"baseUrl": "src",
|
||||
"isolatedModules": true,
|
||||
"jsx": "react-jsx",
|
||||
"lib": ["ES2020", "DOM", "DOM.Iterable"],
|
||||
"lib": ["ESNext", "DOM", "DOM.Iterable"],
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "bundler",
|
||||
"noEmit": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"noUncheckedIndexedAccess": true,
|
||||
"noImplicitOverride": true,
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"resolveJsonModule": true,
|
||||
"skipLibCheck": true,
|
||||
"strict": true,
|
||||
"target": "ES2020",
|
||||
"target": "ESNext",
|
||||
"useDefineForClassFields": true
|
||||
},
|
||||
"include": ["src"]
|
||||
|
||||
@@ -77,7 +77,7 @@ package_ahriman-triggers() {
|
||||
package_ahriman-web() {
|
||||
pkgname='ahriman-web'
|
||||
pkgdesc="ArcH linux ReposItory MANager, web server"
|
||||
depends=("$pkgbase-core=$pkgver" 'python-aiohttp-cors' 'python-aiohttp-jinja2')
|
||||
depends=("$pkgbase-core=$pkgver" 'python-aiohttp-cors' 'python-aiohttp-jinja2' 'python-aiohttp-sse-git')
|
||||
optdepends=('python-aioauth-client: OAuth2 authorization support'
|
||||
'python-aiohttp-apispec>=3.0.0: autogenerated API documentation'
|
||||
'python-aiohttp-openmetrics: HTTP metrics support'
|
||||
|
||||
@@ -30,6 +30,14 @@ allow_read_only = yes
|
||||
; If no intervals set, auto refresh will be disabled. 0 can only be the first element and will disable auto refresh
|
||||
; by default.
|
||||
autorefresh_intervals = 5 1 10 30 60
|
||||
; Allowed CORS headers. By default everything is allowed.
|
||||
;cors_allow_headers =
|
||||
; Allowed CORS methods. By default everything is allowed.
|
||||
;cors_allow_methods =
|
||||
; Allowed CORS origins.
|
||||
;cors_allow_origins = *
|
||||
; Exposed CORS headers. By default everything is exposed.
|
||||
;cors_expose_headers =
|
||||
; Enable file upload endpoint used by some triggers.
|
||||
;enable_archive_upload = no
|
||||
; Address to bind the server.
|
||||
@@ -38,6 +46,8 @@ host = 127.0.0.1
|
||||
;index_url =
|
||||
; Max file size in bytes which can be uploaded to the server. Requires ${web:enable_archive_upload} to be enabled.
|
||||
;max_body_size =
|
||||
; Max event queue size used for server sent event endpoints (0 is infinite)
|
||||
;max_queue_size = 0
|
||||
; Port to listen. Must be set, if the web service is enabled.
|
||||
;port =
|
||||
; Disable status (e.g. package status, logs, etc) endpoints. Useful for build only modes.
|
||||
|
||||
@@ -58,6 +58,7 @@ web = [
|
||||
"aiohttp",
|
||||
"aiohttp_cors",
|
||||
"aiohttp_jinja2",
|
||||
"aiohttp_sse",
|
||||
]
|
||||
web-auth = [
|
||||
"ahriman[web]",
|
||||
|
||||
@@ -104,7 +104,7 @@ class PkgbuildParser(shlex.shlex):
|
||||
|
||||
# ignore substitution and extend bash symbols
|
||||
self.wordchars += "${}#:+-@!"
|
||||
# in case of default behaviour, it will ignore, for example, segment part of url outside of quotes
|
||||
# in case of default behavior, it will ignore, for example, segment part of url outside of quotes
|
||||
self.commenters = ""
|
||||
|
||||
@staticmethod
|
||||
|
||||
@@ -72,7 +72,7 @@ class AUR(Remote):
|
||||
parse RPC response to package list
|
||||
|
||||
Args:
|
||||
response(dict[str, Any]): RPC response json
|
||||
response(dict[str, Any]): RPC response JSON
|
||||
|
||||
Returns:
|
||||
list[AURPackage]: list of parsed packages
|
||||
|
||||
@@ -74,7 +74,7 @@ class Official(Remote):
|
||||
parse RPC response to package list
|
||||
|
||||
Args:
|
||||
response(dict[str, Any]): RPC response json
|
||||
response(dict[str, Any]): RPC response JSON
|
||||
|
||||
Returns:
|
||||
list[AURPackage]: list of parsed packages
|
||||
|
||||
@@ -32,7 +32,7 @@ class OfficialSyncdb(Official):
|
||||
updates.
|
||||
|
||||
This approach also has limitations, because we don't require superuser rights (neither going to download database
|
||||
separately), the database file might be outdated and must be handled manually (or kind of). This behaviour might be
|
||||
separately), the database file might be outdated and must be handled manually (or kind of). This behavior might be
|
||||
changed in the future.
|
||||
|
||||
Still we leave search function based on the official repositories RPC.
|
||||
|
||||
@@ -50,13 +50,13 @@ class Auth(LazyLogging):
|
||||
@property
|
||||
def auth_control(self) -> str:
|
||||
"""
|
||||
This workaround is required to make different behaviour for login interface.
|
||||
This workaround is required to make different behavior for login interface.
|
||||
In case of internal authentication it must provide an interface (modal form) to log in with button sends POST
|
||||
request. But for an external providers behaviour can be different: e.g. OAuth provider requires sending GET
|
||||
request. But for an external providers behavior can be different: e.g. OAuth provider requires sending GET
|
||||
request to external resource
|
||||
|
||||
Returns:
|
||||
str: login control as html code to insert
|
||||
str: login control as HTML code to insert
|
||||
"""
|
||||
return "<button type=\"button\" class=\"btn btn-link\" data-bs-toggle=\"modal\" data-bs-target=\"#login-modal\" style=\"text-decoration: none\"><i class=\"bi bi-box-arrow-in-right\"></i> login</button>"
|
||||
|
||||
|
||||
@@ -30,7 +30,13 @@ except ImportError:
|
||||
from typing import Any
|
||||
|
||||
|
||||
__all__ = ["authorized_userid", "check_authorized", "forget", "remember"]
|
||||
__all__ = [
|
||||
"authorized_userid",
|
||||
"check_authorized",
|
||||
"forget",
|
||||
"get_session",
|
||||
"remember",
|
||||
]
|
||||
|
||||
|
||||
async def authorized_userid(*args: Any, **kwargs: Any) -> Any:
|
||||
|
||||
@@ -62,10 +62,10 @@ class OAuth(Mapping):
|
||||
@property
|
||||
def auth_control(self) -> str:
|
||||
"""
|
||||
get authorization html control
|
||||
get authorization HTML control
|
||||
|
||||
Returns:
|
||||
str: login control as html code to insert
|
||||
str: login control as HTML code to insert
|
||||
"""
|
||||
return "<a class=\"nav-link\" href=\"/api/v1/login\" title=\"login via OAuth2\"><i class=\"bi bi-box-arrow-in-right\"></i> login</a>"
|
||||
|
||||
|
||||
@@ -416,7 +416,7 @@ class Sources(LazyLogging):
|
||||
else:
|
||||
patch.write(sources_dir / "PKGBUILD")
|
||||
|
||||
def read(self, sources_dir: Path, commit_sha: str, path: Path) -> str:
|
||||
def read(self, sources_dir: Path, commit_sha: str, path: Path) -> str | None:
|
||||
"""
|
||||
read file content from the specified commit
|
||||
|
||||
@@ -426,6 +426,10 @@ class Sources(LazyLogging):
|
||||
path(Path): path to file inside the repository
|
||||
|
||||
Returns:
|
||||
str: file content at specified commit
|
||||
str | None: file content at specified commit if available
|
||||
"""
|
||||
return check_output(*self.git(), "show", f"{commit_sha}:{path}", cwd=sources_dir, logger=self.logger)
|
||||
try:
|
||||
return check_output(*self.git(), "show", f"{commit_sha}:{path}", cwd=sources_dir, logger=self.logger)
|
||||
except CalledProcessError:
|
||||
self.logger.exception("failed to read file %s at %s", path, commit_sha)
|
||||
return None
|
||||
|
||||
@@ -72,7 +72,7 @@ class Configuration(configparser.RawConfigParser):
|
||||
def __init__(self, allow_no_value: bool = False, allow_multi_key: bool = True) -> None:
|
||||
"""
|
||||
Args:
|
||||
allow_no_value(bool, optional): copies :class:`configparser.RawConfigParser` behaviour. In case if it is set
|
||||
allow_no_value(bool, optional): copies :class:`configparser.RawConfigParser` behavior. In case if it is set
|
||||
to ``True``, the keys without values will be allowed (Default value = False)
|
||||
allow_multi_key(bool, optional): if set to ``False``, then the default dictionary class will be used to
|
||||
store keys internally. Otherwise, the special implementation will be used, which supports arrays
|
||||
|
||||
@@ -358,6 +358,38 @@ CONFIGURATION_SCHEMA: ConfigurationSchema = {
|
||||
"min": 0,
|
||||
},
|
||||
},
|
||||
"cors_allow_headers": {
|
||||
"type": "list",
|
||||
"coerce": "list",
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"empty": False,
|
||||
},
|
||||
},
|
||||
"cors_allow_methods": {
|
||||
"type": "list",
|
||||
"coerce": "list",
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"empty": False,
|
||||
},
|
||||
},
|
||||
"cors_allow_origins": {
|
||||
"type": "list",
|
||||
"coerce": "list",
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"empty": False,
|
||||
},
|
||||
},
|
||||
"cors_expose_headers": {
|
||||
"type": "list",
|
||||
"coerce": "list",
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"empty": False,
|
||||
},
|
||||
},
|
||||
"enable_archive_upload": {
|
||||
"type": "boolean",
|
||||
"coerce": "boolean",
|
||||
|
||||
@@ -81,7 +81,7 @@ class ChangesOperations(Operations):
|
||||
values
|
||||
(:package_base, :last_commit_sha, :changes, :pkgbuild, :repository)
|
||||
on conflict (package_base, repository) do update set
|
||||
last_commit_sha = :last_commit_sha, changes = :changes, pkgbuild = :pkgbuild
|
||||
last_commit_sha = :last_commit_sha, changes = :changes, pkgbuild = coalesce(:pkgbuild, pkgbuild)
|
||||
""",
|
||||
{
|
||||
"package_base": package_base,
|
||||
|
||||
@@ -171,7 +171,7 @@ class SyncHttpClient(LazyLogging):
|
||||
headers(dict[str, str] | None, optional): request headers (Default value = None)
|
||||
params(list[tuple[str, str]] | None, optional): request query parameters (Default value = None)
|
||||
data(Any | None, optional): request raw data parameters (Default value = None)
|
||||
json(dict[str, Any] | None, optional): request json parameters (Default value = None)
|
||||
json(dict[str, Any] | None, optional): request JSON parameters (Default value = None)
|
||||
files(dict[str, MultipartType] | None, optional): multipart upload (Default value = None)
|
||||
stream(bool | None, optional): handle response as stream (Default value = None)
|
||||
session(requests.Session | None, optional): session object if any (Default value = None)
|
||||
|
||||
@@ -27,7 +27,7 @@ from ahriman.models.result import Result
|
||||
|
||||
class Console(Report):
|
||||
"""
|
||||
html report generator
|
||||
HTML report generator
|
||||
|
||||
Attributes:
|
||||
use_utf(bool): print utf8 symbols instead of ASCII
|
||||
|
||||
@@ -27,10 +27,10 @@ from ahriman.models.result import Result
|
||||
|
||||
class HTML(Report, JinjaTemplate):
|
||||
"""
|
||||
html report generator
|
||||
HTML report generator
|
||||
|
||||
Attributes:
|
||||
report_path(Path): output path to html report
|
||||
report_path(Path): output path to HTML report
|
||||
template(Path | str): name or path to template for full package list
|
||||
"""
|
||||
|
||||
|
||||
@@ -71,7 +71,7 @@ class Report(LazyLogging):
|
||||
Args:
|
||||
repository_id(RepositoryId): repository unique identifier
|
||||
configuration(Configuration): configuration instance
|
||||
target(str): target to generate report aka section name (e.g. html)
|
||||
target(str): target to generate report aka section name (e.g. HTML)
|
||||
|
||||
Returns:
|
||||
Report: client according to current settings
|
||||
|
||||
113
src/ahriman/core/status/event_bus.py
Normal file
113
src/ahriman/core/status/event_bus.py
Normal file
@@ -0,0 +1,113 @@
|
||||
#
|
||||
# Copyright (c) 2021-2026 ahriman team.
|
||||
#
|
||||
# This file is part of ahriman
|
||||
# (see https://github.com/arcan1s/ahriman).
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
import uuid
|
||||
|
||||
from asyncio import Lock, Queue, QueueFull
|
||||
from typing import Any
|
||||
|
||||
from ahriman.core.log import LazyLogging
|
||||
from ahriman.models.event import EventType
|
||||
|
||||
|
||||
SSEvent = tuple[str, dict[str, Any]]
|
||||
|
||||
|
||||
class EventBus(LazyLogging):
|
||||
"""
|
||||
event bus implementation
|
||||
|
||||
Attributes:
|
||||
max_size(int): maximum size of queue
|
||||
"""
|
||||
|
||||
def __init__(self, max_size: int) -> None:
|
||||
"""
|
||||
Args:
|
||||
max_size(int): maximum size of queue
|
||||
"""
|
||||
self.max_size = max_size
|
||||
|
||||
self._lock = Lock()
|
||||
self._subscribers: dict[str, tuple[list[EventType] | None, Queue[SSEvent | None]]] = {}
|
||||
|
||||
async def broadcast(self, event_type: EventType, object_id: str | None, **kwargs: Any) -> None:
|
||||
"""
|
||||
broadcast event to all subscribers
|
||||
|
||||
Args:
|
||||
event_type(EventType): event type
|
||||
object_id(str | None): object identifier (e.g. package base)
|
||||
**kwargs(Any): additional event data
|
||||
"""
|
||||
event: dict[str, Any] = {"object_id": object_id}
|
||||
event.update(kwargs)
|
||||
|
||||
async with self._lock:
|
||||
for subscriber_id, (topics, queue) in self._subscribers.items():
|
||||
if topics is not None and event_type not in topics:
|
||||
continue
|
||||
try:
|
||||
queue.put_nowait((event_type, event))
|
||||
except QueueFull:
|
||||
self.logger.warning("discard message to slow subscriber %s", subscriber_id)
|
||||
|
||||
async def shutdown(self) -> None:
|
||||
"""
|
||||
gracefully shutdown all subscribers
|
||||
"""
|
||||
async with self._lock:
|
||||
for _, queue in self._subscribers.values():
|
||||
try:
|
||||
queue.put_nowait(None)
|
||||
except QueueFull:
|
||||
pass
|
||||
queue.shutdown()
|
||||
|
||||
async def subscribe(self, topics: list[EventType] | None = None) -> tuple[str, Queue[SSEvent | None]]:
|
||||
"""
|
||||
register new subscriber
|
||||
|
||||
Args:
|
||||
topics(list[EventType] | None, optional): list of event types to filter by. If ``None`` is set,
|
||||
all events will be delivered (Default value = None)
|
||||
|
||||
Returns:
|
||||
tuple[str, Queue[SSEvent | None]]: subscriber identifier and associated queue
|
||||
"""
|
||||
subscriber_id = str(uuid.uuid4())
|
||||
queue: Queue[SSEvent | None] = Queue(self.max_size)
|
||||
|
||||
async with self._lock:
|
||||
self._subscribers[subscriber_id] = (topics, queue)
|
||||
|
||||
return subscriber_id, queue
|
||||
|
||||
async def unsubscribe(self, subscriber_id: str) -> None:
|
||||
"""
|
||||
unsubscribe from events
|
||||
|
||||
Args:
|
||||
subscriber_id(str): subscriber unique identifier
|
||||
"""
|
||||
async with self._lock:
|
||||
result = self._subscribers.pop(subscriber_id, None)
|
||||
if result is not None:
|
||||
_, queue = result
|
||||
queue.shutdown()
|
||||
@@ -17,15 +17,16 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
from collections.abc import Callable
|
||||
# pylint: disable=too-many-public-methods
|
||||
from asyncio import Lock
|
||||
from dataclasses import replace
|
||||
from threading import Lock
|
||||
from typing import Any, Self
|
||||
from typing import Self
|
||||
|
||||
from ahriman.core.exceptions import UnknownPackageError
|
||||
from ahriman.core.log import LazyLogging
|
||||
from ahriman.core.repository.package_info import PackageInfo
|
||||
from ahriman.core.status import Client
|
||||
from ahriman.core.status.event_bus import EventBus
|
||||
from ahriman.models.build_status import BuildStatus, BuildStatusEnum
|
||||
from ahriman.models.changes import Changes
|
||||
from ahriman.models.dependencies import Dependencies
|
||||
@@ -41,51 +42,74 @@ class Watcher(LazyLogging):
|
||||
|
||||
Attributes:
|
||||
client(Client): reporter instance
|
||||
event_bus(EventBus): event bus instance
|
||||
package_info(PackageInfo): package info instance
|
||||
status(BuildStatus): daemon status
|
||||
"""
|
||||
|
||||
def __init__(self, client: Client, package_info: PackageInfo) -> None:
|
||||
def __init__(self, client: Client, package_info: PackageInfo, event_bus: EventBus) -> None:
|
||||
"""
|
||||
Args:
|
||||
client(Client): reporter instance
|
||||
package_info(PackageInfo): package info instance
|
||||
event_bus(EventBus): event bus instance
|
||||
"""
|
||||
self.client = client
|
||||
self.package_info = package_info
|
||||
self.event_bus = event_bus
|
||||
|
||||
self._lock = Lock()
|
||||
self._known: dict[str, tuple[Package, BuildStatus]] = {}
|
||||
self.status = BuildStatus()
|
||||
|
||||
@property
|
||||
def packages(self) -> list[tuple[Package, BuildStatus]]:
|
||||
async def event_add(self, event: Event) -> None:
|
||||
"""
|
||||
get current known packages list
|
||||
create new event
|
||||
|
||||
Args:
|
||||
event(Event): audit log event
|
||||
"""
|
||||
self.client.event_add(event)
|
||||
|
||||
async def event_get(self, event: str | EventType | None, object_id: str | None,
|
||||
from_date: int | float | None = None, to_date: int | float | None = None,
|
||||
limit: int = -1, offset: int = 0) -> list[Event]:
|
||||
"""
|
||||
retrieve list of events
|
||||
|
||||
Args:
|
||||
event(str | EventType | None): filter by event type
|
||||
object_id(str | None): filter by event object
|
||||
from_date(int | float | None, optional): minimal creation date, inclusive (Default value = None)
|
||||
to_date(int | float | None, optional): maximal creation date, exclusive (Default value = None)
|
||||
limit(int, optional): limit records to the specified count, -1 means unlimited (Default value = -1)
|
||||
offset(int, optional): records offset (Default value = 0)
|
||||
|
||||
Returns:
|
||||
list[tuple[Package, BuildStatus]]: list of packages together with their statuses
|
||||
list[Event]: list of audit log events
|
||||
"""
|
||||
with self._lock:
|
||||
return list(self._known.values())
|
||||
return self.client.event_get(event, object_id, from_date, to_date, limit, offset)
|
||||
|
||||
event_add: Callable[[Event], None]
|
||||
|
||||
event_get: Callable[[str | EventType | None, str | None, int | None, int | None, int, int], list[Event]]
|
||||
|
||||
def load(self) -> None:
|
||||
async def load(self) -> None:
|
||||
"""
|
||||
load packages from local database
|
||||
"""
|
||||
with self._lock:
|
||||
async with self._lock:
|
||||
self._known = {
|
||||
package.base: (package, status)
|
||||
for package, status in self.client.package_get(None)
|
||||
}
|
||||
|
||||
logs_rotate: Callable[[int], None]
|
||||
async def logs_rotate(self, keep_last_records: int) -> None:
|
||||
"""
|
||||
remove older logs from storage
|
||||
|
||||
def package_archives(self, package_base: str) -> list[Package]:
|
||||
Args:
|
||||
keep_last_records(int): number of last records to keep
|
||||
"""
|
||||
self.client.logs_rotate(keep_last_records)
|
||||
|
||||
async def package_archives(self, package_base: str) -> list[Package]:
|
||||
"""
|
||||
get known package archives
|
||||
|
||||
@@ -97,15 +121,51 @@ class Watcher(LazyLogging):
|
||||
"""
|
||||
return self.package_info.package_archives(package_base)
|
||||
|
||||
package_changes_get: Callable[[str], Changes]
|
||||
async def package_changes_get(self, package_base: str) -> Changes:
|
||||
"""
|
||||
get package changes
|
||||
|
||||
package_changes_update: Callable[[str, Changes], None]
|
||||
Args:
|
||||
package_base(str): package base to retrieve
|
||||
|
||||
package_dependencies_get: Callable[[str], Dependencies]
|
||||
Returns:
|
||||
Changes: package changes if available and empty object otherwise
|
||||
"""
|
||||
return self.client.package_changes_get(package_base)
|
||||
|
||||
package_dependencies_update: Callable[[str, Dependencies], None]
|
||||
async def package_changes_update(self, package_base: str, changes: Changes) -> None:
|
||||
"""
|
||||
update package changes
|
||||
|
||||
def package_get(self, package_base: str) -> tuple[Package, BuildStatus]:
|
||||
Args:
|
||||
package_base(str): package base to update
|
||||
changes(Changes): changes descriptor
|
||||
"""
|
||||
self.client.package_changes_update(package_base, changes)
|
||||
|
||||
async def package_dependencies_get(self, package_base: str) -> Dependencies:
|
||||
"""
|
||||
get package dependencies
|
||||
|
||||
Args:
|
||||
package_base(str): package base to retrieve
|
||||
|
||||
Returns:
|
||||
list[Dependencies]: package implicit dependencies if available
|
||||
"""
|
||||
return self.client.package_dependencies_get(package_base)
|
||||
|
||||
async def package_dependencies_update(self, package_base: str, dependencies: Dependencies) -> None:
|
||||
"""
|
||||
update package dependencies
|
||||
|
||||
Args:
|
||||
package_base(str): package base to update
|
||||
dependencies(Dependencies): dependencies descriptor
|
||||
"""
|
||||
self.client.package_dependencies_update(package_base, dependencies)
|
||||
|
||||
async def package_get(self, package_base: str) -> tuple[Package, BuildStatus]:
|
||||
"""
|
||||
get current package base build status
|
||||
|
||||
@@ -119,18 +179,12 @@ class Watcher(LazyLogging):
|
||||
UnknownPackageError: if no package found
|
||||
"""
|
||||
try:
|
||||
with self._lock:
|
||||
async with self._lock:
|
||||
return self._known[package_base]
|
||||
except KeyError:
|
||||
raise UnknownPackageError(package_base) from None
|
||||
|
||||
package_logs_add: Callable[[LogRecord], None]
|
||||
|
||||
package_logs_get: Callable[[str, str | None, str | None, int, int], list[LogRecord]]
|
||||
|
||||
package_logs_remove: Callable[[str, str | None], None]
|
||||
|
||||
def package_hold_update(self, package_base: str, *, enabled: bool) -> None:
|
||||
async def package_hold_update(self, package_base: str, *, enabled: bool) -> None:
|
||||
"""
|
||||
update package hold status
|
||||
|
||||
@@ -138,29 +192,98 @@ class Watcher(LazyLogging):
|
||||
package_base(str): package base name
|
||||
enabled(bool): new hold status
|
||||
"""
|
||||
package, status = self.package_get(package_base)
|
||||
with self._lock:
|
||||
package, status = await self.package_get(package_base)
|
||||
async with self._lock:
|
||||
self._known[package_base] = (package, replace(status, is_held=enabled))
|
||||
self.client.package_hold_update(package_base, enabled=enabled)
|
||||
|
||||
package_patches_get: Callable[[str, str | None], list[PkgbuildPatch]]
|
||||
await self.event_bus.broadcast(EventType.PackageHeld, package_base, is_held=enabled)
|
||||
|
||||
package_patches_remove: Callable[[str, str], None]
|
||||
async def package_logs_add(self, log_record: LogRecord) -> None:
|
||||
"""
|
||||
post log record
|
||||
|
||||
package_patches_update: Callable[[str, PkgbuildPatch], None]
|
||||
Args:
|
||||
log_record(LogRecord): log record
|
||||
"""
|
||||
self.client.package_logs_add(log_record)
|
||||
|
||||
def package_remove(self, package_base: str) -> None:
|
||||
await self.event_bus.broadcast(EventType.BuildLog, log_record.log_record_id.package_base, **log_record.view())
|
||||
|
||||
async def package_logs_get(self, package_base: str, version: str | None = None, process_id: str | None = None,
|
||||
limit: int = -1, offset: int = 0) -> list[LogRecord]:
|
||||
"""
|
||||
get package logs
|
||||
|
||||
Args:
|
||||
package_base(str): package base
|
||||
version(str | None, optional): package version to search (Default value = None)
|
||||
process_id(str | None, optional): process identifier to search (Default value = None)
|
||||
limit(int, optional): limit records to the specified count, -1 means unlimited (Default value = -1)
|
||||
offset(int, optional): records offset (Default value = 0)
|
||||
|
||||
Returns:
|
||||
list[LogRecord]: package logs
|
||||
"""
|
||||
return self.client.package_logs_get(package_base, version, process_id, limit, offset)
|
||||
|
||||
async def package_logs_remove(self, package_base: str, version: str | None) -> None:
|
||||
"""
|
||||
remove package logs
|
||||
|
||||
Args:
|
||||
package_base(str): package base
|
||||
version(str | None): package version to remove logs. If ``None`` is set, all logs will be removed
|
||||
"""
|
||||
self.client.package_logs_remove(package_base, version)
|
||||
|
||||
async def package_patches_get(self, package_base: str, variable: str | None) -> list[PkgbuildPatch]:
|
||||
"""
|
||||
get package patches
|
||||
|
||||
Args:
|
||||
package_base(str): package base to retrieve
|
||||
variable(str | None): optional filter by patch variable
|
||||
|
||||
Returns:
|
||||
list[PkgbuildPatch]: list of patches for the specified package
|
||||
"""
|
||||
return self.client.package_patches_get(package_base, variable)
|
||||
|
||||
async def package_patches_remove(self, package_base: str, variable: str | None) -> None:
|
||||
"""
|
||||
remove package patch
|
||||
|
||||
Args:
|
||||
package_base(str): package base to update
|
||||
variable(str | None): patch name. If ``None`` is set, all patches will be removed
|
||||
"""
|
||||
self.client.package_patches_remove(package_base, variable)
|
||||
|
||||
async def package_patches_update(self, package_base: str, patch: PkgbuildPatch) -> None:
|
||||
"""
|
||||
create or update package patch
|
||||
|
||||
Args:
|
||||
package_base(str): package base to update
|
||||
patch(PkgbuildPatch): package patch
|
||||
"""
|
||||
self.client.package_patches_update(package_base, patch)
|
||||
|
||||
async def package_remove(self, package_base: str) -> None:
|
||||
"""
|
||||
remove package base from known list if any
|
||||
|
||||
Args:
|
||||
package_base(str): package base
|
||||
"""
|
||||
with self._lock:
|
||||
async with self._lock:
|
||||
self._known.pop(package_base, None)
|
||||
self.client.package_remove(package_base)
|
||||
|
||||
def package_status_update(self, package_base: str, status: BuildStatusEnum) -> None:
|
||||
await self.event_bus.broadcast(EventType.PackageRemoved, package_base)
|
||||
|
||||
async def package_status_update(self, package_base: str, status: BuildStatusEnum) -> None:
|
||||
"""
|
||||
update package status
|
||||
|
||||
@@ -168,12 +291,14 @@ class Watcher(LazyLogging):
|
||||
package_base(str): package base to update
|
||||
status(BuildStatusEnum): new build status
|
||||
"""
|
||||
package, current_status = self.package_get(package_base)
|
||||
with self._lock:
|
||||
package, current_status = await self.package_get(package_base)
|
||||
async with self._lock:
|
||||
self._known[package_base] = (package, BuildStatus(status, is_held=current_status.is_held))
|
||||
self.client.package_status_update(package_base, status)
|
||||
|
||||
def package_update(self, package: Package, status: BuildStatusEnum) -> None:
|
||||
await self.event_bus.broadcast(EventType.PackageStatusChanged, package_base, status=status.value)
|
||||
|
||||
async def package_update(self, package: Package, status: BuildStatusEnum) -> None:
|
||||
"""
|
||||
update package
|
||||
|
||||
@@ -181,12 +306,32 @@ class Watcher(LazyLogging):
|
||||
package(Package): package description
|
||||
status(BuildStatusEnum): new build status
|
||||
"""
|
||||
with self._lock:
|
||||
async with self._lock:
|
||||
_, current_status = self._known.get(package.base, (package, BuildStatus()))
|
||||
self._known[package.base] = (package, BuildStatus(status, is_held=current_status.is_held))
|
||||
self.client.package_update(package, status)
|
||||
|
||||
def status_update(self, status: BuildStatusEnum) -> None:
|
||||
await self.event_bus.broadcast(
|
||||
EventType.PackageUpdated, package.base, status=status.value, version=package.version,
|
||||
)
|
||||
|
||||
async def packages(self) -> list[tuple[Package, BuildStatus]]:
|
||||
"""
|
||||
get current known packages list
|
||||
|
||||
Returns:
|
||||
list[tuple[Package, BuildStatus]]: list of packages together with their statuses
|
||||
"""
|
||||
async with self._lock:
|
||||
return list(self._known.values())
|
||||
|
||||
async def shutdown(self) -> None:
|
||||
"""
|
||||
gracefully shutdown watcher
|
||||
"""
|
||||
await self.event_bus.shutdown()
|
||||
|
||||
async def status_update(self, status: BuildStatusEnum) -> None:
|
||||
"""
|
||||
update service status
|
||||
|
||||
@@ -195,6 +340,8 @@ class Watcher(LazyLogging):
|
||||
"""
|
||||
self.status = BuildStatus(status)
|
||||
|
||||
await self.event_bus.broadcast(EventType.ServiceStatusChanged, None, status=status.value)
|
||||
|
||||
def __call__(self, package_base: str | None) -> Self:
|
||||
"""
|
||||
extract client for future calls
|
||||
@@ -204,24 +351,11 @@ class Watcher(LazyLogging):
|
||||
|
||||
Returns:
|
||||
Self: instance of self to pass calls to the client
|
||||
"""
|
||||
if package_base is not None:
|
||||
_ = self.package_get(package_base)
|
||||
return self
|
||||
|
||||
def __getattr__(self, item: str) -> Any:
|
||||
"""
|
||||
proxy methods for reporter client
|
||||
|
||||
Args:
|
||||
item(str): property name
|
||||
|
||||
Returns:
|
||||
Any: attribute by its name
|
||||
|
||||
Raises:
|
||||
AttributeError: in case if no such attribute found
|
||||
UnknownPackageError: if no package found
|
||||
"""
|
||||
if (method := getattr(self.client, item, None)) is not None:
|
||||
return method
|
||||
raise AttributeError(f"'{self.__class__.__qualname__}' object has no attribute '{item}'")
|
||||
# keep check here instead of calling package_get to keep this method synchronized
|
||||
if package_base is not None and package_base not in self._known:
|
||||
raise UnknownPackageError(package_base)
|
||||
return self
|
||||
|
||||
@@ -34,7 +34,7 @@ class Trigger(LazyLogging):
|
||||
|
||||
Attributes:
|
||||
CONFIGURATION_SCHEMA(ConfigurationSchema): (class attribute) configuration schema template
|
||||
REQUIRES_REPOSITORY(bool): (class attribute) either trigger requires loaded repository or not
|
||||
REQUIRES_REPOSITORY(bool): (class attribute) either trigger requires a repository to be loaded or not
|
||||
configuration(Configuration): configuration instance
|
||||
repository_id(RepositoryId): repository unique identifier
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ class HttpUpload(SyncHttpClient):
|
||||
str: calculated checksum of the file
|
||||
"""
|
||||
with path.open("rb") as local_file:
|
||||
md5 = hashlib.md5(local_file.read()) # nosec
|
||||
md5 = hashlib.md5(local_file.read(), usedforsecurity=False)
|
||||
return md5.hexdigest()
|
||||
|
||||
@staticmethod
|
||||
|
||||
@@ -62,9 +62,7 @@ class S3(Upload):
|
||||
@staticmethod
|
||||
def calculate_etag(path: Path, chunk_size: int) -> str:
|
||||
"""
|
||||
calculate amazon s3 etag
|
||||
credits to https://teppen.io/2018/10/23/aws_s3_verify_etags/
|
||||
For this method we have to define nosec because it is out of any security context and provided by AWS
|
||||
calculate amazon s3 etag. Credits to https://teppen.io/2018/10/23/aws_s3_verify_etags/
|
||||
|
||||
Args:
|
||||
path(Path): path to local file
|
||||
@@ -76,14 +74,17 @@ class S3(Upload):
|
||||
md5s = []
|
||||
with path.open("rb") as local_file:
|
||||
for chunk in iter(lambda: local_file.read(chunk_size), b""):
|
||||
md5s.append(hashlib.md5(chunk)) # nosec
|
||||
md5s.append(hashlib.md5(chunk, usedforsecurity=False))
|
||||
|
||||
# in case if there is only one chunk it must be just this checksum
|
||||
# and checksum of joined digest otherwise (including empty list)
|
||||
checksum = md5s[0] if len(md5s) == 1 else hashlib.md5(b"".join(md5.digest() for md5 in md5s)) # nosec
|
||||
# in case if there are more than one chunk it should be appended with amount of chunks
|
||||
if len(md5s) == 1:
|
||||
return md5s[0].hexdigest()
|
||||
|
||||
# otherwise it is checksum of joined digest (including empty list)
|
||||
md5 = hashlib.md5(b"".join(md5.digest() for md5 in md5s), usedforsecurity=False)
|
||||
# in case if there are more (exactly) than one chunk it should be appended with amount of chunks
|
||||
suffix = f"-{len(md5s)}" if len(md5s) > 1 else ""
|
||||
return f"{checksum.hexdigest()}{suffix}"
|
||||
return f"{md5.hexdigest()}{suffix}"
|
||||
|
||||
@staticmethod
|
||||
def files_remove(local_files: dict[Path, str], remote_objects: dict[Path, Any]) -> None:
|
||||
|
||||
@@ -231,13 +231,13 @@ def check_user(root: Path, *, unsafe: bool) -> None:
|
||||
|
||||
def dataclass_view(instance: Any) -> dict[str, Any]:
|
||||
"""
|
||||
convert dataclass instance to json object
|
||||
convert dataclass instance to JSON object
|
||||
|
||||
Args:
|
||||
instance(Any): dataclass instance
|
||||
|
||||
Returns:
|
||||
dict[str, Any]: json representation of the dataclass with empty field removed
|
||||
dict[str, Any]: JSON representation of the dataclass with empty field removed
|
||||
"""
|
||||
return asdict(instance, dict_factory=lambda fields: {key: value for key, value in fields if value is not None})
|
||||
|
||||
@@ -287,15 +287,15 @@ def filelock(path: Path) -> Iterator[FileLock]:
|
||||
|
||||
def filter_json(source: T, known_fields: Iterable[str] | None = None) -> T:
|
||||
"""
|
||||
recursively filter json object removing ``None`` values and optionally filtering by known fields
|
||||
recursively filter JSON object removing ``None`` values and optionally filtering by known fields
|
||||
|
||||
Args:
|
||||
source(T): raw json object (dict, list, or scalar)
|
||||
source(T): raw JSON object (dict, list, or scalar)
|
||||
known_fields(Iterable[str] | None, optional): list of fields which have to be known for the target object
|
||||
(Default value = None)
|
||||
|
||||
Returns:
|
||||
T: json without ``None`` values
|
||||
T: JSON without ``None`` values
|
||||
|
||||
Examples:
|
||||
This wrapper is mainly used for the dataclasses, thus the flow must be something like this::
|
||||
|
||||
@@ -62,7 +62,7 @@ class AURPackage:
|
||||
Examples:
|
||||
Mainly this class must be used from class methods instead of default :func:`__init__()`::
|
||||
|
||||
>>> package = AURPackage.from_json(metadata) # load package from json dump
|
||||
>>> package = AURPackage.from_json(metadata) # load package from JSON dump
|
||||
>>> # ...or alternatively...
|
||||
>>> package = AURPackage.from_repo(metadata) # load package from official repository RPC
|
||||
>>> # properties of the class are built based on ones from AUR RPC, thus additional method is required
|
||||
@@ -175,7 +175,7 @@ class AURPackage:
|
||||
construct package descriptor from official repository RPC properties
|
||||
|
||||
Args:
|
||||
dump(dict[str, Any]): json dump body
|
||||
dump(dict[str, Any]): JSON dump body
|
||||
|
||||
Returns:
|
||||
Self: AUR package descriptor
|
||||
|
||||
@@ -89,7 +89,7 @@ class BuildStatus:
|
||||
|
||||
def view(self) -> dict[str, Any]:
|
||||
"""
|
||||
generate json status view
|
||||
generate JSON status view
|
||||
|
||||
Returns:
|
||||
dict[str, Any]: json-friendly dictionary
|
||||
|
||||
@@ -41,10 +41,10 @@ class Changes:
|
||||
@classmethod
|
||||
def from_json(cls, dump: dict[str, Any]) -> Self:
|
||||
"""
|
||||
construct changes from the json dump
|
||||
construct changes from the JSON dump
|
||||
|
||||
Args:
|
||||
dump(dict[str, Any]): json dump body
|
||||
dump(dict[str, Any]): JSON dump body
|
||||
|
||||
Returns:
|
||||
Self: changes object
|
||||
@@ -55,7 +55,7 @@ class Changes:
|
||||
|
||||
def view(self) -> dict[str, Any]:
|
||||
"""
|
||||
generate json change view
|
||||
generate JSON change view
|
||||
|
||||
Returns:
|
||||
dict[str, Any]: json-friendly dictionary
|
||||
|
||||
@@ -49,10 +49,10 @@ class Counters:
|
||||
@classmethod
|
||||
def from_json(cls, dump: dict[str, Any]) -> Self:
|
||||
"""
|
||||
construct counters from json dump
|
||||
construct counters from JSON dump
|
||||
|
||||
Args:
|
||||
dump(dict[str, Any]): json dump body
|
||||
dump(dict[str, Any]): JSON dump body
|
||||
|
||||
Returns:
|
||||
Self: status counters
|
||||
|
||||
@@ -28,16 +28,24 @@ class EventType(StrEnum):
|
||||
predefined event types
|
||||
|
||||
Attributes:
|
||||
BuildLog(EventType): new build log line
|
||||
PackageHeld(EventType): package hold status has been changed
|
||||
PackageOutdated(EventType): package has been marked as out-of-date
|
||||
PackageRemoved(EventType): package has been removed
|
||||
PackageStatusChanged(EventType): package build status has been changed
|
||||
PackageUpdateFailed(EventType): package update has been failed
|
||||
PackageUpdated(EventType): package has been updated
|
||||
ServiceStatusChanged(EventType): service status has been changed
|
||||
"""
|
||||
|
||||
BuildLog = "build-log"
|
||||
PackageHeld = "package-held"
|
||||
PackageOutdated = "package-outdated"
|
||||
PackageRemoved = "package-removed"
|
||||
PackageStatusChanged = "package-status-changed"
|
||||
PackageUpdateFailed = "package-update-failed"
|
||||
PackageUpdated = "package-updated"
|
||||
ServiceStatusChanged = "service-status-changed"
|
||||
|
||||
|
||||
class Event:
|
||||
@@ -72,10 +80,10 @@ class Event:
|
||||
@classmethod
|
||||
def from_json(cls, dump: dict[str, Any]) -> Self:
|
||||
"""
|
||||
construct event from the json dump
|
||||
construct event from the JSON dump
|
||||
|
||||
Args:
|
||||
dump(dict[str, Any]): json dump body
|
||||
dump(dict[str, Any]): JSON dump body
|
||||
|
||||
Returns:
|
||||
Self: event object
|
||||
@@ -102,7 +110,7 @@ class Event:
|
||||
|
||||
def view(self) -> dict[str, Any]:
|
||||
"""
|
||||
generate json event view
|
||||
generate JSON event view
|
||||
|
||||
Returns:
|
||||
dict[str, Any]: json-friendly dictionary
|
||||
|
||||
@@ -50,10 +50,10 @@ class InternalStatus:
|
||||
@classmethod
|
||||
def from_json(cls, dump: dict[str, Any]) -> Self:
|
||||
"""
|
||||
construct internal status from json dump
|
||||
construct internal status from JSON dump
|
||||
|
||||
Args:
|
||||
dump(dict[str, Any]): json dump body
|
||||
dump(dict[str, Any]): JSON dump body
|
||||
|
||||
Returns:
|
||||
Self: internal status
|
||||
@@ -70,7 +70,7 @@ class InternalStatus:
|
||||
|
||||
def view(self) -> dict[str, Any]:
|
||||
"""
|
||||
generate json status view
|
||||
generate JSON status view
|
||||
|
||||
Returns:
|
||||
dict[str, Any]: json-friendly dictionary
|
||||
|
||||
@@ -41,11 +41,11 @@ class LogRecord:
|
||||
@classmethod
|
||||
def from_json(cls, package_base: str, dump: dict[str, Any]) -> Self:
|
||||
"""
|
||||
construct log record from the json dump
|
||||
construct log record from the JSON dump
|
||||
|
||||
Args:
|
||||
package_base(str): package base for which log record belongs
|
||||
dump(dict[str, Any]): json dump body
|
||||
dump(dict[str, Any]): JSON dump body
|
||||
|
||||
Returns:
|
||||
Self: log record object
|
||||
@@ -63,7 +63,7 @@ class LogRecord:
|
||||
|
||||
def view(self) -> dict[str, Any]:
|
||||
"""
|
||||
generate json log record view
|
||||
generate JSON log record view
|
||||
|
||||
Returns:
|
||||
dict[str, Any]: json-friendly dictionary
|
||||
|
||||
@@ -50,11 +50,11 @@ class Package(LazyLogging):
|
||||
version(str): package full version
|
||||
|
||||
Examples:
|
||||
Different usages of this class may generate different (incomplete) data, e.g. if instantiating class from json::
|
||||
Different usages of this class may generate different (incomplete) data, e.g. if instantiating class from JSON::
|
||||
|
||||
>>> package = Package.from_json(dump)
|
||||
|
||||
it will contain every data available in the json body. Otherwise, if generate package from local archive::
|
||||
it will contain every data available in the JSON body. Otherwise, if generate package from local archive::
|
||||
|
||||
>>> package = Package.from_archive(local_path, pacman)
|
||||
|
||||
@@ -273,10 +273,10 @@ class Package(LazyLogging):
|
||||
@classmethod
|
||||
def from_json(cls, dump: dict[str, Any]) -> Self:
|
||||
"""
|
||||
construct package properties from json dump
|
||||
construct package properties from JSON dump
|
||||
|
||||
Args:
|
||||
dump(dict[str, Any]): json dump body
|
||||
dump(dict[str, Any]): JSON dump body
|
||||
|
||||
Returns:
|
||||
Self: package properties
|
||||
@@ -396,7 +396,7 @@ class Package(LazyLogging):
|
||||
|
||||
def view(self) -> dict[str, Any]:
|
||||
"""
|
||||
generate json package view
|
||||
generate JSON package view
|
||||
|
||||
Returns:
|
||||
dict[str, Any]: json-friendly dictionary
|
||||
|
||||
@@ -49,7 +49,7 @@ class PackageDescription:
|
||||
|
||||
Examples:
|
||||
Unlike the :class:`ahriman.models.package.Package` class, this implementation only holds properties.
|
||||
The recommended way to deal with it is to read data based on the source type - either json or
|
||||
The recommended way to deal with it is to read data based on the source type - either JSON or
|
||||
:class:`pyalpm.Package` instance::
|
||||
|
||||
>>> description = PackageDescription.from_json(dump)
|
||||
@@ -126,10 +126,10 @@ class PackageDescription:
|
||||
@classmethod
|
||||
def from_json(cls, dump: dict[str, Any]) -> Self:
|
||||
"""
|
||||
construct package properties from json dump
|
||||
construct package properties from JSON dump
|
||||
|
||||
Args:
|
||||
dump(dict[str, Any]): json dump body
|
||||
dump(dict[str, Any]): JSON dump body
|
||||
|
||||
Returns:
|
||||
Self: package properties
|
||||
@@ -169,7 +169,7 @@ class PackageDescription:
|
||||
|
||||
def view(self) -> dict[str, Any]:
|
||||
"""
|
||||
generate json package view
|
||||
generate JSON package view
|
||||
|
||||
Returns:
|
||||
dict[str, Any]: json-friendly dictionary
|
||||
|
||||
@@ -87,10 +87,10 @@ class PkgbuildPatch:
|
||||
@classmethod
|
||||
def from_json(cls, dump: dict[str, Any]) -> Self:
|
||||
"""
|
||||
construct patch descriptor from the json dump
|
||||
construct patch descriptor from the JSON dump
|
||||
|
||||
Args:
|
||||
dump(dict[str, Any]): json dump body
|
||||
dump(dict[str, Any]): JSON dump body
|
||||
|
||||
Returns:
|
||||
Self: patch object
|
||||
@@ -125,7 +125,7 @@ class PkgbuildPatch:
|
||||
# the source value looks like shell array, remove brackets and parse with shlex
|
||||
return shlex.split(shell_array[1:-1])
|
||||
case json_array if json_array.startswith("[") and json_array.endswith("]"):
|
||||
# json (aka python) array, parse with json parser instead
|
||||
# JSON (aka python) array, parse with JSON parser instead
|
||||
parsed: list[str] = json.loads(json_array)
|
||||
return parsed
|
||||
case variable:
|
||||
@@ -220,7 +220,7 @@ class PkgbuildPatch:
|
||||
|
||||
def view(self) -> dict[str, Any]:
|
||||
"""
|
||||
generate json patch view
|
||||
generate JSON patch view
|
||||
|
||||
Returns:
|
||||
dict[str, Any]: json-friendly dictionary
|
||||
|
||||
@@ -74,10 +74,10 @@ class RemoteSource:
|
||||
@classmethod
|
||||
def from_json(cls, dump: dict[str, Any]) -> Self:
|
||||
"""
|
||||
construct remote source from the json dump (or database row)
|
||||
construct remote source from the JSON dump (or database row)
|
||||
|
||||
Args:
|
||||
dump(dict[str, Any]): json dump body
|
||||
dump(dict[str, Any]): JSON dump body
|
||||
|
||||
Returns:
|
||||
Self: remote source
|
||||
@@ -102,7 +102,7 @@ class RemoteSource:
|
||||
|
||||
def view(self) -> dict[str, Any]:
|
||||
"""
|
||||
generate json package remote view
|
||||
generate JSON package remote view
|
||||
|
||||
Returns:
|
||||
dict[str, Any]: json-friendly dictionary
|
||||
|
||||
@@ -28,10 +28,10 @@ class ReportSettings(StrEnum):
|
||||
|
||||
Attributes:
|
||||
Disabled(ReportSettings): option which generates no report for testing purpose
|
||||
HTML(ReportSettings): html report generation
|
||||
HTML(ReportSettings): HTML report generation
|
||||
Email(ReportSettings): email report generation
|
||||
Console(ReportSettings): print result to console
|
||||
Telegram(ReportSettings): markdown report to telegram channel
|
||||
Telegram(ReportSettings): Markdown report to telegram channel
|
||||
RSS(ReportSettings): RSS report generation
|
||||
RemoteCall(ReportSettings): remote ahriman server call
|
||||
"""
|
||||
|
||||
@@ -70,7 +70,7 @@ class RepositoryId:
|
||||
|
||||
def view(self) -> dict[str, Any]:
|
||||
"""
|
||||
generate json package view
|
||||
generate JSON package view
|
||||
|
||||
Returns:
|
||||
dict[str, Any]: json-friendly dictionary
|
||||
|
||||
@@ -38,10 +38,10 @@ class RepositoryStats:
|
||||
@classmethod
|
||||
def from_json(cls, dump: dict[str, Any]) -> Self:
|
||||
"""
|
||||
construct counters from json dump
|
||||
construct counters from JSON dump
|
||||
|
||||
Args:
|
||||
dump(dict[str, Any]): json dump body
|
||||
dump(dict[str, Any]): JSON dump body
|
||||
|
||||
Returns:
|
||||
Self: status counters
|
||||
|
||||
@@ -45,7 +45,7 @@ class Worker:
|
||||
|
||||
def view(self) -> dict[str, Any]:
|
||||
"""
|
||||
generate json patch view
|
||||
generate JSON worker view
|
||||
|
||||
Returns:
|
||||
dict[str, Any]: json-friendly dictionary
|
||||
|
||||
@@ -21,26 +21,34 @@ import aiohttp_cors
|
||||
|
||||
from aiohttp.web import Application
|
||||
|
||||
from ahriman.core.configuration import Configuration
|
||||
|
||||
|
||||
__all__ = ["setup_cors"]
|
||||
|
||||
|
||||
def setup_cors(application: Application) -> aiohttp_cors.CorsConfig:
|
||||
def setup_cors(application: Application, configuration: Configuration) -> aiohttp_cors.CorsConfig:
|
||||
"""
|
||||
setup CORS for the web application
|
||||
|
||||
Args:
|
||||
application(Application): web application instance
|
||||
configuration(Configuration): configuration instance
|
||||
|
||||
Returns:
|
||||
aiohttp_cors.CorsConfig: generated CORS configuration
|
||||
"""
|
||||
allow_headers = configuration.getlist("web", "cors_allow_headers", fallback=[]) or "*"
|
||||
allow_methods = configuration.getlist("web", "cors_allow_methods", fallback=[]) or "*"
|
||||
expose_headers = configuration.getlist("web", "cors_expose_headers", fallback=[]) or "*"
|
||||
|
||||
cors = aiohttp_cors.setup(application, defaults={
|
||||
"*": aiohttp_cors.ResourceOptions( # type: ignore[no-untyped-call]
|
||||
expose_headers="*",
|
||||
allow_headers="*",
|
||||
allow_methods="*",
|
||||
origin: aiohttp_cors.ResourceOptions( # type: ignore[no-untyped-call]
|
||||
expose_headers=expose_headers,
|
||||
allow_headers=allow_headers,
|
||||
allow_methods=allow_methods,
|
||||
)
|
||||
for origin in configuration.getlist("web", "cors_allow_origins", fallback=["*"])
|
||||
})
|
||||
for route in application.router.routes():
|
||||
cors.add(route)
|
||||
|
||||
61
src/ahriman/web/middlewares/etag_handler.py
Normal file
61
src/ahriman/web/middlewares/etag_handler.py
Normal file
@@ -0,0 +1,61 @@
|
||||
#
|
||||
# Copyright (c) 2021-2026 ahriman team.
|
||||
#
|
||||
# This file is part of ahriman
|
||||
# (see https://github.com/arcan1s/ahriman).
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
import hashlib
|
||||
|
||||
from aiohttp import ETag
|
||||
from aiohttp.typedefs import Middleware
|
||||
from aiohttp.web import HTTPNotModified, Request, Response, StreamResponse, middleware
|
||||
|
||||
from ahriman.web.middlewares import HandlerType
|
||||
|
||||
|
||||
__all__ = ["etag_handler"]
|
||||
|
||||
|
||||
def etag_handler() -> Middleware:
|
||||
"""
|
||||
middleware to handle ETag header for conditional requests. It computes ETag from the response body
|
||||
and returns 304 Not Modified if the client sends a matching ``If-None-Match`` header
|
||||
|
||||
Returns:
|
||||
Middleware: built middleware
|
||||
|
||||
Raises:
|
||||
HTTPNotModified: if content matches ``If-None-Match`` header sent
|
||||
"""
|
||||
@middleware
|
||||
async def handle(request: Request, handler: HandlerType) -> StreamResponse:
|
||||
response = await handler(request)
|
||||
|
||||
if not isinstance(response, Response) or not isinstance(response.body, bytes):
|
||||
return response
|
||||
|
||||
if request.method not in ("GET", "HEAD"):
|
||||
return response
|
||||
|
||||
etag = ETag(value=hashlib.md5(response.body, usedforsecurity=False).hexdigest())
|
||||
response.etag = etag
|
||||
|
||||
if request.if_none_match is not None and etag in request.if_none_match:
|
||||
raise HTTPNotModified(headers={"ETag": response.headers["ETag"]})
|
||||
|
||||
return response
|
||||
|
||||
return handle
|
||||
@@ -42,13 +42,13 @@ __all__ = ["exception_handler"]
|
||||
|
||||
def _is_templated_unauthorized(request: Request) -> bool:
|
||||
"""
|
||||
check if the request is eligible for rendering html template
|
||||
check if the request is eligible for rendering HTML template
|
||||
|
||||
Args:
|
||||
request(Request): source request to check
|
||||
|
||||
Returns:
|
||||
bool: ``True`` in case if response should be rendered as html and ``False`` otherwise
|
||||
bool: ``True`` in case if response should be rendered as HTML and ``False`` otherwise
|
||||
"""
|
||||
return request.path in ("/api/v1/login", "/api/v1/logout") \
|
||||
and "application/json" not in request.headers.getall("accept", [])
|
||||
|
||||
@@ -28,6 +28,7 @@ from ahriman.web.schemas.configuration_schema import ConfigurationSchema
|
||||
from ahriman.web.schemas.counters_schema import CountersSchema
|
||||
from ahriman.web.schemas.dependencies_schema import DependenciesSchema
|
||||
from ahriman.web.schemas.error_schema import ErrorSchema
|
||||
from ahriman.web.schemas.event_bus_filter_schema import EventBusFilterSchema
|
||||
from ahriman.web.schemas.event_schema import EventSchema
|
||||
from ahriman.web.schemas.event_search_schema import EventSearchSchema
|
||||
from ahriman.web.schemas.file_schema import FileSchema
|
||||
@@ -61,6 +62,7 @@ from ahriman.web.schemas.repository_id_schema import RepositoryIdSchema
|
||||
from ahriman.web.schemas.repository_stats_schema import RepositoryStatsSchema
|
||||
from ahriman.web.schemas.rollback_schema import RollbackSchema
|
||||
from ahriman.web.schemas.search_schema import SearchSchema
|
||||
from ahriman.web.schemas.sse_schema import SSESchema
|
||||
from ahriman.web.schemas.status_schema import StatusSchema
|
||||
from ahriman.web.schemas.update_flags_schema import UpdateFlagsSchema
|
||||
from ahriman.web.schemas.worker_schema import WorkerSchema
|
||||
|
||||
33
src/ahriman/web/schemas/event_bus_filter_schema.py
Normal file
33
src/ahriman/web/schemas/event_bus_filter_schema.py
Normal file
@@ -0,0 +1,33 @@
|
||||
#
|
||||
# Copyright (c) 2021-2026 ahriman team.
|
||||
#
|
||||
# This file is part of ahriman
|
||||
# (see https://github.com/arcan1s/ahriman).
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
from ahriman.models.event import EventType
|
||||
from ahriman.web.apispec import fields
|
||||
from ahriman.web.schemas.repository_id_schema import RepositoryIdSchema
|
||||
|
||||
|
||||
class EventBusFilterSchema(RepositoryIdSchema):
|
||||
"""
|
||||
request event bus filter schema
|
||||
"""
|
||||
|
||||
event = fields.List(fields.String(), metadata={
|
||||
"description": "Event type filter",
|
||||
"example": [EventType.PackageUpdated],
|
||||
})
|
||||
35
src/ahriman/web/schemas/sse_schema.py
Normal file
35
src/ahriman/web/schemas/sse_schema.py
Normal file
@@ -0,0 +1,35 @@
|
||||
#
|
||||
# Copyright (c) 2021-2026 ahriman team.
|
||||
#
|
||||
# This file is part of ahriman
|
||||
# (see https://github.com/arcan1s/ahriman).
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
from ahriman.models.event import EventType
|
||||
from ahriman.web.apispec import Schema, fields
|
||||
|
||||
|
||||
class SSESchema(Schema):
|
||||
"""
|
||||
response SSE schema
|
||||
"""
|
||||
|
||||
event = fields.String(required=True, metadata={
|
||||
"description": "Event type",
|
||||
"example": EventType.PackageUpdated,
|
||||
})
|
||||
data = fields.Dict(keys=fields.String(), values=fields.Raw(), metadata={
|
||||
"description": "Event data",
|
||||
})
|
||||
@@ -39,7 +39,7 @@ async def server_info(view: BaseView) -> dict[str, Any]:
|
||||
view(BaseView): view of the request
|
||||
|
||||
Returns:
|
||||
dict[str, Any]: server info as a json response
|
||||
dict[str, Any]: server info as a JSON response
|
||||
"""
|
||||
autorefresh_intervals = [
|
||||
{
|
||||
|
||||
@@ -57,7 +57,7 @@ class DocsView(BaseView):
|
||||
@aiohttp_jinja2.template("api.jinja2")
|
||||
async def get(self) -> dict[str, Any]:
|
||||
"""
|
||||
return static docs html
|
||||
return static docs HTML
|
||||
|
||||
Returns:
|
||||
dict[str, Any]: parameters for jinja template
|
||||
|
||||
@@ -60,7 +60,7 @@ class SwaggerView(BaseView):
|
||||
get api specification
|
||||
|
||||
Returns:
|
||||
Response: 200 with json api specification
|
||||
Response: 200 with JSON api specification
|
||||
"""
|
||||
spec = self.request.app["swagger_dict"]
|
||||
is_body_parameter: Callable[[dict[str, str]], bool] = lambda p: p["in"] == "body" or p["in"] == "formData"
|
||||
|
||||
@@ -169,7 +169,7 @@ class BaseView(View, CorsViewMixin):
|
||||
filter and convert data and return :class:`aiohttp.web.Response` object
|
||||
|
||||
Args:
|
||||
data(dict[str, Any] | list[Any]): response in json format
|
||||
data(dict[str, Any] | list[Any]): response in JSON format
|
||||
**kwargs(Any): keyword arguments for :func:`aiohttp.web.json_response` function
|
||||
|
||||
Returns:
|
||||
|
||||
103
src/ahriman/web/views/v1/auditlog/event_bus.py
Normal file
103
src/ahriman/web/views/v1/auditlog/event_bus.py
Normal file
@@ -0,0 +1,103 @@
|
||||
#
|
||||
# Copyright (c) 2021-2026 ahriman team.
|
||||
#
|
||||
# This file is part of ahriman
|
||||
# (see https://github.com/arcan1s/ahriman).
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
import json
|
||||
|
||||
from aiohttp.web import HTTPBadRequest, StreamResponse
|
||||
from aiohttp_sse import EventSourceResponse, sse_response
|
||||
from asyncio import Queue, QueueShutDown, wait_for
|
||||
from typing import ClassVar
|
||||
|
||||
from ahriman.core.status.event_bus import SSEvent
|
||||
from ahriman.models.event import EventType
|
||||
from ahriman.models.user_access import UserAccess
|
||||
from ahriman.web.apispec.decorators import apidocs
|
||||
from ahriman.web.schemas import EventBusFilterSchema, SSESchema
|
||||
from ahriman.web.views.base import BaseView
|
||||
|
||||
|
||||
class EventBusView(BaseView):
|
||||
"""
|
||||
event bus SSE view
|
||||
|
||||
Attributes:
|
||||
GET_PERMISSION(UserAccess): (class attribute) get permissions of self
|
||||
"""
|
||||
|
||||
GET_PERMISSION: ClassVar[UserAccess] = UserAccess.Full
|
||||
ROUTES = ["/api/v1/events/stream"]
|
||||
|
||||
@staticmethod
|
||||
async def _run(response: EventSourceResponse, queue: Queue[SSEvent | None]) -> None:
|
||||
"""
|
||||
read events from queue and send them to the client
|
||||
|
||||
Args:
|
||||
response(EventSourceResponse): SSE response instance
|
||||
queue(Queue[SSEvent | None]): subscriber queue
|
||||
"""
|
||||
while response.is_connected():
|
||||
try:
|
||||
message = await wait_for(queue.get(), timeout=response.ping_interval)
|
||||
except TimeoutError:
|
||||
continue
|
||||
|
||||
if message is None:
|
||||
break # terminate queue on sentinel event
|
||||
event_type, data = message
|
||||
|
||||
await response.send(json.dumps(data), event=event_type)
|
||||
|
||||
@apidocs(
|
||||
tags=["Audit log"],
|
||||
summary="Live updates",
|
||||
description="Stream live updates via SSE",
|
||||
permission=GET_PERMISSION,
|
||||
error_400_enabled=True,
|
||||
error_404_description="Repository is unknown",
|
||||
schema=SSESchema(many=True),
|
||||
query_schema=EventBusFilterSchema,
|
||||
)
|
||||
async def get(self) -> StreamResponse:
|
||||
"""
|
||||
subscribe on updates
|
||||
|
||||
Returns:
|
||||
StreamResponse: 200 with streaming updates
|
||||
|
||||
Raises:
|
||||
HTTPBadRequest: if invalid event type is supplied
|
||||
"""
|
||||
try:
|
||||
topics = [EventType(event) for event in self.request.query.getall("event", [])] or None
|
||||
except ValueError as ex:
|
||||
raise HTTPBadRequest(reason=str(ex))
|
||||
event_bus = self.service().event_bus
|
||||
|
||||
async with sse_response(self.request) as response:
|
||||
subscription_id, queue = await event_bus.subscribe(topics)
|
||||
|
||||
try:
|
||||
await self._run(response, queue)
|
||||
except (ConnectionResetError, QueueShutDown):
|
||||
pass
|
||||
finally:
|
||||
await event_bus.unsubscribe(subscription_id)
|
||||
|
||||
return response
|
||||
@@ -67,7 +67,7 @@ class EventsView(BaseView):
|
||||
except ValueError as ex:
|
||||
raise HTTPBadRequest(reason=str(ex))
|
||||
|
||||
events = self.service().event_get(event, object_id, from_date, to_date, limit, offset)
|
||||
events = await self.service().event_get(event, object_id, from_date, to_date, limit, offset)
|
||||
response = [event.view() for event in events]
|
||||
|
||||
return self.json_response(response)
|
||||
@@ -94,6 +94,6 @@ class EventsView(BaseView):
|
||||
except Exception as ex:
|
||||
raise HTTPBadRequest(reason=str(ex))
|
||||
|
||||
self.service().event_add(event)
|
||||
await self.service().event_add(event)
|
||||
|
||||
raise HTTPNoContent
|
||||
|
||||
@@ -60,6 +60,6 @@ class Archives(StatusViewGuard, BaseView):
|
||||
"""
|
||||
package_base = self.request.match_info["package"]
|
||||
|
||||
archives = self.service(package_base=package_base).package_archives(package_base)
|
||||
archives = await self.service(package_base=package_base).package_archives(package_base)
|
||||
|
||||
return self.json_response([archive.view() for archive in archives])
|
||||
|
||||
@@ -63,7 +63,7 @@ class ChangesView(StatusViewGuard, BaseView):
|
||||
"""
|
||||
package_base = self.request.match_info["package"]
|
||||
|
||||
changes = self.service(package_base=package_base).package_changes_get(package_base)
|
||||
changes = await self.service(package_base=package_base).package_changes_get(package_base)
|
||||
|
||||
return self.json_response(changes.view())
|
||||
|
||||
@@ -97,6 +97,6 @@ class ChangesView(StatusViewGuard, BaseView):
|
||||
raise HTTPBadRequest(reason=str(ex))
|
||||
|
||||
changes = Changes(last_commit_sha, change, pkgbuild)
|
||||
self.service().package_changes_update(package_base, changes)
|
||||
await self.service().package_changes_update(package_base, changes)
|
||||
|
||||
raise HTTPNoContent
|
||||
|
||||
@@ -63,7 +63,7 @@ class DependenciesView(StatusViewGuard, BaseView):
|
||||
"""
|
||||
package_base = self.request.match_info["package"]
|
||||
|
||||
dependencies = self.service(package_base=package_base).package_dependencies_get(package_base)
|
||||
dependencies = await self.service(package_base=package_base).package_dependencies_get(package_base)
|
||||
|
||||
return self.json_response(dependencies.view())
|
||||
|
||||
@@ -95,6 +95,6 @@ class DependenciesView(StatusViewGuard, BaseView):
|
||||
except Exception as ex:
|
||||
raise HTTPBadRequest(reason=str(ex))
|
||||
|
||||
self.service(package_base=package_base).package_dependencies_update(package_base, dependencies)
|
||||
await self.service(package_base=package_base).package_dependencies_update(package_base, dependencies)
|
||||
|
||||
raise HTTPNoContent
|
||||
|
||||
@@ -68,7 +68,7 @@ class HoldView(StatusViewGuard, BaseView):
|
||||
raise HTTPBadRequest(reason=str(ex))
|
||||
|
||||
try:
|
||||
self.service().package_hold_update(package_base, enabled=is_held)
|
||||
await self.service().package_hold_update(package_base, enabled=is_held)
|
||||
except UnknownPackageError:
|
||||
raise HTTPNotFound(reason=f"Package {package_base} is unknown")
|
||||
|
||||
|
||||
@@ -62,7 +62,7 @@ class LogsView(StatusViewGuard, BaseView):
|
||||
"""
|
||||
package_base = self.request.match_info["package"]
|
||||
version = self.request.query.get("version")
|
||||
self.service().package_logs_remove(package_base, version)
|
||||
await self.service().package_logs_remove(package_base, version)
|
||||
|
||||
raise HTTPNoContent
|
||||
|
||||
@@ -89,8 +89,8 @@ class LogsView(StatusViewGuard, BaseView):
|
||||
package_base = self.request.match_info["package"]
|
||||
|
||||
try:
|
||||
_, status = self.service().package_get(package_base)
|
||||
logs = self.service(package_base=package_base).package_logs_get(package_base, None, None, -1, 0)
|
||||
_, status = await self.service().package_get(package_base)
|
||||
logs = await self.service(package_base=package_base).package_logs_get(package_base, None, None, -1, 0)
|
||||
except UnknownPackageError:
|
||||
raise HTTPNotFound(reason=f"Package {package_base} is unknown")
|
||||
|
||||
@@ -127,6 +127,6 @@ class LogsView(StatusViewGuard, BaseView):
|
||||
except Exception as ex:
|
||||
raise HTTPBadRequest(reason=str(ex))
|
||||
|
||||
self.service().package_logs_add(log_record)
|
||||
await self.service().package_logs_add(log_record)
|
||||
|
||||
raise HTTPNoContent
|
||||
|
||||
@@ -66,7 +66,7 @@ class PackageView(StatusViewGuard, BaseView):
|
||||
HTTPNoContent: on success response
|
||||
"""
|
||||
package_base = self.request.match_info["package"]
|
||||
self.service().package_remove(package_base)
|
||||
await self.service().package_remove(package_base)
|
||||
|
||||
raise HTTPNoContent
|
||||
|
||||
@@ -94,7 +94,7 @@ class PackageView(StatusViewGuard, BaseView):
|
||||
repository_id = self.repository_id()
|
||||
|
||||
try:
|
||||
package, status = self.service(repository_id).package_get(package_base)
|
||||
package, status = await self.service(repository_id).package_get(package_base)
|
||||
except UnknownPackageError:
|
||||
raise HTTPNotFound(reason=f"Package {package_base} is unknown")
|
||||
|
||||
@@ -137,9 +137,9 @@ class PackageView(StatusViewGuard, BaseView):
|
||||
|
||||
try:
|
||||
if package is None:
|
||||
self.service().package_status_update(package_base, status)
|
||||
await self.service().package_status_update(package_base, status)
|
||||
else:
|
||||
self.service().package_update(package, status)
|
||||
await self.service().package_update(package, status)
|
||||
except UnknownPackageError:
|
||||
raise HTTPBadRequest(reason=f"Package {package_base} is unknown, but no package body set")
|
||||
|
||||
|
||||
@@ -67,7 +67,7 @@ class PackagesView(StatusViewGuard, BaseView):
|
||||
stop = offset + limit if limit >= 0 else None
|
||||
|
||||
repository_id = self.repository_id()
|
||||
packages = self.service(repository_id).packages
|
||||
packages = await self.service(repository_id).packages()
|
||||
|
||||
comparator: Callable[[tuple[Package, BuildStatus]], Comparable] = lambda items: items[0].base
|
||||
response = [
|
||||
@@ -95,6 +95,6 @@ class PackagesView(StatusViewGuard, BaseView):
|
||||
Raises:
|
||||
HTTPNoContent: on success response
|
||||
"""
|
||||
self.service().load()
|
||||
await self.service().load()
|
||||
|
||||
raise HTTPNoContent
|
||||
|
||||
@@ -57,7 +57,7 @@ class PatchView(StatusViewGuard, BaseView):
|
||||
package_base = self.request.match_info["package"]
|
||||
variable = self.request.match_info["patch"]
|
||||
|
||||
self.service().package_patches_remove(package_base, variable)
|
||||
await self.service().package_patches_remove(package_base, variable)
|
||||
|
||||
raise HTTPNoContent
|
||||
|
||||
@@ -83,7 +83,7 @@ class PatchView(StatusViewGuard, BaseView):
|
||||
package_base = self.request.match_info["package"]
|
||||
variable = self.request.match_info["patch"]
|
||||
|
||||
patches = self.service().package_patches_get(package_base, variable)
|
||||
patches = await self.service().package_patches_get(package_base, variable)
|
||||
|
||||
selected = next((patch for patch in patches if patch.key == variable), None)
|
||||
if selected is None:
|
||||
|
||||
@@ -57,7 +57,7 @@ class PatchesView(StatusViewGuard, BaseView):
|
||||
Response: 200 with package patches on success
|
||||
"""
|
||||
package_base = self.request.match_info["package"]
|
||||
patches = self.service().package_patches_get(package_base, None)
|
||||
patches = await self.service().package_patches_get(package_base, None)
|
||||
|
||||
response = [patch.view() for patch in patches]
|
||||
return self.json_response(response)
|
||||
@@ -88,6 +88,6 @@ class PatchesView(StatusViewGuard, BaseView):
|
||||
except Exception as ex:
|
||||
raise HTTPBadRequest(reason=str(ex))
|
||||
|
||||
self.service().package_patches_update(package_base, PkgbuildPatch.parse(key, value))
|
||||
await self.service().package_patches_update(package_base, PkgbuildPatch.parse(key, value))
|
||||
|
||||
raise HTTPNoContent
|
||||
|
||||
@@ -59,6 +59,6 @@ class LogsView(BaseView):
|
||||
except Exception as ex:
|
||||
raise HTTPBadRequest(reason=str(ex))
|
||||
|
||||
self.service().logs_rotate(keep_last_records)
|
||||
await self.service().logs_rotate(keep_last_records)
|
||||
|
||||
raise HTTPNoContent
|
||||
|
||||
@@ -62,7 +62,7 @@ class StatusView(StatusViewGuard, BaseView):
|
||||
Response: 200 with service status object
|
||||
"""
|
||||
repository_id = self.repository_id()
|
||||
packages = self.service(repository_id).packages
|
||||
packages = await self.service(repository_id).packages()
|
||||
counters = Counters.from_packages(packages)
|
||||
stats = RepositoryStats.from_packages([package for package, _ in packages])
|
||||
|
||||
@@ -101,6 +101,6 @@ class StatusView(StatusViewGuard, BaseView):
|
||||
except Exception as ex:
|
||||
raise HTTPBadRequest(reason=str(ex))
|
||||
|
||||
self.service().status_update(status)
|
||||
await self.service().status_update(status)
|
||||
|
||||
raise HTTPNoContent
|
||||
|
||||
@@ -67,7 +67,7 @@ class LogsView(StatusViewGuard, BaseView):
|
||||
version = self.request.query.get("version", None)
|
||||
process = self.request.query.get("process_id", None)
|
||||
|
||||
logs = self.service(package_base=package_base).package_logs_get(package_base, version, process, limit, offset)
|
||||
logs = await self.service(package_base=package_base).package_logs_get(package_base, version, process, limit, offset)
|
||||
|
||||
head = self.request.query.get("head", "false")
|
||||
# pylint: disable=protected-access
|
||||
|
||||
@@ -33,11 +33,13 @@ from ahriman.core.exceptions import InitializeError
|
||||
from ahriman.core.repository.package_info import PackageInfo
|
||||
from ahriman.core.spawn import Spawn
|
||||
from ahriman.core.status import Client
|
||||
from ahriman.core.status.event_bus import EventBus
|
||||
from ahriman.core.status.watcher import Watcher
|
||||
from ahriman.models.repository_id import RepositoryId
|
||||
from ahriman.web.apispec.info import setup_apispec
|
||||
from ahriman.web.cors import setup_cors
|
||||
from ahriman.web.keys import AuthKey, ConfigurationKey, SpawnKey, WatcherKey, WorkersKey
|
||||
from ahriman.web.middlewares.etag_handler import etag_handler
|
||||
from ahriman.web.middlewares.exception_handler import exception_handler
|
||||
from ahriman.web.middlewares.metrics_handler import metrics_handler
|
||||
from ahriman.web.middlewares.request_id_handler import request_id_handler
|
||||
@@ -107,7 +109,9 @@ def _create_watcher(path: Path, repository_id: RepositoryId) -> Watcher:
|
||||
package_info.reporter = client
|
||||
package_info.repository_id = repository_id
|
||||
|
||||
return Watcher(client, package_info)
|
||||
event_bus = EventBus(configuration.getint("web", "max_queue_size", fallback=0))
|
||||
|
||||
return Watcher(client, package_info, event_bus)
|
||||
|
||||
|
||||
async def _on_shutdown(application: Application) -> None:
|
||||
@@ -117,6 +121,8 @@ async def _on_shutdown(application: Application) -> None:
|
||||
Args:
|
||||
application(Application): web application instance
|
||||
"""
|
||||
for watcher in application[WatcherKey].values():
|
||||
await watcher.shutdown()
|
||||
application.logger.warning("server terminated")
|
||||
|
||||
|
||||
@@ -134,7 +140,7 @@ async def _on_startup(application: Application) -> None:
|
||||
|
||||
try:
|
||||
for watcher in application[WatcherKey].values():
|
||||
watcher.load()
|
||||
await watcher.load()
|
||||
except Exception:
|
||||
message = "could not load packages"
|
||||
application.logger.exception(message)
|
||||
@@ -181,13 +187,14 @@ def setup_server(configuration: Configuration, spawner: Spawn, repositories: lis
|
||||
application.middlewares.append(normalize_path_middleware(append_slash=False, remove_slash=True))
|
||||
application.middlewares.append(request_id_handler())
|
||||
application.middlewares.append(exception_handler(application.logger))
|
||||
application.middlewares.append(etag_handler())
|
||||
application.middlewares.append(metrics_handler())
|
||||
|
||||
application.logger.info("setup routes")
|
||||
setup_routes(application, configuration)
|
||||
|
||||
application.logger.info("setup CORS")
|
||||
setup_cors(application)
|
||||
setup_cors(application, configuration)
|
||||
|
||||
application.logger.info("setup templates")
|
||||
loader = jinja2.FileSystemLoader(searchpath=configuration.getpathlist("web", "templates"))
|
||||
|
||||
@@ -19,6 +19,7 @@ from ahriman.core.repository import Repository
|
||||
from ahriman.core.repository.package_info import PackageInfo
|
||||
from ahriman.core.spawn import Spawn
|
||||
from ahriman.core.status import Client
|
||||
from ahriman.core.status.event_bus import EventBus
|
||||
from ahriman.core.status.watcher import Watcher
|
||||
from ahriman.models.aur_package import AURPackage
|
||||
from ahriman.models.build_status import BuildStatus, BuildStatusEnum
|
||||
@@ -690,4 +691,5 @@ def watcher(local_client: Client) -> Watcher:
|
||||
Watcher: package status watcher test instance
|
||||
"""
|
||||
package_info = PackageInfo()
|
||||
return Watcher(local_client, package_info)
|
||||
event_bus = EventBus(0)
|
||||
return Watcher(local_client, package_info, event_bus)
|
||||
|
||||
@@ -605,3 +605,12 @@ def test_read(sources: Sources, mocker: MockerFixture) -> None:
|
||||
check_output_mock = mocker.patch("ahriman.core.build_tools.sources.check_output", return_value="content")
|
||||
assert sources.read(Path("local"), "sha", Path("PKGBUILD")) == "content"
|
||||
check_output_mock.assert_called_once()
|
||||
|
||||
|
||||
def test_read_failed(sources: Sources, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must return None in case if file cannot be read from commit
|
||||
"""
|
||||
mocker.patch("ahriman.core.build_tools.sources.check_output",
|
||||
side_effect=CalledProcessError(1, ["command"], "error"))
|
||||
assert sources.read(Path("local"), "sha", Path("PKGBUILD")) is None
|
||||
|
||||
@@ -53,3 +53,12 @@ def test_changes_insert_remove_full(database: SQLite, package_ahriman: Package,
|
||||
assert database.changes_get(package_ahriman.base).changes is None
|
||||
assert database.changes_get(package_python_schedule.base).changes is None
|
||||
assert database.changes_get(package_ahriman.base, RepositoryId("i686", database._repository_id.name)) == changes2
|
||||
|
||||
|
||||
def test_changes_insert_pkgbuild_preserve(database: SQLite, package_ahriman: Package) -> None:
|
||||
"""
|
||||
must preserve existing pkgbuild when inserting changes without pkgbuild
|
||||
"""
|
||||
database.changes_insert(package_ahriman.base, Changes("sha1", "change1", "pkgbuild1"))
|
||||
database.changes_insert(package_ahriman.base, Changes("sha2", "change2", None))
|
||||
assert database.changes_get(package_ahriman.base) == Changes("sha2", "change2", "pkgbuild1")
|
||||
|
||||
@@ -2,6 +2,7 @@ import pytest
|
||||
|
||||
from ahriman.core.configuration import Configuration
|
||||
from ahriman.core.status import Client
|
||||
from ahriman.core.status.event_bus import EventBus
|
||||
from ahriman.core.status.web_client import WebClient
|
||||
|
||||
|
||||
@@ -16,6 +17,17 @@ def client() -> Client:
|
||||
return Client()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def event_bus() -> EventBus:
|
||||
"""
|
||||
fixture for event bus
|
||||
|
||||
Returns:
|
||||
EventBus: event bus test instance
|
||||
"""
|
||||
return EventBus(0)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def web_client(configuration: Configuration) -> WebClient:
|
||||
"""
|
||||
|
||||
107
tests/ahriman/core/status/test_event_bus.py
Normal file
107
tests/ahriman/core/status/test_event_bus.py
Normal file
@@ -0,0 +1,107 @@
|
||||
import pytest
|
||||
|
||||
from ahriman.core.status.event_bus import EventBus
|
||||
from ahriman.models.event import EventType
|
||||
from ahriman.models.package import Package
|
||||
|
||||
|
||||
async def test_broadcast(event_bus: EventBus, package_ahriman: Package) -> None:
|
||||
"""
|
||||
must broadcast event to all subscribers
|
||||
"""
|
||||
_, queue = await event_bus.subscribe()
|
||||
await event_bus.broadcast(EventType.PackageUpdated, package_ahriman.base, version=package_ahriman.version)
|
||||
|
||||
message = queue.get_nowait()
|
||||
assert message == (
|
||||
EventType.PackageUpdated,
|
||||
{"object_id": package_ahriman.base, "version": package_ahriman.version},
|
||||
)
|
||||
|
||||
|
||||
async def test_broadcast_with_topics(event_bus: EventBus, package_ahriman: Package) -> None:
|
||||
"""
|
||||
must broadcast event to subscribers with matching topics
|
||||
"""
|
||||
_, queue = await event_bus.subscribe([EventType.PackageUpdated])
|
||||
await event_bus.broadcast(EventType.PackageUpdated, package_ahriman.base)
|
||||
assert not queue.empty()
|
||||
|
||||
|
||||
async def test_broadcast_topic_isolation(event_bus: EventBus, package_ahriman: Package) -> None:
|
||||
"""
|
||||
must not broadcast event to subscribers with non-matching topics
|
||||
"""
|
||||
_, queue = await event_bus.subscribe([EventType.BuildLog])
|
||||
await event_bus.broadcast(EventType.PackageUpdated, package_ahriman.base)
|
||||
assert queue.empty()
|
||||
|
||||
|
||||
async def test_broadcast_queue_full(event_bus: EventBus, package_ahriman: Package) -> None:
|
||||
"""
|
||||
must discard message to slow subscriber
|
||||
"""
|
||||
event_bus.max_size = 1
|
||||
_, queue = await event_bus.subscribe()
|
||||
|
||||
await event_bus.broadcast(EventType.PackageUpdated, package_ahriman.base)
|
||||
await event_bus.broadcast(EventType.PackageRemoved, package_ahriman.base)
|
||||
assert queue.qsize() == 1
|
||||
|
||||
|
||||
async def test_shutdown(event_bus: EventBus) -> None:
|
||||
"""
|
||||
must send sentinel to all subscribers on shutdown
|
||||
"""
|
||||
_, queue = await event_bus.subscribe()
|
||||
|
||||
await event_bus.shutdown()
|
||||
message = queue.get_nowait()
|
||||
assert message is None
|
||||
|
||||
|
||||
async def test_shutdown_queue_full(event_bus: EventBus, package_ahriman: Package) -> None:
|
||||
"""
|
||||
must handle shutdown when queue is full
|
||||
"""
|
||||
event_bus.max_size = 1
|
||||
_, queue = await event_bus.subscribe()
|
||||
|
||||
await event_bus.broadcast(EventType.PackageUpdated, package_ahriman.base)
|
||||
await event_bus.shutdown()
|
||||
|
||||
|
||||
async def test_subscribe(event_bus: EventBus) -> None:
|
||||
"""
|
||||
must register new subscriber
|
||||
"""
|
||||
subscriber_id, queue = await event_bus.subscribe()
|
||||
|
||||
assert subscriber_id
|
||||
assert queue.empty()
|
||||
assert subscriber_id in event_bus._subscribers
|
||||
|
||||
|
||||
async def test_subscribe_with_topics(event_bus: EventBus) -> None:
|
||||
"""
|
||||
must register subscriber with topic filter
|
||||
"""
|
||||
subscriber_id, _ = await event_bus.subscribe([EventType.BuildLog])
|
||||
topics, _ = event_bus._subscribers[subscriber_id]
|
||||
assert topics == [EventType.BuildLog]
|
||||
|
||||
|
||||
async def test_unsubscribe(event_bus: EventBus) -> None:
|
||||
"""
|
||||
must remove subscriber
|
||||
"""
|
||||
subscriber_id, _ = await event_bus.subscribe()
|
||||
await event_bus.unsubscribe(subscriber_id)
|
||||
assert subscriber_id not in event_bus._subscribers
|
||||
|
||||
|
||||
async def test_unsubscribe_unknown(event_bus: EventBus) -> None:
|
||||
"""
|
||||
must not fail on unknown subscriber removal
|
||||
"""
|
||||
await event_bus.unsubscribe("unknown")
|
||||
@@ -5,34 +5,53 @@ from pytest_mock import MockerFixture
|
||||
from ahriman.core.exceptions import UnknownPackageError
|
||||
from ahriman.core.status.watcher import Watcher
|
||||
from ahriman.models.build_status import BuildStatus, BuildStatusEnum
|
||||
from ahriman.models.changes import Changes
|
||||
from ahriman.models.dependencies import Dependencies
|
||||
from ahriman.models.event import Event, EventType
|
||||
from ahriman.models.log_record import LogRecord
|
||||
from ahriman.models.log_record_id import LogRecordId
|
||||
from ahriman.models.package import Package
|
||||
from ahriman.models.pkgbuild_patch import PkgbuildPatch
|
||||
|
||||
|
||||
def test_packages(watcher: Watcher, package_ahriman: Package) -> None:
|
||||
async def test_event_add(watcher: Watcher, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must return list of available packages
|
||||
must create new event
|
||||
"""
|
||||
assert not watcher.packages
|
||||
event = Event("event", "object")
|
||||
cache_mock = mocker.patch("ahriman.core.status.local_client.LocalClient.event_add")
|
||||
|
||||
watcher._known = {package_ahriman.base: (package_ahriman, BuildStatus())}
|
||||
assert watcher.packages
|
||||
await watcher.event_add(event)
|
||||
cache_mock.assert_called_once_with(event)
|
||||
|
||||
|
||||
def test_load(watcher: Watcher, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
async def test_event_get(watcher: Watcher, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must retrieve events
|
||||
"""
|
||||
event = Event("event", "object")
|
||||
cache_mock = mocker.patch("ahriman.core.status.local_client.LocalClient.event_get", return_value=[event])
|
||||
|
||||
result = await watcher.event_get(None, None)
|
||||
assert result == [event]
|
||||
cache_mock.assert_called_once_with(None, None, None, None, -1, 0)
|
||||
|
||||
|
||||
async def test_load(watcher: Watcher, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must correctly load packages
|
||||
"""
|
||||
cache_mock = mocker.patch("ahriman.core.status.local_client.LocalClient.package_get",
|
||||
return_value=[(package_ahriman, BuildStatus())])
|
||||
|
||||
watcher.load()
|
||||
await watcher.load()
|
||||
cache_mock.assert_called_once_with(None)
|
||||
package, status = watcher._known[package_ahriman.base]
|
||||
assert package == package_ahriman
|
||||
assert status.status == BuildStatusEnum.Unknown
|
||||
|
||||
|
||||
def test_load_known(watcher: Watcher, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
async def test_load_known(watcher: Watcher, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must correctly load packages with known statuses
|
||||
"""
|
||||
@@ -40,147 +59,307 @@ def test_load_known(watcher: Watcher, package_ahriman: Package, mocker: MockerFi
|
||||
mocker.patch("ahriman.core.status.local_client.LocalClient.package_get", return_value=[(package_ahriman, status)])
|
||||
watcher._known = {package_ahriman.base: (package_ahriman, status)}
|
||||
|
||||
watcher.load()
|
||||
await watcher.load()
|
||||
_, status = watcher._known[package_ahriman.base]
|
||||
assert status.status == BuildStatusEnum.Success
|
||||
|
||||
|
||||
def test_package_archives(watcher: Watcher, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
async def test_logs_rotate(watcher: Watcher, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must rotate logs
|
||||
"""
|
||||
cache_mock = mocker.patch("ahriman.core.status.local_client.LocalClient.logs_rotate")
|
||||
await watcher.logs_rotate(42)
|
||||
cache_mock.assert_called_once_with(42)
|
||||
|
||||
|
||||
async def test_package_archives(watcher: Watcher, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must return package archives from package info
|
||||
"""
|
||||
archives_mock = mocker.patch("ahriman.core.repository.package_info.PackageInfo.package_archives",
|
||||
return_value=[package_ahriman])
|
||||
|
||||
result = watcher.package_archives(package_ahriman.base)
|
||||
result = await watcher.package_archives(package_ahriman.base)
|
||||
assert result == [package_ahriman]
|
||||
archives_mock.assert_called_once_with(package_ahriman.base)
|
||||
|
||||
|
||||
def test_package_get(watcher: Watcher, package_ahriman: Package) -> None:
|
||||
async def test_package_get(watcher: Watcher, package_ahriman: Package) -> None:
|
||||
"""
|
||||
must return package status
|
||||
"""
|
||||
watcher._known = {package_ahriman.base: (package_ahriman, BuildStatus())}
|
||||
package, status = watcher.package_get(package_ahriman.base)
|
||||
package, status = await watcher.package_get(package_ahriman.base)
|
||||
assert package == package_ahriman
|
||||
assert status.status == BuildStatusEnum.Unknown
|
||||
|
||||
|
||||
def test_package_get_failed(watcher: Watcher, package_ahriman: Package) -> None:
|
||||
async def test_package_get_failed(watcher: Watcher, package_ahriman: Package) -> None:
|
||||
"""
|
||||
must fail on unknown package
|
||||
"""
|
||||
with pytest.raises(UnknownPackageError):
|
||||
watcher.package_get(package_ahriman.base)
|
||||
await watcher.package_get(package_ahriman.base)
|
||||
|
||||
|
||||
def test_package_hold_update(watcher: Watcher, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
async def test_package_changes_get(watcher: Watcher, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must return package changes
|
||||
"""
|
||||
changes = Changes("sha")
|
||||
cache_mock = mocker.patch("ahriman.core.status.local_client.LocalClient.package_changes_get",
|
||||
return_value=changes)
|
||||
|
||||
assert await watcher.package_changes_get(package_ahriman.base) == changes
|
||||
cache_mock.assert_called_once_with(package_ahriman.base)
|
||||
|
||||
|
||||
async def test_package_changes_update(watcher: Watcher, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must update package changes
|
||||
"""
|
||||
changes = Changes("sha")
|
||||
cache_mock = mocker.patch("ahriman.core.status.local_client.LocalClient.package_changes_update")
|
||||
|
||||
await watcher.package_changes_update(package_ahriman.base, changes)
|
||||
cache_mock.assert_called_once_with(package_ahriman.base, changes)
|
||||
|
||||
|
||||
async def test_package_dependencies_get(watcher: Watcher, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must return package dependencies
|
||||
"""
|
||||
dependencies = Dependencies({"path": [package_ahriman.base]})
|
||||
cache_mock = mocker.patch("ahriman.core.status.local_client.LocalClient.package_dependencies_get",
|
||||
return_value=dependencies)
|
||||
|
||||
assert await watcher.package_dependencies_get(package_ahriman.base) == dependencies
|
||||
cache_mock.assert_called_once_with(package_ahriman.base)
|
||||
|
||||
|
||||
async def test_package_dependencies_update(watcher: Watcher, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must update package dependencies
|
||||
"""
|
||||
dependencies = Dependencies({"path": [package_ahriman.base]})
|
||||
cache_mock = mocker.patch("ahriman.core.status.local_client.LocalClient.package_dependencies_update")
|
||||
|
||||
await watcher.package_dependencies_update(package_ahriman.base, dependencies)
|
||||
cache_mock.assert_called_once_with(package_ahriman.base, dependencies)
|
||||
|
||||
|
||||
async def test_package_hold_update(watcher: Watcher, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must update package hold status
|
||||
"""
|
||||
cache_mock = mocker.patch("ahriman.core.status.local_client.LocalClient.package_hold_update")
|
||||
broadcast_mock = mocker.patch("ahriman.core.status.event_bus.EventBus.broadcast")
|
||||
watcher._known = {package_ahriman.base: (package_ahriman, BuildStatus())}
|
||||
|
||||
watcher.package_hold_update(package_ahriman.base, enabled=True)
|
||||
await watcher.package_hold_update(package_ahriman.base, enabled=True)
|
||||
cache_mock.assert_called_once_with(package_ahriman.base, enabled=True)
|
||||
_, status = watcher._known[package_ahriman.base]
|
||||
assert status.is_held is True
|
||||
broadcast_mock.assert_called_once_with(EventType.PackageHeld, package_ahriman.base, is_held=True)
|
||||
|
||||
|
||||
def test_package_hold_update_unknown(watcher: Watcher, package_ahriman: Package) -> None:
|
||||
async def test_package_hold_update_unknown(watcher: Watcher, package_ahriman: Package) -> None:
|
||||
"""
|
||||
must fail on unknown package hold update
|
||||
"""
|
||||
with pytest.raises(UnknownPackageError):
|
||||
watcher.package_hold_update(package_ahriman.base, enabled=True)
|
||||
await watcher.package_hold_update(package_ahriman.base, enabled=True)
|
||||
|
||||
|
||||
def test_package_remove(watcher: Watcher, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
async def test_package_logs_add(watcher: Watcher, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must post log record
|
||||
"""
|
||||
log_record = LogRecord(LogRecordId(package_ahriman.base, "1.0.0"), 42.0, "message")
|
||||
cache_mock = mocker.patch("ahriman.core.status.local_client.LocalClient.package_logs_add")
|
||||
broadcast_mock = mocker.patch("ahriman.core.status.event_bus.EventBus.broadcast")
|
||||
|
||||
await watcher.package_logs_add(log_record)
|
||||
cache_mock.assert_called_once_with(log_record)
|
||||
broadcast_mock.assert_called_once_with(EventType.BuildLog, package_ahriman.base, **log_record.view())
|
||||
|
||||
|
||||
async def test_package_logs_get(watcher: Watcher, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must return package logs
|
||||
"""
|
||||
log_record = LogRecord(LogRecordId(package_ahriman.base, "1.0.0"), 42.0, "message")
|
||||
cache_mock = mocker.patch("ahriman.core.status.local_client.LocalClient.package_logs_get",
|
||||
return_value=[log_record])
|
||||
|
||||
assert await watcher.package_logs_get(package_ahriman.base) == [log_record]
|
||||
cache_mock.assert_called_once_with(package_ahriman.base, None, None, -1, 0)
|
||||
|
||||
|
||||
async def test_package_logs_remove(watcher: Watcher, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must remove package logs
|
||||
"""
|
||||
cache_mock = mocker.patch("ahriman.core.status.local_client.LocalClient.package_logs_remove")
|
||||
await watcher.package_logs_remove(package_ahriman.base, None)
|
||||
cache_mock.assert_called_once_with(package_ahriman.base, None)
|
||||
|
||||
|
||||
async def test_package_patches_get(watcher: Watcher, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must return package patches
|
||||
"""
|
||||
patch = PkgbuildPatch("key", "value")
|
||||
cache_mock = mocker.patch("ahriman.core.status.local_client.LocalClient.package_patches_get", return_value=[patch])
|
||||
|
||||
assert await watcher.package_patches_get(package_ahriman.base, None) == [patch]
|
||||
cache_mock.assert_called_once_with(package_ahriman.base, None)
|
||||
|
||||
|
||||
async def test_package_patches_remove(watcher: Watcher, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must remove package patches
|
||||
"""
|
||||
cache_mock = mocker.patch("ahriman.core.status.local_client.LocalClient.package_patches_remove")
|
||||
await watcher.package_patches_remove(package_ahriman.base, None)
|
||||
cache_mock.assert_called_once_with(package_ahriman.base, None)
|
||||
|
||||
|
||||
async def test_package_patches_update(watcher: Watcher, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must update package patches
|
||||
"""
|
||||
patch = PkgbuildPatch("key", "value")
|
||||
cache_mock = mocker.patch("ahriman.core.status.local_client.LocalClient.package_patches_update")
|
||||
|
||||
await watcher.package_patches_update(package_ahriman.base, patch)
|
||||
cache_mock.assert_called_once_with(package_ahriman.base, patch)
|
||||
|
||||
|
||||
async def test_package_remove(watcher: Watcher, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must remove package base
|
||||
"""
|
||||
cache_mock = mocker.patch("ahriman.core.status.local_client.LocalClient.package_remove")
|
||||
broadcast_mock = mocker.patch("ahriman.core.status.event_bus.EventBus.broadcast")
|
||||
watcher._known = {package_ahriman.base: (package_ahriman, BuildStatus())}
|
||||
|
||||
watcher.package_remove(package_ahriman.base)
|
||||
await watcher.package_remove(package_ahriman.base)
|
||||
assert not watcher._known
|
||||
cache_mock.assert_called_once_with(package_ahriman.base)
|
||||
broadcast_mock.assert_called_once_with(EventType.PackageRemoved, package_ahriman.base)
|
||||
|
||||
|
||||
def test_package_remove_unknown(watcher: Watcher, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
async def test_package_remove_unknown(watcher: Watcher, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must not fail on unknown base removal
|
||||
"""
|
||||
cache_mock = mocker.patch("ahriman.core.status.local_client.LocalClient.package_remove")
|
||||
watcher.package_remove(package_ahriman.base)
|
||||
broadcast_mock = mocker.patch("ahriman.core.status.event_bus.EventBus.broadcast")
|
||||
|
||||
await watcher.package_remove(package_ahriman.base)
|
||||
cache_mock.assert_called_once_with(package_ahriman.base)
|
||||
broadcast_mock.assert_called_once_with(EventType.PackageRemoved, package_ahriman.base)
|
||||
|
||||
|
||||
def test_package_status_update(watcher: Watcher, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
async def test_package_status_update(watcher: Watcher, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must update package status only for known package
|
||||
"""
|
||||
cache_mock = mocker.patch("ahriman.core.status.local_client.LocalClient.package_status_update")
|
||||
broadcast_mock = mocker.patch("ahriman.core.status.event_bus.EventBus.broadcast")
|
||||
watcher._known = {package_ahriman.base: (package_ahriman, BuildStatus())}
|
||||
|
||||
watcher.package_status_update(package_ahriman.base, BuildStatusEnum.Success)
|
||||
await watcher.package_status_update(package_ahriman.base, BuildStatusEnum.Success)
|
||||
cache_mock.assert_called_once_with(package_ahriman.base, pytest.helpers.anyvar(int))
|
||||
package, status = watcher._known[package_ahriman.base]
|
||||
assert package == package_ahriman
|
||||
assert status.status == BuildStatusEnum.Success
|
||||
broadcast_mock.assert_called_once_with(
|
||||
EventType.PackageStatusChanged, package_ahriman.base, status=BuildStatusEnum.Success.value,
|
||||
)
|
||||
|
||||
|
||||
def test_package_status_update_preserves_hold(watcher: Watcher, package_ahriman: Package,
|
||||
mocker: MockerFixture) -> None:
|
||||
async def test_package_status_update_preserves_hold(watcher: Watcher, package_ahriman: Package,
|
||||
mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must preserve hold status on package status update
|
||||
"""
|
||||
mocker.patch("ahriman.core.status.local_client.LocalClient.package_status_update")
|
||||
mocker.patch("ahriman.core.status.event_bus.EventBus.broadcast")
|
||||
watcher._known = {package_ahriman.base: (package_ahriman, BuildStatus(is_held=True))}
|
||||
|
||||
watcher.package_status_update(package_ahriman.base, BuildStatusEnum.Success)
|
||||
await watcher.package_status_update(package_ahriman.base, BuildStatusEnum.Success)
|
||||
_, status = watcher._known[package_ahriman.base]
|
||||
assert status.is_held is True
|
||||
|
||||
|
||||
def test_package_status_update_unknown(watcher: Watcher, package_ahriman: Package) -> None:
|
||||
async def test_package_status_update_unknown(watcher: Watcher, package_ahriman: Package) -> None:
|
||||
"""
|
||||
must fail on unknown package status update only
|
||||
"""
|
||||
with pytest.raises(UnknownPackageError):
|
||||
watcher.package_status_update(package_ahriman.base, BuildStatusEnum.Unknown)
|
||||
await watcher.package_status_update(package_ahriman.base, BuildStatusEnum.Unknown)
|
||||
|
||||
|
||||
def test_package_update(watcher: Watcher, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
async def test_package_update(watcher: Watcher, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must add package to cache
|
||||
"""
|
||||
cache_mock = mocker.patch("ahriman.core.status.local_client.LocalClient.package_update")
|
||||
broadcast_mock = mocker.patch("ahriman.core.status.event_bus.EventBus.broadcast")
|
||||
|
||||
watcher.package_update(package_ahriman, BuildStatusEnum.Unknown)
|
||||
assert watcher.packages
|
||||
await watcher.package_update(package_ahriman, BuildStatusEnum.Unknown)
|
||||
assert await watcher.packages()
|
||||
cache_mock.assert_called_once_with(package_ahriman, pytest.helpers.anyvar(int))
|
||||
broadcast_mock.assert_called_once_with(
|
||||
EventType.PackageUpdated, package_ahriman.base,
|
||||
status=BuildStatusEnum.Unknown.value, version=package_ahriman.version,
|
||||
)
|
||||
|
||||
|
||||
def test_package_update_preserves_hold(watcher: Watcher, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
async def test_package_update_preserves_hold(watcher: Watcher, package_ahriman: Package,
|
||||
mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must preserve hold status on package update
|
||||
"""
|
||||
mocker.patch("ahriman.core.status.local_client.LocalClient.package_update")
|
||||
watcher._known = {package_ahriman.base: (package_ahriman, BuildStatus(is_held=True))}
|
||||
|
||||
watcher.package_update(package_ahriman, BuildStatusEnum.Success)
|
||||
await watcher.package_update(package_ahriman, BuildStatusEnum.Success)
|
||||
_, status = watcher._known[package_ahriman.base]
|
||||
assert status.is_held is True
|
||||
|
||||
|
||||
def test_status_update(watcher: Watcher) -> None:
|
||||
async def test_packages(watcher: Watcher, package_ahriman: Package) -> None:
|
||||
"""
|
||||
must return list of available packages
|
||||
"""
|
||||
assert not await watcher.packages()
|
||||
|
||||
watcher._known = {package_ahriman.base: (package_ahriman, BuildStatus())}
|
||||
assert await watcher.packages()
|
||||
|
||||
|
||||
async def test_shutdown(watcher: Watcher, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must gracefully shutdown watcher
|
||||
"""
|
||||
shutdown_mock = mocker.patch("ahriman.core.status.event_bus.EventBus.shutdown")
|
||||
await watcher.shutdown()
|
||||
shutdown_mock.assert_called_once_with()
|
||||
|
||||
|
||||
async def test_status_update(watcher: Watcher, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must update service status
|
||||
"""
|
||||
watcher.status_update(BuildStatusEnum.Success)
|
||||
broadcast_mock = mocker.patch("ahriman.core.status.event_bus.EventBus.broadcast")
|
||||
|
||||
await watcher.status_update(BuildStatusEnum.Success)
|
||||
assert watcher.status.status == BuildStatusEnum.Success
|
||||
broadcast_mock.assert_called_once_with(EventType.ServiceStatusChanged, None, status=BuildStatusEnum.Success.value)
|
||||
|
||||
|
||||
def test_call(watcher: Watcher, package_ahriman: Package) -> None:
|
||||
@@ -204,18 +383,3 @@ def test_call_failed(watcher: Watcher, package_ahriman: Package) -> None:
|
||||
"""
|
||||
with pytest.raises(UnknownPackageError):
|
||||
assert watcher(package_ahriman.base)
|
||||
|
||||
|
||||
def test_getattr(watcher: Watcher) -> None:
|
||||
"""
|
||||
must return client method call
|
||||
"""
|
||||
assert watcher.package_logs_remove
|
||||
|
||||
|
||||
def test_getattr_unknown_method(watcher: Watcher) -> None:
|
||||
"""
|
||||
must raise AttributeError in case if no reporter attribute found
|
||||
"""
|
||||
with pytest.raises(AttributeError):
|
||||
assert watcher.random_method
|
||||
|
||||
85
tests/ahriman/web/middlewares/test_etag_handler.py
Normal file
85
tests/ahriman/web/middlewares/test_etag_handler.py
Normal file
@@ -0,0 +1,85 @@
|
||||
import hashlib
|
||||
import pytest
|
||||
|
||||
from aiohttp import ETag
|
||||
from aiohttp.web import HTTPNotModified, Response, StreamResponse
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from ahriman.web.middlewares.etag_handler import etag_handler
|
||||
|
||||
|
||||
async def test_etag_handler() -> None:
|
||||
"""
|
||||
must set ETag header on GET responses
|
||||
"""
|
||||
request = pytest.helpers.request("", "", "GET")
|
||||
request.if_none_match = None
|
||||
request_handler = AsyncMock(return_value=Response(body=b"hello"))
|
||||
|
||||
handler = etag_handler()
|
||||
result = await handler(request, request_handler)
|
||||
assert result.etag is not None
|
||||
|
||||
|
||||
async def test_etag_handler_not_modified() -> None:
|
||||
"""
|
||||
must raise NotModified when ETag matches If-None-Match
|
||||
"""
|
||||
body = b"hello"
|
||||
request = pytest.helpers.request("", "", "GET")
|
||||
request.if_none_match = (ETag(value=hashlib.md5(body, usedforsecurity=False).hexdigest()),)
|
||||
request_handler = AsyncMock(return_value=Response(body=body))
|
||||
|
||||
handler = etag_handler()
|
||||
with pytest.raises(HTTPNotModified):
|
||||
await handler(request, request_handler)
|
||||
|
||||
|
||||
async def test_etag_handler_no_match() -> None:
|
||||
"""
|
||||
must return full response when ETag does not match If-None-Match
|
||||
"""
|
||||
request = pytest.helpers.request("", "", "GET")
|
||||
request.if_none_match = (ETag(value="outdated"),)
|
||||
request_handler = AsyncMock(return_value=Response(body=b"hello"))
|
||||
|
||||
handler = etag_handler()
|
||||
result = await handler(request, request_handler)
|
||||
assert result.status == 200
|
||||
assert result.etag is not None
|
||||
|
||||
|
||||
async def test_etag_handler_skip_post() -> None:
|
||||
"""
|
||||
must skip ETag for non-GET/HEAD methods
|
||||
"""
|
||||
request = pytest.helpers.request("", "", "POST")
|
||||
request_handler = AsyncMock(return_value=Response(body=b"hello"))
|
||||
|
||||
handler = etag_handler()
|
||||
result = await handler(request, request_handler)
|
||||
assert result.etag is None
|
||||
|
||||
|
||||
async def test_etag_handler_skip_no_body() -> None:
|
||||
"""
|
||||
must skip ETag for responses without body
|
||||
"""
|
||||
request = pytest.helpers.request("", "", "GET")
|
||||
request_handler = AsyncMock(return_value=Response())
|
||||
|
||||
handler = etag_handler()
|
||||
result = await handler(request, request_handler)
|
||||
assert result.etag is None
|
||||
|
||||
|
||||
async def test_etag_handler_skip_stream() -> None:
|
||||
"""
|
||||
must skip ETag for streaming responses
|
||||
"""
|
||||
request = pytest.helpers.request("", "", "GET")
|
||||
request_handler = AsyncMock(return_value=StreamResponse())
|
||||
|
||||
handler = etag_handler()
|
||||
result = await handler(request, request_handler)
|
||||
assert "ETag" not in result.headers
|
||||
@@ -0,0 +1 @@
|
||||
# schema testing goes in view class tests
|
||||
1
tests/ahriman/web/schemas/test_sse_schema.py
Normal file
1
tests/ahriman/web/schemas/test_sse_schema.py
Normal file
@@ -0,0 +1 @@
|
||||
# schema testing goes in view class tests
|
||||
@@ -2,13 +2,17 @@ import aiohttp_cors
|
||||
import pytest
|
||||
|
||||
from aiohttp.web import Application
|
||||
from pytest_mock import MockerFixture
|
||||
|
||||
from ahriman.web.cors import setup_cors
|
||||
from ahriman.web.keys import ConfigurationKey
|
||||
|
||||
|
||||
def test_setup_cors(application: Application) -> None:
|
||||
"""
|
||||
must setup CORS
|
||||
"""
|
||||
cors: aiohttp_cors.CorsConfig = application[aiohttp_cors.APP_CONFIG_KEY]
|
||||
cors = application[aiohttp_cors.APP_CONFIG_KEY]
|
||||
# let's test here that it is enabled for all requests
|
||||
for route in application.router.routes():
|
||||
# we don't want to deal with match info here though
|
||||
@@ -18,3 +22,34 @@ def test_setup_cors(application: Application) -> None:
|
||||
continue
|
||||
request = pytest.helpers.request(application, url, route.method, resource=route.resource)
|
||||
assert cors._cors_impl._router_adapter.is_cors_enabled_on_request(request)
|
||||
|
||||
|
||||
def test_setup_cors_custom_origins(application: Application, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must setup CORS with custom origins
|
||||
"""
|
||||
configuration = application[ConfigurationKey]
|
||||
configuration.set_option("web", "cors_allow_origins", "https://example.com https://httpbin.com")
|
||||
|
||||
setup_mock = mocker.patch("ahriman.web.cors.aiohttp_cors.setup", return_value=mocker.MagicMock())
|
||||
setup_cors(application, configuration)
|
||||
|
||||
defaults = setup_mock.call_args.kwargs["defaults"]
|
||||
assert "https://example.com" in defaults
|
||||
assert "https://httpbin.com" in defaults
|
||||
assert "*" not in defaults
|
||||
|
||||
|
||||
def test_setup_cors_custom_methods(application: Application, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must setup CORS with custom methods
|
||||
"""
|
||||
configuration = application[ConfigurationKey]
|
||||
configuration.set_option("web", "cors_allow_methods", "GET POST")
|
||||
|
||||
setup_mock = mocker.patch("ahriman.web.cors.aiohttp_cors.setup", return_value=mocker.MagicMock())
|
||||
setup_cors(application, configuration)
|
||||
|
||||
defaults = setup_mock.call_args.kwargs["defaults"]
|
||||
resource_options = next(iter(defaults.values()))
|
||||
assert resource_options.allow_methods == {"GET", "POST"}
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
import asyncio
|
||||
import pytest
|
||||
|
||||
from aiohttp.test_utils import TestClient
|
||||
from asyncio import Queue
|
||||
from pytest_mock import MockerFixture
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from ahriman.core.status.watcher import Watcher
|
||||
from ahriman.models.event import EventType
|
||||
from ahriman.models.package import Package
|
||||
from ahriman.models.user_access import UserAccess
|
||||
from ahriman.web.keys import WatcherKey
|
||||
from ahriman.web.views.v1.auditlog.event_bus import EventBusView
|
||||
|
||||
|
||||
async def _producer(watcher: Watcher, package_ahriman: Package) -> None:
|
||||
"""
|
||||
create producer
|
||||
|
||||
Args:
|
||||
watcher(Watcher): watcher test instance
|
||||
package_ahriman(Package): package test instance
|
||||
"""
|
||||
await asyncio.sleep(0.1)
|
||||
await watcher.event_bus.broadcast(EventType.PackageRemoved, package_ahriman.base)
|
||||
await watcher.event_bus.broadcast(EventType.PackageUpdated, package_ahriman.base, status="success")
|
||||
await asyncio.sleep(0.1)
|
||||
await watcher.event_bus.shutdown()
|
||||
|
||||
|
||||
async def test_get_permission() -> None:
|
||||
"""
|
||||
must return correct permission for the request
|
||||
"""
|
||||
for method in ("GET",):
|
||||
request = pytest.helpers.request("", "", method)
|
||||
assert await EventBusView.get_permission(request) == UserAccess.Full
|
||||
|
||||
|
||||
def test_routes() -> None:
|
||||
"""
|
||||
must return correct routes
|
||||
"""
|
||||
assert EventBusView.ROUTES == ["/api/v1/events/stream"]
|
||||
|
||||
|
||||
async def test_run_timeout() -> None:
|
||||
"""
|
||||
must handle timeout and continue loop
|
||||
"""
|
||||
queue = Queue()
|
||||
|
||||
async def _shutdown() -> None:
|
||||
await asyncio.sleep(0.05)
|
||||
await queue.put(None)
|
||||
|
||||
response = AsyncMock()
|
||||
response.is_connected = lambda: True
|
||||
response.ping_interval = 0.01
|
||||
|
||||
asyncio.create_task(_shutdown())
|
||||
await EventBusView._run(response, queue)
|
||||
|
||||
|
||||
async def test_get(client: TestClient, package_ahriman: Package) -> None:
|
||||
"""
|
||||
must stream events via SSE
|
||||
"""
|
||||
watcher = next(iter(client.app[WatcherKey].values()))
|
||||
asyncio.create_task(_producer(watcher, package_ahriman))
|
||||
request_schema = pytest.helpers.schema_request(EventBusView.get, location="querystring")
|
||||
# no content validation here because it is a streaming response
|
||||
|
||||
assert not request_schema.validate({})
|
||||
response = await client.get("/api/v1/events/stream")
|
||||
assert response.status == 200
|
||||
|
||||
body = await response.text()
|
||||
assert EventType.PackageUpdated in body
|
||||
assert "ahriman" in body
|
||||
|
||||
|
||||
async def test_get_with_topic_filter(client: TestClient, package_ahriman: Package) -> None:
|
||||
"""
|
||||
must filter events by topic
|
||||
"""
|
||||
watcher = next(iter(client.app[WatcherKey].values()))
|
||||
asyncio.create_task(_producer(watcher, package_ahriman))
|
||||
request_schema = pytest.helpers.schema_request(EventBusView.get, location="querystring")
|
||||
|
||||
payload = {"event": [EventType.PackageUpdated]}
|
||||
assert not request_schema.validate(payload)
|
||||
response = await client.get("/api/v1/events/stream", params=payload)
|
||||
assert response.status == 200
|
||||
|
||||
body = await response.text()
|
||||
assert EventType.PackageUpdated in body
|
||||
assert EventType.PackageRemoved not in body
|
||||
|
||||
|
||||
async def test_get_bad_request(client: TestClient) -> None:
|
||||
"""
|
||||
must return bad request for invalid event type
|
||||
"""
|
||||
response_schema = pytest.helpers.schema_response(EventBusView.get, code=400)
|
||||
|
||||
response = await client.get("/api/v1/events/stream", params={"event": "invalid"})
|
||||
assert response.status == 400
|
||||
assert not response_schema.validate(await response.json())
|
||||
|
||||
|
||||
async def test_get_not_found(client: TestClient) -> None:
|
||||
"""
|
||||
must return not found for unknown repository
|
||||
"""
|
||||
response_schema = pytest.helpers.schema_response(EventBusView.get, code=404)
|
||||
|
||||
response = await client.get("/api/v1/events/stream", params={"architecture": "unknown", "repository": "unknown"})
|
||||
assert response.status == 404
|
||||
assert not response_schema.validate(await response.json())
|
||||
|
||||
|
||||
async def test_get_connection_reset(client: TestClient, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must handle connection reset
|
||||
"""
|
||||
mocker.patch.object(EventBusView, "_run", side_effect=ConnectionResetError)
|
||||
response = await client.get("/api/v1/events/stream")
|
||||
assert response.status == 200
|
||||
Reference in New Issue
Block a user