Skip to content

Commit

Permalink
fix:[#348] logging output configuration to recipe
Browse files Browse the repository at this point in the history
- move log config to main init
- log_file path to /tmp
- remove logging config from conn_check
- set logging level to debug
- set log file permissions
  • Loading branch information
jardon committed Oct 23, 2024
1 parent b7c4503 commit eaacfc8
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 32 deletions.
1 change: 0 additions & 1 deletion debian/vanilla-first-setup.install
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
recipe.json usr/share/org.vanillaos.FirstSetup/
__first_setup_reset_session usr/bin/
__first_setup_cleanup usr/bin/
first-setup.txt etc/vanilla/
data/org.vanillaos.FirstSetup.policy usr/share/polkit-1/actions/
data/org.vanillaos.FirstSetup.rules usr/share/polkit-1/rules.d/
1 change: 0 additions & 1 deletion first-setup.txt

This file was deleted.

2 changes: 1 addition & 1 deletion recipe.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"log_file": "/etc/vanilla/first-setup.log",
"log_file": "/tmp/first-setup.log",
"distro_name": "Vanilla OS",
"distro_logo": "org.vanillaos.FirstSetup-flower",
"pre_run": [
Expand Down
1 change: 0 additions & 1 deletion vanilla_first_setup/defaults/conn_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from vanilla_first_setup.utils.run_async import RunAsync
from vanilla_first_setup.utils.network import check_connection

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("FirstSetup::Conn_Check")


Expand Down
29 changes: 26 additions & 3 deletions vanilla_first_setup/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,42 @@
import os
import sys
import logging
import json
from gettext import gettext as _
from vanilla_first_setup.window import VanillaWindow
from vanilla_first_setup.utils.recipe import recipe_path
import subprocess


logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("FirstSetup::Main")


class FirstSetupApplication(Adw.Application):
"""The main application singleton class."""

def __init__(self):
# here we create a temporary file to store the output of the commands
# the log path is defined in the recipe
with open(recipe_path, 'r') as file:
recipe = json.load(file)

if "log_file" not in recipe:
logger.critical("Missing 'log_file' in the recipe.")
sys.exit(1)

log_path = recipe["log_file"]

if not os.path.exists(log_path):
try:
open(log_path, "a").close()
os.chmod(log_path, 0o666)
logging.basicConfig(level=logging.DEBUG,
filename=log_path,
filemode='a',
)
except OSError as e:
logger.warning(f"failed to create log file: {log_path}: {e}")
logging.warning("No log will be stored.")


super().__init__(
application_id="org.vanillaos.FirstSetup",
flags=Gio.ApplicationFlags.HANDLES_COMMAND_LINE,
Expand Down
15 changes: 0 additions & 15 deletions vanilla_first_setup/utils/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,6 @@ def __init__(self, window, new_user: bool = False):
self.__load()

def __load(self):
# here we create a temporary file to store the output of the commands
# the log path is defined in the recipe
if "log_file" not in self.__recipe.raw:
logger.critical("Missing 'log_file' in the recipe.")
sys.exit(1)

log_path = self.__recipe.raw["log_file"]

if not os.path.exists(log_path):
try:
open(log_path, "a").close()
except OSError as e:
logger.warning(f"failed to create log file: {log_path}: {e}")
logging.warning("No log will be stored.")

for i, (key, step) in enumerate(self.__recipe.raw["steps"].items()):
_status = True
_protected = False
Expand Down
14 changes: 5 additions & 9 deletions vanilla_first_setup/utils/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,26 @@

logger = logging.getLogger("FirstSetup::RecipeLoader")

recipe_path = os.environ["VANILLA_CUSTOM_RECIPE"] if "VANILLA_CUSTOM_RECIPE" in os.environ else "/usr/share/org.vanillaos.FirstSetup/recipe.json"

class RecipeLoader:
recipe_path = "/usr/share/org.vanillaos.FirstSetup/recipe.json"

def __init__(self):
self.__recipe = {}
self.__load()

def __load(self):
if "VANILLA_CUSTOM_RECIPE" in os.environ:
self.recipe_path = os.environ["VANILLA_CUSTOM_RECIPE"]

logger.info(f"Loading recipe from {self.recipe_path}")
logger.info(f"Loading recipe from {recipe_path}")

if os.path.exists(self.recipe_path):
with open(self.recipe_path, "r") as f:
if os.path.exists(recipe_path):
with open(recipe_path, "r") as f:
self.__recipe = json.load(f)
return

if not self.__validate():
logger.error("Invalid recipe file")
sys.exit(1)

logger.error(f"Recipe not found at {self.recipe_path}")
logger.error(f"Recipe not found at {recipe_path}")
sys.exit(1)

def __validate(self):
Expand Down
2 changes: 1 addition & 1 deletion vanilla_first_setup/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def __on_page_changed(self, *args):

# process the commands
res = Processor.get_setup_commands(
self.recipe.get("log_file", "/tmp/vanilla_first_setup.log"),
self.recipe.get("log_file"),
self.recipe.get("pre_run", []),
self.recipe.get("post_run"),
commands,
Expand Down

0 comments on commit eaacfc8

Please sign in to comment.