|
|
@@ -31,7 +31,7 @@ import java.util.List;
|
|
|
*/
|
|
|
@Slf4j
|
|
|
@Data
|
|
|
-@EqualsAndHashCode(callSuper=false)
|
|
|
+@EqualsAndHashCode(callSuper = false)
|
|
|
public class JavaImageBuildHandler extends AbstractHandler {
|
|
|
|
|
|
public final DockerApi dockerApi;
|
|
|
@@ -63,18 +63,18 @@ public class JavaImageBuildHandler extends AbstractHandler {
|
|
|
Git git = gitApi.clone(repoUrl, directory.toString());
|
|
|
git.checkout().setName(branchName).call();
|
|
|
} catch (Exception e) {
|
|
|
- context.appendErrorResult(PipelineException.GIT_CLONE_ERROR,e);
|
|
|
- throw new PipelineException(HttpStatus.SC_SERVICE_UNAVAILABLE, PipelineException.GIT_CLONE_ERROR,e);
|
|
|
+ context.appendErrorResult(PipelineException.GIT_CLONE_ERROR, e);
|
|
|
+ throw new PipelineException(HttpStatus.SC_SERVICE_UNAVAILABLE, PipelineException.GIT_CLONE_ERROR, e);
|
|
|
}
|
|
|
//2. 帮助学生创建dockerfile和maven的setting.xml文件, 读取学生项目architect名字
|
|
|
Path dockerfile = Paths.get(directory.toString(), "Dockerfile");
|
|
|
Path mavenSetting = Paths.get(directory.toString(), "settings.xml");
|
|
|
try {
|
|
|
- PipelineTemplateTable.copyTemplateDockerfile(templateName,dockerfile);
|
|
|
+ PipelineTemplateTable.copyTemplateDockerfile(templateName, dockerfile);
|
|
|
PipelineTemplateTable.copyTemplateMavenSetting(mavenSetting);
|
|
|
} catch (IOException e) {
|
|
|
- context.appendErrorResult(PipelineException.DOCKERFILE_CREATE_ERROR,e);
|
|
|
- throw new PipelineException(HttpStatus.SC_SERVICE_UNAVAILABLE,PipelineException.DOCKERFILE_CREATE_ERROR,e);
|
|
|
+ context.appendErrorResult(PipelineException.DOCKERFILE_CREATE_ERROR, e);
|
|
|
+ throw new PipelineException(HttpStatus.SC_SERVICE_UNAVAILABLE, PipelineException.DOCKERFILE_CREATE_ERROR, e);
|
|
|
}
|
|
|
|
|
|
//3. 修改dockerfile内部需要替换的内容
|
|
|
@@ -82,13 +82,13 @@ public class JavaImageBuildHandler extends AbstractHandler {
|
|
|
try {
|
|
|
dockerfileLines = Files.readAllLines(dockerfile);
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
- for (String line: dockerfileLines){
|
|
|
- line = line.replace("-jar name-",jarName);
|
|
|
- sb.append(line+"\n");
|
|
|
+ for (String line : dockerfileLines) {
|
|
|
+ line = line.replace("-jar name-", jarName);
|
|
|
+ sb.append(line + "\n");
|
|
|
}
|
|
|
- Files.write(dockerfile,sb.toString().getBytes(), StandardOpenOption.WRITE);
|
|
|
+ Files.write(dockerfile, sb.toString().getBytes(), StandardOpenOption.WRITE);
|
|
|
} catch (IOException e) {
|
|
|
- throw new PipelineException(HttpStatus.SC_SERVICE_UNAVAILABLE,PipelineException.DOCKERFILE_MODIFY_ERROR,e);
|
|
|
+ throw new PipelineException(HttpStatus.SC_SERVICE_UNAVAILABLE, PipelineException.DOCKERFILE_MODIFY_ERROR, e);
|
|
|
}
|
|
|
//4. 镜像构建
|
|
|
String imageTag = String.valueOf(new Date().getTime());
|
|
|
@@ -97,30 +97,30 @@ public class JavaImageBuildHandler extends AbstractHandler {
|
|
|
final long MAX_BUFFER_SIZE = 1 << 18;
|
|
|
try {
|
|
|
dockerApi.buildAndPush(directory.toString(), imageName, imageTag, new ProgressHandler() {
|
|
|
- @Override
|
|
|
- public void progress(ProgressMessage message) throws DockerException {
|
|
|
- String value = message.stream();
|
|
|
- if (value == null) {
|
|
|
- value = "";
|
|
|
- }
|
|
|
- if (!StringUtils.isEmpty(message.error())) {
|
|
|
- value += "\n" + message.error();
|
|
|
- }
|
|
|
- sb.append(value);
|
|
|
- if (sb.length() > DEFAULT_BUFFER_LENGTH) {
|
|
|
- context.appendResult(sb.toString());
|
|
|
- if (context.getResult().length() > MAX_BUFFER_SIZE) {
|
|
|
- context.setResult(context.getResult().substring((int)(context.getResult().length() - MAX_BUFFER_SIZE)));
|
|
|
- }
|
|
|
- sb.setLength(0);
|
|
|
+ @Override
|
|
|
+ public void progress(ProgressMessage message) throws DockerException {
|
|
|
+ String value = message.stream();
|
|
|
+ if (value == null) {
|
|
|
+ value = "";
|
|
|
+ }
|
|
|
+ if (!StringUtils.isEmpty(message.error())) {
|
|
|
+ value += "\n" + message.error();
|
|
|
+ }
|
|
|
+ sb.append(value);
|
|
|
+ if (sb.length() > DEFAULT_BUFFER_LENGTH) {
|
|
|
+ context.appendResult(sb.toString());
|
|
|
+ if (context.getResult().length() > MAX_BUFFER_SIZE) {
|
|
|
+ context.setResult(context.getResult().substring((int) (context.getResult().length() - MAX_BUFFER_SIZE)));
|
|
|
}
|
|
|
+ sb.setLength(0);
|
|
|
}
|
|
|
- });
|
|
|
- } catch (Exception e) {
|
|
|
- context.appendErrorResult(PipelineException.IMAGE_BUILD_ERROR,e);
|
|
|
- context.setResult(context.getResult() + "IMAGE BUILD DETAILS:" + sb.toString());
|
|
|
- throw new PipelineException(HttpStatus.SC_SERVICE_UNAVAILABLE, PipelineException.IMAGE_BUILD_ERROR ,e);
|
|
|
- }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } catch (Exception e) {
|
|
|
+ context.setResult(context.getResult() + "IMAGE BUILD DETAILS:" + sb.toString());
|
|
|
+ context.appendErrorResult(PipelineException.IMAGE_BUILD_ERROR, e);
|
|
|
+ throw new PipelineException(HttpStatus.SC_SERVICE_UNAVAILABLE, PipelineException.IMAGE_BUILD_ERROR, e);
|
|
|
+ }
|
|
|
//5. 将镜像名字加入context
|
|
|
context.getConfigs().put("imageName", imageName);
|
|
|
context.getConfigs().put("imageTag", imageTag);
|