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

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

Add Refresh command

This patch allows us to execute refresh command using ogClient. This command
gets all the configuration in our machine and send this information to the
server. The format of the message that ogClient will send to the server will be:

{"disk": "1", "partition_setup": [{"partition": "1", "code": "LINUX",
"filesystem": "NTFS", "size": "498688", "format": "0"}, {"partition": "2",
"code": "LINUX", "filesystem": "NTFS", "size": "498688", "format": "0"},
{"partition": "3", "code": "LINUX", "filesystem": "NTFS", "size": "498688",
"format": "0"}]}

  • 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(httpparser, ogRest):
51        cmd = httpparser.getCMD()
52        cmds = cmd.split(" ")
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 procsession(httpparser, ogRest):
62        disk = httpparser.getDisk()
63        partition = httpparser.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 procsoftware(httpparser, path, ogRest):
74        disk = httpparser.getDisk()
75        partition = httpparser.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
85def prochardware(path, ogRest):
86        try:
87                ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/InventarioHardware', path], stdout=subprocess.PIPE, shell=True)
88                (output, error) = ogRest.proc.communicate()
89        except:
90                raise ValueError('Error: Incorrect command value')
91
92        return output.decode('utf-8')
93
94def procsetup(httpparser, ogRest):
95        disk = httpparser.getDisk()
96        cache = httpparser.getCache()
97        cachesize = httpparser.getCacheSize()
98        partlist = httpparser.getPartitionSetup()
99        listConfigs = []
100
101        for part in partlist:
102                cfg = 'dis=' + disk + '*che=' + cache + '*tch=' + cachesize + '!par=' + part["partition"] + '*cpt='+part["code"] + '*sfi=' + part['filesystem'] + '*tam=' + part['size'] + '*ope=' + part['format'] + '%'
103                if ogRest.terminated:
104                        break
105
106                try:
107                        ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/Configurar', disk, cfg], stdout=subprocess.PIPE, shell=True)
108                        (output, error) = ogRest.proc.communicate()
109                except:
110                        continue
111
112        result = subprocess.check_output([OG_PATH + 'interfaceAdm/getConfiguration'], shell=True)
113        return parseGetConf(result.decode('utf-8'))[1]
114
115def procirestore(httpparser, ogRest):
116        disk = httpparser.getDisk()
117        partition = httpparser.getPartition()
118        name = httpparser.getName()
119        repo = httpparser.getRepo()
120        ctype = httpparser.getType()
121        profile = httpparser.getProfile()
122        cid = httpparser.getId()
123
124        try:
125                ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/RestaurarImagen', disk, partition, name, repo, ctype], stdout=subprocess.PIPE, shell=True)
126                (output, error) = ogRest.proc.communicate()
127        except:
128                raise ValueError('Error: Incorrect command value')
129
130        return output.decode('utf-8')
131
132def procicreate(path, httpparser, ogRest):
133        disk = httpparser.getDisk()
134        partition = httpparser.getPartition()
135        name = httpparser.getName()
136        repo = httpparser.getRepo()
137
138        try:
139                ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/InventarioSoftware', disk, partition, path], stdout=subprocess.PIPE, shell=True)
140                (output, error) = ogRest.proc.communicate()
141        except:
142                raise ValueError('Error: Incorrect command value')
143
144        if ogRest.terminated:
145                return
146
147        try:
148                ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/CrearImagen', disk, partition, name, repo], stdout=subprocess.PIPE, shell=True)
149                ogRest.proc.communicate()
150        except:
151                raise ValueError('Error: Incorrect command value')
152
153        return output.decode('utf-8')
154
155def procrefresh(ogRest):
156        listConfigs = []
157        disk = -1;
158
159        try:
160                ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/getConfiguration'], stdout=subprocess.PIPE, shell=True)
161                (output, error) = ogRest.proc.communicate()
162        except:
163                raise ValueError('Error: Incorrect command value')
164
165        return parseGetConf(output.decode('utf-8'))
Note: See TracBrowser for help on using the repository browser.