| skipped 73 lines |
74 | 74 | | |
75 | 75 | | public static final String ENV_OS_INFO = "ONEDEV_OS_INFO"; |
76 | 76 | | |
| 77 | + | public static final String ENV_REGISTRY_LOGINS = "ONEDEV_REGISTRY_LOGINS"; |
| 78 | + | |
77 | 79 | | public static final String BEARER = "Bearer"; |
78 | 80 | | |
79 | 81 | | public static final String LOG_END_MESSAGE = "===== End of OneDev K8s Helper Log ====="; |
| skipped 122 lines |
202 | 204 | | } |
203 | 205 | | |
204 | 206 | | private static void generateCommandScript(List<Integer> position, String stepNames, |
205 | | - | List<String> setupCommands, CommandExecutable commandExecutable, File workspace, |
| 207 | + | List<String> setupCommands, CommandFacade commandFacade, File workspace, |
206 | 208 | | OsInfo osInfo) { |
207 | 209 | | try { |
208 | 210 | | String positionStr = stringifyPosition(position); |
209 | 211 | | File commandHome = getCommandHome(); |
210 | | - | File stepScriptFile = new File(commandHome, "step-" + positionStr + commandExecutable.getScriptExtension()); |
211 | | - | OsExecution execution = commandExecutable.getExecution(osInfo); |
212 | | - | FileUtils.writeLines(stepScriptFile, execution.getCommands(), commandExecutable.getEndOfLine()); |
| 212 | + | File stepScriptFile = new File(commandHome, "step-" + positionStr + commandFacade.getScriptExtension()); |
| 213 | + | OsExecution execution = commandFacade.getExecution(osInfo); |
| 214 | + | FileUtils.writeLines(stepScriptFile, execution.getCommands(), commandFacade.getEndOfLine()); |
213 | 215 | | |
214 | 216 | | if (SystemUtils.IS_OS_WINDOWS) { |
215 | 217 | | StringBuilder escapedStepNames = new StringBuilder(); |
| skipped 27 lines |
243 | 245 | | "cd " + workspace.getAbsolutePath() |
244 | 246 | | + " && cmd /c " + setupScriptFile.getAbsolutePath() |
245 | 247 | | + " && cmd /c echo " + TaskLogger.wrapWithAnsiNotice("Running step ^\"" + escapedStepNames + "^\"...") |
246 | | - | + " && " + commandExecutable.getInterpreter() + " " + stepScriptFile.getAbsolutePath(), |
| 248 | + | + " && " + commandFacade.getInterpreter() + " " + stepScriptFile.getAbsolutePath(), |
247 | 249 | | "set exit_code=%errorlevel%", |
248 | 250 | | "if \"%exit_code%\"==\"0\" (", |
249 | 251 | | " echo " + TaskLogger.wrapWithAnsiSuccess("Step ^\"" + escapedStepNames + "^\" is successful"), |
| skipped 34 lines |
284 | 286 | | "cd " + workspace.getAbsolutePath() |
285 | 287 | | + " && sh " + setupScriptFile.getAbsolutePath() |
286 | 288 | | + " && echo '" + TaskLogger.wrapWithAnsiNotice("Running step \"" + escapedStepNames + "\"...") + "'" |
287 | | - | + " && " + commandExecutable.getInterpreter() + " " + stepScriptFile.getAbsolutePath(), |
| 289 | + | + " && " + commandFacade.getInterpreter() + " " + stepScriptFile.getAbsolutePath(), |
288 | 290 | | "exitCode=\"$?\"", |
289 | 291 | | "if [ $exitCode -eq 0 ]", |
290 | 292 | | "then", |
| skipped 117 lines |
408 | 410 | | commands.add("@echo off"); |
409 | 411 | | commands.add("echo hello from container"); |
410 | 412 | | generateCommandScript(Lists.newArrayList(0), "test", Lists.newArrayList(), |
411 | | - | new CommandExecutable("any", commands, true), getWorkspace(), osInfo); |
| 413 | + | new CommandFacade("any", commands, true), getWorkspace(), osInfo); |
412 | 414 | | } else { |
413 | 415 | | WebTarget target = client.target(serverUrl).path("api/k8s/job-data"); |
414 | 416 | | Invocation.Builder builder = target.request(); |
| skipped 38 lines |
453 | 455 | | |
454 | 456 | | logger.info("Generating command scripts..."); |
455 | 457 | | |
456 | | - | CompositeExecutable entryExecutable = new CompositeExecutable(jobData.getActions()); |
457 | | - | entryExecutable.traverse(new LeafVisitor<Void>() { |
| 458 | + | CompositeFacade entryFacade = new CompositeFacade(jobData.getActions()); |
| 459 | + | entryFacade.traverse(new LeafVisitor<Void>() { |
458 | 460 | | |
459 | 461 | | @Override |
460 | | - | public Void visit(LeafExecutable executable, List<Integer> position) { |
461 | | - | String stepNames = entryExecutable.getNamesAsString(position); |
| 462 | + | public Void visit(LeafFacade facade, List<Integer> position) { |
| 463 | + | String stepNames = entryFacade.getNamesAsString(position); |
462 | 464 | | |
463 | 465 | | List<String> setupCommands = new ArrayList<>(); |
464 | 466 | | if (SystemUtils.IS_OS_WINDOWS) { |
| skipped 27 lines |
492 | 494 | | String positionStr = stringifyPosition(position); |
493 | 495 | | |
494 | 496 | | File workingDir = getWorkspace(); |
495 | | - | CommandExecutable commandExecutable; |
496 | | - | if (executable instanceof CommandExecutable) { |
497 | | - | commandExecutable = (CommandExecutable) executable; |
498 | | - | } else if (executable instanceof ContainerExecutable) { |
499 | | - | ContainerExecutable containerExecutable = (ContainerExecutable) executable; |
500 | | - | OsContainer container = containerExecutable.getContainer(osInfo); |
| 497 | + | CommandFacade commandFacade; |
| 498 | + | if (facade instanceof CommandFacade) { |
| 499 | + | commandFacade = (CommandFacade) facade; |
| 500 | + | } else if (facade instanceof BuildImageFacade) { |
| 501 | + | BuildImageFacade buildImageFacade = (BuildImageFacade) facade; |
| 502 | + | |
| 503 | + | List<String> commands = new ArrayList<>(); |
| 504 | + | |
| 505 | + | StringBuilder buildCommand = new StringBuilder("docker build "); |
| 506 | + | |
| 507 | + | String[] parsedTags = StringUtils.parseQuoteTokens(buildImageFacade.getTags()); |
| 508 | + | for (String tag: parsedTags) |
| 509 | + | buildCommand.append("-t ").append(tag).append(" "); |
| 510 | + | |
| 511 | + | List<String> loginCommands = new ArrayList<>(); |
| 512 | + | try { |
| 513 | + | List<RegistryLoginFacade> registryLogins = SerializationUtils.deserialize( |
| 514 | + | Hex.decodeHex(System.getenv(ENV_REGISTRY_LOGINS).toCharArray())); |
| 515 | + | for (RegistryLoginFacade login: registryLogins) { |
| 516 | + | StringBuilder loginCommand = new StringBuilder("echo "); |
| 517 | + | loginCommand.append(login.getPassword()).append("|docker login -u "); |
| 518 | + | loginCommand.append(login.getUserName()).append(" --password-stdin || exit /b 1"); |
| 519 | + | if (login.getRegistryUrl() != null) |
| 520 | + | loginCommand.append(" ").append(login.getRegistryUrl()); |
| 521 | + | loginCommands.add(loginCommand.toString()); |
| 522 | + | } |
| 523 | + | } catch (DecoderException e) { |
| 524 | + | throw new RuntimeException(e); |
| 525 | + | } |
| 526 | + | |
| 527 | + | if (SystemUtils.IS_OS_WINDOWS) { |
| 528 | + | if (buildImageFacade.getDockerfile() != null) |
| 529 | + | buildCommand.append("-f ").append("%workspace%\\" + buildImageFacade.getDockerfile().replace('/', '\\')); |
| 530 | + | else |
| 531 | + | buildCommand.append("-f ").append("%workspace%\\Dockerfile"); |
| 532 | + | |
| 533 | + | buildCommand.append(" "); |
| 534 | + | |
| 535 | + | if (buildImageFacade.getBuildPath() != null) |
| 536 | + | buildCommand.append("%workspace%\\" + buildImageFacade.getBuildPath().replace('/', '\\')); |
| 537 | + | else |
| 538 | + | buildCommand.append("%workspace%"); |
| 539 | + | |
| 540 | + | buildCommand.append(" || exit /b 1"); |
| 541 | + | |
| 542 | + | commands.add("@echo off"); |
| 543 | + | commands.addAll(loginCommands); |
| 544 | + | commands.add("set workspace=%cd%"); |
| 545 | + | } else { |
| 546 | + | if (buildImageFacade.getDockerfile() != null) |
| 547 | + | buildCommand.append("-f ").append("$workspace/" + buildImageFacade.getDockerfile()); |
| 548 | + | else |
| 549 | + | buildCommand.append("-f ").append("$workspace/Dockerfile"); |
| 550 | + | |
| 551 | + | buildCommand.append(" "); |
| 552 | + | |
| 553 | + | if (buildImageFacade.getBuildPath() != null) |
| 554 | + | buildCommand.append("$workspace/" + buildImageFacade.getBuildPath()); |
| 555 | + | else |
| 556 | + | buildCommand.append("$workspace"); |
| 557 | + | |
| 558 | + | commands.add("set -e"); |
| 559 | + | commands.addAll(loginCommands); |
| 560 | + | commands.add("workspace=$(pwd)"); |
| 561 | + | } |
| 562 | + | |
| 563 | + | commands.add(buildCommand.toString()); |
| 564 | + | |
| 565 | + | if (buildImageFacade.isPublish()) { |
| 566 | + | for (String tag: parsedTags) |
| 567 | + | commands.add("docker push " + tag); |
| 568 | + | } |
| 569 | + | |
| 570 | + | commandFacade = new CommandFacade("any", commands, true); |
| 571 | + | } else if (facade instanceof RunContainerFacade) { |
| 572 | + | RunContainerFacade containerFacade = (RunContainerFacade) facade; |
| 573 | + | OsContainer container = containerFacade.getContainer(osInfo); |
501 | 574 | | if (container.getWorkingDir() != null) |
502 | 575 | | workingDir = new File(container.getWorkingDir()); |
503 | 576 | | // We will inspect container image and populate appropriate commands in sidecar as |
504 | 577 | | // container images are not pulled at init stage |
505 | | - | commandExecutable = new CommandExecutable("any", Lists.newArrayList(), true); |
| 578 | + | commandFacade = new CommandFacade("any", Lists.newArrayList(), true); |
506 | 579 | | } else { |
507 | 580 | | String command; |
508 | 581 | | String classPath; |
| skipped 1 lines |
510 | 583 | | classPath = "C:\\k8s-helper\\*"; |
511 | 584 | | else |
512 | 585 | | classPath = "/k8s-helper/*"; |
513 | | - | if (executable instanceof CheckoutExecutable) { |
514 | | - | CheckoutExecutable checkoutExecutable = (CheckoutExecutable) executable; |
515 | | - | checkoutExecutable.getCloneInfo(); |
| 586 | + | if (facade instanceof CheckoutFacade) { |
| 587 | + | CheckoutFacade checkoutFacade = (CheckoutFacade) facade; |
| 588 | + | checkoutFacade.getCloneInfo(); |
516 | 589 | | command = String.format("java -classpath \"%s\" io.onedev.k8shelper.CheckoutCode %s %b %b %d %s", |
517 | | - | classPath, positionStr, checkoutExecutable.isWithLfs(), checkoutExecutable.isWithSubmodules(), |
518 | | - | checkoutExecutable.getCloneDepth(), checkoutExecutable.getCloneInfo().toString()); |
| 590 | + | classPath, positionStr, checkoutFacade.isWithLfs(), checkoutFacade.isWithSubmodules(), |
| 591 | + | checkoutFacade.getCloneDepth(), checkoutFacade.getCloneInfo().toString()); |
519 | 592 | | } else { |
520 | | - | ServerExecutable serverExecutable = (ServerExecutable) executable; |
| 593 | + | ServerSideFacade serverSideFacade = (ServerSideFacade) facade; |
521 | 594 | | |
522 | | - | String includeFiles = encodeAsCommandArg(serverExecutable.getIncludeFiles()); |
523 | | - | String excludeFiles = encodeAsCommandArg(serverExecutable.getExcludeFiles()); |
524 | | - | String placeholders = encodeAsCommandArg(serverExecutable.getPlaceholders()); |
525 | | - | command = String.format("java -classpath \"%s\" io.onedev.k8shelper.RunServerStep %s %s %s %s", |
| 595 | + | String includeFiles = encodeAsCommandArg(serverSideFacade.getIncludeFiles()); |
| 596 | + | String excludeFiles = encodeAsCommandArg(serverSideFacade.getExcludeFiles()); |
| 597 | + | String placeholders = encodeAsCommandArg(serverSideFacade.getPlaceholders()); |
| 598 | + | command = String.format("java -classpath \"%s\" io.onedev.k8shelper.RunServerSideStep %s %s %s %s", |
526 | 599 | | classPath, positionStr, includeFiles, excludeFiles, placeholders); |
527 | 600 | | } |
528 | 601 | | |
| skipped 2 lines |
531 | 604 | | commands.add("@echo off"); |
532 | 605 | | commands.add(command); |
533 | 606 | | |
534 | | - | commandExecutable = new CommandExecutable("any", commands, true); |
| 607 | + | commandFacade = new CommandFacade("any", commands, true); |
535 | 608 | | } |
536 | 609 | | |
537 | | - | generateCommandScript(position, stepNames, setupCommands, commandExecutable, workingDir, osInfo); |
| 610 | + | generateCommandScript(position, stepNames, setupCommands, commandFacade, workingDir, osInfo); |
538 | 611 | | |
539 | 612 | | return null; |
540 | 613 | | } |
| skipped 344 lines |
885 | 958 | | LeafHandler commandHandler = new LeafHandler() { |
886 | 959 | | |
887 | 960 | | @Override |
888 | | - | public boolean execute(LeafExecutable executable, List<Integer> position) { |
| 961 | + | public boolean execute(LeafFacade facade, List<Integer> position) { |
889 | 962 | | String positionStr = stringifyPosition(position); |
890 | 963 | | |
891 | 964 | | File file; |
| skipped 9 lines |
901 | 974 | | |
902 | 975 | | try { |
903 | 976 | | String stepScript = FileUtils.readFileToString(stepScriptFile, UTF_8); |
904 | | - | if (executable instanceof ContainerExecutable) { |
905 | | - | ContainerExecutable containerExecutable = (ContainerExecutable) executable; |
906 | | - | OsContainer container = containerExecutable.getContainer(osInfo); |
| 977 | + | if (facade instanceof RunContainerFacade) { |
| 978 | + | RunContainerFacade rubContainerFacade = (RunContainerFacade) facade; |
| 979 | + | OsContainer container = rubContainerFacade.getContainer(osInfo); |
907 | 980 | | stepScript = getContainerRunScript(container.getImage(), container.getArgs()); |
908 | 981 | | } else { |
909 | 982 | | stepScript = FileUtils.readFileToString(stepScriptFile, UTF_8); |
| skipped 35 lines |
945 | 1018 | | } |
946 | 1019 | | |
947 | 1020 | | @Override |
948 | | - | public void skip(LeafExecutable executable, List<Integer> position) { |
| 1021 | + | public void skip(LeafFacade facade, List<Integer> position) { |
949 | 1022 | | File file = new File(getMarkHome(), stringifyPosition(position) + ".skip"); |
950 | 1023 | | try { |
951 | 1024 | | if (!file.createNewFile()) |
| skipped 6 lines |
958 | 1031 | | }; |
959 | 1032 | | |
960 | 1033 | | if (test) { |
961 | | - | CommandExecutable executable = new CommandExecutable( |
| 1034 | + | CommandFacade facade = new CommandFacade( |
962 | 1035 | | "this does not matter", Lists.newArrayList("this does not matter"), false); |
963 | | - | executable.execute(commandHandler, Lists.newArrayList(0)); |
| 1036 | + | facade.execute(commandHandler, Lists.newArrayList(0)); |
964 | 1037 | | } else { |
965 | 1038 | | JobData jobData = readJobData(); |
966 | 1039 | | |
967 | 1040 | | List<Action> actions = jobData.getActions(); |
968 | 1041 | | |
969 | | - | new CompositeExecutable(actions).execute(commandHandler, new ArrayList<>()); |
| 1042 | + | new CompositeFacade(actions).execute(commandHandler, new ArrayList<>()); |
970 | 1043 | | } |
971 | 1044 | | } |
972 | 1045 | | |
| skipped 103 lines |
1076 | 1149 | | } |
1077 | 1150 | | |
1078 | 1151 | | }; |
1079 | | - | runServerStep(serverUrl, jobToken, position, includeFiles, excludeFiles, placeholders, |
| 1152 | + | runServerSideStep(serverUrl, jobToken, position, includeFiles, excludeFiles, placeholders, |
1080 | 1153 | | getBuildHome(), getWorkspace(), logger); |
1081 | 1154 | | } |
1082 | 1155 | | |
1083 | | - | public static void runServerStep(String serverUrl, String jobToken, List<Integer> position, |
| 1156 | + | public static void runServerSideStep(String serverUrl, String jobToken, List<Integer> position, |
1084 | 1157 | | Collection<String> includeFiles, Collection<String> excludeFiles, |
1085 | 1158 | | Collection<String> placeholders, File buildHome, File workspace, TaskLogger logger) { |
1086 | 1159 | | Client client = ClientBuilder.newClient(); |
| skipped 126 lines |