package nl.jpoint.vertx.mod.cluster.command;

import nl.jpoint.vertx.mod.cluster.Constants;
import nl.jpoint.vertx.mod.cluster.request.ModuleRequest;
import nl.jpoint.vertx.mod.cluster.util.LogConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vertx.java.core.AsyncResult;
import org.vertx.java.core.Handler;
import org.vertx.java.core.json.JsonObject;
import org.vertx.java.platform.PlatformManager;

import java.util.concurrent.atomic.AtomicInteger;

public class InstallModule implements Command<ModuleRequest> {


    private static final Logger LOG = LoggerFactory.getLogger(InstallModule.class);

    private final PlatformManager platformManager;

    public InstallModule(PlatformManager platformManager) {
        this.platformManager = platformManager;
    }

    @Override
    public JsonObject execute(final ModuleRequest request) {

        final AtomicInteger waitFor = new AtomicInteger(1);
        final JsonObject result = new JsonObject();
        LOG.info("[{} - {}]: Installing module {}", LogConstants.DEPLOY_REQUEST, request.getId().toString(), request.getModuleId());

        platformManager.installModule(request.getModuleId(), new Handler<AsyncResult<Void>>() {
            @Override
            public void handle(AsyncResult<Void> event) {
                if (event.failed()) {
                    LOG.error("[{} - {}]: Error installing module {} with cause {}", LogConstants.DEPLOY_REQUEST, request.getId().toString(), request.getModuleId(), event.cause().getMessage());
                    result.putBoolean(Constants.STATUS_SUCCESS, false);
                } else {
                    result.putBoolean(Constants.STATUS_SUCCESS, true);
                    LOG.info("[{} - {}]: Installed module {}", LogConstants.DEPLOY_REQUEST, request.getId().toString(), request.getModuleId());
                }
                waitFor.decrementAndGet();
            }
        });

        while (waitFor.intValue() != 0) {
            // Wait for install
        }
        return result;

    }
}

@@ -81,24 +81,88 @@
 .Constants;%0A
+import nl.jpoint.vertx.mod.cluster.request.DeployModuleRequest;%0A
 import nl.jp
@@ -487,16 +487,109 @@
 nager;%0A%0A
+import java.io.BufferedReader;%0Aimport java.io.IOException;%0Aimport java.io.InputStreamReader;%0A
 import j
@@ -789,358 +789,79 @@
     
-private final PlatformManager platformManager;%0A%0A    public InstallModule(PlatformManager platformManager) %7B%0A        this.platformManager = platformManager;%0A    %7D%0A%0A    @Override%0A    public JsonObject execute(final ModuleRequest request) %7B%0A%0A        final AtomicInteger waitFor = new AtomicInteger(1);%0A        final JsonObject result = new JsonObject();
+@Override%0A    public JsonObject execute(final ModuleRequest request) %7B%0A
 %0A   
@@ -898,32 +898,33 @@
 alling module %7B%7D
+.
 %22, LogConstants.
@@ -991,25 +991,24 @@
 eId());%0A
-%0A
         
 platform
@@ -1003,218 +1003,617 @@
     
-platformManager.installModule(request.getModuleId(), new Handler%3CAsy
+boolean success = false;%0A%0A        try %7B%0A            final Process runProcess = Runtime.getRuntime().exec(new String%5B%5D%7B%22/etc/init.d/vertx%22, %22install%22, request.getModuleId(), String.valueOf(((DeployModuleRequest) request).getInsta
 nc
-R
 es
-ult%3CVoid%3E%3E() %7B%0A            @Override%0A            public void handle(AsyncResult%3CVoid%3E event) %7B%0A                if (event.failed()) %7B%0A    
+())%7D);%0A            runProcess.waitFor();%0A%0A            int exitValue = runProcess.exitValue();%0A            if (exitValue == 0) %7B%0A                success = true;%0A            %7D%0A%0A            BufferedReader output = new BufferedReader(new InputStreamReader(runProcess.getInputStream()));%0A            String outputLine;%0A            while ((outputLine = output.readLine()) != null) %7B%0A
     
@@ -1632,13 +1632,12 @@
 LOG.
-error
+info
 (%22%5B%7B
@@ -1649,34 +1649,25 @@
 %7D%5D: 
-Error i
+I
 nstall
-ing m
+ M
 odule %7B%7D
  wit
@@ -1666,22 +1666,8 @@
 e %7B%7D
- with cause %7B%7D
 %22, L
@@ -1713,240 +1713,287 @@
 Id()
-.toString(), request.getModuleId(), event.cause().getMessage());%0A                    result.putBoolean(Constants.STATUS_SUCCESS, false);%0A                %7D else %7B%0A                    result.putBoolean(Constants.STATUS_SUCCESS, true);
+, outputLine);%0A            %7D%0A%0A            if (exitValue != 0) %7B%0A                BufferedReader errorOut = new BufferedReader(new InputStreamReader(runProcess.getErrorStream()));%0A                String errorLine;%0A                while ((errorLine = errorOut.readLine()) != null) %7B
 %0A   
@@ -2005,36 +2005,37 @@
             LOG.
-info
+error
 (%22%5B%7B%7D - %7B%7D%5D: Ins
@@ -2042,18 +2042,16 @@
 tall
-ed
  module 
 %7B%7D%22,
@@ -2042,24 +2042,31 @@
 tall module 
+failed 
 %7B%7D%22, LogCons
@@ -2106,119 +2106,244 @@
 Id()
-.toString(), request.getModuleId());%0A                %7D%0A                waitFor.decrementAndGet();%0A            %7D
+, errorLine);%0A                %7D%0A            %7D%0A        %7D catch (IOException %7C InterruptedException e) %7B%0A            LOG.error(%22%5B%7B%7D - %7B%7D%5D: Failed to install module %7B%7D%22, LogConstants.DEPLOY_REQUEST, request.getId(), request.getModuleId());
 %0A   
@@ -2348,18 +2348,16 @@
        %7D
-);
 %0A%0A      
@@ -2362,73 +2362,107 @@
     
-while (waitFor.intValue() != 0) %7B%0A            // Wait for install
+return new JsonObject()%0A                .putString(Constants.DEPLOY_ID, request.getId().toString())
 %0A   
@@ -2466,26 +2466,24 @@
         
-%7D%0A
         
 return r
@@ -2478,23 +2478,55 @@
     
-return result;%0A
+.putBoolean(Constants.STATUS_SUCCESS, success);
 %0A   

Use wrapper to install module