refs #596 refactor read_config, remove unused ogclient.cfg, use templates, make logging level configurable, fix bugs

pull/1/head
Natalia Serrano 2024-08-14 11:32:24 +02:00
parent 45f533aca9
commit 0371870fa0
5 changed files with 76 additions and 75 deletions

View File

@ -7,13 +7,9 @@ import re
import subprocess
import configparser
from boottoolsfunctions import _run
from boottoolsfunctions import _run, _read_config
config = configparser.ConfigParser (comment_prefixes='#', inline_comment_prefixes='#')
if not os.path.exists ('boottoolsgenerator.cfg'):
print ('configuration file "boottoolsgenerator.cfg" not found')
sys.exit (1)
config.read ('boottoolsgenerator.cfg')
config = _read_config ('boottoolsgenerator.cfg')
debconf_settings = config['debconf'].get ('settings')
def _aptget_clean():

View File

@ -3,13 +3,9 @@
import os
import subprocess
import configparser
from boottoolsfunctions import _run
from boottoolsfunctions import _run, _read_config
config = configparser.ConfigParser (inline_comment_prefixes='#')
if not os.path.exists ('boottoolsgenerator.cfg'):
print ('configuration file "boottoolsgenerator.cfg" not found')
sys.exit (1)
config.read ('boottoolsgenerator.cfg')
config = _read_config ('boottoolsgenerator.cfg')
debconf_settings2 = config['debconf'].get ('settings2')
subprocess.run (['debconf-set-selections'], input=debconf_settings2, text=True)

View File

@ -43,6 +43,14 @@ 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')
sys.exit (1)
config = configparser.ConfigParser (comment_prefixes='#', inline_comment_prefixes='#')
config.read (fn)
return config
def btogGetVar (osarch):
btdir = '/tmp/opengnsys_installer/opengnsys/client/boot-tools'
bttargetdir = '/var/lib/tftpboot/ogclient/'
@ -58,7 +66,6 @@ def btogGetVar (osarch):
return btdir, bttargetdir, btrootfsimg, btrootfsmnt, btrootfsimglabel, log_file, versionboottools, btvirtualdisksize
def btogGetOsInfo1 (type_client):
ogclientcfg = '/tmp/ogclient.cfg'
if 'precise' == type_client: # ogLive 1.0.4-rc2 basado en Ubuntu 12.04 LTS.
osdistrib='ubuntu'
oscodename='precise'
@ -158,31 +165,15 @@ def btogGetOsInfo1 (type_client):
else: # Parámetro desconocido
print ('Parámetro no válido.')
os.exit (1)
return ogclientcfg, osdistrib, oscodename, osrelease, osarch, oshttp
return osdistrib, oscodename, osrelease, osarch, oshttp
def btogGetOsInfo2 (type_client, versionboottools, ogclientcfg, osdistrib, oscodename, osrelease, osarch, oshttp):
def btogGetOsInfo2 (type_client, versionboottools, osdistrib, oscodename, osrelease, osarch, oshttp):
branch = 'master'
giturl = f'https://api.github.com/repos/opengnsys/OpenGnsys/commits?sha={branch}&path=/client'
gitrelease = 'r20240808' ## TODO: are we going to keep the following? $(curl -s "$GITURL" | jq -r '"r" + (.[0].commit.committer.date | split("-") | join("")[:8]) + "." + (.[0].sha[:7])')
nameisoclient ='-'.join ([versionboottools, oscodename, osrelease, osarch, gitrelease])
namehostclient = '-'.join ([versionboottools, oscodename, gitrelease])
### El fichero de configuración debe sustituir a estos 3 ficheros (borrar las 3 líneas siguientes).
#echo "$NAMEISOCLIENT" > /tmp/opengnsys_info_rootfs
#echo "$NAMEHOSTCLIENT" > /tmp/opengnsys_chroot
# Generar fichero de configuración.
with open (ogclientcfg, 'w') as f:
print (f'TYPECLIENT="{type_client}"', file=f)
print (f'OSDISTRIB="{osdistrib}"', file=f)
print (f'OSCODENAME="{oscodename}"', file=f)
print (f'OSRELEASE="{osrelease}"', file=f)
print (f'OSARCH="{osarch}"', file=f)
print (f'OSHTTP="{oshttp}"', file=f)
print (f'GITRELEASE="{gitrelease}"', file=f)
print (f'NAMEISOCLIENT="{nameisoclient}"', file=f)
print (f'NAMEHOSTCLIENT="{namehostclient}"', file=f)
return gitrelease, nameisoclient, namehostclient
def btogSetFsVirtual (btrootfsimg, btrootfsimglabel, btrootfsmnt, btvirtualdisksize, bttargetdir, osarch):
@ -225,7 +216,8 @@ def btogSetFsVirtual (btrootfsimg, btrootfsimglabel, btrootfsmnt, btvirtualdisks
logger.info ('Partitioning disk image')
stdout, _ = _run (['bash', '-c', f"echo $'n\np\n1\n\n\nt\n83\nw' |fdisk {diskloop}"])
logger.debug (f'fdisk output follows: {stdout}')
logger.debug (f'fdisk stdout follows:')
for i in stdout.strip().split('\n'): logger.debug (' ' + i)
time.sleep (3)
logger.debug ('losetup --detach')
@ -254,6 +246,34 @@ def btogSetFsVirtual (btrootfsimg, btrootfsimglabel, btrootfsmnt, btvirtualdisks
logger.info (f'"{btrootfsimg}" "{btvirtualdisksize}" MB : OK')
schroot_conf_tpl = """
[IMGogclient]
type=loopback
file=/var/lib/tftpboot/ogclient/ogclient.img
description=ogclient Ubuntu image
#priority=1
users=root
groups=root
root-groups=root
mount-options=-o offset=32256
root-users=root
""".strip()
mount_defaults_tpl = """
# mount.defaults: static file system information for chroots.
# Note that the mount point will be prefixed by the chroot path
# (CHROOT_PATH)
#
# <file system> <mount point> <type> <options> <dump> <pass>
proc /proc proc defaults 0 0
#procbususb /proc/bus/usb usbfs defaults 0 0
#/dev /dev none rw,bind 0 0
/dev/pts /dev/pts none rw,bind 0 0
/dev/shm /dev/shm none rw,bind 0 0
#/home /home none rw,bind 0 0
/tmp /tmp none rw,bind 0 0
""".strip()
#btogSetFsAcces: habilita el acceso al sistema root del cliente con schroot
def btogSetFsAccess (btrootfsimg):
if (_grep (btrootfsimg, '/etc/schroot/schroot.conf')):
@ -265,34 +285,14 @@ def btogSetFsAccess (btrootfsimg):
shutil.copy ('/etc/schroot/schroot.conf', '/etc/schroot/schroot.conf.bak')
with open ('/etc/schroot/schroot.conf', 'w') as f:
print ('[IMGogclient]', file=f)
print ('type=loopback', file=f)
print ('file=/var/lib/tftpboot/ogclient/ogclient.img', file=f)
print ('description=ogclient Ubuntu image', file=f)
print ('#priority=1', file=f)
print ('users=root', file=f)
print ('groups=root', file=f)
print ('root-groups=root', file=f)
print ('mount-options=-o offset=32256', file=f)
print ('root-users=root', file=f)
f.write (schroot_conf_tpl + '\n')
#cp /etc/schroot/mount-defaults /etc/schroot/mount-defaults.`getDateTime`
if (os.path.exists ('/etc/schroot/mount-defaults')):
shutil.copy ('/etc/schroot/mount-defaults', '/etc/schroot/mount-defaults.bak')
with open ('/etc/schroot/mount-defaults', 'w') as f:
print ('# mount.defaults: static file system information for chroots.', file=f)
print ('# Note that the mount point will be prefixed by the chroot path', file=f)
print ('# (CHROOT_PATH)', file=f)
print ('#', file=f)
print ('# <file system> <mount point> <type> <options> <dump> <pass>', file=f)
print ('proc /proc proc defaults 0 0', file=f)
print ('#procbususb /proc/bus/usb usbfs defaults 0 0', file=f)
print ('#/dev /dev none rw,bind 0 0', file=f)
print ('/dev/pts /dev/pts none rw,bind 0 0', file=f)
print ('/dev/shm /dev/shm none rw,bind 0 0', file=f)
print ('#/home /home none rw,bind 0 0', file=f)
print ('/tmp /tmp none rw,bind 0 0', file=f)
f.write (mount_defaults_tpl + '\n')
map (os.unlink, glob.glob('/etc/schroot/setup.d/*chrootname'))
#for i in glob.glob ('/etc/schroot/setup.d/*chrootname'):

View File

@ -62,6 +62,10 @@ xwindows =
#roxterm gparted #+80M
#openbox midori #xvesa en compilacion
[General]
logging_level = INFO
[debconf]
settings =

View File

@ -11,29 +11,29 @@ import shutil
curdir = os.path.dirname (__file__)
sys.path.insert (0, curdir)
from boottoolsfunctions import _run, _mount, _umount, btogGetOsInfo1, btogGetOsInfo2, btogGetVar, btogSetFsVirtual, btogSetFsAccess, btogSetFsBase, boottoolsSshServer, boottoolsSshClient, btogFsInitrd, btogFsSqfs, btogIsoGenerator
from boottoolsfunctions import _run, _mount, _umount, _read_config, btogGetOsInfo1, btogGetOsInfo2, btogGetVar, btogSetFsVirtual, btogSetFsAccess, btogSetFsBase, boottoolsSshServer, boottoolsSshClient, btogFsInitrd, btogFsSqfs, btogIsoGenerator
config = _read_config ('boottoolsgenerator.cfg')
isolinux_tpl = config['ISOLinux'].get ('template')
lvl = config['General'].get ('logging_level')
numeric_level = getattr (logging, lvl.upper(), None)
if numeric_level is None:
numeric_level = getattr (logging, 'INFO')
def _logging():
#logging.root.handlers = []
logging.basicConfig (
format='%(levelname)s %(asctime)s (%(funcName)s) %(message)s',
level=logging.INFO,
level=numeric_level,
handlers = [
logging.FileHandler ('/tmp/boot-tools_installation.log'),
logging.StreamHandler (sys.stdout),
],
)
return = logging.getLogger ('boottools')
return logging.getLogger ('boottools')
logger = _logging()
config = configparser.ConfigParser (comment_prefixes='#', inline_comment_prefixes='#')
if not os.path.exists ('boottoolsgenerator.cfg'):
print ('configuration file "boottoolsgenerator.cfg" not found')
sys.exit (1)
config.read ('boottoolsgenerator.cfg')
isolinux_tpl = config['ISOLinux'].get ('template')
def clone_client_dirs():
if not os.path.exists ('/tmp/opengnsys_installer/opengnsys/client/engine'):
branch = 'main'
@ -59,9 +59,9 @@ clone_client_dirs()
####################################################################3
logger.info ('FASE 1 - Asignación de variables')
ogclientcfg, osdistrib, oscodename, osrelease, osarch, oshttp = btogGetOsInfo1(type_client)
osdistrib, oscodename, osrelease, osarch, oshttp = btogGetOsInfo1(type_client)
btdir, bttargetdir, btrootfsimg, btrootfsmnt, btrootfsimglabel, log_file, versionboottools, btvirtualdisksize = btogGetVar(osarch)
gitrelease, nameisoclient, namehostclient = btogGetOsInfo2(type_client, versionboottools, ogclientcfg, osdistrib, oscodename, osrelease, osarch, oshttp)
gitrelease, nameisoclient, namehostclient = btogGetOsInfo2(type_client, versionboottools, osdistrib, oscodename, osrelease, osarch, oshttp)
def _mount_rootfs():
global btrootfsimg, btrootfsmnt
@ -158,9 +158,10 @@ def _cerodos():
logger.error ('mount failed')
sys.exit (3)
logger.debug (f'running \'{curdir}/02-boottoolsFsOpengnsys.py --mntpt "{btrootfsmnt}" --osdistrib "{osdistrib}" --oscodename "{oscodename}" --osrelease "{osrelease}" --osarch "{osarch}" --oshttp "{oshttp))}"\'')
logger.debug (f'running \'{curdir}/02-boottoolsFsOpengnsys.py --mntpt "{btrootfsmnt}" --osdistrib "{osdistrib}" --oscodename "{oscodename}" --osrelease "{osrelease}" --osarch "{osarch}" --oshttp "{oshttp}"\'')
stdout, _ = _run ([f'{curdir}/02-boottoolsFsOpengnsys.py', '--mntpt', btrootfsmnt, '--osdistrib', osdistrib, '--oscodename', oscodename, '--osrelease', osrelease, '--osarch', osarch, '--oshttp', oshttp])
logger.debug (f'02-boottoolsFsOpengnsys stdout follows: {stdout}')
logger.debug (f'02-boottoolsFsOpengnsys stdout follows:')
for i in stdout.strip().split('\n'): logger.debug (' ' + i)
_umount (btrootfsmnt)
@ -170,9 +171,10 @@ _cerodos()
## aqui empiezan las cosas de chroot
def _cerotres():
logger.debug (f'running \'schroot --chroot IMGogclient -- {curdir}/03-boottoolsSoftwareInstall.py --osrelease "{osrelease}" --osarch "{osarch))}"\'')
logger.debug (f'running \'schroot --chroot IMGogclient -- {curdir}/03-boottoolsSoftwareInstall.py --osrelease "{osrelease}" --osarch "{osarch}"\'')
stdout, _ = _run (['schroot', '--chroot', 'IMGogclient', '--', f'{curdir}/03-boottoolsSoftwareInstall.py', '--osrelease', osrelease, '--osarch', osarch])
logger.debug (f'03-boottoolsSoftwareInstall stdout follows: {stdout}')
logger.debug (f'03-boottoolsSoftwareInstall stdout follows:')
for i in stdout.strip().split('\n'): logger.debug (' ' + i)
logger.info ('FASE 5 - Instalar software')
logger.info ('Fase 5.1 instalar paquetes deb con apt-get')
@ -183,10 +185,11 @@ def _cerocuatro():
#cd /
stdout, _ = _run (['schroot', '--chroot', 'IMGogclient', '--', f'{curdir}/04-boottoolsSoftwareCompile.py'])
#cd -
logger.debug (f'04-boottoolsSoftwareCompile stdout follows: {stdout}')
logger.debug (f'04-boottoolsSoftwareCompile stdout follows:')
for i in stdout.strip().split('\n'): logger.debug (' ' + i)
logger.info ('Fase 5.2 compilar software.')
_cerocuatro():
_cerocuatro()
## ya no chroot
@ -226,7 +229,8 @@ _ssh_stuff()
def _cerocinco():
logger.debug (f'running \'schroot --chroot IMGogclient -- {curdir}/05-boottoolsFsLocales.py\'')
stdout, _ = _run (['schroot', '--chroot', 'IMGogclient', '--', f'{curdir}/05-boottoolsFsLocales.py'])
logger.debug (f'05-boottoolsFsLocales stdout follows: {stdout}')
logger.debug (f'05-boottoolsFsLocales stdout follows:')
for i in stdout.strip().split('\n'): logger.debug (' ' + i)
logger.info ('Fase 6.3 Configurando las locales')
_cerocinco()
@ -234,9 +238,10 @@ _cerocinco()
def _ceroseis():
#cd /
#schroot -c IMGogclient -- /usr/bin/boot-tools/boottoolsInitrdGenerate.sh
logger.debug (f'running \'schroot --chroot IMGogclient -- {curdir}/06-boottoolsInitrdGenerate.py --osrelease "{osrelease))}"\'')
logger.debug (f'running \'schroot --chroot IMGogclient -- {curdir}/06-boottoolsInitrdGenerate.py --osrelease "{osrelease}"\'')
stdout, _ = _run (['schroot', '--chroot', 'IMGogclient', '--', f'{curdir}/06-boottoolsInitrdGenerate.py', '--osrelease', osrelease])
logger.debug (f'06-boottoolsInitrdGenerate stdout follows: {stdout}')
logger.debug (f'06-boottoolsInitrdGenerate stdout follows:')
for i in stdout.strip().split('\n'): logger.debug (' ' + i)
## esto deja initrd.img-6.8.0-31-generic y vmlinuz-6.8.0-31-generic en /tmp
_ceroseis()