1 | #!/usr/bin/python3 |
---|
2 | |
---|
3 | import sys |
---|
4 | import os |
---|
5 | import logging |
---|
6 | import subprocess |
---|
7 | import glob |
---|
8 | import stat |
---|
9 | |
---|
10 | sys.path.insert (0, __file__) |
---|
11 | from boottoolsfunctions import btogGetOsInfo1, btogGetOsInfo2, btogGetVar, btogSetFsVirtual, btogSetFsAccess, btogSetFsBase |
---|
12 | |
---|
13 | logging.basicConfig (filename='/tmp/boot-tools_installation.log', filemode='a', format='%(levelname)s %(asctime)s (%(funcName)s) %(message)s', level=logging.INFO) |
---|
14 | logger = logging.getLogger ('boottools') |
---|
15 | |
---|
16 | if not os.path.exists ('/tmp/opengnsys_installer/opengnsys/client/engine'): |
---|
17 | branch = 'main' |
---|
18 | tmpdir = subprocess.run (['mktemp', '--tmpdir', '--directory', 'oggit.XXXXXX'], capture_output=True).stdout.decode ('utf-8').strip() |
---|
19 | subprocess.run (['git', 'clone', '-c', 'http.sslVerify=false', '--branch', branch, 'https://ognproject.evlt.uma.es/gitea/opengnsys/opengnsys.git', tmpdir]) |
---|
20 | subprocess.run (['rsync', '-aHv', '{}/client/engine'.format(tmpdir), '{}/client/shared'.format(tmpdir), '/tmp/opengnsys_installer/opengnsys/client/']) |
---|
21 | subprocess.run (['rm', '-rf', tmpdir]) |
---|
22 | |
---|
23 | type_client = sys.argv[1] if len(sys.argv)>1 else 'host' |
---|
24 | #WORKDIR = '/tmp/opengnsys_installer' |
---|
25 | #INSTALL_TARGET = '/opt/opengnsys' |
---|
26 | #PROGRAMDIR = $(readlink -e $(dirname "$0")) |
---|
27 | |
---|
28 | if os.getuid(): |
---|
29 | logger.error ('ERROR: this program must run under root privileges!!') |
---|
30 | os._exit (1) |
---|
31 | |
---|
32 | os.chdir ('/tmp') |
---|
33 | |
---|
34 | #for i in `mount | grep IMGogclient | grep /var | cut -f3 -d" "`; do echo $i; umount $i; done |
---|
35 | #for i in `mount | grep IMGogclient | grep /var | cut -f3 -d" "`; do echo $i; umount $i; done |
---|
36 | #for i in `mount | grep IMGogclient | grep /var | cut -f3 -d" "`; do echo $i; umount $i; done |
---|
37 | |
---|
38 | |
---|
39 | |
---|
40 | ####################################################################3 |
---|
41 | logger.info ('FASE 1 - Asignación de variables') |
---|
42 | ogclientcfg, osdistrib, oscodename, osrelease, osarch, oshttp = btogGetOsInfo1(type_client) |
---|
43 | btdir, bttargetdir, btrootfsimg, btrootfsmnt, btrootfsimglabel, log_file, versionboottools, btvirtualdisksize = btogGetVar(osarch) |
---|
44 | gitrelease, nameisoclient, namehostclient = btogGetOsInfo2(type_client, versionboottools, ogclientcfg, osdistrib, oscodename, osrelease, osarch, oshttp) |
---|
45 | logger.info ('OpenGnsys CLIENT installation begins') |
---|
46 | |
---|
47 | |
---|
48 | ########################################################################## |
---|
49 | logger.info ('FASE 2 - Instalación de software adicional.') |
---|
50 | #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 |
---|
51 | subprocess.run (['apt-get', 'update']) #>>/tmp/fase2.out |
---|
52 | acse_gpxe = subprocess.run (['apt-cache', 'search', 'gpxe'], capture_output=True).stdout.decode ('utf-8').strip() |
---|
53 | acse_ipxe = subprocess.run (['apt-cache', 'search', 'ipxe'], capture_output=True).stdout.decode ('utf-8').strip() |
---|
54 | if acse_ipxe: |
---|
55 | pxepkg = 'ipxe' |
---|
56 | elif acse_gpxe: |
---|
57 | pxepkg = 'gpxe' |
---|
58 | else: |
---|
59 | logger.error ('neither gpxe nor ipxe found in apt-cache') |
---|
60 | os._exit (1) |
---|
61 | logger.info ('PXE package is "{}"'.format (pxepkg)) |
---|
62 | ## TODO qemu no existe, hace falta? |
---|
63 | subprocess.run (['apt-get', '-y', 'install', 'debootstrap', 'subversion', 'schroot', 'squashfs-tools', 'syslinux', 'genisoimage', 'qemu-utils', 'lsof', pxepkg]) #>>/tmp/fase2.out |
---|
64 | |
---|
65 | |
---|
66 | ###################################################################3 |
---|
67 | logger.info ('FASE 3 - Creación del Sistema raiz RootFS (Segundo Sistema archivos (img))') |
---|
68 | logger.info ('Fase 3.1 Generar y formatear el disco virtual. Generar el dispositivo loop.') |
---|
69 | rc = subprocess.run ('file {} |grep -q "partition 1 *: ID=0x83"'.format (btrootfsimg), shell=True).returncode |
---|
70 | print (rc) |
---|
71 | if (rc): ## 'file|grep' failed |
---|
72 | try: btogSetFsVirtual (btrootfsimg, btrootfsimglabel, btrootfsmnt, btvirtualdisksize, bttargetdir, osarch) |
---|
73 | except Exception as e: |
---|
74 | logger.error (str (e)) |
---|
75 | os._exit (2) |
---|
76 | #echo ================= nati after btogSetFsVirtual; ls -la /opt/opengnsys/tftpboot/ogclient/ |
---|
77 | |
---|
78 | logger.info ('Fase 3.2 - Configurar acceso schroot al Segundo Sistema de archivos (img)') |
---|
79 | #cat /etc/schroot/schroot.conf | grep $BTROOTFSIMG || btogSetFsAccess |
---|
80 | btogSetFsAccess (btrootfsimg) |
---|
81 | |
---|
82 | logger.info ('Fase 3.3 Generar sistema de archivos con debootstrap') |
---|
83 | logger.debug ('Try creation of a file within chroot (this operation may fail--that is ok)') |
---|
84 | cp = subprocess.run (['schroot', '--preserve-environment', '--chroot', 'IMGogclient', '--', 'touch', '/tmp/ogclientOK']) |
---|
85 | if (cp.returncode): |
---|
86 | try: btogSetFsBase (btrootfsimg, btrootfsmnt, osarch, oscodename, oshttp) #>>/tmp/fase3.out |
---|
87 | except Exception as e: |
---|
88 | logger.error (str (e)) |
---|
89 | os._exit (3) |
---|
90 | else: |
---|
91 | os.unlink ('/tmp/ogclientOK') |
---|
92 | #echo ================= nati after fase 3; ls -la /opt/opengnsys/tftpboot/ogclient/ |
---|
93 | |
---|
94 | |
---|
95 | ########################################################################### |
---|
96 | logger.info ('FASE 4 - Incorporando ficheros OpenGnsys al sistema raíz rootfs') |
---|
97 | |
---|
98 | #cp -a ${BTDIR}/includes/usr/bin/* /tmp >>/tmp/fase5.out |
---|
99 | subprocess.run (['cp', '-a'] + glob.glob ('{}/includes/usr/bin/*'.format (btdir)) + ['/tmp']) |
---|
100 | |
---|
101 | #chmod +x /tmp/boot-tools/*.sh |
---|
102 | for i in glob.glob ('/tmp/boot-tools/*.sh'): |
---|
103 | st = os.stat (i) |
---|
104 | os.chmod (i, st.st_mode|stat.S_IXUSR|stat.S_IXGRP|stat.S_IXOTH) |
---|
105 | |
---|
106 | # Incluir revisión. |
---|
107 | #sed -i "1 s/$/ $GITRELEASE ($OSRELEASE)/" ${BTDIR}/includes/etc/initramfs-tools/scripts/VERSION.txt |
---|
108 | subprocess.run (['sed', '-i', '1 s/$/ {} ({})/'.format (gitrelease, osrelease), '{}/includes/etc/initramfs-tools/scripts/VERSION.txt'.format (btdir)]) |
---|
109 | |
---|
110 | # En Ubuntu 13.04+ es necesario matar proceso de "udev" antes de desmontar. |
---|
111 | #umount $BTROOTFSMNT 2>/dev/null || (kill -9 $(lsof -t $BTROOTFSMNT); umount $BTROOTFSMNT) 2>/dev/null |
---|
112 | """ |
---|
113 | schroot --preserve-environment --chroot IMGogclient -- /tmp/boot-tools/boottoolsFsOpengnsys.sh >>/tmp/fase4.out |
---|
114 | |
---|
115 | |
---|
116 | ############################################################################################ |
---|
117 | logger.info ('FASE 5 - Instalar software') |
---|
118 | logger.info ('Fase 5.1 instalar paquetes deb con apt-get') |
---|
119 | schroot --preserve-environment --chroot IMGogclient -- /usr/bin/boot-tools/boottoolsSoftwareInstall.sh >>/tmp/fase5.out |
---|
120 | logger.info ('Fase 5.2 compilar software.') |
---|
121 | cd / |
---|
122 | schroot --preserve-environment --chroot IMGogclient -- /usr/bin/boot-tools/boottoolsSoftwareCompile.sh >>/tmp/fase5.out |
---|
123 | cd - |
---|
124 | |
---|
125 | |
---|
126 | ############################################################################################ |
---|
127 | logger.info ('FASE 6 - Personalizar el sistema creado') |
---|
128 | logger.info ('Fase 6.1 Incorporar la clave publica del servidor') |
---|
129 | cd / |
---|
130 | ssh-keygen -q -f /root/.ssh/id_rsa -N "" |
---|
131 | cp /root/.ssh/id_rsa.pub /tmp |
---|
132 | schroot --preserve-environment --chroot IMGogclient -- /usr/bin/boot-tools/boottoolsSshServer.sh |
---|
133 | cd - |
---|
134 | logger.info ('Fase 6.2. Incorpoar la clave publica del propio cliente') |
---|
135 | schroot --preserve-environment --chroot IMGogclient -- /usr/bin/boot-tools/boottoolsSshClient.sh |
---|
136 | |
---|
137 | logger.info ('Fase 6.3. Configurando las locales') |
---|
138 | schroot --preserve-environment --chroot IMGogclient -- /usr/bin/boot-tools/boottoolsFsLocales.sh |
---|
139 | |
---|
140 | for i in `mount | grep IMGogclient | grep /var | cut -f3 -d" "`; do echo $i; umount $i; done |
---|
141 | for i in `mount | grep IMGogclient | grep /var | cut -f3 -d" "`; do echo $i; umount $i; done |
---|
142 | for i in `mount | grep IMGogclient | grep /var | cut -f3 -d" "`; do echo $i; umount $i; done |
---|
143 | |
---|
144 | echo ================= nati after fase 6; ls -la /opt/opengnsys/tftpboot/ogclient/ |
---|
145 | |
---|
146 | |
---|
147 | ######################################################################### |
---|
148 | logger.info ('FASE 7 - Generar distribucion') |
---|
149 | logger.info ('Fase 7.1 Generar el initrd') |
---|
150 | btogFsInitrd >>/tmp/fase7.out |
---|
151 | echo ================= nati after fase 8.1; ls -la /opt/opengnsys/tftpboot/ogclient/ |
---|
152 | logger.info ('Fase 7.2 Generar fichero sqfs a partir del fichero img') |
---|
153 | btogFsSqfs |
---|
154 | echo ================= nati after fase 8.2; ls -la /opt/opengnsys/tftpboot/ogclient/ |
---|
155 | umount $BTROOTFSMNT 2>/dev/null |
---|
156 | logger.info ('Fase 7.3 Generar la ISO') |
---|
157 | btogIsoGenerator >>/tmp/fase7.out |
---|
158 | echo ================= nati after fase 8.3; ls -la /opt/opengnsys/tftpboot/ogclient/ |
---|
159 | |
---|
160 | echoAndLog "OpenGnsys installation finished at $(date)" |
---|
161 | """ |
---|