def refresh_old_version_checkboxes(self): get_root_state_directory() old_state_dirs = self.version_history.get_disposable_state_directories( ) self.refresh_version_checkboxes(self.window().state_dir_list, old_state_dirs, enabled=True)
async def get_log(self, request): # First, flush all the logs to make sure it is written to file for handler in logging.getLogger().handlers: handler.flush() # Default response response = {'content': '', 'max_lines': 0} # Get the location of log file param_process = request.query.get('process', 'core') log_name = f'tribler-{param_process}-info.log' log_file_name = self.log_dir / log_name # If the log file is not present in the versioned state directory, try root state directory location if not log_file_name.exists(): log_file_name = get_root_state_directory() / log_name # If the log file is still not found, maybe it is not created yet, then return the default response if not log_file_name.exists(): return RESTResponse(response) # If the log file exists and return last requested 'max_lines' of log try: max_lines = int(request.query['max_lines']) with log_file_name.open(mode='r') as log_file: response['content'] = self.tail(log_file, max_lines) response['max_lines'] = max_lines except ValueError: with log_file_name.open(mode='r') as log_file: response['content'] = self.tail(log_file, 100) # default 100 lines response['max_lines'] = 0 return RESTResponse(response)
def __init__(self, api_port, api_key, error_handler): QObject.__init__(self, None) self._logger = logging.getLogger(self.__class__.__name__) self.base_path = get_base_path() if not is_frozen(): self.base_path = os.path.join(get_base_path(), "..") root_state_dir = get_root_state_directory() self.version_history = VersionHistory(root_state_dir) self.core_process = None self.api_port = api_port self.api_key = api_key self.events_manager = EventRequestManager(self.api_port, self.api_key, error_handler) self.shutting_down = False self.should_stop_on_shutdown = False self.use_existing_core = True self.is_core_running = False self.core_traceback = None self.core_traceback_timestamp = 0 self.check_state_timer = QTimer() self.check_state_timer.setSingleShot(True) connect(self.check_state_timer.timeout, self.check_core_ready)
def __init__(self): QWidget.__init__(self) self.settings = None self.version_history = VersionHistory(get_root_state_directory()) self.lang_list = sorted([ lang_name for lang_name, lang_code in AVAILABLE_TRANSLATIONS.items() ]) self.lang_list.insert(0, tr("System default"))
async def start(self, options): # Determine ipv8 port ipv8_port = options.ipv8_port if ipv8_port == -1 and "HELPER_INDEX" in os.environ and "HELPER_BASE" in os.environ: base_port = int(os.environ["HELPER_BASE"]) ipv8_port = base_port + int(os.environ["HELPER_INDEX"]) * 5 statedir = Path( os.path.join(get_root_state_directory(), "tunnel-%d") % ipv8_port) config = TriblerConfig(statedir, config_file=statedir / 'triblerd.conf') config.set_tunnel_community_socks5_listen_ports([]) config.set_tunnel_community_random_slots(options.random_slots) config.set_tunnel_community_competing_slots(options.competing_slots) config.set_torrent_checking_enabled(False) config.set_ipv8_enabled(True) config.set_libtorrent_enabled(False) config.set_ipv8_port(ipv8_port) config.set_ipv8_address(options.ipv8_address) config.set_trustchain_enabled(True) config.set_market_community_enabled(False) config.set_dht_enabled(True) config.set_tunnel_community_exitnode_enabled(bool(options.exit)) config.set_popularity_community_enabled(False) config.set_testnet(bool(options.testnet)) config.set_chant_enabled(False) config.set_bootstrap_enabled(False) if not options.no_rest_api: config.set_http_api_enabled(True) api_port = options.restapi if "HELPER_INDEX" in os.environ and "HELPER_BASE" in os.environ: api_port = int(os.environ["HELPER_BASE"]) + 10000 + int( os.environ["HELPER_INDEX"]) config.set_http_api_port(api_port) if options.ipv8_bootstrap_override is not None: config.set_ipv8_bootstrap_override(options.ipv8_bootstrap_override) self.session = Session(config) self.log_circuits = options.log_circuits self.session.notifier.add_observer(NTFY.TUNNEL_REMOVE, self.circuit_removed) await self.session.start() if options.log_rejects: # We set this after Tribler has started since the tunnel_community won't be available otherwise self.session.tunnel_community.reject_callback = self.on_circuit_reject # Tunnel helpers store more TrustChain blocks self.session.trustchain_community.settings.max_db_blocks = 5000000 self.tribler_started()
def make_config(options) -> TriblerConfig: # Determine ipv8 port ipv8_port = options.ipv8_port if ipv8_port == -1: if "HELPER_INDEX" in os.environ and "HELPER_BASE" in os.environ: base_port = int(os.environ["HELPER_BASE"]) ipv8_port = base_port + int(os.environ["HELPER_INDEX"]) * 5 else: raise ValueError( 'ipv8_port option is not set, and HELPER_BASE/HELPER_INDEX env vars are not defined' ) statedir = Path( os.path.join(get_root_state_directory(), "tunnel-%d") % ipv8_port) config = TriblerConfig.load(file=statedir / 'triblerd.conf', state_dir=statedir) config.tunnel_community.random_slots = options.random_slots config.tunnel_community.competing_slots = options.competing_slots config.torrent_checking.enabled = False config.ipv8.enabled = True config.libtorrent.enabled = False config.ipv8.port = ipv8_port config.ipv8.address = options.ipv8_address config.dht.enabled = True config.tunnel_community.exitnode_enabled = bool(options.exit) config.popularity_community.enabled = False config.tunnel_community.testnet = bool(options.testnet) config.chant.enabled = False config.bootstrap.enabled = False if not options.no_rest_api: https = bool(options.cert_file) config.api.https_enabled = https config.api.http_enabled = not https config.api.key = options.api_key api_port = options.restapi if "HELPER_INDEX" in os.environ and "HELPER_BASE" in os.environ: api_port = int(os.environ["HELPER_BASE"]) + 10000 + int( os.environ["HELPER_INDEX"]) if https: config.api.https_port = api_port config.api.put_path_as_relative('https_certfile', options.cert_file, config.state_dir) else: config.api.http_port = api_port else: config.api.https_enabled = False config.api.http_enabled = False if options.ipv8_bootstrap_override is not None: config.ipv8.bootstrap_override = options.ipv8_bootstrap_override return config
def _check_already_running(self): self.logger.info( f'Check if we are already running a Tribler instance in: {self.working_dir}' ) root_state_dir = get_root_state_directory() self.process_checker = ProcessChecker(root_state_dir) if self.process_checker.already_running: self.logger.error( f"Another Tribler instance is already using directory: {self.working_dir}" ) asyncio.get_running_loop().stop()
def __init__(self, state_directory=None): self.state_directory = state_directory or get_root_state_directory() self.lock_file_path = self.state_directory / LOCK_FILE_NAME if self.lock_file_path.exists(): # Check for stale lock file (created before the os was last restarted). # The stale file might contain the pid of another running process and # not the Tribler itself. To find out we can simply check if the lock file # was last modified before os reboot. # lock_file_modification_time < system boot time file_pid = self.get_pid_from_lock_file() if file_pid < 1 or self.lock_file_path.stat( ).st_mtime < psutil.boot_time(): self.remove_lock_file() self.already_running = self.is_process_running()
def start(self, core_args=None, core_env=None): """ First test whether we already have a Tribler process listening on port <CORE_API_PORT>. If so, use that one and don't start a new, fresh session. """ def on_request_error(_): self.use_existing_core = False self.start_tribler_core(core_args=core_args, core_env=core_env) self.events_manager.connect() connect(self.events_manager.reply.error, on_request_error) # This is a hack to determine if we have notify the user to wait for the directory fork to finish _, _, src_dir, tgt_dir = should_fork_state_directory( get_root_state_directory(), version_id) if src_dir is not None: # There is going to be a directory fork, so we extend the core connection timeout and notify the user self.events_manager.remaining_connection_attempts = 1200 self.events_manager.change_loading_text.emit( "Copying data from previous Tribler version, please wait")
async def test_debug_pane_core_logs_in_root_dir(env_state_directory, enable_api, session): """ Test whether the API returns the logs when logs are present in the root directory. """ # Tribler logs are by default set to root state directory. Here we define the # root state directory by updating 'TSTATEDIR' environment variable. log_dir = get_root_state_directory() process = 'core' num_logs = 100 create_dummy_logs(log_dir, process='core', num_logs=num_logs) json_response = await do_request( session, f'debug/log?process={process}&max_lines={num_logs}', expected_code=200) logs = json_response['content'].strip().split("\n") # Check number of logs returned is correct assert len(logs) == num_logs
def on_remove_version_dirs(self, _): root_version_dir = str(get_root_state_directory()) def dir_from_checkbox_text(checkbox): # eg text: "/home/<user>/.Tribler/v7.8 5 GB" state_dir = checkbox.text().rpartition(" ")[0] if not state_dir.startswith(root_version_dir): return None # safety check just for case state_dir = state_dir[len(root_version_dir):] if state_dir.startswith('/'): state_dir = state_dir[1:] return state_dir dirs_selected_for_deletion = [] for checkbox in self.window().state_dir_list.findChildren(QCheckBox): if checkbox.isChecked(): state_dir = dir_from_checkbox_text(checkbox) if state_dir: dirs_selected_for_deletion.append(state_dir) if self.on_confirm_remove_version_dirs(dirs_selected_for_deletion): remove_state_dirs(root_version_dir, dirs_selected_for_deletion) self.refresh_old_version_checkboxes()
def __init__(self): QWidget.__init__(self) self.settings = None self.saved_dialog = None self.version_history = VersionHistory(get_root_state_directory())
def refresh_current_version_checkbox(self): get_root_state_directory() code_version_dir = self.version_history.code_version.directory self.refresh_version_checkboxes(self.window().state_dir_current, [code_version_dir], enabled=False)
strategy=SentryStrategy.SEND_ALLOWED) logger.info('Sentry has been initialised in debug mode') def init_boot_logger(): # this logger config will be used before Core and GUI # set theirs configs explicitly logging.basicConfig(level=logging.INFO, stream=sys.stdout) if __name__ == "__main__": init_boot_logger() init_sentry_reporter() # Get root state directory (e.g. from environment variable or from system default) root_state_dir = get_root_state_directory() logger.info(f'Root state dir: {root_state_dir}') # Check whether we need to start the core or the user interface if 'CORE_PROCESS' in os.environ: logger.info('Running in "core" mode') # Check for missing Core dependencies check_for_missing_dependencies(scope='core') base_path = os.environ['CORE_BASE_PATH'] api_port = os.environ['CORE_API_PORT'] api_key = os.environ['CORE_API_KEY'] core_test_mode = bool(os.environ.get("TRIBLER_CORE_TEST_MODE", False)) start_tribler_core(base_path, api_port,
async def start_tribler(self, options): """ Main method to startup Tribler. """ async def signal_handler(sig): print(f"Received shut down signal {sig}") if not self._stopping: self._stopping = True await self.session.shutdown() print("Tribler shut down") get_event_loop().stop() self.process_checker.remove_lock_file() signal.signal(signal.SIGINT, lambda sig, _: ensure_future(signal_handler(sig))) signal.signal(signal.SIGTERM, lambda sig, _: ensure_future(signal_handler(sig))) statedir = Path(options.statedir or Path(get_appstate_dir(), '.Tribler')) config = TriblerConfig.load(file=statedir / 'triblerd.conf', state_dir=statedir) # Check if we are already running a Tribler instance root_state_dir = get_root_state_directory() self.process_checker = ProcessChecker(root_state_dir) if self.process_checker.already_running: print( f"Another Tribler instance is already using statedir {config.state_dir}" ) get_event_loop().stop() return print("Starting Tribler") if options.restapi > 0: config.api.http_enabled = True config.api.http_port = options.restapi if options.ipv8 > 0: config.ipv8.port = options.ipv8 elif options.ipv8 == 0: config.ipv8.enabled = False if options.libtorrent != -1 and options.libtorrent > 0: config.libtorrent.port = options.libtorrent if options.ipv8_bootstrap_override is not None: config.ipv8.bootstrap_override = options.ipv8_bootstrap_override if options.testnet: config.tunnel_community.testnet = True config.chant.testnet = True config.bandwidth_accounting.testnet = True self.session = Session(config) try: await self.session.start_components() except Exception as e: print(str(e)) get_event_loop().stop() else: print("Tribler started")