Initial commit, most things work
Some checks failed
Build and deploy / deploy (push) Failing after 46s

This commit is contained in:
2025-04-26 20:56:56 +01:00
commit 35c8964fe5
25 changed files with 1232 additions and 0 deletions

23
src/server/api/root.ts Normal file
View File

@@ -0,0 +1,23 @@
import { dockerRouter } from "@/server/api/routers/docker";
import { createCallerFactory, createTRPCRouter } from "@/server/api/trpc";
/**
* This is the primary router for your server.
*
* All routers added in /api/routers should be manually added here.
*/
export const appRouter = createTRPCRouter({
docker: dockerRouter,
});
// export type definition of API
export type AppRouter = typeof appRouter;
/**
* Create a server-side caller for the tRPC API.
* @example
* const trpc = createCaller(createContext);
* const res = await trpc.post.all();
* ^? Post[]
*/
export const createCaller = createCallerFactory(appRouter);

View File

@@ -0,0 +1,84 @@
import Docker from "dockerode";
import semver from "semver";
import { $ } from "zx";
import { createTRPCRouter, publicProcedure } from "@/server/api/trpc";
export const dockerRouter = createTRPCRouter({
list: publicProcedure.query(async () => {
const docker = new Docker({ socketPath: "/var/run/docker.sock" });
const containers = await docker.listContainers();
// curl -fsSL -v "https://quay.io/token?service=quay.io&scope=repository:linuxserver/code-server:pull"
// curl -fsSL -v -H "Accept: application/vnd.docker.distribution.manifest.list.v2+json" -H "Authorization: Bearer djE6bGludXhzZXJ2ZXIvY29kZS1zZXJ2ZXI6MTc0NTQ1NTk1MTY3NjM5ODA4Mg==" quay.io/v2/linuxserver.io/code-server/tags/list
// curl -fsSL -v -H "Accept: application/vnd.docker.distribution.manifest.list.v2+json" quay.io/v2/linuxserver.io/code-server/tags/list
// link: </v2/linuxserver.io/code-server/tags/list?n=100&last=4.18.0-ls180>; rel="next"
// link: </v2/linuxserver.io/code-server/tags/list?n=100&last=4.90.1-ls217>; rel="next"
// curl -fsSL -v -H "Accept: application/json" "hub.docker.com/v2/repositories/linuxserver/code-server/tags?n=25&ordering=last_updated"
return Promise.all(
containers.map(async (container) => {
const version = (
await docker.getImage(container.Image).inspect()
).RepoTags[0]?.split(":")[1];
const latest = {
version: version,
hash: "N/A",
};
if (version && isSemver(version)) {
latest.version = await getLatestSemverTag(container.Image);
latest.hash = await getLatestHash(
`${container.Image.split(":")[0]}:${latest.version}`,
);
} else {
latest.hash = await getLatestHash(container.Image);
}
return {
...container,
current: {
version,
hash: (
await docker.getImage(container.Image).inspect()
).RepoDigests[0]
?.split(":")[1]
?.substring(0, 12),
},
latest,
};
}),
);
}),
});
function isSemver(tag: string): boolean {
const removeV = tag.replace(/^v/i, "");
return !!semver.valid(removeV);
}
async function getLatestSemverTag(image: string): Promise<string> {
try {
const allTags =
await $`bin/regctl tag ls --exclude 'version.*' --exclude 'unstable.*' --exclude '.*rc.*' --exclude 'lib.*' --exclude 'release.*' --exclude 'arm.*
' --exclude 'amd.*' ${image}`.text();
const semverTags = allTags
.split("\n")
.slice(-20)
.filter((tag) => isSemver(tag));
return semver.rsort(semverTags)[0] ?? "Error";
} catch (ex) {
return "Error";
}
}
async function getLatestHash(image: string): Promise<string> {
try {
const latestImage = await $`bin/regctl image digest ${image}`.text();
const latestHash = latestImage.split(":")?.[1];
return latestHash?.substring(0, 12) ?? "Error";
} catch (ex) {
return "Error";
}
}

97
src/server/api/trpc.ts Normal file
View File

@@ -0,0 +1,97 @@
/**
* YOU PROBABLY DON'T NEED TO EDIT THIS FILE, UNLESS:
* 1. You want to modify request context (see Part 1).
* 2. You want to create a new middleware or type of procedure (see Part 3).
*
* TL;DR - This is where all the tRPC server stuff is created and plugged in. The pieces you will
* need to use are documented accordingly near the end.
*/
import { initTRPC } from "@trpc/server";
import superjson from "superjson";
import { ZodError } from "zod";
/**
* 1. CONTEXT
*
* This section defines the "contexts" that are available in the backend API.
*
* These allow you to access things when processing a request, like the database, the session, etc.
*
* This helper generates the "internals" for a tRPC context. The API handler and RSC clients each
* wrap this and provides the required context.
*
* @see https://trpc.io/docs/server/context
*/
export const createTRPCContext = async (opts: { headers: Headers }) => {
return {
...opts,
};
};
/**
* 2. INITIALIZATION
*
* This is where the tRPC API is initialized, connecting the context and transformer. We also parse
* ZodErrors so that you get typesafety on the frontend if your procedure fails due to validation
* errors on the backend.
*/
const t = initTRPC.context<typeof createTRPCContext>().create({
transformer: superjson,
errorFormatter({ shape, error }) {
return {
...shape,
data: {
...shape.data,
zodError:
error.cause instanceof ZodError ? error.cause.flatten() : null,
},
};
},
});
/**
* Create a server-side caller.
*
* @see https://trpc.io/docs/server/server-side-calls
*/
export const createCallerFactory = t.createCallerFactory;
/**
* 3. ROUTER & PROCEDURE (THE IMPORTANT BIT)
*
* These are the pieces you use to build your tRPC API. You should import these a lot in the
* "/src/server/api/routers" directory.
*/
/**
* This is how you create new routers and sub-routers in your tRPC API.
*
* @see https://trpc.io/docs/router
*/
export const createTRPCRouter = t.router;
/**
* Middleware for timing procedure execution and adding an artificial delay in development.
*
* You can remove this if you don't like it, but it can help catch unwanted waterfalls by simulating
* network latency that would occur in production but not in local development.
*/
const timingMiddleware = t.middleware(async ({ next, path }) => {
const start = Date.now();
const result = await next();
const end = Date.now();
console.log(`[TRPC] ${path} took ${end - start}ms to execute`);
return result;
});
/**
* Public (unauthenticated) procedure
*
* This is the base piece you use to build new queries and mutations on your tRPC API. It does not
* guarantee that a user querying is authorized, but you can still access user session data if they
* are logged in.
*/
export const publicProcedure = t.procedure.use(timingMiddleware);