Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) FuseSource, Inc. http://fusesource.com Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package io.fabric8.openshift;
 
 import  io.fabric8.api.Container;
 import  io.fabric8.api.ContainerAutoScaler;
 import  io.fabric8.api.Containers;
 import  io.fabric8.api.FabricService;
 import  io.fabric8.api.NameValidator;
 import  org.slf4j.Logger;
 import  org.slf4j.LoggerFactory;
 
 import java.util.List;
 import java.util.Map;

 
 public class OpenShiftAutoScaler implements ContainerAutoScaler {
     private static final transient Logger LOG = LoggerFactory.getLogger(OpenShiftAutoScaler.class);
 
 
     public OpenShiftAutoScaler(OpenshiftContainerProvider containerProvider) {
         this. = containerProvider;
     }
 
     @Override
     public int getWeight() {
         return 100;
     }
 
     @Override
     public void createContainers(String versionString profileint countthrows Exception {
         FabricService fabricService = .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 < counti++) {
                 Container[] containers = fabricService.getContainers();
                 final CreateOpenshiftContainerOptions.Builder configuredBuilder = builder.number(1).version(version).profiles(profile);
 
                 NameValidator openShiftValidator = .createNameValidator(configuredBuilder.build());
                 NameValidator fabricNameValidator = Containers.createNameValidator(fabricService.getContainers());
                 NameValidator nameValidator = Containers.joinNameValidators(openShiftValidatorfabricNameValidator);
 
                 String name = Containers.createContainerName(containersprofile.getScheme(), nameValidator);
 
                 CreateOpenshiftContainerOptions options = configuredBuilder.name(name).build();
                 .info("Creating container name " + name + " version " + version + " profile " + profile + " " + count + " container(s)");
                 fabricService.createContainers(options);
             }
         } else {
             .warn("Could not create version " + version + " profile " + profile + " due to missing autoscale configuration");
         }
     }
 
     protected CreateOpenshiftContainerOptions.Builder createAutoScaleOptions(FabricService fabricService) {
         CreateOpenshiftContainerOptions.Builder builder = CreateOpenshiftContainerOptions.builder();
         String zookeeperUrl = fabricService.getZookeeperUrl();
         String zookeeperPassword = fabricService.getZookeeperPassword();
         builder = builder.zookeeperUrl(zookeeperUrl).zookeeperPassword(zookeeperPassword);
 
         Map<String, ?> properties = .getConfiguration();
 
         String serverUrl = validateProperty(properties,
                 "serverUrl",
                 "OPENSHIFT_BROKER_HOST",
                 .);
 
         String domain = validateProperty(properties,
                 "domain",
                 .,
                 "OPENSHIFT_NAMESPACE",
                 "");
 
         String login = validateProperty(properties,
                 "login",
                 .,
                "OPENSHIFT_LOGIN",
                "");
        String password = validateProperty(properties,
                "login",
                "OPENSHIFT_PASSWORD",
                "");
        if (Strings.isNotBlank(domain) && Strings.isNotBlank(login) && Strings.isNotBlank(password)) {
            .info("Using serverUrl: " + serverUrl + " domain: " + domain + " login: " + login);
            return builder.serverUrl(serverUrl).domain(domain).login(login).password(password);
        } else {
            return null;
        }
    }
    protected String validateProperty(Map<String, ?> propertiesString nameString propertyNameString envVarNameString defaultValue) {
        String answer = Maps.stringValue(propertiespropertyName, Systems.getEnvVar(envVarNamedefaultValue));
        if (Strings.isNullOrBlank(answer)) {
            .warn("No configured value for " + name + " in property " + propertyName + " or environment variable $" + envVarName);
        }
        return answer;
    }
    @Override
    public void destroyContainers(String profileint countList<Container> containers) {
        // TODO
    }
New to GrepCode? Check out our FAQ X