[05b1088] | 1 | # |
---|
[cb9edc8] | 2 | # Copyright (C) 2020-2021 Soleta Networks <info@soleta.eu> |
---|
[05b1088] | 3 | # |
---|
| 4 | # This program is free software: you can redistribute it and/or modify it under |
---|
| 5 | # the terms of the GNU Affero General Public License as published by the |
---|
[cb9edc8] | 6 | # Free Software Foundation; either version 3 of the License, or |
---|
| 7 | # (at your option) any later version. |
---|
[05b1088] | 8 | |
---|
[62a8ca4] | 9 | import os |
---|
| 10 | import subprocess |
---|
[097769b] | 11 | |
---|
| 12 | import fdisk |
---|
| 13 | |
---|
[d7b7b0b] | 14 | from src.ogClient import ogClient |
---|
[bd98dd1] | 15 | from src.ogRest import ThreadState |
---|
[097769b] | 16 | from src.live.partcodes import GUID_MAP |
---|
[62a8ca4] | 17 | |
---|
[d3f9788] | 18 | from src.utils.net import ethtool |
---|
[097769b] | 19 | from src.utils.menu import generate_menu |
---|
| 20 | from src.utils.fs import mount_mkdir, umount, get_usedperc |
---|
[e6079c4] | 21 | from src.utils.probe import os_probe, cache_probe |
---|
[097769b] | 22 | from src.utils.disk import get_disks |
---|
[81ee4b0] | 23 | from src.utils.cache import generate_cache_txt |
---|
[097769b] | 24 | |
---|
[d3f9788] | 25 | |
---|
[54c0ebf] | 26 | OG_SHELL = '/bin/bash' |
---|
[2997952] | 27 | |
---|
[f0aa3df] | 28 | class OgLiveOperations: |
---|
[621fb7a] | 29 | def __init__(self, config): |
---|
[147c890] | 30 | self._url = config['opengnsys']['url'] |
---|
| 31 | self._url_log = config['opengnsys']['url_log'] |
---|
[f0c550e] | 32 | |
---|
[0807ec7] | 33 | def _restartBrowser(self, url): |
---|
[f0c550e] | 34 | try: |
---|
| 35 | proc = subprocess.call(["pkill", "-9", "browser"]) |
---|
[0807ec7] | 36 | proc = subprocess.Popen(["browser", "-qws", url]) |
---|
[f0c550e] | 37 | except: |
---|
| 38 | raise ValueError('Error: cannot restart browser') |
---|
| 39 | |
---|
[097769b] | 40 | def _refresh_payload_disk(self, cxt, part_setup, num_disk): |
---|
| 41 | part_setup['disk'] = str(num_disk) |
---|
| 42 | part_setup['disk_type'] = 'DISK' |
---|
| 43 | part_setup['code'] = '2' if cxt.label.name == 'gpt' else '1' |
---|
| 44 | part_setup['partition'] = '0' |
---|
| 45 | part_setup['filesystem'] = '' |
---|
| 46 | part_setup['os'] = '' |
---|
| 47 | part_setup['size'] = str(cxt.nsectors * cxt.sector_size // 1024) |
---|
| 48 | part_setup['used_size'] = '0' |
---|
| 49 | |
---|
[3da8100] | 50 | def _refresh_payload_partition(self, cxt, pa, part_setup, disk): |
---|
[097769b] | 51 | parttype = cxt.partition_to_string(pa, fdisk.FDISK_FIELD_TYPEID) |
---|
| 52 | fstype = cxt.partition_to_string(pa, fdisk.FDISK_FIELD_FSTYPE) |
---|
[e6079c4] | 53 | padev = cxt.partition_to_string(pa, fdisk.FDISK_FIELD_DEVICE) |
---|
[097769b] | 54 | size = cxt.partition_to_string(pa, fdisk.FDISK_FIELD_SIZE) |
---|
[3da8100] | 55 | partnum = pa.partno + 1 |
---|
[e6079c4] | 56 | source = padev |
---|
| 57 | target = padev.replace('dev', 'mnt') |
---|
| 58 | |
---|
[097769b] | 59 | if cxt.label.name == 'gpt': |
---|
| 60 | code = GUID_MAP.get(parttype, 0x0) |
---|
| 61 | else: |
---|
| 62 | code = int(parttype, base=16) |
---|
| 63 | |
---|
| 64 | if mount_mkdir(source, target): |
---|
| 65 | probe_result = os_probe(target) |
---|
| 66 | part_setup['os'] = probe_result |
---|
| 67 | part_setup['used_size'] = get_usedperc(target) |
---|
| 68 | umount(target) |
---|
| 69 | else: |
---|
| 70 | part_setup['os'] = '' |
---|
| 71 | part_setup['used_size'] = '0' |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | part_setup['disk_type'] = '' |
---|
[3da8100] | 75 | part_setup['partition'] = str(partnum) |
---|
[097769b] | 76 | part_setup['filesystem'] = fstype.upper() if fstype else 'EMPTY' |
---|
| 77 | # part_setup['code'] = hex(code).removeprefix('0x') |
---|
| 78 | part_setup['code'] = hex(code)[2:] |
---|
| 79 | part_setup['size'] = str(int(size) // 1024) |
---|
| 80 | |
---|
[e6079c4] | 81 | def _refresh_part_setup_cache(self, cxt, pa, part_setup, cache): |
---|
| 82 | padev = cxt.partition_to_string(pa, fdisk.FDISK_FIELD_DEVICE) |
---|
| 83 | if padev == cache: |
---|
| 84 | part_setup['filesystem'] = 'CACHE' |
---|
| 85 | part_setup['code'] = 'ca' |
---|
| 86 | |
---|
| 87 | |
---|
[99ae598] | 88 | def parseGetConf(self, out): |
---|
| 89 | parsed = {'serial_number': '', |
---|
[91f034e] | 90 | 'disk_setup': list(), |
---|
[99ae598] | 91 | 'partition_setup': list()} |
---|
| 92 | configs = out.split('\n') |
---|
| 93 | for line in configs[:-1]: |
---|
| 94 | if 'ser=' in line: |
---|
| 95 | parsed['serial_number'] = line.partition('ser=')[2] |
---|
| 96 | else: |
---|
| 97 | part_setup = {} |
---|
| 98 | params = dict(param.split('=') for param in line.split('\t')) |
---|
| 99 | # Parse partition configuration. |
---|
| 100 | part_setup['disk'] = params['disk'] |
---|
[b5c3f58] | 101 | part_setup['disk_type'] = params.get('dtype', '') |
---|
[99ae598] | 102 | part_setup['partition'] = params['par'] |
---|
| 103 | part_setup['code'] = params['cpt'] |
---|
| 104 | part_setup['filesystem'] = params['fsi'] |
---|
| 105 | part_setup['os'] = params['soi'] |
---|
| 106 | part_setup['size'] = params['tam'] |
---|
| 107 | part_setup['used_size'] = params['uso'] |
---|
| 108 | if part_setup['partition'] == '0': |
---|
[91f034e] | 109 | parsed['disk_setup'].append(part_setup) |
---|
[99ae598] | 110 | else: |
---|
| 111 | parsed['partition_setup'].append(part_setup) |
---|
| 112 | return parsed |
---|
| 113 | |
---|
| 114 | def poweroff(self): |
---|
| 115 | if os.path.exists('/scripts/oginit'): |
---|
[d7b7b0b] | 116 | cmd = f'source {ogClient.OG_PATH}etc/preinit/loadenviron.sh; ' \ |
---|
| 117 | f'{ogClient.OG_PATH}scripts/poweroff' |
---|
[99ae598] | 118 | subprocess.call([cmd], shell=True, executable=OG_SHELL) |
---|
| 119 | else: |
---|
| 120 | subprocess.call(['/sbin/poweroff']) |
---|
| 121 | |
---|
| 122 | def reboot(self): |
---|
| 123 | if os.path.exists('/scripts/oginit'): |
---|
[d7b7b0b] | 124 | cmd = f'source {ogClient.OG_PATH}etc/preinit/loadenviron.sh; ' \ |
---|
| 125 | f'{ogClient.OG_PATH}scripts/reboot' |
---|
[99ae598] | 126 | subprocess.call([cmd], shell=True, executable=OG_SHELL) |
---|
| 127 | else: |
---|
| 128 | subprocess.call(['/sbin/reboot']) |
---|
| 129 | |
---|
[32b73c5] | 130 | def shellrun(self, request, ogRest): |
---|
[99ae598] | 131 | cmd = request.getrun() |
---|
| 132 | cmds = cmd.split(";|\n\r") |
---|
[0807ec7] | 133 | |
---|
[147c890] | 134 | self._restartBrowser(self._url_log) |
---|
[0807ec7] | 135 | |
---|
[99ae598] | 136 | try: |
---|
| 137 | ogRest.proc = subprocess.Popen(cmds, |
---|
| 138 | stdout=subprocess.PIPE, |
---|
| 139 | shell=True, |
---|
| 140 | executable=OG_SHELL) |
---|
| 141 | (output, error) = ogRest.proc.communicate() |
---|
| 142 | except: |
---|
| 143 | raise ValueError('Error: Incorrect command value') |
---|
| 144 | |
---|
[d7b7b0b] | 145 | cmd_get_conf = f'{ogClient.OG_PATH}interfaceAdm/getConfiguration' |
---|
[eec50f7] | 146 | result = subprocess.check_output([cmd_get_conf], shell=True) |
---|
[147c890] | 147 | self._restartBrowser(self._url) |
---|
[f0c550e] | 148 | |
---|
[99ae598] | 149 | return output.decode('utf-8') |
---|
| 150 | |
---|
| 151 | def session(self, request, ogRest): |
---|
| 152 | disk = request.getDisk() |
---|
| 153 | partition = request.getPartition() |
---|
[d7b7b0b] | 154 | cmd = f'{ogClient.OG_PATH}interfaceAdm/IniciarSesion {disk} {partition}' |
---|
[99ae598] | 155 | |
---|
| 156 | try: |
---|
| 157 | ogRest.proc = subprocess.Popen([cmd], |
---|
| 158 | stdout=subprocess.PIPE, |
---|
| 159 | shell=True, |
---|
| 160 | executable=OG_SHELL) |
---|
| 161 | (output, error) = ogRest.proc.communicate() |
---|
| 162 | except: |
---|
| 163 | raise ValueError('Error: Incorrect command value') |
---|
| 164 | |
---|
| 165 | return output.decode('utf-8') |
---|
| 166 | |
---|
| 167 | def software(self, request, path, ogRest): |
---|
| 168 | disk = request.getDisk() |
---|
| 169 | partition = request.getPartition() |
---|
| 170 | |
---|
[147c890] | 171 | self._restartBrowser(self._url_log) |
---|
[0807ec7] | 172 | |
---|
[99ae598] | 173 | try: |
---|
[d7b7b0b] | 174 | cmd = f'{ogClient.OG_PATH}interfaceAdm/InventarioSoftware {disk} ' \ |
---|
[99ae598] | 175 | f'{partition} {path}' |
---|
| 176 | |
---|
| 177 | ogRest.proc = subprocess.Popen([cmd], |
---|
| 178 | stdout=subprocess.PIPE, |
---|
| 179 | shell=True, |
---|
| 180 | executable=OG_SHELL) |
---|
| 181 | (output, error) = ogRest.proc.communicate() |
---|
| 182 | except: |
---|
| 183 | raise ValueError('Error: Incorrect command value') |
---|
| 184 | |
---|
[147c890] | 185 | self._restartBrowser(self._url) |
---|
[0807ec7] | 186 | |
---|
[2e3d47b] | 187 | software = '' |
---|
| 188 | with open(path, 'r') as f: |
---|
| 189 | software = f.read() |
---|
| 190 | return software |
---|
[99ae598] | 191 | |
---|
| 192 | def hardware(self, path, ogRest): |
---|
[147c890] | 193 | self._restartBrowser(self._url_log) |
---|
[0807ec7] | 194 | |
---|
[99ae598] | 195 | try: |
---|
[d7b7b0b] | 196 | cmd = f'{ogClient.OG_PATH}interfaceAdm/InventarioHardware {path}' |
---|
[99ae598] | 197 | ogRest.proc = subprocess.Popen([cmd], |
---|
| 198 | stdout=subprocess.PIPE, |
---|
| 199 | shell=True, |
---|
| 200 | executable=OG_SHELL) |
---|
| 201 | (output, error) = ogRest.proc.communicate() |
---|
| 202 | except: |
---|
| 203 | raise ValueError('Error: Incorrect command value') |
---|
| 204 | |
---|
[147c890] | 205 | self._restartBrowser(self._url) |
---|
[0807ec7] | 206 | |
---|
[99ae598] | 207 | return output.decode('utf-8') |
---|
| 208 | |
---|
| 209 | def setup(self, request, ogRest): |
---|
[a11224d] | 210 | table_type = request.getType() |
---|
[99ae598] | 211 | disk = request.getDisk() |
---|
| 212 | cache = request.getCache() |
---|
| 213 | cache_size = request.getCacheSize() |
---|
| 214 | partlist = request.getPartitionSetup() |
---|
| 215 | cfg = f'dis={disk}*che={cache}*tch={cache_size}!' |
---|
| 216 | |
---|
| 217 | for part in partlist: |
---|
| 218 | cfg += f'par={part["partition"]}*cpt={part["code"]}*' \ |
---|
| 219 | f'sfi={part["filesystem"]}*tam={part["size"]}*' \ |
---|
| 220 | f'ope={part["format"]}%' |
---|
| 221 | |
---|
| 222 | if ogRest.terminated: |
---|
| 223 | break |
---|
| 224 | |
---|
[a11224d] | 225 | cmd = f'{ogClient.OG_PATH}interfaceAdm/Configurar {table_type} {cfg}' |
---|
[99ae598] | 226 | try: |
---|
| 227 | ogRest.proc = subprocess.Popen([cmd], |
---|
| 228 | stdout=subprocess.PIPE, |
---|
| 229 | shell=True, |
---|
| 230 | executable=OG_SHELL) |
---|
| 231 | (output, error) = ogRest.proc.communicate() |
---|
| 232 | except: |
---|
| 233 | raise ValueError('Error: Incorrect command value') |
---|
| 234 | |
---|
[d7b7b0b] | 235 | cmd_get_conf = f'{ogClient.OG_PATH}interfaceAdm/getConfiguration' |
---|
[99ae598] | 236 | result = subprocess.check_output([cmd_get_conf], shell=True) |
---|
[147c890] | 237 | self._restartBrowser(self._url) |
---|
[f0c550e] | 238 | |
---|
[99ae598] | 239 | return self.parseGetConf(result.decode('utf-8')) |
---|
| 240 | |
---|
| 241 | def image_restore(self, request, ogRest): |
---|
| 242 | disk = request.getDisk() |
---|
| 243 | partition = request.getPartition() |
---|
| 244 | name = request.getName() |
---|
| 245 | repo = request.getRepo() |
---|
| 246 | ctype = request.getType() |
---|
| 247 | profile = request.getProfile() |
---|
| 248 | cid = request.getId() |
---|
[d7b7b0b] | 249 | cmd = f'{ogClient.OG_PATH}interfaceAdm/RestaurarImagen {disk} {partition} ' \ |
---|
[99ae598] | 250 | f'{name} {repo} {ctype}' |
---|
| 251 | |
---|
[147c890] | 252 | self._restartBrowser(self._url_log) |
---|
[0807ec7] | 253 | |
---|
[99ae598] | 254 | try: |
---|
| 255 | ogRest.proc = subprocess.Popen([cmd], |
---|
| 256 | stdout=subprocess.PIPE, |
---|
| 257 | shell=True, |
---|
| 258 | executable=OG_SHELL) |
---|
| 259 | (output, error) = ogRest.proc.communicate() |
---|
[ffbcf7e] | 260 | if (ogRest.proc.returncode): |
---|
| 261 | raise Exception |
---|
[99ae598] | 262 | except: |
---|
| 263 | raise ValueError('Error: Incorrect command value') |
---|
| 264 | |
---|
[d7b7b0b] | 265 | cmd_get_conf = f'{ogClient.OG_PATH}interfaceAdm/getConfiguration' |
---|
[eec50f7] | 266 | result = subprocess.check_output([cmd_get_conf], shell=True) |
---|
[147c890] | 267 | self._restartBrowser(self._url) |
---|
[f0c550e] | 268 | |
---|
[99ae598] | 269 | return output.decode('utf-8') |
---|
| 270 | |
---|
| 271 | def image_create(self, path, request, ogRest): |
---|
| 272 | disk = request.getDisk() |
---|
| 273 | partition = request.getPartition() |
---|
| 274 | name = request.getName() |
---|
| 275 | repo = request.getRepo() |
---|
[d7b7b0b] | 276 | cmd_software = f'{ogClient.OG_PATH}interfaceAdm/InventarioSoftware {disk} ' \ |
---|
[99ae598] | 277 | f'{partition} {path}' |
---|
[d7b7b0b] | 278 | cmd_create_image = f'{ogClient.OG_PATH}interfaceAdm/CrearImagen {disk} ' \ |
---|
[99ae598] | 279 | f'{partition} {name} {repo}' |
---|
| 280 | |
---|
[147c890] | 281 | self._restartBrowser(self._url_log) |
---|
[0807ec7] | 282 | |
---|
[99ae598] | 283 | try: |
---|
| 284 | ogRest.proc = subprocess.Popen([cmd_software], |
---|
| 285 | stdout=subprocess.PIPE, |
---|
| 286 | shell=True, |
---|
| 287 | executable=OG_SHELL) |
---|
| 288 | (output, error) = ogRest.proc.communicate() |
---|
| 289 | except: |
---|
| 290 | raise ValueError('Error: Incorrect command value') |
---|
| 291 | |
---|
| 292 | if ogRest.terminated: |
---|
| 293 | return |
---|
| 294 | |
---|
| 295 | try: |
---|
| 296 | ogRest.proc = subprocess.Popen([cmd_create_image], |
---|
| 297 | stdout=subprocess.PIPE, |
---|
| 298 | shell=True, |
---|
| 299 | executable=OG_SHELL) |
---|
| 300 | ogRest.proc.communicate() |
---|
| 301 | except: |
---|
| 302 | raise ValueError('Error: Incorrect command value') |
---|
| 303 | |
---|
[baa03de] | 304 | if ogRest.proc.returncode != 0: |
---|
| 305 | raise ValueError('Error: Image creation failed') |
---|
| 306 | |
---|
[c86eae4] | 307 | with open('/tmp/image.info') as file_info: |
---|
| 308 | line = file_info.readline().rstrip() |
---|
| 309 | |
---|
| 310 | image_info = {} |
---|
| 311 | |
---|
| 312 | (image_info['clonator'], |
---|
| 313 | image_info['compressor'], |
---|
| 314 | image_info['filesystem'], |
---|
| 315 | image_info['datasize'], |
---|
| 316 | image_info['clientname']) = line.split(':', 5) |
---|
| 317 | |
---|
| 318 | os.remove('/tmp/image.info') |
---|
| 319 | |
---|
[147c890] | 320 | self._restartBrowser(self._url) |
---|
[0807ec7] | 321 | |
---|
[c86eae4] | 322 | return image_info |
---|
[99ae598] | 323 | |
---|
| 324 | def refresh(self, ogRest): |
---|
[147c890] | 325 | self._restartBrowser(self._url_log) |
---|
[0807ec7] | 326 | |
---|
[e6079c4] | 327 | cache = cache_probe() |
---|
[097769b] | 328 | disks = get_disks() |
---|
[a3cf8d1] | 329 | interface = os.getenv('DEVICE') |
---|
| 330 | link = ethtool(interface) |
---|
[097769b] | 331 | parsed = { 'serial_number': '', |
---|
| 332 | 'disk_setup': [], |
---|
[a3cf8d1] | 333 | 'partition_setup': [], |
---|
| 334 | 'link': link |
---|
[097769b] | 335 | } |
---|
| 336 | |
---|
| 337 | for num_disk, disk in enumerate(get_disks(), start=1): |
---|
| 338 | print(disk) |
---|
| 339 | part_setup = {} |
---|
| 340 | try: |
---|
| 341 | cxt = fdisk.Context(device=f'/dev/{disk}') |
---|
| 342 | except: |
---|
| 343 | continue |
---|
| 344 | |
---|
| 345 | self._refresh_payload_disk(cxt, part_setup, num_disk) |
---|
| 346 | parsed['disk_setup'].append(part_setup) |
---|
| 347 | |
---|
[3da8100] | 348 | for pa in cxt.partitions: |
---|
[097769b] | 349 | part_setup = part_setup.copy() |
---|
[3da8100] | 350 | self._refresh_payload_partition(cxt, pa, part_setup, disk) |
---|
[e6079c4] | 351 | self._refresh_part_setup_cache(cxt, pa, part_setup, cache) |
---|
[097769b] | 352 | parsed['partition_setup'].append(part_setup) |
---|
| 353 | |
---|
| 354 | generate_menu(parsed['partition_setup']) |
---|
[81ee4b0] | 355 | generate_cache_txt() |
---|
[147c890] | 356 | self._restartBrowser(self._url) |
---|
[f0c550e] | 357 | |
---|
[097769b] | 358 | return parsed |
---|
[bd98dd1] | 359 | |
---|
| 360 | def probe(self, ogRest): |
---|
[d3f9788] | 361 | |
---|
| 362 | interface = os.getenv('DEVICE') |
---|
[bd98dd1] | 363 | speed = ethtool(interface) |
---|
| 364 | |
---|
| 365 | return {'status': 'OPG' if ogRest.state != ThreadState.BUSY else 'BSY', |
---|
| 366 | 'speed': speed} |
---|