refs #596 use f-strings, refactor the main program a bit

pull/1/head
Natalia Serrano 2024-08-14 09:47:28 +02:00
parent 4ac6b8cdd4
commit c5e9f6858d
6 changed files with 245 additions and 201 deletions

View File

@ -20,8 +20,8 @@ def boottoolsFsOpengnsys (ogclientmount, osdistrib, oscodename, osrelease, osarc
print ('Iniciando la personalización con datos del repositorio')
#sed -e "s/OSCODENAME/$OSCODENAME/g" ${SVNCLIENTDIR}/includes/etc/apt/sources.list.${OSDISTRIB,,} > ${SVNCLIENTDIR}/includes/etc/apt/sources.list
sources_list_in = '{}/includes/etc/apt/sources.list.{}'.format (svnclientdir, osdistrib.lower())
sources_list_out = '{}/includes/etc/apt/sources.list'.format (svnclientdir)
sources_list_in = f'{svnclientdir}/includes/etc/apt/sources.list.{osdistrib.lower()}'
sources_list_out = f'{svnclientdir}/includes/etc/apt/sources.list'
fdin = open (sources_list_in, 'r')
fdout = open (sources_list_out, 'w')
while True:
@ -31,32 +31,30 @@ def boottoolsFsOpengnsys (ogclientmount, osdistrib, oscodename, osrelease, osarc
fdin.close()
fdout.close()
subprocess.run ('chmod -R 775 {}/includes/usr/bin/*'.format (svnclientdir), shell=True)
subprocess.run (f'chmod -R 775 {svnclientdir}/includes/usr/bin/*', shell=True)
os.makedirs ('{}/opt/opengnsys/lib/engine/bin/'.format (ogclientmount), exist_ok=True)
os.makedirs ('{}/usr/local/etc' .format (ogclientmount), exist_ok=True)
os.makedirs ('{}/usr/local/lib' .format (ogclientmount), exist_ok=True)
os.makedirs ('{}/usr/local/plugins' .format (ogclientmount), exist_ok=True)
os.makedirs (f'{ogclientmount}/opt/opengnsys/lib/engine/bin/', exist_ok=True)
os.makedirs (f'{ogclientmount}/usr/local/etc', exist_ok=True)
os.makedirs (f'{ogclientmount}/usr/local/lib', exist_ok=True)
os.makedirs (f'{ogclientmount}/usr/local/plugins', exist_ok=True)
subprocess.run ('rsync -aH {}/includes/* {}/' .format (svnclientdir, ogclientmount), shell=True)
subprocess.run ('rsync -aH {}/* {}/opt/opengnsys/' .format (svnclientstructure, ogclientmount), shell=True)
subprocess.run ('rsync -aH {}/* {}/opt/opengnsys/lib/engine/bin/'.format (svnclientengine, ogclientmount), shell=True)
subprocess.run (f'rsync -aH {svnclientdir}/includes/* {ogclientmount}/' , shell=True)
subprocess.run (f'rsync -aH {svnclientstructure}/* {ogclientmount}/opt/opengnsys/' , shell=True)
subprocess.run (f'rsync -aH {svnclientengine}/* {ogclientmount}/opt/opengnsys/lib/engine/bin/', shell=True)
# Si no existe, copiar pci.ids.
if not os.path.exists ('{}/etc/pci.ids'.format (ogclientmount)):
shutil.copy ('{}/lib/pci.ids'.format (svnclientstructure), '{}/etc/'.format (ogclientmount))
if not os.path.exists (f'{ogclientmount}/etc/pci.ids'):
shutil.copy (f'{svnclientstructure}/lib/pci.ids', f'{ogclientmount}/etc/')
# Dependencias Qt para el Browser.
subprocess.run ('rsync -aH {}/etc/*.qmap {}/usr/local/etc' .format (svnclientstructure, ogclientmount), shell=True)
subprocess.run ('rsync -aH {}/lib/qtlib/* {}/usr/local/lib' .format (svnclientstructure, ogclientmount), shell=True)
subprocess.run ('rsync -aH {}/lib/fonts {}/usr/local/lib' .format (svnclientstructure, ogclientmount), shell=True)
subprocess.run ('rsync -aH {}/lib/qtplugins/* {}/usr/local/plugins'.format (svnclientstructure, ogclientmount), shell=True)
subprocess.run (f'rsync -aH {svnclientstructure}/etc/*.qmap {ogclientmount}/usr/local/etc', shell=True)
subprocess.run (f'rsync -aH {svnclientstructure}/lib/qtlib/* {ogclientmount}/usr/local/lib', shell=True)
subprocess.run (f'rsync -aH {svnclientstructure}/lib/fonts {ogclientmount}/usr/local/lib', shell=True)
subprocess.run (f'rsync -aH {svnclientstructure}/lib/qtplugins/* {ogclientmount}/usr/local/plugins', shell=True)
# Browser y ogAdmClient.
if os.path.exists ('{}/bin/browser'.format (svnclientstructure)):
shutil.copy ('{}/bin/browser'.format (svnclientstructure), '{}/bin/'.format (ogclientmount))
if os.path.exists ('{}/bin/ogAdmClient'.format (svnclientstructure)):
shutil.copy ('{}/bin/ogAdmClient'.format (svnclientstructure), '{}/bin/'.format (ogclientmount))
if os.path.exists (f'{svnclientstructure}/bin/browser'): shutil.copy (f'{svnclientstructure}/bin/browser', f'{ogclientmount}/bin/')
if os.path.exists (f'{svnclientstructure}/bin/ogAdmClient'): shutil.copy (f'{svnclientstructure}/bin/ogAdmClient', f'{ogclientmount}/bin/')
# El fichero de configuración debe sustituir a los 2 ficheros (borrar las 2 líneas).
#echo "${VERSIONBOOTTOOLS}-${OSCODENAME}-${OSRELEASE}-${GITRELEASE}" > $nameisoclientfile

View File

@ -78,7 +78,7 @@ def boottoolsSoftwareInstall (osarch, osrelease):
_mock_mtab()
# Instalamos el kernel.
_aptget_install (['linux-image-{}'.format (osrelease), 'linux-headers-{}'.format (osrelease), 'linux-modules-{}'.format (osrelease), 'linux-modules-extra-{}'.format (osrelease), 'dkms', 'shim-signed', 'openssl'])
_aptget_install ([f'linux-image-{osrelease}', f'linux-headers-{osrelease}', f'linux-modules-{osrelease}', f'linux-modules-extra-{osrelease}', 'dkms', 'shim-signed', 'openssl'])
subprocess.run (['debconf-set-selections'], input=debconf_settings, text=True)
@ -95,10 +95,10 @@ def boottoolsSoftwareInstall (osarch, osrelease):
stdout, _ = _run (['dkms', 'status'])
for l in stdout.strip().split ('\n'):
if not l: continue
print ('l "{}"'.format (l))
print (f'l "{l}"')
mod, vers, status = l.split (',')
if 'added' in status:
print ('dkms installing {} {}'.format (mod, vers))
print (f'dkms installing {mod} {vers}')
_run (['dkms', 'install', '-m', mod.strip(), '-v', vers.strip()])
_oghook_activate()

View File

@ -18,7 +18,7 @@ except:
_run (['wget', 'https://sourceforge.net/projects/ms-sys/files/latest/download', '-O', 'ms-sys.tar.gz'])
_run (['tar', '-xpzf', 'ms-sys.tar.gz'])
mssys_dir = subprocess.run (['tar tzf ms-sys.tar.gz |head -n 1'], shell=True, capture_output=True, text=True).stdout.strip()
print ('mssys_dir "{}"'.format (mssys_dir))
print (f'mssys_dir "{mssys_dir}"')
os.chdir (mssys_dir)
_run (['make', 'install'])
os.chdir ('..')

View File

@ -12,8 +12,8 @@ def boottoolsInitrdGenerate (osrelease):
shutil.copy ('/bin/busybox', '/usr/lib/initramfs-tools/bin')
os.chdir ('/tmp')
_run (['mkinitramfs', '-o', '/tmp/initrd.img-{}'.format (osrelease), '-v', osrelease])
shutil.copy ('/boot/vmlinuz-{}'.format (osrelease), '/tmp/')
_run (['mkinitramfs', '-o', f'/tmp/initrd.img-{osrelease}', '-v', osrelease])
shutil.copy (f'/boot/vmlinuz-{osrelease}', '/tmp/')
if __name__ == '__main__':
parser = argparse.ArgumentParser()

View File

@ -158,7 +158,7 @@ def btogGetOsInfo1 (type_client):
def btogGetOsInfo2 (type_client, versionboottools, ogclientcfg, osdistrib, oscodename, osrelease, osarch, oshttp):
branch = 'master'
giturl = 'https://api.github.com/repos/opengnsys/OpenGnsys/commits?sha={}&path=/client'.format (branch)
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])
@ -169,46 +169,46 @@ def btogGetOsInfo2 (type_client, versionboottools, ogclientcfg, osdistrib, oscod
# Generar fichero de configuración.
with open (ogclientcfg, 'w') as f:
print ('TYPECLIENT="{}"'.format (type_client), file=f)
print ('OSDISTRIB="{}"'.format (osdistrib), file=f)
print ('OSCODENAME="{}"'.format (oscodename), file=f)
print ('OSRELEASE="{}"'.format (osrelease), file=f)
print ('OSARCH="{}"'.format (osarch), file=f)
print ('OSHTTP="{}"'.format (oshttp), file=f)
print ('GITRELEASE="{}"'.format (gitrelease), file=f)
print ('NAMEISOCLIENT="{}"'.format (nameisoclient), file=f)
print ('NAMEHOSTCLIENT="{}"'.format (namehostclient), file=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):
#if not btrootfsimg: btogGetVar()
logger.info ('Creación y formateo del disco virtual "{}" "{}" MB'.format (btrootfsimg, btvirtualdisksize))
logger.info (f'Creación y formateo del disco virtual "{btrootfsimg}" "{btvirtualdisksize}" MB')
try: _umount (btrootfsmnt)
except: pass
if (_is_mounted (btrootfsmnt)):
raise Exception ('failed to umount "{}"'.format (btrootfsmnt))
raise Exception (f'failed to umount "{btrootfsmnt}"')
try: os.makedirs (btrootfsmnt, exist_ok=True)
except:
raise Exception ('Creando directorio "{}" : ERROR'.format (btrootfsmnt))
raise Exception (f'Creando directorio "{btrootfsmnt}" : ERROR')
try: _run (['chown', '-R', 'root:opengnsys', bttargetdir])
except:
raise Exception ('Failed to chown root:opengnsys "{}"'.format (btrootfsmnt))
raise Exception (f'Failed to chown root:opengnsys "{btrootfsmnt}"')
logger.info ('Creating disk image "{}"'.format (btrootfsimg))
logger.info (f'Creating disk image "{btrootfsimg}"')
if 'i386' == osarch:
try: _run (['dd', 'if=/dev/zero', 'of={}'.format (btrootfsimg), 'bs=1048576', 'count={}'.format (btvirtualdisksize)])
try: _run (['dd', 'if=/dev/zero', f'of={btrootfsimg}', 'bs=1048576', f'count={btvirtualdisksize}'])
except:
raise Exception ('Creando el disco virtual "{}" con tamaño maxima "{}" MB : ERROR'.format (btrootfsimg, btvirtualdisksize))
raise Exception (f'Creando el disco virtual "{btrootfsimg}" con tamaño maxima "{btvirtualdisksize}" MB : ERROR')
else:
try: _run (['qemu-img', 'create', btrootfsimg, btvirtualdisksize])
except:
raise Exception ('Creando el disco virtual "{}" con tamaño maxima "{}" MB : ERROR'.format (btrootfsimg, btvirtualdisksize))
raise Exception (f'Creando el disco virtual "{btrootfsimg}" con tamaño maxima "{btvirtualdisksize}" MB : ERROR')
logger.debug ('losetup --find')
diskloop, _ = _run (['losetup', '--find'])
@ -220,8 +220,8 @@ def btogSetFsVirtual (btrootfsimg, btrootfsimglabel, btrootfsmnt, btvirtualdisks
except: raise Exception ('losetup failed')
logger.info ('Partitioning disk image')
stdout, _ = _run (['bash', '-c', "echo $'n\np\n1\n\n\nt\n83\nw' |fdisk {}".format (diskloop)])
logger.debug ('fdisk output follows: {}'.format (stdout))
stdout, _ = _run (['bash', '-c', f"echo $'n\np\n1\n\n\nt\n83\nw' |fdisk {diskloop}"])
logger.debug (f'fdisk output follows: {stdout}')
time.sleep (3)
logger.debug ('losetup --detach')
@ -248,7 +248,7 @@ def btogSetFsVirtual (btrootfsimg, btrootfsimglabel, btrootfsmnt, btvirtualdisks
except:
raise Exception ('Liberando la particion virtual despues del formateo: ERROR')
logger.info ('"{}" "{}" MB : OK'.format (btrootfsimg, btvirtualdisksize))
logger.info (f'"{btrootfsimg}" "{btvirtualdisksize}" MB : OK')
#btogSetFsAcces: habilita el acceso al sistema root del cliente con schroot
def btogSetFsAccess (btrootfsimg):
@ -307,14 +307,14 @@ def btogSetFsBase (btrootfsimg, btrootfsmnt, osarch, oscodename, oshttp):
raise Exception ('rootfs should be mounted')
logger.info ('debootstrapping system')
logger.debug ('debootstrap --arch="{}" --components=main,universe "{}" "{}" "{}"'.format (osarch, oscodename, btrootfsmnt, oshttp))
try: _run (['debootstrap', '--arch={}'.format(osarch), '--components=main,universe', oscodename, btrootfsmnt, oshttp])
logger.debug (f'debootstrap --arch="{osarch}" --components=main,universe "{oscodename}" "{btrootfsmnt}" "{oshttp}"')
try: _run (['debootstrap', f'--arch={osarch}', '--components=main,universe', oscodename, btrootfsmnt, oshttp])
except:
if (_is_mounted (btrootfsmnt)):
_run (['umount', btrootfsmnt])
raise Exception ('debootstrap --arch="{}" --components=main,universe "{}" "{}" "{}" : ha fallado!'.format (osarch, oscodename, btrootfsmnt, oshttp))
raise Exception (f'debootstrap --arch="{osarch}" --components=main,universe "{oscodename}" "{btrootfsmnt}" "{oshttp}" : ha fallado!')
logger.info ('debootstrap --arch="{}" --components=main,universe "{}" "{}" "{}" : ok'.format (osarch, oscodename, btrootfsmnt, oshttp))
logger.info (f'debootstrap --arch="{osarch}" --components=main,universe "{oscodename}" "{btrootfsmnt}" "{oshttp}" : ok')
return 0
def boottoolsSshServer (btrootfsmnt):
@ -323,22 +323,22 @@ def boottoolsSshServer (btrootfsmnt):
#shutil.copy ('/root/.ssh/id_rsa.pub', '/tmp/')
logger.debug ('comprobando directorio .ssh del root')
if not os.path.exists ('{}/root/.ssh'.format (btrootfsmnt)): ## crea directorio dentro del chroot
if not os.path.exists (f'{btrootfsmnt}/root/.ssh'): ## crea directorio dentro del chroot
logger.debug ('creando directorio .ssh 600')
os.mkdir ('{}/root/.ssh'.format (btrootfsmnt))
os.chmod ('{}/root/.ssh'.format (btrootfsmnt), 0o700)
os.mkdir (f'{btrootfsmnt}/root/.ssh')
os.chmod (f'{btrootfsmnt}/root/.ssh', 0o700)
logger.debug ('creando el fichero authorized_keys') ## crea archivo en el chroot
if not os.path.exists ('{}/root/.ssh/authorized_keys'.format (btrootfsmnt)):
open ('{}/root/.ssh/authorized_keys'.format (btrootfsmnt), 'w').close()
os.chmod ('{}/root/.ssh/authorized_keys'.format (btrootfsmnt), 0o600)
if not os.path.exists (f'{btrootfsmnt}/root/.ssh/authorized_keys'):
open (f'{btrootfsmnt}/root/.ssh/authorized_keys', 'w').close()
os.chmod (f'{btrootfsmnt}/root/.ssh/authorized_keys', 0o600)
logger.debug ('importando la clave publica del servidor OG')
#cat /tmp/id_rsa.pub
if os.path.exists ('/root/.ssh/id_rsa.pub'): ## coge la publica de la VM y la pone en el authorized_keys del chroot
#cat /tmp/id_rsa.pub >> /root/.ssh/authorized_keys
fdin = open ('/root/.ssh/id_rsa.pub', 'r')
fdout = open ('{}/root/.ssh/authorized_keys'.format (btrootfsmnt), 'a')
fdout = open (f'{btrootfsmnt}/root/.ssh/authorized_keys', 'a')
while True:
l = fdin.readline()
if not l: break
@ -350,12 +350,12 @@ def boottoolsSshServer (btrootfsmnt):
logger.error ('no key publica og')
def boottoolsSshClient (btrootfsmnt):
if not os.path.exists ('{}/root/.ssh/id_rsa'.format (btrootfsmnt)):
_run (['ssh-keygen', '-q', '-f', '{}/root/.ssh/id_rsa'.format (btrootfsmnt), '-N', '']) ## crea un par de claves en el chroot
if not os.path.exists (f'{btrootfsmnt}/root/.ssh/id_rsa'):
_run (['ssh-keygen', '-q', '-f', f'{btrootfsmnt}/root/.ssh/id_rsa', '-N', '']) ## crea un par de claves en el chroot
#cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys ## coge la publica y se la autoriza a sí mismo
fdin = open ('{}//root/.ssh/id_rsa.pub'.format (btrootfsmnt), 'r')
fdout = open ('{}/root/.ssh/authorized_keys'.format (btrootfsmnt), 'a')
fdin = open (f'{btrootfsmnt}//root/.ssh/id_rsa.pub', 'r')
fdout = open (f'{btrootfsmnt}/root/.ssh/authorized_keys', 'a')
while True:
l = fdin.readline()
if not l: break
@ -373,33 +373,33 @@ def btogFsInitrd (bttargetdir, osrelease):
## backup de oginitrd.img, oginitrd.img.sum, ogvmlinuz y ogvmlinuz.sum
now = datetime.datetime.now(datetime.timezone.utc).strftime ('%Y%m%d-%H%M%S%z')
if os.path.exists ('{}/oginitrd.img'.format (bttargetdir)):
os.rename ('{}/oginitrd.img' .format (bttargetdir), '{}/oginitrd.img.{}' .format (bttargetdir, now))
os.rename ('{}/oginitrd.img.sum'.format (bttargetdir), '{}/oginitrd.img.sum.{}'.format (bttargetdir, now))
if os.path.exists ('{}/ogvmlinuz'.format (bttargetdir)):
os.rename ('{}/ogvmlinuz' .format (bttargetdir), '{}/ogvmlinuz.{}' .format (bttargetdir, now))
os.rename ('{}/ogvmlinuz.sum'.format (bttargetdir), '{}/ogvmlinuz.sum.{}'.format (bttargetdir, now))
if os.path.exists (f'{bttargetdir}/oginitrd.img'):
os.rename (f'{bttargetdir}/oginitrd.img' , f'{bttargetdir}/oginitrd.img.{now}')
os.rename (f'{bttargetdir}/oginitrd.img.sum', f'{bttargetdir}/oginitrd.img.sum.{now}')
if os.path.exists (f'{bttargetdir}/ogvmlinuz'):
os.rename (f'{bttargetdir}/ogvmlinuz' , f'{bttargetdir}/ogvmlinuz.{now}')
os.rename (f'{bttargetdir}/ogvmlinuz.sum', f'{bttargetdir}/ogvmlinuz.sum.{now}')
shutil.copy ('/tmp/initrd.img-{}'.format (osrelease), '{}/oginitrd.img'.format (bttargetdir))
shutil.copy ('/tmp/vmlinuz-{}' .format (osrelease), '{}/ogvmlinuz' .format (bttargetdir))
shutil.copy (f'/tmp/initrd.img-{osrelease}', f'{bttargetdir}/oginitrd.img')
shutil.copy (f'/tmp/vmlinuz-{osrelease}', f'{bttargetdir}/ogvmlinuz')
#DATASUM=`md5sum "${BTTARGETDIR}/oginitrd.img" | cut -f1 -d" "`
md5, _ = _run (['md5sum', '{}/oginitrd.img'.format (bttargetdir)])
md5, _ = _run (['md5sum', f'{bttargetdir}/oginitrd.img'])
md5, rest = md5.split (' ', 1)
#echo $DATASUM > ${BTTARGETDIR}/oginitrd.img.sum
with open ('{}/oginitrd.img.sum'.format (bttargetdir), 'w') as fd:
with open (f'{bttargetdir}/oginitrd.img.sum', 'w') as fd:
fd.write (md5)
#DATASUM=`md5sum "${BTTARGETDIR}/ogvmlinuz" | cut -f1 -d" "`
md5, _ = _run (['md5sum', '{}/ogvmlinuz'.format (bttargetdir)])
md5, _ = _run (['md5sum', f'{bttargetdir}/ogvmlinuz'])
md5, rest = md5.split (' ', 1)
#echo $DATASUM > ${BTTARGETDIR}/ogvmlinuz.sum
with open ('{}/ogvmlinuz.sum'.format (bttargetdir), 'w') as fd:
with open (f'{bttargetdir}/ogvmlinuz.sum', 'w') as fd:
fd.write (md5)
#cd -
#chmod -R 755 $BTTARGETDIR
for f in glob.glob ('{}/oginitrd*'.format (bttargetdir)) + glob.glob ('{}/vmlinuz*'.format (bttargetdir)):
for f in glob.glob (f'{bttargetdir}/oginitrd*') + glob.glob (f'{bttargetdir}/vmlinuz*'):
os.chmod (f, 0o755)
#btogFsSqfs convierte el sistema root en sqfs
@ -407,18 +407,18 @@ def btogFsSqfs (bttargetdir, btrootfsmnt):
logger.info ('Iniciando la creación del sistema de archivos en sqfs')
# si ya existe un sqfs lo renombramos
if os.path.exists ('{}/ogclient.sqfs'.format (bttargetdir)):
if os.path.exists (f'{bttargetdir}/ogclient.sqfs'):
now = datetime.datetime.now(datetime.timezone.utc).strftime ('%Y%m%d-%H%M%S%z')
os.rename ('{}/ogclient.sqfs'.format (bttargetdir), '{}/ogclient.sqfs.{}'.format (bttargetdir, now))
os.rename (f'{bttargetdir}/ogclient.sqfs', f'{bttargetdir}/ogclient.sqfs.{now}')
_run (['mksquashfs', btrootfsmnt, '{}/ogclient.sqfs'.format (bttargetdir)])
os.chmod ('{}/ogclient.sqfs'.format (bttargetdir), 0o744)
_run (['mksquashfs', btrootfsmnt, f'{bttargetdir}/ogclient.sqfs'])
os.chmod (f'{bttargetdir}/ogclient.sqfs', 0o744)
#DATASUM=`md5sum "${BTTARGETDIR}/ogclient.sqfs" | cut -f1 -d" "`
md5, _ = _run (['md5sum', '{}/ogclient.sqfs'.format (bttargetdir)])
md5, _ = _run (['md5sum', f'{bttargetdir}/ogclient.sqfs'])
md5, rest = md5.split (' ', 1)
#echo $DATASUM > ${BTTARGETDIR}/ogclient.sqfs.sum
with open ('{}/ogclient.sqfs.sum'.format (bttargetdir), 'w') as fd:
with open (f'{bttargetdir}/ogclient.sqfs.sum', 'w') as fd:
fd.write (md5)
# btogIsoGenerator genera la iso del cliente
@ -434,16 +434,16 @@ def btogIsoGenerator (pxepkg, isolinux_tpl, bttargetdir, nameisoclient):
elif 'ipxe' == pxepkg:
subprocess.run (['cp -a /usr/lib/ipxe/* /tmp/iso/isolinux'], shell=True)
else:
raise Exception ('unknown pxepkg value "{}"'.format (pxepkg))
raise Exception (f'unknown pxepkg value "{pxepkg}"')
# Si existe el fichero ISO, montarlo para extraer isolinux.bin.
if os.path.exists ('/tmp/iso/isolinux/{}.iso'.format (pxepkg)):
if os.path.exists (f'/tmp/iso/isolinux/{pxepkg}.iso'):
os.mkdir ('/tmp/iso/isolinux/mount')
_run (['mount', '-o', 'loop', '/tmp/iso/isolinux/{}.iso'.format (pxepkg), '/tmp/iso/isolinux/mount'])
_run (['mount', '-o', 'loop', f'/tmp/iso/isolinux/{pxepkg}.iso', '/tmp/iso/isolinux/mount'])
subprocess.run (['cp -a /tmp/iso/isolinux/mount/* /tmp/iso/isolinux'], shell=True)
_run (['umount', '/tmp/iso/isolinux/mount'])
os.rmdir ('/tmp/iso/isolinux/mount')
os.unlink ('/tmp/iso/isolinux/{}.iso'.format (pxepkg))
os.unlink (f'/tmp/iso/isolinux/{pxepkg}.iso')
## ojo que aquí hay que interpolar $NAMEISOCLIENT y $PXEPKG
with open ('/tmp/iso/isolinux/isolinux.cfg', 'w') as fd:
@ -452,33 +452,33 @@ def btogIsoGenerator (pxepkg, isolinux_tpl, bttargetdir, nameisoclient):
# preparamos el directorio boot-tools.
if not os.path.exists ('/tmp/iso/ogclient'):
os.mkdir ('/tmp/iso/ogclient')
shutil.copy ('{}/ogclient.sqfs' .format (bttargetdir), '/tmp/iso/ogclient/')
shutil.copy ('{}/ogclient.sqfs.sum'.format (bttargetdir), '/tmp/iso/ogclient/')
shutil.copy ('{}/ogvmlinuz' .format (bttargetdir), '/tmp/iso/ogclient/')
shutil.copy ('{}/ogvmlinuz.sum' .format (bttargetdir), '/tmp/iso/ogclient/')
shutil.copy ('{}/ogvmlinuz' .format (bttargetdir), '/tmp/iso/ogclient/linuxISO')
shutil.copy ('{}/ogvmlinuz.sum' .format (bttargetdir), '/tmp/iso/ogclient/linuxISO.sum')
shutil.copy ('{}/oginitrd.img' .format (bttargetdir), '/tmp/iso/ogclient/')
shutil.copy ('{}/oginitrd.img.sum' .format (bttargetdir), '/tmp/iso/ogclient/')
shutil.copy (f'{bttargetdir}/ogclient.sqfs', '/tmp/iso/ogclient/')
shutil.copy (f'{bttargetdir}/ogclient.sqfs.sum', '/tmp/iso/ogclient/')
shutil.copy (f'{bttargetdir}/ogvmlinuz', '/tmp/iso/ogclient/')
shutil.copy (f'{bttargetdir}/ogvmlinuz.sum', '/tmp/iso/ogclient/')
shutil.copy (f'{bttargetdir}/ogvmlinuz', '/tmp/iso/ogclient/linuxISO')
shutil.copy (f'{bttargetdir}/ogvmlinuz.sum', '/tmp/iso/ogclient/linuxISO.sum')
shutil.copy (f'{bttargetdir}/oginitrd.img', '/tmp/iso/ogclient/')
shutil.copy (f'{bttargetdir}/oginitrd.img.sum', '/tmp/iso/ogclient/')
#el ogclienteToISO debe tener una copia del ogvmlinuz como linuxISO
#cp -prv /var/lib/tftpboot/ogclientToIso/* /tmp/iso/ogclient
oldpwd = os.getcwd()
os.chdir ('/tmp')
logger.debug ('mkisofs -V ogClient -o {}.iso -b isolinux/isolinux.bin -c isolinux/boot.cat -J -no-emul-boot -boot-load-size 4 -boot-info-table /tmp/iso'.format (nameisoclient))
_run (['mkisofs', '-V', 'ogClient', '-o', '{}.iso'.format (nameisoclient), '-b', 'isolinux/isolinux.bin', '-c', 'isolinux/boot.cat', '-J', '-no-emul-boot', '-boot-load-size', '4', '-boot-info-table', '/tmp/iso'])
logger.debug (f'mkisofs -V ogClient -o {nameisoclient}.iso -b isolinux/isolinux.bin -c isolinux/boot.cat -J -no-emul-boot -boot-load-size 4 -boot-info-table /tmp/iso')
_run (['mkisofs', '-V', 'ogClient', '-o', f'{nameisoclient}.iso', '-b', 'isolinux/isolinux.bin', '-c', 'isolinux/boot.cat', '-J', '-no-emul-boot', '-boot-load-size', '4', '-boot-info-table', '/tmp/iso'])
##Generamos el md5
#DATASUM=`md5sum ${NAMEISOCLIENT}.iso | cut -f1 -d" "`
md5, _ = _run (['md5sum', '{}.iso'.format (nameisoclient)])
md5, _ = _run (['md5sum', f'{nameisoclient}.iso'])
md5, rest = md5.split (' ', 1)
#echo $DATASUM > ${NAMEISOCLIENT}.iso.sum
with open ('{}.iso.sum'.format (nameisoclient), 'w') as fd:
with open (f'{nameisoclient}.iso.sum', 'w') as fd:
fd.write (md5)
os.chdir (oldpwd)
os.rename ('/tmp/{}.iso' .format (nameisoclient), '/var/lib/tftpboot/ogclient/{}.iso' .format (nameisoclient))
os.rename ('/tmp/{}.iso.sum'.format (nameisoclient), '/var/lib/tftpboot/ogclient/{}.iso.sum'.format (nameisoclient))
os.rename (f'/tmp/{nameisoclient}.iso' , f'/var/lib/tftpboot/ogclient/{nameisoclient}.iso')
os.rename (f'/tmp/{nameisoclient}.iso.sum', f'/var/lib/tftpboot/ogclient/{nameisoclient}.iso.sum')
#def __unused_boottoolsBootGraphics():
# find /tmp/opengnsys_installer/ -name .svn -type d -exec rm -fr {} \; 2>/dev/null;

View File

@ -13,6 +13,7 @@ curdir = os.path.dirname (__file__)
sys.path.insert (0, curdir)
from boottoolsfunctions import _run, _is_mounted, btogGetOsInfo1, btogGetOsInfo2, btogGetVar, btogSetFsVirtual, btogSetFsAccess, btogSetFsBase, boottoolsSshServer, boottoolsSshClient, btogFsInitrd, btogFsSqfs, btogIsoGenerator
def _logging():
#logging.root.handlers = []
logging.basicConfig (
format='%(levelname)s %(asctime)s (%(funcName)s) %(message)s',
@ -22,7 +23,9 @@ logging.basicConfig (
logging.StreamHandler (sys.stdout),
],
)
logger = logging.getLogger ('boottools')
return = logging.getLogger ('boottools')
logger = _logging()
config = configparser.ConfigParser (comment_prefixes='#', inline_comment_prefixes='#')
if not os.path.exists ('boottoolsgenerator.cfg'):
@ -31,12 +34,13 @@ if not os.path.exists ('boottoolsgenerator.cfg'):
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'
tmpdir, _ = _run (['mktemp', '--tmpdir', '--directory', 'oggit.XXXXXX'])
logger.debug ('tmpdir "{}"'.format(tmpdir))
logger.debug (f'tmpdir "{tmpdir}"')
_run (['git', 'clone', '-c', 'http.sslVerify=false', '--branch', branch, 'https://ognproject.evlt.uma.es/gitea/opengnsys/opengnsys.git', tmpdir])
_run (['rsync', '-aH', '{}/client/engine'.format(tmpdir), '{}/client/shared'.format(tmpdir), '/tmp/opengnsys_installer/opengnsys/client/'])
_run (['rsync', '-aH', f'{tmpdir}/client/engine', f'{tmpdir}/client/shared', '/tmp/opengnsys_installer/opengnsys/client/'])
_run (['rm', '-rf', tmpdir])
type_client = sys.argv[1] if len(sys.argv)>1 else 'host'
@ -50,20 +54,24 @@ if os.getuid():
#os.chdir ('/tmp')
logger.info ('OpenGnsys CLIENT installation begins')
clone_client_dirs()
####################################################################3
logger.info ('FASE 1 - Asignación de variables')
ogclientcfg, 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)
logger.info ('OpenGnsys CLIENT installation begins')
## this is convenient in case the previous run failed and we want to run this program again
if _is_mounted (btrootfsmnt):
_run (['umount', btrootfsmnt])
##########################################################################
logger.info ('FASE 2 - Instalación de software adicional.')
def _get_pxepkg():
#grep "http://free.nchc.org.tw/drbl-core" /etc/apt/sources.list || echo "deb http://free.nchc.org.tw/drbl-core drbl stable" >> /etc/apt/sources.list
_run (['apt-get', 'update']) #>>/tmp/fase2.out
acse_gpxe, _ = _run (['apt-cache', 'search', 'gpxe'])
@ -75,15 +83,16 @@ elif acse_gpxe:
else:
logger.error ('neither gpxe nor ipxe found in apt-cache')
sys.exit (1)
logger.info ('PXE package is "{}"'.format (pxepkg))
logger.info (f'PXE package is "{pxepkg}"')
return pxepkg
pxepkg = _get_pxepkg()
## TODO qemu no existe, hace falta?
_run (['apt-get', '-y', 'install', 'jq', 'syslinux', 'syslinux-efi', 'syslinux-utils', 'debootstrap', 'subversion', 'schroot', 'squashfs-tools', 'syslinux', 'genisoimage', 'qemu-utils', 'lsof', pxepkg]) #>>/tmp/fase2.out
###################################################################3
logger.info ('FASE 3 - Creación del Sistema raiz RootFS (Segundo Sistema archivos (img))')
logger.info ('Fase 3.1 Generar y formatear el disco virtual. Generar el dispositivo loop.')
rc = subprocess.run ('file {} |grep -q "partition 1 *: ID=0x83"'.format (btrootfsimg), shell=True).returncode
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)
@ -91,18 +100,22 @@ if (rc): ## 'file|grep' failed
logger.error (str (e))
sys.exit (2)
logger.info ('FASE 3 - Creación del Sistema raiz RootFS (Segundo Sistema archivos (img))')
logger.info ('Fase 3.1 Generar y formatear el disco virtual. Generar el dispositivo loop.')
_mkrootfs()
logger.info ('Fase 3.2 - Configurar acceso schroot al Segundo Sistema de archivos (img)')
btogSetFsAccess (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.'
logger.info ('Fase 3.3 Generar sistema de archivos con debootstrap')
def _debootstrap():
logger.debug ('Try creation of a file within chroot (this operation may fail with "... etc/resolv.conf: No such file or directory"--that is ok)')
logger.debug ('Running \'schroot --chroot IMGogclient -- stat /etc\'')
cp = subprocess.run (['schroot', '--chroot', 'IMGogclient', '--', 'stat', '/etc'])
if (cp.returncode):
logger.debug ('schroot returned code "{}", calling btogSetFsBase()'.format (cp.returncode))
logger.debug (f'schroot returned code "{cp.returncode}", calling btogSetFsBase()')
try: _run (['mount', btrootfsimg, btrootfsmnt, '-o', 'loop,offset=32256'])
except:
logger.error ('mount failed')
@ -112,13 +125,15 @@ if (cp.returncode):
logger.error (str (e))
sys.exit (3)
###########################################################################
logger.info ('Fase 3.3 Generar sistema de archivos con debootstrap')
_debootstrap()
logger.info ('FASE 4 - Incorporando ficheros OpenGnsys al sistema raíz rootfs')
## por qué esta copia???
#cp -a ${BTDIR}/includes/usr/bin/* /tmp >>/tmp/fase5.out
#_run (['cp', '-a'] + glob.glob ('{}/includes/usr/bin/*'.format (btdir)) + ['/tmp'])
#_run (['cp', '-a'] + glob.glob (f'{btdir}/includes/usr/bin/*') + ['/tmp'])
#chmod +x /tmp/boot-tools/*.sh
#for i in glob.glob ('/tmp/boot-tools/*.sh'):
@ -128,76 +143,107 @@ logger.info ('FASE 4 - Incorporando ficheros OpenGnsys al sistema raíz rootfs')
# Incluir revisión.
## FIXME esto la incluye incondicionalmente, y luego terminamos con "OpenGnsys Client 1.2.0-rc1 gitrelease (osrelease) gitrelease (osrelease) gitrelease (osrelease) gitrelease (osrelease) gitrelease (osrelease) gitrelease (osrelease) ..."
#sed -i "1 s/$/ $GITRELEASE ($OSRELEASE)/" ${BTDIR}/includes/etc/initramfs-tools/scripts/VERSION.txt
_run (['sed', '-i', '1 s/$/ {} ({})/'.format (gitrelease, osrelease), '{}/includes/etc/initramfs-tools/scripts/VERSION.txt'.format (btdir)])
_run (['sed', '-i', f'1 s/$/ {gitrelease} ({osrelease})/', f'{btdir}/includes/etc/initramfs-tools/scripts/VERSION.txt'])
# En Ubuntu 13.04+ es necesario matar proceso de "udev" antes de desmontar.
#umount $BTROOTFSMNT 2>/dev/null || (kill -9 $(lsof -t $BTROOTFSMNT); umount $BTROOTFSMNT) 2>/dev/null
def _cerodos():
if (not _is_mounted (btrootfsmnt)):
try: _run (['mount', btrootfsimg, btrootfsmnt, '-o', 'loop,offset=32256'])
except:
logger.error ('mount failed')
sys.exit (3)
logger.debug ('running \'{}/02-boottoolsFsOpengnsys.py --mntpt "{}" --osdistrib "{}" --oscodename "{}" --osrelease "{}" --osarch "{}" --oshttp "{}"\''.format (curdir, btrootfsmnt, osdistrib, oscodename, osrelease, osarch, oshttp))
stdout, _ = _run (['{}/02-boottoolsFsOpengnsys.py'.format (curdir), '--mntpt', btrootfsmnt, '--osdistrib', osdistrib, '--oscodename', oscodename, '--osrelease', osrelease, '--osarch', osarch, '--oshttp', oshttp])
logger.debug ('02-boottoolsFsOpengnsys stdout follows: {}'.format (stdout))
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}')
_run (['umount', btrootfsmnt])
_cerodos()
## aqui empiezan las cosas de chroot
def _cerotres():
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.info ('FASE 5 - Instalar software')
logger.info ('Fase 5.1 instalar paquetes deb con apt-get')
logger.debug ('running \'schroot --chroot IMGogclient -- {}/03-boottoolsSoftwareInstall.py --osrelease "{}" --osarch "{}"\''.format (curdir, osrelease, osarch))
stdout, _ = _run (['schroot', '--chroot', 'IMGogclient', '--', '{}/03-boottoolsSoftwareInstall.py'.format (curdir), '--osrelease', osrelease, '--osarch', osarch])
logger.debug ('03-boottoolsSoftwareInstall stdout follows: {}'.format (stdout))
_cerotres()
logger.info ('Fase 5.2 compilar software.')
def _cerocuatro():
logger.debug ('running \'schroot --chroot IMGogclient -- {}/04-boottoolsSoftwareCompile.py\'')
#cd /
stdout, _ = _run (['schroot', '--chroot', 'IMGogclient', '--', '{}/04-boottoolsSoftwareCompile.py'.format (curdir)])
stdout, _ = _run (['schroot', '--chroot', 'IMGogclient', '--', f'{curdir}/04-boottoolsSoftwareCompile.py'])
#cd -
logger.debug ('04-boottoolsSoftwareCompile stdout follows: {}'.format (stdout))
logger.debug (f'04-boottoolsSoftwareCompile stdout follows: {stdout}')
logger.info ('Fase 5.2 compilar software.')
_cerocuatro():
## ya no chroot
def _ssh_stuff():
try: _run (['mount', btrootfsimg, btrootfsmnt, '-o', 'loop,offset=32256'])
except:
logger.error ('mount failed')
sys.exit (3)
############################################################################################
logger.info ('FASE 6 - Personalizar el sistema creado')
logger.info ('Fase 6.1 Incorporar la clave publica del servidor')
#cd /
#schroot --chroot IMGogclient -- /usr/bin/boot-tools/boottoolsSshServer.sh ## no necesita chroot
boottoolsSshServer (btrootfsmnt)
#cd -
logger.info ('Fase 6.2 Incorporar la clave publica del propio cliente')
#schroot --chroot IMGogclient -- /usr/bin/boot-tools/boottoolsSshClient.sh ## no necesita chroot
boottoolsSshClient (btrootfsmnt)
if _is_mounted (btrootfsmnt):
_run (['umount', btrootfsmnt])
## el resultado es:
## - hay un nuevo par de claves en la VM /root/.ssh
## - hay otro nuevo par de claves en el rootfs /var/lib/tftpboot/ogclient/ogclientmount/root/.ssh
## - las dos claves públicas (una de cada par) están autorizadan en el rootfs /var/lib/tftpboot/ogclient/ogclientmount/root/.ssh/authorized_keys
############################################################################################
if _is_mounted (btrootfsmnt):
_run (['umount', btrootfsmnt])
logger.info ('FASE 6 - Personalizar el sistema creado')
logger.info ('Fase 6.1 Incorporar la clave publica del servidor')
logger.info ('Fase 6.2 Incorporar la clave publica del propio cliente')
_ssh_stuff()
## chroot de nuevo
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.info ('Fase 6.3 Configurando las locales')
logger.debug ('running \'schroot --chroot IMGogclient -- {}/05-boottoolsFsLocales.py\'')
stdout, _ = _run (['schroot', '--chroot', 'IMGogclient', '--', '{}/05-boottoolsFsLocales.py'.format (curdir)])
logger.debug ('05-boottoolsFsLocales stdout follows: {}'.format (stdout))
_cerocinco()
def _ceroseis():
#cd /
#schroot -c IMGogclient -- /usr/bin/boot-tools/boottoolsInitrdGenerate.sh
logger.debug ('running \'schroot --chroot IMGogclient -- {}/06-boottoolsInitrdGenerate.py --osrelease "{}"\''.format (curdir, osrelease))
stdout, _ = _run (['schroot', '--chroot', 'IMGogclient', '--', '{}/06-boottoolsInitrdGenerate.py'.format (curdir), '--osrelease', osrelease])
logger.debug ('06-boottoolsInitrdGenerate stdout follows: {}'.format (stdout))
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}')
## esto deja initrd.img-6.8.0-31-generic y vmlinuz-6.8.0-31-generic en /tmp
_ceroseis()
## ya no chroot
logger.info ('FASE 7 - Generar distribucion')
logger.info ('Fase 7.1 Generar el initrd')
btogFsInitrd (bttargetdir, osrelease)