package com.ag.web.compute;

import com.ag.secp.platform.common.DockerCommon;
import com.spotify.docker.client.exceptions.DockerException;
import com.wb.util.DbUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.ResultSet;

/**
 * Created by shaoqiang on 2017/5/16.
 */
public class ComputeMange {

    public  static void createNetwork(HttpServletRequest request, HttpServletResponse response){

        String id=request.getParameter("id");
        String name=request.getParameter("name");
        String subset=request.getParameter("subset");
        String iprangle=request.getParameter("iprangle");
        String getway=request.getParameter("getway");

        DockerCommon common=new DockerCommon();
        common.creatNetworkAddrinfo(name,subset,iprangle,getway);


        System.out.println("aaaa");
    }

    public  static void createContainer(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String name=request.getParameter("name");
        String id=request.getParameter("id");
        String networkname=request.getParameter("networkname");
        String imageid=request.getParameter("imageid");
        String NETWORK_IP=request.getParameter("ip");
        String port,ports[]=null;

        if(request.getParameter("ports")!=null){

            port=request.getParameter("ports");
            ports=port.split(",");
        }
        DockerCommon common=new DockerCommon();
        String cid= common.creatContainers(NETWORK_IP,name,imageid,ports,networkname);
//        Map datamap=new HashMap();
//        datamap.put("cid",cid);
//        datamap.put("id",id);
//        DaoUtil.update("agCommon.updatecontainer",datamap);//更新容器id

//        String sql = " update d_container_info set cid='"+cid+"'\n" +
//                "        where  id ='"+id+"'";
//        ResultSet resultSet = (ResultSet) DbUtil.run(request,sql);

        System.out.println(cid);

    }

    /**
     * 启动容器
     * @param request
     * @param response
     */
    public  static void startContainer(HttpServletRequest request, HttpServletResponse response) {
        String cmd=request.getParameter("cmd");
        String cid=request.getParameter("cid");
        DockerCommon comms=new DockerCommon();
        try {
            comms.execContainerCmd(cmd,cid);
        } catch (DockerException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }



}
