Search in sources :

Example 1 with SaturnJobConsoleException

use of com.vip.saturn.job.console.exception.SaturnJobConsoleException in project Saturn by vipshop.

the class MarathonRestClient method deploy.

public static void deploy(String userName, String password, ContainerConfig containerConfig) throws SaturnJobConsoleException {
    JSONObject deployFormModel = new JSONObject();
    deployFormModel.put("id", containerConfig.getTaskId());
    deployFormModel.put("instances", containerConfig.getInstances());
    deployFormModel.put("cmd", containerConfig.getCmd());
    deployFormModel.put("cpus", containerConfig.getCpus());
    deployFormModel.put("mem", containerConfig.getMem());
    deployFormModel.put("constraints", containerConfig.getConstraints());
    JSONObject container = new JSONObject();
    JSONObject docker = new JSONObject();
    docker.put("forcePullImage", containerConfig.getForcePullImage());
    docker.put("image", containerConfig.getImage());
    docker.put("network", "BRIDGE");
    docker.put("parameters", containerConfig.getParameters());
    docker.put("privileged", containerConfig.getPrivileged());
    JSONArray portMappings = new JSONArray();
    JSONObject portMapping = new JSONObject();
    // equal to jmx port
    portMapping.put("containerPort", 24500);
    portMapping.put("hostPort", 0);
    portMapping.put("servicePort", 0);
    portMapping.put("protocol", "tcp");
    portMappings.add(portMapping);
    docker.put("portMappings", portMappings);
    container.put("docker", docker);
    container.put("type", "DOCKER");
    container.put("volumes", containerConfig.getVolumes());
    deployFormModel.put("container", container);
    deployFormModel.put("env", containerConfig.getEnv());
    JSONArray healthChecks = new JSONArray();
    JSONObject healthCheck = new JSONObject();
    healthCheck.put("protocol", "TCP");
    healthChecks.add(healthCheck);
    deployFormModel.put("healthChecks", healthChecks);
    String urlStr = SaturnEnvProperties.VIP_SATURN_DCOS_REST_URI + "/v2/apps";
    CloseableHttpClient httpClient = HttpClients.createDefault();
    try {
        HttpPost httpPost = new HttpPost(urlStr);
        httpPost.setHeader("Authorization", "Basic " + Base64.encodeBase64String((userName + ":" + password).getBytes("UTF-8")));
        httpPost.setHeader("Content-type", "application/json; charset=utf-8");
        httpPost.setEntity(new StringEntity(deployFormModel.toJSONString()));
        CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
        StatusLine statusLine = httpResponse.getStatusLine();
        if (statusLine != null) {
            int statusCode = statusLine.getStatusCode();
            String reasonPhrase = statusLine.getReasonPhrase();
            if (statusCode == 200) {
            } else if (statusCode == 201) {
            } else {
                HttpEntity entity = httpResponse.getEntity();
                if (entity != null) {
                    String entityContent = getEntityContent(entity);
                    throw new SaturnJobConsoleException(entityContent);
                } else {
                    throw new SaturnJobConsoleException("statusCode is " + statusCode + ", reasonPhrase is " + reasonPhrase);
                }
            }
        } else {
            throw new SaturnJobConsoleException("Not status returned, url is " + urlStr);
        }
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
        throw new SaturnJobConsoleException(e);
    } finally {
        try {
            httpClient.close();
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
        }
    }
}
Also used : StatusLine(org.apache.http.StatusLine) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) StringEntity(org.apache.http.entity.StringEntity) JSONObject(com.alibaba.fastjson.JSONObject) HttpEntity(org.apache.http.HttpEntity) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) JSONArray(com.alibaba.fastjson.JSONArray) IOException(java.io.IOException)

Example 2 with SaturnJobConsoleException

use of com.vip.saturn.job.console.exception.SaturnJobConsoleException in project Saturn by vipshop.

the class MarathonRestClient method count.

public static int count(String userName, String password, String appId) throws SaturnJobConsoleException {
    String urlStr = SaturnEnvProperties.VIP_SATURN_DCOS_REST_URI + "/v2/apps/" + appId + "/tasks";
    CloseableHttpClient httpClient = HttpClients.createDefault();
    try {
        HttpGet httpGet = new HttpGet(urlStr);
        httpGet.setHeader("Authorization", "Basic " + Base64.encodeBase64String((userName + ":" + password).getBytes("UTF-8")));
        CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity entity = httpResponse.getEntity();
        if (entity != null) {
            String entityContent = getEntityContent(entity);
            StatusLine statusLine = httpResponse.getStatusLine();
            if (statusLine != null && statusLine.getStatusCode() == 200) {
                Tasks tasks = JSON.parseObject(entityContent, Tasks.class);
                return tasks != null && tasks.getTasks() != null ? tasks.getTasks().size() : 0;
            } else {
                throw new SaturnJobConsoleException(entityContent);
            }
        } else {
            throw new SaturnJobConsoleException("Not status returned, url is " + urlStr);
        }
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
        throw new SaturnJobConsoleException(e);
    } finally {
        try {
            httpClient.close();
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
        }
    }
}
Also used : StatusLine(org.apache.http.StatusLine) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Tasks(com.vip.saturn.job.console.marathon.entity.Tasks) HttpEntity(org.apache.http.HttpEntity) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) IOException(java.io.IOException)

Example 3 with SaturnJobConsoleException

use of com.vip.saturn.job.console.exception.SaturnJobConsoleException in project Saturn by vipshop.

the class ContainerServiceImpl method removeContainer.

@Override
public void removeContainer(String taskId) throws SaturnJobConsoleException {
    CuratorRepository.CuratorFrameworkOp curatorFrameworkOp = curatorRepository.inSessionClient();
    ContainerToken containerToken = getContainerToken();
    String dcosTaskIdNodePath = ContainerNodePath.getDcosTaskIdNodePath(taskId);
    if (!curatorFrameworkOp.checkExists(dcosTaskIdNodePath)) {
        throw new SaturnJobConsoleException("The taskId already exists");
    }
    List<ContainerScaleJobVo> containerScaleJobVos = getContainerScaleJobVos(taskId);
    List<String> allUnSystemJobs = jobDimensionService.getAllUnSystemJobs(curatorFrameworkOp);
    for (String job : allUnSystemJobs) {
        String preferListNodePath = JobNodePath.getConfigNodePath(job, "preferList");
        if (curatorFrameworkOp.checkExists(preferListNodePath)) {
            String preferList = curatorFrameworkOp.getData(preferListNodePath);
            if (preferList != null) {
                String[] split = preferList.trim().split(",");
                for (String tmp : split) {
                    if (tmp.trim().equals("@" + taskId)) {
                        throw new SaturnJobConsoleException("Cannot destroy the container, because it's binding a job");
                    }
                }
            }
        }
    }
    for (ContainerScaleJobVo containerScaleJobVo : containerScaleJobVos) {
        deleteContainerScaleJob(taskId, containerScaleJobVo.getJobName());
    }
    getContainerRestService().destroy(containerToken, taskId);
    curatorFrameworkOp.deleteRecursive(dcosTaskIdNodePath);
}
Also used : ContainerScaleJobVo(com.vip.saturn.job.console.domain.container.vo.ContainerScaleJobVo) CuratorRepository(com.vip.saturn.job.console.repository.zookeeper.CuratorRepository) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException)

Example 4 with SaturnJobConsoleException

use of com.vip.saturn.job.console.exception.SaturnJobConsoleException in project Saturn by vipshop.

the class ContainerServiceImpl method getContainerScaleJob.

private ContainerScaleJob getContainerScaleJob(CuratorRepository.CuratorFrameworkOp curatorFrameworkOp, String id, String jobName) throws SaturnJobConsoleException {
    ContainerScaleJob containerScaleJob = null;
    try {
        String data = curatorFrameworkOp.getData(ContainerNodePath.getDcosTaskScaleJobNodePath(id, jobName));
        if (data != null) {
            containerScaleJob = new ContainerScaleJob();
            ContainerScaleJobConfig containerScaleJobConfig = JSON.parseObject(data, ContainerScaleJobConfig.class);
            containerScaleJob.setContainerScaleJobConfig(containerScaleJobConfig);
            String enabledStr = curatorFrameworkOp.getData(JobNodePath.getConfigNodePath(jobName, "enabled"));
            containerScaleJob.setEnabled(Boolean.valueOf(enabledStr));
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new SaturnJobConsoleException(e);
    }
    return containerScaleJob;
}
Also used : SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException)

Example 5 with SaturnJobConsoleException

use of com.vip.saturn.job.console.exception.SaturnJobConsoleException in project Saturn by vipshop.

the class ContainerServiceImpl method deleteContainerScaleJob.

@Override
public void deleteContainerScaleJob(String taskId, String jobName) throws SaturnJobConsoleException {
    // disable job, and delete it
    // wait 5s to disable job at most
    CuratorRepository.CuratorFrameworkOp curatorFrameworkOp = curatorRepository.inSessionClient();
    try {
        String jobNodePath = JobNodePath.getJobNodePath(jobName);
        if (curatorFrameworkOp.checkExists(jobNodePath)) {
            String enabledNodePath = JobNodePath.getConfigNodePath(jobName, "enabled");
            String enabledStr = curatorFrameworkOp.getData(enabledNodePath);
            Boolean enabled = Boolean.valueOf(enabledStr);
            if (enabled) {
                curatorFrameworkOp.update(enabledNodePath, false);
            }
            long waitStopTime = 5000L;
            while (waitStopTime > 0L) {
                Thread.sleep(100);
                waitStopTime -= 100;
                JobStatus jobStatus = jobDimensionService.getJobStatus(jobName);
                if (JobStatus.STOPPED.equals(jobStatus)) {
                    executorService.removeJob(jobName);
                    deleteScaleJobNodePath(curatorFrameworkOp, taskId, jobName);
                    return;
                }
            }
            throw new SaturnJobConsoleException("The job is not stopped, cannot be deleted, please retry later");
        } else {
            deleteScaleJobNodePath(curatorFrameworkOp, taskId, jobName);
        }
    } catch (SaturnJobConsoleException e) {
        throw e;
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new SaturnJobConsoleException(e.getMessage(), e);
    }
}
Also used : CuratorRepository(com.vip.saturn.job.console.repository.zookeeper.CuratorRepository) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException)

Aggregations

SaturnJobConsoleException (com.vip.saturn.job.console.exception.SaturnJobConsoleException)190 SaturnJobConsoleHttpException (com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException)56 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)56 CuratorRepository (com.vip.saturn.job.console.repository.zookeeper.CuratorRepository)39 RequestResult (com.vip.saturn.job.console.domain.RequestResult)31 ParseException (java.text.ParseException)28 ResponseEntity (org.springframework.http.ResponseEntity)26 Transactional (org.springframework.transaction.annotation.Transactional)23 JobConfig4DB (com.vip.saturn.job.console.mybatis.entity.JobConfig4DB)22 CuratorFrameworkOp (com.vip.saturn.job.console.repository.zookeeper.CuratorRepository.CuratorFrameworkOp)22 IOException (java.io.IOException)19 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)19 StatusLine (org.apache.http.StatusLine)18 HttpEntity (org.apache.http.HttpEntity)16 Audit (com.vip.saturn.job.console.aop.annotation.Audit)13 JobConfig (com.vip.saturn.job.console.domain.JobConfig)11 HttpHeaders (org.springframework.http.HttpHeaders)11 CurrentJobConfig (com.vip.saturn.job.console.mybatis.entity.CurrentJobConfig)9 ZkCluster (com.vip.saturn.job.console.domain.ZkCluster)8 ArrayList (java.util.ArrayList)8