Search in sources :

Example 1 with ModifyGrainBase

use of com.sequenceiq.cloudbreak.orchestrator.salt.poller.checker.ModifyGrainBase in project cloudbreak by hortonworks.

the class SaltOrchestratorTest method testStopClusterManagerAgent.

@Test
public void testStopClusterManagerAgent() throws Exception {
    Set<Node> downscaleTargets = new HashSet<>();
    downscaleTargets.add(new Node("10.0.0.2", "1.1.1.2", "10-0-0-2.example.com", "hg", "fqdn2", null));
    downscaleTargets.add(new Node("10.0.0.3", "1.1.1.3", "10-0-0-3.example.com", "hg", "fqdn3", null));
    PowerMockito.mockStatic(SaltStates.class);
    Set<String> responsiveAddresses = new HashSet<>();
    responsiveAddresses.add("10.0.0.1");
    responsiveAddresses.add("10.0.0.2");
    responsiveAddresses.add("10.0.0.3");
    PowerMockito.when(SaltStates.collectMinionIpAddresses(any(), any())).thenReturn(responsiveAddresses);
    Set<OrchestrationNode> allNodes = new HashSet<>();
    allNodes.add(() -> new Node("10.0.0.1", "1.1.1.1", "10-0-0-1.example.com", "hg", "fqdn3", null));
    allNodes.addAll(downscaleTargets.stream().map(node -> (OrchestrationNode) () -> node).collect(Collectors.toSet()));
    Callable pillarSaveCallable = mock(Callable.class);
    when(saltRunner.runner(any(), any(), any())).thenReturn(pillarSaveCallable);
    when(saltRunner.runner(any(), any(), any(), anyInt(), anyBoolean())).thenReturn(mock(Callable.class));
    saltOrchestrator.stopClusterManagerAgent(() -> allNodes, gatewayConfig, targets, downscaleTargets, exitCriteriaModel, new CmAgentStopFlags(false, false, false));
    ArgumentCaptor<ModifyGrainBase> modifyGrainBaseArgumentCaptor = ArgumentCaptor.forClass(ModifyGrainBase.class);
    ArgumentCaptor<PillarSave> pillarSaveArgumentCaptor = ArgumentCaptor.forClass(PillarSave.class);
    verify(pillarSaveCallable, times(1)).call();
    InOrder inOrder = inOrder(saltCommandRunner, saltRunner);
    inOrder.verify(saltCommandRunner).runModifyGrainCommand(any(), modifyGrainBaseArgumentCaptor.capture(), any(), any());
    ModifyGrainBase modifyGrainBase = modifyGrainBaseArgumentCaptor.getValue();
    assertThat(modifyGrainBase, instanceOf(GrainAddRunner.class));
    assertEquals("roles", modifyGrainBase.getKey());
    assertEquals("cloudera_manager_agent_stop", modifyGrainBase.getValue());
    inOrder.verify(saltRunner).runner(pillarSaveArgumentCaptor.capture(), any(), any());
    PillarSave capturedPillarSave = pillarSaveArgumentCaptor.getValue();
    ArgumentCaptor<SaltJobIdTracker> saltJobIdCaptor = ArgumentCaptor.forClass(SaltJobIdTracker.class);
    inOrder.verify(saltRunner).runner(saltJobIdCaptor.capture(), any(), any(), anyInt(), anyBoolean());
    inOrder.verify(saltCommandRunner).runModifyGrainCommand(any(), modifyGrainBaseArgumentCaptor.capture(), any(), any());
    inOrder.verifyNoMoreInteractions();
    modifyGrainBase = modifyGrainBaseArgumentCaptor.getValue();
    assertThat(modifyGrainBase, instanceOf(GrainRemoveRunner.class));
    assertEquals("roles", modifyGrainBase.getKey());
    assertEquals("cloudera_manager_agent_stop", modifyGrainBase.getValue());
    Set<String> targets = Whitebox.getInternalState(capturedPillarSave, "targets");
    assertTrue(targets.contains("10.0.0.1"));
    Pillar pillar = Whitebox.getInternalState(capturedPillarSave, "pillar");
    Map<String, Map> hosts = (Map) ((Map) pillar.getJson()).get("hosts");
    assertTrue(hosts.keySet().contains("10.0.0.1"));
    assertTrue(hosts.keySet().contains("10.0.0.2"));
    assertTrue(hosts.keySet().contains("10.0.0.3"));
}
Also used : InOrder(org.mockito.InOrder) 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) OrchestrationNode(com.sequenceiq.cloudbreak.common.orchestration.OrchestrationNode) Callable(java.util.concurrent.Callable) ModifyGrainBase(com.sequenceiq.cloudbreak.orchestrator.salt.poller.checker.ModifyGrainBase) CmAgentStopFlags(com.sequenceiq.cloudbreak.orchestrator.model.CmAgentStopFlags) GrainRemoveRunner(com.sequenceiq.cloudbreak.orchestrator.salt.poller.checker.GrainRemoveRunner) Pillar(com.sequenceiq.cloudbreak.orchestrator.salt.domain.Pillar) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet) PillarSave(com.sequenceiq.cloudbreak.orchestrator.salt.poller.PillarSave) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with ModifyGrainBase

use of com.sequenceiq.cloudbreak.orchestrator.salt.poller.checker.ModifyGrainBase 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 ModifyGrainBase

use of com.sequenceiq.cloudbreak.orchestrator.salt.poller.checker.ModifyGrainBase 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)

Example 4 with ModifyGrainBase

use of com.sequenceiq.cloudbreak.orchestrator.salt.poller.checker.ModifyGrainBase in project cloudbreak by hortonworks.

the class SaltOrchestrator method runOrchestratorGrainRunner.

@Override
public void runOrchestratorGrainRunner(OrchestratorGrainRunnerParams grainRunnerParams) throws CloudbreakOrchestratorFailedException {
    try (SaltConnector sc = saltService.createSaltConnector(grainRunnerParams.getPrimaryGatewayConfig())) {
        Set<String> targetHostNames = grainRunnerParams.getTargetHostNames();
        GrainOperation grainOperation = grainRunnerParams.getGrainOperation();
        Set<Node> allNodes = grainRunnerParams.getAllNodes();
        String key = grainRunnerParams.getKey();
        String value = grainRunnerParams.getValue();
        ModifyGrainBase runner;
        switch(grainOperation) {
            case ADD:
                runner = new GrainAddRunner(targetHostNames, allNodes, key, value);
                break;
            case REMOVE:
                runner = new GrainRemoveRunner(targetHostNames, allNodes, key, value);
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + grainOperation);
        }
        saltCommandRunner.runModifyGrainCommand(sc, runner, grainRunnerParams.getExitCriteriaModel(), exitCriteria);
    } catch (Exception e) {
        LOGGER.error("Exception during grain runner", e);
        throw new CloudbreakOrchestratorFailedException(e.getMessage(), e);
    }
}
Also used : ModifyGrainBase(com.sequenceiq.cloudbreak.orchestrator.salt.poller.checker.ModifyGrainBase) GrainRemoveRunner(com.sequenceiq.cloudbreak.orchestrator.salt.poller.checker.GrainRemoveRunner) CloudbreakOrchestratorFailedException(com.sequenceiq.cloudbreak.orchestrator.exception.CloudbreakOrchestratorFailedException) JsonNode(com.fasterxml.jackson.databind.JsonNode) Node(com.sequenceiq.cloudbreak.common.orchestration.Node) GrainOperation(com.sequenceiq.cloudbreak.orchestrator.host.GrainOperation) GrainAddRunner(com.sequenceiq.cloudbreak.orchestrator.salt.poller.checker.GrainAddRunner) 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)

Aggregations

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