[05b1088] | 1 | # |
---|
| 2 | # Copyright (C) 2020 Soleta Networks <info@soleta.eu> |
---|
| 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 |
---|
| 6 | # Free Software Foundation, version 3. |
---|
| 7 | # |
---|
| 8 | |
---|
[62a8ca4] | 9 | import os |
---|
| 10 | import subprocess |
---|
| 11 | |
---|
[2997952] | 12 | OG_PATH = '/opt/opengnsys/' |
---|
| 13 | |
---|
[b5e182f] | 14 | def parseGetConf(out): |
---|
[e96e187] | 15 | parsed = {'serial_number': '', |
---|
| 16 | 'disk_setup': '', |
---|
| 17 | 'partition_setup': list()} |
---|
[b5e182f] | 18 | configs = out.split('\n') |
---|
[e96e187] | 19 | for line in configs[:-1]: |
---|
| 20 | if 'ser=' in line: |
---|
| 21 | parsed['serial_number'] = line.partition('ser=')[2] |
---|
| 22 | else: |
---|
| 23 | part_setup = {} |
---|
| 24 | params = dict(param.split('=') for param in line.split('\t')) |
---|
| 25 | # Parse partition configuration. |
---|
| 26 | part_setup['disk'] = params['disk'] |
---|
| 27 | part_setup['partition'] = params['par'] |
---|
| 28 | part_setup['code'] = params['cpt'] |
---|
| 29 | part_setup['filesystem'] = params['fsi'] |
---|
| 30 | part_setup['os'] = params['soi'] |
---|
| 31 | part_setup['size'] = params['tam'] |
---|
| 32 | part_setup['used_size'] = params['uso'] |
---|
| 33 | if part_setup['partition'] == '0': |
---|
| 34 | parsed['disk_setup'] = part_setup |
---|
| 35 | else: |
---|
| 36 | parsed['partition_setup'].append(part_setup) |
---|
| 37 | return parsed |
---|
[b5e182f] | 38 | |
---|
[ebd640a] | 39 | def poweroff(): |
---|
[62a8ca4] | 40 | if os.path.exists('/scripts/oginit'): |
---|
[38056d7] | 41 | subprocess.call('source ' + OG_PATH + 'etc/preinit/loadenviron.sh; ' + OG_PATH + 'scripts/poweroff', shell=True) |
---|
[62a8ca4] | 42 | else: |
---|
| 43 | subprocess.call(['/sbin/poweroff']) |
---|
[ebd640a] | 44 | |
---|
[fdf7701] | 45 | def reboot(): |
---|
[dbbc7fa] | 46 | if os.path.exists('/scripts/oginit'): |
---|
[38056d7] | 47 | subprocess.call('source ' + OG_PATH + 'etc/preinit/loadenviron.sh; ' + OG_PATH + 'scripts/reboot', shell=True) |
---|
[dbbc7fa] | 48 | else: |
---|
| 49 | subprocess.call(['/sbin/reboot']) |
---|
[e20daf6] | 50 | |
---|
[8fc251e] | 51 | def execCMD(request, ogRest): |
---|
| 52 | cmd = request.getrun() |
---|
[9890d60] | 53 | cmds = cmd.split(";|\n") |
---|
[e6eba4b] | 54 | try: |
---|
[d5dca0f] | 55 | ogRest.proc = subprocess.Popen(cmds, stdout=subprocess.PIPE, shell=True) |
---|
| 56 | (output, error) = ogRest.proc.communicate() |
---|
[e6eba4b] | 57 | except: |
---|
| 58 | raise ValueError('Error: Incorrect command value') |
---|
| 59 | |
---|
[d5dca0f] | 60 | return output.decode('utf-8') |
---|
[2fa8aa4] | 61 | |
---|
[2e80653] | 62 | def session(request, ogRest): |
---|
[8fc251e] | 63 | disk = request.getDisk() |
---|
| 64 | partition = request.getPartition() |
---|
[2e342b5] | 65 | |
---|
[0f32b9c] | 66 | try: |
---|
[d5dca0f] | 67 | ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/IniciarSesion', disk, partition], stdout=subprocess.PIPE, shell=True) |
---|
| 68 | (output, error) = ogRest.proc.communicate() |
---|
[0f32b9c] | 69 | except: |
---|
| 70 | raise ValueError('Error: Incorrect command value') |
---|
| 71 | |
---|
[d5dca0f] | 72 | return output.decode('utf-8') |
---|
[6d1e79b] | 73 | |
---|
[2e80653] | 74 | def software(request, path, ogRest): |
---|
[8fc251e] | 75 | disk = request.getDisk() |
---|
| 76 | partition = request.getPartition() |
---|
[2e342b5] | 77 | |
---|
[683afa6] | 78 | try: |
---|
[ca0a62f] | 79 | cmd = OG_PATH + 'interfaceAdm/InventarioSoftware ' |
---|
| 80 | cmd += str(disk) + ' ' |
---|
| 81 | cmd += str(partition) + ' ' |
---|
| 82 | cmd += path |
---|
| 83 | ogRest.proc = subprocess.Popen([cmd], |
---|
| 84 | stdout=subprocess.PIPE, |
---|
| 85 | shell=True) |
---|
[d5dca0f] | 86 | (output, error) = ogRest.proc.communicate() |
---|
[683afa6] | 87 | except: |
---|
| 88 | raise ValueError('Error: Incorrect command value') |
---|
| 89 | |
---|
[d5dca0f] | 90 | return output.decode('utf-8') |
---|
[261a5ed] | 91 | |
---|
[2e80653] | 92 | def hardware(path, ogRest): |
---|
[1ced3dd] | 93 | try: |
---|
[96c2dde] | 94 | cmd = [OG_PATH + 'interfaceAdm/InventarioHardware ' + path] |
---|
| 95 | ogRest.proc = subprocess.Popen(cmd, |
---|
| 96 | stdout=subprocess.PIPE, |
---|
| 97 | shell=True) |
---|
[d5dca0f] | 98 | (output, error) = ogRest.proc.communicate() |
---|
[1ced3dd] | 99 | except: |
---|
| 100 | raise ValueError('Error: Incorrect command value') |
---|
| 101 | |
---|
[d5dca0f] | 102 | return output.decode('utf-8') |
---|
[efbe8a7] | 103 | |
---|
[2e80653] | 104 | def setup(request, ogRest): |
---|
[8fc251e] | 105 | disk = request.getDisk() |
---|
| 106 | cache = request.getCache() |
---|
| 107 | cachesize = request.getCacheSize() |
---|
| 108 | partlist = request.getPartitionSetup() |
---|
[38b57c4] | 109 | listConfigs = [] |
---|
[2934773] | 110 | cfg = 'dis=' + disk + '*che=' + cache + '*tch=' + cachesize + '!' |
---|
[2e342b5] | 111 | |
---|
[efbe8a7] | 112 | for part in partlist: |
---|
[2934773] | 113 | cfg += 'par=' + part["partition"] + '*cpt='+part["code"] + \ |
---|
| 114 | '*sfi=' + part['filesystem'] + '*tam=' + \ |
---|
| 115 | part['size'] + '*ope=' + part['format'] + '%' |
---|
[d5dca0f] | 116 | if ogRest.terminated: |
---|
| 117 | break |
---|
| 118 | |
---|
[2934773] | 119 | cmd = OG_PATH + 'interfaceAdm/Configurar' + " " + disk + " " + cfg |
---|
| 120 | try: |
---|
| 121 | ogRest.proc = subprocess.Popen([cmd], |
---|
| 122 | stdout=subprocess.PIPE, |
---|
| 123 | shell=True) |
---|
| 124 | (output, error) = ogRest.proc.communicate() |
---|
| 125 | except: |
---|
| 126 | raise ValueError('Error: Incorrect command value') |
---|
[38b57c4] | 127 | |
---|
[2934773] | 128 | cmd_get_conf = OG_PATH + 'interfaceAdm/getConfiguration' |
---|
| 129 | result = subprocess.check_output([cmd_get_conf], shell=True) |
---|
| 130 | return parseGetConf(result.decode('utf-8')) |
---|
[cc11d8f] | 131 | |
---|
[2e80653] | 132 | def image_restore(request, ogRest): |
---|
[8fc251e] | 133 | disk = request.getDisk() |
---|
| 134 | partition = request.getPartition() |
---|
| 135 | name = request.getName() |
---|
| 136 | repo = request.getRepo() |
---|
| 137 | ctype = request.getType() |
---|
| 138 | profile = request.getProfile() |
---|
| 139 | cid = request.getId() |
---|
[2e342b5] | 140 | |
---|
[a306b8b] | 141 | try: |
---|
[d5dca0f] | 142 | ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/RestaurarImagen', disk, partition, name, repo, ctype], stdout=subprocess.PIPE, shell=True) |
---|
| 143 | (output, error) = ogRest.proc.communicate() |
---|
[a306b8b] | 144 | except: |
---|
| 145 | raise ValueError('Error: Incorrect command value') |
---|
| 146 | |
---|
[d5dca0f] | 147 | return output.decode('utf-8') |
---|
[b2fd0b5] | 148 | |
---|
[2e80653] | 149 | def image_create(path, request, ogRest): |
---|
[8fc251e] | 150 | disk = request.getDisk() |
---|
| 151 | partition = request.getPartition() |
---|
| 152 | name = request.getName() |
---|
| 153 | repo = request.getRepo() |
---|
[b2fd0b5] | 154 | |
---|
| 155 | try: |
---|
| 156 | ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/InventarioSoftware', disk, partition, path], stdout=subprocess.PIPE, shell=True) |
---|
| 157 | (output, error) = ogRest.proc.communicate() |
---|
| 158 | except: |
---|
| 159 | raise ValueError('Error: Incorrect command value') |
---|
| 160 | |
---|
| 161 | if ogRest.terminated: |
---|
| 162 | return |
---|
| 163 | |
---|
| 164 | try: |
---|
| 165 | ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/CrearImagen', disk, partition, name, repo], stdout=subprocess.PIPE, shell=True) |
---|
| 166 | ogRest.proc.communicate() |
---|
| 167 | except: |
---|
| 168 | raise ValueError('Error: Incorrect command value') |
---|
| 169 | |
---|
| 170 | return output.decode('utf-8') |
---|
[b5e182f] | 171 | |
---|
[2e80653] | 172 | def refresh(ogRest): |
---|
[b5e182f] | 173 | try: |
---|
[dabc7eb] | 174 | cmd = OG_PATH + 'interfaceAdm/getConfiguration' |
---|
| 175 | ogRest.proc = subprocess.Popen([cmd], |
---|
| 176 | stdout=subprocess.PIPE, |
---|
| 177 | shell=True) |
---|
[b5e182f] | 178 | (output, error) = ogRest.proc.communicate() |
---|
| 179 | except: |
---|
| 180 | raise ValueError('Error: Incorrect command value') |
---|
| 181 | |
---|
| 182 | return parseGetConf(output.decode('utf-8')) |
---|