@JsonProperty public String getConfig() { if (assignment == null) { return null; } return assignment.getConfig(); }
public static Map<String, Integer> readResources(Repository repository, Assignment assignment) { ImmutableMap.Builder<String, Integer> builder = ImmutableMap.builder(); ByteSource resourcesFile = newConfigEntrySupplier(repository, assignment.getConfig(), "airship-resources.properties"); if (resourcesFile != null) { try { Properties resources = new Properties(); resources.load(resourcesFile.openStream()); for (Entry<Object, Object> entry : resources.entrySet()) { builder.put((String) entry.getKey(), Integer.valueOf((String) entry.getValue())); } } catch (IOException ignored) { } } return builder.build(); } }
@Override public boolean apply(@Nullable SlotStatus slotStatus) { return (slotStatus != null) && (slotStatus.getAssignment() != null) && glob.apply(slotStatus.getAssignment().getConfig()); } }
public static Assignment resolveAssignment(Repository repository, Assignment assignment) { // resolve assignment String resolvedBinary = repository.binaryResolve(assignment.getBinary()); Preconditions.checkArgument(resolvedBinary != null, "Unknown binary " + assignment.getBinary()); String resolvedConfig = repository.configResolve(assignment.getConfig()); Preconditions.checkArgument(resolvedConfig != null, "Unknown config " + assignment.getConfig()); assignment = new Assignment(resolvedBinary, resolvedConfig); return assignment; }
public static AssignmentRepresentation from(Assignment assignment) { return new AssignmentRepresentation(assignment.getBinary(), assignment.getConfig()); }
public Assignment upgradeAssignment(Repository repository, Assignment assignment) { Preconditions.checkNotNull(assignment, "assignment is null"); String binary = assignment.getBinary(); if (binaryVersion != null) { binary = repository.binaryUpgrade(binary, binaryVersion); checkArgument(binary != null, "Can not upgrade binary " + assignment.getBinary() + " to " + binaryVersion); } else { checkArgument(repository.binaryToHttpUri(assignment.getBinary()) != null, "Can not locate existing binary " + assignment.getBinary() + " for upgrade"); } String config = assignment.getConfig(); if (configVersion != null) { config = repository.configUpgrade(config, configVersion); checkArgument(config != null, "Can not upgrade config " + assignment.getConfig() + " to " + configVersion); } else { checkArgument(repository.configToHttpUri(assignment.getConfig()) != null, "Can not locate existing config " + assignment.getConfig() + " for upgrade"); } return new Assignment(binary, config); }
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; } }
public static Installation toInstallation(Repository repository, Assignment assignment) { assignment = resolveAssignment(repository, assignment); // load resources Map<String, Integer> resources = readResources(repository, assignment); // create installation URI binaryUri = repository.binaryToHttpUri(assignment.getBinary()); Preconditions.checkNotNull(binaryUri, "Unknown binary %s", binaryUri); URI configUri = repository.configToHttpUri(assignment.getConfig()); Preconditions.checkNotNull(configUri, "Unknown config %s", configUri); return new Installation( repository.configShortName(assignment.getConfig()), assignment, binaryUri, configUri, resources); }
@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; } });
@Override public void setExpectedState(ExpectedSlotStatus slotStatus) { Preconditions.checkNotNull(slotStatus, "slotStatus is null"); if (isDomainCreated()) { List<ReplaceableAttribute> attributes = newArrayList(); attributes.add(new ReplaceableAttribute("state", slotStatus.getStatus().toString(), true)); if (slotStatus.getAssignment() != null) { attributes.add(new ReplaceableAttribute("binary", slotStatus.getAssignment().getBinary(), true)); attributes.add(new ReplaceableAttribute("config", slotStatus.getAssignment().getConfig(), true)); } try { simpleDb.putAttributes(new PutAttributesRequest().withDomainName(domainName).withItemName(slotStatus.getId().toString()).withAttributes(attributes)); expectedStateStoreUp(); } catch (Exception e) { expectedStateStoreDown(e); } } }
private void assertSlotStatus(SlotStatusRepresentation slot, UUID expectedSlotId, Assignment expectedAssignment, SlotLifecycleState expectedState, AgentStatusRepresentation expectedAgent) { assertEquals(slot.getId(), expectedSlotId); if (expectedState != SlotLifecycleState.TERMINATED) { assertEquals(slot.getBinary(), expectedAssignment.getBinary()); assertEquals(slot.getConfig(), expectedAssignment.getConfig()); assertTrue(slot.getInstallPath().startsWith(tempDir.getAbsolutePath())); } else { assertNull(slot.getBinary()); assertNull(slot.getConfig()); assertNull(slot.getInstallPath()); } assertEquals(slot.getStatus(), expectedState.toString()); assertEquals(slot.getInstanceId(), expectedAgent.getInstanceId()); assertTrue(slot.getLocation().startsWith(expectedAgent.getLocation())); assertTrue(slot.getLocation().endsWith(slot.getShortLocation())); assertTrue(slot.getSelf().toASCIIString().startsWith(expectedAgent.getSelf().toASCIIString())); assertTrue(slot.getExternalUri().toASCIIString().startsWith(expectedAgent.getExternalUri().toASCIIString())); }
private void assertSlotStatus(SlotStatusRepresentation slot, UUID expectedSlotId, Assignment expectedAssignment, SlotLifecycleState expectedState, AgentStatusRepresentation expectedAgent) { assertEquals(slot.getId(), expectedSlotId); if (expectedState != SlotLifecycleState.TERMINATED) { assertEquals(slot.getBinary(), expectedAssignment.getBinary()); assertEquals(slot.getConfig(), expectedAssignment.getConfig()); assertTrue(slot.getInstallPath().startsWith(tempDir.getAbsolutePath())); } else { assertNull(slot.getBinary()); assertNull(slot.getConfig()); assertNull(slot.getInstallPath()); } assertEquals(slot.getStatus(), expectedState.toString()); assertEquals(slot.getInstanceId(), expectedAgent.getInstanceId()); assertTrue(slot.getLocation().startsWith(expectedAgent.getLocation())); assertTrue(slot.getLocation().endsWith(slot.getShortLocation())); assertTrue(slot.getSelf().toASCIIString().startsWith(expectedAgent.getSelf().toASCIIString())); assertTrue(slot.getExternalUri().toASCIIString().startsWith(expectedAgent.getExternalUri().toASCIIString())); }
@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); }
@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); }
private void assertSlotStatus(SlotStatusRepresentation slot, UUID expectedSlotId, Assignment expectedAssignment, SlotLifecycleState expectedState, AgentStatusRepresentation expectedAgent) { assertEquals(slot.getId(), expectedSlotId); if (expectedState != SlotLifecycleState.TERMINATED) { assertEquals(slot.getBinary(), expectedAssignment.getBinary()); assertEquals(slot.getConfig(), expectedAssignment.getConfig()); assertNotNull(slot.getInstallPath()); } else { assertNull(slot.getBinary()); assertNull(slot.getConfig()); assertNull(slot.getInstallPath()); } assertEquals(slot.getStatus(), expectedState.toString()); assertEquals(slot.getInstanceId(), expectedAgent.getInstanceId()); assertTrue(slot.getLocation().startsWith(expectedAgent.getLocation())); assertTrue(slot.getLocation().endsWith(slot.getShortLocation())); assertTrue(slot.getSelf().toASCIIString().startsWith(expectedAgent.getSelf().toASCIIString())); assertTrue(slot.getExternalUri().toASCIIString().startsWith(expectedAgent.getExternalUri().toASCIIString())); }
@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("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())