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 | |
---|
9 | import os |
---|
10 | import subprocess |
---|
11 | |
---|
12 | OG_PATH = '/opt/opengnsys/' |
---|
13 | |
---|
14 | def parseGetConf(out): |
---|
15 | parsed = {'serial_number': '', |
---|
16 | 'disk_setup': '', |
---|
17 | 'partition_setup': list()} |
---|
18 | configs = out.split('\n') |
---|
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 |
---|
38 | |
---|
39 | def poweroff(): |
---|
40 | if os.path.exists('/scripts/oginit'): |
---|
41 | subprocess.call('source ' + OG_PATH + 'etc/preinit/loadenviron.sh; ' + OG_PATH + 'scripts/poweroff', shell=True) |
---|
42 | else: |
---|
43 | subprocess.call(['/sbin/poweroff']) |
---|
44 | |
---|
45 | def reboot(): |
---|
46 | if os.path.exists('/scripts/oginit'): |
---|
47 | subprocess.call('source ' + OG_PATH + 'etc/preinit/loadenviron.sh; ' + OG_PATH + 'scripts/reboot', shell=True) |
---|
48 | else: |
---|
49 | subprocess.call(['/sbin/reboot']) |
---|
50 | |
---|
51 | def execCMD(request, ogRest): |
---|
52 | cmd = request.getrun() |
---|
53 | cmds = cmd.split(";|\n") |
---|
54 | try: |
---|
55 | ogRest.proc = subprocess.Popen(cmds, stdout=subprocess.PIPE, shell=True) |
---|
56 | (output, error) = ogRest.proc.communicate() |
---|
57 | except: |
---|
58 | raise ValueError('Error: Incorrect command value') |
---|
59 | |
---|
60 | return output.decode('utf-8') |
---|
61 | |
---|
62 | def session(request, ogRest): |
---|
63 | disk = request.getDisk() |
---|
64 | partition = request.getPartition() |
---|
65 | |
---|
66 | try: |
---|
67 | ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/IniciarSesion', disk, partition], stdout=subprocess.PIPE, shell=True) |
---|
68 | (output, error) = ogRest.proc.communicate() |
---|
69 | except: |
---|
70 | raise ValueError('Error: Incorrect command value') |
---|
71 | |
---|
72 | return output.decode('utf-8') |
---|
73 | |
---|
74 | def software(request, path, ogRest): |
---|
75 | disk = request.getDisk() |
---|
76 | partition = request.getPartition() |
---|
77 | |
---|
78 | try: |
---|
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) |
---|
86 | (output, error) = ogRest.proc.communicate() |
---|
87 | except: |
---|
88 | raise ValueError('Error: Incorrect command value') |
---|
89 | |
---|
90 | return output.decode('utf-8') |
---|
91 | |
---|
92 | def hardware(path, ogRest): |
---|
93 | try: |
---|
94 | cmd = [OG_PATH + 'interfaceAdm/InventarioHardware ' + path] |
---|
95 | ogRest.proc = subprocess.Popen(cmd, |
---|
96 | stdout=subprocess.PIPE, |
---|
97 | shell=True) |
---|
98 | (output, error) = ogRest.proc.communicate() |
---|
99 | except: |
---|
100 | raise ValueError('Error: Incorrect command value') |
---|
101 | |
---|
102 | return output.decode('utf-8') |
---|
103 | |
---|
104 | def setup(request, ogRest): |
---|
105 | disk = request.getDisk() |
---|
106 | cache = request.getCache() |
---|
107 | cachesize = request.getCacheSize() |
---|
108 | partlist = request.getPartitionSetup() |
---|
109 | listConfigs = [] |
---|
110 | cfg = 'dis=' + disk + '*che=' + cache + '*tch=' + cachesize + '!' |
---|
111 | |
---|
112 | for part in partlist: |
---|
113 | cfg += 'par=' + part["partition"] + '*cpt='+part["code"] + \ |
---|
114 | '*sfi=' + part['filesystem'] + '*tam=' + \ |
---|
115 | part['size'] + '*ope=' + part['format'] + '%' |
---|
116 | if ogRest.terminated: |
---|
117 | break |
---|
118 | |
---|
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') |
---|
127 | |
---|
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')) |
---|
131 | |
---|
132 | def image_restore(request, ogRest): |
---|
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() |
---|
140 | |
---|
141 | try: |
---|
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() |
---|
144 | except: |
---|
145 | raise ValueError('Error: Incorrect command value') |
---|
146 | |
---|
147 | return output.decode('utf-8') |
---|
148 | |
---|
149 | def image_create(path, request, ogRest): |
---|
150 | disk = request.getDisk() |
---|
151 | partition = request.getPartition() |
---|
152 | name = request.getName() |
---|
153 | repo = request.getRepo() |
---|
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') |
---|
171 | |
---|
172 | def refresh(ogRest): |
---|
173 | try: |
---|
174 | cmd = OG_PATH + 'interfaceAdm/getConfiguration' |
---|
175 | ogRest.proc = subprocess.Popen([cmd], |
---|
176 | stdout=subprocess.PIPE, |
---|
177 | shell=True) |
---|
178 | (output, error) = ogRest.proc.communicate() |
---|
179 | except: |
---|
180 | raise ValueError('Error: Incorrect command value') |
---|
181 | |
---|
182 | return parseGetConf(output.decode('utf-8')) |
---|