private Predicate<RemoteAgent> filterAgentsWithAssignment(Installation installation) { Preconditions.checkNotNull(installation, "installation is null"); final Assignment assignment = installation.getAssignment(); return new Predicate<RemoteAgent>() { @Override public boolean apply(RemoteAgent agent) { for (RemoteSlot slot : agent.getSlots()) { if ((slot.status().getAssignment() != null) && assignmentEqualsIgnoreVersion(assignment, slot.status().getAssignment())) { return false; } } return true; } }; }
private String toBaseName(Installation installation) { String configSpec = installation.getAssignment().getConfig(); MavenCoordinates mavenCoordinates = MavenCoordinates.fromConfigGAV(configSpec); String baseName; if (mavenCoordinates != null) { baseName = mavenCoordinates.getArtifactId(); } else if (configSpec.startsWith("@")) { baseName = configSpec.substring(1); } else { baseName = configSpec; } return baseName; } }
@Override public Deployment install(Installation installation) { Preconditions.checkNotNull(installation, "installation is null"); deployment = new Deployment(UUID.randomUUID(), location, new File("installation"), new File("data"), installation.getAssignment(), installation.getResources()); return deployment; }
@Override public Deployment install(Installation installation) { Preconditions.checkNotNull(installation, "installation is null"); deployment = new Deployment(UUID.randomUUID(), location, new File("installation"), new File("data"), installation.getAssignment(), installation.getResources()); return deployment; }
public static InstallationRepresentation from(Installation installation) { Assignment assignment = installation.getAssignment(); return new InstallationRepresentation( installation.getShortName(), AssignmentRepresentation.from(assignment), installation.getBinaryFile().toString(), installation.getConfigFile().toString(), installation.getResources()); }
@Test public void testConstructor() { URI binaryFile = URI.create("fake://localhost/binaryFile"); URI configFile = URI.create("fake://localhost/configFile"); Installation installation = new Installation("apple", APPLE_ASSIGNMENT, binaryFile, configFile, ImmutableMap.of("memory", 512)); assertEquals(installation.getAssignment(), APPLE_ASSIGNMENT); assertEquals(installation.getBinaryFile(), binaryFile); assertEquals(installation.getConfigFile(), configFile); }
@Test public void testConstructor() { URI binaryFile = URI.create("fake://localhost/binaryFile"); URI configFile = URI.create("fake://localhost/configFile"); Installation installation = new Installation("apple", APPLE_ASSIGNMENT, binaryFile, configFile, ImmutableMap.of("memory", 512)); assertEquals(installation.getAssignment(), APPLE_ASSIGNMENT); assertEquals(installation.getBinaryFile(), binaryFile); assertEquals(installation.getConfigFile(), configFile); }
@Override public SlotStatus apply(RemoteAgent agent) { SlotStatus slotStatus = agent.install(installation); stateManager.setExpectedState(new ExpectedSlotStatus(slotStatus.getId(), STOPPED, installation.getAssignment())); return slotStatus; } });
@Test public void testInstall() { // no deployment by default assertNull(manager.getDeployment()); // install apple deployment Deployment appleDeployment = manager.install(appleInstallation); assertNotNull(appleDeployment); assertEquals(manager.getDeployment(), appleDeployment); assertEquals(appleDeployment.getAssignment(), appleInstallation.getAssignment()); // install banana Deployment bananaDeployment = manager.install(bananaInstallation); assertNotNull(bananaDeployment); assertEquals(manager.getDeployment(), bananaDeployment); assertEquals(bananaDeployment.getAssignment(), bananaInstallation.getAssignment()); } }
@Override public SlotStatus assign(Installation installation) { Preconditions.checkNotNull(installation, "installation is null"); slotStatus = slotStatus.changeAssignment(STOPPED, installation.getAssignment(), slotStatus.getResources()); mockRemoteAgent.setSlotStatus(slotStatus); return slotStatus; }
@Override public SlotStatus assign(Installation installation) { Preconditions.checkNotNull(installation, "installation is null"); slotStatus = slotStatus.changeAssignment(STOPPED, installation.getAssignment(), slotStatus.getResources()); mockRemoteAgent.setSlotStatus(slotStatus); return slotStatus; }
@Test public void testInstall() { // no deployment by default assertNull(manager.getDeployment()); // install apple deployment Deployment appleDeployment = manager.install(appleInstallation); assertNotNull(appleDeployment); assertEquals(manager.getDeployment(), appleDeployment); assertEquals(appleDeployment.getAssignment(), appleInstallation.getAssignment()); // install banana Deployment bananaDeployment = manager.install(bananaInstallation); assertNotNull(bananaDeployment); assertEquals(manager.getDeployment(), bananaDeployment); assertEquals(bananaDeployment.getAssignment(), bananaInstallation.getAssignment()); } }
@Test public void testRestart() throws Exception { SlotStatus slotStatus = agent.install(appleInstallation); Request request = Request.Builder.preparePut() .setUri(urlFor(slotStatus, "lifecycle")) .setBodyGenerator(createStaticBodyGenerator("restarting", UTF_8)) .build(); Map<String, Object> response = client.execute(request, createJsonResponseHandler(mapCodec, Status.OK.getStatusCode())); Map<String, Object> expected = ImmutableMap.<String, Object>builder() .put("id", slotStatus.getId().toString()) .put("shortId", slotStatus.getId().toString()) .put("binary", appleInstallation.getAssignment().getBinary()) .put("shortBinary", appleInstallation.getAssignment().getBinary()) .put("config", appleInstallation.getAssignment().getConfig()) .put("shortConfig", appleInstallation.getAssignment().getConfig()) .put("self", urlFor(slotStatus).toASCIIString()) .put("externalUri", urlFor(slotStatus).toASCIIString()) .put("location", slotStatus.getLocation()) .put("shortLocation", slotStatus.getLocation()) .put("status", RUNNING.toString()) .put("version", VersionsUtil.createSlotVersion(slotStatus.getId(), RUNNING, appleInstallation.getAssignment())) .put("installPath", slotStatus.getInstallPath()) .put("resources", ImmutableMap.<String, Integer>of("memory", 512)) .build(); assertEquals(response, expected); }
@Override public SlotStatus apply(RemoteSlot slot) { boolean expectRestart = slot.status().getState() == RUNNING; Assignment assignment = newAssignments.get(slot.getId()); Preconditions.checkState(assignment != null, "Error no assignment for slot " + slot.getId()); URI configFile = repository.configToHttpUri(assignment.getConfig()); Installation installation = new Installation( repository.configShortName(assignment.getConfig()), assignment, repository.binaryToHttpUri(assignment.getBinary()), configFile, ImmutableMap.<String, Integer>of()); stateManager.setExpectedState(new ExpectedSlotStatus(slot.getId(), expectRestart ? RUNNING : STOPPED, installation.getAssignment())); SlotStatus slotStatus = slot.assign(installation); return slotStatus; } });
@Test public void testStart() throws Exception { SlotStatus slotStatus = agent.install(appleInstallation); Request request = Request.Builder.preparePut() .setUri(urlFor(slotStatus, "lifecycle")) .setBodyGenerator(createStaticBodyGenerator("running", UTF_8)) .build(); Map<String, Object> response = client.execute(request, createJsonResponseHandler(mapCodec, Status.OK.getStatusCode())); Map<String, Object> expected = ImmutableMap.<String, Object>builder() .put("id", slotStatus.getId().toString()) .put("shortId", slotStatus.getId().toString()) .put("binary", appleInstallation.getAssignment().getBinary()) .put("shortBinary", appleInstallation.getAssignment().getBinary()) .put("config", appleInstallation.getAssignment().getConfig()) .put("shortConfig", appleInstallation.getAssignment().getConfig()) .put("self", urlFor(slotStatus).toASCIIString()) .put("externalUri", urlFor(slotStatus).toASCIIString()) .put("location", slotStatus.getLocation()) .put("shortLocation", slotStatus.getLocation()) .put("status", RUNNING.toString()) .put("version", VersionsUtil.createSlotVersion(slotStatus.getId(), RUNNING, appleInstallation.getAssignment())) .put("installPath", slotStatus.getInstallPath()) .put("resources", ImmutableMap.<String, Integer>of("memory", 512)) .build(); assertEquals(response, expected); }
@Test public void testAssign() throws Exception { SlotStatus slotStatus = agent.install(appleInstallation); String json = installationCodec.toJson(InstallationRepresentation.from(appleInstallation)); Request request = Request.Builder.preparePut() .setUri(urlFor(slotStatus, "assignment")) .setHeader(CONTENT_TYPE, MediaType.APPLICATION_JSON) .setBodyGenerator(createStaticBodyGenerator(json, UTF_8)) .build(); Map<String, Object> response = client.execute(request, createJsonResponseHandler(mapCodec, Status.OK.getStatusCode())); Map<String, Object> expected = ImmutableMap.<String, Object>builder() .put("id", slotStatus.getId().toString()) .put("shortId", slotStatus.getId().toString()) .put("binary", appleInstallation.getAssignment().getBinary()) .put("shortBinary", appleInstallation.getAssignment().getBinary()) .put("config", appleInstallation.getAssignment().getConfig()) .put("shortConfig", appleInstallation.getAssignment().getConfig()) .put("self", urlFor(slotStatus).toASCIIString()) .put("externalUri", urlFor(slotStatus).toASCIIString()) .put("location", slotStatus.getLocation()) .put("shortLocation", slotStatus.getLocation()) .put("status", STOPPED.toString()) .put("version", slotStatus.getVersion()) .put("installPath", slotStatus.getInstallPath()) .put("resources", ImmutableMap.<String, Integer>of("memory", 512)) .build(); assertEquals(response, expected); }
@Test public void testStop() throws Exception { SlotStatus slotStatus = agent.install(appleInstallation); agent.getSlot(slotStatus.getId()).start(); Request request = Request.Builder.preparePut() .setUri(urlFor(slotStatus, "lifecycle")) .setBodyGenerator(createStaticBodyGenerator("stopped", UTF_8)) .build(); Map<String, Object> response = client.execute(request, createJsonResponseHandler(mapCodec, Status.OK.getStatusCode())); Map<String, Object> expected = ImmutableMap.<String, Object>builder() .put("id", slotStatus.getId().toString()) .put("shortId", slotStatus.getId().toString()) .put("binary", appleInstallation.getAssignment().getBinary()) .put("shortBinary", appleInstallation.getAssignment().getBinary()) .put("config", appleInstallation.getAssignment().getConfig()) .put("shortConfig", appleInstallation.getAssignment().getConfig()) .put("self", urlFor(slotStatus).toASCIIString()) .put("externalUri", urlFor(slotStatus).toASCIIString()) .put("location", slotStatus.getLocation()) .put("shortLocation", slotStatus.getLocation()) .put("status", STOPPED.toString()) .put("version", slotStatus.getVersion()) .put("installPath", slotStatus.getInstallPath()) .put("resources", ImmutableMap.<String, Integer>of("memory", 512)) .build(); assertEquals(response, expected); }
.put("id", slot.getId().toString()) .put("shortId", slot.getId().toString()) .put("binary", appleInstallation.getAssignment().getBinary()) .put("shortBinary", appleInstallation.getAssignment().getBinary()) .put("config", appleInstallation.getAssignment().getConfig()) .put("shortConfig", appleInstallation.getAssignment().getConfig()) .put("self", urlFor(slot).toASCIIString()) .put("externalUri", urlFor(slot).toASCIIString())
@Override public synchronized SlotStatus install(Installation installation) { checkNotNull(installation, "installation is null"); AgentStatus agentStatus = getAgentStatus(); Preconditions.checkState(agentStatus.getState() != OFFLINE, "agent is offline"); UUID slotId = UUID.randomUUID(); SlotStatus slotStatus = createSlotStatus(slotId, uriBuilderFrom(agentStatus.getInternalUri()).appendPath("slot").appendPath(slotId.toString()).build(), uriBuilderFrom(agentStatus.getExternalUri()).appendPath("slot").appendPath(slotId.toString()).build(), "instance", "/location", SlotLifecycleState.STOPPED, installation.getAssignment(), "/" + slotId, installation.getResources()); agentStatus.changeSlotStatus(slotStatus); return slotStatus; }
@Override public synchronized SlotStatus install(Installation installation) { checkNotNull(installation, "installation is null"); AgentStatus agentStatus = getAgentStatus(); Preconditions.checkState(agentStatus.getState() != OFFLINE, "agent is offline"); UUID slotId = UUID.randomUUID(); SlotStatus slotStatus = createSlotStatus(slotId, uriBuilderFrom(agentStatus.getInternalUri()).appendPath("slot").appendPath(slotId.toString()).build(), uriBuilderFrom(agentStatus.getExternalUri()).appendPath("slot").appendPath(slotId.toString()).build(), "instance", "/location", SlotLifecycleState.STOPPED, installation.getAssignment(), "/" + slotId, installation.getResources()); agentStatus.changeSlotStatus(slotStatus); return slotStatus; }