2021-12-27 21:12:46 +00:00
|
|
|
import { assert } from 'console';
|
|
|
|
|
import fs from 'fs';
|
|
|
|
|
import path from 'path';
|
|
|
|
|
import CloudRunnerLogger from '../../cloud-runner/services/cloud-runner-logger';
|
2021-12-27 21:25:46 +00:00
|
|
|
import { CloudRunnerState } from '../../cloud-runner/state/cloud-runner-state';
|
2021-12-27 21:53:22 +00:00
|
|
|
import { CloudRunnerAgentSystem } from './remote-client-system';
|
2021-12-27 21:12:46 +00:00
|
|
|
|
|
|
|
|
export class Caching {
|
|
|
|
|
public static async PushToCache(cacheFolder: string, destinationFolder: string, artifactName: string) {
|
2021-12-27 21:49:57 +00:00
|
|
|
try {
|
|
|
|
|
process.chdir(`${destinationFolder}/..`);
|
2021-12-27 21:53:22 +00:00
|
|
|
await CloudRunnerAgentSystem.Run(`zip -r "${artifactName}.zip" "${path.dirname(destinationFolder)}"`);
|
2021-12-27 21:49:57 +00:00
|
|
|
assert(fs.existsSync(`${artifactName}.zip`));
|
2021-12-27 21:53:22 +00:00
|
|
|
await CloudRunnerAgentSystem.Run(`cp "${artifactName}.zip" "${path.join(cacheFolder, `${artifactName}.zip`)}"`);
|
2021-12-27 21:49:57 +00:00
|
|
|
CloudRunnerLogger.logCli(`copied ${artifactName} to ${cacheFolder}`);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
2021-12-27 21:12:46 +00:00
|
|
|
}
|
|
|
|
|
public static async PullFromCache(cacheFolder: string, destinationFolder: string, specificHashMatch: string = ``) {
|
2021-12-27 21:49:57 +00:00
|
|
|
try {
|
|
|
|
|
if (!fs.existsSync(cacheFolder)) {
|
2021-12-27 21:53:22 +00:00
|
|
|
await CloudRunnerAgentSystem.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)) {
|
2021-12-27 21:53:22 +00:00
|
|
|
await CloudRunnerAgentSystem.Run(`mkdir -p ${destinationFolder}`);
|
2021-12-27 21:49:57 +00:00
|
|
|
}
|
2021-12-27 21:12:46 +00:00
|
|
|
|
2021-12-27 21:53:22 +00:00
|
|
|
const latest = await (await CloudRunnerAgentSystem.Run(`ls -t "${cacheFolder}" | grep .zip$ | head -1`)).replace(
|
2021-12-27 21:49:57 +00:00
|
|
|
`\n`,
|
|
|
|
|
``,
|
|
|
|
|
);
|
2021-12-27 21:12:46 +00:00
|
|
|
|
2021-12-27 21:49:57 +00:00
|
|
|
process.chdir(cacheFolder);
|
|
|
|
|
let cacheSelection;
|
2021-12-27 21:12:46 +00:00
|
|
|
|
2021-12-27 21:49:57 +00:00
|
|
|
if (specificHashMatch !== ``) {
|
|
|
|
|
cacheSelection = fs.existsSync(specificHashMatch) ? specificHashMatch : latest;
|
|
|
|
|
} else {
|
|
|
|
|
cacheSelection = latest;
|
|
|
|
|
}
|
|
|
|
|
if (fs.existsSync(cacheSelection)) {
|
|
|
|
|
CloudRunnerLogger.logCli(`Library cache exists`);
|
2021-12-27 21:53:22 +00:00
|
|
|
await CloudRunnerAgentSystem.Run(`unzip "${cacheSelection}" -d "${destinationFolder}"`);
|
2021-12-27 21:49:57 +00:00
|
|
|
assert(fs.existsSync(destinationFolder));
|
2021-12-27 21:53:22 +00:00
|
|
|
await CloudRunnerAgentSystem.Run(`tree ${destinationFolder}`);
|
2021-12-27 21:49:57 +00:00
|
|
|
} else {
|
|
|
|
|
CloudRunnerLogger.logCli(`Library cache doesn't exist`);
|
|
|
|
|
if (cacheSelection !== ``) {
|
|
|
|
|
throw new Error(`Failed to get library cache, but cache hit was found: ${cacheSelection}`);
|
|
|
|
|
}
|
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) {
|
|
|
|
|
CloudRunnerLogger.logCli(`purging ${CloudRunnerState.purgeRemoteCaching}`);
|
|
|
|
|
fs.rmdirSync(CloudRunnerState.cacheFolder, { recursive: true });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static async printCacheState(lfsCacheFolder: string, libraryCacheFolder: string) {
|
2021-12-27 21:53:22 +00:00
|
|
|
await CloudRunnerAgentSystem.Run(
|
2021-12-27 21:25:46 +00:00
|
|
|
`echo ' '
|
|
|
|
|
echo "LFS cache for $branch"
|
|
|
|
|
du -sch "${lfsCacheFolder}/"
|
|
|
|
|
echo '**'
|
|
|
|
|
echo "Library cache for $branch"
|
|
|
|
|
du -sch "${libraryCacheFolder}/"
|
|
|
|
|
echo '**'
|
|
|
|
|
echo "Branch: $branch"
|
|
|
|
|
du -sch "${CloudRunnerState.cacheFolderFull}/"
|
|
|
|
|
echo '**'
|
|
|
|
|
echo 'Full cache'
|
|
|
|
|
du -sch "${CloudRunnerState.cacheFolderFull}/"
|
|
|
|
|
echo ' '`,
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-12-27 21:12:46 +00:00
|
|
|
}
|