Skip to content

Commit

Permalink
nimrodg-api: Use java.nio.Path objects to represent agent paths inste…
Browse files Browse the repository at this point in the history
…ad of Strings

* Allows use of an in-memory filesystem when testing.
* Everything is still stored as strings in the database.
  • Loading branch information
vs49688 committed Jul 23, 2020
1 parent 82918fd commit 469ae21
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,29 @@
*/
package au.edu.uq.rcc.nimrodg.api;

import java.nio.file.Path;
import java.util.Set;

public interface AgentInfo {

String getPlatformString();

String getPath();
/**
* TODO: Will be removed next major version bump.
* Replaced with {@link AgentInfo#getAgentPath()}.
*/
@Deprecated
default String getPath() {
return this.getAgentPath().toString();
}

/**
* TODO: Temporary replacement for {@link AgentInfo#getPath()}.
* Will be replaced with {@code Path AgentInfo#getPath()} next major
* version bump.
*/
@Deprecated
Path getAgentPath();

Set<MachinePair> posixMappings();
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public final class IniSetupConfig {

Expand Down Expand Up @@ -82,7 +84,8 @@ public static SetupConfigBuilder parseToBuilder(Ini ini, Optional<Path> userConf
.build()
);

builder.agents(IniUserConfig.requireSection(ini, "agents"));
builder.agents(IniUserConfig.requireSection(ini, "agents").entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, e -> Paths.get(e.getValue()))));

Pattern p = Pattern.compile("^([^,]+),([^,]+)$");
Section _agentmap = IniUserConfig.requireSection(ini, "agentmap");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public int execute(Namespace args, UserConfig config, PrintStream out, PrintStre
}
case "addagent": {
try(NimrodSetupAPI api = fact.getSetupAPI(config)) {
api.addAgent(args.getString("platform_string"), args.getString("path"));
api.addAgent(args.getString("platform_string"), Paths.get(args.getString("path")));
}
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import au.edu.uq.rcc.nimrodg.api.AgentInfo;
import au.edu.uq.rcc.nimrodg.api.MachinePair;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;

public class TempAgentInfo {
Expand Down Expand Up @@ -56,8 +58,8 @@ public String getPlatformString() {
}

@Override
public String getPath() {
return path;
public Path getAgentPath() {
return Paths.get(path);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
Expand Down Expand Up @@ -239,14 +240,14 @@ public synchronized boolean deleteResourceType(String name) throws SetupExceptio
}

@Override
public synchronized boolean addAgent(String platformString, String path) throws SetupException {
public synchronized boolean addAgent(String platformString, Path path) throws SetupException {
if(platformString == null || path == null) {
throw new IllegalArgumentException();
}

try {
qAddAgent.setString(1, platformString);
qAddAgent.setString(2, path);
qAddAgent.setString(2, path.toString());
return qAddAgent.executeUpdate() == 1;
} catch(SQLException e) {
throw new SetupException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
Expand Down Expand Up @@ -173,11 +174,11 @@ private void _setupT(SetupConfig cfg) throws SQLException {

{
/* Agents */
Map<String, String> agents = cfg.agents();
Map<String, Path> agents = cfg.agents();
try(PreparedStatement ps = prepareAddAgent()) {
for(Map.Entry<String, String> a : agents.entrySet()) {
for(Map.Entry<String, Path> a : agents.entrySet()) {
ps.setString(1, a.getKey());
ps.setString(2, a.getValue());
ps.setString(2, a.getValue().toString());
ps.addBatch();
}
ps.executeBatch();
Expand Down Expand Up @@ -313,11 +314,11 @@ public synchronized boolean deleteResourceType(String name) throws SetupExceptio
}

@Override
public synchronized boolean addAgent(String platformString, String path) throws SetupException {
public synchronized boolean addAgent(String platformString, Path path) throws SetupException {
return this.runSQL(() -> {
try(PreparedStatement ps = prepareAddAgent()) {
ps.setString(1, platformString);
ps.setString(2, path);
ps.setString(2, path.toString());
return ps.executeUpdate() == 1;
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import au.edu.uq.rcc.nimrodg.api.NimrodException;
import au.edu.uq.rcc.nimrodg.api.ResourceType;

import java.nio.file.Path;

public interface NimrodSetupAPI extends AutoCloseable {

class SetupException extends NimrodException {
Expand Down Expand Up @@ -72,7 +74,7 @@ default boolean addResourceType(String name, Class<? extends ResourceType> clazz

boolean deleteResourceType(String name) throws SetupException;

boolean addAgent(String platformString, String path) throws SetupException;
boolean addAgent(String platformString, Path path) throws SetupException;

boolean deleteAgent(String platformString) throws SetupException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import au.edu.uq.rcc.nimrodg.api.MachinePair;

import java.nio.file.Path;
import java.util.Map;

public final class SetupConfig {
Expand All @@ -29,12 +30,12 @@ public final class SetupConfig {
private final String storeDir;
private final AMQPConfig amqp;
private final TransferConfig tx;
private final Map<String, String> agents;
private final Map<String, Path> agents;
private final Map<MachinePair, String> agentMappings;
private final Map<String, String> resourceTypes;
private final Map<String, String> properties;

SetupConfig(String workDir, String storeDir, AMQPConfig amqp, TransferConfig tx, Map<String, String> agents, Map<MachinePair, String> agentMappings, Map<String, String> resourceTypes, Map<String, String> properties) {
SetupConfig(String workDir, String storeDir, AMQPConfig amqp, TransferConfig tx, Map<String, Path> agents, Map<MachinePair, String> agentMappings, Map<String, String> resourceTypes, Map<String, String> properties) {
this.workDir = workDir;
this.storeDir = storeDir;
this.amqp = amqp;
Expand All @@ -61,7 +62,7 @@ public TransferConfig transfer() {
return this.tx;
}

public Map<String, String> agents() {
public Map<String, Path> agents() {
return this.agents;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import au.edu.uq.rcc.nimrodg.api.MachinePair;
import au.edu.uq.rcc.nimrodg.api.ResourceType;

import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -31,7 +32,7 @@ public class SetupConfigBuilder {
private String storeDir;
private AMQPConfig amqp;
private TransferConfig transfer;
private Map<String, String> agents;
private Map<String, Path> agents;
private Map<MachinePair, String> agentMappings;
private Map<String, String> resourceTypes;
private Map<String, String> properties;
Expand Down Expand Up @@ -63,7 +64,7 @@ public SetupConfigBuilder transfer(TransferConfig tx) {
return this;
}

public SetupConfigBuilder agent(String platform, String path) {
public SetupConfigBuilder agent(String platform, Path path) {
if(platform == null) {
return this;
}
Expand All @@ -77,7 +78,7 @@ public SetupConfigBuilder agent(String platform, String path) {
return this;
}

public SetupConfigBuilder agents(Map<String, String> agents) {
public SetupConfigBuilder agents(Map<String, Path> agents) {
if(agents == null) {
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,9 @@ private LocalAgent[] buildAgentInfo(UUID[] uuids) throws IOException {
Optional<Path> outputPath = Optional.empty();

ProcessBuilder pb = new ProcessBuilder(List.of(
agentInfo.getPath(), "--config", configPath.toString()
agentInfo.getAgentPath().toString(), "--config", configPath.toString()
));

if(captureMode == CaptureMode.OFF) {
pb.redirectOutput(ProcessBuilder.Redirect.DISCARD);
} else if(captureMode == CaptureMode.INHERIT) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protected POSIXActuator(Operations ops, Resource node, NimrodURI amqpUri, Certif
CleanupStage cs = CleanupStage.Client;
try(RemoteShell client = makeClient()) {
try {
Path agentPath = Paths.get(config.agentInfo.getPath());
Path agentPath = config.agentInfo.getAgentPath();

remoteEnvironment = client.getEnvironment();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public void before() throws IOException, OperatorCreationException, CertificateE

/* Create placeholder files for the agents. */
for(AgentInfo ai : agentProvider.lookupAgents().values()) {
Path p = memFs.getPath(ai.getPath());
Path p = ai.getAgentPath();
Files.createDirectories(p.getParent());
Files.write(p, new byte[0]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ public static SetupConfig getTestSetupConfig(Path root) {


ap.lookupAgents().forEach((p, ai) -> {
b.agent(p, ai.getPath());
b.agent(p, ai.getAgentPath());
ai.posixMappings().forEach(e -> b.agentMapping(e, p));
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public String getPlatformString() {
}

@Override
public String getPath() {
return path.resolve(".agents").resolve("x86_64-pc-linux-musl").toString();
public Path getAgentPath() {
return path.resolve(".agents").resolve("x86_64-pc-linux-musl");
}

@Override
Expand All @@ -44,8 +44,8 @@ public String getPlatformString() {
}

@Override
public String getPath() {
return path.resolve(".agents").resolve("i686-pc-linux-musl").toString();
public Path getAgentPath() {
return path.resolve(".agents").resolve("i686-pc-linux-musl");
}

@Override
Expand All @@ -60,8 +60,8 @@ public String getPlatformString() {
}

@Override
public String getPath() {
return "/bin/true";
public Path getAgentPath() {
return path.resolve("bin").resolve("true");
}

@Override
Expand Down

0 comments on commit 469ae21

Please sign in to comment.