<commit_before>package org.safehaus.subutai.plugin.sqoop.impl.handler;

import java.util.Arrays;
import java.util.HashSet;
import org.safehaus.subutai.api.commandrunner.Command;
import org.safehaus.subutai.api.commandrunner.RequestBuilder;
import org.safehaus.subutai.api.dbmanager.DBException;
import org.safehaus.subutai.common.protocol.Agent;
import org.safehaus.subutai.common.tracker.ProductOperation;
import org.safehaus.subutai.plugin.common.PluginDAO;
import org.safehaus.subutai.plugin.sqoop.api.SqoopConfig;
import org.safehaus.subutai.plugin.sqoop.impl.CommandFactory;
import org.safehaus.subutai.plugin.sqoop.impl.CommandType;
import org.safehaus.subutai.plugin.sqoop.impl.SqoopImpl;

public class DestroyNodeHandler extends AbstractHandler {

    public DestroyNodeHandler(SqoopImpl manager, String clusterName, ProductOperation po) {
        super(manager, clusterName, po);
    }

    @Override
    public void run() {
        ProductOperation po = productOperation;
        SqoopConfig config = getClusterConfig();
        if(config == null) {
            po.addLogFailed("Sqoop installation not found: " + clusterName);
            return;
        }
        Agent agent = manager.getAgentManager().getAgentByHostname(hostname);
        if(agent == null) {
            po.addLogFailed("Node is not connected");
            return;
        }
        if(!config.getNodes().contains(agent)) {
            po.addLogFailed("Node does not belong to Sqoop installation group");
            return;
        }

        String s = CommandFactory.build(CommandType.PURGE, null);
        Command cmd = manager.getCommandRunner().createCommand(
                new RequestBuilder(s),
                new HashSet<>(Arrays.asList(agent)));

        manager.getCommandRunner().runCommand(cmd);

        if(cmd.hasSucceeded()) {
            po.addLog("Sqoop successfully removed from " + hostname);
            config.getNodes().remove(agent);

            try {
                PluginDAO dao = manager.getPluginDao();
                if(config.getNodes().isEmpty()) {
                    dao.deleteInfo(SqoopConfig.PRODUCT_KEY, config.getClusterName());
                    po.addLogDone("Installation info deleted");
                } else {
                    dao.saveInfo(SqoopConfig.PRODUCT_KEY, config.getClusterName(), config);
                    po.addLogDone("Installation info updated");
                }
            } catch(DBException ex) {
                String m = "Failed to update installation info";
                po.addLogFailed(m);
                manager.getLogger().error(m, ex);
            }

        } else {
            po.addLog(cmd.getAllErrors());
            po.addLogFailed("Failed to remove Sqoop from node");
        }

    }

}<commit_msg>SUBUTAI-1068: destroy nodes when Sqoop removed from all nodes<commit_after>package org.safehaus.subutai.plugin.sqoop.impl.handler;

import java.util.Arrays;
import java.util.HashSet;
import org.safehaus.subutai.api.commandrunner.Command;
import org.safehaus.subutai.api.commandrunner.RequestBuilder;
import org.safehaus.subutai.api.dbmanager.DBException;
import org.safehaus.subutai.api.lxcmanager.LxcDestroyException;
import org.safehaus.subutai.common.protocol.Agent;
import org.safehaus.subutai.common.tracker.ProductOperation;
import org.safehaus.subutai.plugin.common.PluginDAO;
import org.safehaus.subutai.plugin.sqoop.api.SqoopConfig;
import org.safehaus.subutai.plugin.sqoop.impl.CommandFactory;
import org.safehaus.subutai.plugin.sqoop.impl.CommandType;
import org.safehaus.subutai.plugin.sqoop.impl.SqoopImpl;

public class DestroyNodeHandler extends AbstractHandler {

    public DestroyNodeHandler(SqoopImpl manager, String clusterName, ProductOperation po) {
        super(manager, clusterName, po);
    }

    @Override
    public void run() {
        ProductOperation po = productOperation;
        SqoopConfig config = getClusterConfig();
        if(config == null) {
            po.addLogFailed("Sqoop installation not found: " + clusterName);
            return;
        }
        Agent agent = manager.getAgentManager().getAgentByHostname(hostname);
        if(agent == null) {
            po.addLogFailed("Node is not connected");
            return;
        }
        if(!config.getNodes().contains(agent)) {
            po.addLogFailed("Node does not belong to Sqoop installation group");
            return;
        }

        String s = CommandFactory.build(CommandType.PURGE, null);
        Command cmd = manager.getCommandRunner().createCommand(
                new RequestBuilder(s),
                new HashSet<>(Arrays.asList(agent)));

        manager.getCommandRunner().runCommand(cmd);

        if(cmd.hasSucceeded()) {
            po.addLog("Sqoop successfully removed from " + hostname);
            config.getNodes().remove(agent);

            try {
                PluginDAO dao = manager.getPluginDao();
                if(config.getNodes().isEmpty()) {

                    destroyNodes(config);

                    dao.deleteInfo(SqoopConfig.PRODUCT_KEY, config.getClusterName());
                    po.addLogDone("Installation info deleted");
                } else {
                    dao.saveInfo(SqoopConfig.PRODUCT_KEY, config.getClusterName(), config);
                    po.addLogDone("Installation info updated");
                }
            } catch(DBException ex) {
                String m = "Failed to update installation info";
                po.addLogFailed(m);
                manager.getLogger().error(m, ex);
            }

        } else {
            po.addLog(cmd.getAllErrors());
            po.addLogFailed("Failed to remove Sqoop from node");
        }

    }

    private void destroyNodes(SqoopConfig config) {
        if(config.getHadoopNodes() == null || config.getHadoopNodes().isEmpty())
            return;

        productOperation.addLog("Destroying nodes...");
        try {
            manager.getContainerManager().clonesDestroy(config.getHadoopNodes());
            manager.getLogger().info("Destroyed {} node(s)", config.getHadoopNodes().size());
            productOperation.addLog("Nodes successfully destroyed");
        } catch(LxcDestroyException ex) {
            String m = "Failed to detroy node(s)";
            productOperation.addLog(m + ": " + ex.getMessage());
            manager.getLogger().error(m, ex);
        }
    }

}
{'fraction_non_alphanumeric': 0.109375, 'fraction_numerical': 0.000625, 'mean_word_length': 2.4506738544474396, 'pattern_counts': {'":': 1, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6006041', 'n_tokens_mistral': 1807, 'n_tokens_neox': 1759, 'n_words': 311}