mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2025-02-16 14:24:29 +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.services import get_service_by_id
|
||||||
|
from selfprivacy_api.utils import write_to_log
|
||||||
|
|
||||||
|
|
||||||
@strawberry.type
|
@strawberry.type
|
||||||
|
@ -172,8 +173,8 @@ class ServicesMutations:
|
||||||
self, input: SetServiceConfigurationInput
|
self, input: SetServiceConfigurationInput
|
||||||
) -> ServiceMutationReturn:
|
) -> ServiceMutationReturn:
|
||||||
"""Set the new configuration values"""
|
"""Set the new configuration values"""
|
||||||
print('set_service_configuration')
|
write_to_log('set_service_configuration')
|
||||||
print(f"{input=}")
|
write_to_log(f"{input=}")
|
||||||
service = get_service_by_id(input.service_id)
|
service = get_service_by_id(input.service_id)
|
||||||
if service is None:
|
if service is None:
|
||||||
return ServiceMutationReturn(
|
return ServiceMutationReturn(
|
||||||
|
@ -182,9 +183,9 @@ class ServicesMutations:
|
||||||
code=404,
|
code=404,
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
print('Got service by id.')
|
write_to_log('Got service by id.')
|
||||||
service.set_configuration(input.configuration)
|
service.set_configuration(input.configuration)
|
||||||
print('Configuration set.')
|
write_to_log('Configuration set.')
|
||||||
return ServiceMutationReturn(
|
return ServiceMutationReturn(
|
||||||
success=True,
|
success=True,
|
||||||
message="Service configuration updated.",
|
message="Service configuration updated.",
|
||||||
|
|
|
@ -2,7 +2,7 @@ from abc import ABC, abstractmethod
|
||||||
import re
|
import re
|
||||||
from typing import Optional
|
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):
|
class ServiceConfigItem(ABC):
|
||||||
|
@ -57,12 +57,12 @@ class StringServiceConfigItem(ServiceConfigItem):
|
||||||
return service_options.get(self.id, self.default_value)
|
return service_options.get(self.id, self.default_value)
|
||||||
|
|
||||||
def set_value(self, value, service_options):
|
def set_value(self, value, service_options):
|
||||||
print('set_value called')
|
write_to_log('set_value called')
|
||||||
if not self.validate_value(value):
|
if not self.validate_value(value):
|
||||||
raise ValueError(f"Value {value} is not valid")
|
raise ValueError(f"Value {value} is not valid")
|
||||||
if self.regex and not self.regex.match(value):
|
if self.regex and not self.regex.match(value):
|
||||||
raise ValueError(f"Value {value} does not match regex {self.regex}")
|
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
|
service_options[self.id] = value
|
||||||
|
|
||||||
def as_dict(self, service_options):
|
def as_dict(self, service_options):
|
||||||
|
@ -77,13 +77,18 @@ class StringServiceConfigItem(ServiceConfigItem):
|
||||||
}
|
}
|
||||||
|
|
||||||
def validate_value(self, value):
|
def validate_value(self, value):
|
||||||
|
write_to_log('validate_value called')
|
||||||
if not isinstance(value, str):
|
if not isinstance(value, str):
|
||||||
return False
|
return False
|
||||||
|
write_to_log('value is string')
|
||||||
if not self.allow_empty and not value:
|
if not self.allow_empty and not value:
|
||||||
return False
|
return False
|
||||||
|
write_to_log('value is not empty')
|
||||||
if self.regex and not self.regex.match(value):
|
if self.regex and not self.regex.match(value):
|
||||||
return False
|
return False
|
||||||
|
write_to_log('regex match')
|
||||||
if self.widget == "subdomain":
|
if self.widget == "subdomain":
|
||||||
|
write_to_log('subdomain widget')
|
||||||
if check_if_subdomain_is_taken(value):
|
if check_if_subdomain_is_taken(value):
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
|
@ -5,7 +5,7 @@ from typing import List, Optional
|
||||||
from selfprivacy_api import utils
|
from selfprivacy_api import utils
|
||||||
from selfprivacy_api.services.config_item import ServiceConfigItem
|
from selfprivacy_api.services.config_item import ServiceConfigItem
|
||||||
from selfprivacy_api.utils.default_subdomains import DEFAULT_SUBDOMAINS
|
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.waitloop import wait_until_true
|
||||||
from selfprivacy_api.utils.block_devices import BlockDevice, BlockDevices
|
from selfprivacy_api.utils.block_devices import BlockDevice, BlockDevices
|
||||||
|
|
||||||
|
@ -202,30 +202,30 @@ class Service(ABC):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def set_configuration(cls, config_items):
|
def set_configuration(cls, config_items):
|
||||||
print('set_configuration')
|
write_to_log('set_configuration')
|
||||||
print(f'{config_items=}')
|
write_to_log(f'{config_items=}')
|
||||||
print('Starting pre-check for config items')
|
write_to_log('Starting pre-check for config items')
|
||||||
for key, value in config_items.items():
|
for key, value in config_items.items():
|
||||||
print(f'{key=}')
|
write_to_log(f'{key=}')
|
||||||
print(f'{value=}')
|
write_to_log(f'{value=}')
|
||||||
if key not in cls.config_items:
|
if key not in cls.config_items:
|
||||||
raise ValueError(f"Key {key} is not valid for {cls.get_id()}")
|
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:
|
if cls.config_items[key].validate_value(value) is False:
|
||||||
raise ValueError(f"Value {value} is not valid for {key}")
|
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:
|
with WriteUserData() as user_data:
|
||||||
print('Writing to user_data')
|
write_to_log('Writing to user_data')
|
||||||
if "modules" not in 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"] = {}
|
user_data["modules"] = {}
|
||||||
if cls.get_id() not in 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()] = {}
|
user_data["modules"][cls.get_id()] = {}
|
||||||
for key, value in config_items.items():
|
for key, value in config_items.items():
|
||||||
print('Starting writing')
|
write_to_log('Starting writing')
|
||||||
print(f'{key=}')
|
write_to_log(f'{key=}')
|
||||||
print(f'{value=}')
|
write_to_log(f'{value=}')
|
||||||
cls.config_items[key].set_value(
|
cls.config_items[key].set_value(
|
||||||
value,
|
value,
|
||||||
user_data["modules"][cls.get_id()],
|
user_data["modules"][cls.get_id()],
|
||||||
|
@ -393,7 +393,7 @@ class Service(ABC):
|
||||||
try:
|
try:
|
||||||
ensure_folder_ownership(binds)
|
ensure_folder_ownership(binds)
|
||||||
except Exception as error:
|
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
|
# We are continuing anyway but Job has no warning field
|
||||||
Jobs.update(
|
Jobs.update(
|
||||||
job,
|
job,
|
||||||
|
|
|
@ -140,9 +140,12 @@ def is_username_forbidden(username):
|
||||||
|
|
||||||
def check_if_subdomain_is_taken(subdomain: str) -> bool:
|
def check_if_subdomain_is_taken(subdomain: str) -> bool:
|
||||||
"""Check if subdomain is already taken or reserved"""
|
"""Check if subdomain is already taken or reserved"""
|
||||||
|
write_to_log(f"Checking if subdomain {subdomain} is taken")
|
||||||
if subdomain in RESERVED_SUBDOMAINS:
|
if subdomain in RESERVED_SUBDOMAINS:
|
||||||
return True
|
return True
|
||||||
|
write_to_log(f"Subdomain {subdomain} is not reserved")
|
||||||
with ReadUserData() as data:
|
with ReadUserData() as data:
|
||||||
|
write_to_log("entered with ReadUserData")
|
||||||
for module in data["modules"]:
|
for module in data["modules"]:
|
||||||
if (
|
if (
|
||||||
data["modules"][module].get("subdomain", DEFAULT_SUBDOMAINS[module])
|
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.decode("ascii")
|
||||||
hashed_password = hashed_password.rstrip()
|
hashed_password = hashed_password.rstrip()
|
||||||
return hashed_password
|
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