package org.safehaus.subutai.core.peer.impl.container;


import java.util.concurrent.Callable;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

import org.safehaus.subutai.common.command.CommandResult;
import org.safehaus.subutai.common.command.CommandUtil;
import org.safehaus.subutai.common.command.RequestBuilder;
import org.safehaus.subutai.common.command.Response;
import org.safehaus.subutai.core.peer.api.ContainerDestructionException;
import org.safehaus.subutai.core.peer.api.ResourceHost;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;


public class DestroyContainerTask implements Callable
{
    private static final String CONTAINER_DOES_NOT_EXIST = "Container \"%s\" does NOT exist";
    private static final String CONTAINER_DESTROYED = "Destruction of \"%s\" completed successfully";
    private static final int DESTROY_TIMEOUT = 180;

    private final ResourceHost resourceHost;
    private final String hostname;
    private CommandUtil commandUtil = new CommandUtil();


    public DestroyContainerTask( final ResourceHost resourceHost, final String hostname )
    {
        Preconditions.checkNotNull( resourceHost );
        Preconditions.checkArgument( !Strings.isNullOrEmpty( hostname ) );

        this.resourceHost = resourceHost;
        this.hostname = hostname;
    }


    @Override
    public Object call() throws Exception
    {
        final Semaphore semaphore = new Semaphore( 0 );

        final StringBuilder out = new StringBuilder();

        RequestBuilder destroyCommand =
                new RequestBuilder( "subutai destroy" ).withCmdArgs( Lists.newArrayList( hostname ) )
                                                       .withTimeout( DESTROY_TIMEOUT ).daemon();

        commandUtil.executeAsync( destroyCommand, resourceHost, new CommandUtil.StoppableCallback()
        {
            @Override
            public void onResponse( final Response response, final CommandResult commandResult )
            {
                out.append( commandResult.getStdOut() );
                if ( commandResult.getStdOut().contains( String.format( CONTAINER_DESTROYED, hostname ) )
                        || commandResult.getStdOut().contains( String.format( CONTAINER_DOES_NOT_EXIST, hostname ) ) )
                {
                    semaphore.release();
                    stop();
                }
            }
        } );


        if ( !semaphore.tryAcquire( DESTROY_TIMEOUT + 3, TimeUnit.SECONDS ) )
        {
            throw new ContainerDestructionException(
                    String.format( "Unexpected command result while destroying container: %s", out ) );
        }

        return null;
    }
}

@@ -92,383 +92,122 @@
 le;%0A
-import java.util.concurrent.Semaphore;%0Aimport java.util.concurrent.TimeUnit;%0A%0Aimport org.safehaus.subutai.common.command.CommandResult;%0Aimport org.safehaus.subutai.common.command.CommandUtil;%0Aimport org.safehaus.subutai.common.command.RequestBuilder;%0Aimport org.safehaus.subutai.common.command.Response;%0Aimport org.safehaus.subutai.core.peer.api.ContainerDestructionException
+%0Aimport org.safehaus.subutai.common.command.CommandUtil;%0Aimport org.safehaus.subutai.common.command.RequestBuilder
 ;%0Aim
@@ -443,16 +443,22 @@
 lable%0A%7B%0A
+    //
     priv
@@ -543,16 +543,22 @@
 exist%22;%0A
+    //
     priv
@@ -1209,24 +1209,34 @@
 ption%0A    %7B%0A
+        //
         fina
@@ -1276,24 +1276,26 @@
 hore( 0 );%0A%0A
+//
         fina
@@ -1570,20 +1570,83 @@
 UT )
-.daemon(
+;%0A%0A        commandUtil.execute( destroyCommand, resourceHost 
 );%0A%0A
+        //
     
@@ -1741,16 +1741,26 @@
 lback()%0A
+        //
         
@@ -1766,23 +1766,33 @@
  %7B%0A     
+   //
        
+     
 @Overrid
@@ -1794,16 +1794,25 @@
 erride%0A 
+       //
         
@@ -1810,24 +1810,25 @@
 /           
+ 
 public void 
@@ -1900,16 +1900,26 @@
 esult )%0A
+        //
         
@@ -1921,32 +1921,37 @@
          %7B%0A     
+   //
            out.a
@@ -1945,16 +1945,21 @@
         
+     
 out.appe
@@ -1987,24 +1987,34 @@
 StdOut() );%0A
+        //
             
@@ -2099,32 +2099,42 @@
 D, hostname ) )%0A
+        //
                 
@@ -2220,16 +2220,27 @@
 T_EXIST,
+%0A        //
  hostnam
@@ -2243,24 +2243,34 @@
 tname ) ) )%0A
+        //
             
@@ -2274,32 +2274,35 @@
        %7B%0A       
+ //
              sem
@@ -2298,16 +2298,23 @@
         
+       
 semaphor
@@ -2323,24 +2323,33 @@
 release();%0A 
+       //
             
@@ -2355,16 +2355,17 @@
         
+ 
 stop();%0A
@@ -2363,24 +2363,31 @@
  stop();%0A   
+     //
             
@@ -2391,22 +2391,35 @@
     
+   
 %7D%0A     
+   //
        
+     
 %7D%0A  
@@ -2428,15 +2428,35 @@
     
-%7D );%0A%0A%0A
+//        %7D );%0A%0A%0A        //
     
@@ -2529,34 +2529,54 @@
 NDS ) )%0A        
-%7B%0A
+//        %7B%0A        //
             thro
@@ -2612,16 +2612,26 @@
 eption(%0A
+        //
         
@@ -2722,24 +2722,34 @@
 %22, out ) );%0A
+        //
         %7D%0A%0A 

Fix bug with IP & destroy