use of com.sequenceiq.cloudbreak.orchestrator.salt.client.target.HostAndRoleTarget in project cloudbreak by hortonworks.
the class SaltOrchestrator method getHostnamesForRole.
@SuppressFBWarnings("REC_CATCH_EXCEPTION")
private Set<String> getHostnamesForRole(GatewayConfig primaryGateway, String roleToSearch, Set<Node> allNodes) throws CloudbreakOrchestratorFailedException {
Map<String, JsonNode> roles;
Collection<String> targetHosts = allNodes.stream().map(Node::getHostname).collect(Collectors.toSet());
if (targetHosts.isEmpty()) {
LOGGER.warn("No hosts to get role for");
roles = Map.of();
} else {
try (SaltConnector sc = saltService.createSaltConnector(primaryGateway)) {
roles = SaltStates.getGrains(sc, new HostAndRoleTarget(roleToSearch, targetHosts), "roles");
} catch (Exception e) {
LOGGER.warn("Error occurred when getting roles", e);
throw new CloudbreakOrchestratorFailedException(e.getMessage(), e);
}
}
return roles.keySet();
}
use of com.sequenceiq.cloudbreak.orchestrator.salt.client.target.HostAndRoleTarget in project cloudbreak by hortonworks.
the class SaltOrchestratorTest method testExistingIsPrimaryReplacement.
@Test
public void testExistingIsPrimaryReplacement() throws Exception {
ObjectMapper mapper = new ObjectMapper();
GatewayConfig replica1 = mock(GatewayConfig.class);
GatewayConfig replica2 = mock(GatewayConfig.class);
Node replicaNode1 = mock(Node.class);
Node replicaNode2 = mock(Node.class);
when(replica1.getHostname()).thenReturn("replica1.domain");
when(replica2.getHostname()).thenReturn("replica2.domain");
when(replicaNode1.getHostname()).thenReturn("replica1.domain");
when(replicaNode2.getHostname()).thenReturn("replica2.domain");
PowerMockito.mockStatic(SaltStates.class);
HostAndRoleTarget replicaTarget = new HostAndRoleTarget("freeipa_replica", Set.of("replica1.domain", "replica2.domain"));
ArrayNode replicaRole = mapper.createArrayNode();
replicaRole.add("freeipa_replica");
PowerMockito.when(SaltStates.getGrains(eq(saltConnector), eq(replicaTarget), eq("roles"))).thenReturn(Map.of("replica1.domain", replicaRole, "replica2.domain", replicaRole));
saltOrchestrator.installFreeIpa(replica1, List.of(replica1, replica2), Set.of(replicaNode1, replicaNode2), exitCriteriaModel);
ArgumentCaptor<ModifyGrainBase> modifyGrainCaptor = ArgumentCaptor.forClass(ModifyGrainBase.class);
verify(saltCommandRunner, times(2)).runModifyGrainCommand(eq(saltConnector), modifyGrainCaptor.capture(), eq(exitCriteriaModel), eq(exitCriteria));
List<ModifyGrainBase> modifyGrains = modifyGrainCaptor.getAllValues();
GrainAddRunner addReplacementRole = (GrainAddRunner) modifyGrains.get(0);
assertEquals("freeipa_primary_replacement", addReplacementRole.getValue());
assertEquals(Set.of("replica1.domain"), addReplacementRole.getTargetHostnames());
GrainRemoveRunner removeReplicaRole = (GrainRemoveRunner) modifyGrains.get(1);
assertEquals("freeipa_replica", removeReplicaRole.getValue());
assertEquals(Set.of("replica1.domain"), removeReplicaRole.getTargetHostnames());
ArgumentCaptor<SaltJobIdTracker> saltJobIdTrackerCaptor = ArgumentCaptor.forClass(SaltJobIdTracker.class);
verify(saltRunner, times(3)).runner(saltJobIdTrackerCaptor.capture(), eq(exitCriteria), eq(exitCriteriaModel), anyInt(), anyBoolean());
List<SaltJobIdTracker> saltJobIdTrackers = saltJobIdTrackerCaptor.getAllValues();
HighStateRunner highStateRunner = (HighStateRunner) saltJobIdTrackers.get(0).getSaltJobRunner();
assertTrue(highStateRunner.getTargetHostnames().contains("replica1.domain") || highStateRunner.getTargetHostnames().contains("replica2.domain"));
assertEquals(1, highStateRunner.getTargetHostnames().size());
HighStateRunner highStateRunner2 = (HighStateRunner) saltJobIdTrackers.get(1).getSaltJobRunner();
assertTrue(highStateRunner2.getTargetHostnames().contains("replica2.domain") || highStateRunner2.getTargetHostnames().contains("replica1.domain"));
assertEquals(1, highStateRunner2.getTargetHostnames().size());
assertFalse(highStateRunner.getTargetHostnames().containsAll(highStateRunner2.getTargetHostnames()));
HighStateRunner highStateRunner3 = (HighStateRunner) saltJobIdTrackers.get(2).getSaltJobRunner();
assertTrue(highStateRunner3.getTargetHostnames().contains("replica1.domain"));
assertEquals(1, highStateRunner3.getTargetHostnames().size());
}
use of com.sequenceiq.cloudbreak.orchestrator.salt.client.target.HostAndRoleTarget in project cloudbreak by hortonworks.
the class SaltOrchestratorTest method testNewInstanceIsPrimaryReplacement.
@Test
public void testNewInstanceIsPrimaryReplacement() throws Exception {
ObjectMapper mapper = new ObjectMapper();
GatewayConfig newInstance = mock(GatewayConfig.class);
GatewayConfig replica2 = mock(GatewayConfig.class);
Node newInstanceNode = mock(Node.class);
Node replicaNode2 = mock(Node.class);
when(newInstance.getHostname()).thenReturn("newInstance.domain");
when(replica2.getHostname()).thenReturn("replica2.domain");
when(newInstanceNode.getHostname()).thenReturn("newInstance.domain");
when(replicaNode2.getHostname()).thenReturn("replica2.domain");
PowerMockito.mockStatic(SaltStates.class);
HostAndRoleTarget replicaTarget = new HostAndRoleTarget("freeipa_replica", Set.of("newInstance.domain", "replica2.domain"));
ArrayNode replicaRole = mapper.createArrayNode();
replicaRole.add("freeipa_replica");
PowerMockito.when(SaltStates.getGrains(eq(saltConnector), eq(replicaTarget), eq("roles"))).thenReturn(Map.of("replica2.domain", replicaRole));
saltOrchestrator.installFreeIpa(newInstance, List.of(newInstance, replica2), Set.of(newInstanceNode, replicaNode2), exitCriteriaModel);
ArgumentCaptor<ModifyGrainBase> modifyGrainCaptor = ArgumentCaptor.forClass(ModifyGrainBase.class);
verify(saltCommandRunner, times(2)).runModifyGrainCommand(eq(saltConnector), modifyGrainCaptor.capture(), eq(exitCriteriaModel), eq(exitCriteria));
List<ModifyGrainBase> modifyGrains = modifyGrainCaptor.getAllValues();
GrainAddRunner addReplacementRole = (GrainAddRunner) modifyGrains.get(0);
assertEquals("freeipa_primary_replacement", addReplacementRole.getValue());
assertEquals(Set.of("newInstance.domain"), addReplacementRole.getTargetHostnames());
GrainRemoveRunner removeReplicaRole = (GrainRemoveRunner) modifyGrains.get(1);
assertEquals("freeipa_replica", removeReplicaRole.getValue());
assertEquals(Set.of("newInstance.domain"), removeReplicaRole.getTargetHostnames());
ArgumentCaptor<SaltJobIdTracker> saltJobIdTrackerCaptor = ArgumentCaptor.forClass(SaltJobIdTracker.class);
verify(saltRunner, times(2)).runner(saltJobIdTrackerCaptor.capture(), eq(exitCriteria), eq(exitCriteriaModel), anyInt(), anyBoolean());
List<SaltJobIdTracker> saltJobIdTrackers = saltJobIdTrackerCaptor.getAllValues();
HighStateRunner highStateRunner = (HighStateRunner) saltJobIdTrackers.get(0).getSaltJobRunner();
assertTrue(highStateRunner.getTargetHostnames().contains("replica2.domain"));
assertEquals(1, highStateRunner.getTargetHostnames().size());
HighStateRunner highStateRunner2 = (HighStateRunner) saltJobIdTrackers.get(1).getSaltJobRunner();
assertTrue(highStateRunner2.getTargetHostnames().contains("newInstance.domain"));
assertEquals(1, highStateRunner2.getTargetHostnames().size());
}
Aggregations