From 1bd24c9733184af15737cb46d1c009c398aaa7ab Mon Sep 17 00:00:00 2001 From: Natalia Serrano Date: Thu, 15 Aug 2024 17:38:00 +0200 Subject: [PATCH] refs #596 separate more functions to their own module --- boottoolsfunctions/{__init__.py => btog.py} | 37 +++++++------- boottoolsfunctions/utils.py | 54 +++++++++++++++++++++ boottoolsgenerator.py | 28 +++++------ 3 files changed, 87 insertions(+), 32 deletions(-) rename boottoolsfunctions/{__init__.py => btog.py} (95%) create mode 100644 boottoolsfunctions/utils.py diff --git a/boottoolsfunctions/__init__.py b/boottoolsfunctions/btog.py similarity index 95% rename from boottoolsfunctions/__init__.py rename to boottoolsfunctions/btog.py index d605174..3f14d92 100644 --- a/boottoolsfunctions/__init__.py +++ b/boottoolsfunctions/btog.py @@ -7,9 +7,10 @@ import shutil import glob import datetime -logger = logging.getLogger ('boottools') from . import utils +logger = logging.getLogger ('boottools') + schroot_conf_tpl = """ [IMGogclient] type=loopback @@ -38,7 +39,7 @@ proc /proc proc defaults 0 0 /tmp /tmp none rw,bind 0 0 """.strip() -def btogGetVar (osarch): +def GetVar (osarch): btdir = '/tmp/opengnsys/oglive_builder' bttargetdir = '/var/lib/tftpboot/ogclient/' btrootfsimg = os.path.join (bttargetdir, 'ogclient.img') @@ -52,7 +53,7 @@ def btogGetVar (osarch): btvirtualdisksize = '3G' return btdir, bttargetdir, btrootfsimg, btrootfsmnt, btrootfsimglabel, log_file, versionboottools, btvirtualdisksize -def btogGetOsInfo1 (type_client): +def GetOsInfo1 (type_client): if 'precise' == type_client: # ogLive 1.0.4-rc2 basado en Ubuntu 12.04 LTS. osdistrib='ubuntu' oscodename='precise' @@ -154,13 +155,13 @@ def btogGetOsInfo1 (type_client): os.exit (1) return osdistrib, oscodename, osrelease, osarch, oshttp -def btogGetOsInfo2 (ogrepo_dir, versionboottools, oscodename, osrelease, osarch): +def GetOsInfo2 (ogrepo_dir, versionboottools, oscodename, osrelease, osarch): gitrelease = subprocess.run (['git', '--git-dir', f'{ogrepo_dir}/.git', 'log', '--date', 'format:r%Y%m%d', '--format=%ad', '-1'], capture_output=True, text=True).stdout.strip() nameisoclient ='-'.join ([versionboottools, oscodename, osrelease, osarch, gitrelease]) namehostclient = '-'.join ([versionboottools, oscodename, gitrelease]) return gitrelease, nameisoclient, namehostclient -def btogSetFsVirtual (btrootfsimg, btrootfsimglabel, btrootfsmnt, btvirtualdisksize, bttargetdir, osarch): +def SetFsVirtual (btrootfsimg, btrootfsimglabel, btrootfsmnt, btvirtualdisksize, bttargetdir, osarch): logger.info (f'Creación y formateo del disco virtual "{btrootfsimg}" "{btvirtualdisksize}" MB') try: utils.umount (btrootfsmnt) @@ -228,8 +229,8 @@ def btogSetFsVirtual (btrootfsimg, btrootfsimglabel, btrootfsmnt, btvirtualdisks logger.info (f'"{btrootfsimg}" "{btvirtualdisksize}" MB : OK') -#btogSetFsAcces: habilita el acceso al sistema root del cliente con schroot -def btogSetFsAccess (btrootfsimg): +#SetFsAcces: habilita el acceso al sistema root del cliente con schroot +def SetFsAccess (btrootfsimg): if (utils.grep (btrootfsimg, '/etc/schroot/schroot.conf')): return logger.info ('Iniciando la configuración del schroot') @@ -250,9 +251,9 @@ def btogSetFsAccess (btrootfsimg): logger.info ('Finalizado: OK') return 0 -# btogSetfsBase: Genera el sistema root base con debootstrap +# SetfsBase: Genera el sistema root base con debootstrap # trabaja sobre un rootfs ya montado -def btogSetFsBase (btrootfsimg, btrootfsmnt, osarch, oscodename, oshttp): +def SetFsBase (btrootfsimg, btrootfsmnt, osarch, oscodename, oshttp): logger.info ('Iniciando la generación del sistema de archivos') logger.info ('debootstrapping system') @@ -263,7 +264,7 @@ def btogSetFsBase (btrootfsimg, btrootfsmnt, osarch, oscodename, oshttp): logger.info (f'debootstrap --arch="{osarch}" --components=main,universe "{oscodename}" "{btrootfsmnt}" "{oshttp}" : ok') return 0 -def boottoolsFsOpengnsys (builder, og_shared, og_engine, ogclientmount, osdistrib, oscodename): +def FsOpengnsys (builder, og_shared, og_engine, ogclientmount, osdistrib, oscodename): print ('Iniciando la personalización con datos del repositorio') sources_list_in = f'{builder}/includes/etc/apt/sources.list.{osdistrib.lower()}' @@ -301,7 +302,7 @@ def boottoolsFsOpengnsys (builder, og_shared, og_engine, ogclientmount, osdistri if os.path.exists (f'{og_shared}/bin/browser'): shutil.copy (f'{og_shared}/bin/browser', f'{ogclientmount}/bin/') if os.path.exists (f'{og_shared}/bin/ogAdmClient'): shutil.copy (f'{og_shared}/bin/ogAdmClient', f'{ogclientmount}/bin/') -def boottoolsSshServer (btrootfsmnt): +def SshServer (btrootfsmnt): if not os.path.exists ('/root/.ssh/id_rsa'): ## crea un par de claves en la VM, no en el chroot utils.run (['ssh-keygen', '-q', '-f', '/root/.ssh/id_rsa', '-N', '']) @@ -331,7 +332,7 @@ def boottoolsSshServer (btrootfsmnt): else: logger.error ('no key publica og') -def boottoolsSshClient (btrootfsmnt): +def SshClient (btrootfsmnt): if not os.path.exists (f'{btrootfsmnt}/root/.ssh/id_rsa'): utils.run (['ssh-keygen', '-q', '-f', f'{btrootfsmnt}/root/.ssh/id_rsa', '-N', '']) ## crea un par de claves en el chroot @@ -348,8 +349,8 @@ def boottoolsSshClient (btrootfsmnt): ## TODO: exportamos la publica a los repos #cp /root/.ssh/id_rsa.pub /tmp/rsa.ogclient.pub -#btogFsInitrd genera un initrd. -def btogFsInitrd (bttargetdir, osrelease): +#FsInitrd genera un initrd. +def FsInitrd (bttargetdir, osrelease): #echo "cp /tmp/*-${OSRELEASE} ${BTTARGETDIR}" #cp /tmp/*-${OSRELEASE} ${BTTARGETDIR} ## esto copia algo?? @@ -372,8 +373,8 @@ def btogFsInitrd (bttargetdir, osrelease): for f in glob.glob (f'{bttargetdir}/oginitrd*') + glob.glob (f'{bttargetdir}/vmlinuz*'): os.chmod (f, 0o755) -#btogFsSqfs convierte el sistema root en sqfs -def btogFsSqfs (bttargetdir, btrootfsmnt): +#FsSqfs convierte el sistema root en sqfs +def FsSqfs (bttargetdir, btrootfsmnt): logger.info ('Iniciando la creación del sistema de archivos en sqfs') # si ya existe un sqfs lo renombramos @@ -386,8 +387,8 @@ def btogFsSqfs (bttargetdir, btrootfsmnt): utils.write_md5 (f'{bttargetdir}/ogclient.sqfs') -# btogIsoGenerator genera la iso del cliente -def btogIsoGenerator (pxepkg, isolinux_tpl, bttargetdir, nameisoclient): +# IsoGenerator genera la iso del cliente +def IsoGenerator (pxepkg, isolinux_tpl, bttargetdir, nameisoclient): #Preparamos los gestores de arranque try: os.makedirs ('/tmp/iso/isolinux', exist_ok=True) except: raise diff --git a/boottoolsfunctions/utils.py b/boottoolsfunctions/utils.py new file mode 100644 index 0000000..b1385f2 --- /dev/null +++ b/boottoolsfunctions/utils.py @@ -0,0 +1,54 @@ +import logging +import subprocess +import re +import os +import configparser + +logger = logging.getLogger ('boottools') + +def run (args): + cp = subprocess.run (args, text=True, capture_output=True) + if cp.returncode: + logger.error ('command "{}" failed with rc "{}"'.format (' '.join(args), cp.returncode)) + + logger.error ('stdout follows:') + for i in cp.stdout.strip().split('\n'): logger.error (' ' + i) + + logger.error ('stderr follows:') + for i in cp.stderr.strip().split('\n'): logger.error (' ' + i) + + raise Exception ('command "{}" failed with rc "{}"'.format (' '.join(args), cp.returncode)) + stdout = cp.stdout.strip() + stderr = cp.stderr.strip() + return stdout, stderr + +def grep (regex, file): + with open (file, 'r') as f: + for l in f: + if (re.findall (regex, l)): return 1 + return 0 + +def is_mounted (mntpt): + return grep (mntpt, '/proc/mounts') + +def mount (dev, mntpt, opts=[]): + if not is_mounted (mntpt): + run (['mount', dev, mntpt] + opts) + +def umount (mntpt): + if (is_mounted (mntpt)): + run (['umount', mntpt]) + +def read_config (fn): + if not os.path.exists (fn): + print (f'configuration file "{fn}" not found') + return + config = configparser.ConfigParser (comment_prefixes='#', inline_comment_prefixes='#') + config.read (fn) + return config + +def write_md5 (fn): + md5, _ = run (['md5sum', fn]) + md5, rest = md5.split (' ', 1) + with open (f'{fn}.sum', 'w') as fd: + fd.write (md5 + '\n') diff --git a/boottoolsgenerator.py b/boottoolsgenerator.py index dbd6e60..03ddb78 100755 --- a/boottoolsgenerator.py +++ b/boottoolsgenerator.py @@ -10,7 +10,7 @@ import shutil curdir = os.path.dirname (__file__) sys.path.insert (0, curdir) -from boottoolsfunctions import utils, btogGetOsInfo1, btogGetOsInfo2, btogGetVar, btogSetFsVirtual, btogSetFsAccess, btogSetFsBase, boottoolsFsOpengnsys, boottoolsSshServer, boottoolsSshClient, btogFsInitrd, btogFsSqfs, btogIsoGenerator +from boottoolsfunctions import utils, btog def _logging(): #logging.root.handlers = [] @@ -61,14 +61,14 @@ def _mkrootfs(): rc = subprocess.run (f'file "{btrootfsimg}" |grep -q "partition 1 *: ID=0x83"', shell=True).returncode print (rc) if (rc): ## 'file|grep' failed - try: btogSetFsVirtual (btrootfsimg, btrootfsimglabel, btrootfsmnt, btvirtualdisksize, bttargetdir, osarch) + try: btog.SetFsVirtual (btrootfsimg, btrootfsimglabel, btrootfsmnt, btvirtualdisksize, bttargetdir, osarch) except Exception as e: logger.error (str (e)) sys.exit (1) def _schroot (btrootfsimg): logger.info ('Fase 3.2 - Configurar acceso schroot al Segundo Sistema de archivos (img)') - btogSetFsAccess (btrootfsimg) + btog.SetFsAccess (btrootfsimg) ## para hacer schroot --cosas, el mntpt tiene que estar desmontado ## si está montado da un pete tal que 'E: 10mount: mount: /run/schroot/mount/IMGogclient-7fbf51a2-e37e-48e5-8e5d-83f8901fc7ed: wrong fs type, bad option, bad superblock on /dev/loop1, missing codepage or helper program, or other error.' @@ -79,9 +79,9 @@ def _debootstrap(): logger.debug ('Running \'schroot --chroot IMGogclient -- stat /etc\'') cp = subprocess.run (['schroot', '--chroot', 'IMGogclient', '--', 'stat', '/etc']) if (cp.returncode): - logger.debug (f'schroot returned code "{cp.returncode}", calling btogSetFsBase()') + logger.debug (f'schroot returned code "{cp.returncode}", calling btog.SetFsBase()') _mount_rootfs() - try: btogSetFsBase (btrootfsimg, btrootfsmnt, osarch, oscodename, oshttp) + try: btog.SetFsBase (btrootfsimg, btrootfsmnt, osarch, oscodename, oshttp) except Exception as e: logger.error (str (e)) sys.exit (1) @@ -97,7 +97,7 @@ def _copy_files (btrootfsmnt, osdistrib, oscodename): builder = '/tmp/opengnsys/oglive_builder' og_shared = '/tmp/opengnsys/shared' og_engine = '/tmp/opengnsys/engine' - boottoolsFsOpengnsys (builder, og_shared, og_engine, btrootfsmnt, osdistrib, oscodename) + btog.FsOpengnsys (builder, og_shared, og_engine, btrootfsmnt, osdistrib, oscodename) utils.umount (btrootfsmnt) def _install_compile_software (curdir, osrelease, osarch): @@ -130,8 +130,8 @@ def _debconf2 (curdir): def _ssh_stuff(): logger.info ('Fase 6.2 Configurar ssh') _mount_rootfs() - boottoolsSshServer (btrootfsmnt) - boottoolsSshClient (btrootfsmnt) + btog.SshServer (btrootfsmnt) + btog.SshClient (btrootfsmnt) utils.umount (btrootfsmnt) ## el resultado es: ## - hay un nuevo par de claves en la VM /root/.ssh @@ -141,14 +141,14 @@ def _ssh_stuff(): def _mkinitrd_squashfs_isofs (bttargetdir, osrelease, btrootfsmnt, pxepkg, isolinux_tpl, nameisoclient): logger.info ('Fase 7.1 Copiar el initrd a su sitio') _mount_rootfs() - btogFsInitrd (bttargetdir, osrelease) + btog.FsInitrd (bttargetdir, osrelease) logger.info ('Fase 7.2 Generar fichero sqfs a partir del fichero img') - btogFsSqfs (bttargetdir, btrootfsmnt) + btog.FsSqfs (bttargetdir, btrootfsmnt) utils.umount (btrootfsmnt) logger.info ('Fase 7.3 Generar la ISO') - btogIsoGenerator (pxepkg, isolinux_tpl, bttargetdir, nameisoclient) + btog.IsoGenerator (pxepkg, isolinux_tpl, bttargetdir, nameisoclient) @@ -174,9 +174,9 @@ ogrepo_dir = '/tmp/ogrepo' clone_client_dirs (ogrepo_url, ogrepo_branch, ogrepo_dir) logger.info ('FASE 1 - Asignación de variables') -osdistrib, oscodename, osrelease, osarch, oshttp = btogGetOsInfo1(type_client) -btdir, bttargetdir, btrootfsimg, btrootfsmnt, btrootfsimglabel, log_file, versionboottools, btvirtualdisksize = btogGetVar(osarch) -gitrelease, nameisoclient, namehostclient = btogGetOsInfo2(ogrepo_dir, versionboottools, oscodename, osrelease, osarch) +osdistrib, oscodename, osrelease, osarch, oshttp = btog.GetOsInfo1(type_client) +btdir, bttargetdir, btrootfsimg, btrootfsmnt, btrootfsimglabel, log_file, versionboottools, btvirtualdisksize = btog.GetVar(osarch) +gitrelease, nameisoclient, namehostclient = btog.GetOsInfo2(ogrepo_dir, versionboottools, oscodename, osrelease, osarch) logger.info (':'.join ([osdistrib, oscodename, osrelease, osarch, oshttp]))