add stax metadata
Build and push Docker image / build (push) Failing after 7s Details

This commit is contained in:
Hamcha 2024-04-19 01:17:58 +02:00
parent 334b38c5f8
commit 447f0a0d7d
Signed by: hamcha
GPG Key ID: 1669C533B8CF6D89
5 changed files with 66 additions and 5 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
.vscode
.vscode
coverage/

View File

@ -4,6 +4,7 @@
*/
import { ComposeStack, Service } from "../mod.ts";
import { setProjectRoot } from "../src/path.ts";
interface ImmichStackOptions {
stackName?: string;
@ -18,7 +19,7 @@ interface ImmichStackOptions {
export class ImmichStack extends ComposeStack {
constructor(props: ImmichStackOptions) {
super(props.stackName ?? "immich");
super(import.meta.url, props.stackName ?? "immich");
const redis = new Service(this, "redis", {
image: "registry.hub.docker.com/library/redis:6.2-alpine",
@ -88,4 +89,6 @@ export class ImmichStack extends ComposeStack {
}
}
setProjectRoot(import.meta.url);
const _stack = new ImmichStack({ immichVersion: "v1.100.0" });

View File

@ -1,5 +1,6 @@
import { ComposeSpecification } from "../compose-schema.ts";
import { ComposeSpecification, ListOrDict } from "../compose-schema.ts";
import { registerDeployment } from "../deployment.ts";
import { relative } from "../path.ts";
import { Network } from "./network.ts";
import { Service } from "./service.ts";
import { Volume } from "./volume.ts";
@ -24,7 +25,7 @@ function mapRecordToCompose<T, F extends { toCompose(): T }>(
*
* class ExampleStack extends Stack {
* constructor(name: string) {
* super(name);
* super(import.meta.url, name);
*
* const redis = this.addService("redis", {
* image: "registry.hub.docker.com/library/redis:6.2-alpine",
@ -41,7 +42,7 @@ export class ComposeStack {
private networks: Record<string, Network> = {};
private volumes: Record<string, Volume> = {};
constructor(public name: string) {
constructor(public readonly stackPath: string, public name: string) {
registerDeployment(this);
}
@ -52,6 +53,9 @@ export class ComposeStack {
throw new Error(`Service ${name} already defined.`);
}
// Inject stax labels
this.injectLabels(service);
this.services[name] = service;
return this.services[name];
}
@ -63,6 +67,11 @@ export class ComposeStack {
throw new Error(`Network ${name} already defined.`);
}
// Inject stax labels if not external
if (!network.options.external) {
this.injectLabels(network);
}
this.networks[name] = network;
return this.networks[name];
}
@ -74,6 +83,9 @@ export class ComposeStack {
throw new Error(`Volume ${name} already defined.`);
}
// Inject stax labels
this.injectLabels(volume);
this.volumes[name] = volume;
return this.volumes[name];
}
@ -86,4 +98,16 @@ export class ComposeStack {
volumes: mapRecordToCompose(this.volumes),
};
}
private injectLabels<
T extends { options: { labels?: ListOrDict | undefined } }
>(resourse: T) {
resourse.options.labels = {
...resourse.options.labels,
...{
"com.stax.stack": this.name,
"com.stax.stack-path": relative(this.stackPath),
},
};
}
}

12
src/path.ts Normal file
View File

@ -0,0 +1,12 @@
let basePath: string = "";
export function setProjectRoot(path: string) {
// Replace last segment with /
const index = path.lastIndexOf("/");
basePath = path.slice(0, index + 1);
}
export function relative(path: string): string {
// Remove base path
return path.slice(basePath.length);
}

21
src/path_test.ts Normal file
View File

@ -0,0 +1,21 @@
import { assertEquals } from "https://deno.land/std@0.223.0/assert/mod.ts";
import { relative, setProjectRoot } from "./path.ts";
Deno.test("Test relative without setProjectRoot", () => {
const rel = relative("file://c:/projects/staxman/containerlib/path.ts");
assertEquals(rel, "file://c:/projects/staxman/containerlib/path.ts");
});
Deno.test("Test relative with setProjectRoot", () => {
setProjectRoot("file://c:/projects/staxman/containerlib/module.ts");
// In subdir
const insubdir = relative(
"file://c:/projects/staxman/containerlib/subdir/path.ts"
);
assertEquals(insubdir, "subdir/path.ts");
// Same folder as base
const samedir = relative("file://c:/projects/staxman/containerlib/path.ts");
assertEquals(samedir, "path.ts");
});