<commit_before>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.entity.drivers.downloads.DownloadResolver;
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 {

    private String expandedInstallDir;

    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);
    }

    private 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<String> urls = resolver.getTargets();
        String saveAs = resolver.getFilename();
        expandedInstallDir = getInstallDir()+"/"+resolver.getUnpackedDirectoryName(format("redis-%s", getVersion()));

        /*
         * FIXME On jenkins releng3 box, needed to explicitly install jemalloc:
         *   wget http://www.canonware.com/download/jemalloc/jemalloc-3.3.0.tar.bz2
         *   tar -xvjpf jemalloc-3.3.0.tar.bz2 
         *   cd jemalloc-3.3.0
         *   ./configure
         *   make
         *   sudo make install
         *   cd ..
         *   
         *   cd redis-2.6.7
         *   make distclean
         *   cd deps; make hiredis lua jemalloc linenoise; cd ..
         *   "make LDFLAGS="-all-static"
         */

        List<String> commands = ImmutableList.<String>builder()
                .addAll(CommonCommands.downloadUrlAs(urls, saveAs))
                .add(CommonCommands.INSTALL_TAR)
                .add("tar xzfv " + saveAs)
                .add(format("cd redis-%s", getVersion()))
                .add("make distclean")
                .add("cd deps; make hiredis lua jemalloc linenoise; cd ..")
                .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", getExpandedInstallDir()),
                        "make install PREFIX="+getRunDir())
                .execute();
        
        getEntity().doExtraConfigurationDuringStart();
    }
    <commit_msg>Add jemalloc library to installed packages for Redis<commit_after>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.entity.drivers.downloads.DownloadResolver;
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 {

    private String expandedInstallDir;

    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);
    }

    private 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<String> urls = resolver.getTargets();
        String saveAs = resolver.getFilename();
        expandedInstallDir = getInstallDir()+"/"+resolver.getUnpackedDirectoryName(format("redis-%s", getVersion()));

        List<String> commands = ImmutableList.<String>builder()
                .addAll(CommonCommands.downloadUrlAs(urls, saveAs))
                .add(CommonCommands.INSTALL_TAR)
                .add(CommonCommands.installPackage(MutableMap.of("apt", "libjemalloc-dev", "yum", "jemalloc-devel", "port", null, "brew", null), null))
                .add("tar xzfv " + saveAs)
                .add(format("cd redis-%s", getVersion()))
                .add("make distclean")
                .add("cd deps; make hiredis lua jemalloc linenoise; cd ..")
                .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", getExpandedInstallDir()),
                        "make install PREFIX="+getRunDir())
                .execute();
        
        getEntity().doExtraConfigurationDuringStart();
    }
    
{'fraction_non_alphanumeric': 0.10692447529333994, 'fraction_numerical': 0.002478929102627665, 'mean_word_length': 2.5916913946587536, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '986118', 'n_tokens_mistral': 1731, 'n_tokens_neox': 1661, 'n_words': 335}