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