Skip to content

Commit

Permalink
[HWORKS-1130] [HWORKS-1130] [HWORKS-1130] Use base image to do conda …
Browse files Browse the repository at this point in the history
…search instead of using host 's anaconda installation which has been removed (#1523)

* [HWORKS-1130] Use base image to do conda search instead of using host's anaconda installation which has been removed

* [HWORKS-1130] User home as environment variable
  • Loading branch information
kouzant authored Apr 24, 2024
1 parent 0bb019f commit e21f878
Showing 1 changed file with 32 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.hops.hopsworks.common.python.library;

import com.lambdista.util.Try;
import com.logicalclocks.servicediscoverclient.exceptions.ServiceDiscoveryException;
import io.hops.hopsworks.common.dao.python.LibraryFacade;
import io.hops.hopsworks.common.provenance.core.opensearch.BasicOpenSearchHit;
import io.hops.hopsworks.common.provenance.core.opensearch.OpenSearchHelper;
Expand All @@ -27,6 +28,7 @@
import io.hops.hopsworks.common.util.OSProcessExecutor;
import io.hops.hopsworks.common.util.ProcessDescriptor;
import io.hops.hopsworks.common.util.ProcessResult;
import io.hops.hopsworks.common.util.ProjectUtils;
import io.hops.hopsworks.common.util.Settings;
import io.hops.hopsworks.exceptions.OpenSearchException;
import io.hops.hopsworks.exceptions.GenericException;
Expand Down Expand Up @@ -54,6 +56,8 @@
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand Down Expand Up @@ -85,6 +89,8 @@ public class LibraryController {
private OpenSearchClientController provOpenSearchController;
@EJB
private PyPiLibraryOpenSearchIndexer pypiIndexer;
@EJB
private ProjectUtils projectUtils;

public PythonDep getPythonDep(String dependency, Project project) throws PythonException {
return libraryFacade.findByDependencyAndProject(dependency, project)
Expand Down Expand Up @@ -115,8 +121,7 @@ public void uninstallLibrary(Project proj, Users user, CondaInstallType installT

public HashMap<String, List<LibraryVersionDTO>> condaSearch(String library, String url) throws ServiceException {
HashMap<String, List<LibraryVersionDTO>> libVersions = new HashMap<>();
String prog = settings.getHopsworksDomainDir() + "/bin/condasearch.sh";
String[] lines = condaList(prog, library, url);
String[] lines = condaList(library, url);
String[] libVersion;
String foundLib = "";
String foundVersion;
Expand Down Expand Up @@ -236,41 +241,53 @@ public HashMap<String, List<LibraryVersionDTO>> pipSearch(String query)
return versions;
}

private String[] condaList(String program, String library, String url)
throws ServiceException {
private String[] condaList(String library, String url) throws ServiceException {
Path condaSearchScript = Paths.get(settings.getSudoersDir(), "condaSearch.sh");
String userHome = System.getProperty("user.home", "/home/glassfish");

ProcessDescriptor.Builder pdBuilder = new ProcessDescriptor.Builder();
pdBuilder.addCommand(program);
pdBuilder.addEnvironmentVariable("HW_USER_HOME", userHome);
pdBuilder.addCommand("/usr/bin/sudo");
pdBuilder.addCommand(condaSearchScript.toString());
if (url != null && !url.isEmpty()) {
pdBuilder.addCommand(url);
pdBuilder.addCommand(library);
} else if(library != null && !library.isEmpty()) {
pdBuilder.addCommand(library);
}
ProcessResult processResult;
ProcessDescriptor processDescriptor = pdBuilder.redirectErrorStream(true)
.setWaitTimeout(5L, TimeUnit.MINUTES)
.build();


try {
processResult = osProcessExecutor.execute(processDescriptor);
String imageName = projectUtils.getFullDockerImageName(settings.getBaseDockerImagePythonName());
pdBuilder.addCommand(imageName);

ProcessDescriptor processDescriptor = pdBuilder.redirectErrorStream(true)
.setWaitTimeout(5L, TimeUnit.MINUTES)
.build();

ProcessResult processResult = osProcessExecutor.execute(processDescriptor);
boolean exited = processResult.processExited();
int errCode = processResult.getExitCode();
if (!exited) {
throw new ServiceException(RESTCodes.ServiceErrorCode.ANACONDA_LIST_LIB_ERROR, Level.WARNING,
"errCode: " + errCode + ", " + processResult.getStderr());
"errCode: " + errCode + ", " + processResult.getStderr());
}
if (errCode == 1 || errCode == 23) { // 1 for conda, 23 for pip
throw new ServiceException(RESTCodes.ServiceErrorCode.ANACONDA_LIST_LIB_NOT_FOUND, Level.FINE,
"errCode: " + errCode + ", " + processResult.getStderr());
"errCode: " + errCode + ", " + processResult.getStderr());
} else if (errCode != 0) {
throw new ServiceException(RESTCodes.ServiceErrorCode.ANACONDA_LIST_LIB_ERROR, Level.WARNING,
"errCode: " + errCode + ", " + processResult.getStderr());
"errCode: " + errCode + ", " + processResult.getStderr());
}
String result = processResult.getStdout();
return (result != null && !result.isEmpty())? result.split("\n") : new String[0];
} catch (ServiceDiscoveryException ex) {
throw new ServiceException(RESTCodes.ServiceErrorCode.ANACONDA_LIST_LIB_ERROR, Level.SEVERE,
"Could not construct Docker registry URL", ex.getMessage(), ex);
} catch (IOException ex) {
throw new ServiceException(RESTCodes.ServiceErrorCode.ANACONDA_LIST_LIB_ERROR, Level.WARNING,
"lib: " + library, ex.getMessage(), ex);
}
String result = processResult.getStdout();
return (result != null && !result.isEmpty())? result.split("\n") : new String[0];
}

/**
Expand Down

0 comments on commit e21f878

Please sign in to comment.