| skipped 48 lines |
49 | 49 | | |
50 | 50 | | public static final String BEARER = "Bearer"; |
51 | 51 | | |
52 | | - | public static final String EXECUTING_COMMAND_MESSAGE = "===== Executing OneDev K8s Command ====="; |
53 | | - | |
54 | 52 | | public static final String LOG_END_MESSAGE = "===== End of OneDev K8s Helper Log ====="; |
55 | 53 | | |
56 | 54 | | private static final Logger logger = LoggerFactory.getLogger(KubernetesHelper.class); |
57 | 55 | | |
58 | | - | public static File getBuildHome() { |
| 56 | + | private static File getBuildHome() { |
59 | 57 | | if (isWindows()) |
60 | 58 | | return new File("C:\\onedev-build"); |
61 | 59 | | else |
62 | 60 | | return new File("/onedev-build"); |
63 | 61 | | } |
64 | 62 | | |
| 63 | + | private static File getJobContextFile() { |
| 64 | + | return new File(getBuildHome(), "job-context"); |
| 65 | + | } |
| 66 | + | |
65 | 67 | | private static File getTrustCertsHome() { |
66 | 68 | | if (isWindows()) |
67 | 69 | | return new File("C:\\onedev-build\\trust-certs"); |
| skipped 12 lines |
80 | 82 | | return new File(getBuildHome(), "workspace"); |
81 | 83 | | } |
82 | 84 | | |
83 | | - | public static File getCommandHome() { |
| 85 | + | private static File getCommandHome() { |
84 | 86 | | return new File(getBuildHome(), "command"); |
85 | 87 | | } |
86 | 88 | | |
87 | | - | public static File getMarkHome() { |
| 89 | + | private static File getMarkHome() { |
88 | 90 | | return new File(KubernetesHelper.getBuildHome(), "mark"); |
89 | 91 | | } |
90 | 92 | | |
91 | | - | public static boolean isWindows() { |
| 93 | + | private static boolean isWindows() { |
92 | 94 | | return System.getProperty("os.name").toLowerCase().contains("windows"); |
93 | 95 | | } |
94 | 96 | | |
| skipped 193 lines |
288 | 290 | | WebTarget target = client.target(serverUrl).path("rest/k8s/test"); |
289 | 291 | | Invocation.Builder builder = target.request(); |
290 | 292 | | builder.header(HttpHeaders.AUTHORIZATION, BEARER + " " + jobToken); |
291 | | - | checkStatus(builder.get()); |
| 293 | + | Response response = builder.get(); |
| 294 | + | try { |
| 295 | + | checkStatus(response); |
| 296 | + | } finally { |
| 297 | + | response.close(); |
| 298 | + | } |
292 | 299 | | File tempFile = null; |
293 | 300 | | try { |
294 | 301 | | tempFile = File.createTempFile("test", null, cacheHome); |
| skipped 16 lines |
311 | 318 | | logger.info("Retrieving job context from {}...", serverUrl); |
312 | 319 | | |
313 | 320 | | Map<String, Object> jobContext; |
314 | | - | Response response = checkStatus(builder.post( |
315 | | - | Entity.entity(getWorkspace().getAbsolutePath(), MediaType.APPLICATION_OCTET_STREAM))); |
| 321 | + | Response response = builder.post( |
| 322 | + | Entity.entity(getWorkspace().getAbsolutePath(), MediaType.APPLICATION_OCTET_STREAM)); |
| 323 | + | byte[] jobContextBytes; |
316 | 324 | | try { |
317 | | - | jobContext = SerializationUtils.deserialize(response.readEntity(byte[].class)); |
| 325 | + | checkStatus(response); |
| 326 | + | jobContextBytes = response.readEntity(byte[].class); |
318 | 327 | | } finally { |
319 | 328 | | response.close(); |
320 | 329 | | } |
321 | 330 | | |
| 331 | + | FileUtils.writeByteArrayToFile(getJobContextFile(), jobContextBytes); |
| 332 | + | jobContext = SerializationUtils.deserialize(jobContextBytes); |
| 333 | + | |
322 | 334 | | File workspace = getWorkspace(); |
323 | 335 | | File workspaceCache = null; |
324 | 336 | | |
| skipped 2 lines |
327 | 339 | | builder = target.request(); |
328 | 340 | | builder.header(HttpHeaders.AUTHORIZATION, BEARER + " " + jobToken); |
329 | 341 | | Map<CacheInstance, String> cacheAllocations; |
| 342 | + | response = builder.post(Entity.entity( |
| 343 | + | new CacheAllocationRequest(new Date(), getCacheInstances(cacheHome)).toString(), |
| 344 | + | MediaType.APPLICATION_OCTET_STREAM)); |
330 | 345 | | try { |
331 | | - | response = builder.post(Entity.entity( |
332 | | - | new CacheAllocationRequest(new Date(), getCacheInstances(cacheHome)).toString(), |
333 | | - | MediaType.APPLICATION_OCTET_STREAM)); |
334 | 346 | | checkStatus(response); |
335 | 347 | | cacheAllocations = SerializationUtils.deserialize(response.readEntity(byte[].class)); |
336 | 348 | | } finally { |
| skipped 138 lines |
475 | 487 | | target = client.target(serverUrl).path("rest/k8s/download-dependencies"); |
476 | 488 | | builder = target.request(); |
477 | 489 | | builder.header(HttpHeaders.AUTHORIZATION, BEARER + " " + jobToken); |
478 | | - | response = checkStatus(builder.get()); |
| 490 | + | |
| 491 | + | response = builder.get(); |
479 | 492 | | try { |
| 493 | + | checkStatus(response); |
480 | 494 | | InputStream is = response.readEntity(InputStream.class); |
481 | 495 | | try { |
482 | 496 | | TarUtils.untar(is, workspace); |
| skipped 82 lines |
565 | 579 | | |
566 | 580 | | @SuppressWarnings("unchecked") |
567 | 581 | | public static void sidecar(String serverUrl, String jobToken, boolean test) { |
568 | | - | installJVMCert(); |
569 | | - | |
570 | 582 | | CommandHandler commandHandler = new CommandHandler() { |
571 | 583 | | |
572 | 584 | | @Override |
| skipped 34 lines |
607 | 619 | | if (test) { |
608 | 620 | | CommandExecutable executable = new CommandExecutable( |
609 | 621 | | "this does not matter", Lists.newArrayList("this does not matter")); |
610 | | - | logger.info(EXECUTING_COMMAND_MESSAGE); |
611 | 622 | | executable.execute(commandHandler, Lists.newArrayList(1)); |
612 | 623 | | } else { |
613 | | - | Client client = ClientBuilder.newClient(); |
614 | | - | client.property(ClientProperties.REQUEST_ENTITY_PROCESSING, "CHUNKED"); |
| 624 | + | byte[] jobContextBytes; |
615 | 625 | | try { |
616 | | - | WebTarget target = client.target(serverUrl).path("rest/k8s/job-context"); |
617 | | - | Invocation.Builder builder = target.request(); |
618 | | - | builder.header(HttpHeaders.AUTHORIZATION, BEARER + " " + jobToken); |
| 626 | + | jobContextBytes = FileUtils.readFileToByteArray(getJobContextFile()); |
| 627 | + | } catch (IOException e) { |
| 628 | + | throw new RuntimeException(e); |
| 629 | + | } |
| 630 | + | Map<String, Object> jobContext = SerializationUtils.deserialize(jobContextBytes); |
619 | 631 | | |
620 | | - | Map<String, Object> jobContext; |
621 | | - | Response response = checkStatus(builder.post(Entity.entity(null, MediaType.APPLICATION_OCTET_STREAM))); |
622 | | - | try { |
623 | | - | jobContext = SerializationUtils.deserialize(response.readEntity(byte[].class)); |
624 | | - | } finally { |
625 | | - | response.close(); |
626 | | - | } |
| 632 | + | List<Action> actions = (List<Action>) jobContext.get("actions"); |
627 | 633 | | |
628 | | - | List<Action> actions = (List<Action>) jobContext.get("actions"); |
| 634 | + | new CompositeExecutable(actions).execute(commandHandler, new ArrayList<>()); |
629 | 635 | | |
630 | | - | logger.info(EXECUTING_COMMAND_MESSAGE); |
631 | | - | new CompositeExecutable(actions).execute(commandHandler, new ArrayList<>()); |
| 636 | + | installJVMCert(); |
632 | 637 | | |
| 638 | + | Client client = ClientBuilder.newClient(); |
| 639 | + | client.property(ClientProperties.REQUEST_ENTITY_PROCESSING, "CHUNKED"); |
| 640 | + | try { |
633 | 641 | | logger.info("Uploading job outcomes to '{}'...", serverUrl); |
634 | 642 | | |
635 | 643 | | Set<String> includes = (Set<String>) jobContext.get("collectFiles.includes"); |
636 | 644 | | Set<String> excludes = (Set<String>) jobContext.get("collectFiles.excludes"); |
637 | 645 | | |
638 | | - | target = client.target(serverUrl).path("rest/k8s/upload-outcomes"); |
| 646 | + | WebTarget target = client.target(serverUrl).path("rest/k8s/upload-outcomes"); |
639 | 647 | | |
640 | 648 | | StreamingOutput os = new StreamingOutput() { |
641 | 649 | | |
| skipped 4 lines |
646 | 654 | | } |
647 | 655 | | |
648 | 656 | | }; |
649 | | - | builder = target.request(); |
| 657 | + | Invocation.Builder builder = target.request(); |
650 | 658 | | builder.header(HttpHeaders.AUTHORIZATION, BEARER + " " + jobToken); |
| 659 | + | Response response = builder.post(Entity.entity(os, MediaType.APPLICATION_OCTET_STREAM_TYPE)); |
651 | 660 | | try { |
652 | | - | response = builder.post(Entity.entity(os, MediaType.APPLICATION_OCTET_STREAM_TYPE)); |
653 | 661 | | checkStatus(response); |
654 | 662 | | } finally { |
655 | 663 | | response.close(); |
| skipped 6 lines |
662 | 670 | | StringBuilder toStringBuilder = new StringBuilder(); |
663 | 671 | | for (CacheInstance instance: getCacheInstances(getCacheHome()).keySet()) |
664 | 672 | | toStringBuilder.append(instance.toString()).append(";"); |
| 673 | + | response = builder.post(Entity.entity(toStringBuilder.toString(), MediaType.APPLICATION_OCTET_STREAM)); |
665 | 674 | | try { |
666 | | - | checkStatus(builder.post(Entity.entity(toStringBuilder.toString(), MediaType.APPLICATION_OCTET_STREAM))); |
| 675 | + | checkStatus(response); |
667 | 676 | | } finally { |
668 | 677 | | response.close(); |
669 | 678 | | } |
| skipped 8 lines |