package org.safehaus.subutai.impl.flume.handler;

import org.safehaus.subutai.api.commandrunner.AgentResult;
import org.safehaus.subutai.api.commandrunner.Command;
import org.safehaus.subutai.api.commandrunner.RequestBuilder;
import org.safehaus.subutai.api.flume.Config;
import org.safehaus.subutai.impl.flume.CommandType;
import org.safehaus.subutai.impl.flume.Commands;
import org.safehaus.subutai.impl.flume.FlumeImpl;
import org.safehaus.subutai.shared.operation.AbstractOperationHandler;
import org.safehaus.subutai.shared.operation.ProductOperation;
import org.safehaus.subutai.shared.protocol.Agent;

public class UninstallHandler extends AbstractOperationHandler<FlumeImpl> {

    public UninstallHandler(FlumeImpl manager, String clusterName) {
        super(manager, clusterName);
        this.productOperation = manager.getTracker().createProductOperation(
                Config.PRODUCT_KEY, "Destroy cluster " + clusterName);
    }

    @Override
    public void run() {
        ProductOperation po = productOperation;
        Config config = manager.getCluster(clusterName);
        if(config == null) {
            po.addLogFailed("Cluster does not exist: " + clusterName);
            return;
        }

        po.addLog("Uninstalling Flume...");

        Command cmd = manager.getCommandRunner().createCommand(
                new RequestBuilder(Commands.make(CommandType.PURGE)),
                config.getNodes());
        manager.getCommandRunner().runCommand(cmd);

        if(cmd.hasCompleted()) {
            for(Agent agent : config.getNodes()) {
                AgentResult result = cmd.getResults().get(agent.getUuid());
                if(result.getExitCode() != null && result.getExitCode() == 0)
                    if(result.getStdOut().contains("Flume is not installed"))
                        po.addLog("Flume not installed on " + agent.getHostname());
                    else
                        po.addLog(String.format("Flume removed from node %s",
                                agent.getHostname()));
                else
                    po.addLog(String.format("Error on node %s: %s",
                            agent.getHostname(), result.getStdErr()));
            }

            po.addLog("Updating db...");
            if(manager.getDbManager().deleteInfo(Config.PRODUCT_KEY, config.getClusterName()))
                po.addLogDone("Cluster info deleted from DB\nDone");
            else
                po.addLogFailed("Error while deleting cluster info from DB. Check logs.\nFailed");
        } else {
            po.addLog(cmd.getAllErrors());
            po.addLogFailed("Uninstallation failed");
        }
    }

}

@@ -1206,32 +1206,448 @@
 urn;%0A        %7D%0A%0A
+        // check if nodes are connected%0A        for(Agent a : config.getNodes()) %7B%0A            Agent agent = manager.getAgentManager().getAgentByHostname(a.getHostname());%0A            if(agent == null) %7B%0A                po.addLogFailed(String.format(%0A                        %22Node %25s is not connected. Operations aborted.%22,%0A                        a.getHostname()));%0A                return;%0A            %7D%0A        %7D%0A%0A
         po.addLo

check if nodes are connected in uninstall operation