feat(update): move caddy code into separate file and support git revs

This commit is contained in:
matt1432 2025-04-08 19:09:03 -04:00
parent d4d07b8a8d
commit 59cfbec460
4 changed files with 99 additions and 78 deletions
apps/update-sources/src
configurations/cluster/modules/caddy

View file

@ -3,6 +3,7 @@ import { writeFileSync } from 'node:fs';
import { parseArgs } from './lib';
import { updateCaddyPlugins } from './caddy';
import { updateDocker } from './docker';
import { updateFirefoxAddons } from '././firefox';
import { updateFlakeInputs } from './flake';
@ -10,7 +11,6 @@ import updateNodeModules from './node-modules';
import {
runNixUpdate,
updateCaddyPlugins,
updateVuetorrent,
} from './misc';

View file

@ -0,0 +1,89 @@
import { writeFileSync } from 'node:fs';
import { spawnSync } from 'node:child_process';
import { replaceInFile } from './lib';
/* Types */
interface Plugin {
url: string
version: string
type: 'version' | 'git'
}
/* Constants */
const FLAKE = process.env.FLAKE;
const genPluginsText = (
plugins: Record<string, Plugin>,
) => `{
plugins = {
${Object.entries(plugins)
.map(([key, value]) => `
${key} = {
url = "${value.url}";
version = "${value.version}";
type = "${value.type}";
};
`)
.join('')}
};
hash = "";
}
`;
export const updateCaddyPlugins = () => {
let updates = '';
const dir = `${FLAKE}/configurations/cluster/modules/caddy`;
// Setup workspace
spawnSync(
[
'rm -rf /tmp/update-caddy',
'mkdir -p /tmp/update-caddy',
'cd /tmp/update-caddy || exit 1',
'go mod init temp',
].join('; '),
[],
{ shell: true, cwd: '/tmp' },
);
const plugins = JSON.parse(spawnSync('nix',
['eval', '-f', `${dir}/plugins.nix`, '--json'],
{ shell: true }).stdout.toString()).plugins as Record<string, Plugin>;
// Get most recent versions of plugins
Object.entries(plugins).forEach(([key, value]) => {
const NEW_VERSION = spawnSync([
'go mod init temp > /dev/null',
`go get ${value.url}${value.type === 'git' ? '@HEAD' : ''} > /dev/null`,
`grep '${value.url}' go.mod`,
].join('; '), [], { shell: true, cwd: '/tmp/update-caddy' })
.stdout
.toString()
.trim()
.replace(' // indirect', '')
.split(' ')[1];
if (plugins[key].version !== NEW_VERSION) {
updates += `${key}: ${plugins[key].version} -> ${NEW_VERSION}\n`;
plugins[key].version = NEW_VERSION;
}
});
writeFileSync(`${dir}/plugins.nix`, genPluginsText(plugins));
// Get new hash
const caddyPkgAttr = 'nixosConfigurations.thingone.config.services.caddy.package';
const NEW_HASH = spawnSync(
`nix build "$FLAKE#${caddyPkgAttr}" |& sed -n 's/.*got: *//p'`,
[],
{ shell: true },
).stdout.toString().trim();
replaceInFile(/hash = ".*";/, `hash = "${NEW_HASH}";`, `${dir}/plugins.nix`);
return updates;
};

View file

@ -1,7 +1,7 @@
import { writeFileSync } from 'node:fs';
import { spawnSync } from 'node:child_process';
import { parseFetchurl, replaceInFile } from './lib';
import { parseFetchurl } from './lib';
/* Constants */
@ -84,78 +84,3 @@ export const runNixUpdate = (
stderr: execution.stderr.toString(),
};
};
const genPluginsText = (
plugins: Record<string, { url: string, version: string }>,
) => `# This file was autogenerated. DO NOT EDIT!
{
plugins = {
${Object.entries(plugins)
.map(([key, value]) => `
${key} = {
url = "${value.url}";
version = "${value.version}";
};
`)
.join('')}
};
hash = "";
}
`;
export const updateCaddyPlugins = () => {
let updates = '';
const dir = `${FLAKE}/configurations/cluster/modules/caddy`;
// Setup workspace
spawnSync(
[
'rm -rf /tmp/update-caddy',
'mkdir -p /tmp/update-caddy',
'cd /tmp/update-caddy || exit 1',
'go mod init temp',
].join('; '),
[],
{ shell: true, cwd: '/tmp' },
);
const plugins = JSON.parse(spawnSync('nix',
['eval', '-f', `${dir}/plugins.nix`, '--json'],
{ shell: true }).stdout.toString()).plugins as Record<string, { url: string, version: string }>;
// Get most recent versions of plugins
Object.entries(plugins).forEach(([key, value]) => {
const NEW_VERSION = spawnSync([
'go mod init temp > /dev/null',
`go get ${value.url} > /dev/null`,
`grep '${value.url}' go.mod`,
].join('; '), [], { shell: true, cwd: '/tmp/update-caddy' })
.stdout
.toString()
.trim()
.replace(' // indirect', '')
.split(' ')[1];
if (plugins[key].version !== NEW_VERSION) {
updates += `${key}: ${plugins[key].version} -> ${NEW_VERSION}`;
plugins[key].version = NEW_VERSION;
}
});
writeFileSync(`${dir}/plugins.nix`, genPluginsText(plugins));
// Get new hash
const caddyPkgAttr = 'nixosConfigurations.thingone.config.services.caddy.package';
const NEW_HASH = spawnSync(
`nix build "$FLAKE#${caddyPkgAttr}" |& sed -n 's/.*got: *//p'`,
[],
{ shell: true },
).stdout.toString().trim();
replaceInFile(/hash = ".*";/, `hash = "${NEW_HASH}";`, `${dir}/plugins.nix`);
return updates;
};

View file

@ -1,11 +1,18 @@
# This file was autogenerated. DO NOT EDIT!
{
plugins = {
certmagic = {
url = "github.com/caddyserver/certmagic";
version = "v0.22.2";
type = "version";
};
cloudflare = {
url = "github.com/caddy-dns/cloudflare";
version = "v0.0.0-20250228175314-1fb64108d4de";
type = "version";
};
};
hash = "sha256-YYpsf8HMONR1teMiSymo2y+HrKoxuJMKIea5/NEykGc=";
hash = "sha256-rT+igzVTqHXUHqYFENH9GJYNTgY6oB4XlEzUU/vihdk=";
}