def read_groups(filename): """Parse a JSON file and create the appropriate group and client objects. Return a 2-tuple with a client instances list and a group instances list. """ try: file = open(filename) data = json.loads(file.read()) file.close() except (IOError, OSError) as exc: LOG.e(exc) return [], [] saved_clients = {} for key, cln in data['clients'].items(): new = structs.Client('offline', cln['mac'], '', cln['alias']) saved_clients[key] = new groups = [] for group in data['groups']: members = {} for key, dct in group['members'].items(): members[saved_clients[key]] = dct groups.append(structs.Group(group['name'], members)) return saved_clients.values(), groups
def add_client(self, handle, reply, already=False): """Callback after running `info` on a client.""" # already is True if the client was started before epoptes LOG.w("add_client's been called for", handle) try: info = {} for line in reply.strip().split('\n'): key, value = line.split('=', 1) info[key.strip()] = value.strip() user, host, _ip, mac, type_, uid, version, name = \ info['user'], info['hostname'], info['ip'], info['mac'], \ info['type'], int(info['uid']), info['version'], info['name'] except (KeyError, ValueError) as exc: LOG.e(" Can't extract client information, won't add this client", exc) return False # Check if the incoming client is the same with the computer in which # epoptes is running, so we don't add it to the list. if (mac in self.current_macs) and ((uid == self.uid) or (uid == 0)): LOG.w(" Won't add this client to my lists") return False # Compatibility check if LooseVersion(version) < LooseVersion(COMPATIBILITY_VERSION): self.daemon.command(handle, "die 'Incompatible Epoptes server version!'") if not self.displayed_compatibility_warning: self.displayed_compatibility_warning = True self.warning_dialog( _("""A connection attempt was made by a client with""" """ version %s, which is incompatible with the current""" """ epoptes version.\n\nYou need to update your clients""" """ to the latest epoptes-client version.""") % version) return False sel_group = self.get_selected_group()[1] client = None for inst in structs.clients: # Find if the new handle is a known client if mac == inst.mac: client = inst LOG.w(' Old client: ', end='') break if client is None: LOG.w(' New client: ', end='') client = structs.Client(mac=mac) LOG.w('hostname=%s, type=%s, uid=%s, user=%s' % (host, type_, uid, user)) # Update/fill the client information client.type, client.hostname = type_, host if uid == 0: # This is a root epoptes-client client.hsystem = handle else: # This is a user epoptes-client client.add_user(user, name, handle) if not already and (sel_group.has_client(client) or self.is_default_group_selected()): self.notify_queue.enqueue( _("Connected:"), _("%(user)s on %(host)s") % { "user": user, "host": host }) if sel_group.has_client(client) or self.is_default_group_selected(): self.fill_icon_view(sel_group, True) return True
def addClient(self, handle, r, already=False): # already is True if the client was started before epoptes print "---\n**addClient's been called for", handle try: info = {} for line in r.strip().split('\n'): key, value = line.split('=', 1) info[key.strip()] = value.strip() user, host, ip, mac, type, uid, version, name = \ info['user'], info['hostname'], info['ip'], info['mac'], \ info['type'], int(info['uid']), info['version'], info['name'] except: print "Can't extract client information, won't add this client" return # Check if the incoming client is the same with the computer in which # epoptes is running, so we don't add it to the list. if (mac in self.current_macs) and ((uid == self.uid) or (uid == 0)): print "* Won't add this client to my lists" return False print ' Continuing inside addClient...' # Compatibility check if [int(x) for x in re.split('[^0-9]*', version)] < COMPATIBILITY_VERSION: if not self.shownCompatibilityWarning: self.shownCompatibilityWarning = True dlg = self.get('warningDialog') # Show different messages for LTSP clients and standalones. msg = _( "A connection attempt was made by a client with version %s, \ which is incompatible with the current epoptes version.\ \n\nYou need to update your clients to the latest epoptes-client version." ) % version dlg.set_property('text', msg) dlg.set_transient_for(self.mainwin) dlg.show() self.daemon.command(handle, u"exit") return False sel_group = self.getSelectedGroup()[1] client = None for inst in structs.clients: # Find if the new handle is a known client if mac == inst.mac: client = inst print '* This is an existing client' break if client is None: print '* This client is a new one, creating an instance' client = structs.Client(mac=mac) # Update/fill the client information client.type, client.hostname = type, host if uid == 0: # This is a root epoptes-client print '* I am a root client' client.hsystem = handle else: # This is a user epoptes-client print '* I am a user client, will add', user, 'in my list' client.add_user(user, name, handle) if not already and (sel_group.has_client(client) or self.isDefaultGroupSelected()): loginNotify(user, host) if sel_group.has_client(client) or self.isDefaultGroupSelected(): self.fillIconView(sel_group, True)