-
Notifications
You must be signed in to change notification settings - Fork 2k
ci: add architectures update script #1341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
ttshivers
wants to merge
1
commit into
nodejs:main
Choose a base branch
from
ttshivers:arch_update
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,47 @@ | ||
name: update-architectures | ||
|
||
on: | ||
# Convert to schedule when done or whatever is preferred | ||
push: | ||
pull_request: | ||
|
||
jobs: | ||
update-architectures: | ||
name: update-architectures | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout the docker-node repo | ||
uses: actions/checkout@v2 | ||
with: | ||
path: docker-node | ||
|
||
- name: Checkout the official-images repo | ||
uses: actions/checkout@v2 | ||
with: | ||
path: official-images | ||
repository: docker-library/official-images | ||
|
||
- name: Download bashbrew | ||
run: | | ||
mkdir -p ${GITHUB_WORKSPACE}/bin | ||
wget --no-verbose -O ${GITHUB_WORKSPACE}/bin/bashbrew https://doi-janky.infosiftr.net/job/bashbrew/job/master/lastSuccessfulBuild/artifact/bashbrew-amd64 | ||
sudo chmod +x ${GITHUB_WORKSPACE}/bin/bashbrew | ||
echo "::add-path::${GITHUB_WORKSPACE}/bin" | ||
|
||
- name: Update architectures | ||
uses: actions/github-script@v3 | ||
id: arch-updater | ||
env: | ||
BASHBREW_LIBRARY: "${{ github.workspace }}/official-images/library" | ||
with: | ||
script: | | ||
const script = require(`${process.env.GITHUB_WORKSPACE}/docker-node/updateArches.js`) | ||
return script(); | ||
|
||
- name: Open a PR | ||
if: steps.arch-updater.outputs.result == 'true' | ||
# TODO: open a PR | ||
run: | | ||
cd docker-node | ||
git diff --exit-code |
This file contains hidden or 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,146 @@ | ||
const { execFileSync } = require('child_process'); | ||
const { readFileSync, readdirSync, writeFileSync } = require('fs'); | ||
const path = require('path'); | ||
|
||
const nodeDirRegex = /^\d+$/; | ||
|
||
// Given a name and a tag, this returns an array of architectures that it supports | ||
const fetchImageArches = (repoTag) => execFileSync('bashbrew', [ | ||
'cat', repoTag, | ||
], { encoding: 'utf8' }).split('\n') | ||
.find((line) => line.startsWith('Architectures:')) | ||
.split(':')[1] | ||
.trim() | ||
.split(/\s*,\s*/); | ||
|
||
// Parses an "architectures" file into an object like: | ||
// { | ||
// arch1: ['variant1', 'variant2'], | ||
// //... | ||
// } | ||
const parseArchitecturesFile = (file) => Object.fromEntries( | ||
[...readFileSync(file, 'utf8').matchAll(/^(?<arch>\S+)\s+(?<variants>\S+)$/mg)] | ||
.slice(1) | ||
.map(({ groups: { arch, variants } }) => [arch, variants.split(',')]), | ||
); | ||
|
||
// Takes in an object like: | ||
// { | ||
// arch1: ['variant1', 'variant2'], | ||
// // ... | ||
// } | ||
// and returns an object like | ||
// { | ||
// variant1: ['arch1', 'arch2'], | ||
// // ... | ||
// } | ||
const invertObject = (obj) => Object.entries(obj) | ||
.reduce((acc, [key, vals]) => vals.reduce((valAcc, val) => { | ||
const { [val]: keys, ...rest } = valAcc; | ||
return { | ||
...rest, | ||
[val]: keys | ||
? [...keys, key] | ||
: [key], | ||
}; | ||
}, acc), {}); | ||
|
||
// Returns a list of the child directories in the given path | ||
const getChildDirectories = (parent) => readdirSync(parent, { withFileTypes: true }) | ||
.filter((dirent) => dirent.isDirectory()) | ||
.map(({ name }) => path.resolve(parent, name)); | ||
|
||
const getNodeVerionDirs = (base) => getChildDirectories(base) | ||
.filter((childPath) => nodeDirRegex.test(path.basename(childPath))); | ||
|
||
// Assume no duplicates | ||
const areArraysEquilivant = (arches1, arches2) => arches1.length === arches2.length | ||
&& arches1.every((arch) => arches2.includes(arch)); | ||
|
||
// Returns the paths of Dockerfiles that are at: base/*/Dockerfile | ||
const getDockerfilesInChildDirs = (base) => getChildDirectories(base) | ||
.map((childDir) => path.resolve(childDir, 'Dockerfile')); | ||
|
||
// Given a path to a Dockerfile like .../14/variant/Dockerfile, this will return "variant" | ||
const getVariantFromPath = (file) => path.dirname(file).split(path.sep).slice(-1); | ||
|
||
const getBaseImageFromDockerfile = (file) => readFileSync(file, 'utf8') | ||
.match(/^FROM (\S+)/m)[1]; | ||
|
||
// Given a dockerfile, this function returns an array like [variant, [arch1, arch2, ...]] | ||
const getVariantAndArches = (dockerfile) => { | ||
const variant = getVariantFromPath(dockerfile); | ||
const baseImage = getBaseImageFromDockerfile(dockerfile); | ||
const arches = fetchImageArches(baseImage); | ||
|
||
// TODO: filter by arches node supports | ||
return [variant, arches]; | ||
}; | ||
|
||
const getStoredVariantArches = (file) => { | ||
const storedArchVariants = parseArchitecturesFile(file); | ||
return invertObject(storedArchVariants); | ||
}; | ||
|
||
const areVariantArchesEquilivant = (current, stored) => Object.keys(current).length | ||
=== Object.keys(stored).length | ||
&& Object.entries(current).every( | ||
([variant, arches]) => stored[variant] && areArraysEquilivant(arches, stored[variant]), | ||
); | ||
|
||
const formatEntry = ([arch, variants], variantOffset) => `${arch}${' '.repeat(variantOffset - arch.length)}${variants.join(',')}`; | ||
|
||
const sortObjectKeys = (obj) => Object.keys(obj) | ||
.sort() | ||
.reduce((acc, key) => ({ | ||
...acc, | ||
[key]: obj[key] | ||
}), {}); | ||
|
||
const storeArchitectures = (variantArches, architecturesFile) => { | ||
const archVariants = sortObjectKeys(invertObject(variantArches)); | ||
const data = { | ||
'bashbrew-arch': ['variants'], | ||
...archVariants, | ||
}; | ||
|
||
const maxKeyLength = Math.max(...Object.keys(data).map((key) => key.length)); | ||
// Variants start 2 spaces after the longest key | ||
const variantOffset = maxKeyLength + 2; | ||
|
||
const str = Object.entries(data) | ||
.map((entry) => formatEntry(entry, variantOffset)) | ||
.join('\n') + '\n'; | ||
|
||
writeFileSync(architecturesFile, str); | ||
ttshivers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Just here for debugging purposes | ||
console.log(str); | ||
console.log('\n\n'); | ||
}; | ||
|
||
const updateNodeDirArches = (nodeDir) => { | ||
const dockerfiles = getDockerfilesInChildDirs(nodeDir); | ||
|
||
const currentVariantArches = Object.fromEntries(dockerfiles.map(getVariantAndArches)); | ||
const architecturesFile = path.resolve(nodeDir, 'architectures'); | ||
const storedVariantArches = getStoredVariantArches(architecturesFile); | ||
|
||
if (areVariantArchesEquilivant(currentVariantArches, storedVariantArches)) { | ||
console.log('Architectures up-to-date: ', nodeDir); | ||
return false; | ||
} | ||
|
||
console.log('Architectures outdated: ', nodeDir); | ||
storeArchitectures(currentVariantArches, architecturesFile); | ||
|
||
return true; | ||
}; | ||
|
||
const updateArchitectures = () => { | ||
const nodeDirs = getNodeVerionDirs(__dirname); | ||
const dirsUpdated = nodeDirs.map(updateNodeDirArches); | ||
return dirsUpdated.some((updated) => updated); | ||
}; | ||
|
||
module.exports = updateArchitectures; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.