io.airlift.airship.shared.InstallationUtils Maven / Gradle / Ivy
package io.airlift.airship.shared;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.InputSupplier;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.TreeMap;
import static io.airlift.airship.shared.ConfigUtils.newConfigEntrySupplier;
public class InstallationUtils
{
public static Installation toInstallation(Repository repository, Assignment assignment)
{
assignment = resolveAssignment(repository, assignment);
// load resources
Map 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);
}
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 boolean resourcesAreAvailable(Map availableResources, Map requiredResources)
{
for (Entry entry : requiredResources.entrySet()) {
int available = Objects.firstNonNull(availableResources.get(entry.getKey()), 0);
if (available < entry.getValue()) {
return false;
}
}
return true;
}
public static Map getAvailableResources(AgentStatus agentStatus)
{
Map availableResources = new TreeMap(agentStatus.getResources());
for (SlotStatus slotStatus : agentStatus.getSlotStatuses()) {
for (Entry entry : slotStatus.getResources().entrySet()) {
int value = Objects.firstNonNull(availableResources.get(entry.getKey()), 0);
availableResources.put(entry.getKey(), value - entry.getValue());
}
}
return availableResources;
}
public static Map readResources(Repository repository, Assignment assignment)
{
ImmutableMap.Builder builder = ImmutableMap.builder();
InputSupplier extends InputStream> resourcesFile = newConfigEntrySupplier(repository, assignment.getConfig(), "airship-resources.properties");
if (resourcesFile != null) {
try {
Properties resources = new Properties();
resources.load(resourcesFile.getInput());
for (Entry