[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 | |
---|
[3550da7] | 9 | import hashlib |
---|
[30fdcce] | 10 | import logging |
---|
[62a8ca4] | 11 | import os |
---|
| 12 | import subprocess |
---|
[3550da7] | 13 | import shlex |
---|
[00a95bd] | 14 | import shutil |
---|
[3550da7] | 15 | |
---|
| 16 | from subprocess import Popen, PIPE |
---|
[097769b] | 17 | |
---|
| 18 | import fdisk |
---|
| 19 | |
---|
[d7b7b0b] | 20 | from src.ogClient import ogClient |
---|
[bd98dd1] | 21 | from src.ogRest import ThreadState |
---|
[097769b] | 22 | from src.live.partcodes import GUID_MAP |
---|
[782f46a] | 23 | from src.live.parttypes import get_parttype |
---|
[62a8ca4] | 24 | |
---|
[3550da7] | 25 | from src.utils.legacy import * |
---|
[d3f9788] | 26 | from src.utils.net import ethtool |
---|
[097769b] | 27 | from src.utils.menu import generate_menu |
---|
[3550da7] | 28 | from src.utils.fs import * |
---|
[e6079c4] | 29 | from src.utils.probe import os_probe, cache_probe |
---|
[00a95bd] | 30 | from src.utils.disk import * |
---|
[782f46a] | 31 | from src.utils.cache import generate_cache_txt, umount_cache, init_cache |
---|
[00a95bd] | 32 | from src.utils.tiptorrent import * |
---|
[097769b] | 33 | |
---|
[d3f9788] | 34 | |
---|
[54c0ebf] | 35 | OG_SHELL = '/bin/bash' |
---|
[2997952] | 36 | |
---|
[f0aa3df] | 37 | class OgLiveOperations: |
---|
[621fb7a] | 38 | def __init__(self, config): |
---|
[147c890] | 39 | self._url = config['opengnsys']['url'] |
---|
| 40 | self._url_log = config['opengnsys']['url_log'] |
---|
[3550da7] | 41 | self._smb_user = config['samba']['user'] |
---|
| 42 | self._smb_pass = config['samba']['pass'] |
---|
[f0c550e] | 43 | |
---|
[0807ec7] | 44 | def _restartBrowser(self, url): |
---|
[f0c550e] | 45 | try: |
---|
| 46 | proc = subprocess.call(["pkill", "-9", "browser"]) |
---|
[0807ec7] | 47 | proc = subprocess.Popen(["browser", "-qws", url]) |
---|
[f0c550e] | 48 | except: |
---|
[699a6c2] | 49 | logging.error('Cannot restart browser') |
---|
[f0c550e] | 50 | raise ValueError('Error: cannot restart browser') |
---|
| 51 | |
---|
[097769b] | 52 | def _refresh_payload_disk(self, cxt, part_setup, num_disk): |
---|
| 53 | part_setup['disk'] = str(num_disk) |
---|
| 54 | part_setup['disk_type'] = 'DISK' |
---|
| 55 | part_setup['partition'] = '0' |
---|
| 56 | part_setup['filesystem'] = '' |
---|
| 57 | part_setup['os'] = '' |
---|
| 58 | part_setup['size'] = str(cxt.nsectors * cxt.sector_size // 1024) |
---|
| 59 | part_setup['used_size'] = '0' |
---|
[74a61d6] | 60 | if not cxt.label: |
---|
| 61 | part_setup['code'] = '0' |
---|
| 62 | else: |
---|
| 63 | part_setup['code'] = '2' if cxt.label.name == 'gpt' else '1' |
---|
[097769b] | 64 | |
---|
[3da8100] | 65 | def _refresh_payload_partition(self, cxt, pa, part_setup, disk): |
---|
[097769b] | 66 | parttype = cxt.partition_to_string(pa, fdisk.FDISK_FIELD_TYPEID) |
---|
| 67 | fstype = cxt.partition_to_string(pa, fdisk.FDISK_FIELD_FSTYPE) |
---|
[e6079c4] | 68 | padev = cxt.partition_to_string(pa, fdisk.FDISK_FIELD_DEVICE) |
---|
[097769b] | 69 | size = cxt.partition_to_string(pa, fdisk.FDISK_FIELD_SIZE) |
---|
[3da8100] | 70 | partnum = pa.partno + 1 |
---|
[e6079c4] | 71 | source = padev |
---|
| 72 | target = padev.replace('dev', 'mnt') |
---|
| 73 | |
---|
[097769b] | 74 | if cxt.label.name == 'gpt': |
---|
| 75 | code = GUID_MAP.get(parttype, 0x0) |
---|
| 76 | else: |
---|
| 77 | code = int(parttype, base=16) |
---|
| 78 | |
---|
| 79 | if mount_mkdir(source, target): |
---|
| 80 | probe_result = os_probe(target) |
---|
| 81 | part_setup['os'] = probe_result |
---|
| 82 | part_setup['used_size'] = get_usedperc(target) |
---|
| 83 | umount(target) |
---|
| 84 | else: |
---|
| 85 | part_setup['os'] = '' |
---|
| 86 | part_setup['used_size'] = '0' |
---|
| 87 | |
---|
| 88 | |
---|
| 89 | part_setup['disk_type'] = '' |
---|
[3da8100] | 90 | part_setup['partition'] = str(partnum) |
---|
[097769b] | 91 | part_setup['filesystem'] = fstype.upper() if fstype else 'EMPTY' |
---|
| 92 | # part_setup['code'] = hex(code).removeprefix('0x') |
---|
| 93 | part_setup['code'] = hex(code)[2:] |
---|
| 94 | part_setup['size'] = str(int(size) // 1024) |
---|
| 95 | |
---|
[6c441e9] | 96 | if (part_setup['filesystem'] == 'VFAT'): |
---|
| 97 | part_setup['filesystem'] = 'FAT32' |
---|
| 98 | |
---|
[e6079c4] | 99 | def _refresh_part_setup_cache(self, cxt, pa, part_setup, cache): |
---|
| 100 | padev = cxt.partition_to_string(pa, fdisk.FDISK_FIELD_DEVICE) |
---|
| 101 | if padev == cache: |
---|
| 102 | part_setup['filesystem'] = 'CACHE' |
---|
| 103 | part_setup['code'] = 'ca' |
---|
| 104 | |
---|
[3550da7] | 105 | def _compute_md5(self, path, bs=2**20): |
---|
| 106 | m = hashlib.md5() |
---|
| 107 | with open(path, 'rb') as f: |
---|
| 108 | while True: |
---|
| 109 | buf = f.read(bs) |
---|
| 110 | if not buf: |
---|
| 111 | break |
---|
| 112 | m.update(buf) |
---|
| 113 | return m.hexdigest() |
---|
| 114 | |
---|
| 115 | def _write_md5_file(self, path): |
---|
| 116 | if not os.path.exists(path): |
---|
| 117 | logging.error('Invalid path in _write_md5_file') |
---|
| 118 | raise ValueError('Invalid image path when computing md5 checksum') |
---|
| 119 | filename = path + ".full.sum" |
---|
| 120 | dig = self._compute_md5(path) |
---|
| 121 | with open(filename, 'w') as f: |
---|
| 122 | f.write(dig) |
---|
| 123 | |
---|
[3703fd6] | 124 | def _copy_image_to_cache(self, image_name): |
---|
| 125 | """ |
---|
| 126 | Copies /opt/opengnsys/image/{image_name} into |
---|
| 127 | /opt/opengnsys/cache/opt/opengnsys/images/ |
---|
| 128 | |
---|
| 129 | Implies a unicast transfer. Does not use tiptorrent. |
---|
| 130 | """ |
---|
| 131 | src = f'/opt/opengnsys/images/{image_name}.img' |
---|
| 132 | dst = f'/opt/opengnsys/cache/opt/opengnsys/images/{image_name}.img' |
---|
| 133 | try: |
---|
| 134 | r = shutil.copy(src, dst) |
---|
| 135 | tip_write_csum(image_name) |
---|
| 136 | except: |
---|
| 137 | logging.error('Error copying image to cache', repo) |
---|
| 138 | raise ValueError(f'Error: Cannot copy image {image_name} to cache') |
---|
| 139 | |
---|
| 140 | def _restore_image_unicast(self, repo, name, devpath, cache=False): |
---|
[00a95bd] | 141 | if ogChangeRepo(repo).returncode != 0: |
---|
[699a6c2] | 142 | self._restartBrowser(self._url) |
---|
[00a95bd] | 143 | logging.error('ogChangeRepo could not change repository to %s', repo) |
---|
| 144 | raise ValueError(f'Error: Cannot change repository to {repo}') |
---|
[3703fd6] | 145 | logging.debug(f'restore_image_unicast: name => {name}') |
---|
| 146 | if cache: |
---|
| 147 | image_path = f'/opt/opengnsys/cache/opt/opengnsys/images/{name}.img' |
---|
| 148 | if (not os.path.exists(image_path) or |
---|
| 149 | not tip_check_csum(repo, name)): |
---|
| 150 | self._copy_image_to_cache(name) |
---|
| 151 | else: |
---|
| 152 | image_path = f'/opt/opengnsys/images/{name}.img' |
---|
[00a95bd] | 153 | self._restore_image(image_path, devpath) |
---|
| 154 | |
---|
| 155 | def _restore_image_tiptorrent(self, repo, name, devpath): |
---|
| 156 | image_path = f'/opt/opengnsys/cache/opt/opengnsys/images/{name}.img' |
---|
[699a6c2] | 157 | try: |
---|
| 158 | if (not os.path.exists(image_path) or |
---|
| 159 | not tip_check_csum(repo, name)): |
---|
| 160 | tip_client_get(repo, name) |
---|
| 161 | except: |
---|
| 162 | self._restartBrowser(self._url) |
---|
| 163 | raise ValueError('Error before restoring image') |
---|
[00a95bd] | 164 | self._restore_image(image_path, devpath) |
---|
| 165 | |
---|
| 166 | def _restore_image(self, image_path, devpath): |
---|
[3703fd6] | 167 | logging.debug(f'Restoring image at {image_path} into {devpath}') |
---|
[f2515fc] | 168 | cmd_lzop = shlex.split(f'lzop -dc {image_path}') |
---|
[00a95bd] | 169 | cmd_pc = shlex.split(f'partclone.restore -d0 -C -I -o {devpath}') |
---|
| 170 | cmd_mbuffer = shlex.split('mbuffer -q -m 40M') if shutil.which('mbuffer') else None |
---|
| 171 | |
---|
[3703fd6] | 172 | if not os.path.exists(image_path): |
---|
| 173 | logging.error('f{image_path} does not exist, exiting.') |
---|
| 174 | raise ValueError(f'Error: Image not found at {image_path}') |
---|
| 175 | |
---|
[f2515fc] | 176 | with open('/tmp/command.log', 'wb', 0) as logfile: |
---|
[00a95bd] | 177 | proc_lzop = subprocess.Popen(cmd_lzop, |
---|
[f2515fc] | 178 | stdout=subprocess.PIPE) |
---|
[00a95bd] | 179 | proc_pc = subprocess.Popen(cmd_pc, |
---|
| 180 | stdin=proc_lzop.stdout, |
---|
| 181 | stderr=logfile) |
---|
| 182 | proc_lzop.stdout.close() |
---|
| 183 | proc_pc.communicate() |
---|
| 184 | |
---|
[70f1d0d] | 185 | def _ogbrowser_clear_logs(self): |
---|
| 186 | logfiles = ['/tmp/command.log', '/tmp/session.log'] |
---|
| 187 | for logfile in logfiles: |
---|
| 188 | with open(logfile, 'wb', 0) as f: |
---|
| 189 | f.truncate(0) |
---|
| 190 | |
---|
[99ae598] | 191 | def poweroff(self): |
---|
[30fdcce] | 192 | logging.info('Powering off client') |
---|
[99ae598] | 193 | if os.path.exists('/scripts/oginit'): |
---|
[d7b7b0b] | 194 | cmd = f'source {ogClient.OG_PATH}etc/preinit/loadenviron.sh; ' \ |
---|
| 195 | f'{ogClient.OG_PATH}scripts/poweroff' |
---|
[99ae598] | 196 | subprocess.call([cmd], shell=True, executable=OG_SHELL) |
---|
| 197 | else: |
---|
| 198 | subprocess.call(['/sbin/poweroff']) |
---|
| 199 | |
---|
| 200 | def reboot(self): |
---|
[30fdcce] | 201 | logging.info('Rebooting client') |
---|
[99ae598] | 202 | if os.path.exists('/scripts/oginit'): |
---|
[d7b7b0b] | 203 | cmd = f'source {ogClient.OG_PATH}etc/preinit/loadenviron.sh; ' \ |
---|
| 204 | f'{ogClient.OG_PATH}scripts/reboot' |
---|
[99ae598] | 205 | subprocess.call([cmd], shell=True, executable=OG_SHELL) |
---|
| 206 | else: |
---|
| 207 | subprocess.call(['/sbin/reboot']) |
---|
| 208 | |
---|
[32b73c5] | 209 | def shellrun(self, request, ogRest): |
---|
[99ae598] | 210 | cmd = request.getrun() |
---|
| 211 | cmds = cmd.split(";|\n\r") |
---|
[0807ec7] | 212 | |
---|
[147c890] | 213 | self._restartBrowser(self._url_log) |
---|
[0807ec7] | 214 | |
---|
[99ae598] | 215 | try: |
---|
| 216 | ogRest.proc = subprocess.Popen(cmds, |
---|
| 217 | stdout=subprocess.PIPE, |
---|
| 218 | shell=True, |
---|
| 219 | executable=OG_SHELL) |
---|
| 220 | (output, error) = ogRest.proc.communicate() |
---|
| 221 | except: |
---|
[30fdcce] | 222 | logging.error('Exception when running "shell run" subprocess') |
---|
[99ae598] | 223 | raise ValueError('Error: Incorrect command value') |
---|
| 224 | |
---|
[30fdcce] | 225 | if ogRest.proc.returncode != 0: |
---|
| 226 | logging.warn('Non zero exit code when running: %s', ' '.join(cmds)) |
---|
| 227 | else: |
---|
| 228 | logging.info('Shell run command OK') |
---|
| 229 | |
---|
[1ab981a] | 230 | self.refresh(ogRest) |
---|
[f0c550e] | 231 | |
---|
[99ae598] | 232 | return output.decode('utf-8') |
---|
| 233 | |
---|
| 234 | def session(self, request, ogRest): |
---|
| 235 | disk = request.getDisk() |
---|
| 236 | partition = request.getPartition() |
---|
[d7b7b0b] | 237 | cmd = f'{ogClient.OG_PATH}interfaceAdm/IniciarSesion {disk} {partition}' |
---|
[99ae598] | 238 | |
---|
| 239 | try: |
---|
| 240 | ogRest.proc = subprocess.Popen([cmd], |
---|
| 241 | stdout=subprocess.PIPE, |
---|
| 242 | shell=True, |
---|
| 243 | executable=OG_SHELL) |
---|
| 244 | (output, error) = ogRest.proc.communicate() |
---|
| 245 | except: |
---|
[30fdcce] | 246 | logging.error('Exception when running session subprocess') |
---|
[99ae598] | 247 | raise ValueError('Error: Incorrect command value') |
---|
| 248 | |
---|
[30fdcce] | 249 | logging.info('Starting OS at disk %s partition %s', disk, partition) |
---|
[99ae598] | 250 | return output.decode('utf-8') |
---|
| 251 | |
---|
| 252 | def software(self, request, path, ogRest): |
---|
| 253 | disk = request.getDisk() |
---|
| 254 | partition = request.getPartition() |
---|
| 255 | |
---|
[147c890] | 256 | self._restartBrowser(self._url_log) |
---|
[0807ec7] | 257 | |
---|
[99ae598] | 258 | try: |
---|
[d7b7b0b] | 259 | cmd = f'{ogClient.OG_PATH}interfaceAdm/InventarioSoftware {disk} ' \ |
---|
[99ae598] | 260 | f'{partition} {path}' |
---|
| 261 | |
---|
| 262 | ogRest.proc = subprocess.Popen([cmd], |
---|
| 263 | stdout=subprocess.PIPE, |
---|
| 264 | shell=True, |
---|
| 265 | executable=OG_SHELL) |
---|
| 266 | (output, error) = ogRest.proc.communicate() |
---|
| 267 | except: |
---|
[30fdcce] | 268 | logging.error('Exception when running software inventory subprocess') |
---|
[99ae598] | 269 | raise ValueError('Error: Incorrect command value') |
---|
| 270 | |
---|
[147c890] | 271 | self._restartBrowser(self._url) |
---|
[0807ec7] | 272 | |
---|
[2e3d47b] | 273 | software = '' |
---|
| 274 | with open(path, 'r') as f: |
---|
| 275 | software = f.read() |
---|
[30fdcce] | 276 | |
---|
| 277 | logging.info('Software inventory command OK') |
---|
[2e3d47b] | 278 | return software |
---|
[99ae598] | 279 | |
---|
| 280 | def hardware(self, path, ogRest): |
---|
[147c890] | 281 | self._restartBrowser(self._url_log) |
---|
[0807ec7] | 282 | |
---|
[99ae598] | 283 | try: |
---|
[d7b7b0b] | 284 | cmd = f'{ogClient.OG_PATH}interfaceAdm/InventarioHardware {path}' |
---|
[99ae598] | 285 | ogRest.proc = subprocess.Popen([cmd], |
---|
| 286 | stdout=subprocess.PIPE, |
---|
| 287 | shell=True, |
---|
| 288 | executable=OG_SHELL) |
---|
| 289 | (output, error) = ogRest.proc.communicate() |
---|
| 290 | except: |
---|
[30fdcce] | 291 | logging.error('Exception when running hardware inventory subprocess') |
---|
[99ae598] | 292 | raise ValueError('Error: Incorrect command value') |
---|
| 293 | |
---|
[147c890] | 294 | self._restartBrowser(self._url) |
---|
[0807ec7] | 295 | |
---|
[30fdcce] | 296 | logging.info('Hardware inventory command OK') |
---|
[99ae598] | 297 | return output.decode('utf-8') |
---|
| 298 | |
---|
| 299 | def setup(self, request, ogRest): |
---|
[a11224d] | 300 | table_type = request.getType() |
---|
[99ae598] | 301 | disk = request.getDisk() |
---|
| 302 | cache = request.getCache() |
---|
| 303 | cache_size = request.getCacheSize() |
---|
| 304 | partlist = request.getPartitionSetup() |
---|
| 305 | |
---|
[782f46a] | 306 | self._ogbrowser_clear_logs() |
---|
| 307 | self._restartBrowser(self._url_log) |
---|
| 308 | |
---|
| 309 | diskname = get_disks()[int(disk)-1] |
---|
| 310 | cxt = fdisk.Context(f'/dev/{diskname}', |
---|
| 311 | details=True) |
---|
| 312 | |
---|
| 313 | if table_type == 'MSDOS': |
---|
| 314 | cxt.create_disklabel('dos') |
---|
| 315 | elif table_type == 'GPT': |
---|
| 316 | cxt.create_disklabel('gpt') |
---|
[99ae598] | 317 | |
---|
[782f46a] | 318 | for part in partlist: |
---|
| 319 | logging.debug(f'Adding partition: {part}') |
---|
| 320 | if part["code"] == 'EMPTY': |
---|
| 321 | continue |
---|
[99ae598] | 322 | if ogRest.terminated: |
---|
| 323 | break |
---|
[782f46a] | 324 | if part["code"] == 'CACHE': |
---|
| 325 | umount_cache() |
---|
| 326 | |
---|
| 327 | pa = fdisk.Partition(start_follow_default=True, |
---|
| 328 | end_follow_default=False, |
---|
| 329 | partno_follow_default=False) |
---|
| 330 | parttype = get_parttype(cxt, part["code"]) |
---|
| 331 | size = int(part["size"]) |
---|
| 332 | pa.size = (size * (1 << 10)) // cxt.sector_size |
---|
| 333 | pa.partno = int(part["partition"]) - 1 |
---|
| 334 | pa.type = parttype |
---|
| 335 | cxt.add_partition(pa) |
---|
| 336 | |
---|
| 337 | cxt.write_disklabel() |
---|
| 338 | subprocess.run('partprobe') |
---|
[99ae598] | 339 | |
---|
[782f46a] | 340 | for part in partlist: |
---|
| 341 | if part["filesystem"] == 'EMPTY': |
---|
| 342 | continue |
---|
| 343 | partition = int(part["partition"]) |
---|
| 344 | fs = part["filesystem"].lower() |
---|
| 345 | if fs == 'cache': |
---|
| 346 | mkfs('ext4', int(disk), partition, label='CACHE') |
---|
| 347 | init_cache() |
---|
| 348 | else: |
---|
| 349 | mkfs(fs, int(disk), partition) |
---|
[99ae598] | 350 | |
---|
[30fdcce] | 351 | logging.info('Setup command OK') |
---|
[1ab981a] | 352 | result = self.refresh(ogRest) |
---|
[f0c550e] | 353 | |
---|
[782f46a] | 354 | self._restartBrowser(self._url) |
---|
| 355 | |
---|
[1ab981a] | 356 | return result |
---|
[99ae598] | 357 | |
---|
| 358 | def image_restore(self, request, ogRest): |
---|
| 359 | disk = request.getDisk() |
---|
| 360 | partition = request.getPartition() |
---|
| 361 | name = request.getName() |
---|
| 362 | repo = request.getRepo() |
---|
| 363 | ctype = request.getType() |
---|
| 364 | profile = request.getProfile() |
---|
| 365 | cid = request.getId() |
---|
[00a95bd] | 366 | partdev = get_partition_device(int(disk), int(partition)) |
---|
[99ae598] | 367 | |
---|
[70f1d0d] | 368 | self._ogbrowser_clear_logs() |
---|
[147c890] | 369 | self._restartBrowser(self._url_log) |
---|
[0807ec7] | 370 | |
---|
[00a95bd] | 371 | logging.debug('Image restore params:') |
---|
| 372 | logging.debug(f'\tname: {name}') |
---|
| 373 | logging.debug(f'\trepo: {repo}') |
---|
| 374 | logging.debug(f'\tprofile: {profile}') |
---|
| 375 | logging.debug(f'\tctype: {ctype}') |
---|
| 376 | |
---|
| 377 | if shutil.which('restoreImageCustom'): |
---|
| 378 | restoreImageCustom(repo, name, disk, partition, ctype) |
---|
| 379 | elif 'UNICAST' in ctype: |
---|
[3703fd6] | 380 | cache = 'DIRECT' not in ctype |
---|
| 381 | self._restore_image_unicast(repo, name, partdev, cache) |
---|
[00a95bd] | 382 | elif ctype == 'TIPTORRENT': |
---|
| 383 | self._restore_image_tiptorrent(repo, name, partdev) |
---|
| 384 | |
---|
| 385 | output = configureOs(disk, partition) |
---|
[99ae598] | 386 | |
---|
[1ab981a] | 387 | self.refresh(ogRest) |
---|
[f0c550e] | 388 | |
---|
[30fdcce] | 389 | logging.info('Image restore command OK') |
---|
[00a95bd] | 390 | return output |
---|
[99ae598] | 391 | |
---|
| 392 | def image_create(self, path, request, ogRest): |
---|
[3550da7] | 393 | disk = int(request.getDisk()) |
---|
| 394 | partition = int(request.getPartition()) |
---|
[99ae598] | 395 | name = request.getName() |
---|
| 396 | repo = request.getRepo() |
---|
[d7b7b0b] | 397 | cmd_software = f'{ogClient.OG_PATH}interfaceAdm/InventarioSoftware {disk} ' \ |
---|
[99ae598] | 398 | f'{partition} {path}' |
---|
[3550da7] | 399 | image_path = f'/opt/opengnsys/images/{name}.img' |
---|
[99ae598] | 400 | |
---|
[c010c42] | 401 | self._ogbrowser_clear_logs() |
---|
[147c890] | 402 | self._restartBrowser(self._url_log) |
---|
[0807ec7] | 403 | |
---|
[3550da7] | 404 | if ogChangeRepo(repo).returncode != 0: |
---|
[699a6c2] | 405 | self._restartBrowser(self._url) |
---|
[3550da7] | 406 | logging.error('ogChangeRepo could not change repository to %s', repo) |
---|
| 407 | raise ValueError(f'Error: Cannot change repository to {repo}') |
---|
| 408 | |
---|
[99ae598] | 409 | try: |
---|
| 410 | ogRest.proc = subprocess.Popen([cmd_software], |
---|
| 411 | stdout=subprocess.PIPE, |
---|
| 412 | shell=True, |
---|
| 413 | executable=OG_SHELL) |
---|
| 414 | (output, error) = ogRest.proc.communicate() |
---|
| 415 | except: |
---|
[699a6c2] | 416 | self._restartBrowser(self._url) |
---|
[30fdcce] | 417 | logging.error('Exception when running software inventory subprocess') |
---|
[99ae598] | 418 | raise ValueError('Error: Incorrect command value') |
---|
| 419 | |
---|
| 420 | if ogRest.terminated: |
---|
| 421 | return |
---|
| 422 | |
---|
| 423 | try: |
---|
[3550da7] | 424 | diskname = get_disks()[disk-1] |
---|
| 425 | cxt = fdisk.Context(f'/dev/{diskname}', details=True) |
---|
| 426 | pa = None |
---|
[99ae598] | 427 | |
---|
[3550da7] | 428 | for i, p in enumerate(cxt.partitions): |
---|
| 429 | if (p.partno + 1) == partition: |
---|
| 430 | pa = cxt.partitions[i] |
---|
| 431 | |
---|
| 432 | if pa is None: |
---|
[699a6c2] | 433 | self._restartBrowser(self._url) |
---|
[3550da7] | 434 | logging.error('Target partition not found') |
---|
| 435 | raise ValueError('Target partition number not found') |
---|
| 436 | |
---|
| 437 | padev = cxt.partition_to_string(pa, fdisk.FDISK_FIELD_DEVICE) |
---|
| 438 | fstype = cxt.partition_to_string(pa, fdisk.FDISK_FIELD_FSTYPE) |
---|
| 439 | if not fstype: |
---|
| 440 | logging.error('No filesystem detected. Aborting image creation.') |
---|
| 441 | raise ValueError('Target partition has no filesystem present') |
---|
| 442 | |
---|
| 443 | cambiar_acceso(user=self._smb_user, pwd=self._smb_pass) |
---|
[87e738b] | 444 | ogCopyEfiBootLoader(disk, partition) |
---|
[3550da7] | 445 | ogReduceFs(disk, partition) |
---|
| 446 | |
---|
| 447 | cmd1 = shlex.split(f'partclone.{fstype} -I -C --clone -s {padev} -O -') |
---|
| 448 | cmd2 = shlex.split(f'lzop -1 -fo {image_path}') |
---|
| 449 | |
---|
| 450 | logfile = open('/tmp/command.log', 'wb', 0) |
---|
[baa03de] | 451 | |
---|
[3550da7] | 452 | p1 = Popen(cmd1, stdout=PIPE, stderr=logfile) |
---|
| 453 | p2 = Popen(cmd2, stdin=p1.stdout) |
---|
| 454 | p1.stdout.close() |
---|
[c86eae4] | 455 | |
---|
[3550da7] | 456 | try: |
---|
| 457 | retdata = p2.communicate() |
---|
| 458 | except OSError as e: |
---|
| 459 | logging.error('Unexpected error when running partclone and lzop commands') |
---|
| 460 | finally: |
---|
| 461 | logfile.close() |
---|
| 462 | p2.terminate() |
---|
| 463 | p1.poll() |
---|
[c86eae4] | 464 | |
---|
[3550da7] | 465 | logging.info(f'partclone process exited with code {p1.returncode}') |
---|
| 466 | logging.info(f'lzop process exited with code {p2.returncode}') |
---|
| 467 | |
---|
[91077da] | 468 | ogExtendFs(disk, partition) |
---|
[3550da7] | 469 | |
---|
| 470 | image_info = ogGetImageInfo(image_path) |
---|
| 471 | except: |
---|
[699a6c2] | 472 | self._restartBrowser(self._url) |
---|
[3550da7] | 473 | logging.error('Exception when running "image create" subprocess') |
---|
| 474 | raise ValueError('Error: Incorrect command value') |
---|
[c86eae4] | 475 | |
---|
[3550da7] | 476 | self._write_md5_file(f'/opt/opengnsys/images/{name}.img') |
---|
[c86eae4] | 477 | |
---|
[147c890] | 478 | self._restartBrowser(self._url) |
---|
[0807ec7] | 479 | |
---|
[30fdcce] | 480 | logging.info('Image creation command OK') |
---|
[c86eae4] | 481 | return image_info |
---|
[99ae598] | 482 | |
---|
| 483 | def refresh(self, ogRest): |
---|
[147c890] | 484 | self._restartBrowser(self._url_log) |
---|
[0807ec7] | 485 | |
---|
[e6079c4] | 486 | cache = cache_probe() |
---|
[097769b] | 487 | disks = get_disks() |
---|
[a3cf8d1] | 488 | interface = os.getenv('DEVICE') |
---|
| 489 | link = ethtool(interface) |
---|
[097769b] | 490 | parsed = { 'serial_number': '', |
---|
| 491 | 'disk_setup': [], |
---|
[a3cf8d1] | 492 | 'partition_setup': [], |
---|
| 493 | 'link': link |
---|
[097769b] | 494 | } |
---|
| 495 | |
---|
| 496 | for num_disk, disk in enumerate(get_disks(), start=1): |
---|
[30fdcce] | 497 | logging.debug('refresh: processing %s', disk) |
---|
[097769b] | 498 | part_setup = {} |
---|
| 499 | try: |
---|
[1b5281c] | 500 | cxt = fdisk.Context(device=f'/dev/{disk}', details=True) |
---|
[097769b] | 501 | except: |
---|
| 502 | continue |
---|
| 503 | |
---|
| 504 | self._refresh_payload_disk(cxt, part_setup, num_disk) |
---|
| 505 | parsed['disk_setup'].append(part_setup) |
---|
| 506 | |
---|
[3da8100] | 507 | for pa in cxt.partitions: |
---|
[097769b] | 508 | part_setup = part_setup.copy() |
---|
[3da8100] | 509 | self._refresh_payload_partition(cxt, pa, part_setup, disk) |
---|
[e6079c4] | 510 | self._refresh_part_setup_cache(cxt, pa, part_setup, cache) |
---|
[097769b] | 511 | parsed['partition_setup'].append(part_setup) |
---|
| 512 | |
---|
| 513 | generate_menu(parsed['partition_setup']) |
---|
[81ee4b0] | 514 | generate_cache_txt() |
---|
[147c890] | 515 | self._restartBrowser(self._url) |
---|
[f0c550e] | 516 | |
---|
[30fdcce] | 517 | logging.info('Sending response to refresh request') |
---|
[097769b] | 518 | return parsed |
---|
[bd98dd1] | 519 | |
---|
| 520 | def probe(self, ogRest): |
---|
[d3f9788] | 521 | |
---|
| 522 | interface = os.getenv('DEVICE') |
---|
[bd98dd1] | 523 | speed = ethtool(interface) |
---|
| 524 | |
---|
| 525 | return {'status': 'OPG' if ogRest.state != ThreadState.BUSY else 'BSY', |
---|
| 526 | 'speed': speed} |
---|