Skip to content

Commit

Permalink
Merge pull request #401 from FlowCI/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
gy2006 committed May 6, 2021
2 parents e1852ea + c165980 commit 462e88d
Show file tree
Hide file tree
Showing 60 changed files with 1,372 additions and 579 deletions.
Expand Up @@ -18,7 +18,7 @@

import com.flowci.core.agent.domain.Agent;
import com.flowci.core.agent.domain.AgentAction;
import com.flowci.core.agent.domain.CreateOrUpdateAgent;
import com.flowci.core.agent.domain.AgentOption;
import com.flowci.core.agent.domain.DeleteAgent;
import com.flowci.core.agent.service.AgentService;
import com.flowci.core.auth.annotation.Action;
Expand All @@ -28,7 +28,6 @@
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
* @author yang
Expand All @@ -55,12 +54,12 @@ public List<Agent> list() {

@PostMapping()
@Action(AgentAction.CREATE_UPDATE)
public Agent createOrUpdate(@Validated @RequestBody CreateOrUpdateAgent body) {
public Agent createOrUpdate(@Validated @RequestBody AgentOption body) {
if (body.hasToken()) {
return agentService.update(body.getToken(), body.getName(), body.getTags());
return agentService.update(body);
}

return agentService.create(body.getName(), body.getTags(), Optional.empty());
return agentService.create(body);
}

@DeleteMapping()
Expand Down
Expand Up @@ -18,7 +18,7 @@

import com.flowci.core.agent.domain.AgentHost;
import com.flowci.core.agent.domain.AgentHostAction;
import com.flowci.core.agent.domain.SaveAgentHost;
import com.flowci.core.agent.domain.AgentHostOption;
import com.flowci.core.agent.service.AgentHostService;
import com.flowci.core.auth.annotation.Action;
import lombok.extern.log4j.Log4j2;
Expand Down Expand Up @@ -56,7 +56,7 @@ public AgentHost deleteByName(@PathVariable String name) {

@PostMapping
@Action(AgentHostAction.CREATE_UPDATE)
public AgentHost createOrUpdate(@RequestBody @Validated SaveAgentHost body) {
public AgentHost createOrUpdate(@RequestBody @Validated AgentHostOption body) {
AgentHost host = body.toObj();
return agentHostService.createOrUpdate(host);
}
Expand Down
@@ -0,0 +1,9 @@
package com.flowci.core.agent.dao;

import com.flowci.core.agent.domain.AgentProfile;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface AgentProfileDao extends MongoRepository<AgentProfile, String> {
}
36 changes: 14 additions & 22 deletions core/src/main/java/com/flowci/core/agent/domain/Agent.java
Expand Up @@ -22,14 +22,15 @@
import com.flowci.domain.SimpleKeyPair;
import com.flowci.tree.Selector;
import com.google.common.base.Strings;
import lombok.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;

import java.time.Instant;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

Expand Down Expand Up @@ -64,22 +65,6 @@ public static Status fromBytes(byte[] bytes) {
}
}

@Getter
@Setter
@Accessors(chain = true)
public static class Resource {

private int cpu;

private int totalMemory; // in MB

private int freeMemory; // in MB

private int totalDisk; // in MB

private int freeDisk; // in MB
}

@Indexed(name = "index_agent_name", unique = true)
private String name;

Expand All @@ -97,13 +82,15 @@ public static class Resource {

private OS os = OS.UNKNOWN;

private Resource resource = new Resource();

private Set<String> tags = Collections.emptySet();

private int exitOnIdle;

private Status status = Status.OFFLINE;

private Date statusUpdatedAt;
private Instant statusUpdatedAt;

private Instant connectedAt;

private String jobId;

Expand All @@ -121,9 +108,14 @@ public Agent(String name, Set<String> tags) {
this.setTags(tags);
}

@JsonIgnore
public AgentConfig getConfig() {
return new AgentConfig().setExitOnIdle(exitOnIdle);
}

public void setStatus(Status status) {
this.status = status;
this.statusUpdatedAt = new Date();
this.statusUpdatedAt = Instant.now();
}

public void setTags(Set<String> tags) {
Expand Down
13 changes: 13 additions & 0 deletions core/src/main/java/com/flowci/core/agent/domain/AgentConfig.java
@@ -0,0 +1,13 @@
package com.flowci.core.agent.domain;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

@Getter
@Setter
@Accessors(chain = true)
public class AgentConfig {

private int ExitOnIdle;
}
Expand Up @@ -80,6 +80,11 @@ public enum Type {
*/
private Set<String> tags = new HashSet<>();

/**
* Ref to Agent.exitOnIdle
*/
private int exitOnIdle;

/**
* Error message if connection fail
*/
Expand Down
Expand Up @@ -29,12 +29,14 @@

@Getter
@Setter
public class SaveAgentHost {
public class AgentHostOption {

private String id;

private Set<String> tags = new HashSet<>();

private int exitOnIdle;

@NotNull
private AgentHost.Type type;

Expand Down Expand Up @@ -68,6 +70,7 @@ public AgentHost toObj() {
host.setIp(ip);
host.setMaxSize(maxSize);
host.setPort(port);
host.setExitOnIdle(exitOnIdle);
return host;
}

Expand All @@ -77,6 +80,7 @@ public AgentHost toObj() {
host.setName(name);
host.setTags(tags);
host.setMaxSize(maxSize);
host.setExitOnIdle(exitOnIdle);
return host;
}

Expand All @@ -88,6 +92,7 @@ public AgentHost toObj() {
host.setSecret(secret);
host.setNamespace(namespace);
host.setMaxSize(maxSize);
host.setExitOnIdle(exitOnIdle);
return host;
}

Expand Down
Expand Up @@ -38,6 +38,4 @@ public class AgentInit {
private Common.OS os;

private Agent.Status status;

private Agent.Resource resource = new Agent.Resource();
}
Expand Up @@ -17,15 +17,18 @@
package com.flowci.core.agent.domain;

import com.google.common.base.Strings;
import java.util.Set;
import javax.validation.constraints.NotEmpty;
import lombok.Data;
import lombok.experimental.Accessors;

import javax.validation.constraints.NotEmpty;
import java.util.Set;

/**
* @author yang
*/
@Data
public class CreateOrUpdateAgent {
@Accessors(chain = true)
public class AgentOption {

@NotEmpty
private String name;
Expand All @@ -34,6 +37,10 @@ public class CreateOrUpdateAgent {

private String token;

private int exitOnIdle;

private String hostId;

public boolean hasToken() {
return !Strings.isNullOrEmpty(token);
}
Expand Down
29 changes: 29 additions & 0 deletions core/src/main/java/com/flowci/core/agent/domain/AgentProfile.java
@@ -0,0 +1,29 @@
package com.flowci.core.agent.domain;

import com.flowci.core.common.domain.Mongoable;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
* Id is agent token
*/
@Getter
@Setter
@Accessors(chain = true)
public class AgentProfile extends Mongoable {

public static final AgentProfile EMPTY = new AgentProfile();

private int cpuNum;

private double cpuUsage;

private int totalMemory; // in MB

private int freeMemory; // in MB

private int totalDisk; // in MB

private int freeDisk; // in MB
}
2 changes: 2 additions & 0 deletions core/src/main/java/com/flowci/core/agent/domain/ShellIn.java
Expand Up @@ -50,6 +50,8 @@ public enum ShellType {

private Set<String> envFilters;

private Set<String> secrets;

public ShellIn() {
super(Type.SHELL);
}
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/com/flowci/core/agent/domain/ShellOut.java
Expand Up @@ -5,11 +5,13 @@
import com.flowci.domain.Vars;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.util.Date;

@Getter
@Setter
@Accessors(chain = true)
public final class ShellOut implements Executed, CmdOut {

private String id;
Expand Down
@@ -0,0 +1,15 @@
package com.flowci.core.agent.event;

import com.flowci.core.agent.domain.AgentProfile;
import lombok.Getter;

@Getter
public class OnAgentProfileEvent extends EventFromClient {

private final AgentProfile profile;

public OnAgentProfileEvent(Object source, AgentProfile profile) {
super(source, null, null);
this.profile = profile;
}
}
@@ -1,14 +1,19 @@
package com.flowci.core.agent.event;

import com.flowci.core.agent.domain.Agent;
import com.flowci.core.agent.domain.AgentInit;
import lombok.Getter;
import lombok.Setter;
import org.springframework.web.socket.WebSocketSession;

@Getter
@Setter
public class OnConnectedEvent extends EventFromClient {

private final AgentInit init;

private Agent agent;

public OnConnectedEvent(Object source, String token, WebSocketSession session, AgentInit init) {
super(source, token, session);
this.init = init;
Expand Down

0 comments on commit 462e88d

Please sign in to comment.