-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
EntrypointEnvChecker.java
79 lines (69 loc) · 2.43 KB
/
EntrypointEnvChecker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* Copyright (c) 2022 Airbyte, Inc., all rights reserved.
*/
package io.airbyte.workers.helper;
import io.airbyte.workers.exception.WorkerException;
import io.airbyte.workers.process.ProcessFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Collections;
/**
* Should only be used by connector testing.
*/
public class EntrypointEnvChecker {
/**
* @param processFactory any process factory
* @param jobId used as input to processFactory.create
* @param jobAttempt used as input to processFactory.create
* @param jobRoot used as input to processFactory.create
* @param imageName used as input to processFactory.create
* @return the entrypoint in the env variable AIRBYTE_ENTRYPOINT
* @throws RuntimeException if there is ambiguous output from the container
*/
@SuppressWarnings("PMD.AvoidLiteralsInIfCondition")
public static String getEntrypointEnvVariable(final ProcessFactory processFactory,
final String jobId,
final int jobAttempt,
final Path jobRoot,
final String imageName)
throws IOException, InterruptedException, WorkerException {
final Process process = processFactory.create(
"entrypoint-checker",
jobId,
jobAttempt,
jobRoot,
imageName,
false,
false,
Collections.emptyMap(),
"printenv",
null,
null,
Collections.emptyMap(),
Collections.emptyMap(),
Collections.emptyMap());
final BufferedReader stdout = new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8));
String outputLine = null;
String line = stdout.readLine();
while ((line != null) && outputLine == null) {
if (line.contains("AIRBYTE_ENTRYPOINT")) {
outputLine = line;
}
line = stdout.readLine();
}
process.waitFor();
if (outputLine != null) {
final String[] splits = outputLine.split("=", 2);
if (splits.length != 2) {
throw new RuntimeException("String could not be split into multiple segments: " + outputLine);
} else {
return splits[1].strip();
}
} else {
return null;
}
}
}