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);
}
}
}
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);
}
}
}
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);
}
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;
}
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);
}
}
Aggregations