Skip to content

Commit

Permalink
Validate workDir and userName parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
jordeu committed Dec 15, 2021
1 parent bbfe9d2 commit 05382e6
Showing 1 changed file with 37 additions and 8 deletions.
45 changes: 37 additions & 8 deletions src/main/java/io/seqera/tower/agent/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@
import java.lang.module.ModuleDescriptor;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -72,9 +75,11 @@ public class Agent implements Runnable {
@Option(names = {"-u", "--url"}, description = "Tower server API endpoint URL. If not provided TOWER_API_ENDPOINT variable will be used [default: https://api.tower.nf].", defaultValue = "${TOWER_API_ENDPOINT:-https://api.tower.nf}", required = true)
String url;

@Option(names = {"-w", "--work-dir"}, description = "Default path where the pipeline scratch data is stored. It can be changed when launching a pipeline from Tower [default: $HOME/work].", defaultValue = "${HOME}/work")
@Option(names = {"-w", "--work-dir"}, description = "Default path where the pipeline scratch data is stored. It can be changed when launching a pipeline from Tower [default: ~/work].")
Path workDir;

private String validatedWorkDir;
private String validatedUserName;
private final ApplicationContext ctx;
private AgentClientSocket agentClient;

Expand All @@ -89,6 +94,7 @@ public static void main(String[] args) throws Exception {
@Override
public void run() {
try {
validateParameters();
checkTower();
connectTower();
sendPeriodicHeartbeat();
Expand Down Expand Up @@ -182,17 +188,40 @@ private void sendPeriodicHeartbeat() {
}

private void sendInfoMessage() throws IOException {
String userName = new UnixSystem().getUsername();
String workDir = this.workDir.toAbsolutePath().normalize().toString();
String agentVersion = getVersion();

agentClient.send(new InfoMessage(
userName,
workDir,
agentVersion
validatedUserName,
validatedWorkDir,
getVersion()
));
}

private void validateParameters() {
// Fetch username
validatedUserName = System.getProperty("user.name");
if (validatedUserName == null || validatedUserName.isEmpty() || validatedUserName.isBlank()) {
logger.error("Impossible to detect current Unix username.");
System.exit(-1);
}

// Set default workDir
if (workDir == null) {
String defaultPath = System.getProperty("user.home") + "/work";
try {
workDir = Paths.get(defaultPath);
} catch (InvalidPathException e) {
logger.error("Impossible to define a default work directory. Please provide one using '--work-dir'.");
System.exit(-1);
}
}

// Validate workDir exists
if (!Files.exists(workDir)) {
logger.error("The work directory '{}' do not exists. Create it or provide a different one using '--work-dir'.", workDir);
System.exit(-1);
}
validatedWorkDir = workDir.toAbsolutePath().normalize().toString();
}

/**
* Do some health checks to the Tower API endpoint to verify that it is available and
* compatible with this Agent.
Expand Down

0 comments on commit 05382e6

Please sign in to comment.