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

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

Fix /software command Linux operation

This patch runs the script InventarioSoftware? with the right arguments.
This also increases the recv buffer size for the test server.

  • Property mode set to 100644
File size: 4.9 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        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
38def 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
44def 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
50def 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
61def 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
73def software(request, path, ogRest):
74        disk = request.getDisk()
75        partition = request.getPartition()
76
77        try:
78                cmd = OG_PATH + 'interfaceAdm/InventarioSoftware '
79                cmd += str(disk) + ' '
80                cmd += str(partition) + ' '
81                cmd += path
82                ogRest.proc = subprocess.Popen([cmd],
83                                               stdout=subprocess.PIPE,
84                                               shell=True)
85                (output, error) = ogRest.proc.communicate()
86        except:
87                raise ValueError('Error: Incorrect command value')
88
89        return output.decode('utf-8')
90
91def hardware(path, ogRest):
92        try:
93                cmd = [OG_PATH + 'interfaceAdm/InventarioHardware ' + path]
94                ogRest.proc = subprocess.Popen(cmd,
95                                               stdout=subprocess.PIPE,
96                                               shell=True)
97                (output, error) = ogRest.proc.communicate()
98        except:
99                raise ValueError('Error: Incorrect command value')
100
101        return output.decode('utf-8')
102
103def setup(request, ogRest):
104        disk = request.getDisk()
105        cache = request.getCache()
106        cachesize = request.getCacheSize()
107        partlist = request.getPartitionSetup()
108        listConfigs = []
109
110        for part in partlist:
111                cfg = 'dis=' + disk + '*che=' + cache + '*tch=' + cachesize + '!par=' + part["partition"] + '*cpt='+part["code"] + '*sfi=' + part['filesystem'] + '*tam=' + part['size'] + '*ope=' + part['format'] + '%'
112                if ogRest.terminated:
113                        break
114
115                try:
116                        ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/Configurar', disk, cfg], stdout=subprocess.PIPE, shell=True)
117                        (output, error) = ogRest.proc.communicate()
118                except:
119                        continue
120
121        result = subprocess.check_output([OG_PATH + 'interfaceAdm/getConfiguration'], shell=True)
122        return parseGetConf(result.decode('utf-8'))[1]
123
124def image_restore(request, ogRest):
125        disk = request.getDisk()
126        partition = request.getPartition()
127        name = request.getName()
128        repo = request.getRepo()
129        ctype = request.getType()
130        profile = request.getProfile()
131        cid = request.getId()
132
133        try:
134                ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/RestaurarImagen', disk, partition, name, repo, ctype], stdout=subprocess.PIPE, shell=True)
135                (output, error) = ogRest.proc.communicate()
136        except:
137                raise ValueError('Error: Incorrect command value')
138
139        return output.decode('utf-8')
140
141def image_create(path, request, ogRest):
142        disk = request.getDisk()
143        partition = request.getPartition()
144        name = request.getName()
145        repo = request.getRepo()
146
147        try:
148                ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/InventarioSoftware', disk, partition, path], stdout=subprocess.PIPE, shell=True)
149                (output, error) = ogRest.proc.communicate()
150        except:
151                raise ValueError('Error: Incorrect command value')
152
153        if ogRest.terminated:
154                return
155
156        try:
157                ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/CrearImagen', disk, partition, name, repo], stdout=subprocess.PIPE, shell=True)
158                ogRest.proc.communicate()
159        except:
160                raise ValueError('Error: Incorrect command value')
161
162        return output.decode('utf-8')
163
164def refresh(ogRest):
165        listConfigs = []
166        disk = -1;
167
168        try:
169                ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/getConfiguration'], stdout=subprocess.PIPE, shell=True)
170                (output, error) = ogRest.proc.communicate()
171        except:
172                raise ValueError('Error: Incorrect command value')
173
174        return parseGetConf(output.decode('utf-8'))
Note: See TracBrowser for help on using the repository browser.