def cleanup_yarn_request(request_id): """Clean up the Nexus yarn content for the Cachito request.""" payload = { "repository_name": get_yarn_proxy_repo_name(request_id), "username": get_yarn_proxy_repo_username(request_id), } nexus.execute_script("js_cleanup", payload)
def finalize_nexus_for_js_request(repo_name, username): """ Finalize the Nexus configuration so that the request's npm repository is ready for consumption. :param str repo_name: the name of the repository for the request for this package manager :param str username: the username of the user to be created for the request for this package manager :return: the password of the Nexus user that has access to the request's npm repository :rtype: str :raise CachitoError: if the script execution fails """ # Generate a 24-32 character (each byte is two hex characters) password password = secrets.token_hex(random.randint(12, 16)) payload = { "password": password, "repository_name": repo_name, "username": username } script_name = "js_after_content_staged" try: nexus.execute_script(script_name, payload) except NexusScriptError: log.exception("Failed to execute the script %s", script_name) raise CachitoError( "Failed to configure Nexus to allow the request's npm repository to be ready for " "consumption") return password
def cleanup_pip_request(request_id): """Clean up the Nexus Python content for the Cachito request.""" payload = { "pip_repository_name": get_pypi_hosted_repo_name(request_id), "raw_repository_name": get_raw_hosted_repo_name(request_id), "username": get_hosted_repositories_username(request_id), } nexus.execute_script("pip_cleanup", payload)
def test_execute_script_connection_error(mock_requests): mock_requests.post.side_effect = requests.ConnectionError expected = "Could not connect to the Nexus instance to execute the script js_cleanup" with pytest.raises(NexusScriptError, match=expected): nexus.execute_script("js_cleanup", { "repository_name": "cachito-npm-1", "username": "******" }) mock_requests.post.assert_called_once()
def test_execute_script(mock_requests): mock_requests.post.return_value.ok = True nexus.execute_script("js_cleanup", { "repository_name": "cachito-npm-1", "username": "******" }) mock_requests.post.assert_called_once() assert mock_requests.post.call_args[0][0].endswith( "/service/rest/v1/script/js_cleanup/run")
def test_execute_script_failed(mock_requests): mock_requests.post.return_value.ok = False mock_requests.post.return_value.text = "some error" expected = "The Nexus script js_cleanup failed with: some error" with pytest.raises(NexusScriptError, match=expected): nexus.execute_script("js_cleanup", { "repository_name": "cachito-npm-1", "username": "******" }) mock_requests.post.assert_called_once()
def prepare_nexus_for_js_request(repo_name): """ Prepare Nexus so that Cachito can stage JavaScript content. :param str repo_name: the name of the repository for the request for this package manager :raise CachitoError: if the script execution fails """ config = get_worker_config() # Note that the http_username and http_password represent the unprivileged user that # the new Nexus npm proxy repository will use to connect to the "cachito-js" Nexus group # repository payload = { "repository_name": repo_name, "http_password": config.cachito_nexus_proxy_password, "http_username": config.cachito_nexus_proxy_username, "npm_proxy_url": config.cachito_nexus_npm_proxy_url, } script_name = "js_before_content_staged" try: nexus.execute_script(script_name, payload) except NexusScriptError: log.exception(f"Failed to execute the script {script_name}") raise CachitoError("Failed to prepare Nexus for Cachito to stage JavaScript content")