Skip to content

Commit

Permalink
moved logging to common
Browse files Browse the repository at this point in the history
  • Loading branch information
rgaudin committed Mar 25, 2024
1 parent 5e886e0 commit 43a9849
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/offspot_demo/__about__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
NAME = "demo"
__version__ = "0.1.0-dev0"
11 changes: 10 additions & 1 deletion src/offspot_demo/constants.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import logging

Check warning on line 1 in src/offspot_demo/constants.py

View check run for this annotation

Codecov / codecov/patch

src/offspot_demo/constants.py#L1

Added line #L1 was not covered by tests
import os
from pathlib import Path

from jinja2 import Environment, FileSystemLoader, select_autoescape

from offspot_demo.__about__ import NAME as PROJECT_NAME

Check warning on line 7 in src/offspot_demo/constants.py

View check run for this annotation

Codecov / codecov/patch

src/offspot_demo/constants.py#L7

Added line #L7 was not covered by tests

OFFSPOT_IMAGE_ID = "offspot-demo"
OFFSPOT_IMAGE_URL = f"https://api.imager.kiwix.org/auto-images/{OFFSPOT_IMAGE_ID}/json"
TARGET_DIR = Path(os.getenv("TARGET_DIR", "/data"))
Expand Down Expand Up @@ -32,4 +35,10 @@

# maintenance container and images must be labeled with this
# in order not to be purged by deploy
DOCKER_LABEL_MAINT = "maintenance"
DOCKER_LABEL_MAINT = "maintenance"

Check warning on line 38 in src/offspot_demo/constants.py

View check run for this annotation

Codecov / codecov/patch

src/offspot_demo/constants.py#L38

Added line #L38 was not covered by tests


logging.basicConfig(level=logging.INFO)

Check warning on line 41 in src/offspot_demo/constants.py

View check run for this annotation

Codecov / codecov/patch

src/offspot_demo/constants.py#L41

Added line #L41 was not covered by tests

def get_logger(name: str | None)-> logging.Logger:
return logging.getLogger(PROJECT_NAME if not name else f"{PROJECT_NAME}/{name}")

Check warning on line 44 in src/offspot_demo/constants.py

View check run for this annotation

Codecov / codecov/patch

src/offspot_demo/constants.py#L43-L44

Added lines #L43 - L44 were not covered by tests
4 changes: 2 additions & 2 deletions src/offspot_demo/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
DOCKER_LABEL_MAINT,
IMAGE_PATH,
TARGET_DIR,
get_logger,
)
from offspot_demo.prepare import prepare_image
from offspot_demo.toggle import toggle_demo
Expand All @@ -37,8 +38,7 @@

ONE_MIB = 2**20

Check warning on line 39 in src/offspot_demo/deploy.py

View check run for this annotation

Codecov / codecov/patch

src/offspot_demo/deploy.py#L39

Added line #L39 was not covered by tests

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("demo-deploy")
logger = get_logger("deploy")

Check warning on line 41 in src/offspot_demo/deploy.py

View check run for this annotation

Codecov / codecov/patch

src/offspot_demo/deploy.py#L41

Added line #L41 was not covered by tests


def fail(message: str = "An error occured", code: int = 1) -> int:
Expand Down
8 changes: 8 additions & 0 deletions src/offspot_demo/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import os

Check warning on line 1 in src/offspot_demo/utils/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/offspot_demo/utils/__init__.py#L1

Added line #L1 was not covered by tests


def get_environ() -> dict[str, str]:

Check warning on line 4 in src/offspot_demo/utils/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/offspot_demo/utils/__init__.py#L4

Added line #L4 was not covered by tests
"""current environment variable with langs set to C to control cli output"""
environ = os.environ.copy()
environ.update({"LANG": "C", "LC_ALL": "C"})
return environ

Check warning on line 8 in src/offspot_demo/utils/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/offspot_demo/utils/__init__.py#L6-L8

Added lines #L6 - L8 were not covered by tests
28 changes: 12 additions & 16 deletions src/offspot_demo/utils/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
import pathlib
import subprocess

Check warning on line 5 in src/offspot_demo/utils/image.py

View check run for this annotation

Codecov / codecov/patch

src/offspot_demo/utils/image.py#L1-L5

Added lines #L1 - L5 were not covered by tests

logger = logging.getLogger()
from offspot_demo.constants import get_logger
from offspot_demo.utils import get_environ

Check warning on line 8 in src/offspot_demo/utils/image.py

View check run for this annotation

Codecov / codecov/patch

src/offspot_demo/utils/image.py#L7-L8

Added lines #L7 - L8 were not covered by tests

# /!\ need to retrieve debug setting
only_on_debug: bool = True
logger = get_logger("deploy") # reusing deploy logger to get its level

Check warning on line 10 in src/offspot_demo/utils/image.py

View check run for this annotation

Codecov / codecov/patch

src/offspot_demo/utils/image.py#L10

Added line #L10 was not covered by tests

def only_on_debug() -> bool:
return logger.level <= logging.DEBUG

Check warning on line 13 in src/offspot_demo/utils/image.py

View check run for this annotation

Codecov / codecov/patch

src/offspot_demo/utils/image.py#L12-L13

Added lines #L12 - L13 were not covered by tests


def flush_writes():

Check warning on line 16 in src/offspot_demo/utils/image.py

View check run for this annotation

Codecov / codecov/patch

src/offspot_demo/utils/image.py#L16

Added line #L16 was not covered by tests
Expand All @@ -17,19 +20,12 @@ def flush_writes():
subprocess.run(

Check warning on line 20 in src/offspot_demo/utils/image.py

View check run for this annotation

Codecov / codecov/patch

src/offspot_demo/utils/image.py#L19-L20

Added lines #L19 - L20 were not covered by tests
["/usr/bin/env", "sync", "-f"],
check=True,
capture_output=only_on_debug,
capture_output=only_on_debug(),
text=True,
env=get_environ(),
)


def get_environ() -> dict[str, str]:
"""current environment variable with langs set to C to control cli output"""
environ = os.environ.copy()
environ.update({"LANG": "C", "LC_ALL": "C"})
return environ


def get_loopdev() -> str:

Check warning on line 29 in src/offspot_demo/utils/image.py

View check run for this annotation

Codecov / codecov/patch

src/offspot_demo/utils/image.py#L29

Added line #L29 was not covered by tests
"""free loop-device path ready to ease"""
return subprocess.run(

Check warning on line 31 in src/offspot_demo/utils/image.py

View check run for this annotation

Codecov / codecov/patch

src/offspot_demo/utils/image.py#L31

Added line #L31 was not covered by tests
Expand Down Expand Up @@ -79,7 +75,7 @@ def create_block_special_device(dev_path: str, major: int, minor: int):
subprocess.run(

Check warning on line 75 in src/offspot_demo/utils/image.py

View check run for this annotation

Codecov / codecov/patch

src/offspot_demo/utils/image.py#L74-L75

Added lines #L74 - L75 were not covered by tests
["/usr/bin/env", "mknod", dev_path, "b", str(major), str(minor)],
check=True,
capture_output=only_on_debug,
capture_output=only_on_debug(),
text=True,
env=get_environ(),
)
Expand All @@ -90,7 +86,7 @@ def attach_to_device(img_fpath: pathlib.Path, loop_dev: str):
subprocess.run(

Check warning on line 86 in src/offspot_demo/utils/image.py

View check run for this annotation

Codecov / codecov/patch

src/offspot_demo/utils/image.py#L86

Added line #L86 was not covered by tests
["/usr/bin/env", "losetup", "--partscan", loop_dev, str(img_fpath)],
check=True,
capture_output=only_on_debug,
capture_output=only_on_debug(),
text=True,
env=get_environ(),
)
Expand Down Expand Up @@ -118,7 +114,7 @@ def detach_device(loop_dev: str, *, failsafe: bool = False) -> bool:
ps = subprocess.run(

Check warning on line 114 in src/offspot_demo/utils/image.py

View check run for this annotation

Codecov / codecov/patch

src/offspot_demo/utils/image.py#L114

Added line #L114 was not covered by tests
["/usr/bin/env", "losetup", "--detach", loop_dev],
check=not failsafe,
capture_output=only_on_debug,
capture_output=only_on_debug(),
text=True,
env=get_environ(),
)
Expand All @@ -145,7 +141,7 @@ def mount_on(dev_path: str, mount_point: pathlib.Path, filesystem: str | None) -
return (

Check warning on line 141 in src/offspot_demo/utils/image.py

View check run for this annotation

Codecov / codecov/patch

src/offspot_demo/utils/image.py#L139-L141

Added lines #L139 - L141 were not covered by tests
subprocess.run(
commands,
capture_output=only_on_debug,
capture_output=only_on_debug(),
text=True,
check=False,
env=get_environ(),
Expand All @@ -160,7 +156,7 @@ def unmount(mount_point: pathlib.Path) -> bool:
return (

Check warning on line 156 in src/offspot_demo/utils/image.py

View check run for this annotation

Codecov / codecov/patch

src/offspot_demo/utils/image.py#L155-L156

Added lines #L155 - L156 were not covered by tests
subprocess.run(
["/usr/bin/env", "umount", str(mount_point)],
capture_output=only_on_debug,
capture_output=only_on_debug(),
text=True,
check=False,
env=get_environ(),
Expand Down

0 comments on commit 43a9849

Please sign in to comment.