• ■ ■ ■ ■ ■ ■
    src/main/java/io/onedev/k8shelper/KubernetesHelper.java
    skipped 67 lines
    68 68  
    69 69   public static final String WORKSPACE = "workspace";
    70 70  
     71 + public static final String HOME_PREFIX = "home:";
     72 +
    71 73   public static final String ATTRIBUTES = "attributes";
    72 74  
    73 75   public static final String PLACEHOLDER_PREFIX = "<&onedev#";
    skipped 73 lines
    147 149   return instances;
    148 150   }
    149 151  
    150  - public static void preprocess(File cacheHome, Map<CacheInstance, String> cacheAllocations, Consumer<File> cacheCleaner) {
     152 + public static void checkCacheAllocations(File cacheHome, Map<CacheInstance, String> cacheAllocations, Consumer<File> cacheCleaner) {
    151 153   for (Iterator<Map.Entry<CacheInstance, String>> it = cacheAllocations.entrySet().iterator(); it.hasNext();) {
    152 154   Map.Entry<CacheInstance, String> entry = it.next();
    153 155   File cacheDirectory = entry.getKey().getDirectory(cacheHome);
    skipped 260 lines
    414 416   cacheAllocations = SerializationUtils.deserialize(response.readEntity(byte[].class));
    415 417   }
    416 418  
    417  - preprocess(cacheHome, cacheAllocations, new Consumer<File>() {
     419 + checkCacheAllocations(cacheHome, cacheAllocations, new Consumer<File>() {
    418 420   
    419 421   @Override
    420 422   public void accept(File dir) {
    skipped 36 lines
    457 459  
    458 460   for (Map.Entry<CacheInstance, String> entry: cacheAllocations.entrySet()) {
    459 461   if (!PathUtils.isCurrent(entry.getValue())) {
    460  - String link = PathUtils.resolve(workspace.getAbsolutePath(), entry.getValue());
    461  - File linkTarget = entry.getKey().getDirectory(cacheHome);
    462  - // create possible missing parent directories
    463  - if (isWindows()) {
    464  - setupCommands.add(String.format("echo Setting up cache \"%s\"...", link));
    465  - setupCommands.add(String.format("if not exist \"%s\" mkdir \"%s\"", link, link));
    466  - setupCommands.add(String.format("rmdir /q /s \"%s\"", link));
    467  - setupCommands.add(String.format("mklink /D \"%s\" \"%s\"", link, linkTarget.getAbsolutePath()));
    468  - } else {
    469  - setupCommands.add(String.format("echo Setting up cache \"%s\"...", link));
    470  - setupCommands.add(String.format("mkdir -p \"%s\"", link));
    471  - setupCommands.add(String.format("rm -rf \"%s\"", link));
    472  - setupCommands.add(String.format("ln -s \"%s\" \"%s\"", linkTarget.getAbsolutePath(), link));
     462 + for (String link: resolveCachePath(workspace.getAbsolutePath(), entry.getValue())) {
     463 + File linkTarget = entry.getKey().getDirectory(cacheHome);
     464 + // create possible missing parent directories
     465 + if (isWindows()) {
     466 + setupCommands.add(String.format("echo Setting up cache \"%s\"...", link));
     467 + setupCommands.add(String.format("if not exist \"%s\" mkdir \"%s\"", link, link));
     468 + setupCommands.add(String.format("rmdir /q /s \"%s\"", link));
     469 + setupCommands.add(String.format("mklink /D \"%s\" \"%s\"", link, linkTarget.getAbsolutePath()));
     470 + } else {
     471 + setupCommands.add(String.format("echo Setting up cache \"%s\"...", link));
     472 + setupCommands.add(String.format("mkdir -p \"%s\"", link));
     473 + setupCommands.add(String.format("rm -rf \"%s\"", link));
     474 + setupCommands.add(String.format("ln -s \"%s\" \"%s\"", linkTarget.getAbsolutePath(), link));
     475 + }
    473 476   }
    474 477   }
    475 478   }
    skipped 85 lines
    561 564   }
    562 565   }
    563 566   
    564  - public static void initRepositoryIfNecessary(Commandline git, LineConsumer infoLogger,
    565  - LineConsumer errorLogger) {
     567 + public static void checkoutRepository(Commandline git, String commitHash,
     568 + LineConsumer infoLogger, LineConsumer errorLogger) {
     569 + git.clearArgs();
     570 + git.addArgs("checkout", "--quiet", commitHash);
     571 + git.execute(infoLogger, errorLogger).checkReturnCode();
     572 + }
     573 +
     574 + public static void cloneRepository(Commandline git, String cloneUrl, String remoteUrl,
     575 + String commitHash, int cloneDepth, LineConsumer infoLogger, LineConsumer errorLogger) {
    566 576   git.clearArgs();
    567 577   if (!new File(git.workingDir(), ".git").exists()) {
    568 578   git.addArgs("init", ".");
    skipped 15 lines
    584 594  
    585 595   }).checkReturnCode();
    586 596   }
    587  - }
    588 597  
    589  - public static void checkoutRepository(Commandline git, String commitHash,
    590  - LineConsumer infoLogger, LineConsumer errorLogger) {
    591 598   git.clearArgs();
    592  - git.addArgs("checkout", "--quiet", commitHash);
     599 + git.addArgs("fetch", cloneUrl, "--force", "--quiet");
     600 + if (cloneDepth != 0)
     601 + git.addArgs("--depth=" + cloneDepth);
     602 + git.addArgs(commitHash);
    593 603   git.execute(infoLogger, errorLogger).checkReturnCode();
    594  - }
     604 + 
     605 + checkoutRepository(git, commitHash, infoLogger, errorLogger);
    595 606  
    596  - public static void deinitSubmodulesIfNecessary(Commandline git,
    597  - LineConsumer infoLogger, LineConsumer errorLogger) {
     607 + git.clearArgs();
     608 + git.addArgs("remote", "add", "origin", remoteUrl);
     609 + git.execute(infoLogger, errorLogger).checkReturnCode();
     610 +
    598 611   if (new File(git.workingDir(), ".gitmodules").exists()) {
    599 612   // deinit submodules in case submodule url is changed
    600 613   git.clearArgs();
    skipped 9 lines
    610 623   }
    611 624  
    612 625   }).checkReturnCode();
    613  - }
    614  - }
    615 626  
    616  - public static void updateSubmodulesIfNecessary(Commandline git, int cloneDepth,
    617  - LineConsumer infoLogger, LineConsumer errorLogger) {
    618  - if (new File(git.workingDir(), ".gitmodules").exists()) {
    619 627   infoLogger.consume("Retrieving submodules...");
    620 628  
    621 629   git.clearArgs();
    skipped 15 lines
    637 645  
    638 646   }).checkReturnCode();
    639 647   }
    640  - }
    641  -
    642  - public static void cloneRepository(Commandline git, String cloneUrl, String commitHash,
    643  - int cloneDepth, LineConsumer infoLogger, LineConsumer errorLogger) {
    644  - initRepositoryIfNecessary(git, infoLogger, errorLogger);
    645  -
    646  - git.clearArgs();
    647  - git.addArgs("fetch", cloneUrl, "--force", "--quiet");
    648  - if (cloneDepth != 0)
    649  - git.addArgs("--depth=" + cloneDepth);
    650  - git.addArgs(commitHash);
    651  - git.execute(infoLogger, errorLogger).checkReturnCode();
    652  - 
    653  - checkoutRepository(git, commitHash, infoLogger, errorLogger);
    654  - deinitSubmodulesIfNecessary(git, infoLogger, errorLogger);
    655 648   }
    656 649  
    657 650   private static JobData readJobData() {
    skipped 190 lines
    848 841   infoLogger, errorLogger);
    849 842   }
    850 843  
    851  - cloneRepository(git, cloneInfo.getCloneUrl(), jobData.getCommitHash(), cloneDepth,
    852  - infoLogger, errorLogger);
    853  - addOriginRemote(git, cloneInfo.getCloneUrl(), infoLogger, errorLogger);
    854  - updateSubmodulesIfNecessary(git, cloneDepth, infoLogger, errorLogger);
    855  - }
    856  -
    857  - public static void addOriginRemote(Commandline git, String remoteUrl,
    858  - LineConsumer infoLogger, LineConsumer errorLogger) {
    859  - git.clearArgs();
    860  - git.addArgs("remote", "add", "origin", remoteUrl);
    861  - git.execute(infoLogger, errorLogger).checkReturnCode();
     844 + cloneRepository(git, cloneInfo.getCloneUrl(), cloneInfo.getCloneUrl(),
     845 + jobData.getCommitHash(), cloneDepth, infoLogger, errorLogger);
    862 846   }
    863 847  
    864 848   public static void runServerStep(String serverUrl, String jobToken, String positionStr,
    skipped 122 lines
    987 971   }
    988 972   matcher.appendTail(buffer);
    989 973   return buffer.toString();
     974 + }
     975 +
     976 + public static String[] resolveCachePath(String basePath, String cachePath) {
     977 + if (cachePath.startsWith(HOME_PREFIX)) {
     978 + cachePath = cachePath.substring(HOME_PREFIX.length());
     979 + if (SystemUtils.IS_OS_WINDOWS) {
     980 + cachePath = cachePath.replace('/', '\\');
     981 + if (cachePath.startsWith("\\"))
     982 + cachePath = cachePath.substring(1);
     983 + return new String[] {
     984 + "C:\\Users\\ContainerAdministrator\\" + cachePath,
     985 + "C:\\Users\\ContainerUser\\" + cachePath
     986 + };
     987 + } else {
     988 + cachePath = cachePath.replace('\\', '/');
     989 + if (cachePath.startsWith("/"))
     990 + cachePath = cachePath.substring(1);
     991 + return new String[] {"/root/" + cachePath};
     992 + }
     993 + } else {
     994 + return new String[] {PathUtils.resolve(basePath, cachePath)};
     995 + }
    990 996   }
    991 997  
    992 998   public static String replacePlaceholders(String string, File buildHome) {
    skipped 22 lines
Please wait...
Page is in error, reload to recover