Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ dependencies {
// Note that the java SDK modules use syslog4j, so we'll need to figure something out
// there. I doubt any of the apps actually use the logging code that triggers it though
testImplementation('org.syslog4j:syslog4j:0.9.46')
testImplementation('uk.org.webcompere:system-stubs-jupiter:2.1.8')

// isolate the sdk generated code dependencies from the standard dependencies

Expand Down
8 changes: 1 addition & 7 deletions src/main/java/us/kbase/sdk/tester/ConfigLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public class ConfigLoader {
private final String authAllowInsecure;
private final AuthToken token;
private final String endPoint;
private final String jobSrvUrl;
private final String wsUrl;
private final String shockUrl;
private final String handleUrl;
Expand Down Expand Up @@ -61,11 +60,11 @@ public ConfigLoader(Properties props, boolean testMode, String configPathInfo
throw new IllegalStateException("Error: KBase services end-point is not set in " +
configPathInfo);
}
jobSrvUrl = getConfigUrl(props, "job_service_url", endPoint, "userandjobstate");
wsUrl = getConfigUrl(props, "workspace_url", endPoint, "ws");
shockUrl = getConfigUrl(props, "shock_url", endPoint, "shock-api");
handleUrl = getConfigUrl(props, "handle_url", endPoint, "handle_service");
srvWizUrl = getConfigUrl(props, "srv_wiz_url", endPoint, "service_wizard");
// not sure if this is still used for the ee2 url or not
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yikes. I hope not!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But, sure, leave it in for now. Do we need to do some kind of audit to find out?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's harmful to leave it for now. I'm not planning to do anything unless it becomes an issue

njswUrl = getConfigUrl(props, "njsw_url", endPoint, "njs_wrapper");
catalogUrl = getConfigUrl(props, "catalog_url", endPoint, "catalog");
secureCfgParams = new TreeMap<String, String>();
Expand Down Expand Up @@ -102,10 +101,6 @@ public String getHandleUrl() {
return handleUrl;
}

public String getJobSrvUrl() {
return jobSrvUrl;
}

public String getNjswUrl() {
return njswUrl;
}
Expand All @@ -127,7 +122,6 @@ public void generateConfigProperties(File configPropsFile) throws Exception {
try {
pw.println("[global]");
pw.println("kbase_endpoint = " + endPoint);
pw.println("job_service_url = " + jobSrvUrl);
pw.println("workspace_url = " + wsUrl);
pw.println("shock_url = " + shockUrl);
pw.println("handle_url = " + handleUrl);
Expand Down
120 changes: 120 additions & 0 deletions src/main/java/us/kbase/sdk/util/DeployConfigGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package us.kbase.sdk.util;

import static java.util.Objects.requireNonNull;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import org.ini4j.Ini;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/** Generates a deploy.cfg file for a KBase service or app */
public class DeployConfigGenerator {

private static final Pattern MUSTACHE_ENTRY = Pattern.compile(
"\\{\\{\\s*([a-zA-Z0-9_]+)\\s*\\}\\}"
);

/** Generate a KBase service / app deploy.cfg file given a Mustache template and a properties
* ini file.
* The template file will be backed up and then overwritten in place.
* @param templatePath - the path to the template file.
* @param propertiesPath - the path to the properties / .ini file containing the properties
* to be inserted into the mustache template. The properties must be in a section called
* "global". If not provided or the file doesn't exist, a KBASE_ENDPOINT environment variable
* must exist with the kbase endpoint url as the value, which will be used to build the
* properties.
* @throws IOException if file reads or writes fail.
*/
public static void generateDeployConfig(final Path templatePath, final Path propertiesPath)
throws IOException {
requireNonNull(templatePath, "templatePath");

final String templateText = Files.readString(templatePath, StandardCharsets.UTF_8);

// this is recapitulating the behavior or the original prepare_deploy_cfg.py code
// for now. We might want to be more stringent about the input vs. silently accepting
// whatever.
Comment on lines +43 to +44
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

final Map<String, String> props;
if (propertiesPath != null && Files.exists(propertiesPath)) {
props = new HashMap<>();
final Ini ini = new Ini(propertiesPath.toFile());
if (ini.get("global") != null) {
ini.get("global").forEach(props::put);
}
} else if (System.getenv("KBASE_ENDPOINT") != null) {
props = loadFromEnv();
} else {
if (propertiesPath == null) {
throw new IllegalArgumentException(
"Properties file was not provided and KBASE_ENDPOINT environment "
+ "variable not found"
);
} else {
throw new IllegalArgumentException(String.format(
"Neither %s file nor KBASE_ENDPOINT environment variable found",
propertiesPath
));
}
}
final String output = renderTemplate(templateText, props);

// Create human-readable backup filename
final String timestamp = LocalDateTime.now()
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH-mm-ss-SSS"));
final Path backupPath = templatePath.resolveSibling(
templatePath.getFileName() + ".bak." + timestamp
);

Files.writeString(backupPath, templateText, StandardCharsets.UTF_8);
Files.writeString(templatePath, output, StandardCharsets.UTF_8);
}

private static Map<String, String> loadFromEnv() {
final Map<String, String> props = new HashMap<>();
final String kbaseEndpoint = System.getenv("KBASE_ENDPOINT");

props.put("kbase_endpoint", kbaseEndpoint);
props.put("workspace_url", kbaseEndpoint + "/ws");
props.put("shock_url", kbaseEndpoint + "/shock-api");
props.put("handle_url", kbaseEndpoint + "/handle_service");
props.put("srv_wiz_url", kbaseEndpoint + "/service_wizard");
props.put("njsw_url", kbaseEndpoint + "/njs_wrapper");

final String authServiceUrl = System.getenv("AUTH_SERVICE_URL");
if (authServiceUrl != null) {
props.put("auth_service_url", authServiceUrl);
}

final String insecure = System.getenv("AUTH_SERVICE_URL_ALLOW_INSECURE");
props.put("auth_service_url_allow_insecure", insecure == null ? "false" : insecure);

System.getenv().forEach((key, value) -> {
if (key.startsWith("KBASE_SECURE_CONFIG_PARAM_")) {
String paramName = key.substring("KBASE_SECURE_CONFIG_PARAM_".length());
props.put(paramName, value);
}
});

return props;
}

private static String renderTemplate(final String template, final Map<String, String> props) {
final Matcher matcher = MUSTACHE_ENTRY.matcher(template);
final StringBuffer sb = new StringBuffer();
while (matcher.find()) {
final String key = matcher.group(1);
final String value = props.getOrDefault(key, "");
matcher.appendReplacement(sb, Matcher.quoteReplacement(value));
}
matcher.appendTail(sb);
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[${module_name}]
kbase-endpoint = {{ kbase_endpoint }}
job-service-url = {{ job_service_url }}
workspace-url = {{ workspace_url }}
shock-url = {{ shock_url }}
handle-service-url = {{ handle_url }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ kbase_endpoint=https://appdev.kbase.us/services

# Next set of URLs correspond to core services. By default they
# are defined automatically based on 'kbase_endpoint':
#job_service_url=
#workspace_url=
#shock_url=
#handle_url=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public class TestLocalManagerTest {

# Next set of URLs correspond to core services. By default they
# are defined automatically based on 'kbase_endpoint':
#job_service_url=
#workspace_url=
#shock_url=
#handle_url=
Expand Down
Loading
Loading