source: admin/Sources/Clients/ogagent/src/opengnsys/linux/operations.py @ 062ea34

918-git-images-111dconfigfileconfigure-oglivegit-imageslgromero-new-oglivemainmaint-cronmount-efivarfsmultivmmultivm-ogboot-installerogClonningEngineogboot-installer-jenkinsoglive-ipv6test-python-scriptsticket-301ticket-50ticket-50-oldticket-577ticket-585ticket-611ticket-612ticket-693ticket-700ubu24tplunification2use-local-agent-oglivevarios-instalacion
Last change on this file since 062ea34 was 703551b3, checked in by Ramón M. Gómez <ramongomez@…>, 6 years ago

#877: Copy hosts file on OGAgent activation, and some code cleanup.

  • Property mode set to 100644
File size: 9.1 KB
Line 
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: Adolfo Gómez, dkmaster at dkmon dot com
31'''
32from __future__ import unicode_literals
33
34import socket
35import platform
36import fcntl
37import os
38import locale
39import ctypes  # @UnusedImport
40import ctypes.util
41import subprocess
42import struct
43import array
44import six
45from opengnsys import utils
46from .renamer import rename
47
48
49def _getMacAddr(ifname):
50    '''
51    Returns the mac address of an interface
52    Mac is returned as unicode utf-8 encoded
53    '''
54    if isinstance(ifname, list):
55        return dict([(name, _getMacAddr(name)) for name in ifname])
56    if isinstance(ifname, six.text_type):
57        ifname = ifname.encode('utf-8')  # If unicode, convert to bytes (or str in python 2.7)
58    try:
59        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
60        info = bytearray(fcntl.ioctl(s.fileno(), 0x8927, struct.pack(str('256s'), ifname[:15])))
61        return six.text_type(''.join(['%02x:' % char for char in info[18:24]])[:-1])
62    except Exception:
63        return None
64
65
66def _getIpAddr(ifname):
67    '''
68    Returns the ip address of an interface
69    Ip is returned as unicode utf-8 encoded
70    '''
71    if isinstance(ifname, list):
72        return dict([(name, _getIpAddr(name)) for name in ifname])
73    if isinstance(ifname, six.text_type):
74        ifname = ifname.encode('utf-8')  # If unicode, convert to bytes (or str in python 2.7)
75    try:
76        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
77        return six.text_type(socket.inet_ntoa(fcntl.ioctl(
78            s.fileno(),
79            0x8915,  # SIOCGIFADDR
80            struct.pack(str('256s'), ifname[:15])
81        )[20:24]))
82    except Exception:
83        return None
84
85
86def _getInterfaces():
87    '''
88    Returns a list of interfaces names coded in utf-8
89    '''
90    max_possible = 128  # arbitrary. raise if needed.
91    space = max_possible * 16
92    if platform.architecture()[0] == '32bit':
93        offset, length = 32, 32
94    elif platform.architecture()[0] == '64bit':
95        offset, length = 16, 40
96    else:
97        raise OSError('Unknown arquitecture {0}'.format(platform.architecture()[0]))
98
99    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
100    names = array.array(str('B'), b'\0' * space)
101    outbytes = struct.unpack(str('iL'), fcntl.ioctl(
102        s.fileno(),
103        0x8912,  # SIOCGIFCONF
104        struct.pack(str('iL'), space, names.buffer_info()[0])
105    ))[0]
106    namestr = names.tostring()
107    # return namestr, outbytes
108    return [namestr[i:i + offset].split(b'\0', 1)[0].decode('utf-8') for i in range(0, outbytes, length)]
109
110
111def _getIpAndMac(ifname):
112    ip, mac = _getIpAddr(ifname), _getMacAddr(ifname)
113    return (ip, mac)
114
115
116def getComputerName():
117    '''
118    Returns computer name, with no domain
119    '''
120    return socket.gethostname().split('.')[0]
121
122
123def getNetworkInfo():
124    '''
125    Obtains a list of network interfaces
126    @return: A "generator" of elements, that are dict-as-object, with this elements:
127      name: Name of the interface
128      mac: mac of the interface
129      ip: ip of the interface
130    '''
131    for ifname in _getInterfaces():
132        ip, mac = _getIpAndMac(ifname)
133        if mac != '00:00:00:00:00:00':  # Skips local interfaces
134            yield utils.Bunch(name=ifname, mac=mac, ip=ip)
135
136
137def getDomainName():
138    return ''
139
140
141def getLinuxVersion():
142    lv = platform.linux_distribution()
143    return lv[0] + ', ' + lv[1]
144
145
146def reboot(flags=0):
147    '''
148    Simple reboot using os command
149    '''
150    # Workaround for dummy thread
151    if six.PY3 is False:
152        import threading
153        threading._DummyThread._Thread__stop = lambda x: 42
154
155    # Check for OpenGnsys Client or GNU/Linux distribution.
156    if os.path.exists('/scripts/oginit'):
157        subprocess.call('source /opt/opengnsys/etc/preinit/loadenviron.sh; /opt/opengnsys/scripts/reboot', shell=True)
158    else:
159        subprocess.call(['/sbin/reboot'])
160
161
162def poweroff(flags=0):
163    '''
164    Simple poweroff using os command
165    '''
166    # Workaround for dummy thread
167    if six.PY3 is False:
168        import threading
169        threading._DummyThread._Thread__stop = lambda x: 42
170
171    # Check for OpenGnsys Client or GNU/Linux distribution.
172    if os.path.exists('/scripts/oginit'):
173        subprocess.call('source /opt/opengnsys/etc/preinit/loadenviron.sh; /opt/opengnsys/scripts/poweroff', shell=True)
174    else:
175        subprocess.call(['/sbin/poweroff'])
176
177
178def logoff():
179    '''
180    Kills all curent user processes, which must send a logogof
181    caveat: If the user has other sessions, will also disconnect from them
182    '''
183    # Workaround for dummy thread
184    if six.PY3 is False:
185        import threading
186        threading._DummyThread._Thread__stop = lambda x: 42
187
188    subprocess.call(['/usr/bin/pkill', '-u', os.environ['USER']])
189
190
191def renameComputer(newName):
192    rename(newName)
193
194
195def joinDomain(domain, ou, account, password, executeInOneStep=False):
196    pass
197
198
199def changeUserPassword(user, oldPassword, newPassword):
200    '''
201    Simple password change for user using command line
202    '''
203    os.system('echo "{1}\n{1}" | /usr/bin/passwd {0} 2> /dev/null'.format(user, newPassword))
204
205
206class XScreenSaverInfo(ctypes.Structure):
207    _fields_ = [('window', ctypes.c_long),
208                ('state', ctypes.c_int),
209                ('kind', ctypes.c_int),
210                ('til_or_since', ctypes.c_ulong),
211                ('idle', ctypes.c_ulong),
212                ('eventMask', ctypes.c_ulong)]
213
214# Initialize xlib & xss
215try:
216    xlibPath = ctypes.util.find_library('X11')
217    xssPath = ctypes.util.find_library('Xss')
218    xlib = ctypes.cdll.LoadLibrary(xlibPath)
219    xss = ctypes.cdll.LoadLibrary(xssPath)
220
221    # Fix result type to XScreenSaverInfo Structure
222    xss.XScreenSaverQueryExtension.restype = ctypes.c_int
223    xss.XScreenSaverAllocInfo.restype = ctypes.POINTER(XScreenSaverInfo)  # Result in a XScreenSaverInfo structure
224except Exception:  # Libraries not accesible, not found or whatever..
225    xlib = xss = None
226
227
228def initIdleDuration(atLeastSeconds):
229    '''
230    On linux we set the screensaver to at least required seconds, or we never will get "idle"
231    '''
232    # Workaround for dummy thread
233    if six.PY3 is False:
234        import threading
235        threading._DummyThread._Thread__stop = lambda x: 42
236
237    subprocess.call(['/usr/bin/xset', 's', '{}'.format(atLeastSeconds + 30)])
238    # And now reset it
239    subprocess.call(['/usr/bin/xset', 's', 'reset'])
240
241
242def getIdleDuration():
243    '''
244    Returns idle duration, in seconds
245    '''
246    if xlib is None or xss is None:
247        return 0  # Libraries not available
248
249    # production code might want to not hardcode the offset 16...
250    display = xlib.XOpenDisplay(None)
251
252    event_base = ctypes.c_int()
253    error_base = ctypes.c_int()
254
255    available = xss.XScreenSaverQueryExtension(display, ctypes.byref(event_base), ctypes.byref(error_base))
256    if available != 1:
257        return 0  # No screen saver is available, no way of getting idle
258
259    info = xss.XScreenSaverAllocInfo()
260    xss.XScreenSaverQueryInfo(display, xlib.XDefaultRootWindow(display), info)
261
262    if info.contents.state != 0:
263        return 3600 * 100 * 1000  # If screen saver is active, return a high enough value
264
265    return info.contents.idle / 1000.0
266
267
268def getCurrentUser():
269    '''
270    Returns current logged in user
271    '''
272    return os.environ['USER']
273
274
275def getSessionLanguage():
276    '''
277    Returns the user's session language
278    '''
279    return locale.getdefaultlocale()[0]
280
281
282def showPopup(title, message):
283    '''
284    Displays a message box on user's session (during 1 min).
285    '''
286    return subprocess.call('zenity --info --timeout 60 --title "{}" --text "{}"'.format(title, message), shell=True)
287
288
289def get_etc_path():
290    """
291    :return:
292    Returns etc directory path.
293    """
294    return os.sep + 'etc'
Note: See TracBrowser for help on using the repository browser.