Skip to content

Commit

Permalink
Send info message
Browse files Browse the repository at this point in the history
  • Loading branch information
jordeu committed Nov 23, 2021
1 parent c89e94d commit 05a5230
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 2 deletions.
38 changes: 37 additions & 1 deletion src/main/java/io/seqera/tower/agent/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

package io.seqera.tower.agent;

import com.sun.security.auth.module.UnixSystem;
import io.micronaut.configuration.picocli.PicocliRunner;
import io.micronaut.context.ApplicationContext;
import io.micronaut.http.HttpRequest;
Expand All @@ -22,6 +23,7 @@
import io.seqera.tower.agent.exchange.CommandRequest;
import io.seqera.tower.agent.exchange.CommandResponse;
import io.seqera.tower.agent.exchange.HeartbeatMessage;
import io.seqera.tower.agent.exchange.InfoMessage;
import io.seqera.tower.agent.model.ServiceInfoResponse;
import io.seqera.tower.agent.utils.VersionProvider;
import org.slf4j.Logger;
Expand All @@ -36,6 +38,7 @@
import java.lang.module.ModuleDescriptor;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -117,6 +120,7 @@ private void connectTower() {
.blockingFirst();
agentClient.setConnectCallback(this::connectTower);
agentClient.setCommandRequestCallback(this::execCommand);
sendInfoMessage();
} catch (URISyntaxException e) {
logger.error("Invalid URI: {}/agent/{}/connect - {}", url, agentKey, e.getMessage());
} catch (WebSocketClientException e) {
Expand Down Expand Up @@ -180,6 +184,18 @@ private void sendPeriodicHeartbeat() {
});
}

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

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

/**
* Do some health checks to the Tower API endpoint to verify that it is available and
* compatible with this Agent.
Expand Down Expand Up @@ -226,8 +242,28 @@ private void checkTower() {
* @throws IOException On reading properties file
*/
private String getVersionApi() throws IOException {
return getProperties().get("versionApi").toString();
}

/**
* Current Agent version
*
* @return Agent version
* @throws IOException On reading properties file
*/
private String getVersion() throws IOException {
return getProperties().get("version").toString();
}

/**
* Load 'build-info.properties'
*
* @return Build properties
* @throws IOException On reading properties file
*/
private Properties getProperties() throws IOException {
Properties properties = new Properties();
properties.load(this.getClass().getResourceAsStream("/META-INF/build-info.properties"));
return properties.get("versionApi").toString();
return properties;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
@JsonSubTypes({
@JsonSubTypes.Type(value = CommandRequest.class, name = "command-request"),
@JsonSubTypes.Type(value = CommandResponse.class, name = "command-response"),
@JsonSubTypes.Type(value = HeartbeatMessage.class, name = "heartbeat")
@JsonSubTypes.Type(value = HeartbeatMessage.class, name = "heartbeat"),
@JsonSubTypes.Type(value = InfoMessage.class, name = "info")
})
public abstract class AgentMessage implements Serializable {
}
55 changes: 55 additions & 0 deletions src/main/java/io/seqera/tower/agent/exchange/InfoMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2021, Seqera Labs.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This Source Code Form is "Incompatible With Secondary Licenses", as
* defined by the Mozilla Public License, v. 2.0.
*/

package io.seqera.tower.agent.exchange;

import io.micronaut.core.annotation.ReflectiveAccess;

/**
* @author Jordi Deu-Pons <jordi@seqera.io>
*/
@ReflectiveAccess
public class InfoMessage extends AgentMessage {

private String userName;
private String workDir;
private String agentVersion;

public InfoMessage() {
}

public InfoMessage(String userName, String workDir, String agentVersion) {
this.userName = userName;
this.workDir = workDir;
this.agentVersion = agentVersion;
}

public String getUserName() {
return userName;
}

public String getWorkDir() {
return workDir;
}

public String getAgentVersion() {
return agentVersion;
}

@Override
public String toString() {
return "InfoMessage[" +
"userName='" + userName + '\'' +
"; workDir='" + workDir + '\'' +
"; agentVersion='" + agentVersion + '\'' +
']';
}
}

0 comments on commit 05a5230

Please sign in to comment.