Commit 42a947309ba2df7c7caf7c8b69b8710f6d3a3e8e

Authored by Liu Haoyu
1 parent 66b3bbb4

可视化功能完善;

Linux服务器监听运维功能.md
... ... @@ -147,10 +147,10 @@
147 147 ## 1.5 配置启动服务
148 148 ### 1.5.1 创建
149 149  
150   -| 调用方式 | 接口地址 |
151   -| ------------ | :------------------------------------------------------ |
152   -| POST | http://ip:port/occupationOfBasicResources/createService |
153   -| Content-Type | application/json;charset=UTF-8 |
  150 +| 调用方式 | 接口地址 |
  151 +| ------------ | :-------------------------------------- |
  152 +| POST | http://ip:port/background/createService |
  153 +| Content-Type | application/json;charset=UTF-8 |
154 154  
155 155 | 请求参数 | | | | |
156 156 | ----------- | ------------ | ------- | ---- | -------------- |
... ... @@ -192,10 +192,10 @@
192 192  
193 193 ### 1.5.2 更新
194 194  
195   -| 调用方式 | 接口地址 |
196   -| ------------ | :------------------------------------------------------ |
197   -| POST | http://ip:port/occupationOfBasicResources/updateService |
198   -| Content-Type | application/json;charset=UTF-8 |
  195 +| 调用方式 | 接口地址 |
  196 +| ------------ | :-------------------------------------- |
  197 +| POST | http://ip:port/background/updateService |
  198 +| Content-Type | application/json;charset=UTF-8 |
199 199  
200 200 | 请求参数 | | | | |
201 201 | ----------- | ------------ | ------- | ---- | -------------- |
... ... @@ -239,10 +239,10 @@
239 239  
240 240 ### 1.5.3 删除
241 241  
242   -| 调用方式 | 接口地址 |
243   -| ------------ | :------------------------------------------------------ |
244   -| POST | http://ip:port/occupationOfBasicResources/deleteService |
245   -| Content-Type | application/json;charset=UTF-8 |
  242 +| 调用方式 | 接口地址 |
  243 +| ------------ | :-------------------------------------- |
  244 +| POST | http://ip:port/background/deleteService |
  245 +| Content-Type | application/json;charset=UTF-8 |
246 246  
247 247 | 请求参数 | | | | |
248 248 | -------- | -------- | ------- | ---- | ------------------ |
... ... @@ -269,12 +269,49 @@
269 269 }
270 270 ```
271 271  
  272 +
  273 +### 1.5.4 操作已经配置好的服务
  274 +
  275 +| 调用方式 | 接口地址 |
  276 +| ------------ | :-------------------------------------- |
  277 +| POST | http://ip:port/background/serviceAction |
  278 +| Content-Type | application/json;charset=UTF-8 |
  279 +
  280 +| 请求参数 | | | | |
  281 +| -------- | -------- | ------- | ---- | --------------------------------------------------- |
  282 +| 参数项 | 名称 | 类型 | 必选 | 描述 |
  283 +| id | 服务主键 | Integer | 是 | 主键(formdata形式) |
  284 +| action | 操作类型 | String | 是 | start/status/restart/stop/remove/load(formdata形式) |
  285 +```
  286 +请求示例
  287 +"id": 1
  288 +"action": "start"
  289 +```
  290 +
  291 +| 返回结果 | | | |
  292 +| -------- | -------- | ------ | --------------------------------- |
  293 +| 参数项 | 名称 | 类型 | 描述 |
  294 +| code | 响应码 | int | 200为操作成功,其他code表示失败 |
  295 +| message | 提示信息 | string | 200为操作成功, 其他为对应错误信息 |
  296 +| data | 返回信息 | | |
  297 +| result | 操作结果 | string | 操作成功或者失败原因 |
  298 +
  299 +```
  300 +响应示例
  301 +{
  302 + "code": 200,
  303 + "message": "操作成功",
  304 + "data": {}
  305 +}
  306 +```
  307 +
  308 +
272 309 ## 1.6 查询配置好的服务运行情况
273 310  
274   -| 调用方式 | 接口地址 |
275   -| ------------ | :----------------------------------------------------- |
276   -| POST | http://ip:port/occupationOfBasicResources/serviceQuery |
277   -| Content-Type | application/json;charset=UTF-8 |
  311 +| 调用方式 | 接口地址 |
  312 +| ------------ | :------------------------------------- |
  313 +| POST | http://ip:port/background/serviceQuery |
  314 +| Content-Type | application/json;charset=UTF-8 |
278 315  
279 316 | 请求参数 | | | | |
280 317 | ----------- | -------- | ------- | ---- | ---------------------- |
... ...
src/main/java/com/objecteye/common/GeneralContent.java
... ... @@ -17,9 +17,21 @@ public class GeneralContent {
17 17 */
18 18 public final static String REDIS_RECORD_MODULE = "redisRecordModule";
19 19 /**
  20 + * 服务对应docker镜像id
  21 + */
  22 + public final static String REDIS_SERVICE_DOCKER_CONTAINER_ID = "redisServiceDockerContainerId";
  23 + /**
  24 + * 服务对应docker启动状态
  25 + */
  26 + public final static String REDIS_SERVICE_DOCKER_STATUS = "redisServiceDockerStatus";
  27 + /**
20 28 * sh功能脚本map
21 29 */
22 30 public final static Map<String, String> MODULE_MAP = new HashMap<>();
  31 + /**
  32 + * docker启动脚本输出对应中文
  33 + */
  34 + public final static Map<String, String> DOCKER_SH_OUT_MAP = new HashMap<>();
23 35  
24 36 static {
25 37 MODULE_MAP.put("arp_cache", "/shell_files/arp_cache.sh");
... ... @@ -54,5 +66,19 @@ public class GeneralContent {
54 66 MODULE_MAP.put("swap", "/shell_files/swap.sh");
55 67 MODULE_MAP.put("upload_transfer_rate", "/shell_files/upload_transfer_rate.sh");
56 68 MODULE_MAP.put("user_accounts", "/shell_files/user_accounts.sh");
  69 + MODULE_MAP.put("sysv", "/shell_files/sysv.sh");
  70 +
  71 + DOCKER_SH_OUT_MAP.put("0", "成功");
  72 + DOCKER_SH_OUT_MAP.put("1", "失败");
  73 + DOCKER_SH_OUT_MAP.put("2", "端口参数为空");
  74 + DOCKER_SH_OUT_MAP.put("3", "端口类型错误");
  75 + DOCKER_SH_OUT_MAP.put("4", "服务未启动");
  76 + DOCKER_SH_OUT_MAP.put("5", "容器启动失败");
  77 + DOCKER_SH_OUT_MAP.put("6", "本地镜像不存在");
  78 + DOCKER_SH_OUT_MAP.put("7", "容器参数为空");
  79 + DOCKER_SH_OUT_MAP.put("8", "方法参数错误");
  80 + DOCKER_SH_OUT_MAP.put("9", "容器不存在");
  81 + DOCKER_SH_OUT_MAP.put("10", "端口已经被占用");
  82 + DOCKER_SH_OUT_MAP.put("11", "未知错误");
57 83 }
58 84 }
... ...
src/main/java/com/objecteye/controller/BackGroundController.java
... ... @@ -77,4 +77,10 @@ public class BackGroundController extends BasicController {
77 77 public CommonResult serviceQuery(@RequestParam Integer currentPage, @RequestParam Integer pageVolume) {
78 78 return pageResultHandle(iBackGroundService.serviceQuery(currentPage, pageVolume));
79 79 }
  80 +
  81 + @ApiOperation("操作已经配置好的服务")
  82 + @RequestMapping(value = "serviceAction", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
  83 + public CommonResult serviceAction(@RequestParam(required = false) Integer id, @RequestParam(required = false) String action) {
  84 + return jsonObjectResultHandle(iBackGroundService.serviceAction(id, action));
  85 + }
80 86 }
... ...
src/main/java/com/objecteye/service/IBackGroundService.java
... ... @@ -90,4 +90,13 @@ public interface IBackGroundService {
90 90 * @return 结果集
91 91 */
92 92 PageResult serviceQuery(Integer currentPage, Integer pageVolume);
  93 +
  94 + /**
  95 + * 操作服务
  96 + *
  97 + * @param configId 服务配置主键
  98 + * @param action 操作
  99 + * @return 操作状态
  100 + */
  101 + JSONObject serviceAction(Integer configId, String action);
93 102 }
... ...
src/main/java/com/objecteye/service/IOccupationOfBasicResourcesService.java
... ... @@ -19,6 +19,14 @@ public interface IOccupationOfBasicResourcesService {
19 19 JSONArray getInfoByModule(Map<String, Object> requestMap);
20 20  
21 21 /**
  22 + * 获取指定module的输出
  23 + *
  24 + * @param module 请求参数
  25 + * @return 输出结果
  26 + */
  27 + JSONArray getInfoByModule(String module);
  28 +
  29 + /**
22 30 * jar包发布
23 31 *
24 32 * @param requestMap 请求参数
... ...
src/main/java/com/objecteye/service/impl/BackGroundServiceImpl.java
... ... @@ -17,10 +17,12 @@ import com.objecteye.vo.VSyServiceMainTable;
17 17 import lombok.extern.slf4j.Slf4j;
18 18 import org.springframework.beans.factory.annotation.Autowired;
19 19 import org.springframework.data.redis.core.RedisTemplate;
  20 +import org.springframework.http.ResponseEntity;
20 21 import org.springframework.scheduling.annotation.Async;
21 22 import org.springframework.scheduling.annotation.Scheduled;
22 23 import org.springframework.stereotype.Component;
23 24 import org.springframework.transaction.annotation.Transactional;
  25 +import org.springframework.web.client.RestTemplate;
24 26  
25 27 import java.util.*;
26 28 import java.util.stream.Collectors;
... ... @@ -38,6 +40,8 @@ public class BackGroundServiceImpl implements IBackGroundService {
38 40 private SyServiceConfigMapper syServiceConfigMapper;
39 41 @Autowired
40 42 private SyServiceConfigItemMapper syServiceConfigItemMapper;
  43 + @Autowired
  44 + private RestTemplate restTemplate;
41 45  
42 46 /**
43 47 * 定时写入的定制信息
... ... @@ -45,7 +49,7 @@ public class BackGroundServiceImpl implements IBackGroundService {
45 49 @Override
46 50 @Scheduled(fixedRate = 3 * 1000)
47 51 public void jobController() {
48   - /*String moduleStr = (String) redisTemplate.opsForValue().get(GeneralContent.REDIS_RECORD_MODULE);
  52 + String moduleStr = (String) redisTemplate.opsForValue().get(GeneralContent.REDIS_RECORD_MODULE);
49 53 if (moduleStr == null || "".equals(moduleStr)) {
50 54 return;
51 55 }
... ... @@ -66,7 +70,7 @@ public class BackGroundServiceImpl implements IBackGroundService {
66 70 doGpuUtilization();
67 71 break;
68 72 }
69   - }*/
  73 + }
70 74 }
71 75  
72 76 /**
... ... @@ -75,11 +79,11 @@ public class BackGroundServiceImpl implements IBackGroundService {
75 79 @Override
76 80 @Scheduled(fixedRate = 1000 * 60)
77 81 public void removeOverTimeData() {
78   - /*Calendar calendar = Calendar.getInstance();
  82 + Calendar calendar = Calendar.getInstance();
79 83 calendar.setTimeInMillis(System.currentTimeMillis());
80 84 calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 1);
81 85 long lastTime = calendar.getTimeInMillis();
82   - syBasicResourceHistoryMapper.deleteLteTime(lastTime);*/
  86 + syBasicResourceHistoryMapper.deleteLteTime(lastTime);
83 87 }
84 88  
85 89 /**
... ... @@ -224,12 +228,28 @@ public class BackGroundServiceImpl implements IBackGroundService {
224 228 */
225 229 @Override
226 230 public JSONObject sdkController(Map<String, Object> requestMap) {
227   - String action = (String) requestMap.getOrDefault("action", "start");
228   - // 参数校验
229   - // 是否启动车车窗检测 0:关闭 1:开启
  231 + String configId = (String) requestMap.get("configId");
  232 + String uri = "/vehicle/loadConfig";
  233 + String configItemName = "ip,port";
  234 + List<SyServiceConfigItem> syServiceConfigItems =
  235 + syServiceConfigItemMapper.selectByExample(SyServiceConfigItemExample.newAndCreateCriteria()
  236 + .andConfigIdEqualTo(Integer.valueOf(configId)).andItemNameIn(Arrays.asList(configItemName.split(",")))
  237 + .example());
  238 + String ip = null;
  239 + String port = null;
  240 + for (SyServiceConfigItem syServiceConfigItem : syServiceConfigItems) {
  241 + if ("ip".equals(syServiceConfigItem.getItemName())) {
  242 + ip = syServiceConfigItem.getItemValue();
  243 + } else if ("port".equals(syServiceConfigItem.getItemName())) {
  244 + port = syServiceConfigItem.getItemValue();
  245 + }
  246 + }
230 247 JSONObject resultObj = new JSONObject();
231   - String params = "action," +
232   - "vehicle_car_win_detect_config," +
  248 + if (ip == null || port == null) {
  249 + resultObj.put("error", "服务配置缺少ip或端口信息");
  250 + return resultObj;
  251 + }
  252 + String params = "vehicle_car_win_detect_config," +
233 253 "vehicle_recg_config," +
234 254 "vehicle_plate_det_recg_config," +
235 255 "vehicle_color_config," +
... ... @@ -240,17 +260,24 @@ public class BackGroundServiceImpl implements IBackGroundService {
240 260 "gpuId," +
241 261 "log," +
242 262 "vehicle_image_quality_config," +
243   - "vehicle_motor_tricycle_analysis_config,port";
  263 + "vehicle_motor_tricycle_analysis_config,port," +
  264 + "vehicle_stain_vp_config";
244 265 List<String> errorList = new ArrayList<>();
  266 + Map<String, String> restRequestMap = new HashMap<>();
245 267 for (String param : params.split(",")) {
246 268 if (!requestMap.containsKey(param)) {
247 269 errorList.add(param + "参数不能为空");
  270 + } else {
  271 + restRequestMap.put(param, (String) requestMap.get(param));
248 272 }
249 273 }
250 274 if (errorList.size() > 0) {
251 275 resultObj.put("error", String.join(",", errorList));
252 276 return resultObj;
253 277 }
  278 +
  279 + ResponseEntity<String> responseEntity = restTemplate.postForEntity(ip + ":" + port + uri, restRequestMap, String.class);
  280 + resultObj = JSON.parseObject(responseEntity.getBody());
254 281 return resultObj;
255 282 }
256 283  
... ... @@ -361,6 +388,7 @@ public class BackGroundServiceImpl implements IBackGroundService {
361 388 List<SyServiceConfigItem> configItems = configIdItemsMap.get(syServiceConfig.getId());
362 389 VSyServiceMainTable vSyServiceMainTable = new VSyServiceMainTable();
363 390 vSyServiceMainTable.setUpdateTime(DateUtil.format(new Date(syServiceConfig.getUpdateTime()), "yyyy-MM-dd HH:mm:ss"));
  391 + vSyServiceMainTable.setConfigId(syServiceConfig.getId());
364 392  
365 393 String ip = null;
366 394 String port = null;
... ... @@ -380,8 +408,131 @@ public class BackGroundServiceImpl implements IBackGroundService {
380 408 }
381 409 vSyServiceMainTable.setPort(ip + ":" + port);
382 410 vSyServiceMainTable.setItems(String.join(",", otherParams));
  411 + vSyServiceMainTable.setStatus((String) redisTemplate.opsForHash().get(GeneralContent.REDIS_SERVICE_DOCKER_STATUS, syServiceConfig.getId()));
383 412 resultList.add(vSyServiceMainTable);
384 413 }
385 414 return new PageResult<>((int) Math.ceil((double) total / pageVolume), resultList);
386 415 }
  416 +
  417 + /**
  418 + * 操作服务
  419 + *
  420 + * @param configId 服务配置主键
  421 + * @param action 操作
  422 + * @return 操作状态
  423 + */
  424 + @Override
  425 + public JSONObject serviceAction(Integer configId, String action) {
  426 + // 如果有启动状态的容器就直接把启动变为重启操作
  427 + if (redisTemplate.opsForHash().hasKey(GeneralContent.REDIS_SERVICE_DOCKER_CONTAINER_ID, configId) && "start".equals(action)) {
  428 + action = "restart";
  429 + }
  430 + if ("start".equals(action)) {
  431 + return serviceDockerStart(configId);
  432 + } else {
  433 + return serviceDockerOthers(configId, action);
  434 + }
  435 + }
  436 +
  437 + /**
  438 + * sysv.sh [stop|restart|status|load] {[containerId]}
  439 + *
  440 + * @param configId 服务配置主键
  441 + * @param action 操作
  442 + * @return 操作状态
  443 + */
  444 + private JSONObject serviceDockerOthers(Integer configId, String action) {
  445 + JSONObject resultObj = new JSONObject();
  446 + String stopStatusAction = "stop,remove";
  447 + String startStatusAction = "load,restart";
  448 + String cmd = System.getProperty("user.dir") + GeneralContent.MODULE_MAP.get("sysv") + " " + action + " ";
  449 + String containerId = (String) redisTemplate.opsForHash().get(GeneralContent.REDIS_SERVICE_DOCKER_CONTAINER_ID, configId);
  450 + SyServiceConfigItem syServiceConfigItem = syServiceConfigItemMapper.selectOneByExample(SyServiceConfigItemExample.newAndCreateCriteria()
  451 + .andConfigIdEqualTo(configId).andItemNameEqualTo("port").example());
  452 + if (containerId == null || "".equals(containerId) || syServiceConfigItem == null) {
  453 + resultObj.put("error", "服务配置项为空或没有找到容器id");
  454 + } else {
  455 + if ("load".equals(action)) {
  456 + cmd += "";
  457 + } else if ("remove".equals(action)) {
  458 + cmd += containerId;
  459 + } else {
  460 + cmd += containerId + " " + syServiceConfigItem.getItemValue();
  461 + }
  462 + List<String> outList = new ArrayList<>();
  463 + LinuxUtils.executeLinuxCmd(cmd, outList);
  464 + if ("remove".equals(action)) {
  465 + redisTemplate.opsForHash().delete(GeneralContent.REDIS_SERVICE_DOCKER_CONTAINER_ID, configId);
  466 + }
  467 + if (outList.size() == 0) {
  468 + resultObj.put("error", "未收到服务器响应");
  469 + } else {
  470 + String outStr = String.join("", outList);
  471 + String[] outArr = outStr.split(" ");
  472 + String resultCode = outArr[outArr.length - 1].trim();
  473 + String resultMsg = GeneralContent.DOCKER_SH_OUT_MAP.get(resultCode);
  474 + // 更新状态值
  475 + if (stopStatusAction.contains(action) && "0".equals(resultCode)) {
  476 + redisTemplate.opsForHash().put(GeneralContent.REDIS_SERVICE_DOCKER_STATUS, configId, "0");
  477 + } else if (startStatusAction.contains(action) && "0".equals(resultCode)) {
  478 + redisTemplate.opsForHash().put(GeneralContent.REDIS_SERVICE_DOCKER_STATUS, configId, "1");
  479 + } else if ("status".equals(action)) {
  480 + if ("0".equals(resultCode)) {
  481 + redisTemplate.opsForHash().put(GeneralContent.REDIS_SERVICE_DOCKER_STATUS, configId, "0");
  482 + } else {
  483 + redisTemplate.opsForHash().put(GeneralContent.REDIS_SERVICE_DOCKER_STATUS, configId, "1");
  484 + }
  485 + }
  486 + if ("0".equals(resultCode)) {
  487 + resultObj.put("result", resultMsg);
  488 + } else {
  489 + resultObj.put("error", resultMsg);
  490 + }
  491 +
  492 + }
  493 + }
  494 + return resultObj;
  495 + }
  496 +
  497 + /**
  498 + * sysv.sh [start] [port]
  499 + *
  500 + * @param configId 服务配置id
  501 + * @return 结果集
  502 + */
  503 + private JSONObject serviceDockerStart(Integer configId) {
  504 + JSONObject resultObj = new JSONObject();
  505 + // 获取port参数
  506 + SyServiceConfigItem syServiceConfigItem = syServiceConfigItemMapper.selectOneByExample(SyServiceConfigItemExample.newAndCreateCriteria()
  507 + .andConfigIdEqualTo(configId).andItemNameEqualTo("port").example());
  508 + if (syServiceConfigItem == null) {
  509 + resultObj.put("error", "没有找到对应数据");
  510 + } else {
  511 + String cmd = System.getProperty("user.dir") + GeneralContent.MODULE_MAP.get("sysv") + " start " + syServiceConfigItem.getItemValue();
  512 + List<String> outList = new ArrayList<>();
  513 + LinuxUtils.executeLinuxCmd(cmd, outList);
  514 + if (outList.size() > 0) {
  515 + String outStr = String.join("", outList);
  516 + String[] outArr = outStr.split(" ");
  517 + String resultCode = outArr[2].trim();
  518 + String resultMsg = GeneralContent.DOCKER_SH_OUT_MAP.get(resultCode);
  519 + if ("0".equals(resultCode)) {
  520 + resultObj.put("containerId", outArr[6].replaceAll(" ", ""));
  521 + redisTemplate.opsForHash().put(GeneralContent.REDIS_SERVICE_DOCKER_CONTAINER_ID, configId, outArr[6].replaceAll(" ", ""));
  522 + redisTemplate.opsForHash().put(GeneralContent.REDIS_SERVICE_DOCKER_STATUS, configId, "1");
  523 + } else {
  524 + redisTemplate.opsForHash().put(GeneralContent.REDIS_SERVICE_DOCKER_STATUS, configId, "0");
  525 + }
  526 + if ("0".equals(resultCode)) {
  527 + resultObj.put("result", resultMsg);
  528 + } else {
  529 + resultObj.put("error", resultMsg);
  530 + }
  531 +
  532 + } else {
  533 + resultObj.put("error", "没有找到对应数据");
  534 + }
  535 + }
  536 + return resultObj;
  537 + }
387 538 }
... ...
src/main/java/com/objecteye/service/impl/OccupationOfBasicResourcesServiceImpl.java
... ... @@ -31,7 +31,17 @@ public class OccupationOfBasicResourcesServiceImpl implements IOccupationOfBasic
31 31 */
32 32 @Override
33 33 public JSONArray getInfoByModule(Map<String, Object> requestMap) {
34   - String module = (String) requestMap.get("module");
  34 + return getInfoByModule((String) requestMap.get("module"));
  35 + }
  36 +
  37 + /**
  38 + * 获取指定module的输出
  39 + *
  40 + * @param module 请求参数
  41 + * @return 输出结果
  42 + */
  43 + @Override
  44 + public JSONArray getInfoByModule(String module) {
35 45 JSONArray resultArr = new JSONArray();
36 46 if (GeneralContent.MODULE_MAP.containsKey(module)) {
37 47 String cmd = System.getProperty("user.dir") + GeneralContent.MODULE_MAP.get(module);
... ...
src/main/java/com/objecteye/shell_files/sysv.sh 0 → 100644
  1 +#!/bin/bash
  2 +#常量
  3 +successful=0 #成功
  4 +failed=1 #失败
  5 +portIsEmpty=2 #端口参数为空
  6 +portTypeError=3 #端口类型错误
  7 +serverNotRunning=4 #服务未启动
  8 +containerNoRunning=5 #容器启动失败
  9 +localImageNotExist=6 #本地镜像不存在
  10 +containerIdIsEmpty=7 #容器参数为空
  11 +methodParameterError=8 #方法参数错误({start|stop|restart|status|load|remove})
  12 +containerIdNotExist=9 #容器不存在
  13 +portOccupied=10 #端口已经被占用
  14 +defaultError=11 #未知错误
  15 +
  16 +
  17 +containerServerUp="Up" #容器运行中
  18 +containerServerExited="Exited" #容器退出
  19 +firstLoad=0 #首次加载镜像
  20 +multipleLoad=1 #多次加载
  21 +
  22 +#变量
  23 +serverPackage=micro-vehicle-service-engine-visual-huizhi-v1.0.tar
  24 +serverImageName=micro-vehicle-service-engine-visual-huizhi
  25 +serverImageVersion=v1.0
  26 +serverImgae=$serverImageName:$serverImageVersion
  27 +functionType=$1
  28 +
  29 +#判断是否是整形
  30 +function isNumber(){
  31 + if [ -n "$(echo $1| sed -n "/^[0-9]\+$/p")" ]
  32 + then
  33 + return $successful
  34 + else
  35 + return $portTypeError
  36 + fi
  37 +}
  38 +
  39 +#通过端口查看服务状态
  40 +#param port
  41 +function checkServerByPort(){
  42 +#echo "port -> $1"
  43 +count=`netstat -ntl|awk '{print $4}'| grep $1|wc -l`
  44 +#echo "count -> $count"
  45 +if [ $count -lt 1 ]
  46 +then
  47 + return $serverNotRunning
  48 +else
  49 + return $successful
  50 +fi
  51 +}
  52 +
  53 +#查看容器服务
  54 +#param containerId
  55 +#param port
  56 +function checkContainerServer(){
  57 +containerId=$1
  58 +port=$2
  59 +count=`docker ps -a |grep $containerId | grep $containerServerUp |wc -l`
  60 +if [ $count == 1 ]
  61 +then
  62 + sleep 2s
  63 + checkServerByPort $port
  64 + serverStatus=" $? "
  65 + #echo "serverStatus-> $serverStatus"
  66 + if [ $serverStatus == $successful ]
  67 + then
  68 + return $successful
  69 + else
  70 + return $serverStatus
  71 + fi
  72 +else
  73 + return $containerNoRunning
  74 +fi
  75 +}
  76 +
  77 +#检查容器id参数
  78 +#param containerId
  79 +function checkContainerId(){
  80 + containerId=$1
  81 + if [ $containerId ]
  82 + then
  83 + checkId=`docker ps -a | awk '{print $1}' |grep $containerId `
  84 + if [ $checkId = $containerId ]
  85 + then
  86 + return $successful
  87 + else
  88 + return $containerIdNotExist
  89 + fi
  90 + else
  91 + return $containerIdIsEmpty
  92 + fi
  93 +}
  94 +
  95 +#检查端口参数
  96 +#param port
  97 +function checkPortParam(){
  98 + port=$1
  99 + if [ $port ]
  100 + then
  101 + isNumber $port
  102 + portParamType=" $? "
  103 + if [ $portParamType == $successful ]
  104 + then
  105 + return $successful
  106 + else
  107 + return $portParamType
  108 + fi
  109 + else
  110 + return $portIsEmpty
  111 + fi
  112 +}
  113 +
  114 +
  115 +
  116 +#启动服务
  117 +function deployService(){
  118 + port=$1
  119 + containerId=`docker run --runtime=nvidia -e NVIDIA_VISIBLE_DEVICES=all -e NVIDIA_DRIVER_CAPABILITIES=all --network host --name port-$port --ulimit core=0:0 -v /home/$USER/zksy/:/root/zksy/ -d $serverImgae /bin/bash -c "/software/monitor/vehicleServerEngine-start.sh $port && tail -f /software/monitor/watchdog.out "`
  120 + containerId=${containerId:0:12}
  121 + #echo "containerId -> $containerId"
  122 + checkContainerId $containerId
  123 + containerIdStatus=" $? "
  124 + if [ $containerIdStatus == $successful ]
  125 + then
  126 + int=1
  127 + containerServerStatus=$defaultError
  128 + while(( $int<=5 ))
  129 + do
  130 + checkContainerServer $containerId $port
  131 + containerServerStatus=" $? "
  132 + #echo "containerServerStatus -> $containerServerStatus $int "
  133 + if [ $containerServerStatus == $successful ]
  134 + then
  135 + echo "code $successful containerId $containerId"
  136 + return $successful
  137 + fi
  138 + int=`expr $int + 1`
  139 + done
  140 + return $containerServerStatus
  141 + else
  142 + return $containerIdStatus
  143 + fi
  144 +}
  145 +
  146 +#检查服务镜像是否存在
  147 +#参数 是否首次加载镜像 0:yes 1;no
  148 +function checkServerImage(){
  149 +image=`docker images |grep $serverImageName |awk '{print $1":"$2}'|grep $serverImgae`
  150 +if [[ -n "$image" && $image = $serverImgae ]]
  151 +then
  152 + echoResult $successful
  153 +else
  154 + if [ $1 == $firstLoad ]
  155 + then
  156 + loadServerPackage
  157 + else
  158 + echoResult $localImageNotExist
  159 + fi
  160 +fi
  161 +}
  162 +
  163 +#加载本地镜像包
  164 +function loadServerPackage(){
  165 + docker import $serverPackage $serverImgae
  166 + checkServerImage $multipleLoad
  167 +}
  168 +
  169 +#输出结果
  170 +function echoResult(){
  171 +result=$1
  172 +echo "code $result"
  173 +}
  174 +
  175 +#启动容器
  176 +#param port
  177 +function start(){
  178 + port=$1
  179 + checkPortParam $port
  180 + portParamStatus=" $? "
  181 + if [ $portParamStatus == $successful ]
  182 + then
  183 + checkServerByPort $port
  184 + portStatus=" $? "
  185 + if [ $portStatus == $serverNotRunning ]
  186 + then
  187 + deployService $port
  188 + deployServiceStatus=" $? "
  189 + if [ $deployServiceStatus != $successful ]
  190 + then
  191 + echoResult $deployServiceStatus
  192 + fi
  193 + else
  194 + echoResult $portOccupied
  195 + fi
  196 + else
  197 + echoResult $portParamStatus
  198 + fi
  199 +}
  200 +
  201 +#重启容器
  202 +#param containerId
  203 +#param port
  204 +function restart(){
  205 + containerId=$1
  206 + port=$2
  207 + checkContainerId $containerId
  208 + containerIdStatus=" $? "
  209 + if [ $containerIdStatus == $successful ]
  210 + then
  211 + checkPortParam $port
  212 + portParamStatus=" $? "
  213 + if [ $portParamStatus == $successful ]
  214 + then
  215 + docker restart $containerId
  216 + int=1
  217 + containerServerStatus=$defaultError
  218 + while(( $int<=5 ))
  219 + do
  220 + checkContainerServer $containerId $port
  221 + containerServerStatus=" $? "
  222 + if [ $containerServerStatus == $successful ]
  223 + then
  224 + break
  225 + fi
  226 + int=`expr $int + 1`
  227 + done
  228 + echoResult $containerServerStatus
  229 + else
  230 + echoResult $portParamStatus
  231 + fi
  232 + else
  233 + echoResult $containerIdStatus
  234 + fi
  235 +}
  236 +
  237 +#暂停容器
  238 +#param containerId
  239 +#param port
  240 +function stop(){
  241 + containerId=$1
  242 + port=$2
  243 + checkContainerId $containerId
  244 + containerIdStatus=" $? "
  245 + if [ $containerIdStatus == $successful ]
  246 + then
  247 + checkPortParam $port
  248 + portParamStatus=" $? "
  249 + if [ $portParamStatus == $successful ]
  250 + then
  251 + docker stop $containerId
  252 + sleep 5s
  253 + count=`docker ps -a |grep $containerId | grep $containerServerExited |wc -l`
  254 + if [ $count == 1 ]
  255 + then
  256 + echoResult $successful
  257 + else
  258 + echoResult $failed
  259 + fi
  260 + else
  261 + echoResult $portParamStatus
  262 + fi
  263 + else
  264 + echoResult $containerIdStatus
  265 + fi
  266 +}
  267 +
  268 +#查看容器容器
  269 +#param containerId
  270 +#param port
  271 +function status(){
  272 + containerId=$1
  273 + port=$2
  274 + checkContainerId $containerId
  275 + containerIdStatus=" $? "
  276 + if [ $containerIdStatus == $successful ]
  277 + then
  278 + checkPortParam $port
  279 + portParamStatus=" $? "
  280 + if [ $portParamStatus == $successful ]
  281 + then
  282 + checkContainerServer $containerId $port
  283 + containerServerStatus=" $? "
  284 + echoResult $containerServerStatus
  285 + else
  286 + echoResult $portParamStatus
  287 + fi
  288 + else
  289 + echoResult $containerIdStatus
  290 + fi
  291 +}
  292 +
  293 +#删除容器
  294 +#param containerId
  295 +function remove(){
  296 + containerId=$1
  297 + checkContainerId $containerId
  298 + containerIdStatus=" $? "
  299 + if [ $containerIdStatus == $successful ]
  300 + then
  301 + docker stop $containerId && docker rm $containerId
  302 + sleep 2s
  303 + checkContainerId $containerId
  304 + containerIdRemoveStatus=" $? "
  305 + if [ $containerIdRemoveStatus == $containerIdNotExist ]
  306 + then
  307 + echoResult $successful
  308 + else
  309 + echoResult $failed
  310 + fi
  311 + else
  312 + echoResult $containerIdStatus
  313 + fi
  314 +}
  315 +
  316 +
  317 +case $functionType in
  318 + start)
  319 + start $2
  320 + ;;
  321 + stop)
  322 + stop $2 $3
  323 + ;;
  324 + restart)
  325 + restart $2 $3
  326 + ;;
  327 + status)
  328 + status $2 $3
  329 + ;;
  330 + load)
  331 + checkServerImage $firstLoad
  332 + ;;
  333 + remove)
  334 + remove $2
  335 + ;;
  336 + *)
  337 + echoResult $methodParameterError
  338 +
  339 +esac
  340 +
  341 +exit 0
  342 +
  343 +
  344 +
  345 +
  346 +
  347 +
  348 +
... ...
src/main/java/com/objecteye/vo/VSyServiceMainTable.java
... ... @@ -32,4 +32,8 @@ public class VSyServiceMainTable implements Serializable {
32 32 * 最后更新
33 33 */
34 34 private String updateTime;
  35 + /**
  36 + * 配置主键
  37 + */
  38 + private Integer configId;
35 39 }
... ...