package brooklyn.entity.nosql.redis;

import static java.lang.String.format;

import java.util.List;

import brooklyn.entity.basic.AbstractSoftwareProcessSshDriver;
import brooklyn.entity.basic.lifecycle.CommonCommands;
import brooklyn.location.Location;
import brooklyn.location.basic.SshMachineLocation;
import brooklyn.util.MutableMap;

import com.google.common.collect.ImmutableList;

/**
 * Start a {@link RedisStore} in a {@link Location} accessible over ssh.
 */
public class RedisStoreSshDriver extends AbstractSoftwareProcessSshDriver implements RedisStoreDriver {

    public RedisStoreSshDriver(RedisStore entity, SshMachineLocation machine) {
        super(entity, machine);
    }

    @Override
    public RedisStore getEntity() {
        return (RedisStore) super.getEntity();
    }
    
    protected Integer getRedisPort() {
        return getEntity().getAttribute(RedisStore.REDIS_PORT);
    }

    @Override
    public void install() {
        String url = format("http://redis.googlecode.com/files/redis-%s.tar.gz", getVersion());
        String saveAs = format("redis-%s.tar.gz", getVersion());

        List<String> commands = ImmutableList.<String>builder()
                .addAll(CommonCommands.downloadUrlAs(url, getEntityVersionLabel("/"), saveAs))
                .add(CommonCommands.INSTALL_TAR)
                .add("tar xzfv " + saveAs)
                .add(format("cd redis-%s", getVersion()))
                .add("make LDFLAGS=\"-all-static\"")
                .build();

        newScript(INSTALLING)
                .failOnNonZeroResultCode()
                .body.append(commands).execute();
    }

    @Override
    public void customize() {
        newScript(MutableMap.of("usePidFile", false), CUSTOMIZING)
                .failOnNonZeroResultCode()
                .body.append(
                        format("cd %s/redis-%s", getInstallDir(), getVersion()),
                        "make install PREFIX="+getRunDir())
                .execute();
        
        getEntity().doExtraConfigurationDuringStart();
    }
    
    @Override
    public void launch() {
        // TODO Should we redirect stdout/stderr: format(" >> %s/console 2>&1 </dev/null &", getRunDir())
        newScript(MutableMap.of("usePidFile", false), LAUNCHING)
                .failOnNonZeroResultCode()
                .body.append("./bin/redis-server redis.conf")
                .execute();
    }
 

    @Override
    public boolean isRunning() {
        return newScript(MutableMap.of("usePidFile", false), CHECK_RUNNING)
                .body.append("./bin/redis-cli ping > /dev/null")
                .execute() == 0;
    }

    /**
     * Restarts redis with the current configuration.
     */
    @Override
    public void stop() {
        newScript(MutableMap.of("usePidFile", false), STOPPING)
                .failOnNonZeroResultCode()
                .body.append("./bin/redis-cli shutdown")
                .execute();
    }
}

@@ -1105,24 +1105,560 @@
 ersion());%0A%0A
+        /*%0A         * FIXME On jenkins releng3 box, needed to explicitly install jemalloc:%0A         *   wget http://www.canonware.com/download/jemalloc/jemalloc-3.3.0.tar.bz2%0A         *   tar -xvjpf jemalloc-3.3.0.tar.bz2 %0A         *   cd jemalloc-3.3.0%0A         *   ./configure%0A         *   make%0A         *   sudo make install%0A         *   cd ..%0A         *   %0A         *   cd redis-2.6.7%0A         *   make distclean%0A         *   cd deps; make hiredis lua jemalloc linenoise; cd ..%0A         *   %22make LDFLAGS=%22-all-static%22%0A         */%0A%0A
         List
@@ -1954,16 +1954,131 @@
 ion()))%0A
+                .add(%22make distclean%22)%0A                .add(%22cd deps; make hiredis lua jemalloc linenoise; cd ..%22)%0A
         

Fix Redis install (make distclean; build reps)