use of com.orion.utils.io.Files1 in project orion-kit by lijiahangmax.
the class GzDecompressor method doDecompress.
@Override
public void doDecompress() throws Exception {
OutputStream out = null;
try {
if (decompressInputStream != null) {
this.inputStream = new GzipCompressorInputStream(decompressInputStream);
} else {
this.inputStream = new GzipCompressorInputStream(Files1.openInputStreamFast(decompressFile));
}
String entityName = Optional.ofNullable(inputStream.getMetaData()).map(GzipParameters::getFilename).orElse(null);
if (decompressTargetFileName == null) {
// 配置 > entity > file > objectId
this.decompressTargetFileName = Objects1.def(entityName, Optional.ofNullable(decompressFile).map(File::getName).map(s -> s.substring(0, s.length() - suffix.length() - 1)).orElseGet(ObjectIds::next));
}
if (decompressTargetOutputStream != null) {
out = decompressTargetOutputStream;
} else {
this.decompressTargetFile = new File(decompressTargetPath, decompressTargetFileName);
out = Files1.openOutputStream(decompressTargetFile);
}
Streams.transfer(inputStream, out);
} finally {
Streams.close(inputStream);
if (decompressTargetOutputStream == null) {
Streams.close(out);
}
}
}
use of com.orion.utils.io.Files1 in project orion-ops by lijiahangmax.
the class FileDownloadServiceImpl method getDownloadToken.
@Override
public String getDownloadToken(Long id, FileDownloadType type) {
String path = null;
String name = null;
Long machineId = Const.HOST_MACHINE_ID;
// 获取日志绝对路径
switch(type) {
case SECRET_KEY:
// 秘钥
path = this.getDownloadSecretKeyFilePath(id);
name = Optional.ofNullable(path).map(Files1::getFileName).orElse(null);
break;
case TERMINAL_LOG:
// terminal 日志
path = this.getDownloadTerminalLogFilePath(id);
name = Optional.ofNullable(path).map(Files1::getFileName).orElse(null);
break;
case EXEC_LOG:
// 执行日志
path = commandExecService.getExecLogFilePath(id);
name = Optional.ofNullable(path).map(Files1::getFileName).orElse(null);
break;
case SFTP_DOWNLOAD:
// sftp 下载文件
FileTransferLogDO transferLog = sftpService.getDownloadFilePath(id);
if (transferLog != null) {
path = transferLog.getLocalFile();
name = Files1.getFileName(transferLog.getRemoteFile());
}
break;
case TAIL_LIST_FILE:
// tail 列表文件
FileTailListDO tailFile = fileTailListDAO.selectById(id);
if (tailFile != null) {
path = tailFile.getFilePath();
name = Files1.getFileName(path);
machineId = tailFile.getMachineId();
}
break;
case APP_BUILD_LOG:
// 应用构建日志
path = applicationBuildService.getBuildLogPath(id);
name = Optional.ofNullable(path).map(Files1::getFileName).orElse(null);
break;
case APP_BUILD_ACTION_LOG:
case APP_RELEASE_ACTION_LOG:
// 应用构建操作日志
path = applicationActionLogService.getActionLogPath(id);
name = Optional.ofNullable(path).map(Files1::getFileName).orElse(null);
break;
case APP_BUILD_BUNDLE:
// 应用构建 产物文件
path = applicationBuildService.getBuildBundlePath(id);
// 如果是文件夹则获取压缩文件
if (path != null && Files1.isDirectory(path)) {
path += "." + Const.SUFFIX_ZIP;
}
name = Optional.ofNullable(path).map(Files1::getFileName).orElse(null);
break;
case APP_RELEASE_MACHINE_LOG:
// 应用发布机器日志
path = applicationReleaseMachineService.getReleaseMachineLogPath(id);
name = Optional.ofNullable(path).map(Files1::getFileName).orElse(null);
break;
case SCHEDULER_TASK_MACHINE_LOG:
// 调度任务机器日志
path = schedulerTaskMachineRecordService.getTaskMachineLogPath(id);
name = Optional.ofNullable(path).map(Files1::getFileName).orElse(null);
break;
default:
break;
}
// 检查文件是否存在
if (path == null || (type.isLocal() && !Files1.isFile(path))) {
throw Exceptions.httpWrapper(HttpWrapper.of(ResultCode.FILE_MISSING));
}
// 设置缓存
FileDownloadDTO download = new FileDownloadDTO();
download.setFilePath(path);
download.setFileName(Strings.def(name, Const.UNKNOWN));
download.setUserId(Currents.getUserId());
download.setType(type.getType());
download.setMachineId(machineId);
String token = UUIds.random19();
String key = Strings.format(KeyConst.FILE_DOWNLOAD_TOKEN, token);
redisTemplate.opsForValue().set(key, JSON.toJSONString(download), KeyConst.FILE_DOWNLOAD_EXPIRE, TimeUnit.SECONDS);
return token;
}
use of com.orion.utils.io.Files1 in project orion-ops by lijiahangmax.
the class ApplicationBuildServiceImpl method submitBuildTask.
@Override
public Long submitBuildTask(ApplicationBuildRequest request) {
Long appId = request.getAppId();
Long profileId = request.getProfileId();
// 查询应用
ApplicationInfoDO app = applicationInfoDAO.selectById(appId);
Valid.notNull(app, MessageConst.APP_ABSENT);
// 查询环境
ApplicationProfileDO profile = applicationProfileDAO.selectById(profileId);
Valid.notNull(profile, MessageConst.PROFILE_ABSENT);
// 查询应用执行块
List<ApplicationActionDO> actions = applicationActionService.getAppProfileActions(appId, profileId, StageType.BUILD.getType());
Valid.notEmpty(actions, MessageConst.APP_PROFILE_NOT_CONFIGURED);
UserDTO user = Currents.getUser();
// 获取构建序列
Integer buildSeq = applicationEnvService.getBuildSeqAndIncrement(appId, profileId);
// 设置构建参数
ApplicationBuildDO buildTask = new ApplicationBuildDO();
buildTask.setAppId(appId);
buildTask.setAppName(app.getAppName());
buildTask.setAppTag(app.getAppTag());
buildTask.setProfileId(profileId);
buildTask.setProfileName(profile.getProfileName());
buildTask.setProfileTag(profile.getProfileTag());
buildTask.setBuildSeq(buildSeq);
buildTask.setBranchName(request.getBranchName());
buildTask.setCommitId(request.getCommitId());
buildTask.setVcsId(app.getVcsId());
buildTask.setBuildStatus(BuildStatus.WAIT.getStatus());
buildTask.setDescription(request.getDescription());
buildTask.setCreateUserId(user.getId());
buildTask.setCreateUserName(user.getUsername());
applicationBuildDAO.insert(buildTask);
Long buildId = buildTask.getId();
// 设置目录信息
buildTask.setLogPath(PathBuilders.getBuildLogPath(buildId));
String bundlePath = applicationEnvService.getAppEnvValue(appId, profileId, ApplicationEnvAttr.BUNDLE_PATH.getKey());
buildTask.setBundlePath(PathBuilders.getBuildBundlePath(buildId) + "/" + Files1.getFileName(bundlePath));
// 更新构建信息
applicationBuildDAO.updateById(buildTask);
// 检查是否包含环境变量命令
final boolean hasEnvCommand = actions.stream().filter(s -> ActionType.BUILD_COMMAND.getType().equals(s.getActionType())).map(ApplicationActionDO::getActionCommand).filter(Strings::isNotBlank).anyMatch(s -> s.contains(EnvConst.SYMBOL));
Map<String, String> env = Maps.newMap();
if (hasEnvCommand) {
// 查询应用环境变量
env.putAll(applicationEnvService.getAppProfileFullEnv(appId, profileId));
// 查询机器环境变量
env.putAll(machineEnvService.getFullMachineEnv(Const.HOST_MACHINE_ID));
// 查询系统环境变量
env.putAll(systemEnvService.getFullSystemEnv());
// 添加构建环境变量
env.putAll(this.getBuildEnv(buildId, buildSeq, app.getVcsId(), request));
}
// 设置action
for (ApplicationActionDO action : actions) {
ApplicationActionLogDO actionLog = new ApplicationActionLogDO();
actionLog.setRelId(buildId);
actionLog.setStageType(StageType.BUILD.getType());
actionLog.setMachineId(Const.HOST_MACHINE_ID);
actionLog.setActionId(action.getAppId());
actionLog.setActionName(action.getActionName());
actionLog.setActionType(action.getActionType());
if (ActionType.BUILD_COMMAND.equals(ActionType.of(action.getActionType()))) {
actionLog.setActionCommand(Strings.format(action.getActionCommand(), EnvConst.SYMBOL, env));
}
actionLog.setRunStatus(ActionStatus.WAIT.getStatus());
applicationActionLogDAO.insert(actionLog);
// 设置日志路径
actionLog.setLogPath(PathBuilders.getBuildActionLogPath(buildId, actionLog.getId()));
// 更新
applicationActionLogDAO.updateById(actionLog);
}
// 提交构建任务
log.info("提交应用构建任务 buildId: {}", buildId);
BuildMachineProcessor buildProcessor = new BuildMachineProcessor(buildId);
Threads.start(buildProcessor, SchedulerPools.APP_BUILD_SCHEDULER);
// 设置日志参数
EventParamsHolder.addParams(buildTask);
return buildId;
}
use of com.orion.utils.io.Files1 in project orion-ops by lijiahangmax.
the class ApplicationVcsServiceImpl method cleanBuildVcs.
@Override
public void cleanBuildVcs(Long id) {
// 查询数据
ApplicationVcsDO vcs = applicationVcsDAO.selectById(id);
Valid.notNull(vcs, MessageConst.UNKNOWN_DATA);
// 设置日志参数
EventParamsHolder.addParam(EventKeys.ID, id);
EventParamsHolder.addParam(EventKeys.NAME, vcs.getVcsName());
File rootPath = new File(Files1.getPath(SystemEnvAttr.VCS_PATH.getValue(), id + Const.EMPTY));
if (!Files1.isDirectory(rootPath)) {
return;
}
// 查询文件夹
File[] files = rootPath.listFiles(e -> !e.getName().equals(Const.EVENT) && e.isDirectory() && Strings.isInteger(e.getName()));
if (Arrays1.isEmpty(files)) {
return;
}
// 保留两个版本 防止清空正在进行中的构建任务
int length = files.length;
if (length <= 2) {
return;
}
Arrays.sort(files, Comparator.comparing(s -> Integer.parseInt(s.getName())));
for (int i = 0; i < length - 2; i++) {
Files1.delete(files[i]);
}
}
use of com.orion.utils.io.Files1 in project orion-ops by lijiahangmax.
the class MachineKeyServiceImpl method listKeys.
@Override
public DataGrid<MachineSecretKeyVO> listKeys(MachineKeyRequest request) {
final int page = request.getPage();
final int limit = request.getLimit();
final boolean checkStatus = request.getMountStatus() != null;
if (checkStatus) {
request.setPage(Const.N_1);
request.setLimit(Const.N_100000);
}
LambdaQueryWrapper<MachineSecretKeyDO> wrapper = new LambdaQueryWrapper<MachineSecretKeyDO>().like(Strings.isNotBlank(request.getName()), MachineSecretKeyDO::getKeyName, request.getName()).like(Strings.isNotBlank(request.getDescription()), MachineSecretKeyDO::getDescription, request.getDescription()).orderByDesc(MachineSecretKeyDO::getCreateTime);
DataGrid<MachineSecretKeyVO> dataGrid = DataQuery.of(machineSecretKeyDAO).page(request).wrapper(wrapper).dataGrid(MachineSecretKeyVO.class);
if (!dataGrid.isEmpty()) {
List<String> loadKeys = SessionHolder.getLoadKeys();
for (MachineSecretKeyVO row : dataGrid.getRows()) {
String path = row.getPath();
boolean isFile = Files1.isFile(new File(MachineKeyService.getKeyPath(path)));
if (isFile) {
boolean match = loadKeys.stream().anyMatch(key -> key.endsWith(path));
if (match) {
row.setMountStatus(MountKeyStatus.MOUNTED.getStatus());
} else {
row.setMountStatus(MountKeyStatus.DUMPED.getStatus());
}
} else {
row.setMountStatus(MountKeyStatus.NOT_FOUND.getStatus());
}
}
}
if (!checkStatus) {
return dataGrid;
} else {
// 手动过滤
List<MachineSecretKeyVO> totalRows = dataGrid.stream().filter(row -> request.getMountStatus().equals(row.getMountStatus())).collect(Collectors.toList());
List<MachineSecretKeyVO> rows = new LimitList<>(totalRows, limit).page(page);
// 封装返回
DataGrid<MachineSecretKeyVO> newDataGrid = DataGrid.of(rows, totalRows.size());
newDataGrid.setPage(page);
newDataGrid.setLimit(limit);
return newDataGrid;
}
}
Aggregations