use of com.baidu.hugegraph.computer.driver.ComputerDriverException in project hugegraph-computer by hugegraph.
the class KubernetesDriverTest method testUploadAlgorithmJarWithError.
@Test
public void testUploadAlgorithmJarWithError() throws FileNotFoundException {
Whitebox.setInternalState(this.driver, "bashPath", "conf/images/upload_test-x.sh");
InputStream inputStream = new FileInputStream("conf/images/test.jar");
Assert.assertThrows(ComputerDriverException.class, () -> {
this.driver.uploadAlgorithmJar("PageRank", inputStream);
}, e -> {
ComputerDriverException exception = (ComputerDriverException) e;
Assert.assertContains("No such file", exception.rootCause().getMessage());
});
}
use of com.baidu.hugegraph.computer.driver.ComputerDriverException in project hugegraph-computer by hugegraph.
the class KubernetesDriver method createKubeClient.
private static NamespacedKubernetesClient createKubeClient(HugeConfig conf) {
String kubeConfig = conf.get(KubeDriverOptions.KUBE_CONFIG);
Config config;
try {
File file = new File(kubeConfig);
@SuppressWarnings("deprecation") String kubeConfigContents = FileUtils.readFileToString(file);
config = Config.fromKubeconfig(kubeConfigContents);
} catch (IOException e) {
throw new ComputerDriverException("Failed to read KubeConfig: %s", e, kubeConfig);
}
return new DefaultKubernetesClient(config);
}
use of com.baidu.hugegraph.computer.driver.ComputerDriverException in project hugegraph-computer by hugegraph.
the class KubernetesDriver method defaultSpec.
private Map<String, Object> defaultSpec() {
Map<String, Object> defaultSpec = new HashMap<>();
Collection<TypedOption<?, ?>> options = KubeSpecOptions.instance().options().values();
for (TypedOption<?, ?> typeOption : options) {
Object value = this.conf.get(typeOption);
if (value != null) {
String specKey = KubeUtil.covertSpecKey(typeOption.name());
if (KubeSpecOptions.MAP_TYPE_CONFIGS.contains(typeOption)) {
if (!Objects.equals(String.valueOf(value), "[]")) {
value = this.conf.getMap((ConfigListOption<String>) typeOption);
defaultSpec.put(specKey, value);
}
} else {
defaultSpec.put(specKey, value);
}
}
}
ComputerJobSpec spec = HugeGraphComputerJob.mapToSpec(defaultSpec);
// Add pullSecrets
List<String> secretNames = this.conf.get(KubeDriverOptions.PULL_SECRET_NAMES);
if (CollectionUtils.isNotEmpty(secretNames)) {
List<LocalObjectReference> secrets = new ArrayList<>();
for (String name : secretNames) {
if (StringUtils.isBlank(name)) {
continue;
}
secrets.add(new LocalObjectReference(name));
}
if (CollectionUtils.isNotEmpty(secrets)) {
spec.withPullSecrets(secrets);
}
}
// Add log4j.xml
String log4jXmlPath = this.conf.get(KubeDriverOptions.LOG4J_XML_PATH);
if (StringUtils.isNotBlank(log4jXmlPath)) {
try {
File file = new File(log4jXmlPath);
@SuppressWarnings("deprecation") String log4jXml = FileUtils.readFileToString(file);
spec.withLog4jXml(log4jXml);
} catch (IOException exception) {
throw new ComputerDriverException("Failed to read log4j file for computer job", exception);
}
}
Map<String, Object> specMap = HugeGraphComputerJob.specToMap(spec);
return Collections.unmodifiableMap(specMap);
}
use of com.baidu.hugegraph.computer.driver.ComputerDriverException in project hugegraph-computer by hugegraph.
the class KubernetesDriver method uploadAlgorithmJar.
@Override
public void uploadAlgorithmJar(String algorithmName, InputStream input) {
File tempFile = null;
try {
Path path = Files.createDirectories(Paths.get(TMP_DIR, UUID.randomUUID().toString()));
tempFile = File.createTempFile("userAlgorithm", ".jar", path.toFile());
FileUtils.copyInputStreamToFile(input, tempFile);
InputStream bashStream;
if (StringUtils.isBlank(this.bashPath)) {
bashStream = this.getClass().getResourceAsStream(DEFAULT_PUSH_BASH_PATH);
} else {
bashStream = new FileInputStream(this.bashPath);
}
String bashAsStr = IOHelpers.readFully(bashStream);
StringBuilder builder = new StringBuilder();
builder.append(BUILD_IMAGE_FUNC);
if (StringUtils.isNotBlank(this.registry)) {
builder.append(" -r ").append(this.registry);
}
if (StringUtils.isNotBlank(this.username)) {
builder.append(" -u ").append(this.username);
}
if (StringUtils.isNotBlank(this.password)) {
builder.append(" -p ").append(this.password);
}
builder.append(" -s ").append(tempFile.getAbsolutePath());
String jarFile = this.buildJarFile(this.jarFileDir, algorithmName);
builder.append(" -j ").append(jarFile);
String imageUrl = this.buildImageUrl(algorithmName);
builder.append(" -i ").append(imageUrl);
builder.append(" -f ").append(this.frameworkImageUrl);
String args = builder.toString();
String[] command = { "bash", "-c", bashAsStr + "\n" + args };
Process process = Runtime.getRuntime().exec(command);
int code = process.waitFor();
if (code != 0) {
InputStream errorStream = process.getErrorStream();
String errorInfo = IOHelpers.readFully(errorStream);
if (StringUtils.isBlank(errorInfo)) {
InputStream stdoutStream = process.getInputStream();
errorInfo = IOHelpers.readFully(stdoutStream);
}
throw new ComputerDriverException(errorInfo);
}
} catch (Throwable exception) {
throw new ComputerDriverException("Failed to upload algorithm Jar", exception);
} finally {
FileUtils.deleteQuietly(tempFile);
}
}
Aggregations