package com.dianping.paas.agent.service.impl;

import com.dianping.paas.agent.service.DockerContainerService;
import com.dianping.paas.core.dto.request.InstanceRestartRequest;
import com.dianping.paas.core.dto.request.InstanceStartRequest;
import com.dianping.paas.core.dto.response.InstanceRestartResponse;
import com.dianping.paas.core.dto.response.InstanceStartResponse;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.CreateContainerResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * Created by yuchao on 12/4/15.
 */
@Component
public class DockerContainerServiceImpl implements DockerContainerService {

    private static final Logger logger = LogManager.getLogger(DockerContainerServiceImpl.class);
    @Resource
    @SuppressWarnings("SpringJavaAutowiringInspection")
    private DockerClient dockerClient;

    public boolean createContainer(InstanceStartRequest request, InstanceStartResponse response) {
        logger.info(String.format("begin createContainer: %s", request));

        String imageId = request.getImageId();
        String containerId = null;
        try {
            CreateContainerResponse createContainerResponse = dockerClient.createContainerCmd(imageId).withCmd("touch", "/test").exec();
            containerId = createContainerResponse.getId();
            response.setContainerId(containerId);
            response.success();
        } catch (Exception e) {
            response.fail(e.toString());
            logger.error(String.format("error createContainer: %s", request), e);
        }

        if (containerId == null) {
            response.fail("create container failed!");
        }

        logger.info(String.format("end createContainer: %s", response));

        return response.isSuccess();
    }

    public void startContainer(InstanceStartRequest request, InstanceStartResponse response) {
        logger.info(String.format("begin startContainer: %s", request));

        try {
            dockerClient.startContainerCmd(response.getContainerId()).exec();
            response.success();
        } catch (Exception e) {
            response.fail(e.toString());
            logger.error(String.format("error startContainer: %s", request), e);
        }

        logger.info(String.format("end startContainer: %s", response));
    }

    public void restartContainer(InstanceRestartRequest request, InstanceRestartResponse response) {
        try {
            dockerClient.restartContainerCmd(request.getContainerId()).exec();
            response.success();
        } catch (Exception e) {
            response.fail(e.toString());
            logger.error(String.format("restartInstance error: %s", request), e);
        }
    }
}

@@ -1236,17 +1236,10 @@
 erId
- = null
 ;%0A
+
     
@@ -1494,16 +1494,169 @@
 nerId);%0A
+%0A            if (containerId == null) %7B%0A                response.fail(%22create container failed, created containerId is null!%22);%0A            %7D else %7B%0A    
         
@@ -1671,32 +1671,46 @@
 onse.success();%0A
+            %7D%0A
         %7D catch 
@@ -1863,108 +1863,8 @@
  %7D%0A%0A
-        if (containerId == null) %7B%0A            response.fail(%22create container failed!%22);%0A        %7D%0A
 %0A   
@@ -2608,32 +2608,106 @@
 nse response) %7B%0A
+        logger.info(String.format(%22begin startContainer: %25s%22, request));%0A%0A
         try %7B%0A  
@@ -2962,32 +2962,32 @@
 , request), e);%0A
-
         %7D%0A    %7D%0A
@@ -2975,21 +2975,94 @@
 );%0A        %7D
+%0A%0A        logger.info(String.format(%22end startContainer: %25s%22, response));
 %0A    %7D%0A%7D%0A

add log in restartContainer