-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CI] Validate CDN assets in typescript (#181733)
## Summary Replaces bash/curl validation with Typescript, to see if it's any faster or more reliable This was a successful run: https://buildkite.com/elastic/kibana-artifacts-container-image/builds/11689#018f1a82-bf0f-4cd4-ac34-4f822d73a17b - the validation seems to have taken place in 5 seconds.
- Loading branch information
Showing
2 changed files
with
99 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import * as glob from 'glob'; | ||
import axios from 'axios'; | ||
|
||
const CDN_URL_PREFIX = process.argv[2]; | ||
const CDN_ASSETS_FOLDER = process.argv[3]; | ||
|
||
async function main() { | ||
const allAssets = glob.sync('**/*', { cwd: CDN_ASSETS_FOLDER, nodir: true }); | ||
const totalAssetCount = allAssets.length; | ||
let assetsProcessed = 0; | ||
let assetsFound = 0; | ||
|
||
const batchSize = 50; | ||
|
||
console.log(`Starting CDN asset validation for ${totalAssetCount} assets...`); | ||
|
||
while (assetsProcessed < totalAssetCount) { | ||
const batch = allAssets.slice(assetsProcessed, assetsProcessed + batchSize); | ||
const results = await Promise.all(batch.map((url) => headAssetUrlWithRetry(url))); | ||
assetsProcessed += results.length; | ||
results.forEach((result) => { | ||
if (result.status === 200) { | ||
console.log(`Testing ${result.assetPath}...${result.status}`); | ||
assetsFound++; | ||
} else { | ||
console.error(`Testing ${result.assetPath}...${result.status} (${result.testUrl})`); | ||
} | ||
}); | ||
} | ||
|
||
return { | ||
totalAssetCount, | ||
assetsProcessed, | ||
assetsFound, | ||
assetsNotFound: totalAssetCount - assetsFound, | ||
}; | ||
} | ||
|
||
async function headAssetUrl(assetPath: string) { | ||
const testUrl = `${CDN_URL_PREFIX}/${assetPath}`; | ||
const response = await axios.head(testUrl, { | ||
timeout: 1000, | ||
}); | ||
return { | ||
status: response.status, | ||
testUrl, | ||
assetPath, | ||
}; | ||
} | ||
|
||
async function headAssetUrlWithRetry( | ||
assetPath: string, | ||
retries = 5 | ||
): Promise<{ | ||
status: number; | ||
testUrl: string; | ||
assetPath: string; | ||
}> { | ||
const result = await headAssetUrl(assetPath); | ||
if (result.status === 200) { | ||
return result; | ||
} else if (retries > 0) { | ||
console.log(`Retrying ${assetPath}...(retries left: ${retries})`); | ||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
return headAssetUrlWithRetry(assetPath, retries - 1); | ||
} else { | ||
return { | ||
status: result.status || 0, | ||
testUrl: result.testUrl, | ||
assetPath, | ||
}; | ||
} | ||
} | ||
|
||
main() | ||
.then(({ totalAssetCount, assetsNotFound }) => { | ||
if (assetsNotFound) { | ||
console.error(`Couldn't find ${assetsNotFound} assets on CDN.`); | ||
process.exit(1); | ||
} else { | ||
console.log(`All ${totalAssetCount} found on CDN.`); | ||
process.exit(0); | ||
} | ||
}) | ||
.catch((error) => { | ||
console.error(error.message); | ||
process.exit(1); | ||
}); |