mirror of
https://github.com/PowerJob/PowerJob.git
synced 2025-07-17 00:00:04 +08:00
[dev] use git4j and maven-invoker to compile container
This commit is contained in:
parent
67729123d2
commit
f6ba0783c2
@ -0,0 +1,21 @@
|
||||
package com.github.kfcfans.oms.common.model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* Git代码库信息
|
||||
*
|
||||
* @author tjq
|
||||
* @since 2020/5/17
|
||||
*/
|
||||
@Data
|
||||
public class GitRepoInfo {
|
||||
// 仓库地址
|
||||
private String repo;
|
||||
// 分支名称
|
||||
private String branch;
|
||||
// 用户名
|
||||
private String username;
|
||||
// 密码
|
||||
private String password;
|
||||
}
|
@ -20,6 +20,8 @@
|
||||
<mysql.version>8.0.19</mysql.version>
|
||||
<h2.db.version>1.4.200</h2.db.version>
|
||||
<zip4j.version>2.5.2</zip4j.version>
|
||||
<jgit.version>5.7.0.202003110725-r</jgit.version>
|
||||
<mvn.invoker.version>3.0.1</mvn.invoker.version>
|
||||
|
||||
<!-- 部署时跳过该module -->
|
||||
<maven.deploy.skip>true</maven.deploy.skip>
|
||||
@ -75,13 +77,28 @@
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- zip4j -->
|
||||
<!-- zip4j(Zip操作) -->
|
||||
<dependency>
|
||||
<groupId>net.lingala.zip4j</groupId>
|
||||
<artifactId>zip4j</artifactId>
|
||||
<version>${zip4j.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- jGit(Git操作) -->
|
||||
<dependency>
|
||||
<groupId>org.eclipse.jgit</groupId>
|
||||
<artifactId>org.eclipse.jgit</artifactId>
|
||||
<version>${jgit.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Maven Invoker(编译 maven 项目) -->
|
||||
<dependency>
|
||||
<groupId>org.apache.maven.shared</groupId>
|
||||
<artifactId>maven-invoker</artifactId>
|
||||
<version>${mvn.invoker.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- swagger2 -->
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
|
@ -20,6 +20,8 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 处理 Worker 请求
|
||||
*
|
||||
@ -85,10 +87,11 @@ public class ServerActor extends AbstractActor {
|
||||
Environment environment = SpringUtils.getBean(Environment.class);
|
||||
String port = environment.getProperty("local.server.port");
|
||||
|
||||
ContainerInfoDO containerInfo = containerInfoRepository.findByContainerName(req.getContainerName());
|
||||
Optional<ContainerInfoDO> containerInfoOpt = containerInfoRepository.findByContainerName(req.getContainerName());
|
||||
AskResponse askResponse = new AskResponse();
|
||||
askResponse.setSuccess(false);
|
||||
if (containerInfo != null) {
|
||||
if (containerInfoOpt.isPresent()) {
|
||||
ContainerInfoDO containerInfo = containerInfoOpt.get();
|
||||
askResponse.setSuccess(true);
|
||||
|
||||
ServerDeployContainerRequest dpReq = new ServerDeployContainerRequest();
|
||||
|
@ -36,6 +36,9 @@ public class ContainerInfoDO {
|
||||
// 状态,枚举值为 ContainerStatus
|
||||
private Integer status;
|
||||
|
||||
// 上一次部署时间
|
||||
private Date lastDeployTime;
|
||||
|
||||
private Date gmtCreate;
|
||||
private Date gmtModified;
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import com.github.kfcfans.oms.server.persistence.core.model.ContainerInfoDO;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 容器信息 数据操作层
|
||||
@ -15,6 +16,6 @@ public interface ContainerInfoRepository extends JpaRepository<ContainerInfoDO,
|
||||
|
||||
List<ContainerInfoDO> findByAppId(Long appId);
|
||||
|
||||
ContainerInfoDO findByContainerName(String containerName);
|
||||
Optional<ContainerInfoDO> findByContainerName(String containerName);
|
||||
|
||||
}
|
||||
|
@ -1,15 +1,34 @@
|
||||
package com.github.kfcfans.oms.server.service;
|
||||
|
||||
import com.github.kfcfans.oms.common.model.GitRepoInfo;
|
||||
import com.github.kfcfans.oms.common.utils.CommonUtils;
|
||||
import com.github.kfcfans.oms.common.utils.JsonUtils;
|
||||
import com.github.kfcfans.oms.server.common.constans.ContainerSourceType;
|
||||
import com.github.kfcfans.oms.server.common.utils.OmsFileUtils;
|
||||
import com.github.kfcfans.oms.server.persistence.core.model.ContainerInfoDO;
|
||||
import com.github.kfcfans.oms.server.persistence.core.repository.ContainerInfoRepository;
|
||||
import com.google.common.collect.Lists;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.io.filefilter.FileFilterUtils;
|
||||
import org.apache.commons.io.filefilter.IOFileFilter;
|
||||
import org.apache.maven.shared.invoker.*;
|
||||
import org.eclipse.jgit.api.CloneCommand;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.transport.CredentialsProvider;
|
||||
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.mongodb.gridfs.GridFsResource;
|
||||
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.io.FilenameFilter;
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 容器服务
|
||||
@ -21,6 +40,9 @@ import java.io.File;
|
||||
@Service
|
||||
public class ContainerService {
|
||||
|
||||
@Resource
|
||||
private ContainerInfoRepository containerInfoRepository;
|
||||
|
||||
private GridFsTemplate gridFsTemplate;
|
||||
|
||||
/**
|
||||
@ -37,12 +59,28 @@ public class ContainerService {
|
||||
return jarFile;
|
||||
}
|
||||
if (gridFsTemplate != null) {
|
||||
downloadJarFromMongoDB(genContainerJarName(md5), jarFile);
|
||||
downloadJarFromGridFS(genContainerJarName(md5), jarFile);
|
||||
}
|
||||
return jarFile;
|
||||
}
|
||||
|
||||
private void downloadJarFromMongoDB(String mongoFileName, File targetFile) {
|
||||
/**
|
||||
* 部署容器
|
||||
* @param containerName 容器名称
|
||||
*/
|
||||
public void deploy(String containerName) throws Exception {
|
||||
Optional<ContainerInfoDO> containerInfoOpt = containerInfoRepository.findByContainerName(containerName);
|
||||
ContainerInfoDO container = containerInfoOpt.orElseThrow(() -> new IllegalArgumentException("can't find container by name: "+ containerName));
|
||||
|
||||
// 获取Jar,Git需要先 clone成Jar计算MD5,JarFile则直接下载
|
||||
ContainerSourceType sourceType = ContainerSourceType.of(container.getSourceType());
|
||||
if (sourceType == ContainerSourceType.Git) {
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void downloadJarFromGridFS(String mongoFileName, File targetFile) {
|
||||
synchronized (mongoFileName.intern()) {
|
||||
if (targetFile.exists()) {
|
||||
return;
|
||||
@ -61,7 +99,6 @@ public class ContainerService {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static String genContainerJarName(String md5) {
|
||||
return String.format("oms-container-%s.jar", md5);
|
||||
}
|
||||
@ -70,4 +107,54 @@ public class ContainerService {
|
||||
public void setGridFsTemplate(GridFsTemplate gridFsTemplate) {
|
||||
this.gridFsTemplate = gridFsTemplate;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
String gitRepoInfoStr = "{\"repo\":\"https://gitee.com/KFCFans/OhMyScheduler-Container-Template.git\",\"branch\":\"master\"}";
|
||||
|
||||
String workerDirStr = OmsFileUtils.genTemporaryPath();
|
||||
File workerDir = new File(workerDirStr);
|
||||
FileUtils.forceMkdir(workerDir);
|
||||
|
||||
// git clone
|
||||
GitRepoInfo gitRepoInfo = JsonUtils.parseObject(gitRepoInfoStr, GitRepoInfo.class);
|
||||
|
||||
CloneCommand cloneCommand = Git.cloneRepository()
|
||||
.setDirectory(workerDir)
|
||||
.setURI(gitRepoInfo.getRepo())
|
||||
.setBranch(gitRepoInfo.getBranch());
|
||||
if (!StringUtils.isEmpty(gitRepoInfo.getUsername())) {
|
||||
CredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider(gitRepoInfo.getUsername(), gitRepoInfo.getPassword());
|
||||
cloneCommand.setCredentialsProvider(credentialsProvider);
|
||||
}
|
||||
cloneCommand.call();
|
||||
|
||||
// mvn clean package -DskipTests -U
|
||||
Invoker mvnInvoker = new DefaultInvoker();
|
||||
InvocationRequest ivkReq = new DefaultInvocationRequest();
|
||||
ivkReq.setGoals(Lists.newArrayList("clean", "package", "-DskipTests", "-U"));
|
||||
ivkReq.setBaseDirectory(workerDir);
|
||||
ivkReq.setOutputHandler(line -> {
|
||||
System.out.println(line);
|
||||
});
|
||||
|
||||
InvocationResult mvnResult = mvnInvoker.execute(ivkReq);
|
||||
if (mvnResult.getExitCode() != 0) {
|
||||
// TODO:输出失败信息
|
||||
return;
|
||||
}
|
||||
|
||||
String targetDirStr = workerDirStr + "/target";
|
||||
File targetDir = new File(targetDirStr);
|
||||
IOFileFilter fileFilter = FileFilterUtils.asFileFilter((dir, name) -> name.endsWith("jar-with-dependencies.jar"));
|
||||
Collection<File> jarFile = FileUtils.listFiles(targetDir, fileFilter, null);
|
||||
|
||||
if (CollectionUtils.isEmpty(jarFile)) {
|
||||
// TODO:输出失败信息
|
||||
return;
|
||||
}
|
||||
|
||||
File jarWithDependency = jarFile.iterator().next();
|
||||
|
||||
}
|
||||
}
|
||||
|
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user