Search in sources :

Example 1 with HostAndRoleTarget

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();
}
Also used : CloudbreakOrchestratorFailedException(com.sequenceiq.cloudbreak.orchestrator.exception.CloudbreakOrchestratorFailedException) HostAndRoleTarget(com.sequenceiq.cloudbreak.orchestrator.salt.client.target.HostAndRoleTarget) JsonNode(com.fasterxml.jackson.databind.JsonNode) SaltConnector(com.sequenceiq.cloudbreak.orchestrator.salt.client.SaltConnector) NotFoundException(com.sequenceiq.cloudbreak.common.exception.NotFoundException) CloudbreakOrchestratorTimeoutException(com.sequenceiq.cloudbreak.orchestrator.exception.CloudbreakOrchestratorTimeoutException) CloudbreakOrchestratorException(com.sequenceiq.cloudbreak.orchestrator.exception.CloudbreakOrchestratorException) CloudbreakOrchestratorFailedException(com.sequenceiq.cloudbreak.orchestrator.exception.CloudbreakOrchestratorFailedException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) CloudbreakServiceException(com.sequenceiq.cloudbreak.common.exception.CloudbreakServiceException) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 2 with HostAndRoleTarget

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());
}
Also used : HighStateRunner(com.sequenceiq.cloudbreak.orchestrator.salt.poller.checker.HighStateRunner) JsonNode(com.fasterxml.jackson.databind.JsonNode) OrchestrationNode(com.sequenceiq.cloudbreak.common.orchestration.OrchestrationNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Node(com.sequenceiq.cloudbreak.common.orchestration.Node) SaltJobIdTracker(com.sequenceiq.cloudbreak.orchestrator.salt.poller.SaltJobIdTracker) GrainAddRunner(com.sequenceiq.cloudbreak.orchestrator.salt.poller.checker.GrainAddRunner) ModifyGrainBase(com.sequenceiq.cloudbreak.orchestrator.salt.poller.checker.ModifyGrainBase) GrainRemoveRunner(com.sequenceiq.cloudbreak.orchestrator.salt.poller.checker.GrainRemoveRunner) HostAndRoleTarget(com.sequenceiq.cloudbreak.orchestrator.salt.client.target.HostAndRoleTarget) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) GatewayConfig(com.sequenceiq.cloudbreak.orchestrator.model.GatewayConfig) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with HostAndRoleTarget

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());
}
Also used : HighStateRunner(com.sequenceiq.cloudbreak.orchestrator.salt.poller.checker.HighStateRunner) JsonNode(com.fasterxml.jackson.databind.JsonNode) OrchestrationNode(com.sequenceiq.cloudbreak.common.orchestration.OrchestrationNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Node(com.sequenceiq.cloudbreak.common.orchestration.Node) SaltJobIdTracker(com.sequenceiq.cloudbreak.orchestrator.salt.poller.SaltJobIdTracker) GrainAddRunner(com.sequenceiq.cloudbreak.orchestrator.salt.poller.checker.GrainAddRunner) ModifyGrainBase(com.sequenceiq.cloudbreak.orchestrator.salt.poller.checker.ModifyGrainBase) GrainRemoveRunner(com.sequenceiq.cloudbreak.orchestrator.salt.poller.checker.GrainRemoveRunner) HostAndRoleTarget(com.sequenceiq.cloudbreak.orchestrator.salt.client.target.HostAndRoleTarget) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) GatewayConfig(com.sequenceiq.cloudbreak.orchestrator.model.GatewayConfig) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)3 HostAndRoleTarget (com.sequenceiq.cloudbreak.orchestrator.salt.client.target.HostAndRoleTarget)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 Node (com.sequenceiq.cloudbreak.common.orchestration.Node)2 OrchestrationNode (com.sequenceiq.cloudbreak.common.orchestration.OrchestrationNode)2 GatewayConfig (com.sequenceiq.cloudbreak.orchestrator.model.GatewayConfig)2 SaltJobIdTracker (com.sequenceiq.cloudbreak.orchestrator.salt.poller.SaltJobIdTracker)2 GrainAddRunner (com.sequenceiq.cloudbreak.orchestrator.salt.poller.checker.GrainAddRunner)2 GrainRemoveRunner (com.sequenceiq.cloudbreak.orchestrator.salt.poller.checker.GrainRemoveRunner)2 HighStateRunner (com.sequenceiq.cloudbreak.orchestrator.salt.poller.checker.HighStateRunner)2 ModifyGrainBase (com.sequenceiq.cloudbreak.orchestrator.salt.poller.checker.ModifyGrainBase)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 CloudbreakServiceException (com.sequenceiq.cloudbreak.common.exception.CloudbreakServiceException)1 NotFoundException (com.sequenceiq.cloudbreak.common.exception.NotFoundException)1 CloudbreakOrchestratorException (com.sequenceiq.cloudbreak.orchestrator.exception.CloudbreakOrchestratorException)1 CloudbreakOrchestratorFailedException (com.sequenceiq.cloudbreak.orchestrator.exception.CloudbreakOrchestratorFailedException)1 CloudbreakOrchestratorTimeoutException (com.sequenceiq.cloudbreak.orchestrator.exception.CloudbreakOrchestratorTimeoutException)1 SaltConnector (com.sequenceiq.cloudbreak.orchestrator.salt.client.SaltConnector)1