Search in sources :

Example 21 with Container

use of io.fabric8.api.Container in project fabric8 by jboss-fuse.

the class JoinAction method doExecute.

@Override
protected Object doExecute() throws Exception {
    if (nonManaged) {
        profile = "unmanaged";
    }
    String oldName = runtimeProperties.getRuntimeIdentity();
    if (System.getenv("OPENSHIFT_BROKER_HOST") != null && containerName != null) {
        System.err.println("Containers in OpenShift cannot be renamed");
        return null;
    }
    if (containerName == null) {
        containerName = oldName;
    }
    FabricValidations.validateContainerName(containerName);
    Configuration bootConfiguration = configAdmin.getConfiguration(BootstrapConfiguration.COMPONENT_PID, null);
    Configuration dataStoreConfiguration = configAdmin.getConfiguration(Constants.DATASTORE_PID, null);
    Configuration configZook = configAdmin.getConfiguration(Constants.ZOOKEEPER_CLIENT_PID, null);
    if (configZook.getProperties() != null && configZook.getProperties().get("zookeeper.url") != null) {
        System.err.println("This container is already connected to a fabric");
        return null;
    }
    Dictionary<String, Object> bootProperties = bootConfiguration.getProperties();
    if (bootProperties == null) {
        bootProperties = new Hashtable<>();
    }
    if (resolver != null) {
        bootProperties.put(ZkDefs.LOCAL_RESOLVER_PROPERTY, resolver);
    }
    if (manualIp != null) {
        bootProperties.put(ZkDefs.MANUAL_IP, manualIp);
    }
    if (bindAddress != null) {
        bootProperties.put(ZkDefs.BIND_ADDRESS, bindAddress);
    }
    zookeeperPassword = zookeeperPassword != null ? zookeeperPassword : ShellUtils.retrieveFabricZookeeperPassword(session);
    if (zookeeperPassword == null) {
        zookeeperPassword = promptForZookeeperPassword();
    }
    if (zookeeperPassword == null || zookeeperPassword.isEmpty()) {
        System.out.println("No password specified. Cannot join fabric ensemble.");
        return null;
    }
    ShellUtils.storeZookeeperPassword(session, zookeeperPassword);
    log.debug("Encoding ZooKeeper password.");
    String encodedPassword = PasswordEncoder.encode(zookeeperPassword);
    bootProperties.put(ZkDefs.MINIMUM_PORT, String.valueOf(minimumPort));
    bootProperties.put(ZkDefs.MAXIMUM_PORT, String.valueOf(maximumPort));
    Hashtable<String, Object> dataStoreProperties = new Hashtable<String, Object>();
    Configuration cfg = configAdmin.getConfiguration(Constants.DATASTORE_PID, null);
    Dictionary<String, Object> props = cfg.getProperties();
    if (props != null) {
        for (Enumeration<String> keys = cfg.getProperties().keys(); keys.hasMoreElements(); ) {
            String k = keys.nextElement();
            dataStoreProperties.put(k, cfg.getProperties().get(k));
        }
    }
    augmentDataStoreProperties(zookeeperPassword, dataStoreProperties);
    if (!containerName.equals(oldName)) {
        if (force || permissionToRenameContainer()) {
            if (!registerContainer(containerName, zookeeperPassword, profile, force)) {
                System.err.println("A container with the name: " + containerName + " is already member of the cluster. You can specify a different name as an argument.");
                return null;
            }
            bootProperties.put(SystemProperties.KARAF_NAME, containerName);
            // Ensure that if we bootstrap CuratorFramework via RuntimeProperties password is set before the URL.
            bootProperties.put("zookeeper.password", encodedPassword);
            bootProperties.put("zookeeper.url", zookeeperUrl);
            // Rename the container
            Path propsPath = runtimeProperties.getConfPath().resolve("system.properties");
            Properties systemProps = new Properties(propsPath.toFile());
            systemProps.put(SystemProperties.KARAF_NAME, containerName);
            // Also pass zookeeper information so that the container can auto-join after the restart.
            systemProps.put("zookeeper.url", zookeeperUrl);
            systemProps.put("zookeeper.password", encodedPassword);
            systemProps.save();
            System.setProperty("runtime.id", containerName);
            System.setProperty(SystemProperties.KARAF_NAME, containerName);
            System.setProperty("karaf.restart", "true");
            System.setProperty("karaf.restart.clean", "false");
            if (!nonManaged) {
                installBundles();
            }
            // it's only a(n almost certain) way of synchronizing CM and ManagedService.update()
            if (!OsgiUtils.updateCmConfigurationAndWait(bundleContext, bootConfiguration, bootProperties, 10, TimeUnit.SECONDS)) {
                log.warn("Timeout waiting for update of PID: {}", BootstrapConfiguration.COMPONENT_PID);
            }
            if (!OsgiUtils.updateCmConfigurationAndWait(bundleContext, dataStoreConfiguration, dataStoreProperties, 10, TimeUnit.SECONDS)) {
                log.warn("Timeout waiting for update of PID: {}", Constants.DATASTORE_PID);
            }
            // we don't want fileinstall to trigger ConfigAdmin update
            Bundle fileinstall = new BundleUtils(bundleContext).findBundle("org.apache.felix.fileinstall");
            if (fileinstall != null) {
                fileinstall.stop(Bundle.STOP_TRANSIENT);
            }
            persistConfiguration(configAdmin, Constants.DATASTORE_PID, dataStoreProperties);
            // Restart the container
            bundleContext.getBundle(0).stop();
            return null;
        } else {
            return null;
        }
    } else {
        bootConfiguration.update(bootProperties);
        dataStoreConfiguration.update(dataStoreProperties);
        persistConfiguration(configAdmin, Constants.DATASTORE_PID, dataStoreProperties);
        if (!registerContainer(containerName, zookeeperPassword, profile, force)) {
            System.err.println("A container with the name: " + containerName + " is already member of the cluster. You can specify a different name as an argument.");
            return null;
        }
        Configuration config = configAdmin.getConfiguration(Constants.ZOOKEEPER_CLIENT_PID, null);
        Hashtable<String, Object> properties = new Hashtable<String, Object>();
        properties.put("zookeeper.url", zookeeperUrl);
        properties.put("zookeeper.password", PasswordEncoder.encode(encodedPassword));
        config.setBundleLocation(null);
        config.update(properties);
        if (!nonManaged) {
            installBundles();
        }
        return null;
    }
}
Also used : ZkPath(io.fabric8.zookeeper.ZkPath) Path(java.nio.file.Path) Configuration(org.osgi.service.cm.Configuration) BootstrapConfiguration(io.fabric8.zookeeper.bootstrap.BootstrapConfiguration) Hashtable(java.util.Hashtable) Bundle(org.osgi.framework.Bundle) SystemProperties(io.fabric8.api.SystemProperties) RuntimeProperties(io.fabric8.api.RuntimeProperties) Properties(org.apache.felix.utils.properties.Properties) BundleUtils(io.fabric8.utils.BundleUtils)

Example 22 with Container

use of io.fabric8.api.Container in project fabric8 by jboss-fuse.

the class WelcomeAction method doExecute.

@Override
protected Object doExecute() throws Exception {
    String name = runtimeProperties.getRuntimeIdentity();
    String appName = runtimeProperties.getProperty("karaf.app.name", "JBoss Fuse");
    System.out.println("Welcome to " + appName);
    System.out.println("");
    // are we part of fabric?
    if (fabricService != null) {
        Container container = fabricService.getCurrentContainer();
        if (container != null) {
            boolean ensemble = container.isEnsembleServer();
            if (ensemble) {
                System.out.println("This container \u001B[1m" + container.getId() + "\u001B[0m is a Fabric ensemble server.");
            } else {
                System.out.println("This container \u001B[1m" + container.getId() + "\u001B[0m is joined to an existing Fabric.");
            }
        } else {
            System.out.println("This container \u001B[1m" + name + "\u001B[0m is a standalone container.");
        }
        String url = fabricService.getWebConsoleUrl();
        if (url != null) {
            System.out.println("Web management console available at: \u001B[1m" + url + "\u001B[0m");
        }
        System.out.println("");
    } else {
        // no we are standalone
        System.out.println("This container \u001B[1m" + name + "\u001B[0m is a standalone container.");
        System.out.println("");
        System.out.println("Create a new Fabric via '\u001B[1mfabric:create\u001B[0m'");
        System.out.println("or join an existing Fabric via '\u001B[1mfabric:join [someUrls]\u001B[0m'");
        System.out.println("");
    }
    return null;
}
Also used : Container(io.fabric8.api.Container)

Example 23 with Container

use of io.fabric8.api.Container in project fabric8 by jboss-fuse.

the class AbstractChildContainerCreateAction method preCreateContainer.

/**
 * Pre validates input before creating the container(s)
 *
 * @param name the name of the container to create
 * @throws IllegalArgumentException is thrown if input is invalid
 */
protected void preCreateContainer(String name) throws IllegalArgumentException {
    FabricValidations.validateContainerName(name);
    if (clusterService.getEnsembleContainers().isEmpty()) {
        return;
    }
    if (FabricCommand.doesContainerExist(fabricService, name)) {
        throw new IllegalArgumentException("A container with name " + name + " already exists.");
    }
    // get the profiles for the given version
    Version ver = version != null ? profileService.getRequiredVersion(version) : fabricService.getRequiredDefaultVersion();
    List<Profile> profiles = ver.getProfiles();
    // validate profiles exists before creating a new container
    Set<String> names = getProfileNames();
    for (String profile : names) {
        Profile prof = getProfile(profiles, profile, ver);
        if (prof == null) {
            throw new IllegalArgumentException("Profile " + profile + " with version " + ver.getId() + " does not exist.");
        }
        if (prof.isAbstract()) {
            throw new IllegalArgumentException("Profile " + profile + " with version " + ver.getId() + " is abstract and can not be associated to containers.");
        }
    }
    if (fabricService.getZookeeperUrl() == null) {
        throw new IllegalArgumentException("should start a zookeeper ensemble first.");
    }
}
Also used : Version(io.fabric8.api.Version) Profile(io.fabric8.api.Profile)

Example 24 with Container

use of io.fabric8.api.Container in project fabric8 by jboss-fuse.

the class AbstractContainerCreateAction method preCreateContainer.

/**
 * Pre validates input before creating the container(s)
 *
 * @param name the name of the container to create
 * @throws IllegalArgumentException is thrown if input is invalid
 */
protected void preCreateContainer(String name) throws IllegalArgumentException {
    FabricValidations.validateContainerName(name);
    if (!isEnsembleServer) {
        if (clusterService.getEnsembleContainers().isEmpty()) {
            if (!isEnsembleServer) {
                throw new IllegalStateException("The use of the --ensemble-server option is mandatory when creating an initial container");
            }
            return;
        }
        if (FabricCommand.doesContainerExist(fabricService, name)) {
            throw new IllegalArgumentException("A container with name " + name + " already exists.");
        }
        // get the profiles for the given version
        Version ver = version != null ? profileService.getRequiredVersion(version) : fabricService.getRequiredDefaultVersion();
        List<Profile> profiles = ver.getProfiles();
        // validate profiles exists before creating a new container
        Set<String> names = getProfileNames();
        for (String profile : names) {
            Profile prof = getProfile(profiles, profile, ver);
            if (prof == null) {
                throw new IllegalArgumentException("Profile " + profile + " with version " + ver.getId() + " does not exist.");
            }
            if (prof.isAbstract()) {
                throw new IllegalArgumentException("Profile " + profile + " with version " + ver.getId() + " is abstract and can not be associated to containers.");
            }
        }
    }
    if (!isEnsembleServer && fabricService.getZookeeperUrl() == null) {
        throw new IllegalArgumentException("Either start a zookeeper ensemble or use --ensemble-server.");
    }
}
Also used : Version(io.fabric8.api.Version) Profile(io.fabric8.api.Profile)

Example 25 with Container

use of io.fabric8.api.Container in project fabric8 by jboss-fuse.

the class ContainerDomainsAction method doExecute.

protected Object doExecute() throws Exception {
    validateContainerName(container);
    Container found = FabricCommand.getContainer(fabricService, container);
    List<String> domains = found.getJmxDomains();
    for (String domain : domains) {
        System.out.println(domain);
    }
    return null;
}
Also used : Container(io.fabric8.api.Container)

Aggregations

Container (io.fabric8.api.Container)139 Test (org.junit.Test)75 FabricService (io.fabric8.api.FabricService)56 ArrayList (java.util.ArrayList)39 Container (io.fabric8.kubernetes.api.model.Container)38 IOException (java.io.IOException)38 Profile (io.fabric8.api.Profile)37 HashMap (java.util.HashMap)34 Map (java.util.Map)30 FabricException (io.fabric8.api.FabricException)27 BundleContext (org.osgi.framework.BundleContext)24 Version (io.fabric8.api.Version)23 File (java.io.File)23 HashSet (java.util.HashSet)20 LinkedList (java.util.LinkedList)17 PodSpec (io.fabric8.kubernetes.api.model.PodSpec)16 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)16 CreateContainerMetadata (io.fabric8.api.CreateContainerMetadata)15 Pod (io.fabric8.kubernetes.api.model.Pod)12 List (java.util.List)12