| 
									
										
										
										
											2021-12-27 21:12:46 +00:00
										 |  |  | import { assert } from 'console'; | 
					
						
							|  |  |  | import fs from 'fs'; | 
					
						
							|  |  |  | import path from 'path'; | 
					
						
							| 
									
										
										
										
											2022-01-02 22:48:43 +00:00
										 |  |  | import { Input } from '../../..'; | 
					
						
							|  |  |  | import CloudRunnerLogger from '../../../cloud-runner/services/cloud-runner-logger'; | 
					
						
							|  |  |  | import { CloudRunnerState } from '../../../cloud-runner/state/cloud-runner-state'; | 
					
						
							|  |  |  | import { CloudRunnerSystem } from './cloud-runner-system'; | 
					
						
							| 
									
										
										
										
											2022-01-01 02:42:57 +00:00
										 |  |  | import { LFSHashing } from './lfs-hashing'; | 
					
						
							| 
									
										
										
										
											2021-12-31 20:12:47 +00:00
										 |  |  | import { RemoteClientLogger } from './remote-client-logger'; | 
					
						
							| 
									
										
										
										
											2022-01-11 22:20:21 +00:00
										 |  |  | import archiver from 'archiver'; | 
					
						
							|  |  |  | import extract from 'extract-zip'; | 
					
						
							| 
									
										
										
										
											2021-12-27 21:12:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | export class Caching { | 
					
						
							| 
									
										
										
										
											2021-12-31 20:33:55 +00:00
										 |  |  |   public static async PushToCache(cacheFolder: string, sourceFolder: string, cacheKey: string) { | 
					
						
							| 
									
										
										
										
											2022-01-06 22:47:05 +00:00
										 |  |  |     const startPath = process.cwd(); | 
					
						
							| 
									
										
										
										
											2021-12-27 21:49:57 +00:00
										 |  |  |     try { | 
					
						
							| 
									
										
										
										
											2022-01-03 02:49:51 +00:00
										 |  |  |       if (!fs.existsSync(cacheFolder)) { | 
					
						
							|  |  |  |         await CloudRunnerSystem.Run(`mkdir -p ${cacheFolder}`); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2022-01-06 22:44:25 +00:00
										 |  |  |       process.chdir(`${sourceFolder}`); | 
					
						
							| 
									
										
										
										
											2021-12-31 20:33:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       if (Input.cloudRunnerTests) { | 
					
						
							| 
									
										
										
										
											2022-01-06 22:44:25 +00:00
										 |  |  |         CloudRunnerLogger.log( | 
					
						
							|  |  |  |           `Hashed cache folder ${await LFSHashing.hashAllFiles(sourceFolder)} ${sourceFolder} ${path.basename( | 
					
						
							|  |  |  |             sourceFolder, | 
					
						
							|  |  |  |           )}`,
 | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2021-12-31 20:33:55 +00:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2022-01-03 01:32:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       if (Input.cloudRunnerTests) { | 
					
						
							|  |  |  |         await CloudRunnerSystem.Run(`ls`); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2022-01-12 20:56:51 +00:00
										 |  |  |       assert(fs.existsSync(`${path.basename(sourceFolder)}`)); | 
					
						
							|  |  |  |       await new Promise<void>((resolve) => { | 
					
						
							|  |  |  |         const output = fs.createWriteStream(`${cacheKey}.zip`); | 
					
						
							|  |  |  |         const archive = archiver('zip', { | 
					
						
							|  |  |  |           zlib: { level: 9 }, // Sets the compression level.
 | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         output.on('close', function () { | 
					
						
							|  |  |  |           CloudRunnerLogger.log(`${archive.pointer()} total bytes`); | 
					
						
							|  |  |  |           CloudRunnerLogger.log('archiver has been finalized and the output file descriptor has closed.'); | 
					
						
							|  |  |  |           resolve(); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         output.on('end', function () { | 
					
						
							|  |  |  |           CloudRunnerLogger.log('Data has been drained'); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         archive.on('warning', function (error) { | 
					
						
							|  |  |  |           if (error.code === 'ENOENT') { | 
					
						
							|  |  |  |             // log warning
 | 
					
						
							|  |  |  |             CloudRunnerLogger.logWarning(error); | 
					
						
							|  |  |  |           } else { | 
					
						
							|  |  |  |             throw error; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         archive.on('error', function (error) { | 
					
						
							| 
									
										
										
										
											2022-01-11 22:20:21 +00:00
										 |  |  |           throw error; | 
					
						
							| 
									
										
										
										
											2022-01-12 20:56:51 +00:00
										 |  |  |         }); | 
					
						
							|  |  |  |         archive.pipe(output); | 
					
						
							| 
									
										
										
										
											2022-01-12 22:11:28 +00:00
										 |  |  |         archive.directory(sourceFolder, false); | 
					
						
							| 
									
										
										
										
											2022-01-12 20:56:51 +00:00
										 |  |  |         archive.finalize(); | 
					
						
							| 
									
										
										
										
											2022-01-11 22:20:21 +00:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2022-01-12 20:56:51 +00:00
										 |  |  |       assert(fs.existsSync(`${cacheKey}.zip`), 'cache zip exists'); | 
					
						
							|  |  |  |       assert(fs.existsSync(`${cacheFolder}`), 'cache folder'); | 
					
						
							| 
									
										
										
										
											2022-01-12 21:54:26 +00:00
										 |  |  |       assert(fs.existsSync(path.resolve(`..`, `${path.basename(sourceFolder)}`)), 'source folder exists'); | 
					
						
							| 
									
										
										
										
											2022-01-02 22:48:43 +00:00
										 |  |  |       await CloudRunnerSystem.Run(`mv ${cacheKey}.zip ${cacheFolder}`); | 
					
						
							| 
									
										
										
										
											2022-01-02 06:52:32 +00:00
										 |  |  |       RemoteClientLogger.log(`moved ${cacheKey}.zip to ${cacheFolder}`); | 
					
						
							| 
									
										
										
										
											2022-01-12 20:56:51 +00:00
										 |  |  |       assert(fs.existsSync(`${path.join(cacheFolder, cacheKey)}.zip`), 'cache zip exists inside cache folder'); | 
					
						
							| 
									
										
										
										
											2021-12-27 21:49:57 +00:00
										 |  |  |     } catch (error) { | 
					
						
							| 
									
										
										
										
											2022-01-06 22:47:05 +00:00
										 |  |  |       process.chdir(`${startPath}`); | 
					
						
							| 
									
										
										
										
											2021-12-27 21:49:57 +00:00
										 |  |  |       throw error; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-01-06 22:47:05 +00:00
										 |  |  |     process.chdir(`${startPath}`); | 
					
						
							| 
									
										
										
										
											2021-12-27 21:12:46 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-12-31 20:33:55 +00:00
										 |  |  |   public static async PullFromCache(cacheFolder: string, destinationFolder: string, cacheKey: string = ``) { | 
					
						
							| 
									
										
										
										
											2022-01-06 22:47:05 +00:00
										 |  |  |     const startPath = process.cwd(); | 
					
						
							| 
									
										
										
										
											2022-01-02 05:21:58 +00:00
										 |  |  |     RemoteClientLogger.log(`Caching for ${path.basename(destinationFolder)}`); | 
					
						
							| 
									
										
										
										
											2021-12-27 21:49:57 +00:00
										 |  |  |     try { | 
					
						
							|  |  |  |       if (!fs.existsSync(cacheFolder)) { | 
					
						
							| 
									
										
										
										
											2022-01-02 22:48:43 +00:00
										 |  |  |         await CloudRunnerSystem.Run(`mkdir -p ${cacheFolder}`); | 
					
						
							| 
									
										
										
										
											2021-12-27 21:49:57 +00:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2021-12-27 21:12:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:49:57 +00:00
										 |  |  |       if (!fs.existsSync(destinationFolder)) { | 
					
						
							| 
									
										
										
										
											2022-01-02 22:48:43 +00:00
										 |  |  |         await CloudRunnerSystem.Run(`mkdir -p ${destinationFolder}`); | 
					
						
							| 
									
										
										
										
											2021-12-27 21:49:57 +00:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2021-12-27 21:12:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-02 22:48:43 +00:00
										 |  |  |       const latestInBranch = await (await CloudRunnerSystem.Run(`ls -t "${cacheFolder}" | grep .zip$ | head -1`)) | 
					
						
							| 
									
										
										
										
											2022-01-02 06:52:32 +00:00
										 |  |  |         .replace(/\n/g, ``) | 
					
						
							|  |  |  |         .replace('.zip', ''); | 
					
						
							| 
									
										
										
										
											2021-12-27 21:12:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:49:57 +00:00
										 |  |  |       process.chdir(cacheFolder); | 
					
						
							| 
									
										
										
										
											2021-12-27 21:12:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-02 18:29:54 +00:00
										 |  |  |       const cacheSelection = cacheKey !== `` && fs.existsSync(`${cacheKey}.zip`) ? cacheKey : latestInBranch; | 
					
						
							| 
									
										
										
										
											2022-01-02 17:02:13 +00:00
										 |  |  |       await CloudRunnerLogger.log(`cache key ${cacheKey} selection ${cacheSelection}`); | 
					
						
							| 
									
										
										
										
											2022-01-01 03:37:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-02 18:29:54 +00:00
										 |  |  |       if (fs.existsSync(`${cacheSelection}.zip`)) { | 
					
						
							| 
									
										
										
										
											2022-01-13 22:08:44 +00:00
										 |  |  |         await CloudRunnerSystem.Run(`mkdir -p results`); | 
					
						
							| 
									
										
										
										
											2021-12-31 17:52:01 +00:00
										 |  |  |         if (Input.cloudRunnerTests) { | 
					
						
							| 
									
										
										
										
											2022-01-02 22:48:43 +00:00
										 |  |  |           await CloudRunnerSystem.Run(`tree ${destinationFolder}`); | 
					
						
							| 
									
										
										
										
											2021-12-31 17:52:01 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-01-12 23:47:39 +00:00
										 |  |  |         RemoteClientLogger.log(`cache item exists ${cacheFolder}/${cacheSelection}.zip`); | 
					
						
							| 
									
										
										
										
											2022-01-06 22:44:25 +00:00
										 |  |  |         assert(`${fs.existsSync(destinationFolder)}`); | 
					
						
							| 
									
										
										
										
											2022-01-12 23:47:39 +00:00
										 |  |  |         assert(`${fs.existsSync(`${cacheSelection}.zip`)}`); | 
					
						
							| 
									
										
										
										
											2022-01-13 22:35:17 +00:00
										 |  |  |         const fullDestination = path.join(process.cwd(), path.basename(destinationFolder), `results`); | 
					
						
							| 
									
										
										
										
											2022-01-13 00:02:57 +00:00
										 |  |  |         if (Input.cloudRunnerTests) { | 
					
						
							|  |  |  |           await CloudRunnerSystem.Run(`tree ${cacheFolder}`); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-01-13 22:35:17 +00:00
										 |  |  |         await extract(`${cacheSelection}.zip`, { dir: fullDestination }); | 
					
						
							| 
									
										
										
										
											2022-01-13 00:02:57 +00:00
										 |  |  |         if (Input.cloudRunnerTests) { | 
					
						
							| 
									
										
										
										
											2022-01-13 00:46:40 +00:00
										 |  |  |           await CloudRunnerSystem.Run(`tree ${fullDestination}`); | 
					
						
							| 
									
										
										
										
											2022-01-13 00:02:57 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-01-12 23:47:39 +00:00
										 |  |  |         RemoteClientLogger.log(`cache item extracted to ${fullDestination}`); | 
					
						
							|  |  |  |         assert(`${fs.existsSync(fullDestination)}`); | 
					
						
							| 
									
										
										
										
											2022-01-13 22:35:17 +00:00
										 |  |  |         await CloudRunnerSystem.Run(`mv "${fullDestination}" "${destinationFolder}"`); | 
					
						
							| 
									
										
										
										
											2021-12-27 21:49:57 +00:00
										 |  |  |       } else { | 
					
						
							| 
									
										
										
										
											2021-12-31 20:33:55 +00:00
										 |  |  |         RemoteClientLogger.logWarning(`cache item ${cacheKey} doesn't exist ${destinationFolder}`); | 
					
						
							| 
									
										
										
										
											2021-12-27 21:49:57 +00:00
										 |  |  |         if (cacheSelection !== ``) { | 
					
						
							| 
									
										
										
										
											2022-01-03 02:49:51 +00:00
										 |  |  |           if (Input.cloudRunnerTests) { | 
					
						
							|  |  |  |             await CloudRunnerSystem.Run(`tree ${cacheFolder}`); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           RemoteClientLogger.logWarning(`cache item ${cacheKey}.zip doesn't exist ${destinationFolder}`); | 
					
						
							| 
									
										
										
										
											2021-12-28 01:01:17 +00:00
										 |  |  |           throw new Error(`Failed to get cache item, but cache hit was found: ${cacheSelection}`); | 
					
						
							| 
									
										
										
										
											2021-12-27 21:49:57 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-12-27 21:12:46 +00:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2021-12-27 21:49:57 +00:00
										 |  |  |     } catch (error) { | 
					
						
							| 
									
										
										
										
											2022-01-06 22:47:05 +00:00
										 |  |  |       process.chdir(`${startPath}`); | 
					
						
							| 
									
										
										
										
											2021-12-27 21:49:57 +00:00
										 |  |  |       throw error; | 
					
						
							| 
									
										
										
										
											2021-12-27 21:12:46 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-01-06 22:47:05 +00:00
										 |  |  |     process.chdir(`${startPath}`); | 
					
						
							| 
									
										
										
										
											2021-12-27 21:12:46 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-12-27 21:25:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   public static handleCachePurging() { | 
					
						
							| 
									
										
										
										
											2022-01-08 01:16:22 +00:00
										 |  |  |     if (process.env.PURGE_REMOTE_BUILDER_CACHE !== undefined) { | 
					
						
							| 
									
										
										
										
											2021-12-31 20:12:47 +00:00
										 |  |  |       RemoteClientLogger.log(`purging ${CloudRunnerState.purgeRemoteCaching}`); | 
					
						
							| 
									
										
										
										
											2021-12-27 21:25:46 +00:00
										 |  |  |       fs.rmdirSync(CloudRunnerState.cacheFolder, { recursive: true }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-12-27 21:12:46 +00:00
										 |  |  | } |