public AgentInstances allAgents() { AgentInstances agents = new AgentInstances(agentStatusChangeListener); for (AgentInstance agent : currentInstances()) { agents.add(agent); } return agents; }
public AgentInstance findAgentAndRefreshStatus(String uuid) { return agentInstances.findAgentAndRefreshStatus(uuid); }
private boolean isMaxPendingAgentsLimitReached() { Integer maxPendingAgentsAllowed = systemEnvironment.get(SystemEnvironment.MAX_PENDING_AGENTS_ALLOWED); int pendingAgentsCount = this.size() - findRegisteredAgents().size(); return pendingAgentsCount >= maxPendingAgentsAllowed; }
public AgentInstances findRegisteredAgents() { this.refresh(); AgentInstances registered = new AgentInstances(agentStatusChangeListener); synchronized (agentInstances) { for (AgentInstance agentInstance : this) { if (agentInstance.getStatus().isRegistered()) { registered.add(agentInstance); } } } return registered; }
@Test public void shouldNotRemovePendingAgentDuringSync() throws Exception { AgentInstances agentInstances = new AgentInstances(systemEnvironment, agentStatusChangeListener, AgentInstanceMother.building()); agentInstances.add(pending); Agents agents = new Agents(); agentInstances.sync(agents); assertThat(agentInstances.size(), is(1)); assertThat(agentInstances.findAgentAndRefreshStatus("uuid4").getStatus(), is(AgentStatus.Pending)); }
@Test public void shouldRemovePendingAgentThatIsTimedOut() { when(systemEnvironment.getAgentConnectionTimeout()).thenReturn(-1); AgentInstances agentInstances = new AgentInstances(systemEnvironment, agentStatusChangeListener, pending, building, disabled); agentInstances.refresh(); assertThat(agentInstances.findAgentAndRefreshStatus("uuid4"), is(instanceOf(NullAgentInstance.class))); }
@Test public void shouldFindRegisteredAgents() { AgentInstances agentInstances = sample(); AgentInstances agents = agentInstances.findRegisteredAgents(); assertThat(agents.size(), is(3)); assertThat(agents.findAgentAndRefreshStatus("uuid2"), is(idle)); assertThat(agents.findAgentAndRefreshStatus("uuid3"), is(building)); assertThat(agents.findAgentAndRefreshStatus("uuid5"), is(disabled)); }
@Test public void agentHostnameShouldBeUnique() { AgentConfig agentConfig = new AgentConfig("uuid2", "CCeDev01", "10.18.5.1"); AgentInstances agentInstances = new AgentInstances(mock(AgentStatusChangeListener.class)); agentInstances.register(AgentRuntimeInfo.fromServer(agentConfig, false, "/var/lib", 0L, "linux", false)); agentInstances.register(AgentRuntimeInfo.fromServer(agentConfig, false, "/var/lib", 0L, "linux", false)); }
@Test public void shouldSyncAgent() throws Exception { AgentInstances agentInstances = new AgentInstances(systemEnvironment, agentStatusChangeListener, AgentInstanceMother.building(), idle); AgentConfig agentConfig = new AgentConfig("uuid2", "CCeDev01", "10.18.5.1"); agentConfig.setDisabled(true); Agents oneAgentIsRemoved = new Agents(agentConfig); agentInstances.sync(oneAgentIsRemoved); assertThat(agentInstances.findAgentAndRefreshStatus("uuid2").getStatus(), is(AgentStatus.Disabled)); }
@Test public void shouldFindEnabledAgents() { AgentInstances agentInstances = sample(); AgentInstances enabledAgents = agentInstances.findEnabledAgents(); assertThat(enabledAgents.size(), is(2)); assertThat(enabledAgents.findAgentAndRefreshStatus("uuid2"), is(idle)); assertThat(enabledAgents.findAgentAndRefreshStatus("uuid3"), is(building)); }
@Test public void shouldUnderstandFilteringAgentListBasedOnUuid() { AgentInstances instances = new AgentInstances(mock(AgentStatusChangeListener.class)); AgentRuntimeInfo agent1 = AgentRuntimeInfo.fromServer(new AgentConfig("uuid-1", "host-1", "192.168.1.2"), true, "/foo/bar", 100l, "linux", false); AgentRuntimeInfo agent2 = AgentRuntimeInfo.fromServer(new AgentConfig("uuid-2", "host-2", "192.168.1.3"), true, "/bar/baz", 200l, "linux", false); AgentRuntimeInfo agent3 = AgentRuntimeInfo.fromServer(new AgentConfig("uuid-3", "host-3", "192.168.1.4"), true, "/baz/quux", 300l, "linux", false); AgentInstance instance1 = AgentInstance.createFromLiveAgent(agent1, systemEnvironment, mock(AgentStatusChangeListener.class)); instances.add(instance1); instances.add(AgentInstance.createFromLiveAgent(agent2, systemEnvironment, mock(AgentStatusChangeListener.class))); AgentInstance instance3 = AgentInstance.createFromLiveAgent(agent3, systemEnvironment, mock(AgentStatusChangeListener.class)); instances.add(instance3); List<AgentInstance> agents = instances.filter(Arrays.asList("uuid-1", "uuid-3")); assertThat(agents, hasItems(instance1, instance3)); assertThat(agents.size(), is(2)); }
public AgentInstance register(AgentRuntimeInfo info) { AgentInstance agentInstance = findAgentAndRefreshStatus(info.getUUId()); if (!agentInstance.isRegistered()) { if(isMaxPendingAgentsLimitReached()) { throw new MaxPendingAgentsLimitReachedException(systemEnvironment.get(SystemEnvironment.MAX_PENDING_AGENTS_ALLOWED)); } agentInstance = AgentInstance.createFromLiveAgent(info, systemEnvironment, agentStatusChangeListener); this.add(agentInstance); } agentInstance.update(info); return agentInstance; }
@Test public void shouldReturnNullAgentsWhenHostNameIsNotFound() throws Exception { AgentInstances agentInstances = new AgentInstances(systemEnvironment, agentStatusChangeListener, AgentInstanceMother.building()); agentInstances.add(idle); agentInstances.add(building); AgentInstance byHostname = agentInstances.findFirstByHostname("not-exist"); assertThat(byHostname, is(instanceOf(NullAgentInstance.class))); }
private AgentInstances sample() { AgentInstances agentInstances = new AgentInstances(null); agentInstances.add(idle); agentInstances.add(building); agentInstances.add(pending); agentInstances.add(disabled); return agentInstances; }
@Test public void shouldFindAgentsByItHostName() throws Exception { AgentInstance idle = AgentInstanceMother.idle(new Date(), "ghost-name"); AgentInstances agentInstances = new AgentInstances(systemEnvironment, agentStatusChangeListener, idle, AgentInstanceMother.building()); AgentInstance byHostname = agentInstances.findFirstByHostname("ghost-name"); assertThat(byHostname, is(idle)); }
public AgentInstances findRegisteredAgents() { return agentInstances.findRegisteredAgents(); }
/** * called from spring timer */ public void refresh() { agentInstances.refresh(); }
@Autowired public AgentService(AgentConfigService agentConfigService, SystemEnvironment systemEnvironment, final EnvironmentConfigService environmentConfigService, SecurityService securityService, AgentDao agentDao, UuidGenerator uuidGenerator, ServerHealthService serverHealthService, final EmailSender emailSender, AgentStatusChangeNotifier agentStatusChangeNotifier) { this(agentConfigService, systemEnvironment, null, environmentConfigService, securityService, agentDao, uuidGenerator, serverHealthService, agentStatusChangeNotifier); this.agentInstances = new AgentInstances(agentStatusChangeNotifier); }
public Registration requestRegistration(Username username, AgentRuntimeInfo agentRuntimeInfo) { LOGGER.debug("Agent is requesting registration {}", agentRuntimeInfo); AgentInstance agentInstance = agentInstances.register(agentRuntimeInfo); Registration registration = agentInstance.assignCertification(); if (agentInstance.isRegistered()) { agentConfigService.saveOrUpdateAgent(agentInstance, username); LOGGER.debug("New Agent approved {}", agentRuntimeInfo); } return registration; }
public AgentInstances(SystemEnvironment systemEnvironment, AgentStatusChangeListener agentStatusChangeListener, AgentInstance... agentInstances) { this(agentStatusChangeListener); this.systemEnvironment = systemEnvironment; for (AgentInstance agentInstance : agentInstances) { this.add(agentInstance); } }