All Downloads are FREE. Search and download functionalities are using the official Maven repository.

brooklyn.entity.osgi.karaf.KarafSshDriver Maven / Gradle / Ivy

There is a newer version: 0.7.0-M1
Show newest version
package brooklyn.entity.osgi.karaf;

import static java.lang.String.format;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import brooklyn.entity.basic.lifecycle.CommonCommands;
import brooklyn.entity.drivers.downloads.DownloadResolver;
import brooklyn.entity.java.JavaSoftwareProcessSshDriver;
import brooklyn.location.basic.SshMachineLocation;
import brooklyn.util.NetworkUtils;

import com.google.common.collect.ImmutableList;

public class KarafSshDriver extends JavaSoftwareProcessSshDriver implements KarafDriver {

    protected String expandedInstallDir;

    // TODO getJmxJavaSystemProperties(), don't set via JAVA_OPTS; set ourselves manually
    // (karaf reads from props files)
    // but do set "java.rmi.server.hostname"

    public KarafSshDriver(KarafContainerImpl entity, SshMachineLocation machine) {
        super(entity, machine);
    }

    @Override
    public KarafContainerImpl getEntity() {
        return (KarafContainerImpl) super.getEntity();
    }

    @Override
    protected String getLogFileLocation() {
        return format("{%s}/data/karaf.out", getRunDir());
    }

    protected String getExpandedInstallDir() {
        if (expandedInstallDir == null) throw new IllegalStateException("expandedInstallDir is null; most likely install was not called");
        return expandedInstallDir;
    }
    
    @Override
    public void install() {
        DownloadResolver resolver = entity.getManagementContext().getEntityDownloadsManager().newDownloader(this);
        List urls = resolver.getTargets();
        String saveAs = resolver.getFilename();
        expandedInstallDir = getInstallDir()+"/"+resolver.getUnpackedDirectoryName(format("apache-karaf-%s", getVersion()));
        
        List commands = ImmutableList.builder()
                .addAll(CommonCommands.downloadUrlAs(urls, saveAs))
                .add(CommonCommands.INSTALL_TAR)
                .add("tar xzfv " + saveAs)
                .build();
        
        newScript(INSTALLING)
                .failOnNonZeroResultCode()
                .body.append(commands)
                .execute();
    }

    @Override
    public void customize() {
        Map ports = new HashMap();
        ports.put("jmxPort", getJmxPort());
        ports.put("rmiServerPort", getRmiServerPort());

        NetworkUtils.checkPortsValid(ports);
        newScript(CUSTOMIZING).
                body.append(
                format("cd %s", getRunDir()),
                format("cp -R %s/{bin,etc,lib,system,deploy} . || exit $!", getExpandedInstallDir()),
                format("sed -i.bk 's/rmiRegistryPort = 1099/rmiRegistryPort = %s/g' etc/org.apache.karaf.management.cfg", getJmxPort()),
                format("sed -i.bk 's/rmiServerPort = 44444/rmiServerPort = %s/g' etc/org.apache.karaf.management.cfg", getRmiServerPort())
        ).execute();
    }

    @Override
    public void launch() {
        Map flags = new HashMap();
        flags.put("usePidFile", true);

        newScript(flags, LAUNCHING).
                body.append(
                "nohup ./bin/start"
        ).execute();
    }

    @Override
    public boolean isRunning() {
        // TODO Can we use the pidFile, auto-generated by launch?

        Integer pid = entity.getAttribute(KarafContainer.KARAF_PID);
        // This method is called on startup, before JMX is initialised, so pid won't always be available.
        if (pid != null) {
            return newScript(CHECK_RUNNING).
                    body.append(
                    format("ps aux | grep 'karaf' | grep %s > /dev/null", pid)
            ).execute() == 0;
        } else {
            // Simple method isn't available, use pid in instance.properties.
            return newScript(CHECK_RUNNING).
                    body.append(
                    format("cd %s/instances/",getRunDir()),
                    "[ $(uname) = \"Darwin\" ] && pid=$(sed -n -e 's/.*pid=\\([0-9]*\\)$/\\1/p' instance.properties) || pid=$(sed -r -n -e 's/.*pid=([0-9]*)$/\\1/p' instance.properties)",
                    "ps aux | grep 'karaf' | grep $(echo ${pid:-X}) > /dev/null"
            ).execute() == 0;
        }
    }

    @Override
    public void stop() {
        newScript(STOPPING).
                body.append(
                format("%s/bin/stop",getRunDir())
        ).execute();
    }

    @Override
    public void kill() {
        stop(); // TODO no pid file to easily do `kill -9`
    }

    @Override
    protected List getCustomJavaConfigOptions() {
        List result = new LinkedList();
        result.addAll(super.getCustomJavaConfigOptions());
        result.add("-Xms200m");
        result.add("-Xmx800m");
        result.add("-XX:MaxPermSize=400m");
        return result;
    }

    // FIXME Playing around / experimenting with these jmx system properties to try to get it to work!
    // With com.sun.management.jmxremote.port, then the Karaf container won't start at all; with that one removed then we just get "Not connected to JMX for entity Venue[id=ad7a6e47-e738-4687-b3bf-b9ad3aae449d,displayName=Venue:ad7a]"
    @Override
    protected Map getJmxJavaSystemProperties() {
//        Map result = super.getJmxJavaSystemProperties()
//        result.remove("com.sun.management.jmxremote.port")
//        return result

        Map result  = new HashMap();
        result.put("java.rmi.server.hostname",getMachine().getAddress().getHostName());
        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy