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) {
|
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:22:25 +00:00
|
|
|
process.chdir(sourceFolder);
|
2021-12-31 20:33:55 +00:00
|
|
|
|
|
|
|
|
if (Input.cloudRunnerTests) {
|
2022-01-06 22:22:25 +00:00
|
|
|
CloudRunnerLogger.log(`Hashed cache folder ${await LFSHashing.hashAllFiles(sourceFolder)} ${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 21:46:40 +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}`));
|
|
|
|
|
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) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
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-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`);
|
2021-12-27 21:49:57 +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-02 22:48:43 +00:00
|
|
|
await CloudRunnerSystem.Run(`mv ${path.basename(destinationFolder)}/* ${destinationFolder}`);
|
2022-01-02 17:24:50 +00:00
|
|
|
assert(fs.existsSync(`${path.join(destinationFolder, `${cacheSelection}.zip`)}`));
|
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) {
|
|
|
|
|
throw error;
|
2021-12-27 21:12:46 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-12-27 21:25:46 +00:00
|
|
|
|
|
|
|
|
public static handleCachePurging() {
|
|
|
|
|
if (process.env.purgeRemoteCaching !== 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-02 22:48:43 +00:00
|
|
|
await CloudRunnerSystem.Run(
|
2021-12-27 21:25:46 +00:00
|
|
|
`echo ' '
|
|
|
|
|
echo "LFS cache for $branch"
|
2021-12-31 20:33:55 +00:00
|
|
|
du -sch "${CloudRunnerState.lfsCacheFolderFull}/"
|
2021-12-27 21:25:46 +00:00
|
|
|
echo '**'
|
|
|
|
|
echo "Library cache for $branch"
|
2021-12-31 20:33:55 +00:00
|
|
|
du -sch "${CloudRunnerState.libraryCacheFolderFull}/"
|
2021-12-27 21:25:46 +00:00
|
|
|
echo '**'
|
|
|
|
|
echo "Branch: $branch"
|
|
|
|
|
du -sch "${CloudRunnerState.cacheFolderFull}/"
|
|
|
|
|
echo '**'
|
|
|
|
|
echo 'Full cache'
|
2022-01-03 05:45:37 +00:00
|
|
|
du -sch "${CloudRunnerState.cacheFolderFull}/../"
|
2021-12-27 21:25:46 +00:00
|
|
|
echo ' '`,
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-12-27 21:12:46 +00:00
|
|
|
}
|