added logging

This commit is contained in:
embed@git.macaw.me
2023-12-21 19:42:13 +00:00
parent f7303dce15
commit 06cffbdbd7
14 changed files with 321 additions and 51 deletions

View File

@@ -9,11 +9,15 @@ import sys
import shutil
import configparser
from subprocess import Popen, PIPE
import logging
import traceback
import gentooimgr.config
import gentooimgr.configs
import gentooimgr.common
import gentooimgr.chroot
import gentooimgr.kernel
from gentooimgr import LOG
from gentooimgr import HERE
from gentooimgr.configs import *
@@ -21,7 +25,7 @@ from gentooimgr.configs import *
FILES_DIR = os.path.join(HERE, "..")
def step1_diskprep(args, cfg):
print("\t:: Step 1: Disk Partitioning")
LOG.info("\t:: Step 1: Disk Partitioning")
# http://rainbow.chard.org/2013/01/30/how-to-align-partitions-for-best-performance-using-parted/
# http://honglus.blogspot.com/2013/06/script-to-automatically-partition-new.html
cmds = [
@@ -37,13 +41,13 @@ def step1_diskprep(args, cfg):
completestep(1, "diskprep")
def step2_mount(args, cfg):
print(f'\t:: Step 2: Mounting {gentooimgr.config.GENTOO_MOUNT}')
LOG.info(f'\t:: Step 2: Mounting {gentooimgr.config.GENTOO_MOUNT}')
proc = Popen(["mount", f'{cfg.get("disk")}{cfg.get("partition")}', cfg.get("mountpoint")])
proc.communicate()
completestep(2, "mount")
def step3_stage3(args, cfg):
print(f'\t:: Step 3: Stage3 Tarball')
LOG.info(f'\t:: Step 3: Stage3 Tarball')
stage3 = cfg.get("stage3") or args.stage3 # FIXME: auto detect stage3 images in mountpoint and add here
if not stage3:
@@ -56,12 +60,12 @@ def step3_stage3(args, cfg):
completestep(3, "stage3")
def step4_binds(args, cfg):
print(f'\t:: Step 4: Binding Filesystems')
LOG.info(f'\t:: Step 4: Binding Filesystems')
gentooimgr.chroot.bind(verbose=False)
completestep(4, "binds")
def step5_portage(args, cfg):
print(f'\t:: Step 5: Portage')
LOG.info(f'\t:: Step 5: Portage')
portage = cfg.get("portage") or args.portage
if not portage:
portage = gentooimgr.common.portage_from_dir(FILES_DIR)
@@ -80,7 +84,7 @@ def step5_portage(args, cfg):
completestep(5, "portage")
def step6_licenses(args, cfg):
print(f'\t:: Step 6: Licenses')
LOG.info(f'\t:: Step 6: Licenses')
license_path = os.path.join(cfg.get("mountpoint"), 'etc', 'portage', 'package.license')
os.makedirs(license_path, exist_ok=True)
for f, licenses in cfg.get("licensefiles", {}).items():
@@ -89,7 +93,7 @@ def step6_licenses(args, cfg):
completestep(6, "license")
def step7_repos(args, cfg):
print(f'\t:: Step 7: Repo Configuration')
LOG.info(f'\t:: Step 7: Repo Configuration')
repo_path = os.path.join(cfg.get("mountpoint"), 'etc', 'portage', 'repos.conf')
os.makedirs(repo_path, exist_ok=True)
# Copy from template
@@ -115,7 +119,7 @@ def step7_repos(args, cfg):
completestep(7, "repos")
def step8_resolv(args, cfg):
print(f'\t:: Step 8: Resolv')
LOG.info(f'\t:: Step 8: Resolv')
proc = Popen(["cp", "--dereference", "/etc/resolv.conf", os.path.join(cfg.get("mountpoint"), 'etc')])
proc.communicate()
# Copy all step files and python module to new chroot
@@ -124,20 +128,20 @@ def step8_resolv(args, cfg):
completestep(8, "resolv")
def step9_sync(args, cfg):
print(f"\t:: Step 9: sync")
print("\t\t:: Entering chroot")
LOG.info(f"\t:: Step 9: sync")
LOG.info("\t\t:: Entering chroot")
os.chroot(cfg.get("mountpoint"))
os.chdir(os.sep)
os.system("source /etc/profile")
proc = Popen(["emerge", "--sync", "--quiet"])
proc.communicate()
print("\t\t:: Emerging base")
LOG.info("\t\t:: Emerging base")
proc = Popen(["emerge", "--update", "--deep", "--newuse", "--keep-going", "@world"])
proc.communicate()
completestep(9, "sync")
def step10_emerge_pkgs(args, cfg):
print(f"\t:: Step 10: emerge pkgs")
LOG.info(f"\t:: Step 10: emerge pkgs")
packages = cfg.get("packages", {})
for oneshot_up in packages.get("oneshots", []):
proc = Popen(["emerge", "--oneshot", "--update", oneshot_up])
@@ -147,7 +151,7 @@ def step10_emerge_pkgs(args, cfg):
proc = Popen(["emerge", "-j1", single])
proc.communicate()
print("KERNEL PACKAGES", packages.get("kernel"))
LOG.info("KERNEL PACKAGES", packages.get("kernel"))
if packages.get("kernel", []):
cmd = ["emerge", "-j", str(args.threads)] + packages.get("kernel", [])
proc = Popen(cmd)
@@ -162,14 +166,14 @@ def step10_emerge_pkgs(args, cfg):
cmd += packages.get("base", [])
cmd += packages.get("additional", [])
cmd += packages.get("bootloader", [])
print(cmd)
LOG.info(cmd)
proc = Popen(cmd)
proc.communicate()
completestep(10, "pkgs")
def step11_kernel(args, cfg):
# at this point, genkernel will be installed
print(f"\t:: Step 11: kernel")
LOG.info(f"\t:: Step 11: kernel")
proc = Popen(["eselect", "kernel", "set", "1"])
proc.communicate()
if not args.kernel_dist:
@@ -180,7 +184,7 @@ def step11_kernel(args, cfg):
completestep(11, "kernel")
def step12_grub(args, cfg):
print(f"\t:: Step 12: kernel")
LOG.info(f"\t:: Step 12: kernel")
proc = Popen(["grub-install", cfg.get('disk')])
proc.communicate()
code = proc.returncode
@@ -196,13 +200,13 @@ def step12_grub(args, cfg):
completestep(12, "grub")
def step13_serial(args, cfg):
print(f"\t:: Step 13: Serial")
LOG.info(f"\t:: Step 13: Serial")
os.system("sed -i 's/^#s0:/s0:/g' /etc/inittab")
os.system("sed -i 's/^#s1:/s1:/g' /etc/inittab")
completestep(13, "serial")
def step14_services(args, cfg):
print(f"\t:: Step 14: Services")
LOG.info(f"\t:: Step 14: Services")
for service in ["acpid", "syslog-ng", "cronie", "sshd", "cloud-init-local", "cloud-init", "cloud-config",
"cloud-final", "ntpd", "nfsclient"]:
if args.profile == "systemd":
@@ -214,11 +218,11 @@ def step14_services(args, cfg):
completestep(14, "services")
def step15_ethnaming(args, cfg):
print(f"\t:: Step 15: Eth Naming")
LOG.info(f"\t:: Step 15: Eth Naming")
completestep(15, "networking")
def step16_sysconfig(args, cfg):
print(f"\t:: Step 16: Sysconfig")
LOG.info(f"\t:: Step 16: Sysconfig")
with open("/etc/timezone", "w") as f:
f.write("UTC")
proc = Popen(["emerge", "--config", "sys-libs/timezone-data"])
@@ -270,7 +274,7 @@ def step16_sysconfig(args, cfg):
completestep(16, "sysconfig")
def step17_fstab(args, cfg):
print(f"\t:: Step 17: fstab")
LOG.info(f"\t:: Step 17: fstab")
with open(os.path.join(os.sep, 'etc', 'fstab'), 'a') as fstab:
fstab.write(f"{cfg.get('disk')}\t/\text4\tdefaults,noatime\t0 1\n")