-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentrypoint.py
More file actions
executable file
·93 lines (74 loc) · 2.67 KB
/
entrypoint.py
File metadata and controls
executable file
·93 lines (74 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from datetime import datetime, timezone
from glob import glob
from logging import ERROR, INFO, LogRecord, StreamHandler
from logging import Formatter as BaseFormatter
from logging import basicConfig as configure_logging
from logging import getLogger as get_logger
from os import X_OK, access, execlp
from pathlib import Path
from subprocess import CalledProcessError, run
from sys import argv, stdout
from typing import Optional
class Formatter(BaseFormatter):
def format(self, record: LogRecord) -> str:
original = record.levelname
record.levelname = original.lower()
result = super().format(record)
record.levelname = original
return result
@staticmethod
def _converter(*_):
return datetime.now(timezone.utc).timetuple()
converter = _converter
default_time_format = "%Y-%m-%dT%H:%M:%S"
default_msec_format = "%s.%06dZ"
configure_logging(level="INFO", handlers=[StreamHandler(stdout)])
formatter = Formatter(fmt="%(asctime)s [%(levelname)s] [%(module)s] %(message)s")
for handler in get_logger().handlers:
handler.setFormatter(formatter)
logger = get_logger(__name__)
entrypoint = Path("/docker-entrypoint.d")
def loglines(output: Optional[str], level: int):
lines = output.splitlines() if output else []
for raw in lines:
if line := raw.strip():
logger.log(level, line)
def execute(script: str):
if access(script, X_OK):
logger.info("Executing: %s", script)
process = run(
args=[script],
check=True,
capture_output=True,
text=True,
)
loglines(output=process.stdout, level=INFO)
else:
logger.info("Executing via shell: %s", script)
process = run(
args=["/bin/bash", script],
check=True,
capture_output=True,
text=True,
)
loglines(output=process.stdout, level=INFO)
def main():
logger.info("Scanning entrypoint directory for user scripts: %s", entrypoint)
scripts: list[str] = [script for script in glob(f"{entrypoint}/*.sh")]
if not scripts:
logger.info("No user scripts found!")
for script in scripts:
try:
execute(script)
except CalledProcessError as ex:
logger.error("Failed to execute user script: %s", script)
loglines(output=ex.stderr, level=ERROR)
exit(1)
logger.info("Initialization complete! Starting up...")
if __name__ == "__main__":
if entrypoint.exists() and entrypoint.is_dir():
main()
if len(argv) > 1:
_, cmd, *args = argv
# https://stackoverflow.com/a/6743663/17173324
execlp(cmd, cmd, *args)