/** * Creates a name validator that excludes any container names that already exist */ public static NameValidator createNameValidator(Container[] containers) { return createNameValidator(containers, Collections.<String>emptySet()); }
/** * Returns all the current alive and successful containers for the given profile which have completed provisioning */ public static List<Container> aliveAndSuccessfulContainersForProfile(String profile, Container[] allContainers) { List<Container> answer = new ArrayList<Container>(); List<Container> containers = containersForProfile(allContainers, profile); for (Container container : containers) { boolean aliveAndProvisionSuccess = isAliveAndProvisionSuccess(container); if (aliveAndProvisionSuccess) { answer.add(container); } } return answer; }
/** * Returns all the current alive and successful containers for the given profile which have completed provisioning */ public static List<Container> aliveAndSuccessfulContainersForProfile(String profile, FabricService fabricService) { Container[] allContainers = fabricService.getContainers(); return aliveAndSuccessfulContainersForProfile(profile, allContainers); }
@Override public void createContainers(AutoScaleRequest request) throws Exception { int count = request.getDelta(); String profile = request.getProfile(); String version = request.getVersion(); FabricService fabricService = request.getFabricService(); CreateOpenshiftContainerOptions.Builder builder = null; if (fabricService != null) { builder = createAutoScaleOptions(fabricService); } if (builder != null) { // TODO this is actually generic to all providers! :) for (int i = 0; i < count; i++) { Container[] containers = fabricService.getContainers(); final CreateOpenshiftContainerOptions.Builder configuredBuilder = builder.number(1).version(version).profiles(profile); NameValidator openShiftValidator = containerProvider.createNameValidator(configuredBuilder.build()); NameValidator fabricNameValidator = Containers.createNameValidator(fabricService.getContainers()); NameValidator nameValidator = Containers.joinNameValidators(openShiftValidator, fabricNameValidator); String name = Containers.createContainerName(containers, profile, containerProvider.getScheme(), nameValidator); CreateOpenshiftContainerOptions options = configuredBuilder.name(name).build(); LOG.info("Creating container name " + name + " version " + version + " profile " + profile + " " + count + " container(s)"); fabricService.createContainers(options); } } else { LOG.warn("Could not create version " + version + " profile " + profile + " due to missing autoscale configuration"); } }
@Override public void createContainers(String version, String profile, int count) throws Exception { FabricService fabricService = containerProvider.getFabricService(); CreateDockerContainerOptions.Builder builder = null; if (fabricService != null) { builder = createAuthoScaleOptions(fabricService); } if (builder != null) { // TODO this is actually generic to all providers! :) for (int i = 0; i < count; i++) { Container[] containers = fabricService.getContainers(); final CreateDockerContainerOptions.Builder configuredBuilder = builder.number(1).version(version).profiles(profile); NameValidator nameValidator = Containers.createNameValidator(fabricService.getContainers()); String name = Containers.createContainerName(containers, profile, containerProvider.getScheme(), nameValidator); CreateDockerContainerOptions options = configuredBuilder.name(name).build(); LOG.info("Creating container name " + name + " version " + version + " profile " + profile + " " + count + " container(s)"); fabricService.createContainers(options); } } else { LOG.warn("Could not create version " + version + " profile " + profile + " due to missing autoscale configuration"); } }
/** * Returns all the current alive or pending profiles for the given profile */ public static List<Container> aliveOrPendingContainersForProfile(String profile, Container[] allContainers) { List<Container> answer = new ArrayList<Container>(); List<Container> containers = containersForProfile(allContainers, profile); for (Container container : containers) { boolean alive = container.isAlive(); boolean provisioningPending = container.isProvisioningPending(); if (alive || provisioningPending) { answer.add(container); } } return answer; }
@Override public void createContainers(AutoScaleRequest request) throws Exception { int count = request.getDelta(); String profile = request.getProfile(); String version = request.getVersion(); FabricService fabricService = request.getFabricService(); CreateChildContainerOptions.Builder builder = null; if (fabricService != null) { builder = createAutoScaleOptions(request, fabricService); } if (builder != null) { Set<String> ignoreContainerNames = new HashSet<>(); for (int i = 0; i < count; i++) { final CreateChildContainerOptions.Builder configuredBuilder = builder.number(1).version(version).profiles(profile); Container[] containers = fabricService.getContainers(); NameValidator nameValidator = Containers.createNameValidator(containers); String name = Containers.createAutoScaleContainerName(containers, profile, containerProvider.getScheme(), nameValidator); ignoreContainerNames.add(name); CreateChildContainerOptions options = configuredBuilder.name(name).build(); LOG.info("Creating container name " + name + " version " + version + " profile " + profile + " " + count + " container(s)"); fabricService.createContainers(options); } } else { LOG.warn("Could not create version " + version + " profile " + profile + " due to missing autoscale configuration"); } }
NameValidator validator = Containers.createNameValidator(containers, ignoreContainerNames); final String containerName = Containers.createUniqueContainerName(containers, originalName, validator); ignoreContainerNames.add(containerName);
/** * Returns the list of container ID links for this profile */ @GET @Path("containers") public Map<String, String> containers() { FabricService fabricService = getFabricService(); if (fabricService != null) { List<Container> containers = Containers.containersForProfile(fabricService.getContainers(), profile.getId(), profile.getVersion()); List<String> keys = Containers.containerIds(containers); // lets get the link to the fabric ResourceSupport node = this; for (int i = 0; i < 2 && node != null; i++) { node = node.getParent(); } String baseURI = node != null ? node.getBaseUri() : ""; return Links.mapIdsToLinks(keys, baseURI + "/container/"); } else { noFabricService(); } return Collections.emptyMap(); }
/** * Creates a name for a new container given the current list of containers and the profile name. * For a profile of "foo" then this method tries to create a name of the form "foo1" or "foo2" * based on how many containers there are and if the name already exists. */ public static String createContainerName(Container[] containers, String profile, String scheme, NameValidator nameValidator) { Map<String, Container> map = new HashMap<String, Container>(); for (Container container : containers) { map.put(container.getId(), container); } String postFix = "." + scheme; String namePrefix = profile; if (namePrefix.endsWith(postFix)) { namePrefix = namePrefix.substring(0, namePrefix.length() - postFix.length()); } // lets filter out non-alpha namePrefix = filterOutNonAlphaNumerics(namePrefix); List<Container> profileContainers = containersForProfile(containers, profile); int idx = profileContainers.size(); while (true) { String name = namePrefix + Integer.toString(++idx); if (!map.containsKey(name) && nameValidator.isValid(name)) { return name; } } }
List<Container> containers = Containers.aliveAndSuccessfulContainersForProfile(version, profile, service); int count = containers.size(); int delta = count - maximumInstances; List<Container> containers = Containers.aliveOrPendingContainersForProfile(version, profile, service); int count = containers.size(); int delta = minimumInstances - count;
public static List<Container> containersForProfile(Container[] containers, String profileId) { List<Container> answer = new ArrayList<Container>(); if (profileId != null) { for (Container c : containers) { if (containerHasProfile(c, profileId)) { answer.add(c); } } } return answer; }
/** * Returns true if the current container is a live and provisioned successfully. */ public static boolean isCurrentContainerAliveAndProvisionSuccess(FabricService service) { if (service == null) { return false; } return isAliveAndProvisionSuccess(service.getCurrentContainer()); }
/** * Returns all the current alive or pending profiles for the given profile */ public static List<Container> aliveOrPendingContainersForProfile(String profile, FabricService fabricService) { Container[] allContainers = fabricService.getContainers(); return aliveOrPendingContainersForProfile(profile, allContainers); }
@Override public void createContainers(AutoScaleRequest request) throws Exception { int count = request.getDelta(); String profile = request.getProfile(); String version = request.getVersion(); FabricService fabricService = request.getFabricService(); Container[] containers = fabricService.getContainers(); FabricRequirements requirements = request.getFabricRequirements(); List<? extends HostConfiguration> hostConfigurations = requirements.getSshHosts(); HostProfileCounter hostProfileCounter = new HostProfileCounter(); AutoScalers.createHostToProfileScaleMap(hostProfileCounter, hostConfigurations, containers); for (int i = 0; i < count; i++) { CreateSshContainerOptions.Builder builder = null; NameValidator nameValidator = Containers.createNameValidator(containers); String name = Containers.createContainerName(containers, profile, containerProvider.getScheme(), nameValidator); if (fabricService != null) { builder = createAutoScaleOptions(request, fabricService, hostProfileCounter); } if (builder != null) { final CreateSshContainerOptions.Builder configuredBuilder = builder.number(1).version(version).profiles(profile); CreateSshContainerOptions options = configuredBuilder.name(name).build(); LOG.info("Creating container name " + name + " version " + version + " profile " + profile + " " + count + " container(s)"); fabricService.createContainers(options); } } }
/** * Returns all the current alive or pending profiles for the given profile */ public static List<Container> aliveOrPendingContainersForProfile(String version, String profile, Container[] allContainers) { List<Container> answer = new ArrayList<Container>(); List<Container> containers = containersForProfile(allContainers, profile, version); for (Container container : containers) { boolean alive = container.isAlive(); boolean provisioningPending = container.isProvisioningPending(); if (alive || provisioningPending) { answer.add(container); } } return answer; }
/** * Returns the list of container ID links for this profile */ @GET @Path("containers") public Map<String, String> containers() { FabricService fabricService = getFabricService(); if (fabricService != null) { List<Container> containers = Containers.containersForProfile(fabricService.getContainers(), profile.getId(), profile.getVersion()); List<String> keys = Containers.containerIds(containers); // lets get the link to the fabric ResourceSupport node = this; for (int i = 0; i < 2 && node != null; i++) { node = node.getParent(); } String baseURI = node != null ? node.getBaseUri() : ""; return Links.mapIdsToLinks(keys, baseURI + "/container/"); } else { noFabricService(); } return Collections.emptyMap(); }
/** * Creates a name for a new container given the current list of containers and the profile name. * For a profile of "foo" then this method tries to create a name of the form "foo1" or "foo2" * based on how many containers there are and if the name already exists. */ public static String createAutoScaleContainerName(Container[] containers, String profile, String scheme, NameValidator nameValidator) { Map<String, Container> map = new HashMap<String, Container>(); for (Container container : containers) { map.put(container.getId(), container); } String postFix = "." + scheme; String namePrefix = profile; if (namePrefix.endsWith(postFix)) { namePrefix = namePrefix.substring(0, namePrefix.length() - postFix.length()); } // lets filter out non-alpha namePrefix = filterOutNonAlphaNumerics(namePrefix); namePrefix = "auto_"+namePrefix; List<Container> profileContainers = containersForProfile(containers, profile); int idx = profileContainers.size(); while (true) { String name = namePrefix + Integer.toString(++idx); if (!map.containsKey(name) && nameValidator.isValid(name)) { return name; } } }
public static List<Container> containersForProfile(Container[] containers, String profileId) { List<Container> answer = new ArrayList<Container>(); if (profileId != null) { for (Container c : containers) { if (containerHasProfile(c, profileId)) { answer.add(c); } } } return answer; }
/** * Returns true if the current container is a live and provisioned successfully. */ public static boolean isCurrentContainerAliveAndProvisionSuccess(FabricService service) { if (service == null) { return false; } return isAliveAndProvisionSuccess(service.getCurrentContainer()); }