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

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

Fix /shell/run commands splitting

This patch splits shell commands either with ';' or '\n'.

  • Property mode set to 100644
File size: 4.8 KB
RevLine 
[05b1088]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
[62a8ca4]9import os
10import subprocess
11
[2997952]12OG_PATH = '/opt/opengnsys/'
13
[b5e182f]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
[ebd640a]38def poweroff():
[62a8ca4]39        if os.path.exists('/scripts/oginit'):
[2997952]40                subprocess.call('source ' + OG_SCRIPT_PATH + 'etc/preinit/loadenviron.sh; ' + OG_SCRIPT_PATH + 'scripts/poweroff', shell=True)
[62a8ca4]41        else:
42                subprocess.call(['/sbin/poweroff'])
[ebd640a]43
[fdf7701]44def reboot():
[dbbc7fa]45        if os.path.exists('/scripts/oginit'):
[2997952]46                subprocess.call('source ' + OG_SCRIPT_PATH + 'etc/preinit/loadenviron.sh; ' + OG_SCRIPT_PATH + 'scripts/reboot', shell=True)
[dbbc7fa]47        else:
48                subprocess.call(['/sbin/reboot'])
[e20daf6]49
[8fc251e]50def execCMD(request, ogRest):
51        cmd = request.getrun()
[9890d60]52        cmds = cmd.split(";|\n")
[e6eba4b]53        try:
[d5dca0f]54                ogRest.proc = subprocess.Popen(cmds, stdout=subprocess.PIPE, shell=True)
55                (output, error) = ogRest.proc.communicate()
[e6eba4b]56        except:
57                raise ValueError('Error: Incorrect command value')
58
[d5dca0f]59        return output.decode('utf-8')
[2fa8aa4]60
[2e80653]61def session(request, ogRest):
[8fc251e]62        disk = request.getDisk()
63        partition = request.getPartition()
[2e342b5]64
[0f32b9c]65        try:
[d5dca0f]66                ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/IniciarSesion', disk, partition], stdout=subprocess.PIPE, shell=True)
67                (output, error) = ogRest.proc.communicate()
[0f32b9c]68        except:
69                raise ValueError('Error: Incorrect command value')
70
[d5dca0f]71        return output.decode('utf-8')
[6d1e79b]72
[2e80653]73def software(request, path, ogRest):
[8fc251e]74        disk = request.getDisk()
75        partition = request.getPartition()
[2e342b5]76
[683afa6]77        try:
[d5dca0f]78                ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/InventarioSoftware', disk, partition, path], stdout=subprocess.PIPE, shell=True)
79                (output, error) = ogRest.proc.communicate()
[683afa6]80        except:
81                raise ValueError('Error: Incorrect command value')
82
[d5dca0f]83        return output.decode('utf-8')
[261a5ed]84
[2e80653]85def hardware(path, ogRest):
[1ced3dd]86        try:
[96c2dde]87                cmd = [OG_PATH + 'interfaceAdm/InventarioHardware ' + path]
88                ogRest.proc = subprocess.Popen(cmd,
89                                               stdout=subprocess.PIPE,
90                                               shell=True)
[d5dca0f]91                (output, error) = ogRest.proc.communicate()
[1ced3dd]92        except:
93                raise ValueError('Error: Incorrect command value')
94
[d5dca0f]95        return output.decode('utf-8')
[efbe8a7]96
[2e80653]97def setup(request, ogRest):
[8fc251e]98        disk = request.getDisk()
99        cache = request.getCache()
100        cachesize = request.getCacheSize()
101        partlist = request.getPartitionSetup()
[38b57c4]102        listConfigs = []
[2e342b5]103
[efbe8a7]104        for part in partlist:
105                cfg = 'dis=' + disk + '*che=' + cache + '*tch=' + cachesize + '!par=' + part["partition"] + '*cpt='+part["code"] + '*sfi=' + part['filesystem'] + '*tam=' + part['size'] + '*ope=' + part['format'] + '%'
[d5dca0f]106                if ogRest.terminated:
107                        break
108
[38b57c4]109                try:
[d5dca0f]110                        ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/Configurar', disk, cfg], stdout=subprocess.PIPE, shell=True)
111                        (output, error) = ogRest.proc.communicate()
[38b57c4]112                except:
113                        continue
114
[b5e182f]115        result = subprocess.check_output([OG_PATH + 'interfaceAdm/getConfiguration'], shell=True)
116        return parseGetConf(result.decode('utf-8'))[1]
[cc11d8f]117
[2e80653]118def image_restore(request, ogRest):
[8fc251e]119        disk = request.getDisk()
120        partition = request.getPartition()
121        name = request.getName()
122        repo = request.getRepo()
123        ctype = request.getType()
124        profile = request.getProfile()
125        cid = request.getId()
[2e342b5]126
[a306b8b]127        try:
[d5dca0f]128                ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/RestaurarImagen', disk, partition, name, repo, ctype], stdout=subprocess.PIPE, shell=True)
129                (output, error) = ogRest.proc.communicate()
[a306b8b]130        except:
131                raise ValueError('Error: Incorrect command value')
132
[d5dca0f]133        return output.decode('utf-8')
[b2fd0b5]134
[2e80653]135def image_create(path, request, ogRest):
[8fc251e]136        disk = request.getDisk()
137        partition = request.getPartition()
138        name = request.getName()
139        repo = request.getRepo()
[b2fd0b5]140
141        try:
142                ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/InventarioSoftware', disk, partition, path], stdout=subprocess.PIPE, shell=True)
143                (output, error) = ogRest.proc.communicate()
144        except:
145                raise ValueError('Error: Incorrect command value')
146
147        if ogRest.terminated:
148                return
149
150        try:
151                ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/CrearImagen', disk, partition, name, repo], stdout=subprocess.PIPE, shell=True)
152                ogRest.proc.communicate()
153        except:
154                raise ValueError('Error: Incorrect command value')
155
156        return output.decode('utf-8')
[b5e182f]157
[2e80653]158def refresh(ogRest):
[b5e182f]159        listConfigs = []
160        disk = -1;
161
162        try:
163                ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/getConfiguration'], stdout=subprocess.PIPE, shell=True)
164                (output, error) = ogRest.proc.communicate()
165        except:
166                raise ValueError('Error: Incorrect command value')
167
168        return parseGetConf(output.decode('utf-8'))
Note: See TracBrowser for help on using the repository browser.