def stat_uninstall(bin_path): agent_datasource = AgentDatasource(config_storage=ConfigStorage(alternate_path=f"{bin_path}/configPlugins.json")) report_enable = agent_datasource.get_report_enable() stats_tracker = StatsTracker(sync=True, anonymizer=Anonymizer(alternate_path=f"{bin_path}/anonymize.json")) stats_tracker.report_enable = report_enable login = getpass.getuser() stats_tracker.log_uninstall(login) print("record uninstall")
def __init__(self, on_skills_ready, on_server_running, on_server_stopped): self.command_id = 0 self.server_running = False self.server_process = None self.current_active_skill = '' self.agent_datasource = AgentDatasource() self.on_skills_ready = on_skills_ready self.on_server_running = on_server_running self.on_server_stopped = on_server_stopped
def test_should_return_the_list_with_the_new_selected_values_if_exists_and_is_installed( mocker): mock_agent = create_mock_agent() mocker.patch.object(AgentDatasource, 'get_instances', return_value=[mock_agent], autospec=True) mocker.patch.object(ConfigStorage, 'read_config', return_value=PluginConfig(selected=["demo_agent"]), autospec=True) mocker.patch.object(ConfigStorage, 'store_config', return_value=None, autospec=True) mocker.patch.object(AgentDatasource, 'all_plugins', return_value=ALL_PLUGINS_WITH_TAR_INSTALLED, autospec=True) message_handler = MessageHandler(ServerStatusDatasource(), AgentDatasource()) select_agent = clai_select_state('nlc2cmd') action = message_handler.process_message(select_agent) assert action.suggested_command == NOOP_COMMAND assert action.origin_command == select_agent.command assert action.execute assert message_handler.agent_datasource.get_current_plugin_name( select_agent.user_name) == ['nlc2cmd']
def test_should_return_the_install_command_when_the_new_plugin_is_not_installed_yet( mocker): mock_agent = create_mock_agent() mocker.patch.object(AgentDatasource, 'get_instances', return_value=[mock_agent], autospec=True) mocker.patch.object(ConfigStorage, 'read_config', return_value=PluginConfig(selected=["nlc2cmd"]), autospec=True) mocker.patch.object(ConfigStorage, 'store_config', return_value=None, autospec=True) mocker.patch.object(AgentDatasource, 'all_plugins', return_value=ALL_PLUGINS, autospec=True) message_handler = MessageHandler(ServerStatusDatasource(), AgentDatasource()) select_agent = clai_select_state('nlc2cmd') action = message_handler.process_message(select_agent) assert action.suggested_command == "$CLAI_PATH/fileExist.sh nlc2cmd" assert action.origin_command == select_agent.command assert message_handler.agent_datasource.get_current_plugin_name( select_agent.user_name) == ['nlc2cmd']
def test_should_return_the_list_of_plugins_with_default_selected_when_the_server_received_plugins_no_selected( mocker): mock_agent = create_mock_agent() mocker.patch.object(AgentDatasource, 'get_instances', return_value=[mock_agent], autospec=True) mocker.patch.object(ConfigStorage, 'read_config', return_value=NO_SELECTED, autospec=True) mocker.patch.object(AgentDatasource, 'all_plugins', return_value=ALL_PLUGINS, autospec=True) message_handler = MessageHandler(ServerStatusDatasource(), AgentDatasource()) action = message_handler.process_message(clai_plugins_state()) assert action.suggested_command == NOOP_COMMAND assert action.origin_command == 'clai skills' assert action.execute assert action.description == expected_description(ALL_PLUGINS, NO_SELECTED.default)
def test_should_return_the_list_without_any_selected_plugin_when_default_doesnt_exist( mocker): mock_agent = create_mock_agent() mocker.patch.object(AgentDatasource, 'get_instances', return_value=[mock_agent], autospec=True) mocker.patch.object(AgentDatasource, 'all_plugins', return_value=ALL_PLUGINS, autospec=True) mocker.patch.object(ConfigStorage, 'read_config', return_value=PluginConfig( default="", default_orchestrator="max_orchestrator"), autospec=True) message_handler = MessageHandler(ServerStatusDatasource(), AgentDatasource()) action = message_handler.process_message(clai_plugins_state()) assert action.suggested_command == NOOP_COMMAND assert action.origin_command == 'clai skills' assert action.execute assert action.description == expected_description(ALL_PLUGINS, '')
def install_orchestration(bin_path): agent_datasource = AgentDatasource(config_storage=ConfigStorage( alternate_path=f'{bin_path}/configPlugins.json')) orchestrator_provider = OrchestratorProvider(agent_datasource) all_orchestrators = orchestrator_provider.all_orchestrator() for orchestrator in all_orchestrators: install_orchestration_dependencies(bin_path, orchestrator.name)
def test_should_return_an_error_when_selected_is_empty(mocker): mock_agent = create_mock_agent() mocker.patch.object(AgentDatasource, 'get_instances', return_value=[mock_agent], autospec=True) mocker.patch.object(AgentDatasource, 'all_plugins', return_value=ALL_PLUGINS, autospec=True) mocker.patch.object(ConfigStorage, 'read_config', return_value=PluginConfig(selected=["nlc2cmd"]), autospec=True) mocker.patch.object(ConfigStorage, 'store_config', return_value=None, autospec=True) message_handler = MessageHandler(ServerStatusDatasource(), AgentDatasource()) select_agent = clai_select_state('') action = message_handler.process_message(select_agent) assert action.suggested_command == NOOP_COMMAND assert action.origin_command == select_agent.command assert action.execute assert action.description == create_error_select('').description assert message_handler.agent_datasource.get_current_plugin_name( select_agent.user_name) == ['nlc2cmd']
def test_should_return_the_command_to_get_the_plugin_from_url_and_move_it_into_plugin_folder_server( ): agent_datasource = AgentDatasource() clai_install_command_runner = ClaiInstallCommandRunner(agent_datasource) command_to_execute = clai_install_command_runner.execute( CLAI_INSTALL_STATE_URL) dir_to_install = CLAI_INSTALL_STATE_URL.command.replace( f'{"clai install"}', '').strip() assert command_to_execute.suggested_command == f'cd $CLAI_PATH/clai/server/plugins && curl -O {dir_to_install}'
def install_plugins(install_path, user_install): agent_datasource = AgentDatasource(config_storage=ConfigStorage( alternate_path=f'{install_path}/configPlugins.json')) plugins = agent_datasource.all_plugins() for plugin in plugins: default = z_default = False if PLATFORM in ('zos', 'os390'): z_default = plugin.z_default else: default = plugin.default if default or z_default: installed = install_plugins_dependencies(install_path, plugin.pkg_name, user_install) if installed: agent_datasource.mark_plugins_as_installed(plugin.name, None) return agent_datasource
def __init__(self, on_skills_ready, on_server_running, on_server_stopped): self.server_running = False self.server_process = None self.current_active_skill = '' self.agent_datasource = AgentDatasource() self.on_skills_ready = on_skills_ready self.on_server_running = on_server_running self.on_server_stopped = on_server_stopped self.my_clai: Optional[Container] = None
def __init__(self, server_status_datasource: ServerStatusDatasource = current_status_datasource, connector: ServerConnector = SocketServerConnector(current_status_datasource), remote_storage: ActionRemoteStorage = action_remote_storage, agent_datasource=AgentDatasource(), stats=stats_tracker ): self.connector = connector self.agent_datasource = agent_datasource self.server_status_datasource = server_status_datasource self.message_handler = MessageHandler(server_status_datasource, agent_datasource=agent_datasource) self.remote_storage = remote_storage self.stats_tracker = stats
def test_should_return_the_command_to_get_the_plugin_from_route_and_move_it_into_plugin_folder_server( mocker): mock_exisit = mocker.patch('os.path.exists') mock_exisit.return_value = True mock_exisit = mocker.patch('os.path.isdir') mock_exisit.return_value = True agent_datasource = AgentDatasource() clai_install_command_runner = ClaiInstallCommandRunner(agent_datasource) command_to_execute = clai_install_command_runner.execute( CLAI_INSTALL_STATE_FOLDER) dir_to_install = CLAI_INSTALL_STATE_FOLDER.command.replace( f'{"clai install"}', '').strip() assert command_to_execute.suggested_command == f'cp -R {dir_to_install} $CLAI_PATH/clai/server/plugins'
def test_should_return_the_message_error_when_the_folder_doesnt_exist(mocker): mock_exisit = mocker.patch('os.path.exists') mock_exisit.return_value = False mock_exisit = mocker.patch('os.path.isdir') mock_exisit.return_value = False agent_datasource = AgentDatasource() clai_install_command_runner = ClaiInstallCommandRunner(agent_datasource) command_to_execute = clai_install_command_runner.execute( CLAI_INSTALL_STATE_FOLDER) dir_to_install = CLAI_INSTALL_STATE_FOLDER.command.replace( f'{"clai install"}', '').strip() assert command_to_execute.suggested_command == ':' assert command_to_execute.description == create_error_install( dir_to_install).description
def test_should_return_valid_action_if_the_select_agent_return_none(mocker): mock_agent = create_mock_agent() mocker.patch.object(AgentDatasource, 'get_instances', return_value=[mock_agent], autospec=True) mock_agent.execute.return_value = None mocker.patch.object(ConfigStorage, 'read_config', return_value=PluginConfig(selected=["demo_agent"]), autospec=True) message_handler = MessageHandler(ServerStatusDatasource(), AgentDatasource()) action = message_handler.process_message(command_state()) assert action.suggested_command is action.origin_command assert action.origin_command == command_state().command assert not action.execute assert not action.description
def test_should_return_the_suggestion_from_agent_ignoring_confidence_if_is_name_agent_command( mocker): mock_agent = create_mock_agent() mocker.patch.object(AgentDatasource, 'get_instances', return_value=[mock_agent], autospec=True) action_to_execute = Action(suggested_command="command", confidence=0.0) mock_agent.execute.return_value = action_to_execute mocker.patch.object(ConfigStorage, 'read_config', return_value=PluginConfig(selected=["demo_agent"]), autospec=True) message_handler = MessageHandler(ServerStatusDatasource(), AgentDatasource()) action = message_handler.process_message(COMMAND_NAME_AGENT_STATE) assert action.suggested_command == action_to_execute.suggested_command assert action.origin_command == command_state().command assert not action.execute assert not action.description
def test_should_return_empty_action_from_selected_agent_when_the_command_goes_to_the_agent_and_not_confidence( mocker): mock_agent = create_mock_agent() mocker.patch.object(AgentDatasource, 'get_instances', return_value=[mock_agent], autospec=True) action_to_execute = Action(suggested_command="command", confidence=0.1) mock_agent.execute.return_value = action_to_execute mocker.patch.object(ConfigStorage, 'read_config', return_value=PluginConfig(selected=["demo_agent"]), autospec=True) message_handler = MessageHandler(ServerStatusDatasource(), AgentDatasource()) action = message_handler.process_message(command_state()) assert action.suggested_command is action.origin_command assert action.origin_command == command_state().command assert not action.execute assert not action.description
class EmulatorPresenter: def __init__(self, on_skills_ready, on_server_running, on_server_stopped): self.command_id = 0 self.server_running = False self.server_process = None self.current_active_skill = '' self.agent_datasource = AgentDatasource() self.on_skills_ready = on_skills_ready self.on_server_running = on_server_running self.on_server_stopped = on_server_stopped def stop_server(self): if self.server_process: self.server_process.kill() def select_skill(self, skill_name: str, installed: bool): if skill_name == self.current_active_skill: return if installed: self._send_select(skill_name) self._send_unselect(self.current_active_skill) else: if self.install_skill(skill_name): self._send_select(skill_name) self._send_unselect(self.current_active_skill) self.request_skills() def request_skills(self): response = self._send_to_emulator("clai skills") skills_as_string = response.description skills_as_array = skills_as_string.splitlines() print(f"skills: {skills_as_array[1:-1]}") self.on_skills_ready(skills_as_array) def install_skill(self, skill_name: str) -> bool: agent_descriptor = self.agent_datasource.get_agent_descriptor( skill_name) exec_message = subprocess.Popen( f"sh {os.getcwd()}/../clai/emulator/emufileExist.sh {agent_descriptor.pkg_name}", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = exec_message.communicate() result_code = exec_message.returncode print(f"code {result_code}") print(f"err {stderr}") print(f"out {stdout}") if result_code == 0: self.agent_datasource.mark_plugins_as_installed(skill_name, "user") return result_code == 0 def send_message(self, message): response = self._send_to_emulator(message, increase_id=False) command_to_execute = response.suggested_command if not response.suggested_command: command_to_execute = response.origin_command exec_message = subprocess.Popen(command_to_execute, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) _, stderr = exec_message.communicate() result_code = exec_message.returncode print(result_code, stderr) response_post = self._send_to_emulator_post_command( result_code, stderr) return response, response_post def _send_select(self, skill_name: str): self._send_to_emulator(f'clai select {skill_name}') def _send_unselect(self, skill_name: str): self._send_to_emulator(f'clai unselect {skill_name}') def _send_to_emulator(self, command: str, increase_id: bool = True) -> Action: response = send_command(command_id=self.command_id, user_name="user", command_to_check=command, host="localhost", port=8020) if increase_id: self.command_id = self.command_id + 1 return response def _send_to_emulator_post_command(self, result_code, stderr): response_post = send_command_post_execute( command_id=self.command_id, user_name="user", result_code=result_code, stderr=stderr, processes=ProcessesValues(last_processes=[]), host="localhost", port=8020) self.command_id = self.command_id + 1 return response_post def run_server(self): # pylint: disable=attribute-defined-outside-init self.server_thread = threading.Thread(target=self._run_server) self.server_thread.start() def _run_server(self): self.server_running = True self.on_server_running() self.server_process = subprocess.Popen( ["python3", f"{os.getcwd()}/clai-run", "new", "--port", "8020"], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) print(f"process id {self.server_process.pid}") time.sleep(1) self.request_skills() self.server_process.wait() stdout, stderr = self.server_process.communicate() return_code = self.server_process.returncode self.server_running = False self.on_server_stopped() print(f"code {return_code}") print(f"out {stdout}") print(f"err {stderr}")
def execute(args): unassisted = args.unassisted no_skills = args.no_skills demo_mode = args.demo_mode bin_path = os.path.join(args.destdir, 'bin') code_path = os.path.join(bin_path, 'clai') cli_path = os.path.join(bin_path, 'bin') temp_path = '~/tmp' mkdir(f"{temp_path}/") create_rc_file_if_not_exist(args.system) if clai_installed(get_setup_file()): print_error('CLAI is already in you system. You should execute uninstall first') sys.exit(1) if not binary_installed(bin_path): mkdir(bin_path) mkdir(code_path) cp_tree('./clai', code_path) cp_tree('./bin', cli_path) copy('./scripts/clai.sh', bin_path) copy('./scripts/saveFilesChanges.sh', bin_path) copy('./configPlugins.json', bin_path) copy('./usersInstalled.json', bin_path) copy('./anonymize.json', bin_path) copy('./scripts/fileExist.sh', bin_path) os.system(f'chmod 775 {bin_path}/saveFilesChanges.sh') os.system(f'chmod 775 {bin_path}/fileExist.sh') os.system(f'chmod -R 777 {code_path}/server/plugins') os.system(f'chmod 777 {bin_path}/clai.sh') os.system(f'chmod 666 {bin_path}/configPlugins.json') os.system(f'chmod 666 {bin_path}/anonymize.json') os.system(f'chmod -R 777 {bin_path}') cli_executable(cli_path) download_file(URL_BASH_PREEXEC, filename='%s/%s' % (temp_path, BASH_PREEXEC)) copy('%s/%s' % (temp_path, BASH_PREEXEC), bin_path) register_the_user(bin_path, args.system) append_setup_to_file(get_setup_file(), bin_path) register_file(args.system) if not no_skills: agent_datasource = AgentDatasource( config_storage=ConfigStorage(alternate_path=f'{bin_path}/configPlugins.json')) plugins = agent_datasource.all_plugins() for plugin in plugins: if plugin.default: installed = install_plugins_dependencies(bin_path, plugin.pkg_name) if installed: agent_datasource.mark_plugins_as_installed(plugin.name, None) save_report_info(unassisted, agent_datasource, bin_path, demo_mode) remove(f"{temp_path}/") os.system(f'chmod -R 777 /var/tmp') print_complete("CLAI has been installed correctly, you need restart your shell.")
def start(self, agent_datasource: AgentDatasource): logger.info(f"-Start tracker-") self.report_enable = agent_datasource.get_report_enable() if self.report_enable: self.consumer_stats = self.pool.map_async(self.consumer, (self.queue, ))