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 | listConfigs = [] |
---|
16 | disk = -1; |
---|
17 | |
---|
18 | configs = out.split('\n') |
---|
19 | configs = filter(None, configs) |
---|
20 | for item in configs: |
---|
21 | i = 0 |
---|
22 | json = {} |
---|
23 | val = item.rstrip().split('\t') |
---|
24 | while i < len(val): |
---|
25 | val[i] = val[i].split('=')[1] |
---|
26 | i += 1 |
---|
27 | |
---|
28 | json['partition'] = val[1] |
---|
29 | json['code'] = val[4] |
---|
30 | json['filesystem'] = val[2] |
---|
31 | json['size'] = val[5] |
---|
32 | json['format'] = val[6] |
---|
33 | disk = val[0] |
---|
34 | listConfigs.append(json) |
---|
35 | |
---|
36 | return [disk, listConfigs] |
---|
37 | |
---|
38 | def poweroff(): |
---|
39 | if os.path.exists('/scripts/oginit'): |
---|
40 | subprocess.call('source ' + OG_SCRIPT_PATH + 'etc/preinit/loadenviron.sh; ' + OG_SCRIPT_PATH + 'scripts/poweroff', shell=True) |
---|
41 | else: |
---|
42 | subprocess.call(['/sbin/poweroff']) |
---|
43 | |
---|
44 | def reboot(): |
---|
45 | if os.path.exists('/scripts/oginit'): |
---|
46 | subprocess.call('source ' + OG_SCRIPT_PATH + 'etc/preinit/loadenviron.sh; ' + OG_SCRIPT_PATH + 'scripts/reboot', shell=True) |
---|
47 | else: |
---|
48 | subprocess.call(['/sbin/reboot']) |
---|
49 | |
---|
50 | def execCMD(request, ogRest): |
---|
51 | cmd = request.getrun() |
---|
52 | cmds = cmd.split(";|\n") |
---|
53 | try: |
---|
54 | ogRest.proc = subprocess.Popen(cmds, stdout=subprocess.PIPE, shell=True) |
---|
55 | (output, error) = ogRest.proc.communicate() |
---|
56 | except: |
---|
57 | raise ValueError('Error: Incorrect command value') |
---|
58 | |
---|
59 | return output.decode('utf-8') |
---|
60 | |
---|
61 | def session(request, ogRest): |
---|
62 | disk = request.getDisk() |
---|
63 | partition = request.getPartition() |
---|
64 | |
---|
65 | try: |
---|
66 | ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/IniciarSesion', disk, partition], stdout=subprocess.PIPE, shell=True) |
---|
67 | (output, error) = ogRest.proc.communicate() |
---|
68 | except: |
---|
69 | raise ValueError('Error: Incorrect command value') |
---|
70 | |
---|
71 | return output.decode('utf-8') |
---|
72 | |
---|
73 | def software(request, path, ogRest): |
---|
74 | disk = request.getDisk() |
---|
75 | partition = request.getPartition() |
---|
76 | |
---|
77 | try: |
---|
78 | ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/InventarioSoftware', disk, partition, path], stdout=subprocess.PIPE, shell=True) |
---|
79 | (output, error) = ogRest.proc.communicate() |
---|
80 | except: |
---|
81 | raise ValueError('Error: Incorrect command value') |
---|
82 | |
---|
83 | return output.decode('utf-8') |
---|
84 | |
---|
85 | def hardware(path, ogRest): |
---|
86 | try: |
---|
87 | cmd = [OG_PATH + 'interfaceAdm/InventarioHardware ' + path] |
---|
88 | ogRest.proc = subprocess.Popen(cmd, |
---|
89 | stdout=subprocess.PIPE, |
---|
90 | shell=True) |
---|
91 | (output, error) = ogRest.proc.communicate() |
---|
92 | except: |
---|
93 | raise ValueError('Error: Incorrect command value') |
---|
94 | |
---|
95 | return output.decode('utf-8') |
---|
96 | |
---|
97 | def setup(request, ogRest): |
---|
98 | disk = request.getDisk() |
---|
99 | cache = request.getCache() |
---|
100 | cachesize = request.getCacheSize() |
---|
101 | partlist = request.getPartitionSetup() |
---|
102 | listConfigs = [] |
---|
103 | |
---|
104 | for part in partlist: |
---|
105 | cfg = 'dis=' + disk + '*che=' + cache + '*tch=' + cachesize + '!par=' + part["partition"] + '*cpt='+part["code"] + '*sfi=' + part['filesystem'] + '*tam=' + part['size'] + '*ope=' + part['format'] + '%' |
---|
106 | if ogRest.terminated: |
---|
107 | break |
---|
108 | |
---|
109 | try: |
---|
110 | ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/Configurar', disk, cfg], stdout=subprocess.PIPE, shell=True) |
---|
111 | (output, error) = ogRest.proc.communicate() |
---|
112 | except: |
---|
113 | continue |
---|
114 | |
---|
115 | result = subprocess.check_output([OG_PATH + 'interfaceAdm/getConfiguration'], shell=True) |
---|
116 | return parseGetConf(result.decode('utf-8'))[1] |
---|
117 | |
---|
118 | def image_restore(request, ogRest): |
---|
119 | disk = request.getDisk() |
---|
120 | partition = request.getPartition() |
---|
121 | name = request.getName() |
---|
122 | repo = request.getRepo() |
---|
123 | ctype = request.getType() |
---|
124 | profile = request.getProfile() |
---|
125 | cid = request.getId() |
---|
126 | |
---|
127 | try: |
---|
128 | ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/RestaurarImagen', disk, partition, name, repo, ctype], stdout=subprocess.PIPE, shell=True) |
---|
129 | (output, error) = ogRest.proc.communicate() |
---|
130 | except: |
---|
131 | raise ValueError('Error: Incorrect command value') |
---|
132 | |
---|
133 | return output.decode('utf-8') |
---|
134 | |
---|
135 | def image_create(path, request, ogRest): |
---|
136 | disk = request.getDisk() |
---|
137 | partition = request.getPartition() |
---|
138 | name = request.getName() |
---|
139 | repo = request.getRepo() |
---|
140 | |
---|
141 | try: |
---|
142 | ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/InventarioSoftware', disk, partition, path], stdout=subprocess.PIPE, shell=True) |
---|
143 | (output, error) = ogRest.proc.communicate() |
---|
144 | except: |
---|
145 | raise ValueError('Error: Incorrect command value') |
---|
146 | |
---|
147 | if ogRest.terminated: |
---|
148 | return |
---|
149 | |
---|
150 | try: |
---|
151 | ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/CrearImagen', disk, partition, name, repo], stdout=subprocess.PIPE, shell=True) |
---|
152 | ogRest.proc.communicate() |
---|
153 | except: |
---|
154 | raise ValueError('Error: Incorrect command value') |
---|
155 | |
---|
156 | return output.decode('utf-8') |
---|
157 | |
---|
158 | def refresh(ogRest): |
---|
159 | listConfigs = [] |
---|
160 | disk = -1; |
---|
161 | |
---|
162 | try: |
---|
163 | ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/getConfiguration'], stdout=subprocess.PIPE, shell=True) |
---|
164 | (output, error) = ogRest.proc.communicate() |
---|
165 | except: |
---|
166 | raise ValueError('Error: Incorrect command value') |
---|
167 | |
---|
168 | return parseGetConf(output.decode('utf-8')) |
---|