mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2024-11-22 04:01:27 +00:00
write to the log file instead of stdout
This commit is contained in:
parent
7dc8aa724d
commit
9fb47272f8
|
@ -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.",
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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())
|
||||
|
|
Loading…
Reference in a new issue