feat(update): migrate docker update script to typescript
All checks were successful
Discord / discord commits (push) Has been skipped

This commit is contained in:
matt1432 2024-07-20 22:02:22 -04:00
parent 970a380805
commit 60bc645ae0
5 changed files with 73 additions and 33 deletions

View file

@ -2,6 +2,7 @@
lib,
system,
buildNpmPackage,
callPackage,
makeWrapper,
mozilla-addons-to-nix,
nodejs_latest,
@ -21,6 +22,7 @@ in
npmDepsHash = "sha256-qpnQSJNl68LrsU8foJYxdBXpoFj7VKQahC9DFmleWTs=";
runtimeInputs = [
(callPackage ../../modules/arion/updateImage.nix {})
mozilla-addons-to-nix.packages.${system}.default
];
nativeBuildInputs = [makeWrapper];

View file

@ -1,5 +1,6 @@
import { parseArgs } from './lib.ts';
import { updateFirefoxAddons } from '././firefox.ts';
import { updateDocker } from './misc.ts';
const args = parseArgs();
@ -7,3 +8,12 @@ const args = parseArgs();
if (args['f'] || args['firefox']) {
console.log(updateFirefoxAddons());
}
if (args['d'] || args['docker']) {
console.log(updateDocker());
}
if (args['a'] || args['all']) {
console.log(updateFirefoxAddons());
console.log(updateDocker());
}

21
apps/update/src/misc.ts Normal file
View file

@ -0,0 +1,21 @@
import { readdirSync } from 'node:fs';
import { spawnSync } from 'node:child_process';
/* Constants */
const FLAKE = process.env.FLAKE;
export const updateDocker = () => {
let updates = '';
const FILE = `${FLAKE}/devices/nos/modules/arion`;
readdirSync(FILE, { withFileTypes: true, recursive: true }).forEach((path) => {
if (path.name === 'compose.nix') {
updates += spawnSync('updateImages', [path.parentPath], { shell: true })
.stdout.toString();
}
});
return updates;
};

View file

@ -97,39 +97,7 @@ in {
# Script for updating the images of all images of a compose.nix file
environment.systemPackages = with pkgs; [
(writeShellApplication {
name = "updateImages";
runtimeInputs = [
(writeShellApplication {
name = "pullImage";
runtimeInputs = [nix-prefetch-docker skopeo];
text = ''
FILE="$1"
IMAGE=$(sed -n 's/.*imageName = "\([^"]*\).*/\1/p' "$FILE")
TAG=$(sed -n 's/.*finalImageTag = "\([^"]*\).*/\1/p' "$FILE")
CURRENT_DIGEST=$(sed -n 's/.*imageDigest = "\([^"]*\).*/\1/p' "$FILE")
NEW_DIGEST=$(skopeo inspect "docker://$IMAGE:$TAG" | jq '.Digest' -r)
output="$IMAGE $TAG"
if ! grep "Locked" "$FILE"; then
if [[ "$CURRENT_DIGEST" != "$NEW_DIGEST" ]]; then
echo -e " $output:\n $CURRENT_DIGEST\n $NEW_DIGEST\n"
PREFETCH=$(nix-prefetch-docker "$IMAGE" "$TAG")
echo -e "pkgs:\npkgs.dockerTools.pullImage $PREFETCH" > "$FILE"
fi
fi
'';
})
];
text = ''
DIR=''${1:-"."}
find "$DIR"/images -type f -exec pullImage {} \;
'';
})
(callPackage ./updateImage.nix {})
];
};
}

View file

@ -0,0 +1,39 @@
{
nix-prefetch-docker,
skopeo,
writeShellApplication,
...
}:
writeShellApplication {
name = "updateImages";
runtimeInputs = [
(writeShellApplication {
name = "pullImage";
runtimeInputs = [nix-prefetch-docker skopeo];
text = ''
FILE="$1"
IMAGE=$(sed -n 's/.*imageName = "\([^"]*\).*/\1/p' "$FILE")
TAG=$(sed -n 's/.*finalImageTag = "\([^"]*\).*/\1/p' "$FILE")
CURRENT_DIGEST=$(sed -n 's/.*imageDigest = "\([^"]*\).*/\1/p' "$FILE")
NEW_DIGEST=$(skopeo inspect "docker://$IMAGE:$TAG" | jq '.Digest' -r)
output="$IMAGE $TAG"
if ! grep "Locked" "$FILE"; then
if [[ "$CURRENT_DIGEST" != "$NEW_DIGEST" ]]; then
echo -e " $output:\n $CURRENT_DIGEST\n $NEW_DIGEST\n"
PREFETCH=$(nix-prefetch-docker "$IMAGE" "$TAG")
echo -e "pkgs:\npkgs.dockerTools.pullImage $PREFETCH" > "$FILE"
fi
fi
'';
})
];
text = ''
DIR=''${1:-"."}
find "$DIR"/images -type f -exec pullImage {} \;
'';
}