Trying to centralize the configuration for logging. Phasing out some parameters which also affects the current ability to create multiple log files for multiple runs. This will be re-introduced when logging is made into a class object in a later version
This commit is contained in:
parent
97b8bbb7d1
commit
f31d5e34cd
|
|
@ -119,7 +119,7 @@ class Partition():
|
||||||
return f'Partition(path={self.path}, fs={self.filesystem}, mounted={self.mountpoint})'
|
return f'Partition(path={self.path}, fs={self.filesystem}, mounted={self.mountpoint})'
|
||||||
|
|
||||||
def format(self, filesystem):
|
def format(self, filesystem):
|
||||||
log(f'Formatting {self} -> {filesystem}', level=LOG_LEVELS.Info, file=storage.get('logfile', None))
|
log(f'Formatting {self} -> {filesystem}', level=LOG_LEVELS.Info)
|
||||||
if filesystem == 'btrfs':
|
if filesystem == 'btrfs':
|
||||||
o = b''.join(sys_command(f'/usr/bin/mkfs.btrfs -f {self.path}'))
|
o = b''.join(sys_command(f'/usr/bin/mkfs.btrfs -f {self.path}'))
|
||||||
if b'UUID' not in o:
|
if b'UUID' not in o:
|
||||||
|
|
@ -162,7 +162,7 @@ class Partition():
|
||||||
|
|
||||||
def mount(self, target, fs=None, options=''):
|
def mount(self, target, fs=None, options=''):
|
||||||
if not self.mountpoint:
|
if not self.mountpoint:
|
||||||
log(f'Mounting {self} to {target}', level=LOG_LEVELS.Info, file=storage.get('logfile', None))
|
log(f'Mounting {self} to {target}', level=LOG_LEVELS.Info)
|
||||||
if not fs:
|
if not fs:
|
||||||
if not self.filesystem: raise DiskError(f'Need to format (or define) the filesystem on {self} before mounting.')
|
if not self.filesystem: raise DiskError(f'Need to format (or define) the filesystem on {self} before mounting.')
|
||||||
fs = self.filesystem
|
fs = self.filesystem
|
||||||
|
|
@ -227,7 +227,7 @@ class Filesystem():
|
||||||
self.add_partition('primary', start='513MiB', end='100%', format='ext4')
|
self.add_partition('primary', start='513MiB', end='100%', format='ext4')
|
||||||
|
|
||||||
def add_partition(self, type, start, end, format=None):
|
def add_partition(self, type, start, end, format=None):
|
||||||
log(f'Adding partition to {self.blockdevice}', level=LOG_LEVELS.Info, file=storage.get('logfile', None))
|
log(f'Adding partition to {self.blockdevice}', level=LOG_LEVELS.Info)
|
||||||
|
|
||||||
previous_partitions = self.blockdevice.partitions
|
previous_partitions = self.blockdevice.partitions
|
||||||
if format:
|
if format:
|
||||||
|
|
|
||||||
|
|
@ -34,13 +34,19 @@ class Installer():
|
||||||
:type hostname: str, optional
|
:type hostname: str, optional
|
||||||
|
|
||||||
"""
|
"""
|
||||||
def __init__(self, partition, boot_partition, *, base_packages='base base-devel linux linux-firmware efibootmgr nano', profile=None, mountpoint='/mnt', hostname='ArchInstalled'):
|
def __init__(self, partition, boot_partition, *, base_packages='base base-devel linux linux-firmware efibootmgr nano', profile=None, mountpoint='/mnt', hostname='ArchInstalled', logdir=None, logfile=None):
|
||||||
self.profile = profile
|
self.profile = profile
|
||||||
self.hostname = hostname
|
self.hostname = hostname
|
||||||
self.mountpoint = mountpoint
|
self.mountpoint = mountpoint
|
||||||
self.init_time = time.strftime('%Y-%m-%d_%H-%M-%S')
|
self.init_time = time.strftime('%Y-%m-%d_%H-%M-%S')
|
||||||
self.milliseconds = int(str(time.time()).split('.')[1])
|
self.milliseconds = int(str(time.time()).split('.')[1])
|
||||||
|
|
||||||
|
|
||||||
|
if logdir:
|
||||||
|
storage['LOG_PATH'] = logdir
|
||||||
|
if logfile:
|
||||||
|
storage['LOG_FILE'] = logfile
|
||||||
|
|
||||||
self.helper_flags = {
|
self.helper_flags = {
|
||||||
'bootloader' : False,
|
'bootloader' : False,
|
||||||
'base' : False,
|
'base' : False,
|
||||||
|
|
@ -53,7 +59,7 @@ class Installer():
|
||||||
self.partition = partition
|
self.partition = partition
|
||||||
self.boot_partition = boot_partition
|
self.boot_partition = boot_partition
|
||||||
|
|
||||||
def log(self, *args, level=LOG_LEVELS.Debug, file=None, **kwargs):
|
def log(self, *args, level=LOG_LEVELS.Debug, **kwargs):
|
||||||
if not file:
|
if not file:
|
||||||
if 'logfile' not in storage:
|
if 'logfile' not in storage:
|
||||||
log_root = os.path.join(os.path.expanduser('~/'), '.cache/archinstall')
|
log_root = os.path.join(os.path.expanduser('~/'), '.cache/archinstall')
|
||||||
|
|
@ -64,7 +70,7 @@ class Installer():
|
||||||
|
|
||||||
file = storage['logfile']
|
file = storage['logfile']
|
||||||
|
|
||||||
log(*args, level=level, file=file, **kwargs)
|
log(*args, level=level, **kwargs)
|
||||||
|
|
||||||
def __enter__(self, *args, **kwargs):
|
def __enter__(self, *args, **kwargs):
|
||||||
self.partition.mount(self.mountpoint)
|
self.partition.mount(self.mountpoint)
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ class luks2():
|
||||||
def encrypt(self, partition, password, key_size=512, hash_type='sha512', iter_time=10000, key_file=None):
|
def encrypt(self, partition, password, key_size=512, hash_type='sha512', iter_time=10000, key_file=None):
|
||||||
# TODO: We should be able to integrate this into the main log some how.
|
# TODO: We should be able to integrate this into the main log some how.
|
||||||
# Perhaps post-mortem?
|
# Perhaps post-mortem?
|
||||||
log(f'Encrypting {partition}', level=LOG_LEVELS.Info, file=storage.get('logfile', None))
|
log(f'Encrypting {partition}', level=LOG_LEVELS.Info)
|
||||||
|
|
||||||
if not key_file:
|
if not key_file:
|
||||||
key_file = f"/tmp/{os.path.basename(self.partition.path)}.disk_pw" # TODO: Make disk-pw-file randomly unique?
|
key_file = f"/tmp/{os.path.basename(self.partition.path)}.disk_pw" # TODO: Make disk-pw-file randomly unique?
|
||||||
|
|
|
||||||
|
|
@ -59,7 +59,7 @@ def insert_mirrors(mirrors, *args, **kwargs):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def use_mirrors(regions :dict, destination='/etc/pacman.d/mirrorlist'):
|
def use_mirrors(regions :dict, destination='/etc/pacman.d/mirrorlist'):
|
||||||
log(f'A new package mirror-list has been created: {destination}', level=LOG_LEVELS.Info, file=storage.get('logfile', None))
|
log(f'A new package mirror-list has been created: {destination}', level=LOG_LEVELS.Info)
|
||||||
for region, mirrors in regions.items():
|
for region, mirrors in regions.items():
|
||||||
with open(destination, 'w') as mirrorlist:
|
with open(destination, 'w') as mirrorlist:
|
||||||
for mirror in mirrors:
|
for mirror in mirrors:
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ import abc
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import logging
|
import logging
|
||||||
|
from pathlib import Path
|
||||||
from .storage import storage
|
from .storage import storage
|
||||||
|
|
||||||
class LOG_LEVELS:
|
class LOG_LEVELS:
|
||||||
|
|
@ -76,30 +77,28 @@ def stylize_output(text :str, *opts, **kwargs):
|
||||||
def log(*args, **kwargs):
|
def log(*args, **kwargs):
|
||||||
string = orig_string = ' '.join([str(x) for x in args])
|
string = orig_string = ' '.join([str(x) for x in args])
|
||||||
|
|
||||||
|
# Attempt to colorize the output if supported
|
||||||
|
# Insert default colors and override with **kwargs
|
||||||
if supports_color():
|
if supports_color():
|
||||||
kwargs = {'bg' : 'black', 'fg': 'white', **kwargs}
|
kwargs = {'bg' : 'black', 'fg': 'white', **kwargs}
|
||||||
string = stylize_output(string, **kwargs)
|
string = stylize_output(string, **kwargs)
|
||||||
|
|
||||||
if (logfile := storage.get('logfile', None)) and 'file' not in kwargs:
|
# If a logfile is defined in storage,
|
||||||
kwargs['file'] = logfile
|
# we use that one to output everything
|
||||||
|
if (logfile := storage.get('LOG_FILE', None)):
|
||||||
|
absolute_logfile = os.path.join(storage.get('LOG_PATH', './'), logfile)
|
||||||
|
if not os.path.isfile(absolute_logfile):
|
||||||
|
os.makedirs(os.path.dirname(absolute_logfile))
|
||||||
|
Path(absolute_logfile).touch() # Overkill?
|
||||||
|
|
||||||
# Log to a file output unless specifically told to suppress this feature.
|
with open(absolute_logfile, 'a') as log_file:
|
||||||
# (level has no effect on the log file, everything will be written there)
|
logfile.write(f"{orig_string}\n")
|
||||||
if 'file' in kwargs and ('suppress' not in kwargs or kwargs['suppress'] == False):
|
|
||||||
if type(kwargs['file']) is str:
|
|
||||||
with open(kwargs['file'], 'a') as log_file:
|
|
||||||
log_file.write(f"{orig_string}\n")
|
|
||||||
elif kwargs['file']:
|
|
||||||
kwargs['file'].write(f"{orig_string}\n")
|
|
||||||
|
|
||||||
# If we assigned a level, try to log it to systemd's journald.
|
# If we assigned a level, try to log it to systemd's journald.
|
||||||
# Unless the level is higher than we've decided to output interactively.
|
# Unless the level is higher than we've decided to output interactively.
|
||||||
# (Remember, log files still get *ALL* the output despite level restrictions)
|
# (Remember, log files still get *ALL* the output despite level restrictions)
|
||||||
if 'level' in kwargs:
|
if 'level' in kwargs:
|
||||||
if 'LOG_LEVEL' not in storage:
|
if kwargs['level'] > storage.get('LOG_LEVEL', LOG_LEVELS.Info):
|
||||||
storage['LOG_LEVEL'] = LOG_LEVELS.Info
|
|
||||||
|
|
||||||
if kwargs['level'] > storage['LOG_LEVEL']:
|
|
||||||
# Level on log message was Debug, but output level is set to Info.
|
# Level on log message was Debug, but output level is set to Info.
|
||||||
# In that case, we'll drop it.
|
# In that case, we'll drop it.
|
||||||
return None
|
return None
|
||||||
|
|
@ -111,4 +110,5 @@ def log(*args, **kwargs):
|
||||||
|
|
||||||
# Finally, print the log unless we skipped it based on level.
|
# Finally, print the log unless we skipped it based on level.
|
||||||
# And we print the string which may or may not contain color formatting.
|
# And we print the string which may or may not contain color formatting.
|
||||||
print(string)
|
sys.stdout.write(string)
|
||||||
|
sys.stdout.flush()
|
||||||
|
|
@ -15,5 +15,7 @@ storage = {
|
||||||
#os.path.abspath(f'{os.path.dirname(__file__)}/../examples')
|
#os.path.abspath(f'{os.path.dirname(__file__)}/../examples')
|
||||||
],
|
],
|
||||||
'UPSTREAM_URL' : 'https://raw.githubusercontent.com/Torxed/archinstall/master/profiles',
|
'UPSTREAM_URL' : 'https://raw.githubusercontent.com/Torxed/archinstall/master/profiles',
|
||||||
'PROFILE_DB' : None # Used in cases when listing profiles is desired, not mandatory for direct profile grabing.
|
'PROFILE_DB' : None, # Used in cases when listing profiles is desired, not mandatory for direct profile grabing.
|
||||||
|
'LOG_PATH' : '/var/log/archinstall',
|
||||||
|
'LOG_FILE' : 'install.log'
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue