Authored by jack

upload aws ebs

package com.monitor.awstools.comp;
import com.monitor.awstools.constant.Constants;
import com.monitor.awstools.constant.JobType;
import com.monitor.awstools.constant.TaskStatus;
import org.apache.commons.lang.StringUtils;
import org.apache.curator.utils.ZKPaths;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* Created by yoho on 2016/9/2.
*/
@Component
public class JobExecuteListenerImpl implements JobListener {
public static final Logger DEBUG = LoggerFactory.getLogger(JobExecuteListenerImpl.class);
@Autowired
TaskSaver taskSaver;
@Override
public String getName() {
return "ALL_JOB_LISTENER";
}
@Override
public void jobToBeExecuted(JobExecutionContext jobExecutionContext) {
}
@Override
public void jobExecutionVetoed(JobExecutionContext jobExecutionContext) {
}
@Override
public void jobWasExecuted(JobExecutionContext jobExecutionContext, JobExecutionException e) {
if (StringUtils.equals(JobType.ONCE.name(), (String) jobExecutionContext.getJobDetail().getJobDataMap().get(Constants.JOBTYPE))) {
DEBUG.info("Once task was executed,change it state: stop...");
String statePath = ZKPaths.makePath(ZKPaths.makePath(Constants.TASKPATH, jobExecutionContext.getJobDetail().getKey().toString()), Constants.TASKSTATUS);
taskSaver.configJob(statePath, TaskStatus.STOP.name());
}
}
}
... ...
package com.monitor.awstools.comp;
import com.monitor.awstools.constant.Constants;
import com.monitor.awstools.constant.TaskStatus;
import com.monitor.awstools.job.AwsTask;
import com.monitor.awstools.task.AwsTask;
import org.apache.commons.lang.StringUtils;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.api.CuratorListener;
import org.apache.curator.framework.recipes.cache.*;
import org.apache.curator.utils.ZKPaths;
import org.quartz.JobKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import static org.apache.curator.framework.recipes.cache.PathChildrenCache.StartMode.BUILD_INITIAL_CACHE;
/**
* Created by yoho on 2016/9/1.
*/
@Component
public class TaskListener {
public static final Logger DEBUG = LoggerFactory.getLogger(TaskListener.class);
@Autowired
ZkClient zkClient;
... ... @@ -35,6 +36,15 @@ public class TaskListener {
@PostConstruct
public void addWatch() {
try {
if (null == zkClient.getCuratorFramework().checkExists().forPath(Constants.TASKPATH)) {
zkClient.getCuratorFramework().create().creatingParentsIfNeeded().forPath(Constants.TASKPATH);
}
} catch (Exception e) {
DEBUG.error("Not found task in zookeeper...error {}", e);
}
childrenCache = new PathChildrenCache(zkClient.getCuratorFramework(), Constants.TASKPATH, true);
PathChildrenCacheListener childrenCacheListener = new PathChildrenCacheListener() {
... ... @@ -42,9 +52,13 @@ public class TaskListener {
public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) throws Exception {
switch (pathChildrenCacheEvent.getType()) {
case CHILD_REMOVED:
deleteNode(pathChildrenCacheEvent);
break;
case CHILD_ADDED:
DEBUG.info("Task Listener found new task ...");
addNode(pathChildrenCacheEvent);
break;
default:
... ... @@ -56,7 +70,7 @@ public class TaskListener {
childrenCache.getListenable().addListener(childrenCacheListener);
try {
childrenCache.start();
childrenCache.start(BUILD_INITIAL_CACHE);
} catch (Exception e) {
}
... ... @@ -82,7 +96,7 @@ public class TaskListener {
String jobKey = ZKPaths.getNodeFromPath(path);
try {
Thread.sleep(1*1000);
Thread.sleep(1 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
... ...
... ... @@ -2,7 +2,7 @@ package com.monitor.awstools.comp;
import com.monitor.awstools.constant.Constants;
import com.monitor.awstools.constant.JobType;
import com.monitor.awstools.job.AwsTask;
import com.monitor.awstools.task.AwsTask;
import org.apache.commons.lang.StringUtils;
import org.apache.curator.utils.ZKPaths;
import org.apache.zookeeper.CreateMode;
... ... @@ -182,4 +182,19 @@ public class TaskSaver {
return awsTask;
}
public boolean checkExist(String jobKey) {
String path = ZKPaths.makePath(Constants.TASKPATH, jobKey);
try {
if (null != zkClient.getCuratorFramework().checkExists().forPath(path)) {
return true;
}
} catch (Exception e) {
DEBUG.error("Failed to check task {} exist?", jobKey);
}
return false;
}
}
... ...
package com.monitor.awstools.comp;
import com.monitor.awstools.constant.Constants;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.Map;
/**
* Created by yoho on 2016/8/30.
*/
@Component
public class TaskScheduler {
public static final Logger DEBUG = LoggerFactory.getLogger(TaskScheduler.class);
public Scheduler scheduler;
@Autowired
TriggerListenerImpl triggerListener;
@Autowired
JobExecuteListenerImpl jobExecuteListener;
@PostConstruct
public void init() {
try {
scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.start();
scheduler.getListenerManager().addTriggerListener(triggerListener);
scheduler.getListenerManager().addJobListener(jobExecuteListener);
scheduler.start();
} catch (SchedulerException e) {
e.printStackTrace();
}
... ... @@ -43,6 +49,7 @@ public class TaskScheduler {
e.printStackTrace();
}
try {
DEBUG.info("Start to scheduleJob {}", jobDetail.getKey().toString());
scheduler.scheduleJob(jobDetail, trigger);
} catch (SchedulerException e) {
e.printStackTrace();
... ... @@ -68,6 +75,7 @@ public class TaskScheduler {
public void deleteJob(JobKey jobKey) {
try {
DEBUG.info("Start to unScheduleJob {}", jobKey);
this.scheduler.pauseJob(jobKey);
this.scheduler.deleteJob(jobKey);
} catch (SchedulerException e) {
... ...
... ... @@ -2,6 +2,7 @@ package com.monitor.awstools.comp;
import com.monitor.awstools.constant.Constants;
import com.monitor.awstools.constant.TaskStatus;
import com.sun.org.apache.xml.internal.resolver.helpers.Debug;
import org.apache.commons.lang.StringUtils;
import org.apache.curator.utils.ZKPaths;
import org.quartz.JobExecutionContext;
... ... @@ -37,16 +38,20 @@ public class TriggerListenerImpl implements TriggerListener {
public boolean vetoJobExecution(Trigger trigger, JobExecutionContext jobExecutionContext) {
String jobKey = jobExecutionContext.getJobDetail().getKey().toString();
try {
byte[] data = this.zkClient.getCuratorFramework().getData().forPath(ZKPaths.makePath(Constants.TASKPATH, jobKey));
byte[] data = this.zkClient.getCuratorFramework().getData().forPath(ZKPaths.makePath(ZKPaths.makePath(Constants.TASKPATH, jobKey), Constants.TASKSTATUS));
if (StringUtils.equals(new String(data), TaskStatus.START.name())) {
DEBUG.info("Start to execute task....");
return false;
}
} catch (Exception e) {
DEBUG.error("Failed to query task status , so vetoJob {}", jobKey);
}
DEBUG.info("Not start to execute task...status is stop");
return true;
}
... ...
... ... @@ -4,13 +4,13 @@ import com.monitor.awstools.constant.Constants;
import com.monitor.awstools.constant.JobType;
import com.monitor.awstools.constant.TaskStatus;
import com.monitor.awstools.constant.TaskType;
import com.monitor.awstools.job.AwsTask;
import com.monitor.awstools.task.AwsTask;
import com.monitor.awstools.model.*;
import com.monitor.awstools.service.AwsQueryService;
import com.monitor.awstools.service.TaskService;
import com.monitor.model.response.BaseResponse;
import com.sun.javafx.collections.MappingChange;
import org.apache.commons.lang.StringUtils;
import org.quartz.CronExpression;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -118,48 +118,43 @@ public class AwsToolController {
*/
@RequestMapping(value = "/task/create")
public BaseResponse createTask(@RequestBody AwsEbsReq task) {
Map<String, String> proMaps = convertProMap(task);
//插入创建时间
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yy/MM/dd HH:mm");
proMaps.put(Constants.CREATETIME, simpleDateFormat.format(new Date()));
try {
taskService.addTask(proMaps.get(Constants.TASKNAME), proMaps.get(Constants.TASKGROUP), JobType.valueOf(proMaps.get(Constants.JOBTYPE)), proMaps);
checkExpression(task);
} catch (Exception e) {
DEBUG.error("Failed to create task.... error {}", e);
DEBUG.error("Failed to create task ... error {}", e.getMessage());
return new BaseResponse(400, e.getMessage());
}
return new BaseResponse();
}
Map<String, String> proMaps = convertProMap(task);
//插入创建时间
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yy/MM/dd HH:mm");
/**
* 修改task
*
* @param task
* @return
*/
@RequestMapping(value = "/task/edit")
public BaseResponse editTask(@RequestBody AwsEbsReq task) {
Map<String, String> proMap = convertProMap(task);
proMaps.put(Constants.CREATETIME, simpleDateFormat.format(new Date()));
try {
taskService.configTask(proMap.get(Constants.TASKNAME), proMap.get(Constants.TASKGROUP), proMap);
if (taskService.checkTaskExist(task.getTaskName(), Constants.GROUP)) {
taskService.configTask(proMaps.get(Constants.TASKNAME), proMaps.get(Constants.TASKGROUP), proMaps);
} else {
taskService.addTask(proMaps.get(Constants.TASKNAME), proMaps.get(Constants.TASKGROUP), JobType.valueOf(proMaps.get(Constants.JOBTYPE)), proMaps);
}
} catch (Exception e) {
DEBUG.error("Failed to re-config task .... error {}", e);
DEBUG.error("Failed to create task.... error {}", e);
return new BaseResponse(400, e.getMessage());
}
return new BaseResponse();
}
/**
* 暂停task
*
... ... @@ -296,6 +291,7 @@ public class AwsToolController {
proMap.put(Constants.EXPRESSION, awsEbsReq.getTaskExp());
proMap.put(Constants.VOLUME, awsEbsReq.getVolumeId());
proMap.put(Constants.DESCRPTION, awsEbsReq.getSanpShotDesc());
... ... @@ -307,6 +303,26 @@ public class AwsToolController {
return proMap;
}
private void checkExpression(AwsEbsReq awsEbsReq) throws Exception {
try {
if (StringUtils.equals("1", awsEbsReq.getTaskType())) {
Integer exp = Integer.valueOf(awsEbsReq.getTaskExp());
if (exp <= 0) {
throw new Exception("Expression can not be less than 0...");
}
} else if (StringUtils.equals("2", awsEbsReq.getTaskType())) {
if (!CronExpression.isValidExpression(awsEbsReq.getTaskExp())) {
throw new Exception("Cron expression is error...");
}
}
} catch (Exception e) {
throw e;
}
}
public AwsEbsTaskResp converAwsTask(AwsTask awsTask) {
AwsEbsTaskResp ebsTaskResp = new AwsEbsTaskResp();
... ...
... ... @@ -9,8 +9,11 @@ import com.amazonaws.services.ec2.AmazonEC2Client;
import com.amazonaws.services.ec2.model.*;
import com.monitor.awstools.constant.Constants;
import com.monitor.awstools.constant.TaskType;
import com.sun.org.apache.xml.internal.resolver.helpers.Debug;
import org.apache.commons.lang.StringUtils;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
... ... @@ -19,6 +22,8 @@ import java.util.List;
* Created by yoho on 2016/8/30.
*/
public class AwsJobImpl implements InterruptableJob {
public static final Logger DEBUG = LoggerFactory.getLogger(AwsJobImpl.class);
@Override
public void interrupt() throws UnableToInterruptJobException {
... ... @@ -39,9 +44,13 @@ public class AwsJobImpl implements InterruptableJob {
String description = (String) jobExecutionContext.getJobDetail().getJobDataMap().get(Constants.DESCRPTION);
CreateSnapshotResult result = amazonEC2.createSnapshot(new CreateSnapshotRequest(volumeId, description));
/*CreateSnapshotResult result = amazonEC2.createSnapshot(new CreateSnapshotRequest(volumeId, description));
result.getSnapshot().getSnapshotId();*/
DEBUG.info("Start to create snapshot {} from volume {}", description, volumeId);
result.getSnapshot().getSnapshotId();
}
}
... ...
... ... @@ -2,13 +2,7 @@ package com.monitor.awstools.service;
import com.monitor.awstools.comp.TaskSaver;
import com.monitor.awstools.comp.TaskScheduler;
import com.monitor.awstools.comp.ZkClient;
import com.monitor.awstools.constant.Constants;
import com.monitor.awstools.constant.JobType;
import com.monitor.awstools.job.AwsTask;
import com.sun.org.apache.xml.internal.resolver.helpers.Debug;
import org.apache.commons.lang.StringUtils;
import org.apache.curator.utils.ZKPaths;
import com.monitor.awstools.task.AwsTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -16,9 +10,7 @@ import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by yoho on 2016/8/30.
... ...
... ... @@ -5,14 +5,12 @@ import com.monitor.awstools.comp.TaskScheduler;
import com.monitor.awstools.constant.Constants;
import com.monitor.awstools.constant.JobType;
import com.monitor.awstools.constant.TaskStatus;
import com.monitor.awstools.job.AwsTask;
import org.apache.commons.lang.StringUtils;
import com.monitor.awstools.task.AwsTask;
import org.apache.curator.utils.ZKPaths;
import org.quartz.JobKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
... ... @@ -46,7 +44,7 @@ public class TaskService {
JobKey jobKey = new JobKey(name, group);
String path= ZKPaths.makePath(ZKPaths.makePath(Constants.TASKPATH,jobKey.toString()),Constants.TASKSTATUS);
String path = ZKPaths.makePath(ZKPaths.makePath(Constants.TASKPATH, jobKey.toString()), Constants.TASKSTATUS);
taskSaver.configJob(path, TaskStatus.STOP.name());
... ... @@ -55,7 +53,7 @@ public class TaskService {
public void resumeTask(String name, String group) throws Exception {
JobKey jobKey = new JobKey(name, group);
String path= ZKPaths.makePath(ZKPaths.makePath(Constants.TASKPATH,jobKey.toString()),Constants.TASKSTATUS);
String path = ZKPaths.makePath(ZKPaths.makePath(Constants.TASKPATH, jobKey.toString()), Constants.TASKSTATUS);
taskSaver.configJob(path, TaskStatus.START.name());
... ... @@ -68,6 +66,8 @@ public class TaskService {
//删除
taskSaver.deleteJob(jobKey.toString());
Thread.sleep(1000);
//新增
taskSaver.storeJob(proMaps);
}
... ... @@ -82,5 +82,9 @@ public class TaskService {
return taskSaver.queryTask(JobKey.jobKey(name, group).toString());
}
public boolean checkTaskExist(String name, String group) {
return taskSaver.checkExist(new JobKey(name, group).toString());
}
}
... ...
package com.monitor.awstools.job;
package com.monitor.awstools.task;
import com.monitor.awstools.constant.Constants;
import com.monitor.awstools.constant.JobType;
import com.monitor.awstools.job.AwsJobImpl;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
... ...
... ... @@ -7,4 +7,4 @@ JavaApiExecutorPoolMaxSize=50
nginxsync.agent=192.168.102.15:6060
zookeeper.address=115.29.176.192:2181
\ No newline at end of file
zookeeper.address=172.16.6.235:2181
\ No newline at end of file
... ...