1 | # -*- coding: utf-8 -*- |
---|
2 | # |
---|
3 | # Copyright (c) 2014 Virtual Cable S.L. |
---|
4 | # All rights reserved. |
---|
5 | # |
---|
6 | # Redistribution and use in source and binary forms, with or without modification, |
---|
7 | # are permitted provided that the following conditions are met: |
---|
8 | # |
---|
9 | # * Redistributions of source code must retain the above copyright notice, |
---|
10 | # this list of conditions and the following disclaimer. |
---|
11 | # * Redistributions in binary form must reproduce the above copyright notice, |
---|
12 | # this list of conditions and the following disclaimer in the documentation |
---|
13 | # and/or other materials provided with the distribution. |
---|
14 | # * Neither the name of Virtual Cable S.L. nor the names of its contributors |
---|
15 | # may be used to endorse or promote products derived from this software |
---|
16 | # without specific prior written permission. |
---|
17 | # |
---|
18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
19 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
20 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
---|
21 | # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
---|
22 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
23 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
---|
24 | # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
---|
25 | # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
---|
26 | # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
27 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
28 | |
---|
29 | """ |
---|
30 | @author: Ramón M. Gómez, ramongomez at us dot es |
---|
31 | """ |
---|
32 | from __future__ import unicode_literals |
---|
33 | |
---|
34 | import socket |
---|
35 | import platform |
---|
36 | import fcntl |
---|
37 | import subprocess |
---|
38 | import struct |
---|
39 | import array |
---|
40 | import six |
---|
41 | from opengnsys import utils |
---|
42 | |
---|
43 | |
---|
44 | def _getMacAddr(ifname): |
---|
45 | """ |
---|
46 | Returns the mac address of an interface |
---|
47 | Mac is returned as unicode utf-8 encoded |
---|
48 | """ |
---|
49 | if isinstance(ifname, list): |
---|
50 | return dict([(name, _getMacAddr(name)) for name in ifname]) |
---|
51 | if isinstance(ifname, six.text_type): |
---|
52 | ifname = ifname.encode('utf-8') # If unicode, convert to bytes (or str in python 2.7) |
---|
53 | try: |
---|
54 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
---|
55 | info = bytearray(fcntl.ioctl(s.fileno(), 0x8927, struct.pack(str('256s'), ifname[:15]))) |
---|
56 | return six.text_type(''.join(['%02x:' % char for char in info[18:24]])[:-1]) |
---|
57 | except Exception: |
---|
58 | return None |
---|
59 | |
---|
60 | |
---|
61 | def _getIpAddr(ifname): |
---|
62 | """ |
---|
63 | Returns the ip address of an interface |
---|
64 | Ip is returned as unicode utf-8 encoded |
---|
65 | """ |
---|
66 | if isinstance(ifname, list): |
---|
67 | return dict([(name, _getIpAddr(name)) for name in ifname]) |
---|
68 | if isinstance(ifname, six.text_type): |
---|
69 | ifname = ifname.encode('utf-8') # If unicode, convert to bytes (or str in python 2.7) |
---|
70 | try: |
---|
71 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
---|
72 | return six.text_type(socket.inet_ntoa(fcntl.ioctl( |
---|
73 | s.fileno(), |
---|
74 | 0x8915, # SIOCGIFADDR |
---|
75 | struct.pack(str('256s'), ifname[:15]) |
---|
76 | )[20:24])) |
---|
77 | except Exception: |
---|
78 | return None |
---|
79 | |
---|
80 | |
---|
81 | def _getInterfaces(): |
---|
82 | """ |
---|
83 | Returns a list of interfaces names coded in utf-8 |
---|
84 | """ |
---|
85 | max_possible = 128 # arbitrary. raise if needed. |
---|
86 | space = max_possible * 16 |
---|
87 | if platform.architecture()[0] == '32bit': |
---|
88 | offset, length = 32, 32 |
---|
89 | elif platform.architecture()[0] == '64bit': |
---|
90 | offset, length = 16, 40 |
---|
91 | else: |
---|
92 | raise OSError('Unknown arquitecture {0}'.format(platform.architecture()[0])) |
---|
93 | |
---|
94 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
---|
95 | names = array.array(str('B'), b'\0' * space) |
---|
96 | outbytes = struct.unpack(str('iL'), fcntl.ioctl( |
---|
97 | s.fileno(), |
---|
98 | 0x8912, # SIOCGIFCONF |
---|
99 | struct.pack(str('iL'), space, names.buffer_info()[0]) |
---|
100 | ))[0] |
---|
101 | namestr = names.tostring() |
---|
102 | # return namestr, outbytes |
---|
103 | return [namestr[i:i + offset].split(b'\0', 1)[0].decode('utf-8') for i in range(0, outbytes, length)] |
---|
104 | |
---|
105 | |
---|
106 | def _getIpAndMac(ifname): |
---|
107 | ip, mac = _getIpAddr(ifname), _getMacAddr(ifname) |
---|
108 | return ip, mac |
---|
109 | |
---|
110 | |
---|
111 | def _exec_ogcommand(ogcmd): |
---|
112 | """ |
---|
113 | Loads OpenGnsys environment variables, executes the command and returns the result |
---|
114 | """ |
---|
115 | ret = subprocess.check_output(ogcmd, shell=True) |
---|
116 | return ret |
---|
117 | |
---|
118 | |
---|
119 | def getComputerName(): |
---|
120 | """ |
---|
121 | Returns computer name, with no domain |
---|
122 | """ |
---|
123 | return socket.gethostname().split('.')[0] |
---|
124 | |
---|
125 | |
---|
126 | def getNetworkInfo(): |
---|
127 | """ |
---|
128 | Obtains a list of network interfaces |
---|
129 | :return: A "generator" of elements, that are dict-as-object, with this elements: |
---|
130 | name: Name of the interface |
---|
131 | mac: mac of the interface |
---|
132 | ip: ip of the interface |
---|
133 | """ |
---|
134 | for ifname in _getInterfaces(): |
---|
135 | ip, mac = _getIpAndMac(ifname) |
---|
136 | if mac != '00:00:00:00:00:00': # Skips local interfaces |
---|
137 | yield utils.Bunch(name=ifname, mac=mac, ip=ip) |
---|
138 | |
---|
139 | |
---|
140 | def getDomainName(): |
---|
141 | return '' |
---|
142 | |
---|
143 | |
---|
144 | def get_oglive_version(): |
---|
145 | """ |
---|
146 | Returns ogLive Kernel version and architecture |
---|
147 | :return: kernel version |
---|
148 | """ |
---|
149 | kv = platform.os.uname() |
---|
150 | return kv[2] + ', ' + kv[4] |
---|
151 | |
---|
152 | |
---|
153 | def reboot(): |
---|
154 | """ |
---|
155 | Simple reboot using OpenGnsys script |
---|
156 | """ |
---|
157 | # Workaround for dummy thread |
---|
158 | if six.PY3 is False: |
---|
159 | import threading |
---|
160 | threading._DummyThread._Thread__stop = lambda x: 42 |
---|
161 | |
---|
162 | _exec_ogcommand('/opt/opengnsys/scripts/reboot') |
---|
163 | |
---|
164 | |
---|
165 | def poweroff(): |
---|
166 | """ |
---|
167 | Simple poweroff using OpenGnsys script |
---|
168 | """ |
---|
169 | # Workaround for dummy thread |
---|
170 | if six.PY3 is False: |
---|
171 | import threading |
---|
172 | threading._DummyThread._Thread__stop = lambda x: 42 |
---|
173 | |
---|
174 | _exec_ogcommand('/opt/opengnsys/scripts/poweroff') |
---|
175 | |
---|
176 | |
---|
177 | def get_disk_config(): |
---|
178 | """ |
---|
179 | Returns disk configuration |
---|
180 | Warning: this operation may take some time |
---|
181 | """ |
---|
182 | try: |
---|
183 | _exec_ogcommand('/opt/opengnsys/interfaceAdm/getConfiguration') |
---|
184 | # Returns content of configuration file |
---|
185 | cfgdata = open('/tmp/getconfig', 'r').read().strip() |
---|
186 | except IOError: |
---|
187 | cfgdata = '' |
---|
188 | return cfgdata |
---|