unity-builder/src/model/cli/remote-client/remote-client-services/caching.ts

117 lines
4.6 KiB
TypeScript
Raw Normal View History

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';
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}`);
}
2021-12-31 20:33:55 +00:00
if (Input.cloudRunnerTests) {
await Caching.printFullCacheHierarchySize();
}
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-06 22:44:25 +00:00
assert(fs.existsSync(`./../${path.basename(sourceFolder)}`));
2022-01-02 17:02:13 +00:00
2022-01-02 22:48:43 +00:00
await CloudRunnerSystem.Run(
2022-01-06 22:22:25 +00:00
`zip${Input.cloudRunnerTests ? '' : ' -q'} -r ${cacheKey}.zip ./../${path.basename(sourceFolder)}`,
2022-01-02 18:29:54 +00:00
);
assert(fs.existsSync(`${cacheKey}.zip`));
2022-01-02 17:24:50 +00:00
assert(fs.existsSync(`${cacheFolder}`));
2022-01-06 22:44:25 +00:00
assert(fs.existsSync(`./../${path.basename(sourceFolder)}`));
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-02 17:24:50 +00:00
assert(fs.existsSync(`${path.join(cacheFolder, cacheKey)}.zip`));
2021-12-31 20:33:55 +00:00
if (Input.cloudRunnerTests) {
await Caching.printFullCacheHierarchySize();
}
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`)) {
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
}
2021-12-31 20:12:47 +00:00
RemoteClientLogger.log(`cache item exists`);
2022-01-06 22:44:25 +00:00
assert(`${fs.existsSync(destinationFolder)}`);
2022-01-02 23:44:49 +00:00
await CloudRunnerSystem.Run(`unzip -q ${cacheSelection}.zip -d ${path.basename(destinationFolder)}`);
2022-01-08 21:28:08 +00:00
process.chdir(path.basename(destinationFolder));
2022-01-08 22:09:01 +00:00
await CloudRunnerSystem.Run(`mv * ${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-31 20:33:55 +00:00
public static async printFullCacheHierarchySize() {
2022-01-08 01:16:22 +00:00
await CloudRunnerSystem.Run(`du -sch "${CloudRunnerState.lfsCacheFolderFull}/"`);
await CloudRunnerSystem.Run(`du -sch "${CloudRunnerState.libraryCacheFolderFull}/"`);
await CloudRunnerSystem.Run(`du -sch "${CloudRunnerState.cacheFolderFull}/"`);
await CloudRunnerSystem.Run(`du -sch "${CloudRunnerState.cacheFolderFull}/../"`);
2021-12-27 21:25:46 +00:00
}
2021-12-27 21:12:46 +00:00
}