source: ogClient-Git/src/linux/ogOperations.py @ e96e187

Last change on this file since e96e187 was e96e187, checked in by Alvaro Neira Ayuso <aneira@…>, 5 years ago

Fix parseGetConf(...) configuration parsing

This patch also reformats the response to the /refresh command.

  • Property mode set to 100644
File size: 5.3 KB
Line 
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
9import os
10import subprocess
11
12OG_PATH = '/opt/opengnsys/'
13
14def 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
39def poweroff():
40        if os.path.exists('/scripts/oginit'):
41                subprocess.call('source ' + OG_SCRIPT_PATH + 'etc/preinit/loadenviron.sh; ' + OG_SCRIPT_PATH + 'scripts/poweroff', shell=True)
42        else:
43                subprocess.call(['/sbin/poweroff'])
44
45def reboot():
46        if os.path.exists('/scripts/oginit'):
47                subprocess.call('source ' + OG_SCRIPT_PATH + 'etc/preinit/loadenviron.sh; ' + OG_SCRIPT_PATH + 'scripts/reboot', shell=True)
48        else:
49                subprocess.call(['/sbin/reboot'])
50
51def 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
62def 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
74def 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
92def 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
104def setup(request, ogRest):
105        disk = request.getDisk()
106        cache = request.getCache()
107        cachesize = request.getCacheSize()
108        partlist = request.getPartitionSetup()
109        listConfigs = []
110
111        for part in partlist:
112                cfg = 'dis=' + disk + '*che=' + cache + '*tch=' + cachesize + '!par=' + part["partition"] + '*cpt='+part["code"] + '*sfi=' + part['filesystem'] + '*tam=' + part['size'] + '*ope=' + part['format'] + '%'
113                if ogRest.terminated:
114                        break
115
116                try:
117                        ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/Configurar', disk, cfg], stdout=subprocess.PIPE, shell=True)
118                        (output, error) = ogRest.proc.communicate()
119                except:
120                        continue
121
122        result = subprocess.check_output([OG_PATH + 'interfaceAdm/getConfiguration'], shell=True)
123        return parseGetConf(result.decode('utf-8'))[1]
124
125def image_restore(request, ogRest):
126        disk = request.getDisk()
127        partition = request.getPartition()
128        name = request.getName()
129        repo = request.getRepo()
130        ctype = request.getType()
131        profile = request.getProfile()
132        cid = request.getId()
133
134        try:
135                ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/RestaurarImagen', disk, partition, name, repo, ctype], stdout=subprocess.PIPE, shell=True)
136                (output, error) = ogRest.proc.communicate()
137        except:
138                raise ValueError('Error: Incorrect command value')
139
140        return output.decode('utf-8')
141
142def image_create(path, request, ogRest):
143        disk = request.getDisk()
144        partition = request.getPartition()
145        name = request.getName()
146        repo = request.getRepo()
147
148        try:
149                ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/InventarioSoftware', disk, partition, path], stdout=subprocess.PIPE, shell=True)
150                (output, error) = ogRest.proc.communicate()
151        except:
152                raise ValueError('Error: Incorrect command value')
153
154        if ogRest.terminated:
155                return
156
157        try:
158                ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/CrearImagen', disk, partition, name, repo], stdout=subprocess.PIPE, shell=True)
159                ogRest.proc.communicate()
160        except:
161                raise ValueError('Error: Incorrect command value')
162
163        return output.decode('utf-8')
164
165def refresh(ogRest):
166        try:
167                cmd = OG_PATH + 'interfaceAdm/getConfiguration'
168                ogRest.proc = subprocess.Popen([cmd],
169                                               stdout=subprocess.PIPE,
170                                               shell=True)
171                (output, error) = ogRest.proc.communicate()
172        except:
173                raise ValueError('Error: Incorrect command value')
174
175        return parseGetConf(output.decode('utf-8'))
Note: See TracBrowser for help on using the repository browser.