source: admin/Sources/Clients/ogagent/src/opengnsys/macos/operations.py @ ec529cc

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-instalacionwebconsole3
Last change on this file since ec529cc was 96b942a, checked in by ramon <ramongomez@…>, 8 years ago

#718: OGAgent para macOS: inicio automático y detección de datos de red.

git-svn-id: https://opengnsys.es/svn/branches/version1.1@5206 a21b9725-9963-47de-94b9-378ad31fedc9

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