def put_lock(self, blocks): """ Put lock """ code = self._read_input() logging.info("log code: [%s]" % code) if not code: # Missing lock code, try to delete the lock # First check that current lock is specified as a part of the path if self.check_update( blocks, True): return l = data.release_lock() if l == data.LCK_OK: # Let other instances know sync.cluster_state.force_push() # Save new configuration config.save_configuration() self._response_plain( RESPONSE_RELEASED) elif l == data.LCK_CONCURRENT: self._response_conflict( RESPONSE_CONCURRENT) else: self._response_not_found() else: # Lock the resource # Acquiring the same lock again is idempotent if data.try_acquire_lock( code): self._response_plain( RESPONSE_ACQUIRED) else: self._response_forbidden( RESPONSE_LOCKED)
def _pull(self): # Ping the instance and get its version _logger.info( '%s Ping', self.address) # _logger.info( "dump_json: %s", sync.cluster_state.get_state() + [{'address': sync.cluster_state.me}]) # Try to push your state to the other side helpers.push_state( self.address, helpers.dump_json( sync.cluster_state.get_state() + [{'address': sync.cluster_state.me}])) info = helpers.info( self.address) if not info: self._reachable = False return try: self._version = data.DataVersion.from_dict( info[ 'version']) except (TypeError, KeyError): _logger.error( '%s Invalid pulled data', self.address) return self._touch_last_reachable() # Check that the other instance has newer configuration if self._version <= data.cur_data().version: return # The instance has newer configuration, try to pull it _logger.info( '%s Pull', self.address) content = helpers.pull( self.address) if content is None: return False # Check in new data if data.push_data( content): config.save_configuration()
def put_lock(self, blocks): """ Put lock """ code = self._read_input() logging.info("log code: [%s]" % code) if not code: # Missing lock code, try to delete the lock # First check that current lock is specified as a part of the path if self.check_update(blocks, True): return l = data.release_lock() if l == data.LCK_OK: # Let other instances know sync.cluster_state.force_push() # Save new configuration config.save_configuration() self._response_plain(RESPONSE_RELEASED) elif l == data.LCK_CONCURRENT: self._response_conflict(RESPONSE_CONCURRENT) else: self._response_not_found() else: # Lock the resource # Acquiring the same lock again is idempotent if data.try_acquire_lock(code): self._response_plain(RESPONSE_ACQUIRED) else: self._response_forbidden(RESPONSE_LOCKED)
def block(): blacklist = load_blacklist() with open(HOSTS_PATH, "r+") as file: content = file.read() for website in blacklist: if website not in content: # print(f"Blocking {website}...") file.write(f"{REDIRECT} {website}") config.save_configuration(True)
def put_copy(self): """ Pushes new data """ # Get content content = self._read_input_json() # Update with the content given if data.push_data( content): config.save_configuration() self._response_created()
def _service_favorite(name): # Just mark a service (daemon) as favorite if name in config.SERVICES_FAVORITES: config.SERVICES_FAVORITES.remove(name) else: config.SERVICES_FAVORITES.append(name) new_config = {"SERVICES_FAVORITES": config.SERVICES_FAVORITES} config.save_configuration(new_config) return "Toggled favorite"
def unblock(): blacklist = load_blacklist() with open(HOSTS_PATH, "r+") as file: content = file.readlines() file.seek(0) for line in content: if not any(website in line for website in blacklist): file.write(line) file.truncate() config.save_configuration(False)
def put_copy(self): """ Pushes new data """ # Get content content = self._read_input_json() # Update with the content given if data.push_data(content): config.save_configuration() self._response_created()
def config_save(): def check_ip(ip): ip, mask = ip.split('/') if '/' in ip else (ip, "32") if not mask.isdigit(): return None mask = int(mask) ip_parts = ip.split('.') # 1 - A IPv4 must be composed with the quad-dotted format # 2 - check if the IP is in a valid range # 3 - check if the mask is valid if len(ip_parts) != 4 or \ filter(lambda x: not x.isdigit() or int(x) < 0 or int(x) > 255, ip_parts) or \ (mask < 0 or mask > 32): return None return ip if mask == 32 else "%s/%s" % (ip, mask) def bool_eval(name): return request.POST.get(name) == "True" def int_port_default(name, default): try: n = int(request.POST.get(name)) return n if n > 1024 else default except: return default def parse_authlist(): raw_ip_list = map(strip, request.POST.get('AUTH_WHITELIST', []).split(',')) ip_list = filter(None, map(check_ip, raw_ip_list)) return ip_list conf = { 'SHOW_DETAILED_INFO': bool_eval('SHOW_DETAILED_INFO'), 'SHOW_TODO': bool_eval('SHOW_TODO'), 'COMMAND_EXECUTION': bool_eval('COMMAND_EXECUTION'), 'SERVICE_EXECUTION': bool_eval('SERVICE_EXECUTION'), 'PORT': int_port_default('PORT', 8086), 'AUTH_WHITELIST': parse_authlist(), } config.save_configuration(conf) return config_edit(config_saved=True)
def helper_clicked(self, args: dict): """ Called when the helper button is clicked """ face = FaceFactory.get_singleton() lesson = self._current_lesson if args["button"] == AbstractUrlList._BUTTON_START: webbrowser.open(lesson["url"]) if args["button"] == AbstractUrlList._BUTTON_REMOVE: self._config[self._config_section].pop(self._current_lesson_index) save_configuration() face.next_step() if args["button"] == AbstractUrlList._BUTTON_REQUEUE: lesson_copy = copy(lesson) self._config[self._config_section].pop(self._current_lesson_index) self._config[self._config_section].append(lesson_copy) save_configuration() face.next_step() if args["button"] == AbstractUrlList._BUTTON_PASTE_URL: new_url = pyperclip.paste() if new_url != "": self._config[self._config_section][ self._current_lesson_index]["url"] = new_url save_configuration() face.set_step_sub_label_text(new_url)
def _pull(self): # Ping the instance and get its version _logger.info('%s Ping', self.address) # _logger.info( "dump_json: %s", sync.cluster_state.get_state() + [{'address': sync.cluster_state.me}]) # Try to push your state to the other side helpers.push_state( self.address, helpers.dump_json(sync.cluster_state.get_state() + [{ 'address': sync.cluster_state.me }])) info = helpers.info(self.address) if not info: self._reachable = False return try: self._version = data.DataVersion.from_dict(info['version']) except (TypeError, KeyError): _logger.error('%s Invalid pulled data', self.address) return self._touch_last_reachable() # Check that the other instance has newer configuration if self._version <= data.cur_data().version: return # The instance has newer configuration, try to pull it _logger.info('%s Pull', self.address) content = helpers.pull(self.address) if content is None: return False # Check in new data if data.push_data(content): config.save_configuration()