mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2024-11-23 04:21:29 +00:00
feat: Use proper logging (#154)
Reviewed-on: https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api/pulls/154 Reviewed-by: Inex Code <inex.code@selfprivacy.org> Co-authored-by: dettlaff <dettlaff@riseup.net> Co-committed-by: dettlaff <dettlaff@riseup.net>
This commit is contained in:
parent
03d751e591
commit
848befe3f1
|
@ -1,5 +1,8 @@
|
|||
#!/usr/bin/env python3
|
||||
"""SelfPrivacy server management API"""
|
||||
import logging
|
||||
import os
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from strawberry.fastapi import GraphQLRouter
|
||||
|
@ -12,6 +15,12 @@ from selfprivacy_api.graphql.schema import schema
|
|||
from selfprivacy_api.migrations import run_migrations
|
||||
|
||||
|
||||
log_level = os.getenv("LOG_LEVEL", "INFO").upper()
|
||||
|
||||
logging.basicConfig(
|
||||
level=getattr(logging, log_level, logging.INFO), format="%(levelname)s: %(message)s"
|
||||
)
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
graphql_app: GraphQLRouter = GraphQLRouter(
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
# pylint: disable=too-few-public-methods
|
||||
import typing
|
||||
|
||||
import strawberry
|
||||
|
||||
from selfprivacy_api.graphql.common_types.service import (
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
import subprocess
|
||||
import pathlib
|
||||
import shutil
|
||||
import logging
|
||||
|
||||
from pydantic import BaseModel
|
||||
from selfprivacy_api.jobs import Job, JobStatus, Jobs
|
||||
|
@ -15,6 +16,8 @@ from selfprivacy_api.utils import ReadUserData, WriteUserData
|
|||
from selfprivacy_api.utils.huey import huey
|
||||
from selfprivacy_api.utils.block_devices import BlockDevices
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class BindMigrationConfig(BaseModel):
|
||||
"""Config for bind migration.
|
||||
|
@ -69,7 +72,7 @@ def move_folder(
|
|||
try:
|
||||
data_path.mkdir(mode=0o750, parents=True, exist_ok=True)
|
||||
except Exception as error:
|
||||
print(f"Error creating data path: {error}")
|
||||
logging.error(f"Error creating data path: {error}")
|
||||
return
|
||||
|
||||
try:
|
||||
|
@ -81,12 +84,12 @@ def move_folder(
|
|||
try:
|
||||
subprocess.run(["mount", "--bind", str(bind_path), str(data_path)], check=True)
|
||||
except subprocess.CalledProcessError as error:
|
||||
print(error)
|
||||
logging.error(error)
|
||||
|
||||
try:
|
||||
subprocess.run(["chown", "-R", f"{user}:{group}", str(data_path)], check=True)
|
||||
except subprocess.CalledProcessError as error:
|
||||
print(error)
|
||||
logging.error(error)
|
||||
|
||||
|
||||
@huey.task()
|
||||
|
|
|
@ -9,6 +9,8 @@ with IDs of the migrations to skip.
|
|||
Adding DISABLE_ALL to that array disables the migrations module entirely.
|
||||
"""
|
||||
|
||||
import logging
|
||||
|
||||
from selfprivacy_api.utils import ReadUserData, UserDataFiles
|
||||
from selfprivacy_api.migrations.write_token_to_redis import WriteTokenToRedis
|
||||
from selfprivacy_api.migrations.check_for_system_rebuild_jobs import (
|
||||
|
@ -17,6 +19,8 @@ from selfprivacy_api.migrations.check_for_system_rebuild_jobs import (
|
|||
from selfprivacy_api.migrations.add_roundcube import AddRoundcube
|
||||
from selfprivacy_api.migrations.add_monitoring import AddMonitoring
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
migrations = [
|
||||
WriteTokenToRedis(),
|
||||
CheckForSystemRebuildJobs(),
|
||||
|
@ -47,6 +51,6 @@ def run_migrations():
|
|||
if migration.is_migration_needed():
|
||||
migration.migrate()
|
||||
except Exception as err:
|
||||
print(f"Error while migrating {migration.get_migration_name()}")
|
||||
print(err)
|
||||
print("Skipping this migration")
|
||||
logging.error(f"Error while migrating {migration.get_migration_name()}")
|
||||
logging.error(err)
|
||||
logging.error("Skipping this migration")
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import logging
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
from selfprivacy_api.migrations.migration import Migration
|
||||
|
@ -11,6 +12,8 @@ from selfprivacy_api.repositories.tokens.abstract_tokens_repository import (
|
|||
)
|
||||
from selfprivacy_api.utils import ReadUserData, UserDataFiles
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class WriteTokenToRedis(Migration):
|
||||
"""Load Json tokens into Redis"""
|
||||
|
@ -35,7 +38,7 @@ class WriteTokenToRedis(Migration):
|
|||
created_at=datetime.now(),
|
||||
)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
logging.error(e)
|
||||
return None
|
||||
|
||||
def is_migration_needed(self) -> bool:
|
||||
|
@ -45,7 +48,7 @@ class WriteTokenToRedis(Migration):
|
|||
):
|
||||
return True
|
||||
except Exception as e:
|
||||
print(e)
|
||||
logging.error(e)
|
||||
return False
|
||||
return False
|
||||
|
||||
|
@ -54,11 +57,11 @@ class WriteTokenToRedis(Migration):
|
|||
try:
|
||||
token = self.get_token_from_json()
|
||||
if token is None:
|
||||
print("No token found in secrets.json")
|
||||
logging.error("No token found in secrets.json")
|
||||
return
|
||||
RedisTokensRepository()._store_token(token)
|
||||
|
||||
print("Done")
|
||||
logging.error("Done")
|
||||
except Exception as e:
|
||||
print(e)
|
||||
print("Error migrating access tokens from json to redis")
|
||||
logging.error(e)
|
||||
logging.error("Error migrating access tokens from json to redis")
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
"""Services module."""
|
||||
|
||||
import logging
|
||||
import base64
|
||||
import typing
|
||||
from typing import List
|
||||
|
@ -30,6 +31,8 @@ from selfprivacy_api.utils import read_account_uri
|
|||
|
||||
CONFIG_STASH_DIR = "/etc/selfprivacy/dump"
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ServiceManager(Service):
|
||||
folders: List[str] = [CONFIG_STASH_DIR]
|
||||
|
@ -76,7 +79,7 @@ class ServiceManager(Service):
|
|||
)
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"Error creating CAA: {e}")
|
||||
logging.error(f"Error creating CAA: {e}")
|
||||
|
||||
for service in ServiceManager.get_enabled_services():
|
||||
dns_records += service.get_dns_records(ip4, ip6)
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
"""Generic size counter using pathlib"""
|
||||
|
||||
import pathlib
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def get_storage_usage(path: str) -> int:
|
||||
|
@ -18,5 +21,5 @@ def get_storage_usage(path: str) -> int:
|
|||
except FileNotFoundError:
|
||||
pass
|
||||
except Exception as error:
|
||||
print(error)
|
||||
logging.error(error)
|
||||
return storage_usage
|
||||
|
|
|
@ -1,11 +1,16 @@
|
|||
from __future__ import annotations
|
||||
import logging
|
||||
import subprocess
|
||||
import pathlib
|
||||
from pydantic import BaseModel
|
||||
from os.path import exists
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from selfprivacy_api.utils.block_devices import BlockDevice, BlockDevices
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# tests override it to a tmpdir
|
||||
VOLUMES_PATH = "/volumes"
|
||||
|
||||
|
@ -87,7 +92,7 @@ class Bind:
|
|||
check=True,
|
||||
)
|
||||
except subprocess.CalledProcessError as error:
|
||||
print(error.stderr)
|
||||
logging.error(error.stderr)
|
||||
raise BindError(f"Unable to bind {source} to {target} :{error.stderr}")
|
||||
|
||||
def unbind(self) -> None:
|
||||
|
@ -119,7 +124,7 @@ class Bind:
|
|||
stderr=subprocess.PIPE,
|
||||
)
|
||||
except subprocess.CalledProcessError as error:
|
||||
print(error.stderr)
|
||||
logging.error(error.stderr)
|
||||
error_message = (
|
||||
f"Unable to set ownership of {true_location} :{error.stderr}"
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue