private ContainerSpec copy(final ContainerSpec origin, final String newServerTemplateId, final String newServerTemplateName) { final Map<Capability, ContainerConfig> configMap = origin.getConfigs(); for (Map.Entry<Capability, ContainerConfig> entry : origin.getConfigs().entrySet()) { configMap.put(entry.getKey(), copy(entry.getValue())); } return new ContainerSpec(origin.getId(), origin.getContainerName(), new ServerTemplateKey(newServerTemplateId, newServerTemplateName), new ReleaseId(origin.getReleasedId()), origin.getStatus(), configMap); }
@Test public void testStartContainer() { createServerTemplateWithContainer(); List<Container> fakeResult = new ArrayList<Container>(); fakeResult.add(container); when(kieServerInstanceManager.startContainer(any(ServerTemplate.class), any(ContainerSpec.class))).thenReturn(fakeResult); specManagementService.startContainer(containerSpec); verify(kieServerInstanceManager, times(1)).startContainer(any(ServerTemplate.class), any(ContainerSpec.class)); ServerTemplate updated = specManagementService.getServerTemplate(serverTemplate.getId()); assertNotNull(updated); ContainerSpec updatedContainer = updated.getContainerSpec(containerSpec.getId()); assertNotNull(updatedContainer); assertEquals(KieContainerStatus.STARTED, updatedContainer.getStatus()); }
@Test public void testStopContainer() { createServerTemplateWithContainer(); List<Container> fakeResult = new ArrayList<Container>(); fakeResult.add(container); when(kieServerInstanceManager.stopContainer(any(ServerTemplate.class), any(ContainerSpec.class))).thenReturn(fakeResult); specManagementService.stopContainer(containerSpec); verify(kieServerInstanceManager, times(1)).stopContainer(any(ServerTemplate.class), any(ContainerSpec.class)); ServerTemplate updated = specManagementService.getServerTemplate(serverTemplate.getId()); assertNotNull(updated); ContainerSpec updatedContainer = updated.getContainerSpec(containerSpec.getId()); assertNotNull(updatedContainer); assertEquals(KieContainerStatus.STOPPED, updatedContainer.getStatus()); }
@Override public synchronized void saveServerTemplate(ServerTemplate serverTemplate) { if (templateStorage.exists(serverTemplate.getId())) { templateStorage.update(serverTemplate); } else { templateStorage.store(serverTemplate); } notificationService.notify(new ServerTemplateUpdated(serverTemplate)); Collection<ContainerSpec> containerSpecs = serverTemplate.getContainersSpec(); if (containerSpecs != null && !containerSpecs.isEmpty()) { for (ContainerSpec containerSpec : containerSpecs) { if (containerSpec.getStatus().equals(KieContainerStatus.STARTED)) { List<Container> containers = kieServerInstanceManager.startContainer(serverTemplate, containerSpec); notificationService.notify(serverTemplate, containerSpec, containers); } } } }
protected List<Container> callRemoteKieServerOperation(ServerTemplate serverTemplate, ContainerSpec containerSpec, RemoteKieServerOperation operation) { List<Container> containers = new ArrayList<org.kie.server.controller.api.model.runtime.Container>(); if (serverTemplate.getServerInstanceKeys() == null || serverTemplate.getServerInstanceKeys().isEmpty() || containerSpec == null) { return containers; } for (ServerInstanceKey instanceUrl : serverTemplate.getServerInstanceKeys()) { Container container = new Container(); container.setContainerSpecId(containerSpec.getId()); container.setServerTemplateId(serverTemplate.getId()); container.setServerInstanceId(instanceUrl.getServerInstanceId()); container.setUrl(instanceUrl.getUrl() + "/containers/" + containerSpec.getId()); container.setStatus(containerSpec.getStatus()); try { final KieServicesClient client = getClient(instanceUrl.getUrl()); operation.doOperation(client, container); containers.add(container); } catch (Exception e) { logger.debug("Unable to connect to {}", instanceUrl); } } return containers; }
private void updateView(final Collection<Container> containers) { containerStatusEmptyPresenter.setup(containerSpec); containerRemoteStatusPresenter.setup(containerSpec, containers); view.clear(); if (isEmpty(containers)) { view.setStatus(containerStatusEmptyPresenter.getView()); } else { view.setStatus(containerRemoteStatusPresenter.getView()); } view.setContainerName(containerSpec.getContainerName()); view.setGroupIp(containerSpec.getReleasedId().getGroupId()); view.setArtifactId(containerSpec.getReleasedId().getArtifactId()); containerRulesConfigPresenter.setVersion(containerSpec.getReleasedId().getVersion()); containerProcessConfigPresenter.disable(); updateStatus(containerSpec.getStatus() != null ? containerSpec.getStatus() : KieContainerStatus.STOPPED); for (Map.Entry<Capability, ContainerConfig> entry : containerSpec.getConfigs().entrySet()) { switch (entry.getKey()) { case RULE: setupRuleConfig((RuleConfig) entry.getValue()); break; case PROCESS: setupProcessConfig((ProcessConfig) entry.getValue()); break; } } }
@Test public void testNewContainerStarted() { final String path = "org:kie:1.0"; final GAV gav = new GAV(path); when(m2RepoService.loadGAVFromJar(path)).thenReturn(gav); when(view.getContainerName()).thenReturn("containerName"); when(view.getContainerAlias()).thenReturn("containerAlias"); when(view.getGroupId()).thenReturn(gav.getGroupId()); when(view.getArtifactId()).thenReturn(gav.getArtifactId()); when(view.getVersion()).thenReturn(gav.getVersion()); when(view.isStartContainer()).thenReturn(true); presenter.asWidget(); final ContainerSpec containerSpec = presenter.buildContainerSpec("templateId", Collections.<Capability, ContainerConfig>emptyMap()); assertEquals(new ReleaseId(gav.getGroupId(), gav.getArtifactId(), gav.getVersion()), containerSpec.getReleasedId()); assertEquals(KieContainerStatus.STARTED, containerSpec.getStatus()); assertEquals("containerAlias", containerSpec.getContainerName()); assertEquals("containerName", containerSpec.getId()); } }
@Override public synchronized void deactivateContainer(ContainerSpecKey containerSpecKey) { ServerTemplate serverTemplate = templateStorage.load(containerSpecKey.getServerTemplateKey().getId()); if (serverTemplate == null) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + containerSpecKey.getServerTemplateKey().getId()); } ContainerSpec containerSpec = serverTemplate.getContainerSpec(containerSpecKey.getId()); if (containerSpec == null) { throw new KieServerControllerIllegalArgumentException("No container spec found for id " + containerSpecKey.getId() + " within server template with id " + serverTemplate.getId()); } if (!containerSpec.getStatus().equals(KieContainerStatus.STARTED)) { throw new KieServerControllerIllegalArgumentException("Container " + containerSpecKey.getId() + " cannot be deactivated because it's not in started state, actual state " + containerSpec.getStatus()); } containerSpec.setStatus(KieContainerStatus.DEACTIVATED); templateStorage.update(serverTemplate); List<Container> containers = kieServerInstanceManager.deactivateContainer(serverTemplate, containerSpec); notificationService.notify(serverTemplate, containerSpec, containers); }
@Test public void testUpgradeContainerWhenContainerSpecStatusIsStarted() { final List<Container> fakeResult = new ArrayList<Container>() {{ add(container); }}; doReturn(fakeResult).when(kieServerInstanceManager).upgradeContainer(any(ServerTemplate.class), any(ContainerSpec.class)); containerSpec.setStatus(KieContainerStatus.STARTED); final ReleaseId initial = containerSpec.getReleasedId(); final ReleaseId upgradeTo = new ReleaseId("org.kie", "kie-server-kjar", "2.0"); ruleCapabilitiesService.upgradeContainer(containerSpec, upgradeTo); verify(kieServerInstanceManager).upgradeContainer(any(ServerTemplate.class), any(ContainerSpec.class)); final ServerTemplate updated = specManagementService.getServerTemplate(serverTemplate.getId()); final Collection<ContainerSpec> containerSpecs = updated.getContainersSpec(); assertNotNull(containerSpecs); assertEquals(1, containerSpecs.size()); ContainerSpec updatedContainer = containerSpecs.iterator().next(); assertNotNull(updatedContainer); assertNotNull(updatedContainer.getReleasedId()); assertNotEquals(initial, updatedContainer.getReleasedId()); assertEquals(upgradeTo, updatedContainer.getReleasedId()); assertEquals(updatedContainer.getStatus(), KieContainerStatus.STARTED); }
@Override public synchronized void activateContainer(ContainerSpecKey containerSpecKey) { ServerTemplate serverTemplate = templateStorage.load(containerSpecKey.getServerTemplateKey().getId()); if (serverTemplate == null) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + containerSpecKey.getServerTemplateKey().getId()); } final ContainerSpec containerSpec = serverTemplate.getContainerSpec(containerSpecKey.getId()); if (containerSpec == null) { throw new KieServerControllerIllegalArgumentException("No container spec found for id " + containerSpecKey.getId() + " within server template with id " + serverTemplate.getId()); } if (!containerSpec.getStatus().equals(KieContainerStatus.DEACTIVATED)) { throw new KieServerControllerIllegalArgumentException("Container " + containerSpecKey.getId() + " cannot be activated because it's not in deactivated state, actual state " + containerSpec.getStatus()); } containerSpec.setStatus(KieContainerStatus.STARTED); templateStorage.update(serverTemplate); List<Container> containers = kieServerInstanceManager.activateContainer(serverTemplate, containerSpec); notificationService.notify(serverTemplate, containerSpec, containers); }
@Test public void testOnDependencyPathSelectedEvent() { final String path = "org:kie:1.0"; final GAV gav = new GAV(path); when(m2RepoService.loadGAVFromJar(path)).thenReturn(gav); when(view.getContainerName()).thenReturn("containerName"); when(view.getContainerAlias()).thenReturn("containerAlias"); when(view.getGroupId()).thenReturn(gav.getGroupId()); when(view.getArtifactId()).thenReturn(gav.getArtifactId()); when(view.getVersion()).thenReturn(gav.getVersion()); presenter.asWidget(); presenter.onDependencyPathSelectedEvent(new DependencyPathSelectedEvent(artifactListWidgetPresenter, path)); verify(m2RepoService).loadGAVFromJar(path); verify(view).setGroupId(gav.getGroupId()); verify(view).setArtifactId(gav.getArtifactId()); verify(view).setVersion(gav.getVersion()); verify(wizardPageStatusChangeEvent).fire(any(WizardPageStatusChangeEvent.class)); final ContainerSpec containerSpec = presenter.buildContainerSpec("templateId", Collections.<Capability, ContainerConfig>emptyMap()); assertEquals(new ReleaseId(gav.getGroupId(), gav.getArtifactId(), gav.getVersion()), containerSpec.getReleasedId()); assertEquals(KieContainerStatus.STOPPED, containerSpec.getStatus()); assertEquals("containerAlias", containerSpec.getContainerName()); assertEquals("containerName", containerSpec.getId()); }
assertNotEquals(initial, updatedContainer.getReleasedId()); assertEquals(upgradeTo, updatedContainer.getReleasedId()); assertEquals(updatedContainer.getStatus(), KieContainerStatus.STARTED);
@Override public synchronized void updateContainerSpec(final String serverTemplateId, final String containerId, final ContainerSpec containerSpec) { ServerTemplate serverTemplate = templateStorage.load(serverTemplateId); if (serverTemplate == null) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + serverTemplateId); } if (!containerSpec.getId().equals(containerId)) { throw new KieServerControllerException("Cannot update container " + containerSpec.getId() + " on container " + containerId); } if (!serverTemplate.hasContainerSpec(containerSpec.getId())) { throw new KieServerControllerIllegalArgumentException("Server template with id " + serverTemplateId + " has no container with id " + containerSpec.getId()); } if (!serverTemplate.hasMatchingId(containerSpec.getServerTemplateKey())) { throw new KieServerControllerException("Cannot change container template key during update."); } // make sure correct server template is set containerSpec.setServerTemplateKey(new ServerTemplateKey(serverTemplate.getId(), serverTemplate.getName())); ContainerSpec currentVersion = serverTemplate.getContainerSpec(containerSpec.getId()); serverTemplate.deleteContainerSpec(currentVersion.getId()); serverTemplate.addContainerSpec(containerSpec); templateStorage.update(serverTemplate); notificationService.notify(new ServerTemplateUpdated(serverTemplate)); // in case container was started before it was update or update comes with status started update container in running servers if (currentVersion.getStatus().equals(KieContainerStatus.STARTED) || containerSpec.getStatus().equals(KieContainerStatus.STARTED)) { List<Container> containers = kieServerInstanceManager.upgradeAndStartContainer(serverTemplate, containerSpec); notificationService.notify(serverTemplate, containerSpec, containers); } }
@Override public synchronized void saveContainerSpec(String serverTemplateId, ContainerSpec containerSpec) { ServerTemplate serverTemplate = templateStorage.load(serverTemplateId); if (serverTemplate == null) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + serverTemplateId); } if (serverTemplate.hasContainerSpec(containerSpec.getId())) { throw new KieServerControllerException("Server template with id " + serverTemplateId + " associated already with container " + containerSpec.getId()); } // make sure correct server template is set containerSpec.setServerTemplateKey(new ServerTemplateKey(serverTemplate.getId(), serverTemplate.getName())); serverTemplate.addContainerSpec(containerSpec); templateStorage.update(serverTemplate); notificationService.notify(new ServerTemplateUpdated(serverTemplate)); if (containerSpec.getStatus().equals(KieContainerStatus.STARTED)) { List<Container> containers = kieServerInstanceManager.startContainer(serverTemplate, containerSpec); notificationService.notify(serverTemplate, containerSpec, containers); } }
assertEquals(serverTemplateId, spec.getServerTemplateKey().getId()); assertEquals("kie server name", spec.getServerTemplateKey().getName()); assertEquals(KieContainerStatus.STARTED, spec.getStatus()); assertEquals(0, spec.getConfigs().size());
@Override public void upgradeContainer(final ContainerSpecKey containerSpecKey, ReleaseId releaseId) { ServerTemplate serverTemplate = templateStorage.load(containerSpecKey.getServerTemplateKey().getId()); if (serverTemplate == null) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + containerSpecKey.getServerTemplateKey().getId()); } ContainerSpec containerSpec = serverTemplate.getContainerSpec(containerSpecKey.getId()); if (containerSpec == null) { throw new KieServerControllerIllegalArgumentException("No container spec found for id " + containerSpecKey.getId()); } if (releaseId.getGroupId() == null) { releaseId.setGroupId(containerSpec.getReleasedId().getGroupId()); } if (releaseId.getArtifactId() == null) { releaseId.setArtifactId(containerSpec.getReleasedId().getArtifactId()); } final List<Container> containers; containerSpec.setReleasedId(releaseId); if (containerSpec.getStatus() == KieContainerStatus.STARTED) { containers = kieServerInstanceManager.upgradeContainer(serverTemplate, containerSpec); } else { containers = kieServerInstanceManager.startContainer(serverTemplate, containerSpec); } containerSpec.setStatus(KieContainerStatus.STARTED); templateStorage.update(serverTemplate); notificationService.notify(serverTemplate, containerSpec, containers); }
container.setContainerSpecId(containerSpec.getId()); container.setUrl("http://fake.server.net/kie-server"); container.setStatus(containerSpec.getStatus());
container.setContainerSpecId(containerSpec.getId()); container.setUrl("http://fake.server.net/kie-server"); container.setStatus(containerSpec.getStatus());