84 lines
3.3 KiB
Java
84 lines
3.3 KiB
Java
package net.geedge.confagent.controller;
|
|
|
|
import cn.hutool.log.Log;
|
|
import net.geedge.confagent.util.ConfagentUtil;
|
|
import net.geedge.confagent.util.R;
|
|
import net.geedge.confagent.util.RCode;
|
|
import net.geedge.confagent.util.Tool;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import java.io.*;
|
|
import java.util.UUID;
|
|
|
|
@RestController
|
|
public class OTAController extends BaseController{
|
|
|
|
private static Log log = Log.get();
|
|
|
|
@Autowired
|
|
private ConfagentUtil confagentUtil;
|
|
|
|
private static final String UPDATE_PACKAGE_PATH = "/tmp/nezha/nz-talon/%s/%s";
|
|
|
|
|
|
@PostMapping("/ota")
|
|
public R ota(HttpServletRequest request, @RequestParam(name = "file", required = false) MultipartFile file, String md5 )throws IOException{
|
|
|
|
//检查token
|
|
String token = request.getHeader("Authorization");
|
|
if (Tool.StrUtil.isBlank(token)) {
|
|
return R.error(RCode.AUTH_TOKEN_ISNULL);
|
|
}else {
|
|
if (confagentUtil.checkToken(token).getCode() != RCode.SUCCESS.getCode())return R.error(RCode.AUTH_TOKEN_INVALID);
|
|
}
|
|
|
|
//将文件缓存到 /tmp 目录下
|
|
String updateFileName = file.getOriginalFilename();
|
|
File updatePackagePath = Tool.FileUtil.file(String.format(UPDATE_PACKAGE_PATH , UUID.randomUUID().toString().replaceAll("-", ""), updateFileName));
|
|
updatePackagePath.mkdirs();
|
|
try {
|
|
file.transferTo(updatePackagePath);
|
|
} catch (Exception e) {
|
|
return R.error(RCode.OTA_FILE_CACHE_ERROR);
|
|
}
|
|
|
|
//校验MD5
|
|
String updateFileMd5 = Tool.DigestUtil.md5Hex(new FileInputStream(updatePackagePath));
|
|
log.info("updateFileMd5 : {}",updateFileMd5);
|
|
md5 = Tool.AesUtil.decrypt(md5 , AES_SECRET_KEY);
|
|
if(!updateFileMd5.equals(md5)){
|
|
return R.error(RCode.OTA_MD5_ERROR);
|
|
}
|
|
|
|
//校验版本号
|
|
String fileName = file.getOriginalFilename();
|
|
String updateVersion = fileName.split("-")[2];
|
|
updateVersion = updateVersion.substring(0,updateVersion.lastIndexOf("."));
|
|
log.info("current version : {} , update version : {}", currentVersion , updateVersion);
|
|
if( !(Tool.DateUtil.betweenDay(Tool.DateUtil.parseDate(updateVersion),Tool.DateUtil.parseDate(currentVersion), false) > 0)){
|
|
return R.error(RCode.OTA_VERSION_ERROR);
|
|
}
|
|
|
|
|
|
//检查atd服务是否启动
|
|
InputStream atdStatus = Tool.RuntimeUtil.exec("systemctl status atd").getInputStream();
|
|
String atdStatusStr = Tool.IoUtil.read(atdStatus).toString();
|
|
if(!atdStatusStr.contains("Active: active (running)")){
|
|
return R.error(RCode.OTA_ATD_SERVICE_DOWN);
|
|
}
|
|
|
|
//延迟一分钟更新 使用echo 将更新命令传入at
|
|
String atCommand ="echo \"rpm -Uvh %s\" | at now +1 minutes";
|
|
atCommand = String.format(atCommand,updatePackagePath);
|
|
log.info("update nz-agent : {}", atCommand);
|
|
new ProcessBuilder("/bin/bash", "-c", atCommand).start();
|
|
return R.ok();
|
|
}
|
|
|
|
}
|