From 9fb47272f8d71cf6e30217019ac944c189df0076 Mon Sep 17 00:00:00 2001 From: Inex Code Date: Tue, 23 Jul 2024 19:35:50 +0300 Subject: [PATCH] write to the log file instead of stdout --- .../graphql/mutations/services_mutations.py | 9 +++--- selfprivacy_api/services/config_item.py | 11 +++++-- selfprivacy_api/services/service.py | 30 +++++++++---------- selfprivacy_api/utils/__init__.py | 10 +++++++ 4 files changed, 38 insertions(+), 22 deletions(-) diff --git a/selfprivacy_api/graphql/mutations/services_mutations.py b/selfprivacy_api/graphql/mutations/services_mutations.py index 2b34a32..1ba3814 100644 --- a/selfprivacy_api/graphql/mutations/services_mutations.py +++ b/selfprivacy_api/graphql/mutations/services_mutations.py @@ -24,6 +24,7 @@ from selfprivacy_api.actions.services import ( ) from selfprivacy_api.services import get_service_by_id +from selfprivacy_api.utils import write_to_log @strawberry.type @@ -172,8 +173,8 @@ class ServicesMutations: self, input: SetServiceConfigurationInput ) -> ServiceMutationReturn: """Set the new configuration values""" - print('set_service_configuration') - print(f"{input=}") + write_to_log('set_service_configuration') + write_to_log(f"{input=}") service = get_service_by_id(input.service_id) if service is None: return ServiceMutationReturn( @@ -182,9 +183,9 @@ class ServicesMutations: code=404, ) try: - print('Got service by id.') + write_to_log('Got service by id.') service.set_configuration(input.configuration) - print('Configuration set.') + write_to_log('Configuration set.') return ServiceMutationReturn( success=True, message="Service configuration updated.", diff --git a/selfprivacy_api/services/config_item.py b/selfprivacy_api/services/config_item.py index 94de486..5979219 100644 --- a/selfprivacy_api/services/config_item.py +++ b/selfprivacy_api/services/config_item.py @@ -2,7 +2,7 @@ from abc import ABC, abstractmethod import re from typing import Optional -from selfprivacy_api.utils import check_if_subdomain_is_taken +from selfprivacy_api.utils import check_if_subdomain_is_taken, write_to_log class ServiceConfigItem(ABC): @@ -57,12 +57,12 @@ class StringServiceConfigItem(ServiceConfigItem): return service_options.get(self.id, self.default_value) def set_value(self, value, service_options): - print('set_value called') + write_to_log('set_value called') if not self.validate_value(value): raise ValueError(f"Value {value} is not valid") if self.regex and not self.regex.match(value): raise ValueError(f"Value {value} does not match regex {self.regex}") - print('seting actual value') + write_to_log('seting actual value') service_options[self.id] = value def as_dict(self, service_options): @@ -77,13 +77,18 @@ class StringServiceConfigItem(ServiceConfigItem): } def validate_value(self, value): + write_to_log('validate_value called') if not isinstance(value, str): return False + write_to_log('value is string') if not self.allow_empty and not value: return False + write_to_log('value is not empty') if self.regex and not self.regex.match(value): return False + write_to_log('regex match') if self.widget == "subdomain": + write_to_log('subdomain widget') if check_if_subdomain_is_taken(value): return False return True diff --git a/selfprivacy_api/services/service.py b/selfprivacy_api/services/service.py index df68f6b..0a0187c 100644 --- a/selfprivacy_api/services/service.py +++ b/selfprivacy_api/services/service.py @@ -5,7 +5,7 @@ from typing import List, Optional from selfprivacy_api import utils from selfprivacy_api.services.config_item import ServiceConfigItem from selfprivacy_api.utils.default_subdomains import DEFAULT_SUBDOMAINS -from selfprivacy_api.utils import ReadUserData, WriteUserData, get_domain +from selfprivacy_api.utils import ReadUserData, WriteUserData, get_domain, write_to_log from selfprivacy_api.utils.waitloop import wait_until_true from selfprivacy_api.utils.block_devices import BlockDevice, BlockDevices @@ -202,30 +202,30 @@ class Service(ABC): @classmethod def set_configuration(cls, config_items): - print('set_configuration') - print(f'{config_items=}') - print('Starting pre-check for config items') + write_to_log('set_configuration') + write_to_log(f'{config_items=}') + write_to_log('Starting pre-check for config items') for key, value in config_items.items(): - print(f'{key=}') - print(f'{value=}') + write_to_log(f'{key=}') + write_to_log(f'{value=}') if key not in cls.config_items: raise ValueError(f"Key {key} is not valid for {cls.get_id()}") - print('key in cls.config_items') + write_to_log('key in cls.config_items') if cls.config_items[key].validate_value(value) is False: raise ValueError(f"Value {value} is not valid for {key}") - print('value is valid') + write_to_log('value is valid') with WriteUserData() as user_data: - print('Writing to user_data') + write_to_log('Writing to user_data') if "modules" not in user_data: - print('modules not in user_data') + write_to_log('modules not in user_data') user_data["modules"] = {} if cls.get_id() not in user_data["modules"]: - print('cls.get_id() not in user_data["modules"]') + write_to_log('cls.get_id() not in user_data["modules"]') user_data["modules"][cls.get_id()] = {} for key, value in config_items.items(): - print('Starting writing') - print(f'{key=}') - print(f'{value=}') + write_to_log('Starting writing') + write_to_log(f'{key=}') + write_to_log(f'{value=}') cls.config_items[key].set_value( value, user_data["modules"][cls.get_id()], @@ -393,7 +393,7 @@ class Service(ABC): try: ensure_folder_ownership(binds) except Exception as error: - # We have logged it via print and we additionally log it here in the error field + # We have logged it via write_to_log and we additionally log it here in the error field # We are continuing anyway but Job has no warning field Jobs.update( job, diff --git a/selfprivacy_api/utils/__init__.py b/selfprivacy_api/utils/__init__.py index 9972ff2..a45ea91 100644 --- a/selfprivacy_api/utils/__init__.py +++ b/selfprivacy_api/utils/__init__.py @@ -140,9 +140,12 @@ def is_username_forbidden(username): def check_if_subdomain_is_taken(subdomain: str) -> bool: """Check if subdomain is already taken or reserved""" + write_to_log(f"Checking if subdomain {subdomain} is taken") if subdomain in RESERVED_SUBDOMAINS: return True + write_to_log(f"Subdomain {subdomain} is not reserved") with ReadUserData() as data: + write_to_log("entered with ReadUserData") for module in data["modules"]: if ( data["modules"][module].get("subdomain", DEFAULT_SUBDOMAINS[module]) @@ -218,3 +221,10 @@ def hash_password(password): hashed_password = hashed_password.decode("ascii") hashed_password = hashed_password.rstrip() return hashed_password + + +def write_to_log(message): + with open("/etc/selfprivacy/log", "a") as log: + log.write(f"{datetime.datetime.now()} {message}\n") + log.flush() + os.fsync(log.fileno())