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

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

Add image/create command for creating a new image

This patch allows us to use a new support for creating images using ogClient.
ogClient receives from the server a message which json body must be:

{"disk" : "1", "partition" : "1", "code" : "1", "id" : "1", "name" : "test",
"repository" : "192.168.2.4" }

ogClient returns to the server the software inventory executed before
create the image. The message for the server is:

{ "disk" : "0", "partition" : "1", "software" : "xyz" }

"xyz" will be the output saved during the execution of InventarioSoftware? in
a specific path.

  • Property mode set to 100644
File size: 4.4 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 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
20def 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
26def 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
37def 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
49def 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
61def 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
70def 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
105def 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
122def 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')
Note: See TracBrowser for help on using the repository browser.