Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2005-2014 Red Hat, Inc. Red Hat licenses this file to you 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 java.net.URL;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 
 
 
 @Component(name = "io.fabric8.container.provider.openshift",
         configurationPid = "io.fabric8.openshift",
         label = "Fabric8 Openshift Container Provider", policy = ., immediate = true, metatype = false)
         @Property(name = "fabric.container.protocol", value = .)
 )
 
     public static final String PROPERTY_AUTOSCALE_SERVER_URL = "autoscale.server.url";
     public static final String PROPERTY_AUTOSCALE_LOGIN = "autoscale.login";
     public static final String PROPERTY_AUTOSCALE_PASSWORD = "autoscale.password";
     public static final String PROPERTY_AUTOSCALE_DOMAIN = "autoscale.domain";
 
     public static final String PREFIX_CARTRIDGE_ID = "id:";
 
     private static final transient Logger LOG = LoggerFactory.getLogger(OpenshiftContainerProvider.class);
     static final String SCHEME = "openshift";
 
     @Reference
     private Configurer configurer;
     @Reference(referenceInterface = IOpenShiftConnection.class, cardinality = .)
     @Reference(referenceInterface = FabricService.class)
    @Reference(referenceInterface = MBeanServer.class)
    private MBeanServer mbeanServer;
    @Property(name="default.cartridge.url", label = "Default Cartridge URL", value = "${default.cartridge.url}")
    private String defaultCartridgeUrl;
    private ObjectName objectName;
    private OpenShiftFacade mbean;
    private final ValidatingReference<Map<String, ?>> configuration = new ValidatingReference<Map<String, ?>>();
    @Activate
    void activate(Map<String, ?> configurationthrows Exception {
        updateConfiguration(configuration);
        .configure(configurationthis);
        activateComponent();
        if ( != null) {
             = new ObjectName("io.fabric8:type=OpenShift");
             = new OpenShiftFacade(this);
            if (!.isRegistered()) {
                .registerMBean();
            }
        }
    }
    @Modified
    void modified(Map<String, ?> configurationthrows Exception {
        updateConfiguration(configuration);
        .configure(configurationthis);
    }
    @Deactivate
        if ( != null) {
            if (.isRegistered()) {
                .unregisterMBean();
            }
        }
        deactivateComponent();
    }
    private void updateConfiguration(Map<String, ?> configuration) {
        this..bind(Collections.unmodifiableMap(new HashMap<StringObject>(configuration)));
    }
        return .get();
    }
    Map<String, ?> getConfiguration() {
        return .get();
    }
    @Override
        return CreateOpenshiftContainerOptions.builder();
    }
    @Override
        assertValid();
        IUser user = getOrCreateConnection(options).getUser();
        IDomain domain =  getOrCreateDomain(useroptions);
        String cartridgeUrl = null;
        Set<Stringprofiles = options.getProfiles();
        String versionId = options.getVersion();
        Map<StringStringopenshiftConfigOverlay = new HashMap<StringString>();
        if (profiles != null && versionId != null) {
            Version version = .get().getVersion(versionId);
            if (version != null) {
                for (String profileId : profiles) {
                    Profile profile = version.getProfile(profileId);
                    if (profile != null) {
                        Profile overlay = profile.getOverlay();
                        Map<StringStringopenshiftConfig = overlay.getConfiguration(.);
                        if (openshiftConfig != null)  {
                            openshiftConfigOverlay.putAll(openshiftConfig);
                        }
                    }
                }
            }
            cartridgeUrl = openshiftConfigOverlay.get("cartridge");
        }
        if (cartridgeUrl == null) {
            cartridgeUrl = ;
        }
        String[] cartridgeUrls = cartridgeUrl.split(" ");
        .info("Creating cartridges: " + cartridgeUrl);
        String standAloneCartridgeUrl = cartridgeUrls[0];
        StandaloneCartridge cartridge;
        if (standAloneCartridgeUrl.startsWith()) {
            cartridge = new StandaloneCartridge(standAloneCartridgeUrl.substring(.length()));
        } else {
            cartridge = new StandaloneCartridge(new URL(standAloneCartridgeUrl));
        }
        String zookeeperUrl = .get().getZookeeperUrl();
        String zookeeperPassword = .get().getZookeeperPassword();
        Map<String,StringuserEnvVars = null;
        if (!options.isEnsembleServer()) {
            userEnvVars = new HashMap<StringString>();
            userEnvVars.put("OPENSHIFT_FUSE_ZOOKEEPER_URL"zookeeperUrl);
            userEnvVars.put("OPENSHIFT_FUSE_ZOOKEEPER_PASSWORD"zookeeperPassword);
            String zkPasswordEncode = System.getProperty("zookeeper.password.encode""true");
            userEnvVars.put("OPENSHIFT_FUSE_ZOOKEEPER_PASSWORD_ENCODE"zkPasswordEncode);
        }
        String initGitUrl = null;
        int timeout = .;
        ApplicationScale scale = null;
        String containerName = options.getName();
        long t0 = System.currentTimeMillis();
        IApplication application;
        try {
            application = domain.createApplication(containerNamecartridgescalenew GearProfile(options.getGearProfile()), initGitUrltimeoutuserEnvVars);
        } catch (OpenShiftTimeoutException e) {
            long t1;
            do {
                Thread.sleep(5000);
                application = domain.getApplicationByName(containerName);
                if (application != null) {
                    break;
                }
                t1 = System.currentTimeMillis();
            } while (t1  - t0 < ..convert(15, .));
        }
        .info("Created application " + containerName);
        // now lets add all the embedded cartridges
        List<IEmbeddableCartridgelist = new ArrayList<IEmbeddableCartridge>();
        for (int idx = 1,  size = cartridgeUrls.lengthidx < sizeidx++) {
            String embeddedUrl = cartridgeUrls[idx];
            .info("Adding embedded cartridge: " + embeddedUrl);
            if (embeddedUrl.startsWith()) {
                list.add(new EmbeddableCartridge(embeddedUrl.substring(.length())));
            } else {
                list.add(new EmbeddableCartridge(new URL(embeddedUrl)));
            }
        }
        if (!list.isEmpty()) {
            application.addEmbeddableCartridges(list);
        }
        String gitUrl = application.getGitUrl();
        CreateOpenshiftContainerMetadata metadata = new CreateOpenshiftContainerMetadata(domain.getId(), application.getUUID(), application.getCreationLog(), gitUrl);
        metadata.setContainerName(containerName);
        metadata.setCreateOptions(options);
        return metadata;
    }
    @Override
    public void start(Container container) {
        assertValid();
        getContainerApplication(containertrue).start();
    }
    @Override
    public void stop(Container container) {
        assertValid();
        getContainerApplication(containertrue).stop();
    }
    @Override
    public void destroy(Container container) {
        assertValid();
        IApplication app = getContainerApplication(containerfalse);
        if (app != null) {
            app.destroy();
        }
    }
    @Override
    public String getScheme() {
        assertValid();
        return ;
    }
    @Override
        assertValid();
        return CreateOpenshiftContainerOptions.class;
    }
    @Override
        assertValid();
        return CreateOpenshiftContainerMetadata.class;
    }
    public List<StringgetDomains(String serverUrlString loginString password) {
        List<Stringanswer = new ArrayList<String>();
        IOpenShiftConnection connection = OpenShiftUtils.createConnection(serverUrlloginpassword);
        if (connection != null) {
            List<IDomaindomains = connection.getDomains();
            if (domains != null) {
                for (IDomain domain : domains) {
                    answer.add(domain.getId());
                }
            }
        }
        return answer;
    }
    public List<StringgetGearProfiles(String serverUrlString loginString password) {
        List<Stringanswer = new ArrayList<String>();
        IOpenShiftConnection connection = OpenShiftUtils.createConnection(serverUrlloginpassword);
        if (connection != null) {
            List<IDomaindomains = connection.getDomains();
            if (domains != null) {
                for (IDomain domain : domains) {
                    List<IGearProfilegearProfiles = domain.getAvailableGearProfiles();
                    for (IGearProfile gearProfile : gearProfiles) {
                        answer.add(gearProfile.getName());
                    }
                    // assume gears are the same on each domain
                    if (!answer.isEmpty()) {
                        break;
                    }
                }
            }
        }
        return answer;
    }
    private IApplication getContainerApplication(Container containerboolean required) {
        IApplication app = null;
        CreateOpenshiftContainerMetadata metadata = OpenShiftUtils.getContainerMetadata(container);
        if (metadata != null) {
            IOpenShiftConnection connection = getOrCreateConnection(metadata.getCreateOptions());
            app = OpenShiftUtils.getApplication(containermetadataconnection);
        }
        if (app == null && required) {
            throw new FabricException("Unable to find OpenShift application for " + container.getId());
        }
        return app;
    }

    
Gets a com.openshift.client.IDomain that matches the specified CreateOpenshiftContainerOptions. If no domain has been provided in the options the default domain is used. Else one is returned or created.
    private static IDomain getOrCreateDomain(IUser userCreateOpenshiftContainerOptions options)  {
        if (options.getDomain() == null || options.getDomain().isEmpty()) {
            return user.getDefaultDomain();
        } else {
            return domainExists(useroptions.getDomain()) ? user.getDomain(options.getDomain()) : user.createDomain(options.getDomain());
        }
    }

    
Checks if there is a com.openshift.client.IDomain matching the specified domainId.
    private static boolean domainExists(IUser userString domainId) {
        for (IDomain domain : user.getDomains()) {
            if (domainId.equals(domain.getId())) {
                return true;
            }
        }
        return false;
    }
        IOpenShiftConnection connection = .getOptional();
        if (connection != null) {
            return connection;
        } else {
            return OpenShiftUtils.createConnection(options);
        }
    }
    @Override
        return new OpenShiftAutoScaler(this);
    }
        this..bind(service);
    }
        this..unbind(service);
    }

    
Creates a name validator that checks there isn't an application of the given name already
        IUser user = getOrCreateConnection(options).getUser();
        final IDomain domain =  getOrCreateDomain(useroptions);
        return new NameValidator() {
            @Override
            public boolean isValid(String name) {
                IApplication application = domain.getApplicationByName(name);
                return application == null;
            }
        };
    }
    void bindFabricService(FabricService fabricService) {
        this..bind(fabricService);
    }
    void unbindFabricService(FabricService fabricService) {
        this..unbind(fabricService);
    }
New to GrepCode? Check out our FAQ X