Skip to content

Modern Docker Management, Designed for Everyone

feat: use drizzle as a database backend (#248)

Kyle Mendell54061d303945
diff --git a/Dockerfile b/Dockerfile
index 144ca1c28..3ba7f74cd 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -45,6 +45,8 @@ RUN mkdir -p /app/data && chmod 755 /app/data
COPY --from=builder /app/build ./build
COPY --from=builder /app/static ./static
+COPY --from=builder /app/src/db/migrations ./src/db/migrations
+
# Copy entrypoint script
COPY --chmod=755 scripts/docker/entrypoint.sh /usr/local/bin/entrypoint.sh
diff --git a/drizzle.config.ts b/drizzle.config.ts
new file mode 100644
index 000000000..3cb6b519e
--- /dev/null
+++ b/drizzle.config.ts
@@ -0,0 +1,11 @@
+import 'dotenv/config';
+import { defineConfig } from 'drizzle-kit';
+
+export default defineConfig({
+ out: './src/db/migrations',
+ schema: './src/db/schema.ts',
+ dialect: 'sqlite',
+ dbCredentials: {
+ url: process.env.DB_CONN_STRING || 'file:data/arcane.db'
+ }
+});
diff --git a/package-lock.json b/package-lock.json
index 5fb014a0e..6f3cff920 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -10,6 +10,7 @@
"dependencies": {
"@codemirror/lang-yaml": "^6.1.2",
"@codemirror/legacy-modes": "^6.5.1",
+ "@libsql/client": "^0.15.8",
"@swimlane/docker-reference": "^2.0.1",
"@tanstack/table-core": "^8.21.3",
"arctic": "^3.7.0",
@@ -19,6 +20,9 @@
"date-fns": "^4.1.0",
"dockerode": "^4.0.6",
"dockerode-compose": "^1.4.0",
+ "dotenv": "^16.5.0",
+ "drizzle-kit": "^0.31.1",
+ "drizzle-orm": "^0.44.1",
"js-yaml": "^4.1.0",
"nanoid": "^5.1.5",
"prettier": "^3.5.3",
@@ -61,6 +65,7 @@
"tailwind-merge": "^3.3.0",
"tailwind-variants": "^1.0.0",
"tailwindcss": "^4.1.7",
+ "tsx": "^4.19.4",
"tw-animate-css": "^1.3.0",
"typescript": "^5.8.0",
"typescript-eslint": "^8.33.0",
@@ -221,6 +226,423 @@
"w3c-keyname": "^2.2.4"
}
},
+ "node_modules/@drizzle-team/brocli": {
+ "version": "0.10.2",
+ "resolved": "https://registry.npmjs.org/@drizzle-team/brocli/-/brocli-0.10.2.tgz",
+ "integrity": "sha512-z33Il7l5dKjUgGULTqBsQBQwckHh5AbIuxhdsIxDDiZAzBOrZO6q9ogcWC65kU382AfynTfgNumVcNIjuIua6w==",
+ "license": "Apache-2.0"
+ },
+ "node_modules/@esbuild-kit/core-utils": {
+ "version": "3.3.2",
+ "resolved": "https://registry.npmjs.org/@esbuild-kit/core-utils/-/core-utils-3.3.2.tgz",
+ "integrity": "sha512-sPRAnw9CdSsRmEtnsl2WXWdyquogVpB3yZ3dgwJfe8zrOzTsV7cJvmwrKVa+0ma5BoiGJ+BoqkMvawbayKUsqQ==",
+ "deprecated": "Merged into tsx: https://tsx.is",
+ "license": "MIT",
+ "dependencies": {
+ "esbuild": "~0.18.20",
+ "source-map-support": "^0.5.21"
+ }
+ },
+ "node_modules/@esbuild-kit/core-utils/node_modules/@esbuild/android-arm": {
+ "version": "0.18.20",
+ "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.18.20.tgz",
+ "integrity": "sha512-fyi7TDI/ijKKNZTUJAQqiG5T7YjJXgnzkURqmGj13C6dCqckZBLdl4h7bkhHt/t0WP+zO9/zwroDvANaOqO5Sw==",
+ "cpu": [
+ "arm"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "android"
+ ],
+ "engines": {
+ "node": ">=12"
+ }
+ },
+ "node_modules/@esbuild-kit/core-utils/node_modules/@esbuild/android-arm64": {
+ "version": "0.18.20",
+ "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.18.20.tgz",
+ "integrity": "sha512-Nz4rJcchGDtENV0eMKUNa6L12zz2zBDXuhj/Vjh18zGqB44Bi7MBMSXjgunJgjRhCmKOjnPuZp4Mb6OKqtMHLQ==",
+ "cpu": [
+ "arm64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "android"
+ ],
+ "engines": {
+ "node": ">=12"
+ }
+ },
+ "node_modules/@esbuild-kit/core-utils/node_modules/@esbuild/android-x64": {
+ "version": "0.18.20",
+ "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.18.20.tgz",
+ "integrity": "sha512-8GDdlePJA8D6zlZYJV/jnrRAi6rOiNaCC/JclcXpB+KIuvfBN4owLtgzY2bsxnx666XjJx2kDPUmnTtR8qKQUg==",
+ "cpu": [
+ "x64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "android"
+ ],
+ "engines": {
+ "node": ">=12"
+ }
+ },
+ "node_modules/@esbuild-kit/core-utils/node_modules/@esbuild/darwin-arm64": {
+ "version": "0.18.20",
+ "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.18.20.tgz",
+ "integrity": "sha512-bxRHW5kHU38zS2lPTPOyuyTm+S+eobPUnTNkdJEfAddYgEcll4xkT8DB9d2008DtTbl7uJag2HuE5NZAZgnNEA==",
+ "cpu": [
+ "arm64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "darwin"
+ ],
+ "engines": {
+ "node": ">=12"
+ }
+ },
+ "node_modules/@esbuild-kit/core-utils/node_modules/@esbuild/darwin-x64": {
+ "version": "0.18.20",
+ "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.18.20.tgz",
+ "integrity": "sha512-pc5gxlMDxzm513qPGbCbDukOdsGtKhfxD1zJKXjCCcU7ju50O7MeAZ8c4krSJcOIJGFR+qx21yMMVYwiQvyTyQ==",
+ "cpu": [
+ "x64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "darwin"
+ ],
+ "engines": {
+ "node": ">=12"
+ }
+ },
+ "node_modules/@esbuild-kit/core-utils/node_modules/@esbuild/freebsd-arm64": {
+ "version": "0.18.20",
+ "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.18.20.tgz",
+ "integrity": "sha512-yqDQHy4QHevpMAaxhhIwYPMv1NECwOvIpGCZkECn8w2WFHXjEwrBn3CeNIYsibZ/iZEUemj++M26W3cNR5h+Tw==",
+ "cpu": [
+ "arm64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "freebsd"
+ ],
+ "engines": {
+ "node": ">=12"
+ }
+ },
+ "node_modules/@esbuild-kit/core-utils/node_modules/@esbuild/freebsd-x64": {
+ "version": "0.18.20",
+ "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.18.20.tgz",
+ "integrity": "sha512-tgWRPPuQsd3RmBZwarGVHZQvtzfEBOreNuxEMKFcd5DaDn2PbBxfwLcj4+aenoh7ctXcbXmOQIn8HI6mCSw5MQ==",
+ "cpu": [
+ "x64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "freebsd"
+ ],
+ "engines": {
+ "node": ">=12"
+ }
+ },
+ "node_modules/@esbuild-kit/core-utils/node_modules/@esbuild/linux-arm": {
+ "version": "0.18.20",
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.18.20.tgz",
+ "integrity": "sha512-/5bHkMWnq1EgKr1V+Ybz3s1hWXok7mDFUMQ4cG10AfW3wL02PSZi5kFpYKrptDsgb2WAJIvRcDm+qIvXf/apvg==",
+ "cpu": [
+ "arm"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "linux"
+ ],
+ "engines": {
+ "node": ">=12"
+ }
+ },
+ "node_modules/@esbuild-kit/core-utils/node_modules/@esbuild/linux-arm64": {
+ "version": "0.18.20",
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.18.20.tgz",
+ "integrity": "sha512-2YbscF+UL7SQAVIpnWvYwM+3LskyDmPhe31pE7/aoTMFKKzIc9lLbyGUpmmb8a8AixOL61sQ/mFh3jEjHYFvdA==",
+ "cpu": [
+ "arm64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "linux"
+ ],
+ "engines": {
+ "node": ">=12"
+ }
+ },
+ "node_modules/@esbuild-kit/core-utils/node_modules/@esbuild/linux-ia32": {
+ "version": "0.18.20",
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.18.20.tgz",
+ "integrity": "sha512-P4etWwq6IsReT0E1KHU40bOnzMHoH73aXp96Fs8TIT6z9Hu8G6+0SHSw9i2isWrD2nbx2qo5yUqACgdfVGx7TA==",
+ "cpu": [
+ "ia32"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "linux"
+ ],
+ "engines": {
+ "node": ">=12"
+ }
+ },
+ "node_modules/@esbuild-kit/core-utils/node_modules/@esbuild/linux-loong64": {
+ "version": "0.18.20",
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.18.20.tgz",
+ "integrity": "sha512-nXW8nqBTrOpDLPgPY9uV+/1DjxoQ7DoB2N8eocyq8I9XuqJ7BiAMDMf9n1xZM9TgW0J8zrquIb/A7s3BJv7rjg==",
+ "cpu": [
+ "loong64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "linux"
+ ],
+ "engines": {
+ "node": ">=12"
+ }
+ },
+ "node_modules/@esbuild-kit/core-utils/node_modules/@esbuild/linux-mips64el": {
+ "version": "0.18.20",
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.18.20.tgz",
+ "integrity": "sha512-d5NeaXZcHp8PzYy5VnXV3VSd2D328Zb+9dEq5HE6bw6+N86JVPExrA6O68OPwobntbNJ0pzCpUFZTo3w0GyetQ==",
+ "cpu": [
+ "mips64el"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "linux"
+ ],
+ "engines": {
+ "node": ">=12"
+ }
+ },
+ "node_modules/@esbuild-kit/core-utils/node_modules/@esbuild/linux-ppc64": {
+ "version": "0.18.20",
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.18.20.tgz",
+ "integrity": "sha512-WHPyeScRNcmANnLQkq6AfyXRFr5D6N2sKgkFo2FqguP44Nw2eyDlbTdZwd9GYk98DZG9QItIiTlFLHJHjxP3FA==",
+ "cpu": [
+ "ppc64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "linux"
+ ],
+ "engines": {
+ "node": ">=12"
+ }
+ },
+ "node_modules/@esbuild-kit/core-utils/node_modules/@esbuild/linux-riscv64": {
+ "version": "0.18.20",
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.18.20.tgz",
+ "integrity": "sha512-WSxo6h5ecI5XH34KC7w5veNnKkju3zBRLEQNY7mv5mtBmrP/MjNBCAlsM2u5hDBlS3NGcTQpoBvRzqBcRtpq1A==",
+ "cpu": [
+ "riscv64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "linux"
+ ],
+ "engines": {
+ "node": ">=12"
+ }
+ },
+ "node_modules/@esbuild-kit/core-utils/node_modules/@esbuild/linux-s390x": {
+ "version": "0.18.20",
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.18.20.tgz",
+ "integrity": "sha512-+8231GMs3mAEth6Ja1iK0a1sQ3ohfcpzpRLH8uuc5/KVDFneH6jtAJLFGafpzpMRO6DzJ6AvXKze9LfFMrIHVQ==",
+ "cpu": [
+ "s390x"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "linux"
+ ],
+ "engines": {
+ "node": ">=12"
+ }
+ },
+ "node_modules/@esbuild-kit/core-utils/node_modules/@esbuild/linux-x64": {
+ "version": "0.18.20",
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.18.20.tgz",
+ "integrity": "sha512-UYqiqemphJcNsFEskc73jQ7B9jgwjWrSayxawS6UVFZGWrAAtkzjxSqnoclCXxWtfwLdzU+vTpcNYhpn43uP1w==",
+ "cpu": [
+ "x64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "linux"
+ ],
+ "engines": {
+ "node": ">=12"
+ }
+ },
+ "node_modules/@esbuild-kit/core-utils/node_modules/@esbuild/netbsd-x64": {
+ "version": "0.18.20",
+ "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.18.20.tgz",
+ "integrity": "sha512-iO1c++VP6xUBUmltHZoMtCUdPlnPGdBom6IrO4gyKPFFVBKioIImVooR5I83nTew5UOYrk3gIJhbZh8X44y06A==",
+ "cpu": [
+ "x64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "netbsd"
+ ],
+ "engines": {
+ "node": ">=12"
+ }
+ },
+ "node_modules/@esbuild-kit/core-utils/node_modules/@esbuild/openbsd-x64": {
+ "version": "0.18.20",
+ "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.18.20.tgz",
+ "integrity": "sha512-e5e4YSsuQfX4cxcygw/UCPIEP6wbIL+se3sxPdCiMbFLBWu0eiZOJ7WoD+ptCLrmjZBK1Wk7I6D/I3NglUGOxg==",
+ "cpu": [
+ "x64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "openbsd"
+ ],
+ "engines": {
+ "node": ">=12"
+ }
+ },
+ "node_modules/@esbuild-kit/core-utils/node_modules/@esbuild/sunos-x64": {
+ "version": "0.18.20",
+ "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.18.20.tgz",
+ "integrity": "sha512-kDbFRFp0YpTQVVrqUd5FTYmWo45zGaXe0X8E1G/LKFC0v8x0vWrhOWSLITcCn63lmZIxfOMXtCfti/RxN/0wnQ==",
+ "cpu": [
+ "x64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "sunos"
+ ],
+ "engines": {
+ "node": ">=12"
+ }
+ },
+ "node_modules/@esbuild-kit/core-utils/node_modules/@esbuild/win32-arm64": {
+ "version": "0.18.20",
+ "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.18.20.tgz",
+ "integrity": "sha512-ddYFR6ItYgoaq4v4JmQQaAI5s7npztfV4Ag6NrhiaW0RrnOXqBkgwZLofVTlq1daVTQNhtI5oieTvkRPfZrePg==",
+ "cpu": [
+ "arm64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "win32"
+ ],
+ "engines": {
+ "node": ">=12"
+ }
+ },
+ "node_modules/@esbuild-kit/core-utils/node_modules/@esbuild/win32-ia32": {
+ "version": "0.18.20",
+ "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.18.20.tgz",
+ "integrity": "sha512-Wv7QBi3ID/rROT08SABTS7eV4hX26sVduqDOTe1MvGMjNd3EjOz4b7zeexIR62GTIEKrfJXKL9LFxTYgkyeu7g==",
+ "cpu": [
+ "ia32"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "win32"
+ ],
+ "engines": {
+ "node": ">=12"
+ }
+ },
+ "node_modules/@esbuild-kit/core-utils/node_modules/@esbuild/win32-x64": {
+ "version": "0.18.20",
+ "resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.18.20.tgz",
+ "integrity": "sha512-kTdfRcSiDfQca/y9QIkng02avJ+NCaQvrMejlsB3RRv5sE9rRoeBPISaZpKxHELzRxZyLvNts1P27W3wV+8geQ==",
+ "cpu": [
+ "x64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "win32"
+ ],
+ "engines": {
+ "node": ">=12"
+ }
+ },
+ "node_modules/@esbuild-kit/core-utils/node_modules/esbuild": {
+ "version": "0.18.20",
+ "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.18.20.tgz",
+ "integrity": "sha512-ceqxoedUrcayh7Y7ZX6NdbbDzGROiyVBgC4PriJThBKSVPWnnFHZAkfI1lJT8QFkOwH4qOS2SJkS4wvpGl8BpA==",
+ "hasInstallScript": true,
+ "license": "MIT",
+ "bin": {
+ "esbuild": "bin/esbuild"
+ },
+ "engines": {
+ "node": ">=12"
+ },
+ "optionalDependencies": {
+ "@esbuild/android-arm": "0.18.20",
+ "@esbuild/android-arm64": "0.18.20",
+ "@esbuild/android-x64": "0.18.20",
+ "@esbuild/darwin-arm64": "0.18.20",
+ "@esbuild/darwin-x64": "0.18.20",
+ "@esbuild/freebsd-arm64": "0.18.20",
+ "@esbuild/freebsd-x64": "0.18.20",
+ "@esbuild/linux-arm": "0.18.20",
+ "@esbuild/linux-arm64": "0.18.20",
+ "@esbuild/linux-ia32": "0.18.20",
+ "@esbuild/linux-loong64": "0.18.20",
+ "@esbuild/linux-mips64el": "0.18.20",
+ "@esbuild/linux-ppc64": "0.18.20",
+ "@esbuild/linux-riscv64": "0.18.20",
+ "@esbuild/linux-s390x": "0.18.20",
+ "@esbuild/linux-x64": "0.18.20",
+ "@esbuild/netbsd-x64": "0.18.20",
+ "@esbuild/openbsd-x64": "0.18.20",
+ "@esbuild/sunos-x64": "0.18.20",
+ "@esbuild/win32-arm64": "0.18.20",
+ "@esbuild/win32-ia32": "0.18.20",
+ "@esbuild/win32-x64": "0.18.20"
+ }
+ },
+ "node_modules/@esbuild-kit/esm-loader": {
+ "version": "2.6.5",
+ "resolved": "https://registry.npmjs.org/@esbuild-kit/esm-loader/-/esm-loader-2.6.5.tgz",
+ "integrity": "sha512-FxEMIkJKnodyA1OaCUoEvbYRkoZlLZ4d/eXFu9Fh8CbBBgP5EmZxrfTRyN0qpXZ4vOvqnE5YdRdcrmUUXuU+dA==",
+ "deprecated": "Merged into tsx: https://tsx.is",
+ "license": "MIT",
+ "dependencies": {
+ "@esbuild-kit/core-utils": "^3.3.2",
+ "get-tsconfig": "^4.7.0"
+ }
+ },
"node_modules/@esbuild/aix-ppc64": {
"version": "0.25.4",
"resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.25.4.tgz",
@@ -228,7 +650,6 @@
"cpu": [
"ppc64"
],
- "dev": true,
"license": "MIT",
"optional": true,
"os": [
@@ -245,7 +666,6 @@
"cpu": [
"arm"
],
- "dev": true,
"license": "MIT",
"optional": true,
"os": [
@@ -262,7 +682,6 @@
"cpu": [
"arm64"
],
- "dev": true,
"license": "MIT",
"optional": true,
"os": [
@@ -279,7 +698,6 @@
"cpu": [
"x64"
],
- "dev": true,
"license": "MIT",
"optional": true,
"os": [
@@ -296,7 +714,6 @@
"cpu": [
"arm64"
],
- "dev": true,
"license": "MIT",
"optional": true,
"os": [
@@ -313,7 +730,6 @@
"cpu": [
"x64"
],
- "dev": true,
"license": "MIT",
"optional": true,
"os": [
@@ -330,7 +746,6 @@
"cpu": [
"arm64"
],
- "dev": true,
"license": "MIT",
"optional": true,
"os": [
@@ -347,7 +762,6 @@
"cpu": [
"x64"
],
- "dev": true,
"license": "MIT",
"optional": true,
"os": [
@@ -364,7 +778,6 @@
"cpu": [
"arm"
],
- "dev": true,
"license": "MIT",
"optional": true,
"os": [
@@ -381,7 +794,6 @@
"cpu": [
"arm64"
],
- "dev": true,
"license": "MIT",
"optional": true,
"os": [
@@ -398,7 +810,6 @@
"cpu": [
"ia32"
],
- "dev": true,
"license": "MIT",
"optional": true,
"os": [
@@ -415,7 +826,6 @@
"cpu": [
"loong64"
],
- "dev": true,
"license": "MIT",
"optional": true,
"os": [
@@ -432,7 +842,6 @@
"cpu": [
"mips64el"
],
- "dev": true,
"license": "MIT",
"optional": true,
"os": [
@@ -449,7 +858,6 @@
"cpu": [
"ppc64"
],
- "dev": true,
"license": "MIT",
"optional": true,
"os": [
@@ -466,7 +874,6 @@
"cpu": [
"riscv64"
],
- "dev": true,
"license": "MIT",
"optional": true,
"os": [
@@ -483,7 +890,6 @@
"cpu": [
"s390x"
],
- "dev": true,
"license": "MIT",
"optional": true,
"os": [
@@ -500,7 +906,6 @@
"cpu": [
"x64"
],
- "dev": true,
"license": "MIT",
"optional": true,
"os": [
@@ -517,7 +922,6 @@
"cpu": [
"arm64"
],
- "dev": true,
"license": "MIT",
"optional": true,
"os": [
@@ -534,7 +938,6 @@
"cpu": [
"x64"
],
- "dev": true,
"license": "MIT",
"optional": true,
"os": [
@@ -551,7 +954,6 @@
"cpu": [
"arm64"
],
- "dev": true,
"license": "MIT",
"optional": true,
"os": [
@@ -568,7 +970,6 @@
"cpu": [
"x64"
],
- "dev": true,
"license": "MIT",
"optional": true,
"os": [
@@ -585,7 +986,6 @@
"cpu": [
"x64"
],
- "dev": true,
"license": "MIT",
"optional": true,
"os": [
@@ -602,7 +1002,6 @@
"cpu": [
"arm64"
],
- "dev": true,
"license": "MIT",
"optional": true,
"os": [
@@ -619,7 +1018,6 @@
"cpu": [
"ia32"
],
- "dev": true,
"license": "MIT",
"optional": true,
"os": [
@@ -636,7 +1034,6 @@
"cpu": [
"x64"
],
- "dev": true,
"license": "MIT",
"optional": true,
"os": [
@@ -1092,25 +1489,195 @@
"@lezer/common": "^1.0.0"
}
},
- "node_modules/@lezer/lr": {
- "version": "1.4.2",
- "resolved": "https://registry.npmjs.org/@lezer/lr/-/lr-1.4.2.tgz",
- "integrity": "sha512-pu0K1jCIdnQ12aWNaAVU5bzi7Bd1w54J3ECgANPmYLtQKP0HBj2cE/5coBD66MT10xbtIuUr7tg0Shbsvk0mDA==",
+ "node_modules/@lezer/lr": {
+ "version": "1.4.2",
+ "resolved": "https://registry.npmjs.org/@lezer/lr/-/lr-1.4.2.tgz",
+ "integrity": "sha512-pu0K1jCIdnQ12aWNaAVU5bzi7Bd1w54J3ECgANPmYLtQKP0HBj2cE/5coBD66MT10xbtIuUr7tg0Shbsvk0mDA==",
+ "license": "MIT",
+ "dependencies": {
+ "@lezer/common": "^1.0.0"
+ }
+ },
+ "node_modules/@lezer/yaml": {
+ "version": "1.0.3",
+ "resolved": "https://registry.npmjs.org/@lezer/yaml/-/yaml-1.0.3.tgz",
+ "integrity": "sha512-GuBLekbw9jDBDhGur82nuwkxKQ+a3W5H0GfaAthDXcAu+XdpS43VlnxA9E9hllkpSP5ellRDKjLLj7Lu9Wr6xA==",
+ "license": "MIT",
+ "dependencies": {
+ "@lezer/common": "^1.2.0",
+ "@lezer/highlight": "^1.0.0",
+ "@lezer/lr": "^1.4.0"
+ }
+ },
+ "node_modules/@libsql/client": {
+ "version": "0.15.8",
+ "resolved": "https://registry.npmjs.org/@libsql/client/-/client-0.15.8.tgz",
+ "integrity": "sha512-TskygwF+ToZeWhPPT0WennyGrP3tmkKraaKopT2YwUjqD6DWDRm6SG5iy0VqnaO+HC9FNBCDX0oQPODU3gqqPQ==",
+ "license": "MIT",
+ "dependencies": {
+ "@libsql/core": "^0.15.8",
+ "@libsql/hrana-client": "^0.7.0",
+ "js-base64": "^3.7.5",
+ "libsql": "^0.5.12",
+ "promise-limit": "^2.7.0"
+ }
+ },
+ "node_modules/@libsql/core": {
+ "version": "0.15.8",
+ "resolved": "https://registry.npmjs.org/@libsql/core/-/core-0.15.8.tgz",
+ "integrity": "sha512-oX2fQqDbZkaBUvFMGvJq1Jh+mVzJrgNbEwK6Wzvp91z3uMe9iaIIXgO8yxB72RpUf7BqzjiKHjEiRXytfTdbUw==",
+ "license": "MIT",
+ "dependencies": {
+ "js-base64": "^3.7.5"
+ }
+ },
+ "node_modules/@libsql/darwin-arm64": {
+ "version": "0.5.12",
+ "resolved": "https://registry.npmjs.org/@libsql/darwin-arm64/-/darwin-arm64-0.5.12.tgz",
+ "integrity": "sha512-RDA87qaCWPE+uJWY91A0Is8KU9x43xDWMH8VNlj330CLFT9rC6jDypqadg0mzu1FEL2leG6BRdX6EcfM6NM3pw==",
+ "cpu": [
+ "arm64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "darwin"
+ ]
+ },
+ "node_modules/@libsql/darwin-x64": {
+ "version": "0.5.12",
+ "resolved": "https://registry.npmjs.org/@libsql/darwin-x64/-/darwin-x64-0.5.12.tgz",
+ "integrity": "sha512-6Ufip8uxQkLOFCsGd07miDt1w+Rx5VIJdDI6mSRBlVaEXWuWx1R8D7gfeCS0k73vDd+oh39pYF/R8nVFkwiOcg==",
+ "cpu": [
+ "x64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "darwin"
+ ]
+ },
+ "node_modules/@libsql/hrana-client": {
+ "version": "0.7.0",
+ "resolved": "https://registry.npmjs.org/@libsql/hrana-client/-/hrana-client-0.7.0.tgz",
+ "integrity": "sha512-OF8fFQSkbL7vJY9rfuegK1R7sPgQ6kFMkDamiEccNUvieQ+3urzfDFI616oPl8V7T9zRmnTkSjMOImYCAVRVuw==",
+ "license": "MIT",
+ "dependencies": {
+ "@libsql/isomorphic-fetch": "^0.3.1",
+ "@libsql/isomorphic-ws": "^0.1.5",
+ "js-base64": "^3.7.5",
+ "node-fetch": "^3.3.2"
+ }
+ },
+ "node_modules/@libsql/isomorphic-fetch": {
+ "version": "0.3.1",
+ "resolved": "https://registry.npmjs.org/@libsql/isomorphic-fetch/-/isomorphic-fetch-0.3.1.tgz",
+ "integrity": "sha512-6kK3SUK5Uu56zPq/Las620n5aS9xJq+jMBcNSOmjhNf/MUvdyji4vrMTqD7ptY7/4/CAVEAYDeotUz60LNQHtw==",
+ "license": "MIT",
+ "engines": {
+ "node": ">=18.0.0"
+ }
+ },
+ "node_modules/@libsql/isomorphic-ws": {
+ "version": "0.1.5",
+ "resolved": "https://registry.npmjs.org/@libsql/isomorphic-ws/-/isomorphic-ws-0.1.5.tgz",
+ "integrity": "sha512-DtLWIH29onUYR00i0GlQ3UdcTRC6EP4u9w/h9LxpUZJWRMARk6dQwZ6Jkd+QdwVpuAOrdxt18v0K2uIYR3fwFg==",
+ "license": "MIT",
+ "dependencies": {
+ "@types/ws": "^8.5.4",
+ "ws": "^8.13.0"
+ }
+ },
+ "node_modules/@libsql/linux-arm-gnueabihf": {
+ "version": "0.5.12",
+ "resolved": "https://registry.npmjs.org/@libsql/linux-arm-gnueabihf/-/linux-arm-gnueabihf-0.5.12.tgz",
+ "integrity": "sha512-I+4K++7byiOjYJNRGcrCBlIXjP6MwUta2GxPGZMoH2kAv6AintO4dvritu6vDe9LyRPXTjRJl30k+ThXR0RWxQ==",
+ "cpu": [
+ "arm"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "linux"
+ ]
+ },
+ "node_modules/@libsql/linux-arm-musleabihf": {
+ "version": "0.5.12",
+ "resolved": "https://registry.npmjs.org/@libsql/linux-arm-musleabihf/-/linux-arm-musleabihf-0.5.12.tgz",
+ "integrity": "sha512-87C3A5ozNEdOnI5VZq9lHpPJ3Ncl3p+qB5roDluVKflx9kKH1hEc8MpwkvU3qeIWppvgHowXlO9CfS572V10OA==",
+ "cpu": [
+ "arm"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "linux"
+ ]
+ },
+ "node_modules/@libsql/linux-arm64-gnu": {
+ "version": "0.5.12",
+ "resolved": "https://registry.npmjs.org/@libsql/linux-arm64-gnu/-/linux-arm64-gnu-0.5.12.tgz",
+ "integrity": "sha512-0jpcuD7nHGD4XVTbId44SaY/JcqURKpxkXUcW4FsQ1qmkG5PsUy5l2Ob29P8HwGRBhDBomFWA4PvwJGrP/2pMA==",
+ "cpu": [
+ "arm64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "linux"
+ ]
+ },
+ "node_modules/@libsql/linux-arm64-musl": {
+ "version": "0.5.12",
+ "resolved": "https://registry.npmjs.org/@libsql/linux-arm64-musl/-/linux-arm64-musl-0.5.12.tgz",
+ "integrity": "sha512-ic5bHp9OTCNgmvqojqkxWfuPm4Y8CKfpUe/AqmXrstzqlE9EKg1qD9KZIjso2g4pX15KPWJGPSd29OXxHkzsog==",
+ "cpu": [
+ "arm64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "linux"
+ ]
+ },
+ "node_modules/@libsql/linux-x64-gnu": {
+ "version": "0.5.12",
+ "resolved": "https://registry.npmjs.org/@libsql/linux-x64-gnu/-/linux-x64-gnu-0.5.12.tgz",
+ "integrity": "sha512-9zDtahCw2q0WJ54c/0vq142JtzI16OB8/U0bVCrpxF9DmLFyKBrAtEvoYdvKtFmvcvNn7YA5LEytr2g2q+xl1g==",
+ "cpu": [
+ "x64"
+ ],
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "linux"
+ ]
+ },
+ "node_modules/@libsql/linux-x64-musl": {
+ "version": "0.5.12",
+ "resolved": "https://registry.npmjs.org/@libsql/linux-x64-musl/-/linux-x64-musl-0.5.12.tgz",
+ "integrity": "sha512-+fisSpE+2yK1N88shPtB7bEB8d+fF3CQmy1KnbJ4Oned8cw5uBfU46A1ICG+49RFaKxmoIQimHVAxfGV9NFQ3w==",
+ "cpu": [
+ "x64"
+ ],
"license": "MIT",
- "dependencies": {
- "@lezer/common": "^1.0.0"
- }
+ "optional": true,
+ "os": [
+ "linux"
+ ]
},
- "node_modules/@lezer/yaml": {
- "version": "1.0.3",
- "resolved": "https://registry.npmjs.org/@lezer/yaml/-/yaml-1.0.3.tgz",
- "integrity": "sha512-GuBLekbw9jDBDhGur82nuwkxKQ+a3W5H0GfaAthDXcAu+XdpS43VlnxA9E9hllkpSP5ellRDKjLLj7Lu9Wr6xA==",
+ "node_modules/@libsql/win32-x64-msvc": {
+ "version": "0.5.12",
+ "resolved": "https://registry.npmjs.org/@libsql/win32-x64-msvc/-/win32-x64-msvc-0.5.12.tgz",
+ "integrity": "sha512-upNJCcgMgpAFXlL//rRVwlPgMT8uG1LoilHgCEpAp+GEjgBjoDgGW6iOkktuJC8paZh5kt9dCPh3r3jF3HWQjg==",
+ "cpu": [
+ "x64"
+ ],
"license": "MIT",
- "dependencies": {
- "@lezer/common": "^1.2.0",
- "@lezer/highlight": "^1.0.0",
- "@lezer/lr": "^1.4.0"
- }
+ "optional": true,
+ "os": [
+ "win32"
+ ]
},
"node_modules/@lucide/svelte": {
"version": "0.511.0",
@@ -1128,6 +1695,12 @@
"integrity": "sha512-l0h88YhZFyKdXIFNfSWpyjStDjGHwZ/U7iobcK1cQQD8sejsONdQtTVU+1wVN1PBw40PiiHB1vA5S7VTfQiP9g==",
"license": "MIT"
},
+ "node_modules/@neon-rs/load": {
+ "version": "0.0.4",
+ "resolved": "https://registry.npmjs.org/@neon-rs/load/-/load-0.0.4.tgz",
+ "integrity": "sha512-kTPhdZyTQxB+2wpiRcFWrDcejc4JI6tkPuS7UZCG4l6Zvc5kU/gGQ/ozvHTh1XR5tS+UlfAfGuPajjzQjCiHCw==",
+ "license": "MIT"
+ },
"node_modules/@noble/ciphers": {
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/@noble/ciphers/-/ciphers-0.2.0.tgz",
@@ -2340,6 +2913,15 @@
"license": "MIT",
"optional": true
},
+ "node_modules/@types/ws": {
+ "version": "8.18.1",
+ "resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.18.1.tgz",
+ "integrity": "sha512-ThVF6DCVhA8kUGy+aazFQ4kXQ7E1Ty7A3ypFOe0IcJV8O/M511G99AW24irKrW56Wt44yG9+ij8FaqoBGkuBXg==",
+ "license": "MIT",
+ "dependencies": {
+ "@types/node": "*"
+ }
+ },
"node_modules/@typeschema/class-validator": {
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/@typeschema/class-validator/-/class-validator-0.3.0.tgz",
@@ -2953,9 +3535,7 @@
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz",
"integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==",
- "dev": true,
- "license": "MIT",
- "optional": true
+ "license": "MIT"
},
"node_modules/buildcheck": {
"version": "0.0.6",
@@ -3206,6 +3786,15 @@
"node": ">=4"
}
},
+ "node_modules/data-uri-to-buffer": {
+ "version": "4.0.1",
+ "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz",
+ "integrity": "sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==",
+ "license": "MIT",
+ "engines": {
+ "node": ">= 12"
+ }
+ },
"node_modules/date-fns": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/date-fns/-/date-fns-4.1.0.tgz",
@@ -3336,6 +3925,158 @@
"tar-fs": "^2.1.1"
}
},
+ "node_modules/dotenv": {
+ "version": "16.5.0",
+ "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.5.0.tgz",
+ "integrity": "sha512-m/C+AwOAr9/W1UOIZUo232ejMNnJAJtYQjUbHoNTBNTJSvqzzDh7vnrei3o3r3m9blf6ZoDkvcw0VmozNRFJxg==",
+ "license": "BSD-2-Clause",
+ "engines": {
+ "node": ">=12"
+ },
+ "funding": {
+ "url": "https://dotenvx.com"
+ }
+ },
+ "node_modules/drizzle-kit": {
+ "version": "0.31.1",
+ "resolved": "https://registry.npmjs.org/drizzle-kit/-/drizzle-kit-0.31.1.tgz",
+ "integrity": "sha512-PUjYKWtzOzPtdtQlTHQG3qfv4Y0XT8+Eas6UbxCmxTj7qgMf+39dDujf1BP1I+qqZtw9uzwTh8jYtkMuCq+B0Q==",
+ "license": "MIT",
+ "dependencies": {
+ "@drizzle-team/brocli": "^0.10.2",
+ "@esbuild-kit/esm-loader": "^2.5.5",
+ "esbuild": "^0.25.2",
+ "esbuild-register": "^3.5.0"
+ },
+ "bin": {
+ "drizzle-kit": "bin.cjs"
+ }
+ },
+ "node_modules/drizzle-orm": {
+ "version": "0.44.1",
+ "resolved": "https://registry.npmjs.org/drizzle-orm/-/drizzle-orm-0.44.1.tgz",
+ "integrity": "sha512-prIWOlwJbiYInvcJxE+IMiJCtMiFVrSUJCwx6AXSJvGOdLu35qZ46QncTZDgloiLNCG0XxTC8agQElSmsl++TA==",
+ "license": "Apache-2.0",
+ "peerDependencies": {
+ "@aws-sdk/client-rds-data": ">=3",
+ "@cloudflare/workers-types": ">=4",
+ "@electric-sql/pglite": ">=0.2.0",
+ "@libsql/client": ">=0.10.0",
+ "@libsql/client-wasm": ">=0.10.0",
+ "@neondatabase/serverless": ">=0.10.0",
+ "@op-engineering/op-sqlite": ">=2",
+ "@opentelemetry/api": "^1.4.1",
+ "@planetscale/database": ">=1.13",
+ "@prisma/client": "*",
+ "@tidbcloud/serverless": "*",
+ "@types/better-sqlite3": "*",
+ "@types/pg": "*",
+ "@types/sql.js": "*",
+ "@upstash/redis": ">=1.34.7",
+ "@vercel/postgres": ">=0.8.0",
+ "@xata.io/client": "*",
+ "better-sqlite3": ">=7",
+ "bun-types": "*",
+ "expo-sqlite": ">=14.0.0",
+ "gel": ">=2",
+ "knex": "*",
+ "kysely": "*",
+ "mysql2": ">=2",
+ "pg": ">=8",
+ "postgres": ">=3",
+ "sql.js": ">=1",
+ "sqlite3": ">=5"
+ },
+ "peerDependenciesMeta": {
+ "@aws-sdk/client-rds-data": {
+ "optional": true
+ },
+ "@cloudflare/workers-types": {
+ "optional": true
+ },
+ "@electric-sql/pglite": {
+ "optional": true
+ },
+ "@libsql/client": {
+ "optional": true
+ },
+ "@libsql/client-wasm": {
+ "optional": true
+ },
+ "@neondatabase/serverless": {
+ "optional": true
+ },
+ "@op-engineering/op-sqlite": {
+ "optional": true
+ },
+ "@opentelemetry/api": {
+ "optional": true
+ },
+ "@planetscale/database": {
+ "optional": true
+ },
+ "@prisma/client": {
+ "optional": true
+ },
+ "@tidbcloud/serverless": {
+ "optional": true
+ },
+ "@types/better-sqlite3": {
+ "optional": true
+ },
+ "@types/pg": {
+ "optional": true
+ },
+ "@types/sql.js": {
+ "optional": true
+ },
+ "@upstash/redis": {
+ "optional": true
+ },
+ "@vercel/postgres": {
+ "optional": true
+ },
+ "@xata.io/client": {
+ "optional": true
+ },
+ "better-sqlite3": {
+ "optional": true
+ },
+ "bun-types": {
+ "optional": true
+ },
+ "expo-sqlite": {
+ "optional": true
+ },
+ "gel": {
+ "optional": true
+ },
+ "knex": {
+ "optional": true
+ },
+ "kysely": {
+ "optional": true
+ },
+ "mysql2": {
+ "optional": true
+ },
+ "pg": {
+ "optional": true
+ },
+ "postgres": {
+ "optional": true
+ },
+ "prisma": {
+ "optional": true
+ },
+ "sql.js": {
+ "optional": true
+ },
+ "sqlite3": {
+ "optional": true
+ }
+ }
+ },
"node_modules/dunder-proto": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz",
@@ -3440,7 +4181,6 @@
"version": "0.25.4",
"resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.25.4.tgz",
"integrity": "sha512-8pgjLUcUjcgDg+2Q4NYXnPbo/vncAY4UmyaCm0jZevERqCHZIaWwdJHkf8XQtu4AxSKCdvrUbT0XUr1IdZzI8Q==",
- "dev": true,
"hasInstallScript": true,
"license": "MIT",
"bin": {
@@ -3477,6 +4217,18 @@
"@esbuild/win32-x64": "0.25.4"
}
},
+ "node_modules/esbuild-register": {
+ "version": "3.6.0",
+ "resolved": "https://registry.npmjs.org/esbuild-register/-/esbuild-register-3.6.0.tgz",
+ "integrity": "sha512-H2/S7Pm8a9CL1uhp9OvjwrBh5Pvx0H8qVOxNu8Wed9Y7qv56MPtq+GGM8RJpq6glYJn9Wspr8uw7l55uyinNeg==",
+ "license": "MIT",
+ "dependencies": {
+ "debug": "^4.3.4"
+ },
+ "peerDependencies": {
+ "esbuild": ">=0.12 <1"
+ }
+ },
"node_modules/esbuild-runner": {
"version": "2.2.2",
"resolved": "https://registry.npmjs.org/esbuild-runner/-/esbuild-runner-2.2.2.tgz",
@@ -3836,6 +4588,29 @@
}
}
},
+ "node_modules/fetch-blob": {
+ "version": "3.2.0",
+ "resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.2.0.tgz",
+ "integrity": "sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==",
+ "funding": [
+ {
+ "type": "github",
+ "url": "https://github.com/sponsors/jimmywarting"
+ },
+ {
+ "type": "paypal",
+ "url": "https://paypal.me/jimmywarting"
+ }
+ ],
+ "license": "MIT",
+ "dependencies": {
+ "node-domexception": "^1.0.0",
+ "web-streams-polyfill": "^3.0.3"
+ },
+ "engines": {
+ "node": "^12.20 || >= 14.13"
+ }
+ },
"node_modules/file-entry-cache": {
"version": "8.0.0",
"resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-8.0.0.tgz",
@@ -3935,6 +4710,18 @@
"node": ">= 6"
}
},
+ "node_modules/formdata-polyfill": {
+ "version": "4.0.10",
+ "resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz",
+ "integrity": "sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==",
+ "license": "MIT",
+ "dependencies": {
+ "fetch-blob": "^3.1.2"
+ },
+ "engines": {
+ "node": ">=12.20.0"
+ }
+ },
"node_modules/formsnap": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/formsnap/-/formsnap-2.0.1.tgz",
@@ -4052,6 +4839,18 @@
"node": ">= 0.4"
}
},
+ "node_modules/get-tsconfig": {
+ "version": "4.10.1",
+ "resolved": "https://registry.npmjs.org/get-tsconfig/-/get-tsconfig-4.10.1.tgz",
+ "integrity": "sha512-auHyJ4AgMz7vgS8Hp3N6HXSmlMdUyhSUrfBF16w153rxtLIEOE+HGqaBppczZvnHLqQJfiHotCYpNhl0lUROFQ==",
+ "license": "MIT",
+ "dependencies": {
+ "resolve-pkg-maps": "^1.0.0"
+ },
+ "funding": {
+ "url": "https://github.com/privatenumber/get-tsconfig?sponsor=1"
+ }
+ },
"node_modules/glob-parent": {
"version": "6.0.2",
"resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-6.0.2.tgz",
@@ -4329,6 +5128,12 @@
"@sideway/pinpoint": "^2.0.0"
}
},
+ "node_modules/js-base64": {
+ "version": "3.7.7",
+ "resolved": "https://registry.npmjs.org/js-base64/-/js-base64-3.7.7.tgz",
+ "integrity": "sha512-7rCnleh0z2CkXhH67J8K1Ytz0b2Y+yxTPL+/KOJoa20hfnVQ/3/T6W/KflYI4bRHRagNeXeU2bkNGI3v1oS/lw==",
+ "license": "BSD-3-Clause"
+ },
"node_modules/js-yaml": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz",
@@ -4426,6 +5231,47 @@
"license": "MIT",
"optional": true
},
+ "node_modules/libsql": {
+ "version": "0.5.12",
+ "resolved": "https://registry.npmjs.org/libsql/-/libsql-0.5.12.tgz",
+ "integrity": "sha512-TikiQZ1j4TwFEqVdJdTM9ZTti28is/ytGEvn0S2MocOj69UKQetWACe/qd8KAD5VeNnQSVd6Nlm2AJx0DFW9Ag==",
+ "cpu": [
+ "x64",
+ "arm64",
+ "wasm32",
+ "arm"
+ ],
+ "license": "MIT",
+ "os": [
+ "darwin",
+ "linux",
+ "win32"
+ ],
+ "dependencies": {
+ "@neon-rs/load": "^0.0.4",
+ "detect-libc": "2.0.2"
+ },
+ "optionalDependencies": {
+ "@libsql/darwin-arm64": "0.5.12",
+ "@libsql/darwin-x64": "0.5.12",
+ "@libsql/linux-arm-gnueabihf": "0.5.12",
+ "@libsql/linux-arm-musleabihf": "0.5.12",
+ "@libsql/linux-arm64-gnu": "0.5.12",
+ "@libsql/linux-arm64-musl": "0.5.12",
+ "@libsql/linux-x64-gnu": "0.5.12",
+ "@libsql/linux-x64-musl": "0.5.12",
+ "@libsql/win32-x64-msvc": "0.5.12"
+ }
+ },
+ "node_modules/libsql/node_modules/detect-libc": {
+ "version": "2.0.2",
+ "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.0.2.tgz",
+ "integrity": "sha512-UX6sGumvvqSaXgdKGUsgZWqcUyIXZ/vZTrlRT/iobiKhGL0zL4d3osHj3uqllWJK+i+sixDS/3COVEOFbupFyw==",
+ "license": "Apache-2.0",
+ "engines": {
+ "node": ">=8"
+ }
+ },
"node_modules/lightningcss": {
"version": "1.30.1",
"resolved": "https://registry.npmjs.org/lightningcss/-/lightningcss-1.30.1.tgz",
@@ -4951,6 +5797,44 @@
"dev": true,
"license": "MIT"
},
+ "node_modules/node-domexception": {
+ "version": "1.0.0",
+ "resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz",
+ "integrity": "sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==",
+ "deprecated": "Use your platform's native DOMException instead",
+ "funding": [
+ {
+ "type": "github",
+ "url": "https://github.com/sponsors/jimmywarting"
+ },
+ {
+ "type": "github",
+ "url": "https://paypal.me/jimmywarting"
+ }
+ ],
+ "license": "MIT",
+ "engines": {
+ "node": ">=10.5.0"
+ }
+ },
+ "node_modules/node-fetch": {
+ "version": "3.3.2",
+ "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.3.2.tgz",
+ "integrity": "sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA==",
+ "license": "MIT",
+ "dependencies": {
+ "data-uri-to-buffer": "^4.0.0",
+ "fetch-blob": "^3.1.4",
+ "formdata-polyfill": "^4.0.10"
+ },
+ "engines": {
+ "node": "^12.20.0 || ^14.13.1 || >=16.0.0"
+ },
+ "funding": {
+ "type": "opencollective",
+ "url": "https://opencollective.com/node-fetch"
+ }
+ },
"node_modules/normalize-url": {
"version": "8.0.1",
"resolved": "https://registry.npmjs.org/normalize-url/-/normalize-url-8.0.1.tgz",
@@ -5337,6 +6221,12 @@
"svelte": "^3.2.0 || ^4.0.0-next.0 || ^5.0.0-next.0"
}
},
+ "node_modules/promise-limit": {
+ "version": "2.7.0",
+ "resolved": "https://registry.npmjs.org/promise-limit/-/promise-limit-2.7.0.tgz",
+ "integrity": "sha512-7nJ6v5lnJsXwGprnGXga4wx6d1POjvi5Qmf1ivTRxTjH4Z/9Czja/UCMLVmB9N93GeWOU93XaFaEt6jbuoagNw==",
+ "license": "ISC"
+ },
"node_modules/proper-lockfile": {
"version": "4.1.2",
"resolved": "https://registry.npmjs.org/proper-lockfile/-/proper-lockfile-4.1.2.tgz",
@@ -5513,6 +6403,15 @@
"node": ">=4"
}
},
+ "node_modules/resolve-pkg-maps": {
+ "version": "1.0.0",
+ "resolved": "https://registry.npmjs.org/resolve-pkg-maps/-/resolve-pkg-maps-1.0.0.tgz",
+ "integrity": "sha512-seS2Tj26TBVOC2NIc2rOe2y2ZO7efxITtLZcGSOnHHNOQ7CkiUBfw0Iw2ck6xkIhPwLhKNLS8BO+hEpngQlqzw==",
+ "license": "MIT",
+ "funding": {
+ "url": "https://github.com/privatenumber/resolve-pkg-maps?sponsor=1"
+ }
+ },
"node_modules/retry": {
"version": "0.12.0",
"resolved": "https://registry.npmjs.org/retry/-/retry-0.12.0.tgz",
@@ -5729,9 +6628,7 @@
"version": "0.6.1",
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
"integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==",
- "dev": true,
"license": "BSD-3-Clause",
- "optional": true,
"engines": {
"node": ">=0.10.0"
}
@@ -5750,9 +6647,7 @@
"version": "0.5.21",
"resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.21.tgz",
"integrity": "sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w==",
- "dev": true,
"license": "MIT",
- "optional": true,
"dependencies": {
"buffer-from": "^1.0.0",
"source-map": "^0.6.0"
@@ -6372,6 +7267,41 @@
"dev": true,
"license": "0BSD"
},
+ "node_modules/tsx": {
+ "version": "4.19.4",
+ "resolved": "https://registry.npmjs.org/tsx/-/tsx-4.19.4.tgz",
+ "integrity": "sha512-gK5GVzDkJK1SI1zwHf32Mqxf2tSJkNx+eYcNly5+nHvWqXUJYUkWBQtKauoESz3ymezAI++ZwT855x5p5eop+Q==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "esbuild": "~0.25.0",
+ "get-tsconfig": "^4.7.5"
+ },
+ "bin": {
+ "tsx": "dist/cli.mjs"
+ },
+ "engines": {
+ "node": ">=18.0.0"
+ },
+ "optionalDependencies": {
+ "fsevents": "~2.3.3"
+ }
+ },
+ "node_modules/tsx/node_modules/fsevents": {
+ "version": "2.3.3",
+ "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz",
+ "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==",
+ "dev": true,
+ "hasInstallScript": true,
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "darwin"
+ ],
+ "engines": {
+ "node": "^8.16.0 || ^10.6.0 || >=11.0.0"
+ }
+ },
"node_modules/tw-animate-css": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/tw-animate-css/-/tw-animate-css-1.3.0.tgz",
@@ -6629,6 +7559,15 @@
"integrity": "sha512-dpojBhNsCNN7T82Tm7k26A6G9ML3NkhDsnw9n/eoxSRlVBB4CEtIQ/KTCLI2Fwf3ataSXRhYFkQi3SlnFwPvPQ==",
"license": "MIT"
},
+ "node_modules/web-streams-polyfill": {
+ "version": "3.3.3",
+ "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.3.3.tgz",
+ "integrity": "sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw==",
+ "license": "MIT",
+ "engines": {
+ "node": ">= 8"
+ }
+ },
"node_modules/which": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz",
@@ -6678,6 +7617,27 @@
"integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==",
"license": "ISC"
},
+ "node_modules/ws": {
+ "version": "8.18.2",
+ "resolved": "https://registry.npmjs.org/ws/-/ws-8.18.2.tgz",
+ "integrity": "sha512-DMricUmwGZUVr++AEAe2uiVM7UoO9MAVZMDu05UQOaUII0lp+zOzLLU4Xqh/JvTqklB1T4uELaaPBKyjE1r4fQ==",
+ "license": "MIT",
+ "engines": {
+ "node": ">=10.0.0"
+ },
+ "peerDependencies": {
+ "bufferutil": "^4.0.1",
+ "utf-8-validate": ">=5.0.2"
+ },
+ "peerDependenciesMeta": {
+ "bufferutil": {
+ "optional": true
+ },
+ "utf-8-validate": {
+ "optional": true
+ }
+ }
+ },
"node_modules/y18n": {
"version": "5.0.8",
"resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz",
diff --git a/package.json b/package.json
index 5ceb8e3a0..25654411f 100644
--- a/package.json
+++ b/package.json
@@ -49,6 +49,7 @@
"tailwind-merge": "^3.3.0",
"tailwind-variants": "^1.0.0",
"tailwindcss": "^4.1.7",
+ "tsx": "^4.19.4",
"tw-animate-css": "^1.3.0",
"typescript": "^5.8.0",
"typescript-eslint": "^8.33.0",
@@ -58,6 +59,7 @@
"dependencies": {
"@codemirror/lang-yaml": "^6.1.2",
"@codemirror/legacy-modes": "^6.5.1",
+ "@libsql/client": "^0.15.8",
"@swimlane/docker-reference": "^2.0.1",
"@tanstack/table-core": "^8.21.3",
"arctic": "^3.7.0",
@@ -67,6 +69,9 @@
"date-fns": "^4.1.0",
"dockerode": "^4.0.6",
"dockerode-compose": "^1.4.0",
+ "drizzle-kit": "^0.31.1",
+ "dotenv": "^16.5.0",
+ "drizzle-orm": "^0.44.1",
"js-yaml": "^4.1.0",
"nanoid": "^5.1.5",
"prettier": "^3.5.3",
diff --git a/scripts/docker/entrypoint.sh b/scripts/docker/entrypoint.sh
index 35bb721d7..419b4439a 100644
--- a/scripts/docker/entrypoint.sh
+++ b/scripts/docker/entrypoint.sh
@@ -97,4 +97,5 @@ mkdir -p "$DATA_DIR"
chown -R "${PUID}:${PGID}" "$DATA_DIR"
echo "Entrypoint: Setup complete. Executing command as user ${APP_USER} (UID: ${PUID}, GID: ${PGID})..."
+# exec su-exec "$APP_USER" "npx drizzle-kit push --force"
exec su-exec "$APP_USER" "$@"
diff --git a/src/db/index.ts b/src/db/index.ts
new file mode 100644
index 000000000..a480d5a26
--- /dev/null
+++ b/src/db/index.ts
@@ -0,0 +1,5 @@
+import 'dotenv/config';
+import { drizzle } from 'drizzle-orm/libsql';
+import { env } from '$env/dynamic/private';
+
+export const db = drizzle(env.DB_CONN_STRING || 'file:data/arcane.db');
diff --git a/src/db/migrate.ts b/src/db/migrate.ts
new file mode 100644
index 000000000..09a4dd345
--- /dev/null
+++ b/src/db/migrate.ts
@@ -0,0 +1,18 @@
+import { migrate } from 'drizzle-orm/libsql/migrator';
+import { db } from './index';
+
+export async function runMigrations() {
+ try {
+ console.log('Running database migrations...');
+ await migrate(db, { migrationsFolder: './src/db/migrations' });
+ console.log('Database migrations completed successfully');
+ } catch (error) {
+ if (error instanceof Error && (error.message.includes('already exists') || error.message.includes('SQLITE_ERROR'))) {
+ console.log('Database tables already exist, continuing...');
+ return; // Don't throw, just continue
+ }
+
+ console.error('Error running migrations:', error);
+ throw error;
+ }
+}
diff --git a/src/db/migrations/0000_short_wong.sql b/src/db/migrations/0000_short_wong.sql
new file mode 100644
index 000000000..dc61ab761
--- /dev/null
+++ b/src/db/migrations/0000_short_wong.sql
@@ -0,0 +1,18 @@
+CREATE TABLE `settings_table` (
+ `id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
+ `dockerHost` text NOT NULL,
+ `stacksDirectory` text NOT NULL,
+ `autoUpdate` integer DEFAULT false NOT NULL,
+ `autoUpdateInterval` integer DEFAULT 300 NOT NULL,
+ `pollingEnabled` integer DEFAULT true NOT NULL,
+ `pollingInterval` integer DEFAULT 5 NOT NULL,
+ `pruneMode` text,
+ `registryCredentials` text DEFAULT '[]' NOT NULL,
+ `templateRegistries` text DEFAULT '[]' NOT NULL,
+ `auth` text NOT NULL,
+ `onboarding` text,
+ `baseServerUrl` text,
+ `maturityThresholdDays` integer DEFAULT 30 NOT NULL,
+ `createdAt` integer DEFAULT (unixepoch()) NOT NULL,
+ `updatedAt` integer DEFAULT (unixepoch()) NOT NULL
+);
diff --git a/src/db/migrations/0001_omniscient_captain_universe.sql b/src/db/migrations/0001_omniscient_captain_universe.sql
new file mode 100644
index 000000000..424c93a02
--- /dev/null
+++ b/src/db/migrations/0001_omniscient_captain_universe.sql
@@ -0,0 +1,37 @@
+CREATE TABLE `stacks_table` (
+ `id` text PRIMARY KEY NOT NULL,
+ `name` text NOT NULL,
+ `dir_name` text,
+ `path` text NOT NULL,
+ `auto_update` integer DEFAULT false NOT NULL,
+ `is_external` integer DEFAULT false NOT NULL,
+ `is_legacy` integer DEFAULT false NOT NULL,
+ `is_remote` integer DEFAULT false NOT NULL,
+ `agent_id` text,
+ `agent_hostname` text,
+ `status` text DEFAULT 'unknown' NOT NULL,
+ `service_count` integer DEFAULT 0 NOT NULL,
+ `running_count` integer DEFAULT 0 NOT NULL,
+ `compose_content` text,
+ `env_content` text,
+ `last_polled` integer,
+ `created_at` integer DEFAULT (unixepoch()) NOT NULL,
+ `updated_at` integer DEFAULT (unixepoch()) NOT NULL
+);
+--> statement-breakpoint
+CREATE UNIQUE INDEX `stacks_table_name_unique` ON `stacks_table` (`name`);--> statement-breakpoint
+CREATE TABLE `users_table` (
+ `id` text PRIMARY KEY NOT NULL,
+ `username` text NOT NULL,
+ `password_hash` text,
+ `display_name` text,
+ `email` text,
+ `roles` text DEFAULT '[]' NOT NULL,
+ `require_password_change` integer DEFAULT false NOT NULL,
+ `oidc_subject_id` text,
+ `last_login` integer,
+ `created_at` integer DEFAULT (unixepoch()) NOT NULL,
+ `updated_at` integer DEFAULT (unixepoch()) NOT NULL
+);
+--> statement-breakpoint
+CREATE UNIQUE INDEX `users_table_username_unique` ON `users_table` (`username`);
\ No newline at end of file
diff --git a/src/db/migrations/0002_chunky_silverclaw.sql b/src/db/migrations/0002_chunky_silverclaw.sql
new file mode 100644
index 000000000..b63ae9e41
--- /dev/null
+++ b/src/db/migrations/0002_chunky_silverclaw.sql
@@ -0,0 +1,49 @@
+CREATE TABLE `agent_tasks_table` (
+ `id` text PRIMARY KEY NOT NULL,
+ `agent_id` text NOT NULL,
+ `type` text NOT NULL,
+ `payload` text NOT NULL,
+ `status` text DEFAULT 'pending' NOT NULL,
+ `result` text,
+ `error` text,
+ `started_at` integer,
+ `completed_at` integer,
+ `created_at` integer DEFAULT (unixepoch()) NOT NULL,
+ `updated_at` integer DEFAULT (unixepoch()) NOT NULL
+);
+--> statement-breakpoint
+CREATE TABLE `agent_tokens_table` (
+ `id` text PRIMARY KEY NOT NULL,
+ `agent_id` text NOT NULL,
+ `token` text NOT NULL,
+ `name` text,
+ `permissions` text DEFAULT '[]' NOT NULL,
+ `last_used` integer,
+ `expires_at` integer,
+ `is_active` integer DEFAULT true NOT NULL,
+ `created_at` integer DEFAULT (unixepoch()) NOT NULL,
+ `updated_at` integer DEFAULT (unixepoch()) NOT NULL
+);
+--> statement-breakpoint
+CREATE UNIQUE INDEX `agent_tokens_table_token_unique` ON `agent_tokens_table` (`token`);--> statement-breakpoint
+CREATE TABLE `agents_table` (
+ `id` text PRIMARY KEY NOT NULL,
+ `hostname` text NOT NULL,
+ `platform` text NOT NULL,
+ `version` text NOT NULL,
+ `capabilities` text DEFAULT '[]' NOT NULL,
+ `status` text DEFAULT 'offline' NOT NULL,
+ `last_seen` integer NOT NULL,
+ `registered_at` integer NOT NULL,
+ `container_count` integer,
+ `image_count` integer,
+ `stack_count` integer,
+ `network_count` integer,
+ `volume_count` integer,
+ `docker_version` text,
+ `docker_containers` integer,
+ `docker_images` integer,
+ `metadata` text,
+ `created_at` integer DEFAULT (unixepoch()) NOT NULL,
+ `updated_at` integer DEFAULT (unixepoch()) NOT NULL
+);
diff --git a/src/db/migrations/0003_typical_ogun.sql b/src/db/migrations/0003_typical_ogun.sql
new file mode 100644
index 000000000..5b802cd44
--- /dev/null
+++ b/src/db/migrations/0003_typical_ogun.sql
@@ -0,0 +1,33 @@
+CREATE TABLE `deployments_table` (
+ `id` text PRIMARY KEY NOT NULL,
+ `name` text NOT NULL,
+ `type` text NOT NULL,
+ `status` text DEFAULT 'pending' NOT NULL,
+ `agent_id` text NOT NULL,
+ `task_id` text,
+ `error` text,
+ `metadata` text,
+ `created_at` integer DEFAULT (unixepoch()) NOT NULL,
+ `updated_at` integer DEFAULT (unixepoch()) NOT NULL
+);
+--> statement-breakpoint
+PRAGMA foreign_keys=OFF;--> statement-breakpoint
+CREATE TABLE `__new_users_table` (
+ `id` text PRIMARY KEY NOT NULL,
+ `username` text NOT NULL,
+ `password_hash` text,
+ `display_name` text,
+ `email` text,
+ `roles` text DEFAULT '[]' NOT NULL,
+ `require_password_change` integer DEFAULT false NOT NULL,
+ `oidc_subject_id` text,
+ `last_login` integer,
+ `created_at` integer DEFAULT (unixepoch() NOT NULL,
+ `updated_at` integer DEFAULT (unixepoch()) NOT NULL
+);
+--> statement-breakpoint
+INSERT INTO `__new_users_table`("id", "username", "password_hash", "display_name", "email", "roles", "require_password_change", "oidc_subject_id", "last_login", "created_at", "updated_at") SELECT "id", "username", "password_hash", "display_name", "email", "roles", "require_password_change", "oidc_subject_id", "last_login", "created_at", "updated_at" FROM `users_table`;--> statement-breakpoint
+DROP TABLE `users_table`;--> statement-breakpoint
+ALTER TABLE `__new_users_table` RENAME TO `users_table`;--> statement-breakpoint
+PRAGMA foreign_keys=ON;--> statement-breakpoint
+CREATE UNIQUE INDEX `users_table_username_unique` ON `users_table` (`username`);
\ No newline at end of file
diff --git a/src/db/migrations/0004_deep_meteorite.sql b/src/db/migrations/0004_deep_meteorite.sql
new file mode 100644
index 000000000..9e564ddf9
--- /dev/null
+++ b/src/db/migrations/0004_deep_meteorite.sql
@@ -0,0 +1,20 @@
+PRAGMA foreign_keys=OFF;--> statement-breakpoint
+CREATE TABLE `__new_users_table` (
+ `id` text PRIMARY KEY NOT NULL,
+ `username` text NOT NULL,
+ `password_hash` text,
+ `display_name` text,
+ `email` text,
+ `roles` text DEFAULT '[]' NOT NULL,
+ `require_password_change` integer DEFAULT false NOT NULL,
+ `oidc_subject_id` text,
+ `last_login` integer,
+ `created_at` integer DEFAULT (unixepoch()) NOT NULL,
+ `updated_at` integer DEFAULT (unixepoch()) NOT NULL
+);
+--> statement-breakpoint
+INSERT INTO `__new_users_table`("id", "username", "password_hash", "display_name", "email", "roles", "require_password_change", "oidc_subject_id", "last_login", "created_at", "updated_at") SELECT "id", "username", "password_hash", "display_name", "email", "roles", "require_password_change", "oidc_subject_id", "last_login", "created_at", "updated_at" FROM `users_table`;--> statement-breakpoint
+DROP TABLE `users_table`;--> statement-breakpoint
+ALTER TABLE `__new_users_table` RENAME TO `users_table`;--> statement-breakpoint
+PRAGMA foreign_keys=ON;--> statement-breakpoint
+CREATE UNIQUE INDEX `users_table_username_unique` ON `users_table` (`username`);
\ No newline at end of file
diff --git a/src/db/migrations/meta/0000_snapshot.json b/src/db/migrations/meta/0000_snapshot.json
new file mode 100644
index 000000000..074fb3f95
--- /dev/null
+++ b/src/db/migrations/meta/0000_snapshot.json
@@ -0,0 +1,149 @@
+{
+ "version": "6",
+ "dialect": "sqlite",
+ "id": "886592d4-3e89-40b1-9c53-1c2118ebc812",
+ "prevId": "00000000-0000-0000-0000-000000000000",
+ "tables": {
+ "settings_table": {
+ "name": "settings_table",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "integer",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": true
+ },
+ "dockerHost": {
+ "name": "dockerHost",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "stacksDirectory": {
+ "name": "stacksDirectory",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "autoUpdate": {
+ "name": "autoUpdate",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "autoUpdateInterval": {
+ "name": "autoUpdateInterval",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 300
+ },
+ "pollingEnabled": {
+ "name": "pollingEnabled",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": true
+ },
+ "pollingInterval": {
+ "name": "pollingInterval",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 5
+ },
+ "pruneMode": {
+ "name": "pruneMode",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "registryCredentials": {
+ "name": "registryCredentials",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'[]'"
+ },
+ "templateRegistries": {
+ "name": "templateRegistries",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'[]'"
+ },
+ "auth": {
+ "name": "auth",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "onboarding": {
+ "name": "onboarding",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "baseServerUrl": {
+ "name": "baseServerUrl",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "maturityThresholdDays": {
+ "name": "maturityThresholdDays",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 30
+ },
+ "createdAt": {
+ "name": "createdAt",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ },
+ "updatedAt": {
+ "name": "updatedAt",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ }
+ },
+ "indexes": {},
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ }
+ },
+ "views": {},
+ "enums": {},
+ "_meta": {
+ "schemas": {},
+ "tables": {},
+ "columns": {}
+ },
+ "internal": {
+ "indexes": {}
+ }
+}
\ No newline at end of file
diff --git a/src/db/migrations/meta/0001_snapshot.json b/src/db/migrations/meta/0001_snapshot.json
new file mode 100644
index 000000000..cf67f6d01
--- /dev/null
+++ b/src/db/migrations/meta/0001_snapshot.json
@@ -0,0 +1,401 @@
+{
+ "version": "6",
+ "dialect": "sqlite",
+ "id": "d0d66cc1-04f5-4493-a5a9-1251ae62e657",
+ "prevId": "886592d4-3e89-40b1-9c53-1c2118ebc812",
+ "tables": {
+ "settings_table": {
+ "name": "settings_table",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "integer",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": true
+ },
+ "dockerHost": {
+ "name": "dockerHost",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "stacksDirectory": {
+ "name": "stacksDirectory",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "autoUpdate": {
+ "name": "autoUpdate",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "autoUpdateInterval": {
+ "name": "autoUpdateInterval",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 300
+ },
+ "pollingEnabled": {
+ "name": "pollingEnabled",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": true
+ },
+ "pollingInterval": {
+ "name": "pollingInterval",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 5
+ },
+ "pruneMode": {
+ "name": "pruneMode",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "registryCredentials": {
+ "name": "registryCredentials",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'[]'"
+ },
+ "templateRegistries": {
+ "name": "templateRegistries",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'[]'"
+ },
+ "auth": {
+ "name": "auth",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "onboarding": {
+ "name": "onboarding",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "baseServerUrl": {
+ "name": "baseServerUrl",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "maturityThresholdDays": {
+ "name": "maturityThresholdDays",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 30
+ },
+ "createdAt": {
+ "name": "createdAt",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ },
+ "updatedAt": {
+ "name": "updatedAt",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ }
+ },
+ "indexes": {},
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "stacks_table": {
+ "name": "stacks_table",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "text",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "name": {
+ "name": "name",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "dir_name": {
+ "name": "dir_name",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "path": {
+ "name": "path",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "auto_update": {
+ "name": "auto_update",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "is_external": {
+ "name": "is_external",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "is_legacy": {
+ "name": "is_legacy",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "is_remote": {
+ "name": "is_remote",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "agent_id": {
+ "name": "agent_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "agent_hostname": {
+ "name": "agent_hostname",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "status": {
+ "name": "status",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'unknown'"
+ },
+ "service_count": {
+ "name": "service_count",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 0
+ },
+ "running_count": {
+ "name": "running_count",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 0
+ },
+ "compose_content": {
+ "name": "compose_content",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "env_content": {
+ "name": "env_content",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "last_polled": {
+ "name": "last_polled",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ }
+ },
+ "indexes": {
+ "stacks_table_name_unique": {
+ "name": "stacks_table_name_unique",
+ "columns": [
+ "name"
+ ],
+ "isUnique": true
+ }
+ },
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "users_table": {
+ "name": "users_table",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "text",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "username": {
+ "name": "username",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "password_hash": {
+ "name": "password_hash",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "display_name": {
+ "name": "display_name",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "email": {
+ "name": "email",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "roles": {
+ "name": "roles",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'[]'"
+ },
+ "require_password_change": {
+ "name": "require_password_change",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "oidc_subject_id": {
+ "name": "oidc_subject_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "last_login": {
+ "name": "last_login",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ }
+ },
+ "indexes": {
+ "users_table_username_unique": {
+ "name": "users_table_username_unique",
+ "columns": [
+ "username"
+ ],
+ "isUnique": true
+ }
+ },
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ }
+ },
+ "views": {},
+ "enums": {},
+ "_meta": {
+ "schemas": {},
+ "tables": {},
+ "columns": {}
+ },
+ "internal": {
+ "indexes": {}
+ }
+}
\ No newline at end of file
diff --git a/src/db/migrations/meta/0002_snapshot.json b/src/db/migrations/meta/0002_snapshot.json
new file mode 100644
index 000000000..979aee696
--- /dev/null
+++ b/src/db/migrations/meta/0002_snapshot.json
@@ -0,0 +1,730 @@
+{
+ "version": "6",
+ "dialect": "sqlite",
+ "id": "bad85df9-7e36-4e23-9808-5b529c767377",
+ "prevId": "d0d66cc1-04f5-4493-a5a9-1251ae62e657",
+ "tables": {
+ "agent_tasks_table": {
+ "name": "agent_tasks_table",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "text",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "agent_id": {
+ "name": "agent_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "type": {
+ "name": "type",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "payload": {
+ "name": "payload",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "status": {
+ "name": "status",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'pending'"
+ },
+ "result": {
+ "name": "result",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "error": {
+ "name": "error",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "started_at": {
+ "name": "started_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "completed_at": {
+ "name": "completed_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ }
+ },
+ "indexes": {},
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "agent_tokens_table": {
+ "name": "agent_tokens_table",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "text",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "agent_id": {
+ "name": "agent_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "token": {
+ "name": "token",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "name": {
+ "name": "name",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "permissions": {
+ "name": "permissions",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'[]'"
+ },
+ "last_used": {
+ "name": "last_used",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "expires_at": {
+ "name": "expires_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "is_active": {
+ "name": "is_active",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": true
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ }
+ },
+ "indexes": {
+ "agent_tokens_table_token_unique": {
+ "name": "agent_tokens_table_token_unique",
+ "columns": [
+ "token"
+ ],
+ "isUnique": true
+ }
+ },
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "agents_table": {
+ "name": "agents_table",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "text",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "hostname": {
+ "name": "hostname",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "platform": {
+ "name": "platform",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "version": {
+ "name": "version",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "capabilities": {
+ "name": "capabilities",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'[]'"
+ },
+ "status": {
+ "name": "status",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'offline'"
+ },
+ "last_seen": {
+ "name": "last_seen",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "registered_at": {
+ "name": "registered_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "container_count": {
+ "name": "container_count",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "image_count": {
+ "name": "image_count",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "stack_count": {
+ "name": "stack_count",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "network_count": {
+ "name": "network_count",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "volume_count": {
+ "name": "volume_count",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "docker_version": {
+ "name": "docker_version",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "docker_containers": {
+ "name": "docker_containers",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "docker_images": {
+ "name": "docker_images",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "metadata": {
+ "name": "metadata",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ }
+ },
+ "indexes": {},
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "settings_table": {
+ "name": "settings_table",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "integer",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": true
+ },
+ "dockerHost": {
+ "name": "dockerHost",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "stacksDirectory": {
+ "name": "stacksDirectory",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "autoUpdate": {
+ "name": "autoUpdate",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "autoUpdateInterval": {
+ "name": "autoUpdateInterval",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 300
+ },
+ "pollingEnabled": {
+ "name": "pollingEnabled",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": true
+ },
+ "pollingInterval": {
+ "name": "pollingInterval",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 5
+ },
+ "pruneMode": {
+ "name": "pruneMode",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "registryCredentials": {
+ "name": "registryCredentials",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'[]'"
+ },
+ "templateRegistries": {
+ "name": "templateRegistries",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'[]'"
+ },
+ "auth": {
+ "name": "auth",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "onboarding": {
+ "name": "onboarding",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "baseServerUrl": {
+ "name": "baseServerUrl",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "maturityThresholdDays": {
+ "name": "maturityThresholdDays",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 30
+ },
+ "createdAt": {
+ "name": "createdAt",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ },
+ "updatedAt": {
+ "name": "updatedAt",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ }
+ },
+ "indexes": {},
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "stacks_table": {
+ "name": "stacks_table",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "text",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "name": {
+ "name": "name",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "dir_name": {
+ "name": "dir_name",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "path": {
+ "name": "path",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "auto_update": {
+ "name": "auto_update",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "is_external": {
+ "name": "is_external",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "is_legacy": {
+ "name": "is_legacy",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "is_remote": {
+ "name": "is_remote",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "agent_id": {
+ "name": "agent_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "agent_hostname": {
+ "name": "agent_hostname",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "status": {
+ "name": "status",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'unknown'"
+ },
+ "service_count": {
+ "name": "service_count",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 0
+ },
+ "running_count": {
+ "name": "running_count",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 0
+ },
+ "compose_content": {
+ "name": "compose_content",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "env_content": {
+ "name": "env_content",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "last_polled": {
+ "name": "last_polled",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ }
+ },
+ "indexes": {
+ "stacks_table_name_unique": {
+ "name": "stacks_table_name_unique",
+ "columns": [
+ "name"
+ ],
+ "isUnique": true
+ }
+ },
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "users_table": {
+ "name": "users_table",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "text",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "username": {
+ "name": "username",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "password_hash": {
+ "name": "password_hash",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "display_name": {
+ "name": "display_name",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "email": {
+ "name": "email",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "roles": {
+ "name": "roles",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'[]'"
+ },
+ "require_password_change": {
+ "name": "require_password_change",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "oidc_subject_id": {
+ "name": "oidc_subject_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "last_login": {
+ "name": "last_login",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ }
+ },
+ "indexes": {
+ "users_table_username_unique": {
+ "name": "users_table_username_unique",
+ "columns": [
+ "username"
+ ],
+ "isUnique": true
+ }
+ },
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ }
+ },
+ "views": {},
+ "enums": {},
+ "_meta": {
+ "schemas": {},
+ "tables": {},
+ "columns": {}
+ },
+ "internal": {
+ "indexes": {}
+ }
+}
\ No newline at end of file
diff --git a/src/db/migrations/meta/0003_snapshot.json b/src/db/migrations/meta/0003_snapshot.json
new file mode 100644
index 000000000..0d275ceba
--- /dev/null
+++ b/src/db/migrations/meta/0003_snapshot.json
@@ -0,0 +1,813 @@
+{
+ "version": "6",
+ "dialect": "sqlite",
+ "id": "556da5cd-ba12-4614-879d-c91a31ad4a41",
+ "prevId": "bad85df9-7e36-4e23-9808-5b529c767377",
+ "tables": {
+ "agent_tasks_table": {
+ "name": "agent_tasks_table",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "text",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "agent_id": {
+ "name": "agent_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "type": {
+ "name": "type",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "payload": {
+ "name": "payload",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "status": {
+ "name": "status",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'pending'"
+ },
+ "result": {
+ "name": "result",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "error": {
+ "name": "error",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "started_at": {
+ "name": "started_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "completed_at": {
+ "name": "completed_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ }
+ },
+ "indexes": {},
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "agent_tokens_table": {
+ "name": "agent_tokens_table",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "text",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "agent_id": {
+ "name": "agent_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "token": {
+ "name": "token",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "name": {
+ "name": "name",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "permissions": {
+ "name": "permissions",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'[]'"
+ },
+ "last_used": {
+ "name": "last_used",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "expires_at": {
+ "name": "expires_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "is_active": {
+ "name": "is_active",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": true
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ }
+ },
+ "indexes": {
+ "agent_tokens_table_token_unique": {
+ "name": "agent_tokens_table_token_unique",
+ "columns": [
+ "token"
+ ],
+ "isUnique": true
+ }
+ },
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "agents_table": {
+ "name": "agents_table",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "text",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "hostname": {
+ "name": "hostname",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "platform": {
+ "name": "platform",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "version": {
+ "name": "version",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "capabilities": {
+ "name": "capabilities",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'[]'"
+ },
+ "status": {
+ "name": "status",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'offline'"
+ },
+ "last_seen": {
+ "name": "last_seen",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "registered_at": {
+ "name": "registered_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "container_count": {
+ "name": "container_count",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "image_count": {
+ "name": "image_count",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "stack_count": {
+ "name": "stack_count",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "network_count": {
+ "name": "network_count",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "volume_count": {
+ "name": "volume_count",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "docker_version": {
+ "name": "docker_version",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "docker_containers": {
+ "name": "docker_containers",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "docker_images": {
+ "name": "docker_images",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "metadata": {
+ "name": "metadata",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ }
+ },
+ "indexes": {},
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "deployments_table": {
+ "name": "deployments_table",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "text",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "name": {
+ "name": "name",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "type": {
+ "name": "type",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "status": {
+ "name": "status",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'pending'"
+ },
+ "agent_id": {
+ "name": "agent_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "task_id": {
+ "name": "task_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "error": {
+ "name": "error",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "metadata": {
+ "name": "metadata",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ }
+ },
+ "indexes": {},
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "settings_table": {
+ "name": "settings_table",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "integer",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": true
+ },
+ "dockerHost": {
+ "name": "dockerHost",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "stacksDirectory": {
+ "name": "stacksDirectory",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "autoUpdate": {
+ "name": "autoUpdate",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "autoUpdateInterval": {
+ "name": "autoUpdateInterval",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 300
+ },
+ "pollingEnabled": {
+ "name": "pollingEnabled",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": true
+ },
+ "pollingInterval": {
+ "name": "pollingInterval",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 5
+ },
+ "pruneMode": {
+ "name": "pruneMode",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "registryCredentials": {
+ "name": "registryCredentials",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'[]'"
+ },
+ "templateRegistries": {
+ "name": "templateRegistries",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'[]'"
+ },
+ "auth": {
+ "name": "auth",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "onboarding": {
+ "name": "onboarding",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "baseServerUrl": {
+ "name": "baseServerUrl",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "maturityThresholdDays": {
+ "name": "maturityThresholdDays",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 30
+ },
+ "createdAt": {
+ "name": "createdAt",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ },
+ "updatedAt": {
+ "name": "updatedAt",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ }
+ },
+ "indexes": {},
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "stacks_table": {
+ "name": "stacks_table",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "text",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "name": {
+ "name": "name",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "dir_name": {
+ "name": "dir_name",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "path": {
+ "name": "path",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "auto_update": {
+ "name": "auto_update",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "is_external": {
+ "name": "is_external",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "is_legacy": {
+ "name": "is_legacy",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "is_remote": {
+ "name": "is_remote",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "agent_id": {
+ "name": "agent_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "agent_hostname": {
+ "name": "agent_hostname",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "status": {
+ "name": "status",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'unknown'"
+ },
+ "service_count": {
+ "name": "service_count",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 0
+ },
+ "running_count": {
+ "name": "running_count",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 0
+ },
+ "compose_content": {
+ "name": "compose_content",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "env_content": {
+ "name": "env_content",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "last_polled": {
+ "name": "last_polled",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ }
+ },
+ "indexes": {
+ "stacks_table_name_unique": {
+ "name": "stacks_table_name_unique",
+ "columns": [
+ "name"
+ ],
+ "isUnique": true
+ }
+ },
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "users_table": {
+ "name": "users_table",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "text",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "username": {
+ "name": "username",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "password_hash": {
+ "name": "password_hash",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "display_name": {
+ "name": "display_name",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "email": {
+ "name": "email",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "roles": {
+ "name": "roles",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'[]'"
+ },
+ "require_password_change": {
+ "name": "require_password_change",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "oidc_subject_id": {
+ "name": "oidc_subject_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "last_login": {
+ "name": "last_login",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch()"
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ }
+ },
+ "indexes": {
+ "users_table_username_unique": {
+ "name": "users_table_username_unique",
+ "columns": [
+ "username"
+ ],
+ "isUnique": true
+ }
+ },
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ }
+ },
+ "views": {},
+ "enums": {},
+ "_meta": {
+ "schemas": {},
+ "tables": {},
+ "columns": {}
+ },
+ "internal": {
+ "indexes": {}
+ }
+}
\ No newline at end of file
diff --git a/src/db/migrations/meta/0004_snapshot.json b/src/db/migrations/meta/0004_snapshot.json
new file mode 100644
index 000000000..4dcd715ee
--- /dev/null
+++ b/src/db/migrations/meta/0004_snapshot.json
@@ -0,0 +1,813 @@
+{
+ "version": "6",
+ "dialect": "sqlite",
+ "id": "780c2018-3df8-4c8f-bef6-2f332b639d9d",
+ "prevId": "556da5cd-ba12-4614-879d-c91a31ad4a41",
+ "tables": {
+ "agent_tasks_table": {
+ "name": "agent_tasks_table",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "text",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "agent_id": {
+ "name": "agent_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "type": {
+ "name": "type",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "payload": {
+ "name": "payload",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "status": {
+ "name": "status",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'pending'"
+ },
+ "result": {
+ "name": "result",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "error": {
+ "name": "error",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "started_at": {
+ "name": "started_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "completed_at": {
+ "name": "completed_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ }
+ },
+ "indexes": {},
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "agent_tokens_table": {
+ "name": "agent_tokens_table",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "text",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "agent_id": {
+ "name": "agent_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "token": {
+ "name": "token",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "name": {
+ "name": "name",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "permissions": {
+ "name": "permissions",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'[]'"
+ },
+ "last_used": {
+ "name": "last_used",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "expires_at": {
+ "name": "expires_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "is_active": {
+ "name": "is_active",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": true
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ }
+ },
+ "indexes": {
+ "agent_tokens_table_token_unique": {
+ "name": "agent_tokens_table_token_unique",
+ "columns": [
+ "token"
+ ],
+ "isUnique": true
+ }
+ },
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "agents_table": {
+ "name": "agents_table",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "text",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "hostname": {
+ "name": "hostname",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "platform": {
+ "name": "platform",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "version": {
+ "name": "version",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "capabilities": {
+ "name": "capabilities",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'[]'"
+ },
+ "status": {
+ "name": "status",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'offline'"
+ },
+ "last_seen": {
+ "name": "last_seen",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "registered_at": {
+ "name": "registered_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "container_count": {
+ "name": "container_count",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "image_count": {
+ "name": "image_count",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "stack_count": {
+ "name": "stack_count",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "network_count": {
+ "name": "network_count",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "volume_count": {
+ "name": "volume_count",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "docker_version": {
+ "name": "docker_version",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "docker_containers": {
+ "name": "docker_containers",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "docker_images": {
+ "name": "docker_images",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "metadata": {
+ "name": "metadata",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ }
+ },
+ "indexes": {},
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "deployments_table": {
+ "name": "deployments_table",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "text",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "name": {
+ "name": "name",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "type": {
+ "name": "type",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "status": {
+ "name": "status",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'pending'"
+ },
+ "agent_id": {
+ "name": "agent_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "task_id": {
+ "name": "task_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "error": {
+ "name": "error",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "metadata": {
+ "name": "metadata",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ }
+ },
+ "indexes": {},
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "settings_table": {
+ "name": "settings_table",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "integer",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": true
+ },
+ "dockerHost": {
+ "name": "dockerHost",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "stacksDirectory": {
+ "name": "stacksDirectory",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "autoUpdate": {
+ "name": "autoUpdate",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "autoUpdateInterval": {
+ "name": "autoUpdateInterval",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 300
+ },
+ "pollingEnabled": {
+ "name": "pollingEnabled",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": true
+ },
+ "pollingInterval": {
+ "name": "pollingInterval",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 5
+ },
+ "pruneMode": {
+ "name": "pruneMode",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "registryCredentials": {
+ "name": "registryCredentials",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'[]'"
+ },
+ "templateRegistries": {
+ "name": "templateRegistries",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'[]'"
+ },
+ "auth": {
+ "name": "auth",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "onboarding": {
+ "name": "onboarding",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "baseServerUrl": {
+ "name": "baseServerUrl",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "maturityThresholdDays": {
+ "name": "maturityThresholdDays",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 30
+ },
+ "createdAt": {
+ "name": "createdAt",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ },
+ "updatedAt": {
+ "name": "updatedAt",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ }
+ },
+ "indexes": {},
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "stacks_table": {
+ "name": "stacks_table",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "text",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "name": {
+ "name": "name",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "dir_name": {
+ "name": "dir_name",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "path": {
+ "name": "path",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "auto_update": {
+ "name": "auto_update",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "is_external": {
+ "name": "is_external",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "is_legacy": {
+ "name": "is_legacy",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "is_remote": {
+ "name": "is_remote",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "agent_id": {
+ "name": "agent_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "agent_hostname": {
+ "name": "agent_hostname",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "status": {
+ "name": "status",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'unknown'"
+ },
+ "service_count": {
+ "name": "service_count",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 0
+ },
+ "running_count": {
+ "name": "running_count",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 0
+ },
+ "compose_content": {
+ "name": "compose_content",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "env_content": {
+ "name": "env_content",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "last_polled": {
+ "name": "last_polled",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ }
+ },
+ "indexes": {
+ "stacks_table_name_unique": {
+ "name": "stacks_table_name_unique",
+ "columns": [
+ "name"
+ ],
+ "isUnique": true
+ }
+ },
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "users_table": {
+ "name": "users_table",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "text",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "username": {
+ "name": "username",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "password_hash": {
+ "name": "password_hash",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "display_name": {
+ "name": "display_name",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "email": {
+ "name": "email",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "roles": {
+ "name": "roles",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'[]'"
+ },
+ "require_password_change": {
+ "name": "require_password_change",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "oidc_subject_id": {
+ "name": "oidc_subject_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "last_login": {
+ "name": "last_login",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ }
+ },
+ "indexes": {
+ "users_table_username_unique": {
+ "name": "users_table_username_unique",
+ "columns": [
+ "username"
+ ],
+ "isUnique": true
+ }
+ },
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ }
+ },
+ "views": {},
+ "enums": {},
+ "_meta": {
+ "schemas": {},
+ "tables": {},
+ "columns": {}
+ },
+ "internal": {
+ "indexes": {}
+ }
+}
\ No newline at end of file
diff --git a/src/db/migrations/meta/_journal.json b/src/db/migrations/meta/_journal.json
new file mode 100644
index 000000000..af3fb85cc
--- /dev/null
+++ b/src/db/migrations/meta/_journal.json
@@ -0,0 +1,41 @@
+{
+ "version": "7",
+ "dialect": "sqlite",
+ "entries": [
+ {
+ "idx": 0,
+ "version": "6",
+ "when": 1748818742198,
+ "tag": "0000_short_wong",
+ "breakpoints": true
+ },
+ {
+ "idx": 1,
+ "version": "6",
+ "when": 1748827322259,
+ "tag": "0001_omniscient_captain_universe",
+ "breakpoints": true
+ },
+ {
+ "idx": 2,
+ "version": "6",
+ "when": 1748837434871,
+ "tag": "0002_chunky_silverclaw",
+ "breakpoints": true
+ },
+ {
+ "idx": 3,
+ "version": "6",
+ "when": 1748886756139,
+ "tag": "0003_typical_ogun",
+ "breakpoints": true
+ },
+ {
+ "idx": 4,
+ "version": "6",
+ "when": 1748889342137,
+ "tag": "0004_deep_meteorite",
+ "breakpoints": true
+ }
+ ]
+}
\ No newline at end of file
diff --git a/src/db/schema.ts b/src/db/schema.ts
new file mode 100644
index 000000000..c120f38cf
--- /dev/null
+++ b/src/db/schema.ts
@@ -0,0 +1,239 @@
+import { int, sqliteTable, text, integer, real } from 'drizzle-orm/sqlite-core';
+import { relations } from 'drizzle-orm';
+import { sql } from 'drizzle-orm';
+
+export const settingsTable = sqliteTable('settings_table', {
+ id: int().primaryKey({ autoIncrement: true }),
+ dockerHost: text().notNull(),
+ stacksDirectory: text().notNull(),
+ autoUpdate: integer({ mode: 'boolean' }).notNull().default(false),
+ autoUpdateInterval: int().notNull().default(300),
+ pollingEnabled: integer({ mode: 'boolean' }).notNull().default(true),
+ pollingInterval: int().notNull().default(5),
+ pruneMode: text({ enum: ['all', 'dangling'] }),
+ registryCredentials: text({ mode: 'json' }).notNull().default('[]'),
+ templateRegistries: text({ mode: 'json' }).notNull().default('[]'),
+ auth: text({ mode: 'json' }).notNull(),
+ onboarding: text({ mode: 'json' }),
+ baseServerUrl: text(),
+ maturityThresholdDays: int().notNull().default(30),
+ createdAt: int({ mode: 'timestamp' })
+ .notNull()
+ .default(sql`(unixepoch())`),
+ updatedAt: int({ mode: 'timestamp' })
+ .notNull()
+ .default(sql`(unixepoch())`)
+});
+
+export const usersTable = sqliteTable('users_table', {
+ id: text('id').primaryKey(),
+ username: text('username').notNull().unique(),
+ passwordHash: text('password_hash'),
+ displayName: text('display_name'),
+ email: text('email'),
+ roles: text('roles', { mode: 'json' }).notNull().default('[]'),
+ requirePasswordChange: integer('require_password_change', { mode: 'boolean' }).notNull().default(false),
+ oidcSubjectId: text('oidc_subject_id'),
+ lastLogin: int('last_login', { mode: 'timestamp' }),
+ createdAt: int('created_at', { mode: 'timestamp' })
+ .notNull()
+ .default(sql`(unixepoch())`),
+ updatedAt: int('updated_at', { mode: 'timestamp' })
+ .notNull()
+ .default(sql`(unixepoch())`)
+});
+
+export const stacksTable = sqliteTable('stacks_table', {
+ id: text('id').primaryKey(),
+ name: text('name').notNull().unique(),
+ dirName: text('dir_name'),
+ path: text('path').notNull(),
+ autoUpdate: integer('auto_update', { mode: 'boolean' }).notNull().default(false),
+ isExternal: integer('is_external', { mode: 'boolean' }).notNull().default(false),
+ isLegacy: integer('is_legacy', { mode: 'boolean' }).notNull().default(false),
+ isRemote: integer('is_remote', { mode: 'boolean' }).notNull().default(false),
+ agentId: text('agent_id'), // For remote stacks
+ agentHostname: text('agent_hostname'), // For remote stacks
+ status: text('status', { enum: ['running', 'stopped', 'partially running', 'unknown'] })
+ .notNull()
+ .default('unknown'),
+ serviceCount: int('service_count').notNull().default(0),
+ runningCount: int('running_count').notNull().default(0),
+ composeContent: text('compose_content'),
+ envContent: text('env_content'),
+ lastPolled: int('last_polled', { mode: 'timestamp' }),
+ createdAt: integer('created_at', { mode: 'timestamp' })
+ .notNull()
+ .default(sql`(unixepoch())`),
+ updatedAt: integer('updated_at', { mode: 'timestamp' })
+ .notNull()
+ .default(sql`(unixepoch())`)
+});
+
+export const agentsTable = sqliteTable('agents_table', {
+ id: text('id').primaryKey(),
+ hostname: text('hostname').notNull(),
+ platform: text('platform').notNull(),
+ version: text('version').notNull(),
+ capabilities: text('capabilities', { mode: 'json' }).notNull().default('[]'),
+ status: text('status', { enum: ['online', 'offline'] })
+ .notNull()
+ .default('offline'),
+ lastSeen: integer('last_seen', { mode: 'timestamp' }).notNull(),
+ registeredAt: integer('registered_at', { mode: 'timestamp' }).notNull(),
+
+ // Metrics (optional)
+ containerCount: int('container_count'),
+ imageCount: int('image_count'),
+ stackCount: int('stack_count'),
+ networkCount: int('network_count'),
+ volumeCount: int('volume_count'),
+
+ // Docker info (optional)
+ dockerVersion: text('docker_version'),
+ dockerContainers: int('docker_containers'),
+ dockerImages: int('docker_images'),
+
+ // Metadata (optional JSON object)
+ metadata: text('metadata', { mode: 'json' }),
+
+ createdAt: int('created_at', { mode: 'timestamp' })
+ .notNull()
+ .default(sql`(unixepoch())`),
+ updatedAt: int('updated_at', { mode: 'timestamp' })
+ .notNull()
+ .default(sql`(unixepoch())`)
+});
+
+export const agentTasksTable = sqliteTable('agent_tasks_table', {
+ id: text('id').primaryKey(),
+ agentId: text('agent_id').notNull(),
+ type: text('type', {
+ enum: ['docker_command', 'stack_deploy', 'image_pull', 'health_check', 'container_start', 'container_stop', 'container_restart', 'container_remove', 'agent_upgrade']
+ }).notNull(),
+ payload: text('payload', { mode: 'json' }).notNull(),
+ status: text('status', { enum: ['pending', 'running', 'completed', 'failed'] })
+ .notNull()
+ .default('pending'),
+ result: text('result', { mode: 'json' }),
+ error: text('error'),
+
+ // Execution timing
+ startedAt: integer('started_at', { mode: 'timestamp' }),
+ completedAt: integer('completed_at', { mode: 'timestamp' }),
+
+ createdAt: integer('created_at', { mode: 'timestamp' })
+ .notNull()
+ .default(sql`(unixepoch())`),
+ updatedAt: integer('updated_at', { mode: 'timestamp' })
+ .notNull()
+ .default(sql`(unixepoch())`)
+});
+
+// Optional: Agent authentication/security table
+export const agentTokensTable = sqliteTable('agent_tokens_table', {
+ id: text('id').primaryKey(),
+ agentId: text('agent_id').notNull(),
+ token: text('token').notNull().unique(), // Hashed token for authentication
+ name: text('name'), // Human-readable name for the token
+ permissions: text('permissions', { mode: 'json' }).notNull().default('[]'), // JSON array of permissions
+ lastUsed: integer('last_used', { mode: 'timestamp' }),
+ expiresAt: integer('expires_at', { mode: 'timestamp' }), // Optional expiration
+ isActive: integer('is_active', { mode: 'boolean' }).notNull().default(true),
+
+ createdAt: integer('created_at', { mode: 'timestamp' })
+ .notNull()
+ .default(sql`(unixepoch())`),
+ updatedAt: integer('updated_at', { mode: 'timestamp' })
+ .notNull()
+ .default(sql`(unixepoch())`)
+});
+
+export const deploymentsTable = sqliteTable('deployments_table', {
+ id: text('id').primaryKey(),
+ name: text('name').notNull(),
+ type: text('type', { enum: ['stack', 'container', 'image'] }).notNull(),
+ status: text('status', { enum: ['pending', 'running', 'stopped', 'failed', 'completed'] })
+ .notNull()
+ .default('pending'),
+ agentId: text('agent_id').notNull(),
+ taskId: text('task_id'), // Links to agentTasksTable
+ error: text('error'),
+
+ // Metadata as JSON to match your interface
+ metadata: text('metadata', { mode: 'json' }),
+
+ createdAt: int('created_at', { mode: 'timestamp' })
+ .notNull()
+ .default(sql`(unixepoch())`),
+ updatedAt: int('updated_at', { mode: 'timestamp' })
+ .notNull()
+ .default(sql`(unixepoch())`)
+});
+
+// Add foreign key relationships if you want referential integrity
+// Note: SQLite foreign keys need to be enabled at runtime
+export const agentRelations = {
+ agentTasks: {
+ // agentTasksTable.agentId -> agentsTable.id
+ },
+ agentTokens: {
+ // agentTokensTable.agentId -> agentsTable.id
+ },
+ remoteStacks: {
+ // stacksTable.agentId -> agentsTable.id (existing relationship)
+ },
+ deployments: {
+ // deploymentsTable.agentId -> agentsTable.id
+ // deploymentsTable.taskId -> agentTasksTable.id
+ }
+};
+
+// Define proper relations using Drizzle's relations function
+export const agentsRelations = relations(agentsTable, ({ many }) => ({
+ tasks: many(agentTasksTable),
+ tokens: many(agentTokensTable),
+ remoteStacks: many(stacksTable),
+ deployments: many(deploymentsTable)
+}));
+
+export const agentTasksRelations = relations(agentTasksTable, ({ one, many }) => ({
+ agent: one(agentsTable, {
+ fields: [agentTasksTable.agentId],
+ references: [agentsTable.id]
+ }),
+ deployments: many(deploymentsTable)
+}));
+
+export const agentTokensRelations = relations(agentTokensTable, ({ one }) => ({
+ agent: one(agentsTable, {
+ fields: [agentTokensTable.agentId],
+ references: [agentsTable.id]
+ })
+}));
+
+export const stacksRelations = relations(stacksTable, ({ one }) => ({
+ agent: one(agentsTable, {
+ fields: [stacksTable.agentId],
+ references: [agentsTable.id]
+ })
+}));
+
+export const deploymentsRelations = relations(deploymentsTable, ({ one }) => ({
+ agent: one(agentsTable, {
+ fields: [deploymentsTable.agentId],
+ references: [agentsTable.id]
+ }),
+ task: one(agentTasksTable, {
+ fields: [deploymentsTable.taskId],
+ references: [agentTasksTable.id]
+ })
+}));
+
+export const usersRelations = relations(usersTable, ({ many }) => ({
+ // Add user relations if needed in the future
+}));
+
+export const settingsRelations = relations(settingsTable, ({ many }) => ({
+ // Settings is typically a singleton, no relations needed
+}));
diff --git a/src/hooks.server.ts b/src/hooks.server.ts
index 2c98c6620..a151ea02b 100644
--- a/src/hooks.server.ts
+++ b/src/hooks.server.ts
@@ -5,16 +5,24 @@ import { getUserByUsername } from '$lib/services/user-service';
import { getSettings } from '$lib/services/settings-service';
import { checkFirstRun } from '$lib/utils/onboarding.utils';
import { sessionHandler } from '$lib/services/session-handler';
-import { initComposeService } from '$lib/services/docker/stack-service';
+import { initComposeService, stackRuntimeUpdater } from '$lib/services/docker/stack-custom-service';
import { initAutoUpdateScheduler } from '$lib/services/docker/scheduler-service';
import { initMaturityPollingScheduler } from '$lib/services/docker/image-service';
+import { migrateSettingsToDatabase } from '$lib/services/database/settings-db-service';
+import { migrateUsersToDatabase } from '$lib/services/database/user-db-service';
+import { migrateStacksToDatabase } from '$lib/services/database/compose-db-service';
+import { runMigrations } from './db/migrate';
// Get environment variable
const isTestEnvironment = process.env.APP_ENV === 'TEST';
// Initialize needed services
try {
+ await runMigrations();
+ await Promise.all([migrateSettingsToDatabase(), migrateUsersToDatabase(), migrateStacksToDatabase()]);
await Promise.all([checkFirstRun(), initComposeService(), initAutoUpdateScheduler(), initMaturityPollingScheduler()]);
+
+ stackRuntimeUpdater.start(2);
} catch (err) {
console.error('Critical service init failed, exiting:', err);
process.exit(1);
diff --git a/src/lib/components/sidebar/sidebar-user.svelte b/src/lib/components/sidebar/sidebar-user.svelte
index 93e871a7a..870483fca 100644
--- a/src/lib/components/sidebar/sidebar-user.svelte
+++ b/src/lib/components/sidebar/sidebar-user.svelte
@@ -15,17 +15,18 @@
let { user, isCollapsed }: { user: User; isCollapsed: boolean } = $props();
const sidebar = useSidebar();
- // Generate Gravatar URL using browser's crypto API
+ // Generate Gravatar URL using SHA-256
async function getGravatarUrl(email: string, size = 40): Promise<string> {
if (!email) return '';
const encoder = new TextEncoder();
const data = encoder.encode(email.toLowerCase().trim());
- const hashBuffer = await crypto.subtle.digest('MD5', data);
+ const hashBuffer = await crypto.subtle.digest('SHA-256', data);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hash = hashArray.map((b) => b.toString(16).padStart(2, '0')).join('');
- return `https://www.gravatar.com/avatar/${hash}?s=${size}&d=mp&r=g`;
+ // Use Gravatar's SHA-256 endpoint
+ return `https://www.gravatar.com/avatar/${hash}?s=${size}`;
}
let gravatarUrl = $state('');
diff --git a/src/lib/components/sidebar/sidebar.svelte b/src/lib/components/sidebar/sidebar.svelte
index 85bf933ef..4133c41e3 100644
--- a/src/lib/components/sidebar/sidebar.svelte
+++ b/src/lib/components/sidebar/sidebar.svelte
@@ -12,7 +12,7 @@
{ title: 'Docker', url: '/settings/docker', icon: DatabaseIcon },
{ title: 'Users', url: '/settings/users', icon: UserIcon },
{ title: 'Security', url: '/settings/security', icon: Shield },
- { title: 'Agents', url: '/agents', icon: ComputerIcon }
+ { title: 'Agents (Preview)', url: '/agents', icon: ComputerIcon }
]
}
],
diff --git a/src/lib/services/agent/agent-manager.ts b/src/lib/services/agent/agent-manager.ts
index dc00558e0..1b3c5c2c4 100644
--- a/src/lib/services/agent/agent-manager.ts
+++ b/src/lib/services/agent/agent-manager.ts
@@ -1,188 +1,245 @@
-import fs from 'fs/promises';
-import path from 'node:path';
-import { BASE_PATH } from '$lib/services/paths-service';
import type { Agent, AgentTask } from '$lib/types/agent.type';
import { nanoid } from 'nanoid';
import { updateDeploymentFromTask } from '$lib/services/deployment-service';
+import { registerAgentInDb, getAgentByIdFromDb, updateAgentInDb, updateAgentHeartbeatInDb, listAgentsFromDb, deleteAgentFromDb, createTaskInDb, getTaskByIdFromDb, updateTaskStatusInDb, listTasksFromDb, getPendingTasksFromDb, getAgentTasksFromDb, updateAgentMetricsInDb, updateAgentDockerInfoInDb } from '$lib/services/database/agent-db-service';
-const AGENTS_DIR = path.join(BASE_PATH, 'agents');
-const TASKS_DIR = path.join(BASE_PATH, 'agent-tasks');
-
-// Ensure directories exist
-await fs.mkdir(AGENTS_DIR, { recursive: true });
-await fs.mkdir(TASKS_DIR, { recursive: true });
-
+/**
+ * Register a new agent or update existing one
+ */
export async function registerAgent(agent: Agent): Promise<Agent> {
- const existing = await getAgent(agent.id);
-
- if (existing) {
- // Update existing agent
- return await updateAgent(agent.id, {
- ...agent,
- status: 'online',
- lastSeen: new Date().toISOString(),
- updatedAt: new Date().toISOString()
- });
- } else {
- // Create new agent
- const newAgent: Agent = {
- ...agent,
- createdAt: new Date().toISOString(),
- updatedAt: new Date().toISOString()
- };
-
- const filePath = path.join(AGENTS_DIR, `${agent.id}.json`);
- await fs.writeFile(filePath, JSON.stringify(newAgent, null, 2));
-
- return newAgent;
+ try {
+ const existing = await getAgent(agent.id);
+
+ if (existing) {
+ // Update existing agent
+ return await updateAgent(agent.id, {
+ ...agent,
+ status: 'online',
+ lastSeen: new Date().toISOString(),
+ updatedAt: new Date().toISOString()
+ });
+ } else {
+ // Create new agent
+ const newAgent: Agent = {
+ ...agent,
+ createdAt: new Date().toISOString(),
+ updatedAt: new Date().toISOString()
+ };
+
+ return await registerAgentInDb(newAgent);
+ }
+ } catch (error) {
+ console.error('Failed to register agent:', error);
+ throw error;
}
}
+/**
+ * Get agent by ID
+ */
export async function getAgent(agentId: string): Promise<Agent | null> {
try {
- const filePath = path.join(AGENTS_DIR, `${agentId}.json`);
- const agentData = await fs.readFile(filePath, 'utf-8');
- return JSON.parse(agentData);
+ return await getAgentByIdFromDb(agentId);
} catch (error) {
+ console.error('Failed to get agent:', error);
return null;
}
}
+/**
+ * Update agent information
+ */
export async function updateAgent(agentId: string, updates: Partial<Agent>): Promise<Agent> {
- const existing = await getAgent(agentId);
- if (!existing) {
- throw new Error('Agent not found');
+ try {
+ return await updateAgentInDb(agentId, updates);
+ } catch (error) {
+ console.error('Failed to update agent:', error);
+ throw error;
}
-
- const updated: Agent = {
- ...existing,
- ...updates,
- updatedAt: new Date().toISOString()
- };
-
- const filePath = path.join(AGENTS_DIR, `${agentId}.json`);
- await fs.writeFile(filePath, JSON.stringify(updated, null, 2));
-
- return updated;
}
+/**
+ * Update agent heartbeat (called by agents to indicate they're online)
+ */
export async function updateAgentHeartbeat(agentId: string): Promise<void> {
- await updateAgent(agentId, {
- status: 'online',
- lastSeen: new Date().toISOString()
- });
+ try {
+ await updateAgentHeartbeatInDb(agentId);
+ } catch (error) {
+ console.error('Failed to update agent heartbeat:', error);
+ // Don't throw here as this is called frequently by agents
+ }
}
+/**
+ * List all agents
+ */
export async function listAgents(): Promise<Agent[]> {
try {
- const files = await fs.readdir(AGENTS_DIR);
- const agents: Agent[] = [];
-
- for (const file of files) {
- if (file.endsWith('.json')) {
- const agentData = await fs.readFile(path.join(AGENTS_DIR, file), 'utf-8');
- agents.push(JSON.parse(agentData));
- }
- }
-
- return agents.sort((a, b) => new Date(b.lastSeen).getTime() - new Date(a.lastSeen).getTime());
+ return await listAgentsFromDb();
} catch (error) {
- console.error('Error listing agents:', error);
+ console.error('Failed to list agents:', error);
return [];
}
}
-// Simplified task creation - no WebSocket sending needed
-export async function sendTaskToAgent(agentId: string, taskType: string, payload: any): Promise<AgentTask> {
- const agent = await getAgent(agentId);
- if (!agent) {
- throw new Error(`Agent ${agentId} not found`);
+/**
+ * Delete an agent and all its tasks
+ */
+export async function deleteAgent(agentId: string): Promise<void> {
+ try {
+ await deleteAgentFromDb(agentId);
+ console.log(`Agent ${agentId} deleted successfully`);
+ } catch (error) {
+ console.error('Failed to delete agent:', error);
+ throw error;
}
+}
- if (agent.status !== 'online') {
- throw new Error(`Agent ${agentId} is not online (status: ${agent.status})`);
- }
+/**
+ * Send a task to an agent
+ */
+export async function sendTaskToAgent(agentId: string, taskType: string, payload: any): Promise<AgentTask> {
+ try {
+ const agent = await getAgent(agentId);
+ if (!agent) {
+ throw new Error(`Agent ${agentId} not found`);
+ }
- // Create task
- const task: AgentTask = {
- id: nanoid(),
- agentId,
- type: taskType as any,
- payload,
- status: 'pending', // Agent will pick this up on next poll
- createdAt: new Date().toISOString()
- };
+ if (agent.status !== 'online') {
+ throw new Error(`Agent ${agentId} is not online (status: ${agent.status})`);
+ }
+
+ // Create task
+ const task: AgentTask = {
+ id: nanoid(),
+ agentId,
+ type: taskType as any,
+ payload,
+ status: 'pending',
+ createdAt: new Date().toISOString()
+ };
- // Save task to file - agent will fetch this via HTTP
- const filePath = path.join(TASKS_DIR, `${task.id}.json`);
- await fs.writeFile(filePath, JSON.stringify(task, null, 2));
+ // Save task to database - agent will pick this up on next poll
+ await createTaskInDb(task);
- console.log(`📋 Task ${task.id} created for agent ${agentId} (will be picked up on next poll)`);
+ console.log(`📋 Task ${task.id} created for agent ${agentId} (will be picked up on next poll)`);
- return task;
+ return task;
+ } catch (error) {
+ console.error('Failed to send task to agent:', error);
+ throw error;
+ }
}
+/**
+ * Update task status (called when agents complete tasks)
+ */
export async function updateTaskStatus(taskId: string, status: string, result?: any, error?: string): Promise<void> {
try {
- const filePath = path.join(TASKS_DIR, `${taskId}.json`);
- const taskData = await fs.readFile(filePath, 'utf-8');
- const task = JSON.parse(taskData);
-
- task.status = status;
- task.result = result;
- task.error = error;
- task.completedAt = status === 'completed' || status === 'failed' ? new Date().toISOString() : undefined;
+ await updateTaskStatusInDb(taskId, status, result, error);
- await fs.writeFile(filePath, JSON.stringify(task, null, 2));
-
- // Update corresponding deployment if it exists
await updateDeploymentFromTask(taskId, status, result, error);
console.log(`Task ${taskId} status updated to: ${status}`);
- } catch (error) {
- console.error('Error updating task status:', error);
+ } catch (dbError) {
+ console.error('Error updating task status:', dbError);
+ // Don't throw here as agents rely on this endpoint
}
}
+/**
+ * Get a single task by ID
+ */
export async function getTask(taskId: string): Promise<AgentTask | null> {
try {
- const filePath = path.join(TASKS_DIR, `${taskId}.json`);
- const taskData = await fs.readFile(filePath, 'utf-8');
- return JSON.parse(taskData);
+ return await getTaskByIdFromDb(taskId);
} catch (error) {
+ console.error('Failed to get task:', error);
return null;
}
}
+/**
+ * List tasks (optionally filtered by agent)
+ */
export async function listTasks(agentId?: string): Promise<AgentTask[]> {
try {
- const files = await fs.readdir(TASKS_DIR);
- const tasks: AgentTask[] = [];
-
- for (const file of files) {
- if (file.endsWith('.json')) {
- const taskData = await fs.readFile(path.join(TASKS_DIR, file), 'utf-8');
- const task = JSON.parse(taskData);
+ return await listTasksFromDb(agentId);
+ } catch (error) {
+ console.error('Failed to list tasks:', error);
+ return [];
+ }
+}
- if (!agentId || task.agentId === agentId) {
- tasks.push(task);
- }
- }
- }
+/**
+ * Get tasks for a specific agent (alias for listTasks)
+ */
+export async function getAgentTasks(agentId: string): Promise<AgentTask[]> {
+ try {
+ return await getAgentTasksFromDb(agentId);
+ } catch (error) {
+ console.error('Failed to get agent tasks:', error);
+ return [];
+ }
+}
- return tasks.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
+/**
+ * Get pending tasks for an agent (called by agents)
+ */
+export async function getPendingTasks(agentId: string): Promise<AgentTask[]> {
+ try {
+ return await getPendingTasksFromDb(agentId);
} catch (error) {
- console.error('Error listing tasks:', error);
+ console.error('Failed to get pending tasks:', error);
return [];
}
}
-// Add this function - it's an alias for listTasks to match the expected API
-export async function getAgentTasks(agentId: string): Promise<AgentTask[]> {
- return listTasks(agentId);
+/**
+ * Update agent metrics
+ */
+export async function updateAgentMetrics(
+ agentId: string,
+ metrics: {
+ containerCount?: number;
+ imageCount?: number;
+ stackCount?: number;
+ networkCount?: number;
+ volumeCount?: number;
+ }
+): Promise<void> {
+ try {
+ await updateAgentMetricsInDb(agentId, metrics);
+ } catch (error) {
+ console.error('Failed to update agent metrics:', error);
+ // Don't throw as this is not critical
+ }
+}
+
+/**
+ * Update agent Docker information
+ */
+export async function updateAgentDockerInfo(
+ agentId: string,
+ dockerInfo: {
+ version?: string;
+ containers?: number;
+ images?: number;
+ }
+): Promise<void> {
+ try {
+ await updateAgentDockerInfoInDb(agentId, dockerInfo);
+ } catch (error) {
+ console.error('Failed to update agent Docker info:', error);
+ // Don't throw as this is not critical
+ }
}
-// Helper functions remain the same
+// ===== CONVENIENCE HELPER FUNCTIONS =====
+
+/**
+ * Send Docker command to agent
+ */
export async function sendDockerCommand(agentId: string, command: string, args: string[] = []): Promise<AgentTask> {
return sendTaskToAgent(agentId, 'docker_command', {
command,
@@ -190,6 +247,9 @@ export async function sendDockerCommand(agentId: string, command: string, args:
});
}
+/**
+ * Deploy stack to agent
+ */
export async function deployStackToAgent(agentId: string, stackId: string, composeContent: string, envContent?: string): Promise<AgentTask> {
return sendTaskToAgent(agentId, 'stack_deploy', {
stackId,
@@ -198,17 +258,121 @@ export async function deployStackToAgent(agentId: string, stackId: string, compo
});
}
+/**
+ * Pull image on agent
+ */
export async function pullImageOnAgent(agentId: string, imageName: string): Promise<AgentTask> {
return sendTaskToAgent(agentId, 'image_pull', {
imageName
});
}
+/**
+ * Send health check to agent
+ */
+export async function sendHealthCheck(agentId: string): Promise<AgentTask> {
+ return sendTaskToAgent(agentId, 'health_check', {});
+}
+
+/**
+ * Send upgrade command to agent
+ */
+export async function upgradeAgent(agentId: string, version?: string): Promise<AgentTask> {
+ return sendTaskToAgent(agentId, 'agent_upgrade', {
+ version: version || 'latest'
+ });
+}
+
+/**
+ * Process agent message (for WebSocket or other real-time communication)
+ */
export async function processAgentMessage(agentId: string, message: any): Promise<void> {
console.log(`Processing message from agent ${agentId}:`, message);
- if (message.type === 'task_result') {
- const { task_id, status, result, error } = message.data;
- await updateTaskStatus(task_id, status, result, error);
+ try {
+ if (message.type === 'task_result') {
+ const { task_id, status, result, error } = message.data;
+ await updateTaskStatus(task_id, status, result, error);
+ } else if (message.type === 'heartbeat') {
+ await updateAgentHeartbeat(agentId);
+ } else if (message.type === 'metrics') {
+ await updateAgentMetrics(agentId, message.data);
+ } else if (message.type === 'docker_info') {
+ await updateAgentDockerInfo(agentId, message.data);
+ }
+ } catch (error) {
+ console.error('Failed to process agent message:', error);
+ }
+}
+
+/**
+ * Check if agent is online (with timeout consideration)
+ */
+export function isAgentOnline(agent: Agent, timeoutMinutes = 5): boolean {
+ if (agent.status !== 'online') return false;
+
+ const now = new Date();
+ const lastSeen = new Date(agent.lastSeen);
+ const timeoutMs = timeoutMinutes * 60 * 1000;
+
+ return now.getTime() - lastSeen.getTime() < timeoutMs;
+}
+
+/**
+ * Get agents that are actually online (considering timeout)
+ */
+export async function getOnlineAgents(timeoutMinutes = 5): Promise<Agent[]> {
+ const allAgents = await listAgents();
+ return allAgents.filter((agent) => isAgentOnline(agent, timeoutMinutes));
+}
+
+/**
+ * Get agent statistics
+ */
+export async function getAgentStats(): Promise<{
+ total: number;
+ online: number;
+ offline: number;
+ totalTasks: number;
+ pendingTasks: number;
+ runningTasks: number;
+ completedTasks: number;
+ failedTasks: number;
+}> {
+ try {
+ const [agents, allTasks] = await Promise.all([listAgents(), listTasks()]);
+
+ const onlineAgents = agents.filter((agent) => isAgentOnline(agent));
+
+ const taskStats = allTasks.reduce(
+ (acc, task) => {
+ acc[task.status]++;
+ return acc;
+ },
+ { pending: 0, running: 0, completed: 0, failed: 0 } as Record<string, number>
+ );
+
+ return {
+ total: agents.length,
+ online: onlineAgents.length,
+ offline: agents.length - onlineAgents.length,
+ totalTasks: allTasks.length,
+ pendingTasks: taskStats.pending,
+ runningTasks: taskStats.running,
+ completedTasks: taskStats.completed,
+ failedTasks: taskStats.failed
+ };
+ } catch (error) {
+ console.error('Failed to get agent stats:', error);
+ return {
+ total: 0,
+ online: 0,
+ offline: 0,
+ totalTasks: 0,
+ pendingTasks: 0,
+ runningTasks: 0,
+ completedTasks: 0,
+ failedTasks: 0
+ };
}
}
diff --git a/src/lib/services/database/agent-db-service.ts b/src/lib/services/database/agent-db-service.ts
new file mode 100644
index 000000000..6da0fe24d
--- /dev/null
+++ b/src/lib/services/database/agent-db-service.ts
@@ -0,0 +1,466 @@
+import { eq, desc, and, asc, or, lt } from 'drizzle-orm';
+import { agentsTable, agentTasksTable } from '../../../db/schema';
+import type { Agent, AgentTask } from '$lib/types/agent.type';
+import { db } from '../../../db';
+
+/**
+ * Convert database agent to Agent type
+ */
+function dbAgentToAgent(dbAgent: any): Agent {
+ return {
+ id: dbAgent.id,
+ hostname: dbAgent.hostname,
+ platform: dbAgent.platform,
+ version: dbAgent.version,
+ capabilities: Array.isArray(dbAgent.capabilities) ? dbAgent.capabilities : JSON.parse(dbAgent.capabilities || '[]'),
+ status: dbAgent.status,
+ lastSeen: dbAgent.lastSeen ? new Date(dbAgent.lastSeen * 1000).toISOString() : new Date().toISOString(),
+ registeredAt: dbAgent.registeredAt ? new Date(dbAgent.registeredAt * 1000).toISOString() : new Date().toISOString(),
+ metrics: {
+ containerCount: dbAgent.containerCount,
+ imageCount: dbAgent.imageCount,
+ stackCount: dbAgent.stackCount,
+ networkCount: dbAgent.networkCount,
+ volumeCount: dbAgent.volumeCount
+ },
+ dockerInfo: dbAgent.dockerVersion
+ ? {
+ version: dbAgent.dockerVersion,
+ containers: dbAgent.dockerContainers || 0,
+ images: dbAgent.dockerImages || 0
+ }
+ : undefined,
+ metadata: dbAgent.metadata ? (typeof dbAgent.metadata === 'string' ? JSON.parse(dbAgent.metadata) : dbAgent.metadata) : undefined,
+ createdAt: new Date(dbAgent.createdAt * 1000).toISOString(),
+ updatedAt: dbAgent.updatedAt ? new Date(dbAgent.updatedAt * 1000).toISOString() : undefined
+ };
+}
+
+/**
+ * Convert database task to AgentTask type
+ */
+function dbTaskToAgentTask(dbTask: any): AgentTask {
+ return {
+ id: dbTask.id,
+ agentId: dbTask.agentId,
+ type: dbTask.type,
+ payload: typeof dbTask.payload === 'string' ? JSON.parse(dbTask.payload) : dbTask.payload,
+ status: dbTask.status,
+ result: dbTask.result ? (typeof dbTask.result === 'string' ? JSON.parse(dbTask.result) : dbTask.result) : undefined,
+ error: dbTask.error,
+ createdAt: new Date(dbTask.createdAt * 1000).toISOString(),
+ updatedAt: dbTask.updatedAt ? new Date(dbTask.updatedAt * 1000).toISOString() : undefined
+ };
+}
+
+/**
+ * Convert Agent to database format
+ */
+function agentToDbAgent(agent: Agent): any {
+ return {
+ id: agent.id,
+ hostname: agent.hostname,
+ platform: agent.platform,
+ version: agent.version,
+ capabilities: JSON.stringify(agent.capabilities),
+ status: agent.status,
+ lastSeen: new Date(agent.lastSeen), // Use Date object
+ registeredAt: new Date(agent.registeredAt), // Use Date object
+ containerCount: agent.metrics?.containerCount,
+ imageCount: agent.metrics?.imageCount,
+ stackCount: agent.metrics?.stackCount,
+ networkCount: agent.metrics?.networkCount,
+ volumeCount: agent.metrics?.volumeCount,
+ dockerVersion: agent.dockerInfo?.version,
+ dockerContainers: agent.dockerInfo?.containers,
+ dockerImages: agent.dockerInfo?.images,
+ metadata: agent.metadata ? JSON.stringify(agent.metadata) : null,
+ updatedAt: new Date() // Use Date object
+ };
+}
+
+/**
+ * Convert AgentTask to database format
+ */
+function agentTaskToDbTask(task: AgentTask): any {
+ return {
+ id: task.id,
+ agentId: task.agentId,
+ type: task.type,
+ payload: JSON.stringify(task.payload),
+ status: task.status,
+ result: task.result ? JSON.stringify(task.result) : null,
+ error: task.error,
+ startedAt: task.status === 'running' ? new Date() : null, // Use Date object
+ completedAt: task.status === 'completed' || task.status === 'failed' ? new Date() : null, // Use Date object
+ updatedAt: new Date() // Use Date object
+ };
+}
+
+// ===== AGENT OPERATIONS =====
+
+/**
+ * Register or update an agent in the database
+ */
+export async function registerAgentInDb(agent: Agent): Promise<Agent> {
+ try {
+ const agentData = agentToDbAgent(agent);
+
+ // Check if agent exists
+ const existing = await getAgentByIdFromDb(agent.id);
+
+ if (existing) {
+ // Update existing agent
+ await db
+ .update(agentsTable)
+ .set({
+ ...agentData,
+ updatedAt: new Date() // Use Date object
+ })
+ .where(eq(agentsTable.id, agent.id));
+ } else {
+ // Insert new agent
+ await db.insert(agentsTable).values({
+ ...agentData,
+ createdAt: new Date(), // Use Date object
+ updatedAt: new Date() // Use Date object
+ });
+ }
+
+ return agent;
+ } catch (error) {
+ console.error('Failed to register agent in database:', error);
+ throw error;
+ }
+}
+
+/**
+ * Get agent by ID from database
+ */
+export async function getAgentByIdFromDb(agentId: string): Promise<Agent | null> {
+ try {
+ const result = await db.select().from(agentsTable).where(eq(agentsTable.id, agentId)).limit(1);
+
+ if (result.length === 0) {
+ return null;
+ }
+
+ return dbAgentToAgent(result[0]);
+ } catch (error) {
+ console.error('Failed to get agent from database:', error);
+ throw error;
+ }
+}
+
+/**
+ * Update agent in database
+ */
+export async function updateAgentInDb(agentId: string, updates: Partial<Agent>): Promise<Agent> {
+ try {
+ const existing = await getAgentByIdFromDb(agentId);
+ if (!existing) {
+ throw new Error('Agent not found');
+ }
+
+ const updated: Agent = {
+ ...existing,
+ ...updates,
+ updatedAt: new Date().toISOString()
+ };
+
+ const agentData = agentToDbAgent(updated);
+
+ await db.update(agentsTable).set(agentData).where(eq(agentsTable.id, agentId));
+
+ return updated;
+ } catch (error) {
+ console.error('Failed to update agent in database:', error);
+ throw error;
+ }
+}
+
+/**
+ * Update agent heartbeat (status and lastSeen)
+ */
+export async function updateAgentHeartbeatInDb(agentId: string): Promise<void> {
+ try {
+ await db
+ .update(agentsTable)
+ .set({
+ status: 'online',
+ lastSeen: new Date(), // Use Date object
+ updatedAt: new Date() // Use Date object
+ })
+ .where(eq(agentsTable.id, agentId));
+ } catch (error) {
+ console.error('Failed to update agent heartbeat in database:', error);
+ throw error;
+ }
+}
+
+/**
+ * List all agents from database
+ */
+export async function listAgentsFromDb(): Promise<Agent[]> {
+ try {
+ const result = await db.select().from(agentsTable).orderBy(desc(agentsTable.lastSeen));
+ return result.map(dbAgentToAgent);
+ } catch (error) {
+ console.error('Failed to list agents from database:', error);
+ throw error;
+ }
+}
+
+/**
+ * Delete agent from database
+ */
+export async function deleteAgentFromDb(agentId: string): Promise<void> {
+ try {
+ // Delete all tasks for this agent first
+ await db.delete(agentTasksTable).where(eq(agentTasksTable.agentId, agentId));
+
+ // Delete the agent
+ await db.delete(agentsTable).where(eq(agentsTable.id, agentId));
+ } catch (error) {
+ console.error('Failed to delete agent from database:', error);
+ throw error;
+ }
+}
+
+// ===== TASK OPERATIONS =====
+
+/**
+ * Create a new task in database
+ */
+export async function createTaskInDb(task: Omit<AgentTask, 'createdAt' | 'updatedAt'>): Promise<AgentTask> {
+ try {
+ const taskData = {
+ ...agentTaskToDbTask(task as AgentTask),
+ createdAt: new Date(), // Use Date object
+ updatedAt: new Date() // Use Date object
+ };
+
+ await db.insert(agentTasksTable).values(taskData);
+
+ return {
+ ...task,
+ createdAt: new Date().toISOString(),
+ updatedAt: new Date().toISOString()
+ };
+ } catch (error) {
+ console.error('Failed to create task in database:', error);
+ throw error;
+ }
+}
+
+/**
+ * Get task by ID from database
+ */
+export async function getTaskByIdFromDb(taskId: string): Promise<AgentTask | null> {
+ try {
+ const result = await db.select().from(agentTasksTable).where(eq(agentTasksTable.id, taskId)).limit(1);
+
+ if (result.length === 0) {
+ return null;
+ }
+
+ return dbTaskToAgentTask(result[0]);
+ } catch (error) {
+ console.error('Failed to get task from database:', error);
+ throw error;
+ }
+}
+
+/**
+ * Update task status in database
+ */
+export async function updateTaskStatusInDb(taskId: string, status: string, result?: any, error?: string): Promise<void> {
+ try {
+ const updateData: any = {
+ status,
+ updatedAt: new Date() // Use Date object
+ };
+
+ if (result !== undefined) {
+ updateData.result = JSON.stringify(result);
+ }
+
+ if (error !== undefined) {
+ updateData.error = error;
+ }
+
+ // Set timing fields based on status
+ if (status === 'running') {
+ updateData.startedAt = new Date(); // Use Date object
+ } else if (status === 'completed' || status === 'failed') {
+ updateData.completedAt = new Date(); // Use Date object
+ }
+
+ await db.update(agentTasksTable).set(updateData).where(eq(agentTasksTable.id, taskId));
+ } catch (error) {
+ console.error('Failed to update task status in database:', error);
+ throw error;
+ }
+}
+
+/**
+ * List tasks from database (optionally filtered by agent)
+ */
+export async function listTasksFromDb(agentId?: string): Promise<AgentTask[]> {
+ try {
+ const baseQuery = db.select().from(agentTasksTable);
+
+ const result = agentId ? await baseQuery.where(eq(agentTasksTable.agentId, agentId)).orderBy(desc(agentTasksTable.createdAt)) : await baseQuery.orderBy(desc(agentTasksTable.createdAt));
+
+ return result.map(dbTaskToAgentTask);
+ } catch (error) {
+ console.error('Failed to list tasks from database:', error);
+ throw error;
+ }
+}
+
+/**
+ * Get pending tasks for an agent
+ */
+export async function getPendingTasksFromDb(agentId: string): Promise<AgentTask[]> {
+ try {
+ const result = await db
+ .select()
+ .from(agentTasksTable)
+ .where(and(eq(agentTasksTable.agentId, agentId), eq(agentTasksTable.status, 'pending')))
+ .orderBy(asc(agentTasksTable.createdAt));
+
+ return result.map(dbTaskToAgentTask);
+ } catch (error) {
+ console.error('Failed to get pending tasks from database:', error);
+ throw error;
+ }
+}
+
+/**
+ * Delete task from database
+ */
+export async function deleteTaskFromDb(taskId: string): Promise<void> {
+ try {
+ await db.delete(agentTasksTable).where(eq(agentTasksTable.id, taskId));
+ } catch (error) {
+ console.error('Failed to delete task from database:', error);
+ throw error;
+ }
+}
+
+/**
+ * Delete old completed/failed tasks (cleanup)
+ */
+export async function cleanupOldTasksFromDb(olderThanDays = 30): Promise<number> {
+ try {
+ const cutoffDate = new Date(Date.now() - olderThanDays * 24 * 60 * 60 * 1000);
+
+ // Delete completed and failed tasks older than cutoff date
+ const result = await db.delete(agentTasksTable).where(
+ and(
+ // Only delete completed or failed tasks
+ or(eq(agentTasksTable.status, 'completed'), eq(agentTasksTable.status, 'failed')),
+ // Only delete tasks completed before cutoff date
+ lt(agentTasksTable.completedAt, cutoffDate)
+ )
+ );
+
+ const deletedCount = result.rowsAffected || 0;
+ console.log(`Cleaned up ${deletedCount} old tasks older than ${olderThanDays} days`);
+ return deletedCount;
+ } catch (error) {
+ console.error('Failed to cleanup old tasks from database:', error);
+ throw error;
+ }
+}
+
+// ===== CONVENIENCE FUNCTIONS =====
+
+/**
+ * Get agent tasks (alias for listTasksFromDb with agentId)
+ */
+export async function getAgentTasksFromDb(agentId: string): Promise<AgentTask[]> {
+ return listTasksFromDb(agentId);
+}
+
+/**
+ * Get agent with its recent tasks
+ */
+export async function getAgentWithTasksFromDb(agentId: string, taskLimit = 10): Promise<{ agent: Agent | null; tasks: AgentTask[] }> {
+ try {
+ const [agent, tasks] = await Promise.all([
+ getAgentByIdFromDb(agentId),
+ db
+ .select()
+ .from(agentTasksTable)
+ .where(eq(agentTasksTable.agentId, agentId))
+ .orderBy(desc(agentTasksTable.createdAt))
+ .limit(taskLimit)
+ .then((result) => result.map(dbTaskToAgentTask))
+ ]);
+
+ return { agent, tasks };
+ } catch (error) {
+ console.error('Failed to get agent with tasks from database:', error);
+ throw error;
+ }
+}
+
+/**
+ * Update agent metrics
+ */
+export async function updateAgentMetricsInDb(
+ agentId: string,
+ metrics: {
+ containerCount?: number;
+ imageCount?: number;
+ stackCount?: number;
+ networkCount?: number;
+ volumeCount?: number;
+ }
+): Promise<void> {
+ try {
+ await db
+ .update(agentsTable)
+ .set({
+ containerCount: metrics.containerCount,
+ imageCount: metrics.imageCount,
+ stackCount: metrics.stackCount,
+ networkCount: metrics.networkCount,
+ volumeCount: metrics.volumeCount,
+ updatedAt: new Date() // Use Date object
+ })
+ .where(eq(agentsTable.id, agentId));
+ } catch (error) {
+ console.error('Failed to update agent metrics in database:', error);
+ throw error;
+ }
+}
+
+/**
+ * Update agent Docker info
+ */
+export async function updateAgentDockerInfoInDb(
+ agentId: string,
+ dockerInfo: {
+ version?: string;
+ containers?: number;
+ images?: number;
+ }
+): Promise<void> {
+ try {
+ await db
+ .update(agentsTable)
+ .set({
+ dockerVersion: dockerInfo.version,
+ dockerContainers: dockerInfo.containers,
+ dockerImages: dockerInfo.images,
+ updatedAt: new Date() // Use Date object
+ })
+ .where(eq(agentsTable.id, agentId));
+ } catch (error) {
+ console.error('Failed to update agent Docker info in database:', error);
+ throw error;
+ }
+}
diff --git a/src/lib/services/database/compose-db-service.ts b/src/lib/services/database/compose-db-service.ts
new file mode 100644
index 000000000..44f42ef6d
--- /dev/null
+++ b/src/lib/services/database/compose-db-service.ts
@@ -0,0 +1,510 @@
+import { eq } from 'drizzle-orm';
+import { stacksTable } from '../../../db/schema';
+import type { Stack } from '$lib/types/docker/stack.type';
+import fs from 'node:fs/promises';
+import path from 'node:path';
+import { STACKS_DIR } from '../paths-service';
+import { db } from '../../../db';
+
+// The toUnixTimestamp and fromUnixTimestamp helpers might still be useful
+// for converting between numbers and ISO strings if your Stack type uses one of those,
+// but for direct Drizzle interaction with { mode: 'timestamp' }, we'll use Date objects.
+
+/**
+ * Helper function to safely convert dates to Unix timestamps (numbers)
+ * This might be used if the Stack type itself uses numeric timestamps.
+ */
+function toUnixTimestamp(date: string | Date | number): number {
+ if (typeof date === 'number') {
+ // Already a timestamp, but ensure it's in seconds
+ return date > 1000000000000 ? Math.floor(date / 1000) : date;
+ }
+ if (typeof date === 'string') {
+ return Math.floor(new Date(date).getTime() / 1000);
+ }
+ if (date instanceof Date) {
+ return Math.floor(date.getTime() / 1000);
+ }
+ // Fallback to current time if input is invalid
+ console.warn('toUnixTimestamp received invalid date input, using Date.now()');
+ return Math.floor(Date.now() / 1000);
+}
+
+/**
+ * Helper function to safely convert Unix timestamps (numbers) to ISO strings.
+ * Used if Stack type expects ISO strings.
+ */
+function fromUnixTimestampToISOString(timestamp: number | null | undefined): string | undefined {
+ if (timestamp === null || timestamp === undefined) return undefined;
+ // Ensure timestamp is in milliseconds for Date constructor
+ const ms = timestamp < 1000000000000 ? timestamp * 1000 : timestamp;
+ return new Date(ms).toISOString();
+}
+
+/**
+ * Helper function to convert various date inputs to a Date object.
+ */
+function ensureDateObject(dateInput: string | Date | number | null | undefined): Date | null {
+ if (dateInput === null || dateInput === undefined) return null;
+ if (dateInput instanceof Date) return dateInput;
+ if (typeof dateInput === 'string') return new Date(dateInput);
+ if (typeof dateInput === 'number') {
+ // Assume Unix timestamp (seconds or milliseconds)
+ return new Date(dateInput * (dateInput < 1000000000000 ? 1000 : 1));
+ }
+ console.warn('ensureDateObject received invalid input:', dateInput);
+ return null;
+}
+
+/**
+ * Load stacks directly from files for migration (to avoid circular dependency)
+ */
+async function loadStacksFromFiles(): Promise<Stack[]> {
+ try {
+ const stackDirEntries = await fs.readdir(STACKS_DIR, { withFileTypes: true });
+ const stacks: Stack[] = [];
+
+ for (const entry of stackDirEntries) {
+ if (!entry.isDirectory()) {
+ continue;
+ }
+
+ const dirName = entry.name;
+ const stackDir = path.join(STACKS_DIR, dirName);
+
+ let composeContent = '';
+
+ const potentialComposePaths = [path.join(stackDir, 'compose.yaml'), path.join(stackDir, 'docker-compose.yml'), path.join(stackDir, 'compose.yml'), path.join(stackDir, 'docker-compose.yaml')];
+
+ for (const p of potentialComposePaths) {
+ try {
+ await fs.access(p);
+ composeContent = await fs.readFile(p, 'utf8');
+ break;
+ } catch {
+ // File not accessible or doesn't exist, try next
+ }
+ }
+
+ if (!composeContent) {
+ console.warn(`No compose file found in directory ${dirName}, skipping.`);
+ continue;
+ }
+
+ let dirStat;
+ try {
+ dirStat = await fs.stat(stackDir);
+ } catch (statErr) {
+ console.error(`Could not stat directory ${stackDir}:`, statErr);
+ const now = new Date();
+ dirStat = { birthtime: now, mtime: now };
+ }
+
+ stacks.push({
+ id: dirName,
+ name: dirName,
+ serviceCount: 0,
+ runningCount: 0,
+ status: 'unknown',
+ createdAt: dirStat.birthtime.toISOString(),
+ updatedAt: dirStat.mtime.toISOString(),
+ isExternal: false
+ });
+ }
+
+ return stacks;
+ } catch (err) {
+ console.error('Error loading stacks from STACKS_DIR:', err);
+ throw new Error('Failed to load compose stacks');
+ }
+}
+
+/**
+ * Migrates stacks from file-based storage to database
+ */
+export async function migrateStacksToDatabase(backupOldFiles: boolean = true): Promise<{
+ success: boolean;
+ migratedCount: number;
+ errors: string[];
+}> {
+ const errors: string[] = [];
+ let migratedCount = 0;
+
+ try {
+ console.log('Starting stack migration from file to database...');
+ const fileStacks = await loadStacksFromFiles();
+ console.log(`Found ${fileStacks.length} stacks in file system`);
+
+ if (fileStacks.length === 0) {
+ console.log('No stacks found in file system. Migration completed.');
+ return { success: true, migratedCount: 0, errors: [] };
+ }
+
+ const existingStacks = await db.select({ id: stacksTable.id }).from(stacksTable).limit(1);
+ if (existingStacks.length > 0) {
+ console.log('Stacks already exist in database. Migration aborted.');
+ return { success: false, migratedCount: 0, errors: ['Stacks already exist in database'] };
+ }
+
+ for (const stack of fileStacks) {
+ // stack.createdAt and stack.updatedAt are ISO strings from loadStacksFromFiles
+ try {
+ // ... (reading composeContent, envContent) ...
+ // ...existing code...
+ const stackDir = path.join(STACKS_DIR, stack.id);
+ let composeContent = '';
+ let envContent = '';
+
+ const potentialComposePaths = [path.join(stackDir, 'compose.yaml'), path.join(stackDir, 'docker-compose.yml'), path.join(stackDir, 'compose.yml'), path.join(stackDir, 'docker-compose.yaml')];
+ for (const composePath of potentialComposePaths) {
+ try {
+ composeContent = await fs.readFile(composePath, 'utf8');
+ break;
+ } catch {
+ /* Try next path */
+ }
+ }
+ try {
+ const envPath = path.join(stackDir, '.env');
+ envContent = await fs.readFile(envPath, 'utf8');
+ } catch {
+ /* .env file doesn't exist, that's ok */
+ }
+
+ const dbInsertValues = {
+ id: stack.id,
+ name: stack.name,
+ dirName: stack.id, // Assuming dirName is stack.id for migration
+ path: stackDir, // Assuming path is STACKS_DIR + stack.id
+ autoUpdate: false, // Default value
+ isExternal: stack.isExternal || false,
+ isLegacy: stack.isLegacy || false,
+ isRemote: stack.isRemote || false,
+ agentId: stack.agentId || null,
+ agentHostname: stack.agentHostname || null,
+ status: stack.status,
+ serviceCount: stack.serviceCount || 0,
+ runningCount: stack.runningCount || 0,
+ composeContent: composeContent || null,
+ envContent: envContent || null,
+ lastPolled: null, // Explicitly null for new migration
+ createdAt: ensureDateObject(stack.createdAt) || new Date(), // Convert ISO string to Date
+ updatedAt: ensureDateObject(stack.updatedAt) || new Date() // Convert ISO string to Date
+ };
+
+ await db.insert(stacksTable).values(dbInsertValues);
+ migratedCount++;
+ console.log(`Migrated stack: ${stack.name}`);
+ } catch (error) {
+ const errorMsg = `Failed to migrate stack ${stack.name}: ${error}`;
+ console.error(errorMsg);
+ errors.push(errorMsg);
+ }
+ }
+ // ...existing code...
+ } catch (error) {
+ console.error('Failed to migrate stacks to database:', error);
+ return {
+ success: false,
+ migratedCount,
+ errors: [...errors, `Migration failed: ${String(error)}`]
+ };
+ }
+ // Add return statement if it was missing from the original snippet
+ return { success: errors.length === 0, migratedCount, errors };
+}
+
+/**
+ * Gets a stack from database by ID
+ */
+export async function getStackByIdFromDb(id: string): Promise<Stack | null> {
+ try {
+ const result = await db.select().from(stacksTable).where(eq(stacksTable.id, id)).limit(1);
+ if (result.length === 0) return null;
+ const dbStack = result[0]; // dbStack.createdAt, .updatedAt, .lastPolled are Date objects or null
+
+ return {
+ id: dbStack.id,
+ name: dbStack.name,
+ serviceCount: dbStack.serviceCount,
+ runningCount: dbStack.runningCount,
+ status: dbStack.status as 'running' | 'stopped' | 'partially running' | 'unknown',
+ isExternal: dbStack.isExternal,
+ isLegacy: dbStack.isLegacy,
+ isRemote: dbStack.isRemote,
+ agentId: dbStack.agentId || undefined,
+ agentHostname: dbStack.agentHostname || undefined,
+ createdAt: dbStack.createdAt.toISOString(), // Convert Date to ISO String
+ updatedAt: dbStack.updatedAt.toISOString(), // Convert Date to ISO String
+ lastPolled: dbStack.lastPolled ? dbStack.lastPolled.toISOString() : undefined, // Convert Date to ISO String
+ composeContent: dbStack.composeContent || undefined,
+ envContent: dbStack.envContent || undefined
+ };
+ } catch (error) {
+ console.error('Failed to get stack from database:', error);
+ throw error;
+ }
+}
+
+/**
+ * Gets a stack from database by name
+ */
+export async function getStackByNameFromDb(name: string): Promise<Stack | null> {
+ try {
+ const result = await db.select().from(stacksTable).where(eq(stacksTable.name, name)).limit(1);
+ if (result.length === 0) return null;
+ const dbStack = result[0];
+
+ return {
+ id: dbStack.id,
+ name: dbStack.name,
+ serviceCount: dbStack.serviceCount,
+ runningCount: dbStack.runningCount,
+ status: dbStack.status as 'running' | 'stopped' | 'partially running' | 'unknown',
+ isExternal: dbStack.isExternal,
+ isLegacy: dbStack.isLegacy,
+ isRemote: dbStack.isRemote,
+ agentId: dbStack.agentId || undefined,
+ agentHostname: dbStack.agentHostname || undefined,
+ createdAt: dbStack.createdAt.toISOString(),
+ updatedAt: dbStack.updatedAt.toISOString(),
+ lastPolled: dbStack.lastPolled ? dbStack.lastPolled.toISOString() : undefined,
+ composeContent: dbStack.composeContent || undefined,
+ envContent: dbStack.envContent || undefined
+ };
+ } catch (error) {
+ console.error('Failed to get stack by name from database:', error);
+ throw error;
+ }
+}
+
+/**
+ * Lists all stacks from database
+ */
+export async function listStacksFromDb(): Promise<Stack[]> {
+ try {
+ const result = await db.select().from(stacksTable);
+
+ return result.map((dbStack) => ({
+ id: dbStack.id,
+ name: dbStack.name,
+ serviceCount: dbStack.serviceCount,
+ runningCount: dbStack.runningCount,
+ status: dbStack.status as 'running' | 'stopped' | 'partially running' | 'unknown',
+ isExternal: dbStack.isExternal,
+ isLegacy: dbStack.isLegacy,
+ isRemote: dbStack.isRemote,
+ agentId: dbStack.agentId || undefined,
+ agentHostname: dbStack.agentHostname || undefined,
+ createdAt: dbStack.createdAt.toISOString(),
+ updatedAt: dbStack.updatedAt.toISOString(),
+ composeContent: dbStack.composeContent || undefined,
+ envContent: dbStack.envContent || undefined,
+ services: []
+ }));
+ } catch (error) {
+ console.error('Failed to list stacks from database:', error);
+ throw error;
+ }
+}
+
+/**
+ * Saves a stack to database (create or update)
+ */
+export async function saveStackToDb(stack: Stack): Promise<Stack> {
+ // Assuming Stack type uses ISO strings for dates
+ try {
+ const now = new Date();
+ // Ensure createdAt, updatedAt, and lastPolled are Date objects for DB
+ const createdAtForDb = ensureDateObject(stack.createdAt) || now;
+ const lastPolledForDb = ensureDateObject(stack.lastPolled);
+
+ const dataToSave = {
+ id: stack.id,
+ name: stack.name,
+ dirName: stack.dirName || stack.id,
+ path: stack.path || path.join(STACKS_DIR, stack.id), // Ensure path is provided or derived
+ isExternal: typeof stack.isExternal === 'boolean' ? stack.isExternal : false,
+ isLegacy: typeof stack.isLegacy === 'boolean' ? stack.isLegacy : false,
+ isRemote: typeof stack.isRemote === 'boolean' ? stack.isRemote : false,
+ agentId: stack.agentId || null,
+ agentHostname: stack.agentHostname || null,
+ status: stack.status,
+ serviceCount: stack.serviceCount || 0,
+ runningCount: stack.runningCount || 0,
+ composeContent: stack.composeContent || null,
+ envContent: stack.envContent || null,
+ lastPolled: lastPolledForDb
+ };
+
+ const existing = await db.select({ id: stacksTable.id }).from(stacksTable).where(eq(stacksTable.id, stack.id)).limit(1);
+
+ if (existing.length > 0) {
+ await db
+ .update(stacksTable)
+ .set({
+ ...dataToSave,
+ updatedAt: now // Always update 'updatedAt' on modification
+ })
+ .where(eq(stacksTable.id, stack.id));
+ } else {
+ await db.insert(stacksTable).values({
+ ...dataToSave,
+ createdAt: createdAtForDb, // Set 'createdAt' for new records
+ updatedAt: now // Set 'updatedAt' for new records
+ });
+ }
+ // Return the stack. Its date fields are strings. If you need to reflect the exact DB Date objects,
+ // you might re-fetch or update the stack object.
+ return stack;
+ } catch (error) {
+ console.error('Failed to save stack to database:', error);
+ throw error;
+ }
+}
+
+/**
+ * Updates stack runtime information (status, service counts, last polled)
+ */
+export async function updateStackRuntimeInfoInDb(
+ id: string,
+ updates: {
+ status?: 'running' | 'stopped' | 'partially running' | 'unknown';
+ serviceCount?: number;
+ runningCount?: number;
+ lastPolled?: Date | string | number; // Input can be flexible
+ }
+): Promise<void> {
+ try {
+ const updateData: Partial<typeof stacksTable.$inferInsert> = {
+ updatedAt: new Date() // Pass Date object directly
+ };
+
+ if (updates.status !== undefined) updateData.status = updates.status;
+ if (updates.serviceCount !== undefined) updateData.serviceCount = updates.serviceCount;
+ if (updates.runningCount !== undefined) updateData.runningCount = updates.runningCount;
+
+ if (updates.lastPolled !== undefined) {
+ updateData.lastPolled = ensureDateObject(updates.lastPolled);
+ }
+
+ await db.update(stacksTable).set(updateData).where(eq(stacksTable.id, id));
+ } catch (error) {
+ console.error('Failed to update stack runtime info in database:', error);
+ throw error;
+ }
+}
+
+/**
+ * Updates stack content (compose and env files)
+ */
+export async function updateStackContentInDb(
+ id: string,
+ updates: {
+ composeContent?: string;
+ envContent?: string;
+ }
+): Promise<void> {
+ try {
+ const updateData: Partial<typeof stacksTable.$inferInsert> = {
+ updatedAt: new Date() // Pass Date object
+ };
+
+ if (updates.composeContent !== undefined) updateData.composeContent = updates.composeContent;
+ if (updates.envContent !== undefined) updateData.envContent = updates.envContent;
+
+ await db.update(stacksTable).set(updateData).where(eq(stacksTable.id, id));
+ } catch (error) {
+ console.error('Failed to update stack content in database:', error);
+ throw error;
+ }
+}
+
+/**
+ * Deletes a stack from database
+ */
+export async function deleteStackFromDb(id: string): Promise<boolean> {
+ try {
+ await db.delete(stacksTable).where(eq(stacksTable.id, id));
+ return true;
+ } catch (error) {
+ console.error('Failed to delete stack from database:', error);
+ throw error;
+ }
+}
+
+/**
+ * Gets stacks by agent ID (for remote stacks)
+ */
+export async function getStacksByAgentIdFromDb(agentId: string): Promise<Stack[]> {
+ try {
+ const result = await db.select().from(stacksTable).where(eq(stacksTable.agentId, agentId));
+ return result.map((dbStack) => ({
+ id: dbStack.id,
+ name: dbStack.name,
+ serviceCount: dbStack.serviceCount,
+ runningCount: dbStack.runningCount,
+ status: dbStack.status as 'running' | 'stopped' | 'partially running' | 'unknown',
+ isExternal: dbStack.isExternal,
+ isLegacy: dbStack.isLegacy,
+ isRemote: dbStack.isRemote,
+ agentId: dbStack.agentId || undefined,
+ agentHostname: dbStack.agentHostname || undefined,
+ createdAt: dbStack.createdAt.toISOString(),
+ updatedAt: dbStack.updatedAt.toISOString(),
+ lastPolled: dbStack.lastPolled ? dbStack.lastPolled.toISOString() : undefined,
+ composeContent: dbStack.composeContent || undefined,
+ envContent: dbStack.envContent || undefined
+ }));
+ } catch (error) {
+ console.error('Failed to get stacks by agent ID from database:', error);
+ throw error;
+ }
+}
+
+/**
+ * Updates stack auto-update setting
+ */
+export async function updateStackAutoUpdateInDb(id: string, autoUpdate: boolean): Promise<void> {
+ try {
+ await db
+ .update(stacksTable)
+ .set({
+ autoUpdate,
+ updatedAt: new Date() // Pass Date object
+ })
+ .where(eq(stacksTable.id, id));
+ } catch (error) {
+ console.error('Failed to update stack auto-update setting in database:', error);
+ throw error;
+ }
+}
+
+/**
+ * Gets stacks that have auto-update enabled
+ */
+export async function getAutoUpdateStacksFromDb(): Promise<Stack[]> {
+ try {
+ const result = await db.select().from(stacksTable).where(eq(stacksTable.autoUpdate, true));
+ return result.map((dbStack) => ({
+ id: dbStack.id,
+ name: dbStack.name,
+ serviceCount: dbStack.serviceCount,
+ runningCount: dbStack.runningCount,
+ status: dbStack.status as 'running' | 'stopped' | 'partially running' | 'unknown',
+ isExternal: dbStack.isExternal,
+ isLegacy: dbStack.isLegacy,
+ isRemote: dbStack.isRemote,
+ agentId: dbStack.agentId || undefined,
+ agentHostname: dbStack.agentHostname || undefined,
+ createdAt: dbStack.createdAt.toISOString(),
+ updatedAt: dbStack.updatedAt.toISOString(),
+ lastPolled: dbStack.lastPolled ? dbStack.lastPolled.toISOString() : undefined,
+ composeContent: dbStack.composeContent || undefined,
+ envContent: dbStack.envContent || undefined
+ }));
+ } catch (error) {
+ console.error('Failed to get auto-update stacks from database:', error);
+ throw error;
+ }
+}
diff --git a/src/lib/services/database/deployment-db-service.ts b/src/lib/services/database/deployment-db-service.ts
new file mode 100644
index 000000000..fec918eae
--- /dev/null
+++ b/src/lib/services/database/deployment-db-service.ts
@@ -0,0 +1,328 @@
+import { eq } from 'drizzle-orm';
+import { deploymentsTable } from '../../../db/schema';
+import type { Deployment } from '$lib/types/deployment.type';
+import fs from 'node:fs/promises';
+import path from 'node:path';
+import { BASE_PATH } from '../paths-service';
+import { db } from '../../../db';
+
+const DEPLOYMENTS_DIR = path.join(BASE_PATH, 'deployments');
+
+/**
+ * Migrates deployments from file-based storage to database
+ * @param backupOldFiles - Whether to backup the old deployment files (default: true)
+ * @returns Promise<{ success: boolean, migratedCount: number, errors: string[] }>
+ */
+export async function migrateDeploymentsToDatabase(backupOldFiles: boolean = true): Promise<{
+ success: boolean;
+ migratedCount: number;
+ errors: string[];
+}> {
+ const errors: string[] = [];
+ let migratedCount = 0;
+
+ try {
+ console.log('Starting deployment migration from file to database...');
+
+ // Get current deployments from file-based system
+ const fileDeployments = await getDeploymentsFromFiles();
+ console.log(`Found ${fileDeployments.length} deployments in file system`);
+
+ if (fileDeployments.length === 0) {
+ console.log('No deployments found in file system. Migration completed.');
+ return { success: true, migratedCount: 0, errors: [] };
+ }
+
+ // Check if deployments already exist in database
+ const existingDeployments = await db.select().from(deploymentsTable).limit(1);
+
+ if (existingDeployments.length > 0) {
+ console.log('Deployments already exist in database. Migration aborted.');
+ return { success: false, migratedCount: 0, errors: ['Deployments already exist in database'] };
+ }
+
+ // Migrate each deployment to database
+ for (const deployment of fileDeployments) {
+ try {
+ const insertData = {
+ id: deployment.id,
+ name: deployment.name,
+ type: deployment.type,
+ status: deployment.status,
+ agentId: deployment.agentId,
+ taskId: deployment.taskId || null,
+ error: deployment.error || null,
+ metadata: deployment.metadata ? JSON.stringify(deployment.metadata) : null,
+ createdAt: new Date(deployment.createdAt),
+ updatedAt: deployment.updatedAt ? new Date(deployment.updatedAt) : new Date(deployment.createdAt)
+ } satisfies typeof deploymentsTable.$inferInsert;
+
+ await db.insert(deploymentsTable).values(insertData);
+
+ migratedCount++;
+ console.log(`Migrated deployment: ${deployment.name} (${deployment.id})`);
+ } catch (error) {
+ const errorMsg = `Failed to migrate deployment ${deployment.name}: ${error}`;
+ console.error(errorMsg);
+ errors.push(errorMsg);
+ }
+ }
+
+ console.log(`Successfully migrated ${migratedCount} deployments to database`);
+
+ // Backup old deployment files if requested
+ if (backupOldFiles && migratedCount > 0) {
+ try {
+ const backupDir = path.join(DEPLOYMENTS_DIR, `backup-${Date.now()}`);
+ await fs.mkdir(backupDir, { recursive: true });
+
+ const files = await fs.readdir(DEPLOYMENTS_DIR);
+ const deploymentFiles = files.filter((file) => file.endsWith('.json'));
+
+ for (const file of deploymentFiles) {
+ const sourcePath = path.join(DEPLOYMENTS_DIR, file);
+ const backupPath = path.join(backupDir, file);
+
+ try {
+ await fs.copyFile(sourcePath, backupPath);
+ } catch (copyError) {
+ console.warn(`Could not backup deployment file ${file}:`, copyError);
+ }
+ }
+
+ console.log(`Deployment files backed up to: ${backupDir}`);
+ } catch (backupError) {
+ console.warn('Could not create backup of deployment files:', backupError);
+ errors.push(`Backup failed: ${backupError}`);
+ }
+ }
+
+ return {
+ success: errors.length === 0,
+ migratedCount,
+ errors
+ };
+ } catch (error) {
+ console.error('Failed to migrate deployments to database:', error);
+ return {
+ success: false,
+ migratedCount,
+ errors: [...errors, `Migration failed: ${error}`]
+ };
+ }
+}
+
+/**
+ * Read deployments directly from file system (for migration purposes)
+ */
+async function getDeploymentsFromFiles(): Promise<Deployment[]> {
+ try {
+ // Check if deployments directory exists
+ const dirExists = await fs
+ .access(DEPLOYMENTS_DIR)
+ .then(() => true)
+ .catch(() => false);
+
+ if (!dirExists) {
+ console.log('No deployments directory found for migration');
+ return [];
+ }
+
+ const files = await fs.readdir(DEPLOYMENTS_DIR);
+ const deployments: Deployment[] = [];
+
+ for (const file of files) {
+ if (file.endsWith('.json')) {
+ try {
+ const filePath = path.join(DEPLOYMENTS_DIR, file);
+ const fileContent = await fs.readFile(filePath, 'utf-8');
+ const deployment = JSON.parse(fileContent) as Deployment;
+ deployments.push(deployment);
+ } catch (error) {
+ console.warn(`Could not read deployment file ${file}:`, error);
+ }
+ }
+ }
+
+ return deployments;
+ } catch (error) {
+ console.error('Error reading deployments from file system:', error);
+ return [];
+ }
+}
+
+/**
+ * Gets deployments from database
+ */
+export async function getDeploymentsFromDb(agentId?: string): Promise<Deployment[]> {
+ try {
+ const query = agentId ? db.select().from(deploymentsTable).where(eq(deploymentsTable.agentId, agentId)) : db.select().from(deploymentsTable);
+
+ const dbDeployments = await query;
+
+ return dbDeployments.map((dbDeployment) => ({
+ id: dbDeployment.id,
+ name: dbDeployment.name,
+ type: dbDeployment.type,
+ status: dbDeployment.status,
+ agentId: dbDeployment.agentId,
+ taskId: dbDeployment.taskId || undefined,
+ error: dbDeployment.error || undefined,
+ metadata: dbDeployment.metadata ? JSON.parse(dbDeployment.metadata as string) : undefined,
+ createdAt: dbDeployment.createdAt.toISOString(),
+ updatedAt: dbDeployment.updatedAt?.toISOString()
+ })) as Deployment[];
+ } catch (error) {
+ console.error('Failed to get deployments from database:', error);
+ return [];
+ }
+}
+
+/**
+ * Gets a single deployment from database
+ */
+export async function getDeploymentFromDb(deploymentId: string): Promise<Deployment | null> {
+ try {
+ const dbDeployments = await db.select().from(deploymentsTable).where(eq(deploymentsTable.id, deploymentId)).limit(1);
+
+ if (dbDeployments.length === 0) {
+ return null;
+ }
+
+ const dbDeployment = dbDeployments[0];
+
+ return {
+ id: dbDeployment.id,
+ name: dbDeployment.name,
+ type: dbDeployment.type,
+ status: dbDeployment.status,
+ agentId: dbDeployment.agentId,
+ taskId: dbDeployment.taskId || undefined,
+ error: dbDeployment.error || undefined,
+ metadata: dbDeployment.metadata ? JSON.parse(dbDeployment.metadata as string) : undefined,
+ createdAt: dbDeployment.createdAt.toISOString(),
+ updatedAt: dbDeployment.updatedAt?.toISOString()
+ } as Deployment;
+ } catch (error) {
+ console.error('Failed to get deployment from database:', error);
+ return null;
+ }
+}
+
+/**
+ * Saves a deployment to database (create or update)
+ */
+export async function saveDeploymentToDb(deployment: Deployment): Promise<Deployment> {
+ try {
+ const now = new Date();
+ const createdAtForDb = new Date(deployment.createdAt);
+
+ const deploymentData = {
+ id: deployment.id,
+ name: deployment.name,
+ type: deployment.type,
+ status: deployment.status,
+ agentId: deployment.agentId,
+ taskId: deployment.taskId || null,
+ error: deployment.error || null,
+ metadata: deployment.metadata ? JSON.stringify(deployment.metadata) : null,
+ createdAt: createdAtForDb,
+ updatedAt: now
+ };
+
+ // Check if deployment exists
+ const existing = await db.select({ id: deploymentsTable.id }).from(deploymentsTable).where(eq(deploymentsTable.id, deployment.id)).limit(1);
+
+ if (existing.length > 0) {
+ // Update existing deployment
+ await db
+ .update(deploymentsTable)
+ .set({
+ ...deploymentData,
+ updatedAt: now
+ })
+ .where(eq(deploymentsTable.id, deployment.id));
+ } else {
+ // Insert new deployment
+ await db.insert(deploymentsTable).values(deploymentData);
+ }
+
+ return deployment;
+ } catch (error) {
+ console.error('Failed to save deployment to database:', error);
+ throw error;
+ }
+}
+
+/**
+ * Updates deployment status and related fields
+ */
+export async function updateDeploymentInDb(
+ deploymentId: string,
+ updates: {
+ status?: 'pending' | 'running' | 'stopped' | 'failed' | 'completed';
+ error?: string;
+ metadata?: any;
+ }
+): Promise<void> {
+ try {
+ const updateData: Partial<typeof deploymentsTable.$inferInsert> = {
+ updatedAt: new Date()
+ };
+
+ if (updates.status !== undefined) updateData.status = updates.status;
+ if (updates.error !== undefined) updateData.error = updates.error;
+ if (updates.metadata !== undefined) {
+ updateData.metadata = updates.metadata ? JSON.stringify(updates.metadata) : null;
+ }
+
+ await db.update(deploymentsTable).set(updateData).where(eq(deploymentsTable.id, deploymentId));
+ } catch (error) {
+ console.error('Failed to update deployment in database:', error);
+ throw error;
+ }
+}
+
+/**
+ * Deletes a deployment from database
+ */
+export async function deleteDeploymentFromDb(deploymentId: string): Promise<boolean> {
+ try {
+ await db.delete(deploymentsTable).where(eq(deploymentsTable.id, deploymentId));
+ return true;
+ } catch (error) {
+ console.error('Failed to delete deployment from database:', error);
+ return false;
+ }
+}
+
+/**
+ * Gets deployments by task ID
+ */
+export async function getDeploymentByTaskIdFromDb(taskId: string): Promise<Deployment | null> {
+ try {
+ const dbDeployments = await db.select().from(deploymentsTable).where(eq(deploymentsTable.taskId, taskId)).limit(1);
+
+ if (dbDeployments.length === 0) {
+ return null;
+ }
+
+ const dbDeployment = dbDeployments[0];
+
+ return {
+ id: dbDeployment.id,
+ name: dbDeployment.name,
+ type: dbDeployment.type,
+ status: dbDeployment.status,
+ agentId: dbDeployment.agentId,
+ taskId: dbDeployment.taskId || undefined,
+ error: dbDeployment.error || undefined,
+ metadata: dbDeployment.metadata ? JSON.parse(dbDeployment.metadata as string) : undefined,
+ createdAt: dbDeployment.createdAt.toISOString(),
+ updatedAt: dbDeployment.updatedAt?.toISOString()
+ } as Deployment;
+ } catch (error) {
+ console.error('Failed to get deployment by task ID from database:', error);
+ return null;
+ }
+}
diff --git a/src/lib/services/database/settings-db-service.ts b/src/lib/services/database/settings-db-service.ts
new file mode 100644
index 000000000..0ec9ab22d
--- /dev/null
+++ b/src/lib/services/database/settings-db-service.ts
@@ -0,0 +1,202 @@
+import { eq } from 'drizzle-orm';
+import { settingsTable } from '../../../db/schema';
+import type { Settings } from '$lib/types/settings.type';
+import fs from 'node:fs/promises';
+import path from 'node:path';
+import { SETTINGS_DIR } from '../paths-service';
+import { decrypt } from '../encryption-service';
+import { db } from '../../../db';
+
+/**
+ * Read settings directly from file system (for migration purposes)
+ */
+async function getSettingsFromFile(): Promise<Settings | null> {
+ try {
+ const settingsFilePath = path.join(SETTINGS_DIR, 'settings.dat');
+ console.log(`Attempting to read settings from: ${settingsFilePath}`);
+
+ // Check if file exists
+ const fileExists = await fs
+ .access(settingsFilePath)
+ .then(() => true)
+ .catch(() => false);
+ console.log(`Settings file exists: ${fileExists}`);
+
+ if (!fileExists) {
+ console.log('No settings file found for migration');
+ return null;
+ }
+
+ // Read the encrypted settings file
+ const fileContent = await fs.readFile(settingsFilePath, 'utf8');
+ console.log(`Read settings file, length: ${fileContent.length}`);
+
+ const settingsData = JSON.parse(fileContent);
+ console.log(`Parsed settings data, keys: ${Object.keys(settingsData).join(', ')}`);
+
+ // Decrypt the sensitive data if it exists
+ let decryptedData = {};
+ if (settingsData._encrypted) {
+ console.log('Decrypting sensitive settings data...');
+ decryptedData = await decrypt(settingsData._encrypted);
+ console.log(`Decrypted data keys: ${Object.keys(decryptedData).join(', ')}`);
+ }
+
+ // Combine non-sensitive and decrypted sensitive data
+ const { _encrypted, ...nonSensitiveData } = settingsData;
+ const completeSettings = {
+ ...nonSensitiveData,
+ ...decryptedData
+ };
+
+ console.log(`Complete settings keys: ${Object.keys(completeSettings).join(', ')}`);
+ console.log(`OIDC settings found: ${completeSettings.auth?.oidc ? 'yes' : 'no'}`);
+
+ return completeSettings as Settings;
+ } catch (error) {
+ console.error('Error reading settings from file:', error);
+ return null;
+ }
+}
+
+/**
+ * Migrates settings from file-based storage to database
+ * @param backupOldFile - Whether to backup the old settings file (default: true)
+ * @returns Promise<boolean> - Success status
+ */
+export async function migrateSettingsToDatabase(backupOldFile: boolean = true): Promise<boolean> {
+ try {
+ console.log('Starting settings migration from file to database...');
+
+ // Get current settings from file-based system (directly from file)
+ const fileSettings = await getSettingsFromFile();
+
+ if (!fileSettings) {
+ console.log('No settings found in file system. Migration completed.');
+ return true; // Not an error, just nothing to migrate
+ }
+
+ console.log('Retrieved settings from file system');
+ console.log(`OIDC enabled: ${fileSettings.auth?.oidcEnabled}`);
+ console.log(`OIDC client ID: ${fileSettings.auth?.oidc?.clientId ? 'present' : 'missing'}`);
+
+ // Check if settings already exist in database
+ const existingSettings = await db.select().from(settingsTable).limit(1);
+
+ if (existingSettings.length > 0) {
+ console.log('Settings already exist in database. Migration aborted.');
+ return false;
+ }
+
+ // Insert settings into database
+ await db.insert(settingsTable).values({
+ dockerHost: fileSettings.dockerHost,
+ stacksDirectory: fileSettings.stacksDirectory,
+ autoUpdate: fileSettings.autoUpdate,
+ autoUpdateInterval: fileSettings.autoUpdateInterval,
+ pollingEnabled: fileSettings.pollingEnabled,
+ pollingInterval: fileSettings.pollingInterval,
+ pruneMode: fileSettings.pruneMode,
+ registryCredentials: JSON.stringify(fileSettings.registryCredentials || []),
+ templateRegistries: JSON.stringify(fileSettings.templateRegistries || []),
+ auth: JSON.stringify(fileSettings.auth),
+ onboarding: fileSettings.onboarding ? JSON.stringify(fileSettings.onboarding) : null,
+ baseServerUrl: fileSettings.baseServerUrl,
+ maturityThresholdDays: fileSettings.maturityThresholdDays
+ });
+
+ console.log('Settings successfully migrated to database');
+ console.log(`OIDC settings migrated: ${fileSettings.auth?.oidcEnabled ? 'enabled' : 'disabled'}`);
+
+ // Backup old settings file if requested
+ if (backupOldFile) {
+ const settingsFilePath = path.join(SETTINGS_DIR, 'settings.dat');
+ const backupPath = path.join(SETTINGS_DIR, `settings.dat.backup.${Date.now()}`);
+
+ try {
+ await fs.access(settingsFilePath);
+ await fs.copyFile(settingsFilePath, backupPath);
+ console.log(`Settings file backed up to: ${backupPath}`);
+ } catch (error) {
+ console.warn('Could not backup settings file:', error);
+ }
+ }
+
+ return true;
+ } catch (error) {
+ console.error('Failed to migrate settings to database:', error);
+ throw error;
+ }
+}
+
+/**
+ * Gets settings from database
+ */
+export async function getSettingsFromDb(): Promise<Settings | null> {
+ try {
+ const result = await db.select().from(settingsTable).limit(1);
+
+ if (result.length === 0) {
+ return null;
+ }
+
+ const dbSettings = result[0];
+
+ return {
+ dockerHost: dbSettings.dockerHost,
+ stacksDirectory: dbSettings.stacksDirectory,
+ autoUpdate: dbSettings.autoUpdate,
+ autoUpdateInterval: dbSettings.autoUpdateInterval,
+ pollingEnabled: dbSettings.pollingEnabled,
+ pollingInterval: dbSettings.pollingInterval,
+ pruneMode: dbSettings.pruneMode as 'all' | 'dangling' | undefined,
+ registryCredentials: JSON.parse(dbSettings.registryCredentials as string),
+ templateRegistries: JSON.parse(dbSettings.templateRegistries as string),
+ auth: JSON.parse(dbSettings.auth as string),
+ onboarding: dbSettings.onboarding ? JSON.parse(dbSettings.onboarding as string) : undefined,
+ baseServerUrl: dbSettings.baseServerUrl || undefined,
+ maturityThresholdDays: dbSettings.maturityThresholdDays
+ };
+ } catch (error) {
+ console.error('Failed to get settings from database:', error);
+ throw error;
+ }
+}
+
+/**
+ * Saves settings to database
+ */
+export async function saveSettingsToDb(settings: Settings): Promise<void> {
+ try {
+ // Check if settings exist
+ const existing = await db.select().from(settingsTable).limit(1);
+
+ const settingsData = {
+ dockerHost: settings.dockerHost,
+ stacksDirectory: settings.stacksDirectory,
+ autoUpdate: settings.autoUpdate,
+ autoUpdateInterval: settings.autoUpdateInterval,
+ pollingEnabled: settings.pollingEnabled,
+ pollingInterval: settings.pollingInterval,
+ pruneMode: settings.pruneMode,
+ registryCredentials: JSON.stringify(settings.registryCredentials || []),
+ templateRegistries: JSON.stringify(settings.templateRegistries || []),
+ auth: JSON.stringify(settings.auth),
+ onboarding: settings.onboarding ? JSON.stringify(settings.onboarding) : null,
+ baseServerUrl: settings.baseServerUrl,
+ maturityThresholdDays: settings.maturityThresholdDays,
+ updatedAt: new Date()
+ };
+
+ if (existing.length > 0) {
+ // Update existing settings
+ await db.update(settingsTable).set(settingsData).where(eq(settingsTable.id, existing[0].id));
+ } else {
+ // Insert new settings
+ await db.insert(settingsTable).values(settingsData);
+ }
+ } catch (error) {
+ console.error('Failed to save settings to database:', error);
+ throw error;
+ }
+}
diff --git a/src/lib/services/database/user-db-service.ts b/src/lib/services/database/user-db-service.ts
new file mode 100644
index 000000000..b5c3dc976
--- /dev/null
+++ b/src/lib/services/database/user-db-service.ts
@@ -0,0 +1,288 @@
+import { eq } from 'drizzle-orm';
+import { usersTable } from '../../../db/schema';
+import { listUsersFromFile as getFileUsers } from '../user-service';
+import type { User } from '$lib/types/user.type';
+import fs from 'node:fs/promises';
+import path from 'node:path';
+import { USER_DIR } from '../paths-service';
+import { db } from '../../../db';
+
+/**
+ * Migrates users from file-based storage to database
+ * @param backupOldFiles - Whether to backup the old user files (default: true)
+ * @returns Promise<{ success: boolean, migratedCount: number, errors: string[] }>
+ */
+export async function migrateUsersToDatabase(backupOldFiles: boolean = true): Promise<{
+ success: boolean;
+ migratedCount: number;
+ errors: string[];
+}> {
+ const errors: string[] = [];
+ let migratedCount = 0;
+
+ try {
+ console.log('Starting user migration from file to database...');
+
+ // Get current users from file-based system (now correctly from files!)
+ const fileUsers = await getFileUsers();
+ console.log(`Found ${fileUsers.length} users in file system`);
+
+ if (fileUsers.length === 0) {
+ console.log('No users found in file system. Migration completed.');
+ return { success: true, migratedCount: 0, errors: [] };
+ }
+
+ // Check if users already exist in database
+ const existingUsers = await db.select().from(usersTable).limit(1);
+
+ if (existingUsers.length > 0) {
+ console.log('Users already exist in database. Migration aborted.');
+ return { success: false, migratedCount: 0, errors: ['Users already exist in database'] };
+ }
+
+ // Migrate each user to database
+ for (const user of fileUsers) {
+ try {
+ const insertData = {
+ id: user.id,
+ username: user.username,
+ passwordHash: user.passwordHash || null,
+ displayName: user.displayName || null,
+ email: user.email || null,
+ roles: JSON.stringify(user.roles || []),
+ requirePasswordChange: user.requirePasswordChange || false,
+ oidcSubjectId: user.oidcSubjectId || null,
+ lastLogin: user.lastLogin ? new Date(user.lastLogin) : null,
+ createdAt: new Date(user.createdAt),
+ updatedAt: user.updatedAt ? new Date(user.updatedAt) : new Date()
+ } satisfies typeof usersTable.$inferInsert;
+
+ await db.insert(usersTable).values(insertData);
+
+ migratedCount++;
+ console.log(`Migrated user: ${user.username}`);
+ } catch (error) {
+ const errorMsg = `Failed to migrate user ${user.username}: ${error}`;
+ console.error(errorMsg);
+ errors.push(errorMsg);
+ }
+ }
+
+ console.log(`Successfully migrated ${migratedCount} users to database`);
+
+ // Backup old user files if requested
+ if (backupOldFiles && migratedCount > 0) {
+ try {
+ const backupDir = path.join(USER_DIR, `backup-${Date.now()}`);
+ await fs.mkdir(backupDir, { recursive: true });
+
+ const files = await fs.readdir(USER_DIR);
+ const userFiles = files.filter((file) => file.endsWith('.dat'));
+
+ for (const file of userFiles) {
+ const sourcePath = path.join(USER_DIR, file);
+ const backupPath = path.join(backupDir, file);
+
+ try {
+ await fs.copyFile(sourcePath, backupPath);
+ } catch (copyError) {
+ console.warn(`Could not backup user file ${file}:`, copyError);
+ }
+ }
+
+ console.log(`User files backed up to: ${backupDir}`);
+ } catch (backupError) {
+ console.warn('Could not create backup of user files:', backupError);
+ errors.push(`Backup failed: ${backupError}`);
+ }
+ }
+
+ return {
+ success: errors.length === 0,
+ migratedCount,
+ errors
+ };
+ } catch (error) {
+ console.error('Failed to migrate users to database:', error);
+ return {
+ success: false,
+ migratedCount,
+ errors: [...errors, `Migration failed: ${error}`]
+ };
+ }
+}
+
+/**
+ * Gets a user from database by ID
+ */
+export async function getUserByIdFromDb(id: string): Promise<User | null> {
+ try {
+ const result = await db.select().from(usersTable).where(eq(usersTable.id, id)).limit(1);
+
+ if (result.length === 0) {
+ return null;
+ }
+
+ const dbUser = result[0];
+
+ return {
+ id: dbUser.id,
+ username: dbUser.username,
+ passwordHash: dbUser.passwordHash || undefined,
+ displayName: dbUser.displayName || undefined,
+ email: dbUser.email || undefined,
+ roles: JSON.parse(dbUser.roles as string),
+ requirePasswordChange: dbUser.requirePasswordChange,
+ oidcSubjectId: dbUser.oidcSubjectId || undefined,
+ lastLogin: dbUser.lastLogin ? dbUser.lastLogin.toISOString() : undefined,
+ createdAt: dbUser.createdAt.toISOString(),
+ updatedAt: dbUser.updatedAt ? dbUser.updatedAt.toISOString() : undefined
+ };
+ } catch (error) {
+ console.error('Failed to get user from database:', error);
+ throw error;
+ }
+}
+
+/**
+ * Gets a user from database by username
+ */
+export async function getUserByUsernameFromDb(username: string): Promise<User | null> {
+ try {
+ const result = await db.select().from(usersTable).where(eq(usersTable.username, username)).limit(1);
+
+ if (result.length === 0) {
+ return null;
+ }
+
+ const dbUser = result[0];
+
+ return {
+ id: dbUser.id,
+ username: dbUser.username,
+ passwordHash: dbUser.passwordHash || undefined,
+ displayName: dbUser.displayName || undefined,
+ email: dbUser.email || undefined,
+ roles: JSON.parse(dbUser.roles as string),
+ requirePasswordChange: dbUser.requirePasswordChange,
+ oidcSubjectId: dbUser.oidcSubjectId || undefined,
+ // Fix: Handle Date objects from Drizzle timestamp mode properly
+ lastLogin: dbUser.lastLogin ? dbUser.lastLogin.toISOString() : undefined,
+ createdAt: dbUser.createdAt.toISOString(),
+ updatedAt: dbUser.updatedAt ? dbUser.updatedAt.toISOString() : undefined
+ };
+ } catch (error) {
+ console.error('Failed to get user by username from database:', error);
+ throw error;
+ }
+}
+
+/**
+ * Gets a user from database by OIDC subject ID
+ */
+export async function getUserByOidcSubjectIdFromDb(oidcSubjectId: string): Promise<User | null> {
+ try {
+ const result = await db.select().from(usersTable).where(eq(usersTable.oidcSubjectId, oidcSubjectId)).limit(1);
+
+ if (result.length === 0) {
+ return null;
+ }
+
+ const dbUser = result[0];
+
+ return {
+ id: dbUser.id,
+ username: dbUser.username,
+ passwordHash: dbUser.passwordHash || undefined,
+ displayName: dbUser.displayName || undefined,
+ email: dbUser.email || undefined,
+ roles: JSON.parse(dbUser.roles as string),
+ requirePasswordChange: dbUser.requirePasswordChange,
+ oidcSubjectId: dbUser.oidcSubjectId || undefined,
+ // Fix: Handle Date objects from Drizzle timestamp mode properly
+ lastLogin: dbUser.lastLogin ? dbUser.lastLogin.toISOString() : undefined,
+ createdAt: dbUser.createdAt.toISOString(),
+ updatedAt: dbUser.updatedAt ? dbUser.updatedAt.toISOString() : undefined
+ };
+ } catch (error) {
+ console.error('Failed to get user by OIDC subject ID from database:', error);
+ throw error;
+ }
+}
+
+/**
+ * Lists all users from database
+ */
+export async function listUsersFromDb(): Promise<User[]> {
+ try {
+ const result = await db.select().from(usersTable);
+
+ return result.map((dbUser) => ({
+ id: dbUser.id,
+ username: dbUser.username,
+ passwordHash: dbUser.passwordHash || undefined,
+ displayName: dbUser.displayName || undefined,
+ email: dbUser.email || undefined,
+ roles: JSON.parse(dbUser.roles as string),
+ requirePasswordChange: dbUser.requirePasswordChange,
+ oidcSubjectId: dbUser.oidcSubjectId || undefined,
+ // Fix: Handle Date objects from Drizzle timestamp mode properly
+ lastLogin: dbUser.lastLogin ? dbUser.lastLogin.toISOString() : undefined,
+ createdAt: dbUser.createdAt.toISOString(),
+ updatedAt: dbUser.updatedAt ? dbUser.updatedAt.toISOString() : undefined
+ }));
+ } catch (error) {
+ console.error('Failed to list users from database:', error);
+ throw error;
+ }
+}
+
+/**
+ * Saves a user to database
+ */
+export async function saveUserToDb(user: User): Promise<User> {
+ try {
+ const userData = {
+ id: user.id,
+ username: user.username,
+ passwordHash: user.passwordHash || null,
+ displayName: user.displayName || null,
+ email: user.email || null,
+ roles: JSON.stringify(user.roles || []),
+ requirePasswordChange: user.requirePasswordChange || false,
+ oidcSubjectId: user.oidcSubjectId || null,
+ lastLogin: user.lastLogin ? new Date(user.lastLogin) : null,
+ createdAt: new Date(user.createdAt),
+ updatedAt: new Date()
+ };
+
+ // Check if user exists
+ const existing = await db.select().from(usersTable).where(eq(usersTable.id, user.id)).limit(1);
+
+ if (existing.length > 0) {
+ // Update existing user
+ await db.update(usersTable).set(userData).where(eq(usersTable.id, user.id));
+ } else {
+ // Insert new user
+ await db.insert(usersTable).values(userData);
+ }
+
+ return user;
+ } catch (error) {
+ console.error('Failed to save user to database:', error);
+ throw error;
+ }
+}
+
+/**
+ * Deletes a user from database
+ */
+export async function deleteUserFromDb(id: string): Promise<boolean> {
+ try {
+ const result = await db.delete(usersTable).where(eq(usersTable.id, id));
+ return true;
+ } catch (error) {
+ console.error('Failed to delete user from database:', error);
+ throw error;
+ }
+}
diff --git a/src/lib/services/deployment-service.ts b/src/lib/services/deployment-service.ts
index 567e7d8ef..4ae6a4dac 100644
--- a/src/lib/services/deployment-service.ts
+++ b/src/lib/services/deployment-service.ts
@@ -3,6 +3,7 @@ import path from 'node:path';
import { BASE_PATH } from '$lib/services/paths-service';
import type { Deployment } from '$lib/types/deployment.type';
import { nanoid } from 'nanoid';
+import { getDeploymentsFromDb, getDeploymentFromDb, saveDeploymentToDb, updateDeploymentInDb, deleteDeploymentFromDb, getDeploymentByTaskIdFromDb } from './database/deployment-db-service';
const DEPLOYMENTS_DIR = path.join(BASE_PATH, 'deployments');
@@ -17,26 +18,25 @@ export async function createDeployment(deployment: Omit<Deployment, 'id' | 'crea
updatedAt: new Date().toISOString()
};
- const filePath = path.join(DEPLOYMENTS_DIR, `${newDeployment.id}.json`);
- await fs.writeFile(filePath, JSON.stringify(newDeployment, null, 2));
-
- return newDeployment;
+ return await saveDeploymentToDb(newDeployment);
}
export async function updateDeployment(deploymentId: string, updates: Partial<Deployment>): Promise<Deployment | null> {
try {
- const filePath = path.join(DEPLOYMENTS_DIR, `${deploymentId}.json`);
- const deploymentData = await fs.readFile(filePath, 'utf-8');
- const deployment = JSON.parse(deploymentData);
+ const existingDeployment = await getDeploymentFromDb(deploymentId);
+
+ if (!existingDeployment) {
+ console.error('Deployment not found:', deploymentId);
+ return null;
+ }
const updatedDeployment = {
- ...deployment,
+ ...existingDeployment,
...updates,
updatedAt: new Date().toISOString()
};
- await fs.writeFile(filePath, JSON.stringify(updatedDeployment, null, 2));
- return updatedDeployment;
+ return await saveDeploymentToDb(updatedDeployment);
} catch (error) {
console.error('Error updating deployment:', error);
return null;
@@ -44,47 +44,15 @@ export async function updateDeployment(deploymentId: string, updates: Partial<De
}
export async function getDeployment(deploymentId: string): Promise<Deployment | null> {
- try {
- const filePath = path.join(DEPLOYMENTS_DIR, `${deploymentId}.json`);
- const deploymentData = await fs.readFile(filePath, 'utf-8');
- return JSON.parse(deploymentData);
- } catch (error) {
- return null;
- }
+ return await getDeploymentFromDb(deploymentId);
}
export async function getDeployments(agentId?: string): Promise<Deployment[]> {
- try {
- const files = await fs.readdir(DEPLOYMENTS_DIR);
- const deployments: Deployment[] = [];
-
- for (const file of files) {
- if (file.endsWith('.json')) {
- const deploymentData = await fs.readFile(path.join(DEPLOYMENTS_DIR, file), 'utf-8');
- const deployment = JSON.parse(deploymentData);
-
- if (!agentId || deployment.agentId === agentId) {
- deployments.push(deployment);
- }
- }
- }
-
- return deployments.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
- } catch (error) {
- console.error('Error listing deployments:', error);
- return [];
- }
+ return await getDeploymentsFromDb(agentId);
}
export async function deleteDeployment(deploymentId: string): Promise<boolean> {
- try {
- const filePath = path.join(DEPLOYMENTS_DIR, `${deploymentId}.json`);
- await fs.unlink(filePath);
- return true;
- } catch (error) {
- console.error('Error deleting deployment:', error);
- return false;
- }
+ return await deleteDeploymentFromDb(deploymentId);
}
// Helper functions for creating specific deployment types
@@ -135,33 +103,40 @@ export async function createImageDeployment(agentId: string, imageName: string,
// Update deployment status based on task completion
export async function updateDeploymentFromTask(taskId: string, status: string, result?: any, error?: string): Promise<void> {
try {
- // Find deployment by taskId
- const deployments = await getDeployments();
- const deployment = deployments.find((d) => d.taskId === taskId);
-
- if (deployment) {
- let deploymentStatus: Deployment['status'];
-
- switch (status) {
- case 'completed':
- deploymentStatus = 'completed';
- break;
- case 'failed':
- deploymentStatus = 'failed';
- break;
- case 'running':
- deploymentStatus = 'running';
- break;
- default:
- deploymentStatus = 'pending';
- }
-
- await updateDeployment(deployment.id, {
- status: deploymentStatus,
- error: status === 'failed' ? error : undefined
- });
+ // Find deployment linked to this task
+ const deployment = await getDeploymentByTaskIdFromDb(taskId);
+
+ if (!deployment) {
+ // No deployment linked to this task, nothing to update
+ return;
}
- } catch (err) {
- console.error('Error updating deployment from task:', err);
+
+ // Map task status to deployment status
+ let deploymentStatus: 'pending' | 'running' | 'stopped' | 'failed' | 'completed';
+
+ switch (status) {
+ case 'running':
+ deploymentStatus = 'running';
+ break;
+ case 'completed':
+ deploymentStatus = 'completed';
+ break;
+ case 'failed':
+ deploymentStatus = 'failed';
+ break;
+ default:
+ deploymentStatus = 'pending';
+ }
+
+ // Update deployment in database
+ await updateDeploymentInDb(deployment.id, {
+ status: deploymentStatus,
+ error: error || undefined
+ });
+
+ console.log(`Deployment ${deployment.id} updated to status: ${deploymentStatus}`);
+ } catch (error) {
+ console.error('Failed to update deployment from task:', error);
+ // Don't throw as this shouldn't break the task update
}
}
diff --git a/src/lib/services/docker/auto-update-service.ts b/src/lib/services/docker/auto-update-service.ts
index 185fb0c9e..8a220330e 100644
--- a/src/lib/services/docker/auto-update-service.ts
+++ b/src/lib/services/docker/auto-update-service.ts
@@ -1,5 +1,5 @@
import { listContainers, getContainer, recreateContainer } from './container-service';
-import { listStacks, getStack } from './stack-service';
+import { listStacks, getStack } from './stack-custom-service';
import { redeployStack } from './stack-custom-service';
import { pullImage, getImage } from './image-service';
import { getSettings } from '../settings-service';
diff --git a/src/lib/services/docker/image-service.ts b/src/lib/services/docker/image-service.ts
index f5ade3a50..ccf62204c 100644
--- a/src/lib/services/docker/image-service.ts
+++ b/src/lib/services/docker/image-service.ts
@@ -5,7 +5,8 @@ import { NotFoundError, DockerApiError, RegistryRateLimitError, PublicRegistryEr
import { parseImageNameForRegistry, areRegistriesEquivalent } from '$lib/utils/registry.utils';
import { getSettings } from '$lib/services/settings-service';
import { tryCatch } from '$lib/utils/try-catch';
-import { maturityCache } from './maturity-cache-service';
+import { maturityCache, getImageMaturity, setImageMaturity, getImageMaturityBatch, setImageMaturityBatch } from './maturity-cache-service';
+
let maturityPollingInterval: NodeJS.Timeout | null = null;
/**
@@ -32,6 +33,9 @@ export async function initMaturityPollingScheduler(): Promise<void> {
console.log(`Starting image maturity polling with interval of ${intervalMinutes} minutes`);
+ // Start background cleanup for the cache
+ maturityCache.startBackgroundCleanup(15); // Every 15 minutes
+
// Schedule regular checks
maturityPollingInterval = setInterval(runMaturityChecks, intervalMs);
}
@@ -45,10 +49,13 @@ export async function stopMaturityPollingScheduler(): Promise<void> {
maturityPollingInterval = null;
console.log('Image maturity polling scheduler stopped');
}
+
+ // Stop background cleanup
+ maturityCache.stopBackgroundCleanup();
}
/**
- * Run maturity checks for all images
+ * Run maturity checks for all images - OPTIMIZED VERSION
*/
async function runMaturityChecks(): Promise<void> {
console.log('Running scheduled image maturity checks...');
@@ -60,37 +67,99 @@ async function runMaturityChecks(): Promise<void> {
}
const images = imagesResult.data;
+
+ // Filter out images without proper tags
+ const validImages = images.filter((image) => image.repo !== '<none>' && image.tag !== '<none>');
+
+ console.log(`Found ${validImages.length} valid images to check maturity for`);
+
+ if (validImages.length === 0) {
+ console.log('No valid images found for maturity checking');
+ return;
+ }
+
+ // Extract image IDs for batch processing
+ const imageIds = validImages.map((image) => image.Id);
+
+ // Get cached maturity data in batch
+ const cachedMaturity = getImageMaturityBatch(imageIds);
+
+ // Separate images that need checking vs those that are cached
+ const imagesToCheck: ServiceImage[] = [];
+ const cachedCount = cachedMaturity.size;
+
+ for (const image of validImages) {
+ if (!cachedMaturity.has(image.Id)) {
+ imagesToCheck.push(image);
+ }
+ }
+
+ console.log(`${cachedCount} images found in cache, ${imagesToCheck.length} need fresh checks`);
+
+ if (imagesToCheck.length === 0) {
+ console.log('All images have cached maturity data, no fresh checks needed');
+ return;
+ }
+
+ // Process images in smaller batches to avoid overwhelming registries
+ const batchSize = 10;
+ const maturityUpdates = new Map<string, import('$lib/types/docker/image.type').ImageMaturity | undefined>();
let checkedCount = 0;
let updatesFound = 0;
- // Check each image for maturity info, with a small delay between checks
- for (const image of images) {
- // Skip images without proper tags
- if (image.repo === '<none>' || image.tag === '<none>') {
- continue;
- }
+ for (let i = 0; i < imagesToCheck.length; i += batchSize) {
+ const batch = imagesToCheck.slice(i, i + batchSize);
+ console.log(`Processing batch ${Math.floor(i / batchSize) + 1}/${Math.ceil(imagesToCheck.length / batchSize)}: ${batch.length} images`);
- const maturityResult = await tryCatch(checkImageMaturity(image.Id));
- const maturityInfo = maturityResult.data;
+ // Process batch with delay between each image
+ for (const image of batch) {
+ const maturityResult = await tryCatch(checkImageMaturityInternal(image.Id));
- if (!maturityResult.error) {
- checkedCount++;
- if (maturityInfo?.updatesAvailable) {
- updatesFound++;
+ if (!maturityResult.error && maturityResult.data) {
+ maturityUpdates.set(image.Id, maturityResult.data);
+ checkedCount++;
+
+ if (maturityResult.data.updatesAvailable) {
+ updatesFound++;
+ }
+ } else if (!maturityResult.error) {
+ // No error but no data - cache the undefined result
+ maturityUpdates.set(image.Id, undefined);
+ checkedCount++;
}
+
+ // Small delay to avoid overwhelming registries
+ await new Promise((resolve) => setTimeout(resolve, 200));
}
- // Small delay to avoid overwhelming registries
- await new Promise((resolve) => setTimeout(resolve, 200));
+ // Batch update cache after each batch
+ if (maturityUpdates.size > 0) {
+ setImageMaturityBatch(maturityUpdates);
+ maturityUpdates.clear();
+ }
+
+ // Larger delay between batches
+ if (i + batchSize < imagesToCheck.length) {
+ await new Promise((resolve) => setTimeout(resolve, 1000));
+ }
}
- console.log(`Maturity check completed: Checked ${checkedCount} images, found ${updatesFound} updates`);
+ console.log(`Maturity check completed: Checked ${checkedCount} images, found ${updatesFound} updates, ${cachedCount} from cache`);
+
+ // Get cache metrics for monitoring
+ const metrics = maturityCache.getMetrics();
+ console.log(`Cache metrics: Hit rate ${(metrics.hitRate * 100).toFixed(1)}%, Size ${metrics.size}/${metrics.maxSize}, Avg age ${Math.round(metrics.averageAge / (1000 * 60))}min`);
// Emit an event that the UI can listen to for updates
if (typeof window !== 'undefined') {
window.dispatchEvent(
new CustomEvent('maturity-check-complete', {
- detail: { checkedCount, updatesFound }
+ detail: {
+ checkedCount: checkedCount + cachedCount,
+ updatesFound,
+ freshChecks: checkedCount,
+ cacheHits: cachedCount
+ }
})
);
}
@@ -278,19 +347,69 @@ export async function pullImage(imageRef: string, platform?: string, authConfig?
/**
* Checks if a newer version of an image is available in the registry
- * and returns maturity information.
+ * and returns maturity information. OPTIMIZED for public API use.
*/
export async function checkImageMaturity(imageId: string): Promise<import('$lib/types/docker/image.type').ImageMaturity | undefined> {
- // First check cache
- const cachedMaturity = maturityCache.get(imageId);
- if (cachedMaturity) {
+ // Use the optimized cache getter
+ const cachedMaturity = getImageMaturity(imageId);
+ if (cachedMaturity !== undefined) {
return cachedMaturity;
}
+ // If not in cache, perform the check and cache the result
+ const maturity = await checkImageMaturityInternal(imageId);
+ setImageMaturity(imageId, maturity);
+
+ return maturity;
+}
+
+/**
+ * Batch check multiple images for maturity - NEW OPTIMIZED METHOD
+ */
+export async function checkImageMaturityBatch(imageIds: string[]): Promise<Map<string, import('$lib/types/docker/image.type').ImageMaturity | undefined>> {
+ // Get cached results first
+ const cachedResults = getImageMaturityBatch(imageIds);
+
+ // Find images that need fresh checks
+ const uncachedIds = imageIds.filter((id) => !cachedResults.has(id));
+
+ if (uncachedIds.length === 0) {
+ return cachedResults;
+ }
+
+ // Process uncached images
+ const freshResults = new Map<string, import('$lib/types/docker/image.type').ImageMaturity | undefined>();
+
+ for (const imageId of uncachedIds) {
+ try {
+ const maturity = await checkImageMaturityInternal(imageId);
+ freshResults.set(imageId, maturity);
+ } catch (error) {
+ console.warn(`Failed to check maturity for image ${imageId}:`, error);
+ freshResults.set(imageId, undefined);
+ }
+
+ // Small delay between checks
+ await new Promise((resolve) => setTimeout(resolve, 100));
+ }
+
+ // Batch update cache
+ if (freshResults.size > 0) {
+ setImageMaturityBatch(freshResults);
+ }
+
+ // Combine cached and fresh results
+ const allResults = new Map([...cachedResults, ...freshResults]);
+ return allResults;
+}
+
+/**
+ * Internal maturity check function (no caching logic)
+ */
+async function checkImageMaturityInternal(imageId: string): Promise<import('$lib/types/docker/image.type').ImageMaturity | undefined> {
const imageResult = await tryCatch(getImage(imageId));
if (imageResult.error) {
- console.warn(`checkImageMaturity: Failed to get image details for ${imageId}:`, imageResult.error);
- maturityCache.set(imageId, undefined);
+ console.warn(`checkImageMaturityInternal: Failed to get image details for ${imageId}:`, imageResult.error);
return undefined;
}
@@ -298,13 +417,11 @@ export async function checkImageMaturity(imageId: string): Promise<import('$lib/
const repoTag = imageDetails.RepoTags?.[0];
if (!repoTag || repoTag.includes('<none>')) {
- maturityCache.set(imageId, undefined);
return undefined;
}
const lastColon = repoTag.lastIndexOf(':');
if (lastColon === -1) {
- maturityCache.set(imageId, undefined);
return undefined;
}
@@ -313,12 +430,11 @@ export async function checkImageMaturity(imageId: string): Promise<import('$lib/
let localCreatedDate: Date | undefined = undefined;
if (imageDetails.Created) {
- // Directly parse the ISO 8601 date string
const parsedDate = new Date(imageDetails.Created);
if (!isNaN(parsedDate.getTime())) {
localCreatedDate = parsedDate;
} else {
- console.warn(`checkImageMaturity: Invalid Created date string for image ${imageId}: ${imageDetails.Created}`);
+ console.warn(`checkImageMaturityInternal: Invalid Created date string for image ${imageId}: ${imageDetails.Created}`);
}
}
@@ -331,14 +447,37 @@ export async function checkImageMaturity(imageId: string): Promise<import('$lib/
} else {
console.error(`Error getting registry info for ${repository}:${currentTag}:`, registryInfoResult.error);
}
- maturityCache.set(imageId, undefined); // Clear maturity on error
return undefined;
}
- const registryInfo = registryInfoResult.data;
+ return registryInfoResult.data;
+}
- maturityCache.set(imageId, registryInfo);
- return registryInfo;
+/**
+ * Get cache metrics for monitoring
+ */
+export function getMaturityCacheMetrics(): {
+ hitRate: number;
+ size: number;
+ maxSize: number;
+ oldestEntry: number;
+ averageAge: number;
+} {
+ return maturityCache.getMetrics();
+}
+
+/**
+ * Invalidate maturity cache for a specific repository
+ */
+export function invalidateRepositoryMaturity(repository: string): number {
+ return maturityCache.invalidateRepository(repository);
+}
+
+/**
+ * Clear all maturity cache data
+ */
+export function clearMaturityCache(): void {
+ maturityCache.clear();
}
/**
diff --git a/src/lib/services/docker/maturity-cache-service.ts b/src/lib/services/docker/maturity-cache-service.ts
index e8f2427b9..ffb2f0024 100644
--- a/src/lib/services/docker/maturity-cache-service.ts
+++ b/src/lib/services/docker/maturity-cache-service.ts
@@ -2,10 +2,22 @@ import type { ImageMaturity } from '$lib/types/docker/image.type';
import { maturityStore, updateImageMaturity } from '$lib/stores/maturity-store';
import { get } from 'svelte/store';
+interface CacheNode {
+ key: string;
+ data: ImageMaturity | undefined;
+ timestamp: number;
+ prev: CacheNode | null;
+ next: CacheNode | null;
+}
+
export class MaturityCache {
- private cache: Map<string, { data: ImageMaturity | undefined; timestamp: number }> = new Map();
- private maxEntries: number = 100; // Default limit
- private ttlMs: number = 30 * 60 * 1000; // 30 minutes default
+ private cache: Map<string, CacheNode> = new Map();
+ private head: CacheNode | null = null;
+ private tail: CacheNode | null = null;
+ private maxEntries: number = 100;
+ private ttlMs: number = 30 * 60 * 1000;
+ private hits = 0;
+ private misses = 0;
constructor(options?: { maxEntries?: number; ttlMinutes?: number }) {
if (options?.maxEntries) this.maxEntries = options.maxEntries;
@@ -15,67 +27,178 @@ export class MaturityCache {
this.initFromStore();
}
- // Get a value from cache, return undefined if not found or expired
+ // Get a value from cache - O(1) operation
get(imageId: string): ImageMaturity | undefined {
- const entry = this.cache.get(imageId);
- if (!entry) return undefined;
+ const node = this.cache.get(imageId);
+ if (!node) {
+ this.misses++;
+ return undefined;
+ }
- // Check if the entry has expired
- if (Date.now() - entry.timestamp > this.ttlMs) {
+ // Check if expired
+ if (Date.now() - node.timestamp > this.ttlMs) {
this.delete(imageId);
+ this.misses++;
return undefined;
}
- // Update LRU status by removing and adding back
- this.cache.delete(imageId);
- this.cache.set(imageId, entry);
-
- return entry.data;
+ // Move to head (most recently used) - O(1) operation
+ this.hits++;
+ this.moveToHead(node);
+ return node.data;
}
- // Add or update a value in the cache
+ // Add or update a value - O(1) operation
set(imageId: string, data: ImageMaturity | undefined): void {
- // If at capacity, remove oldest entry (first in the Map)
- if (this.cache.size >= this.maxEntries) {
- const firstKey = this.cache.keys().next().value;
- if (typeof firstKey === 'string') {
- this.delete(firstKey);
+ const existingNode = this.cache.get(imageId);
+
+ if (existingNode) {
+ // Update existing node
+ existingNode.data = data;
+ existingNode.timestamp = Date.now();
+ this.moveToHead(existingNode);
+ } else {
+ // Create new node
+ const newNode: CacheNode = {
+ key: imageId,
+ data,
+ timestamp: Date.now(),
+ prev: null,
+ next: null
+ };
+
+ // Remove least recently used if at capacity
+ if (this.cache.size >= this.maxEntries) {
+ this.removeTail();
}
- }
- // Add new entry
- this.cache.set(imageId, {
- data,
- timestamp: Date.now()
- });
+ // Add to head
+ this.addToHead(newNode);
+ this.cache.set(imageId, newNode);
+ }
- // Update the store too
- updateImageMaturity(imageId, data);
+ // Update store only when necessary (debounced)
+ this.debouncedStoreUpdate(imageId, data);
}
- // Remove an entry from cache
+ // O(1) removal
delete(imageId: string): void {
+ const node = this.cache.get(imageId);
+ if (!node) return;
+
+ this.removeNode(node);
this.cache.delete(imageId);
- updateImageMaturity(imageId, undefined);
+ this.debouncedStoreUpdate(imageId, undefined);
+ }
+
+ // Efficient LRU operations
+ private addToHead(node: CacheNode): void {
+ node.prev = null;
+ node.next = this.head;
+
+ if (this.head) {
+ this.head.prev = node;
+ }
+
+ this.head = node;
+
+ if (!this.tail) {
+ this.tail = node;
+ }
+ }
+
+ private removeNode(node: CacheNode): void {
+ if (node.prev) {
+ node.prev.next = node.next;
+ } else {
+ this.head = node.next || null;
+ }
+
+ if (node.next) {
+ node.next.prev = node.prev;
+ } else {
+ this.tail = node.prev || null;
+ }
+ }
+
+ private moveToHead(node: CacheNode): void {
+ this.removeNode(node);
+ this.addToHead(node);
+ }
+
+ private removeTail(): void {
+ if (!this.tail) return;
+
+ const lastNode = this.tail;
+ this.removeNode(lastNode);
+ this.cache.delete(lastNode.key);
+ }
+
+ // Debounced store updates to avoid excessive writes
+ private storeUpdateTimeouts = new Map<string, NodeJS.Timeout>();
+
+ private debouncedStoreUpdate(imageId: string, data: ImageMaturity | undefined): void {
+ // Clear existing timeout
+ const existingTimeout = this.storeUpdateTimeouts.get(imageId);
+ if (existingTimeout) {
+ clearTimeout(existingTimeout);
+ }
+
+ // Set new timeout
+ const timeout = setTimeout(() => {
+ updateImageMaturity(imageId, data);
+ this.storeUpdateTimeouts.delete(imageId);
+ }, 100); // 100ms debounce
+
+ this.storeUpdateTimeouts.set(imageId, timeout);
}
// Initialize cache from maturity store
- private initFromStore(): void {
- const storeData = get(maturityStore);
- const entries = Object.entries(storeData.maturityData);
+ private async initFromStore(): Promise<void> {
+ try {
+ const storeData = get(maturityStore);
+
+ if (!storeData.maturityData) return;
+
+ // Convert to array and sort by priority/usage
+ const entries = Object.entries(storeData.maturityData)
+ .filter(([_, maturity]) => maturity !== undefined)
+ .slice(0, this.maxEntries); // Limit from start
+
+ // Initialize in chunks to avoid blocking
+ const chunkSize = 50;
+ for (let i = 0; i < entries.length; i += chunkSize) {
+ const chunk = entries.slice(i, i + chunkSize);
- // Take only up to maxEntries
- entries.slice(0, this.maxEntries).forEach(([imageId, maturity]) => {
- this.cache.set(imageId, {
- data: maturity,
- timestamp: storeData.lastChecked?.getTime() || Date.now()
- });
- });
+ // Process chunk
+ for (const [imageId, maturity] of chunk) {
+ const node: CacheNode = {
+ key: imageId,
+ data: maturity,
+ timestamp: storeData.lastChecked?.getTime() || Date.now(),
+ prev: null,
+ next: null
+ };
+
+ this.cache.set(imageId, node);
+ this.addToHead(node);
+ }
+
+ // Yield control to event loop
+ if (i + chunkSize < entries.length) {
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ }
+ }
+ } catch (error) {
+ console.warn('Failed to initialize maturity cache from store:', error);
+ }
}
// Clear all items from cache
clear(): void {
this.cache.clear();
+ this.head = null;
+ this.tail = null;
}
// Get cache stats
@@ -85,10 +208,189 @@ export class MaturityCache {
maxSize: this.maxEntries
};
}
+
+ // Batch get multiple entries
+ getBatch(imageIds: string[]): Map<string, ImageMaturity | undefined> {
+ const results = new Map<string, ImageMaturity | undefined>();
+ const now = Date.now();
+
+ for (const imageId of imageIds) {
+ const node = this.cache.get(imageId);
+ if (node && now - node.timestamp <= this.ttlMs) {
+ results.set(imageId, node.data);
+ // Move to head without individual operations
+ this.moveToHead(node);
+ }
+ }
+
+ return results;
+ }
+
+ // Batch set multiple entries
+ setBatch(entries: Map<string, ImageMaturity | undefined>): void {
+ const updates: Array<[string, ImageMaturity | undefined]> = [];
+
+ for (const [imageId, data] of entries) {
+ const existingNode = this.cache.get(imageId);
+
+ if (existingNode) {
+ existingNode.data = data;
+ existingNode.timestamp = Date.now();
+ this.moveToHead(existingNode);
+ } else {
+ if (this.cache.size >= this.maxEntries) {
+ this.removeTail();
+ }
+
+ const newNode: CacheNode = {
+ key: imageId,
+ data,
+ timestamp: Date.now(),
+ prev: null,
+ next: null
+ };
+
+ this.addToHead(newNode);
+ this.cache.set(imageId, newNode);
+ }
+
+ updates.push([imageId, data]);
+ }
+
+ // Batch update store
+ this.batchStoreUpdate(updates);
+ }
+
+ private batchStoreUpdate(updates: Array<[string, ImageMaturity | undefined]>): void {
+ // Group updates and send as single operation
+ setTimeout(() => {
+ for (const [imageId, data] of updates) {
+ updateImageMaturity(imageId, data);
+ }
+ }, 50);
+ }
+
+ // Background cleanup of expired entries
+ private cleanupInterval: NodeJS.Timeout | null = null;
+
+ startBackgroundCleanup(intervalMinutes = 10): void {
+ if (this.cleanupInterval) return;
+
+ this.cleanupInterval = setInterval(
+ () => {
+ this.cleanupExpired();
+ },
+ intervalMinutes * 60 * 1000
+ );
+ }
+
+ stopBackgroundCleanup(): void {
+ if (this.cleanupInterval) {
+ clearInterval(this.cleanupInterval);
+ this.cleanupInterval = null;
+ }
+ }
+
+ private cleanupExpired(): void {
+ const now = Date.now();
+ const expiredKeys: string[] = [];
+
+ // Walk from tail (oldest) to head
+ let current = this.tail;
+ while (current) {
+ if (now - current.timestamp > this.ttlMs) {
+ expiredKeys.push(current.key);
+ current = current.prev;
+ } else {
+ // Since we're walking from oldest to newest,
+ // we can break early when we find a non-expired entry
+ break;
+ }
+ }
+
+ // Remove expired entries
+ for (const key of expiredKeys) {
+ this.delete(key);
+ }
+
+ console.log(`Cleaned up ${expiredKeys.length} expired maturity cache entries`);
+ }
+
+ // Add cache invalidation strategies
+ invalidatePattern(pattern: RegExp): number {
+ let invalidatedCount = 0;
+ const keysToDelete: string[] = [];
+
+ for (const [key] of this.cache) {
+ if (pattern.test(key)) {
+ keysToDelete.push(key);
+ }
+ }
+
+ for (const key of keysToDelete) {
+ this.delete(key);
+ invalidatedCount++;
+ }
+
+ return invalidatedCount;
+ }
+
+ invalidateRepository(repository: string): number {
+ const pattern = new RegExp(`^${repository.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}`);
+ return this.invalidatePattern(pattern);
+ }
+
+ // Get cache efficiency metrics
+ getMetrics(): {
+ hitRate: number;
+ size: number;
+ maxSize: number;
+ oldestEntry: number;
+ averageAge: number;
+ } {
+ if (this.cache.size === 0) {
+ return {
+ hitRate: 0,
+ size: 0,
+ maxSize: this.maxEntries,
+ oldestEntry: 0,
+ averageAge: 0
+ };
+ }
+
+ const now = Date.now();
+ let totalAge = 0;
+ let oldestEntry = now;
+
+ for (const [_, node] of this.cache) {
+ const age = now - node.timestamp;
+ totalAge += age;
+ if (node.timestamp < oldestEntry) {
+ oldestEntry = node.timestamp;
+ }
+ }
+
+ return {
+ hitRate: this.hits / Math.max(this.hits + this.misses, 1),
+ size: this.cache.size,
+ maxSize: this.maxEntries,
+ oldestEntry: now - oldestEntry,
+ averageAge: totalAge / this.cache.size
+ };
+ }
}
-// Create a singleton instance
+// Create optimized singleton
export const maturityCache = new MaturityCache({
- maxEntries: 200, // Adjust based on your use case
- ttlMinutes: 60 // Cache entries expire after 1 hour
+ maxEntries: 500, // Increased for better hit rate
+ ttlMinutes: 120 // 2 hours for better persistence
});
+
+// Start background cleanup
+maturityCache.startBackgroundCleanup(15); // Every 15 minutes
+
+// Export convenience functions
+export const getImageMaturity = (imageId: string) => maturityCache.get(imageId);
+export const setImageMaturity = (imageId: string, maturity: ImageMaturity | undefined) => maturityCache.set(imageId, maturity);
+export const getImageMaturityBatch = (imageIds: string[]) => maturityCache.getBatch(imageIds);
+export const setImageMaturityBatch = (entries: Map<string, ImageMaturity | undefined>) => maturityCache.setBatch(entries);
diff --git a/src/lib/services/docker/stack-custom-service.ts b/src/lib/services/docker/stack-custom-service.ts
index 45568b4bb..5c2f167f0 100644
--- a/src/lib/services/docker/stack-custom-service.ts
+++ b/src/lib/services/docker/stack-custom-service.ts
@@ -1,11 +1,28 @@
import { promises as fs } from 'node:fs';
import * as path from 'node:path';
import Dockerode from 'dockerode';
-import { getComposeFilePath, getStackDir, loadEnvFile, parseYamlContent, normalizeHealthcheckTest } from './stack-service';
+import { load as yamlLoad, dump as yamlDump } from 'js-yaml';
+import slugify from 'slugify';
+import { directoryExists } from '$lib/utils/fs.utils';
import { getDockerClient } from './core';
-import { removeVolume } from './volume-service';
+import { getSettings, ensureStacksDirectory } from '$lib/services/settings-service';
+import type { Stack, StackService, StackUpdate } from '$lib/types/docker/stack.type';
+// Database imports
+import { listStacksFromDb, getStackByIdFromDb, saveStackToDb, updateStackRuntimeInfoInDb, updateStackContentInDb, deleteStackFromDb, updateStackAutoUpdateInDb, getAutoUpdateStacksFromDb } from '$lib/services/database/compose-db-service';
+
+interface DockerProgressEvent {
+ status: string;
+ progressDetail?: {
+ current: number;
+ total: number;
+ };
+ progress?: string;
+ id?: string;
+}
+let STACKS_DIR = '';
const stackCache = new Map();
+const CACHE_TTL = 30000; // 30 seconds
// Helper function to parse environment file content
export function parseEnvContent(envContent: string | null): Record<string, string> {
@@ -26,838 +43,1674 @@ export function parseEnvContent(envContent: string | null): Record<string, strin
}
/**
- * Custom stack deployment function using dockerode directly
+ * Initialize compose service - includes database check
*/
-export async function deployStack(stackId: string): Promise<boolean> {
- const stackDir = await getStackDir(stackId);
- const originalCwd = process.cwd();
- let deploymentStarted = false;
-
+export async function initComposeService(): Promise<void> {
try {
- // Load and normalize compose file
- const composePath = await getComposeFilePath(stackId);
- if (!composePath) {
- throw new Error(`Compose file not found for stack ${stackId}`);
- }
-
- const composeContent = await fs.readFile(composePath, 'utf8');
- const envContent = await loadEnvFile(stackId);
+ const settings = await getSettings();
+ STACKS_DIR = settings.stacksDirectory;
+ console.log(`Stacks directory initialized: ${STACKS_DIR}`);
- // Parse env variables and create getter
- const envVars = parseEnvContent(envContent);
- const getEnvVar = (key: string): string | undefined => envVars[key] || process.env[key];
-
- // Normalize content for healthchecks but don't write it back to the file
- const normalizedContent = normalizeHealthcheckTest(composeContent, getEnvVar);
+ await ensureStacksDir();
- // Only write back if it's specifically a healthcheck normalization issue
- // Don't write back the env var interpolations
- if (normalizedContent !== composeContent && normalizedContent.includes('healthcheck') && composeContent.includes('healthcheck')) {
- await fs.writeFile(composePath, normalizedContent, 'utf8');
+ // Check if we need to migrate from file-based to database
+ try {
+ const dbStacks = await listStacksFromDb();
+ if (dbStacks.length === 0) {
+ console.log('No stacks found in database, checking for file-based stacks to migrate...');
+ }
+ } catch (error) {
+ console.warn('Database not yet ready for stack operations:', error);
}
+ } catch (err) {
+ console.error('Error initializing compose service:', err);
+ }
+}
- // Parse normalized content
- const composeData = parseYamlContent(normalizedContent, getEnvVar);
- if (!composeData) {
- throw new Error(`Failed to parse compose file for stack ${stackId}`);
+/**
+ * Update stacks directory
+ */
+export function updateStacksDirectory(directory: string): void {
+ if (directory) {
+ if (!path.isAbsolute(directory)) {
+ STACKS_DIR = path.resolve(directory);
+ } else {
+ STACKS_DIR = directory;
}
+ console.log(`Stacks directory updated to: ${STACKS_DIR}`);
+ }
+}
+
+/**
+ * Ensure stacks directory exists
+ */
+export async function ensureStacksDir(): Promise<string> {
+ try {
+ if (!STACKS_DIR || !path.isAbsolute(STACKS_DIR)) {
+ let dirPath = STACKS_DIR || (await ensureStacksDirectory());
- // Store environment variables in compose data for later use
- composeData._envVars = envVars;
+ if (!path.isAbsolute(dirPath)) {
+ dirPath = path.resolve(dirPath);
+ }
+ STACKS_DIR = dirPath;
+ }
- // Change directory for relative paths
- process.chdir(stackDir);
- console.log(`Temporarily changed CWD to: ${stackDir} for stack operations`);
+ await fs.mkdir(STACKS_DIR, { recursive: true });
+ return STACKS_DIR;
+ } catch (err) {
+ console.error('Error creating stacks directory:', err);
+ throw new Error('Failed to create stacks storage directory');
+ }
+}
- // Get docker client
- const docker = await getDockerClient();
- deploymentStarted = true;
+/**
+ * Get stack directory for a given stackId
+ */
+export async function getStackDir(stackId: string): Promise<string> {
+ const stacksDirAbs = await ensureStacksDir();
+ const safeId = path.basename(stackId);
+ if (safeId !== stackId) {
+ console.warn(`Original stackId "${stackId}" was sanitized to "${safeId}". Ensure this is expected.`);
+ }
+ return path.join(stacksDirAbs, safeId);
+}
- // Deploy in sequence:
- // 1. Create networks
- await createStackNetworks(docker, stackId, composeData.networks || {});
+/**
+ * Get compose file path, prioritizing compose.yaml over docker-compose.yml
+ */
+export async function getComposeFilePath(stackId: string): Promise<string | null> {
+ const stackDirAbs = await getStackDir(stackId);
+ const newPath = path.join(stackDirAbs, 'compose.yaml');
+ const oldPath = path.join(stackDirAbs, 'docker-compose.yml');
+ try {
+ await fs.access(newPath);
+ return newPath;
+ } catch {
+ try {
+ await fs.access(oldPath);
+ return oldPath;
+ } catch {
+ return null;
+ }
+ }
+}
- // 2. Pull images
- await pullStackImages(docker, composeData.services || {});
+/**
+ * Get .env file path
+ */
+async function getEnvFilePath(stackId: string): Promise<string> {
+ const stackDir = await getStackDir(stackId);
+ return path.join(stackDir, '.env');
+}
- // 3. Create volumes (if needed)
- await createStackVolumes(docker, stackId, composeData.volumes || {});
+/**
+ * Save environment variables to .env file
+ */
+async function saveEnvFile(stackId: string, content?: string): Promise<void> {
+ const envPath = await getEnvFilePath(stackId);
+ const fileContent = content === undefined || content === null ? '' : content;
+ await fs.writeFile(envPath, fileContent, 'utf8');
+ console.log(`Saved .env file for stack ${stackId}`);
+}
- // 4. Create and start containers with proper network config
- await createAndStartContainers(docker, stackId, composeData, stackDir);
+/**
+ * Load environment variables from .env file
+ */
+export async function loadEnvFile(stackId: string): Promise<string> {
+ const envPath = await getEnvFilePath(stackId);
- // If everything succeeds, invalidate cache
- stackCache.delete('compose-stacks');
- return true;
+ try {
+ return await fs.readFile(envPath, 'utf8');
} catch (err) {
- if (deploymentStarted) {
- try {
- await cleanupFailedDeployment(stackId);
- } catch (cleanupErr) {
- console.error(`Error cleaning up failed deployment for stack ${stackId}:`, cleanupErr);
- }
+ const nodeErr = err as NodeJS.ErrnoException;
+ if (nodeErr.code === 'ENOENT') {
+ console.log(`No .env file found for stack ${stackId}`);
+ return '';
}
-
- console.error(`Error deploying stack ${stackId}:`, err);
- const errorMessage = err instanceof Error ? err.message : String(err);
- throw new Error(`Failed to deploy stack: ${errorMessage}`);
- } finally {
- process.chdir(originalCwd);
- console.log(`Restored CWD to: ${originalCwd}`);
+ console.error(`Error reading .env file for stack ${stackId}:`, err);
+ throw new Error(`Failed to read .env file: ${nodeErr.message}`, { cause: err });
}
}
/**
- * Creates all networks defined in the compose file
+ * Load compose stacks - FAST version (no runtime updates)
*/
-async function createStackNetworks(docker: Dockerode, stackId: string, networks: Record<string, any>): Promise<void> {
- // Check if we have any non-external networks to create
- const networksToCreate = Object.entries(networks).filter(([_, config]) => !config.external);
-
- // If all networks are external, don't create a default network
- if (Object.keys(networks).length > 0 && networksToCreate.length === 0) {
- console.log(`All networks are external for stack ${stackId}, skipping network creation`);
- return;
- }
+export async function loadComposeStacks(): Promise<Stack[]> {
+ const cacheKey = 'compose-stacks';
+ const cachedData = stackCache.get(cacheKey);
- // Always create a default network if no networks are defined OR if we have non-external networks
- if (Object.keys(networks).length === 0) {
- const defaultNetworkName = `${stackId}_default`;
- console.log(`No networks defined, creating default network: ${defaultNetworkName}`);
+ // Extend cache TTL to 5 minutes for better performance
+ const EXTENDED_CACHE_TTL = 5 * 60 * 1000; // 5 minutes
- try {
- await docker.createNetwork({
- Name: defaultNetworkName,
- Driver: 'bridge',
- Labels: {
- 'com.docker.compose.project': stackId,
- 'com.docker.compose.network': 'default'
- }
- });
- } catch (err: any) {
- if (err.statusCode === 409) {
- console.log(`Default network ${defaultNetworkName} already exists, reusing it.`);
- } else {
- throw err;
- }
- }
- return;
+ if (cachedData && Date.now() - cachedData.timestamp < EXTENDED_CACHE_TTL) {
+ console.log(`Returning ${cachedData.data.length} stacks from cache`);
+ return cachedData.data;
}
- // Process defined networks (only create non-external ones)
- for (const [networkName, networkConfig] of Object.entries(networks)) {
- // Skip external networks
- if (networkConfig.external) {
- console.log(`Using external network: ${networkConfig.name || networkName}`);
- continue;
- }
-
- // Network creation logic for non-external networks
- const networkToCreate = {
- Name: networkConfig.name || `${stackId}_${networkName}`,
- Driver: networkConfig.driver || 'bridge',
- Labels: {
- 'com.docker.compose.project': stackId,
- 'com.docker.compose.network': networkName
- },
- Options: networkConfig.driver_opts || {}
- };
+ try {
+ // Just load from database - NO file system reads, NO service queries
+ const dbStacks = await listStacksFromDb();
+ console.log(`Loaded ${dbStacks.length} stacks from database (fast mode)`);
+
+ // Return stacks with minimal processing
+ const fastStacks = dbStacks.map((stack) => ({
+ ...stack,
+ services: [] // Empty services array for fast loading
+ }));
+
+ stackCache.set(cacheKey, {
+ data: fastStacks,
+ timestamp: Date.now()
+ });
- try {
- console.log(`Creating network: ${networkToCreate.Name}`);
- await docker.createNetwork(networkToCreate);
- } catch (err: any) {
- if (err.statusCode === 409) {
- console.log(`Network ${networkToCreate.Name} already exists, reusing it.`);
- } else {
- throw err;
- }
- }
+ console.log(`Fast load completed: ${fastStacks.length} stacks`);
+ return fastStacks;
+ } catch (error) {
+ console.error('Error loading stacks from database, falling back to file-based approach:', error);
+ return loadComposeStacksFromFiles();
}
}
/**
- * Pulls all images defined in the compose file
+ * Load stacks with full runtime info (use sparingly)
*/
-async function pullStackImages(docker: Dockerode, services: Record<string, any>): Promise<void> {
- const pullPromises = Object.entries(services)
- .filter(([_, serviceConfig]) => serviceConfig.image)
- .map(async ([serviceName, serviceConfig]) => {
- const serviceImage = serviceConfig.image;
- console.log(`Pulling image for service ${serviceName}: ${serviceImage}`);
+export async function loadComposeStacksWithRuntimeInfo(): Promise<Stack[]> {
+ // This is the current heavy function - only use when needed
+ const dbStacks = await listStacksFromDb();
+ return Promise.all(
+ dbStacks.map(async (stack) => {
try {
- await new Promise((resolve, reject) => {
- docker.pull(serviceImage, {}, (pullError, stream) => {
- if (pullError) {
- reject(pullError);
- return;
- }
- if (!stream) {
- reject(new Error(`Docker pull did not return a stream.`));
- return;
+ let composeContent = stack.composeContent;
+
+ if (!composeContent) {
+ try {
+ const composePath = await getComposeFilePath(stack.id);
+ if (composePath) {
+ composeContent = await fs.readFile(composePath, 'utf8');
+ // Don't update DB on every load - do this in background
}
+ } catch (fileError) {
+ console.warn(`Could not load compose file for stack ${stack.id}`);
+ }
+ }
- docker.modem.followProgress(
- stream,
- (progressError, output) => {
- if (progressError) {
- reject(progressError);
- } else {
- resolve(output);
- }
- },
- (event) => {
- if (event.progress) {
- console.log(`${serviceImage}: ${event.status} ${event.progress}`);
- } else if (event.status) {
- console.log(`${serviceImage}: ${event.status}`);
- }
- }
- );
- });
- });
- } catch (err) {
- console.warn(`Warning: Failed to pull image ${serviceImage}:`, err);
- }
- });
+ const services = await getStackServices(stack.id, composeContent || '');
+ const serviceCount = services.length;
+ const runningCount = services.filter((s) => s.state?.Running).length;
+
+ let status: Stack['status'] = 'stopped';
+ if (serviceCount === 0) {
+ status = 'unknown';
+ } else if (runningCount === serviceCount) {
+ status = 'running';
+ } else if (runningCount > 0) {
+ status = 'partially running';
+ }
- await Promise.all(pullPromises);
+ return {
+ ...stack,
+ composeContent,
+ services,
+ serviceCount,
+ runningCount,
+ status
+ };
+ } catch (error) {
+ console.error(`Error processing stack ${stack.id}:`, error);
+ return {
+ ...stack,
+ services: []
+ };
+ }
+ })
+ );
}
/**
- * Pulls all images for a stack without deploying
+ * Original file-based loading (kept as fallback)
*/
-export async function pullImagesForStack(stackId: string): Promise<boolean> {
- console.log(`Pulling images for stack ${stackId}...`);
+async function loadComposeStacksFromFiles(): Promise<Stack[]> {
+ const stacksDir = await ensureStacksDir();
try {
- // Load and normalize compose file
- const composePath = await getComposeFilePath(stackId);
- if (!composePath) {
- throw new Error(`Compose file not found for stack ${stackId}`);
- }
+ const stackDirEntries = await fs.readdir(stacksDir, { withFileTypes: true });
+ const stacks: Stack[] = [];
- const composeContent = await fs.readFile(composePath, 'utf8');
- const envContent = await loadEnvFile(stackId);
-
- // Parse env variables and create getter
- const envVars = parseEnvContent(envContent);
- const getEnvVar = (key: string): string | undefined => envVars[key] || process.env[key];
+ for (const entry of stackDirEntries) {
+ if (!entry.isDirectory()) {
+ continue;
+ }
- // Normalize and parse compose content
- const normalizedContent = normalizeHealthcheckTest(composeContent, getEnvVar);
- const composeData = parseYamlContent(normalizedContent, getEnvVar);
+ const dirName = entry.name;
+ const stackDir = path.join(stacksDir, dirName);
- if (!composeData) {
- throw new Error(`Failed to parse compose file for stack ${stackId}`);
- }
+ let composeFilePath: string | null = null;
+ let composeContent: string | null = null;
- // Get docker client
- const docker = await getDockerClient();
+ const potentialComposePaths = [path.join(stackDir, 'compose.yaml'), path.join(stackDir, 'docker-compose.yml')];
- // Call the internal pullStackImages function with correct arguments
- await pullStackImages(docker, composeData.services || {});
+ for (const p of potentialComposePaths) {
+ try {
+ await fs.access(p);
+ composeContent = await fs.readFile(p, 'utf8');
+ composeFilePath = p;
+ break;
+ } catch {
+ // Try next path
+ }
+ }
- return true;
- } catch (err) {
- console.error(`Error pulling images for stack ${stackId}:`, err);
- throw new Error(`Failed to pull images: ${err instanceof Error ? err.message : String(err)}`);
- }
-}
+ if (!composeContent || !composeFilePath) {
+ console.warn(`No compose file found in directory ${dirName}, skipping.`);
+ continue;
+ }
-/**
- * Creates volumes defined in the compose file
- */
-async function createStackVolumes(docker: Dockerode, stackId: string, volumes: Record<string, any>): Promise<void> {
- for (const [volumeName, volumeConfig] of Object.entries(volumes)) {
- if (volumeConfig?.external) {
- console.log(`Using external volume: ${volumeName}`);
- continue;
- }
+ const services = await getStackServices(dirName, composeContent);
- const volumeToCreate = {
- Name: volumeConfig?.name || `${stackId}_${volumeName}`,
- Driver: volumeConfig?.driver || 'local',
- DriverOpts: volumeConfig?.driver_opts || {},
- Labels: {
- 'com.docker.compose.project': stackId,
- 'com.docker.compose.volume': volumeName,
- ...volumeConfig?.labels
+ let dirStat;
+ try {
+ dirStat = await fs.stat(stackDir);
+ } catch (statErr) {
+ console.error(`Could not stat directory ${stackDir}:`, statErr);
+ const now = new Date().toISOString();
+ dirStat = { birthtime: new Date(now), mtime: new Date(now) };
}
- };
- try {
- console.log(`Creating volume: ${volumeToCreate.Name}`);
- await docker.createVolume(volumeToCreate);
- } catch (err: any) {
- if (err.statusCode === 409) {
- console.log(`Volume ${volumeToCreate.Name} already exists, reusing it.`);
- } else {
- throw err;
+ const serviceCount = services.length;
+ const runningCount = services.filter((s) => s.state?.Running).length;
+
+ let status: Stack['status'] = 'stopped';
+ if (serviceCount === 0) {
+ status = 'unknown';
+ } else if (runningCount === serviceCount) {
+ status = 'running';
+ } else if (runningCount > 0) {
+ status = 'partially running';
}
+
+ stacks.push({
+ id: dirName,
+ name: dirName,
+ services,
+ serviceCount,
+ runningCount,
+ status,
+ createdAt: dirStat.birthtime.toISOString(),
+ updatedAt: dirStat.mtime.toISOString(),
+ isExternal: false
+ });
}
+
+ return stacks;
+ } catch (err) {
+ console.error('Error loading stacks from STACKS_DIR:', err);
+ throw new Error('Failed to load compose stacks');
}
}
/**
- * Creates and starts all containers with proper network configuration
+ * Get stack services from containers
*/
-async function createAndStartContainers(docker: Dockerode, stackId: string, composeData: any, stackDir: string): Promise<void> {
- const services = composeData.services || {};
+async function getStackServices(stackId: string, composeContent: string): Promise<StackService[]> {
+ const docker = await getDockerClient();
+ const composeProjectLabel = 'com.docker.compose.project';
+ const composeServiceLabel = 'com.docker.compose.service';
+
+ console.log(`Getting services for stack ${stackId}, composeContent length: ${composeContent.length}`);
- // Build dependency order - services with depends_on should start after their dependencies
- const serviceList = buildServiceStartOrder(services);
+ try {
+ // Load the .env file to provide environment variable values
+ let envContent = '';
+ let envVars: Record<string, string> = {};
- for (const serviceName of serviceList) {
- const serviceConfig = services[serviceName];
- console.log(`Creating container for service: ${serviceName}`);
+ try {
+ envContent = await loadEnvFile(stackId);
+ envVars = parseEnvContent(envContent);
+ } catch (envError) {
+ console.log(`No .env file found for stack ${stackId}, continuing without env vars`);
+ }
- // Generate container name
- const containerName = serviceConfig.container_name || `${stackId}_${serviceName}`;
+ // Create environment variable getter function
+ const getEnvVar = (key: string) => {
+ return envVars[key] || process.env[key] || '';
+ };
- // Check if container already exists
- const existingContainers = await docker.listContainers({
- all: true,
- filters: { name: [containerName] }
- });
+ // Parse compose content to get service definitions
+ let composeData: Record<string, any> | null = null;
+ let serviceNames: string[] = [];
- if (existingContainers.length > 0) {
- console.log(`Container ${containerName} already exists. Removing it.`);
- const container = docker.getContainer(existingContainers[0].Id);
- if (existingContainers[0].State === 'running') {
- await container.stop();
+ if (composeContent.trim()) {
+ composeData = parseYamlContent(composeContent, getEnvVar);
+ if (composeData && composeData.services) {
+ serviceNames = Object.keys(composeData.services as Record<string, unknown>);
+ console.log(`Found ${serviceNames.length} services defined in compose: [${serviceNames.join(', ')}]`);
+ } else {
+ console.warn(`No services found in compose content for stack ${stackId}`);
}
- await container.remove();
+ } else {
+ console.warn(`Empty compose content for stack ${stackId}`);
}
- // Build container creation options
- const createOptions = await buildContainerOptions(docker, stackId, serviceName, serviceConfig, stackDir, composeData);
+ // List all containers
+ const containers = await docker.listContainers({ all: true });
+ console.log(`Total containers found: ${containers.length}`);
- // Create the container
- const container = await docker.createContainer(createOptions);
- console.log(`Created container ${containerName} with ID: ${container.id}`);
+ // Filter containers based on labels and naming convention
+ const stackContainers = containers.filter((container) => {
+ const labels = container.Labels || {};
+ const names = container.Names || [];
- // Connect to additional networks if specified
- if (serviceConfig.networks && !serviceConfig.network_mode) {
- let networkKeys: string[];
+ // Check if container belongs to this stack
+ const hasCorrectLabel = labels[composeProjectLabel] === stackId;
+ const nameStartsWithPrefix = names.some((name) => name.startsWith(`/${stackId}_`));
- // Handle both array format and object format
- if (Array.isArray(serviceConfig.networks)) {
- networkKeys = serviceConfig.networks;
- } else {
- networkKeys = Object.keys(serviceConfig.networks);
+ const belongs = hasCorrectLabel || nameStartsWithPrefix;
+
+ if (belongs) {
+ console.log(`Container ${container.Id} (${names[0]}) belongs to stack ${stackId}`);
+ console.log(` - Labels: ${JSON.stringify(labels)}`);
+ console.log(` - State: ${container.State}`);
}
- // Only process additional networks if there are more than 1
- if (networkKeys.length > 1) {
- const composeNetworks = composeData.networks || {};
+ return belongs;
+ });
- // Skip the first network since it's already set as the primary network
- for (let i = 1; i < networkKeys.length; i++) {
- const networkKey = networkKeys[i];
+ console.log(`Found ${stackContainers.length} containers for stack ${stackId}`);
- // Get network config (only available in object format)
- const networkConfig = Array.isArray(serviceConfig.networks) ? {} : serviceConfig.networks[networkKey];
+ const services: StackService[] = [];
- // Resolve actual network name using the same logic as primary network
- let actualNetworkName: string;
+ for (const containerData of stackContainers) {
+ const containerName = containerData.Names?.[0]?.substring(1) || '';
+ const labels = containerData.Labels || {};
+ let serviceName = labels[composeServiceLabel];
- // Strategy 1: Direct network key match
- if (composeNetworks[networkKey]) {
- const composeNetworkConfig = composeNetworks[networkKey];
- if (composeNetworkConfig.external) {
- actualNetworkName = composeNetworkConfig.name || networkKey;
- } else {
- actualNetworkName = composeNetworkConfig.name || `${stackId}_${networkKey}`;
- }
- } else {
- // Strategy 2: Check if service network name matches any external network's name
- const matchingExternalNetwork = Object.entries(composeNetworks).find(([_, config]: [string, any]) => config.external && config.name === networkKey);
+ console.log(`Processing container ${containerData.Id} (${containerName})`);
- if (matchingExternalNetwork) {
- actualNetworkName = networkKey;
- } else {
- // Strategy 3: Fallback - assume it's an internal network
- actualNetworkName = `${stackId}_${networkKey}`;
- }
+ // Fallback to parsing from container name if the service label is missing
+ if (!serviceName && serviceNames.length > 0) {
+ console.log(`Container ${containerData.Id} missing service label, trying to parse from name`);
+ for (const name of serviceNames) {
+ const servicePrefixWithUnderscore = `${stackId}_${name}_`;
+ const servicePrefixExact = `${stackId}_${name}`;
+ if (containerName.startsWith(servicePrefixWithUnderscore) || containerName === servicePrefixExact) {
+ serviceName = name;
+ console.log(`Matched service name: ${serviceName}`);
+ break;
}
+ }
+ }
- try {
- console.log(`Connecting container ${containerName} to additional network: ${actualNetworkName}`);
- const network = docker.getNetwork(actualNetworkName);
- await network.connect({
- Container: container.id,
- EndpointConfig: buildEndpointConfig(networkConfig)
- });
- } catch (err) {
- console.error(`Failed to connect container to network ${actualNetworkName}:`, err);
- throw err;
- }
+ // Final fallback if still no match
+ if (!serviceName) {
+ // Extract service name from container name pattern: stackId_serviceName_instance
+ const namePattern = new RegExp(`^${stackId}_([^_]+)(?:_\\d+)?$`);
+ const match = containerName.match(namePattern);
+ if (match) {
+ serviceName = match[1];
+ console.log(`Extracted service name from container name: ${serviceName}`);
+ } else {
+ serviceName = containerName.replace(`${stackId}_`, '').replace(/_\d+$/, '') || containerName;
+ console.log(`Using fallback service name: ${serviceName}`);
}
}
- }
- // Start the container
- console.log(`Starting container: ${containerName}`);
- await container.start();
- }
-}
+ const service: StackService = {
+ id: containerData.Id,
+ name: serviceName,
+ state: {
+ Running: containerData.State === 'running',
+ Status: containerData.State,
+ ExitCode: containerData.State === 'exited' ? -1 : 0
+ }
+ };
-/**
- * Build endpoint configuration for network connection
- */
-function buildEndpointConfig(networkConfig: any): any {
- if (!networkConfig) return {};
+ console.log(`Created service: ${JSON.stringify(service)}`);
- const config: any = {};
+ // Avoid adding duplicates
+ const existingServiceIndex = services.findIndex((s) => s.name === serviceName);
+ if (existingServiceIndex !== -1) {
+ if (!services[existingServiceIndex].id) {
+ services[existingServiceIndex] = service;
+ console.log(`Updated existing service ${serviceName} with container data`);
+ } else {
+ console.log(`Multiple containers found for service ${serviceName} in stack ${stackId}. Keeping first found.`);
+ }
+ } else {
+ services.push(service);
+ console.log(`Added new service ${serviceName}`);
+ }
+ }
- // IPv4 address configuration - this is the part that needs fixing
- if (networkConfig.ipv4_address) {
- // Create IPAMConfig structure as required by Docker API
- config.IPAMConfig = config.IPAMConfig || {};
- config.IPAMConfig.IPv4Address = networkConfig.ipv4_address;
- console.log(`Setting static IPv4 address: ${networkConfig.ipv4_address}`);
- }
+ // Add placeholders for services defined in compose but not found among listed containers
+ if (serviceNames.length > 0) {
+ for (const name of serviceNames) {
+ if (!services.some((s) => s.name === name)) {
+ const placeholderService: StackService = {
+ id: '',
+ name: name,
+ state: {
+ Running: false,
+ Status: 'not created',
+ ExitCode: 0
+ }
+ };
+ services.push(placeholderService);
+ console.log(`Added placeholder service for ${name}`);
+ }
+ }
+ }
- // IPv6 address configuration
- if (networkConfig.ipv6_address) {
- config.IPAMConfig = config.IPAMConfig || {};
- config.IPAMConfig.IPv6Address = networkConfig.ipv6_address;
- }
+ // Sort services alphabetically by name for consistent order
+ services.sort((a, b) => a.name.localeCompare(b.name));
- // DNS aliases - crucial for service discovery
- if (networkConfig.aliases && Array.isArray(networkConfig.aliases)) {
- config.Aliases = networkConfig.aliases;
- }
+ console.log(`Final services for stack ${stackId}: ${services.length} services`);
+ services.forEach((s) => console.log(` - ${s.name}: ${s.state?.Status} (id: ${s.id || 'none'})`));
- // Link-local IPs
- if (networkConfig.link_local_ips && Array.isArray(networkConfig.link_local_ips)) {
- config.IPAMConfig = config.IPAMConfig || {};
- config.IPAMConfig.LinkLocalIPs = networkConfig.link_local_ips;
+ return services;
+ } catch (err) {
+ console.error(`Error getting services for stack ${stackId}:`, err);
+ return [];
}
-
- return config;
}
/**
- * Builds the optimal service start order based on dependencies
+ * Get a specific stack - FAST version for detail pages
*/
-function buildServiceStartOrder(services: Record<string, any>): string[] {
- const visited = new Set<string>();
- const result: string[] = [];
-
- // DFS function to process services
- function visit(serviceName: string) {
- if (visited.has(serviceName)) return;
- visited.add(serviceName);
-
- // Process dependencies first
- const deps = services[serviceName]?.depends_on || {};
- if (typeof deps === 'object') {
- // Handle both array format and object format of depends_on
- const depNames = Array.isArray(deps) ? deps : Object.keys(deps);
- for (const dep of depNames) {
- visit(dep);
+export async function getStack(stackId: string): Promise<Stack> {
+ try {
+ // Try database first
+ const dbStack = await getStackByIdFromDb(stackId);
+ if (dbStack) {
+ console.log(`Found stack ${stackId} in database`);
+
+ // Get services and update status ONLY for single stack view
+ let composeContent = dbStack.composeContent;
+ if (!composeContent) {
+ const composePath = await getComposeFilePath(stackId);
+ if (composePath) {
+ composeContent = await fs.readFile(composePath, 'utf8');
+ // Update in background, don't wait
+ updateStackContentInDb(stackId, { composeContent }).catch(console.error);
+ }
}
- }
- result.push(serviceName);
- }
+ const services = await getStackServices(stackId, composeContent || '');
+ const serviceCount = services.length;
+ const runningCount = services.filter((s) => s.state?.Running).length;
+
+ let status: Stack['status'] = 'stopped';
+ if (serviceCount === 0) {
+ status = 'unknown';
+ } else if (runningCount === serviceCount) {
+ status = 'running';
+ } else if (runningCount > 0) {
+ status = 'partially running';
+ }
- // Visit all services
- Object.keys(services).forEach(visit);
+ // Update runtime info in background
+ updateStackRuntimeInfoInDb(stackId, {
+ status,
+ serviceCount,
+ runningCount,
+ lastPolled: new Date()
+ }).catch(console.error);
+
+ return {
+ ...dbStack,
+ composeContent,
+ services,
+ serviceCount,
+ runningCount,
+ status
+ };
+ }
+ } catch (error) {
+ console.error(`Error loading stack ${stackId} from database:`, error);
+ }
- return result;
+ // Fallback to file-based approach
+ return getStackFromFiles(stackId);
}
/**
- * Builds container creation options from service configuration
+ * Original file-based getStack (kept as fallback)
*/
-async function buildContainerOptions(docker: Dockerode, stackId: string, serviceName: string, serviceConfig: any, stackDir: string, composeData: any): Promise<any> {
- const options: any = {
- name: serviceConfig.container_name || `${stackId}_${serviceName}`,
- Image: serviceConfig.image,
- Tty: serviceConfig.tty || false,
- OpenStdin: serviceConfig.stdin_open || false,
- StopSignal: serviceConfig.stop_signal,
- StopTimeout: serviceConfig.stop_grace_period ? parseInt(serviceConfig.stop_grace_period.replace(/s$/, '')) : undefined,
- Hostname: serviceConfig.hostname || serviceName,
- Domainname: serviceConfig.domainname,
- User: serviceConfig.user,
- WorkingDir: serviceConfig.working_dir,
- Labels: {
- 'com.docker.compose.project': stackId,
- 'com.docker.compose.service': serviceName,
- ...serviceConfig.labels
- }
- };
+async function getStackFromFiles(stackId: string): Promise<Stack> {
+ const stackDir = await getStackDir(stackId);
- // Handle command
- if (serviceConfig.command) {
- if (typeof serviceConfig.command === 'string') {
- options.Cmd = serviceConfig.command.split(/\s+/);
- } else {
- options.Cmd = serviceConfig.command;
- }
+ try {
+ await fs.access(stackDir);
+ } catch {
+ throw new Error(`Stack with ID "${stackId}" not found.`);
}
- // Handle entrypoint
- if (serviceConfig.entrypoint) {
- if (typeof serviceConfig.entrypoint === 'string') {
- options.Entrypoint = serviceConfig.entrypoint.split(/\s+/);
- } else {
- options.Entrypoint = serviceConfig.entrypoint;
- }
+ const composePath = await getComposeFilePath(stackId);
+ if (!composePath) {
+ throw new Error(`Compose file not found for stack ${stackId}`);
}
- // Handle environment variables
- if (serviceConfig.environment) {
- options.Env = [];
- if (Array.isArray(serviceConfig.environment)) {
- options.Env = serviceConfig.environment;
- } else {
- for (const [key, value] of Object.entries(serviceConfig.environment)) {
- options.Env.push(`${key}=${value}`);
- }
- }
+ const composeContent = await fs.readFile(composePath, 'utf8');
+ const envContent = await loadEnvFile(stackId);
+ const services = await getStackServices(stackId, composeContent);
+
+ const dirStat = await fs.stat(stackDir);
+ const serviceCount = services.length;
+ const runningCount = services.filter((s) => s.state?.Running).length;
+
+ let status: Stack['status'] = 'stopped';
+ if (serviceCount === 0) {
+ status = 'unknown';
+ } else if (runningCount === serviceCount) {
+ status = 'running';
+ } else if (runningCount > 0) {
+ status = 'partially running';
}
- // Add .env file variables to container environment
- const envVars = composeData._envVars || {};
- for (const [key, value] of Object.entries(envVars)) {
- // Only add if not already set by service config
- if (!options.Env || !options.Env.some((env: string) => env.startsWith(`${key}=`))) {
- options.Env = options.Env || [];
- options.Env.push(`${key}=${value}`);
+ return {
+ id: stackId,
+ name: stackId,
+ services,
+ serviceCount,
+ runningCount,
+ status,
+ createdAt: dirStat.birthtime.toISOString(),
+ updatedAt: dirStat.mtime.toISOString(),
+ composeContent,
+ envContent: envContent || '',
+ isExternal: false
+ };
+}
+
+/**
+ * Create a new stack - saves to both database and files
+ */
+export async function createStack(name: string, composeContent: string, envContent?: string): Promise<Stack> {
+ const dirName = slugify(name, {
+ lower: true,
+ strict: true,
+ replacement: '-',
+ trim: true
+ });
+
+ const stacksDir = await ensureStacksDirectory();
+ let counter = 1;
+ let uniqueDirName = dirName;
+
+ while (await directoryExists(path.join(stacksDir, uniqueDirName))) {
+ uniqueDirName = `${dirName}-${counter}`;
+ counter++;
+ }
+
+ const stackDir = path.join(stacksDir, uniqueDirName);
+ await fs.mkdir(stackDir, { recursive: true });
+
+ const normalizedComposeContent = normalizeHealthcheckTest(composeContent);
+ await fs.writeFile(path.join(stackDir, 'compose.yaml'), normalizedComposeContent);
+
+ if (envContent) {
+ await fs.writeFile(path.join(stackDir, '.env'), envContent);
+ }
+
+ let serviceCount = 0;
+ try {
+ const composeData = parseYamlContent(composeContent);
+ if (composeData?.services) {
+ serviceCount = Object.keys(composeData.services as Record<string, unknown>).length;
}
+ } catch (parseErr) {
+ console.warn(`Could not parse compose file during creation for stack ${uniqueDirName}:`, parseErr);
}
- // Handle ports
- if (serviceConfig.ports) {
- options.ExposedPorts = {};
- options.HostConfig = options.HostConfig || {};
- options.HostConfig.PortBindings = {};
+ const dirStat = await fs.stat(stackDir);
+
+ const newStack: Stack = {
+ id: uniqueDirName,
+ name: uniqueDirName,
+ serviceCount: serviceCount,
+ runningCount: 0,
+ status: 'stopped',
+ createdAt: dirStat.birthtime.toISOString(),
+ updatedAt: dirStat.mtime.toISOString(),
+ composeContent: composeContent,
+ envContent: envContent || '',
+ isExternal: false,
+ path: stackDir,
+ dirName: uniqueDirName
+ };
- for (const portMapping of serviceConfig.ports) {
- let hostPort, containerPort, protocol;
+ // Save to database
+ try {
+ await saveStackToDb(newStack);
+ console.log(`Stack ${uniqueDirName} saved to database`);
+ } catch (error) {
+ console.error(`Error saving stack ${uniqueDirName} to database:`, error);
+ // Continue anyway - files are already created
+ }
- if (typeof portMapping === 'string') {
- const parts = portMapping.split(':');
- if (parts.length === 1) {
- containerPort = parts[0];
- hostPort = parts[0];
- } else {
- hostPort = parts[0];
- containerPort = parts[1];
- }
+ stackCache.delete('compose-stacks');
+ return newStack;
+}
- // Handle protocol specification
- const protocolParts = containerPort.split('/');
- if (protocolParts.length > 1) {
- containerPort = protocolParts[0];
- protocol = protocolParts[1];
- } else {
- protocol = 'tcp';
- }
+/**
+ * Update a stack - updates both database and files
+ */
+export async function updateStack(currentStackId: string, updates: StackUpdate): Promise<Stack> {
+ let effectiveStackId = currentStackId;
+ let stackAfterRename: Stack | null = null;
+
+ // Handle potential rename first
+ if (updates.name) {
+ const newSlugifiedName = slugify(updates.name, {
+ lower: true,
+ strict: true,
+ replacement: '-',
+ trim: true
+ });
+
+ if (newSlugifiedName !== currentStackId) {
+ console.log(`Rename requested for stack '${currentStackId}' to '${updates.name}' (slug: '${newSlugifiedName}').`);
+ stackAfterRename = await renameStack(currentStackId, updates.name);
+ effectiveStackId = stackAfterRename.id;
+ console.log(`Stack '${currentStackId}' successfully renamed to '${effectiveStackId}'.`);
+ }
+ }
+
+ // Handle content updates
+ let contentUpdated = false;
+ const stackDirForContent = await getStackDir(effectiveStackId);
+ const promises = [];
+
+ if (updates.composeContent !== undefined) {
+ const normalizedComposeContent = normalizeHealthcheckTest(updates.composeContent);
+ const currentComposePath = await getComposeFilePath(effectiveStackId);
+ const targetComposePath = currentComposePath || path.join(stackDirForContent, 'compose.yaml');
+
+ promises.push(fs.writeFile(targetComposePath, normalizedComposeContent, 'utf8'));
+ contentUpdated = true;
+ console.log(`Updating composeContent for stack '${effectiveStackId}'.`);
+ }
+
+ if (updates.envContent !== undefined) {
+ promises.push(saveEnvFile(effectiveStackId, updates.envContent));
+ contentUpdated = true;
+ console.log(`Updating envContent for stack '${effectiveStackId}'.`);
+ }
+
+ if (promises.length > 0) {
+ await Promise.all(promises);
+ }
+
+ // Update database
+ if (contentUpdated) {
+ try {
+ await updateStackContentInDb(effectiveStackId, {
+ composeContent: updates.composeContent,
+ envContent: updates.envContent
+ });
+ console.log(`Stack ${effectiveStackId} content updated in database`);
+ } catch (error) {
+ console.error(`Error updating stack ${effectiveStackId} in database:`, error);
+ }
+ }
+
+ stackCache.delete('compose-stacks');
+
+ if (stackAfterRename && !contentUpdated) {
+ return stackAfterRename;
+ } else {
+ return getStack(effectiveStackId);
+ }
+}
+
+/**
+ * Custom stack deployment function using dockerode directly
+ */
+export async function deployStack(stackId: string): Promise<boolean> {
+ const stackDir = await getStackDir(stackId);
+ const originalCwd = process.cwd();
+ let deploymentStarted = false;
+
+ try {
+ // Load and normalize compose file
+ const composePath = await getComposeFilePath(stackId);
+ if (!composePath) {
+ throw new Error(`Compose file not found for stack ${stackId}`);
+ }
+
+ const composeContent = await fs.readFile(composePath, 'utf8');
+ const envContent = await loadEnvFile(stackId);
+
+ // Parse env variables and create getter
+ const envVars = parseEnvContent(envContent);
+ const getEnvVar = (key: string): string | undefined => envVars[key] || process.env[key];
+
+ // Normalize content for healthchecks but don't write it back to the file
+ const normalizedContent = normalizeHealthcheckTest(composeContent, getEnvVar);
+
+ // Only write back if it's specifically a healthcheck normalization issue
+ if (composeContent !== normalizedContent) {
+ console.log(`Normalized compose content for stack ${stackId}. Writing to disk.`);
+ await fs.writeFile(composePath, normalizedContent, 'utf8');
+ }
+
+ process.chdir(stackDir);
+ console.log(`Changed CWD to: ${stackDir} for stack ${stackId} operations.`);
+
+ // Parse the normalized content
+ const composeData = parseYamlContent(normalizedContent, getEnvVar);
+
+ if (!composeData) {
+ throw new Error(`Failed to parse compose file for stack ${stackId}`);
+ }
+
+ const hasExternalNetworks = composeData.networks && Object.values(composeData.networks).some((net: any) => net.external);
+
+ try {
+ deploymentStarted = true;
+ if (hasExternalNetworks) {
+ console.log(`Stack ${stackId} contains external networks. Using custom deployment approach.`);
+ await deployStackWithExternalNetworks(stackId, composeData, stackDir);
} else {
- hostPort = portMapping.published;
- containerPort = portMapping.target;
- protocol = portMapping.protocol || 'tcp';
+ // Standard approach for stacks without external networks
+ const docker = await getDockerClient();
+
+ // Pull images for all services
+ const imagePullPromises = Object.entries(composeData.services || {})
+ .filter(([_, serviceConfig]) => (serviceConfig as any).image)
+ .map(async ([serviceName, serviceConfig]) => {
+ const serviceImage = (serviceConfig as any).image;
+ console.log(`Pulling image for service ${serviceName}: ${serviceImage}`);
+ try {
+ await pullImage(docker, serviceImage);
+ } catch (pullErr) {
+ console.warn(`Warning: Failed to pull image ${serviceImage} for service ${serviceName}:`, pullErr);
+ }
+ });
+
+ await Promise.all(imagePullPromises);
+
+ // Create networks
+ await createStackNetworks(docker, stackId, composeData.networks || {});
+
+ // Deploy services
+ await createAndStartServices(docker, stackId, composeData, stackDir);
+ }
+
+ // Update database with new status
+ try {
+ await updateStackRuntimeInfoInDb(stackId, {
+ status: 'running',
+ lastPolled: Math.floor(Date.now() / 1000) // ← Solution: Unix timestamp
+ });
+ } catch (dbError) {
+ console.error(`Error updating stack ${stackId} status in database:`, dbError);
}
- const containerPortWithProtocol = `${containerPort}/${protocol}`;
- options.ExposedPorts[containerPortWithProtocol] = {};
- options.HostConfig.PortBindings[containerPortWithProtocol] = [{ HostPort: hostPort.toString() }];
+ stackCache.delete('compose-stacks');
+ return true;
+ } catch (deployErr) {
+ // If deployment started but failed, clean up any containers that were created
+ if (deploymentStarted) {
+ console.log(`Deployment of stack ${stackId} failed. Cleaning up any created containers...`);
+ try {
+ await cleanupFailedDeployment(stackId);
+ } catch (cleanupErr) {
+ console.error(`Error cleaning up failed deployment for stack ${stackId}:`, cleanupErr);
+ }
+ }
+ throw deployErr;
}
+ } catch (err) {
+ console.error(`Error deploying stack ${stackId}:`, err);
+ const errorMessage = err instanceof Error ? err.message : String(err);
+ throw new Error(`Failed to deploy stack: ${errorMessage}`);
+ } finally {
+ process.chdir(originalCwd);
+ console.log(`Restored CWD to: ${originalCwd}.`);
}
+}
- // Handle volumes
- if (serviceConfig.volumes) {
- options.HostConfig = options.HostConfig || {};
- options.HostConfig.Binds = [];
+/**
+ * Start stack (alias for deployStack for compatibility)
+ */
+export async function startStack(stackId: string): Promise<boolean> {
+ return deployStack(stackId);
+}
- for (const volume of serviceConfig.volumes) {
- let source, target, mode;
+/**
+ * Stop stack
+ */
+export async function stopStack(stackId: string): Promise<boolean> {
+ console.log(`Attempting to stop stack ${stackId} by manually stopping containers...`);
+ const docker = await getDockerClient();
+ const composeProjectLabel = 'com.docker.compose.project';
+ let stoppedCount = 0;
+ let removedCount = 0;
- if (typeof volume === 'string') {
- const parts = volume.split(':');
- if (parts.length === 1) {
- // Anonymous volume
- target = parts[0];
- options.Volumes = options.Volumes || {};
- options.Volumes[target] = {};
- continue;
+ try {
+ // Find containers belonging to the stack
+ const containers = await docker.listContainers({
+ all: true,
+ filters: JSON.stringify({
+ label: [`${composeProjectLabel}=${stackId}`]
+ })
+ });
+
+ // Fallback: check by name convention
+ const allContainers = await docker.listContainers({ all: true });
+ const nameFilteredContainers = allContainers.filter((c) => !containers.some((fc) => fc.Id === c.Id) && (c.Labels?.[composeProjectLabel] === stackId || c.Names?.some((name) => name.startsWith(`/${stackId}_`))));
+
+ const seen = new Set<string>();
+ const stackContainers = [...containers, ...nameFilteredContainers].filter((c) => {
+ if (seen.has(c.Id)) return false;
+ seen.add(c.Id);
+ return true;
+ });
+
+ if (stackContainers.length === 0) {
+ return true;
+ }
+
+ console.log(`Found ${stackContainers.length} containers for stack ${stackId}. Attempting to stop and remove...`);
+
+ // Stop and Remove each container
+ for (const containerInfo of stackContainers) {
+ console.log(`Processing container ${containerInfo.Names?.[0]} (ID: ${containerInfo.Id})...`);
+ const container = docker.getContainer(containerInfo.Id);
+ try {
+ // Stop the container if it's running
+ if (containerInfo.State === 'running') {
+ console.log(`Stopping container ${containerInfo.Id}...`);
+ await container.stop();
+ console.log(`Container ${containerInfo.Id} stopped.`);
+ stoppedCount++;
} else {
- source = parts[0];
- target = parts[1];
- mode = parts[2] || 'rw';
+ console.log(`Container ${containerInfo.Id} is already stopped (State: ${containerInfo.State}).`);
}
- } else {
- source = volume.source;
- target = volume.target;
- mode = volume.read_only ? 'ro' : 'rw';
+
+ // Remove the container
+ console.log(`Removing container ${containerInfo.Id}...`);
+ await container.remove({ force: true });
+ console.log(`Container ${containerInfo.Id} removed.`);
+ removedCount++;
+ } catch (containerErr) {
+ console.error(`Error processing container ${containerInfo.Id} for stack ${stackId}:`, containerErr);
}
+ }
- // Handle named volumes vs bind mounts
- if (source.startsWith('.') || source.startsWith('/')) {
- // It's a bind mount - resolve relative paths
- if (source.startsWith('.')) {
- source = path.resolve(stackDir, source);
- }
- } else {
- // It's a named volume
- if (composeData.volumes && composeData.volumes[source]) {
- // Use the full volume name from the volumes section
- const volumeConfig = composeData.volumes[source];
- if (volumeConfig && !volumeConfig.external) {
- source = volumeConfig.name || `${stackId}_${source}`;
+ // Remove networks associated with the stack
+ try {
+ const networks = await docker.listNetworks({
+ filters: JSON.stringify({
+ label: [`${composeProjectLabel}=${stackId}`]
+ })
+ });
+ if (networks.length > 0) {
+ console.log(`Found ${networks.length} networks for stack ${stackId}. Attempting to remove...`);
+ for (const networkInfo of networks) {
+ console.log(`Removing network ${networkInfo.Name} (ID: ${networkInfo.Id})...`);
+ const network = docker.getNetwork(networkInfo.Id);
+ try {
+ await network.remove();
+ console.log(`Network ${networkInfo.Name} removed.`);
+ } catch (networkErr) {
+ console.error(`Error removing network ${networkInfo.Name} (ID: ${networkInfo.Id}):`, networkErr);
}
- } else {
- // Fallback to default Docker volume naming
- source = `${stackId}_${source}`;
}
+ } else {
+ console.log(`No networks found specifically for stack ${stackId}.`);
}
+ } catch (networkListErr) {
+ console.error(`Error listing networks for stack ${stackId}:`, networkListErr);
+ }
- options.HostConfig.Binds.push(`${source}:${target}:${mode}`);
+ console.log(`Stack ${stackId} processing complete. Stopped: ${stoppedCount}, Removed: ${removedCount}.`);
+
+ // Update database with new status
+ try {
+ await updateStackRuntimeInfoInDb(stackId, {
+ status: 'stopped',
+ runningCount: 0,
+ lastPolled: Math.floor(Date.now() / 1000) // ← Solution: Unix timestamp
+ });
+ } catch (dbError) {
+ console.error(`Error updating stack ${stackId} status in database:`, dbError);
}
+
+ stackCache.delete('compose-stacks');
+ return true;
+ } catch (err: unknown) {
+ console.error(`Error during manual stop/remove for stack ${stackId}:`, err);
+ const errorMessage = err instanceof Error ? err.message : String(err);
+ throw new Error(`Failed to stop stack ${stackId}: ${errorMessage}`);
}
+}
+
+/**
+ * Restart stack
+ */
+export async function restartStack(stackId: string): Promise<boolean> {
+ await stopStack(stackId);
+ return deployStack(stackId);
+}
- // Handle restart policy
- if (serviceConfig.restart) {
- options.HostConfig = options.HostConfig || {};
- options.HostConfig.RestartPolicy = parseRestartPolicy(serviceConfig.restart);
+/**
+ * Redeploy stack (pull images and restart)
+ */
+export async function redeployStack(stackId: string): Promise<boolean> {
+ return deployStack(stackId);
+}
+
+/**
+ * Rename stack
+ */
+export async function renameStack(currentStackId: string, newName: string): Promise<Stack> {
+ if (!currentStackId || !newName) {
+ throw new Error('Current stack ID and new name must be provided.');
+ }
+
+ const currentStackDir = await getStackDir(currentStackId);
+ try {
+ await fs.access(currentStackDir);
+ } catch (e) {
+ throw new Error(`Stack with ID '${currentStackId}' not found at ${currentStackDir}.`);
}
- // Handle network mode
- if (serviceConfig.network_mode) {
- options.HostConfig = options.HostConfig || {};
- options.HostConfig.NetworkMode = serviceConfig.network_mode;
- } else if (serviceConfig.networks && Object.keys(serviceConfig.networks).length > 0) {
- // For containers with explicit network configurations
- let serviceNetworks: string[];
- let primaryNetworkKey: string;
+ const newDirBaseName = slugify(newName, {
+ lower: true,
+ strict: true,
+ replacement: '-',
+ trim: true
+ });
- // Handle both array format and object format
- if (Array.isArray(serviceConfig.networks)) {
- // Array format: networks: [test_ext, other_network]
- serviceNetworks = serviceConfig.networks;
- primaryNetworkKey = serviceNetworks[0];
- } else {
- // Object format: networks: { test_ext: {}, other_network: {} }
- serviceNetworks = Object.keys(serviceConfig.networks);
- primaryNetworkKey = serviceNetworks[0];
+ if (newDirBaseName === currentStackId) {
+ throw new Error(`The new name '${newName}' (resolves to '${newDirBaseName}') is effectively the same as the current stack ID '${currentStackId}'. No changes made.`);
+ }
+
+ // Check if the stack is running
+ const running = await isStackRunning(currentStackId);
+ if (running) {
+ throw new Error(`Stack '${currentStackId}' is currently running. Please stop it before renaming.`);
+ }
+
+ const stacksDir = await ensureStacksDir();
+ let newUniqueDirName = newDirBaseName;
+ let counter = 1;
+ const MAX_ATTEMPTS = 100;
+
+ while (counter <= MAX_ATTEMPTS) {
+ const pathToCheck = path.join(stacksDir, newUniqueDirName);
+ const exists = await directoryExists(pathToCheck);
+
+ if (!exists && newUniqueDirName !== currentStackId) {
+ break;
}
- let networkName: string;
+ newUniqueDirName = `${newDirBaseName}-${counter}`;
+ counter++;
+ }
- // Find the network configuration in compose networks section
- const composeNetworks = composeData.networks || {};
+ if (counter > MAX_ATTEMPTS || newUniqueDirName === currentStackId || (await directoryExists(path.join(stacksDir, newUniqueDirName)))) {
+ throw new Error(`Could not generate a unique directory name for '${newName}' that is different from '${currentStackId}' and does not already exist. Please try a different name.`);
+ }
- // Strategy 1: Direct network key match
- if (composeNetworks[primaryNetworkKey]) {
- const networkConfig = composeNetworks[primaryNetworkKey];
- if (networkConfig.external) {
- // Use the external network's specified name
- networkName = networkConfig.name || primaryNetworkKey;
- } else {
- // Internal network - use Docker Compose naming
- networkName = networkConfig.name || `${stackId}_${primaryNetworkKey}`;
+ const newStackDir = path.join(stacksDir, newUniqueDirName);
+
+ try {
+ console.log(`Renaming stack directory from '${currentStackDir}' to '${newStackDir}'...`);
+ await fs.rename(currentStackDir, newStackDir);
+ console.log(`Stack directory for '${currentStackId}' successfully renamed to '${newUniqueDirName}'.`);
+
+ // Update database
+ try {
+ // Delete old entry and create new one
+ await deleteStackFromDb(currentStackId);
+ const updatedStack = await getStack(newUniqueDirName);
+ await saveStackToDb(updatedStack);
+ } catch (dbError) {
+ console.error(`Error updating database after renaming stack:`, dbError);
+ }
+
+ stackCache.delete('compose-stacks');
+ return await getStack(newUniqueDirName);
+ } catch (err) {
+ console.error(`Error renaming stack directory for '${currentStackId}' to '${newUniqueDirName}':`, err);
+ const errorMessage = err instanceof Error ? err.message : String(err);
+ throw new Error(`Failed to rename stack: ${errorMessage}`);
+ }
+}
+
+/**
+ * Discover external stacks
+ */
+export async function discoverExternalStacks(): Promise<Stack[]> {
+ try {
+ const docker = await getDockerClient();
+ const containers = await docker.listContainers({ all: true });
+
+ const composeProjectLabel = 'com.docker.compose.project';
+ const composeServiceLabel = 'com.docker.compose.service';
+
+ const projectMap: Record<string, Array<{ id: string; name: string; state: { Running: boolean; Status: string; ExitCode: number } }>> = {};
+
+ containers.forEach((container) => {
+ const labels = container.Labels || {};
+ const projectName = labels[composeProjectLabel];
+
+ if (projectName) {
+ if (!projectMap[projectName]) {
+ projectMap[projectName] = [];
+ }
+
+ projectMap[projectName].push({
+ id: container.Id,
+ name: labels[composeServiceLabel] || container.Names[0]?.substring(1),
+ state: {
+ Running: container.State === 'running',
+ Status: container.State,
+ ExitCode: 0
+ }
+ });
}
- } else {
- // Strategy 2: Check if service network name matches any external network's name
- const matchingExternalNetwork = Object.entries(composeNetworks).find(([_, config]: [string, any]) => config.external && config.name === primaryNetworkKey);
+ });
- if (matchingExternalNetwork) {
- // Service is directly referencing an external network by its actual name
- networkName = primaryNetworkKey;
- } else {
- // Strategy 3: Fallback - assume it's an internal network
- networkName = `${stackId}_${primaryNetworkKey}`;
+ const externalStacks: Stack[] = [];
+
+ for (const [projectName, services] of Object.entries(projectMap)) {
+ const stackDir = await getStackDir(projectName);
+ try {
+ await fs.access(stackDir);
+ continue; // Stack is managed by Arcane, skip it
+ } catch {
+ // Stack is not managed by Arcane, include it
}
+
+ const serviceCount = services.length;
+ const runningCount = services.filter((s) => s.state.Running).length;
+
+ let status: Stack['status'] = 'stopped';
+ if (runningCount === serviceCount && serviceCount > 0) {
+ status = 'running';
+ } else if (runningCount > 0) {
+ status = 'partially running';
+ }
+
+ externalStacks.push({
+ id: projectName,
+ name: projectName,
+ services,
+ serviceCount,
+ runningCount,
+ status,
+ isExternal: true,
+ createdAt: new Date().toISOString(),
+ updatedAt: new Date().toISOString()
+ });
}
- console.log(`Service ${serviceName} using primary network: ${networkName} (from service network key: ${primaryNetworkKey})`);
+ return externalStacks;
+ } catch (err) {
+ console.error('Error discovering external stacks:', err);
+ return [];
+ }
+}
+
+/**
+ * Import external stack
+ */
+export async function importExternalStack(stackId: string): Promise<Stack> {
+ const docker = await getDockerClient();
+ const containers = await docker.listContainers({ all: true });
+
+ const stackContainers = containers.filter((container) => {
+ const labels = container.Labels || {};
+ return labels['com.docker.compose.project'] === stackId;
+ });
+
+ if (stackContainers.length === 0) {
+ throw new Error(`No containers found for stack '${stackId}'`);
+ }
+
+ const container = stackContainers[0];
+ const labels = container.Labels || {};
+
+ let composeContent = '';
+ let envContent: string | undefined = undefined;
+ let actualComposeFilePathUsed = '';
+
+ const configFilesLabel = labels['com.docker.compose.project.config_files'];
+
+ if (configFilesLabel) {
+ const potentialComposePaths = configFilesLabel
+ .split(',')
+ .map((p) => p.trim())
+ .filter((p) => p);
+
+ let pathToTry = '';
+ if (potentialComposePaths.length > 0) {
+ const primaryNames = ['compose.yaml', 'docker-compose.yml', 'compose.yml', 'docker-compose.yaml'];
+ for (const name of primaryNames) {
+ const foundPath = potentialComposePaths.find((p) => path.basename(p) === name);
+ if (foundPath) {
+ pathToTry = foundPath;
+ break;
+ }
+ }
- // Set the primary network
- options.HostConfig = options.HostConfig || {};
- options.HostConfig.NetworkMode = networkName;
+ if (!pathToTry) {
+ pathToTry = potentialComposePaths[0];
+ }
+ }
- // Add advanced network settings for the primary network (only if object format)
- if (!Array.isArray(serviceConfig.networks) && serviceConfig.networks[primaryNetworkKey]) {
- const endpointConfig = buildEndpointConfig(serviceConfig.networks[primaryNetworkKey]);
+ if (pathToTry) {
+ actualComposeFilePathUsed = pathToTry;
+ try {
+ console.log(`Attempting to read compose file for import from: ${actualComposeFilePathUsed}`);
+ composeContent = await fs.readFile(actualComposeFilePathUsed, 'utf8');
+ console.log(`Successfully read compose file: ${actualComposeFilePathUsed}. Content length: ${composeContent.length}`);
- // If we have DNS settings for the primary network, add them to NetworkingConfig
- if (Object.keys(endpointConfig).length > 0) {
- options.NetworkingConfig = {
- EndpointsConfig: {
- [networkName]: endpointConfig
+ const composeFileDir = path.dirname(actualComposeFilePathUsed);
+ const envFilePath = path.join(composeFileDir, '.env');
+ try {
+ envContent = await fs.readFile(envFilePath, 'utf8');
+ console.log(`Successfully read .env file from: ${envFilePath}`);
+ } catch (envErr) {
+ const nodeEnvErr = envErr as NodeJS.ErrnoException;
+ if (nodeEnvErr.code === 'ENOENT') {
+ console.log(`.env file not found at ${envFilePath}, proceeding without it.`);
+ } else {
+ console.warn(`Could not read .env file at ${envFilePath} during import:`, envErr);
}
- };
- console.log(`Adding network config for primary network ${networkName}:`, endpointConfig);
+ }
+ } catch (err) {
+ console.warn(`Could not read compose file at ${actualComposeFilePathUsed} during import:`, err);
}
+ } else {
+ console.warn(`No suitable compose file path found in 'com.docker.compose.project.config_files' label: "${configFilesLabel}"`);
}
} else {
- // No networks specified on the service level
- const composeNetworks = composeData.networks || {};
-
- // Check if there's a default external network defined
- const defaultExternalNetwork = composeNetworks['default'];
- if (defaultExternalNetwork && defaultExternalNetwork.external) {
- // Use the external default network
- const networkName = defaultExternalNetwork.name || 'default';
- options.HostConfig = options.HostConfig || {};
- options.HostConfig.NetworkMode = networkName;
- console.log(`Service ${serviceName} using external default network: ${networkName}`);
- } else if (Object.keys(composeNetworks).length === 0) {
- // No networks defined at all - create and use default
- const networkName = `${stackId}_default`;
- options.HostConfig = options.HostConfig || {};
- options.HostConfig.NetworkMode = networkName;
- console.log(`Service ${serviceName} using stack default network: ${networkName}`);
- } else {
- // There are networks defined but service doesn't specify any
- // In this case, don't set a network mode - let Docker handle it
- console.log(`Service ${serviceName} has no explicit networks but compose file defines networks - letting Docker handle network assignment`);
+ console.warn(`Label 'com.docker.compose.project.config_files' not found for stack '${stackId}'. Will attempt to generate compose file.`);
+ }
+
+ // Generate compose file if we couldn't read one
+ if (!composeContent) {
+ console.log(`Generating compose file for stack '${stackId}' as no existing file could be read or found.`);
+ const services: Record<string, { image: string }> = {};
+
+ for (const cont of stackContainers) {
+ const containerLabels = cont.Labels || {};
+ const serviceName = containerLabels['com.docker.compose.service'] || cont.Names[0]?.replace(`/${stackId}_`, '').replace(/_\d+$/, '') || `service_${cont.Id.substring(0, 8)}`;
+
+ services[serviceName] = {
+ image: cont.Image
+ };
}
+
+ composeContent = `# Generated compose file for imported stack: ${stackId}
+# This was automatically generated by Arcane from an external stack.
+# The original compose file could not be read from: ${actualComposeFilePathUsed || 'path not specified in labels'}.
+# You may need to adjust this manually for correct operation.
+
+services:
+${yamlDump({ services }, { indent: 2 }).substring('services:'.length).trimStart()}`;
}
- // Handle devices
- if (serviceConfig.devices) {
- options.HostConfig = options.HostConfig || {};
- options.HostConfig.Devices = serviceConfig.devices.map((device: string) => {
- const parts = device.split(':');
- if (parts.length === 1) {
- return { PathOnHost: parts[0], PathInContainer: parts[0], CgroupPermissions: 'rwm' };
- } else if (parts.length === 2) {
- return { PathOnHost: parts[0], PathInContainer: parts[1], CgroupPermissions: 'rwm' };
- } else {
- return { PathOnHost: parts[0], PathInContainer: parts[1], CgroupPermissions: parts[2] };
- }
+ stackCache.delete('compose-stacks');
+ return await createStack(stackId, normalizeHealthcheckTest(composeContent), envContent);
+}
+
+/**
+ * List all stacks
+ */
+export async function listStacks(includeExternal = false): Promise<Stack[]> {
+ const managedStacks = await loadComposeStacks();
+ let allStacks: Stack[] = [...managedStacks];
+
+ if (includeExternal) {
+ const externalStacksList = await discoverExternalStacks();
+ const processedExternalStacks = externalStacksList.map((stack) => ({
+ ...stack,
+ hasArcaneMeta: false
+ }));
+ allStacks = [...allStacks, ...processedExternalStacks];
+ }
+
+ return allStacks;
+}
+
+/**
+ * Check if stack is running
+ */
+export async function isStackRunning(stackId: string): Promise<boolean> {
+ const docker = await getDockerClient();
+ const composeProjectLabel = 'com.docker.compose.project';
+
+ try {
+ const containers = await docker.listContainers({
+ all: true,
+ filters: JSON.stringify({
+ label: [`${composeProjectLabel}=${stackId}`]
+ })
});
+
+ const allContainers = await docker.listContainers({ all: true });
+ const nameFilteredContainers = allContainers.filter((c) => !containers.some((fc) => fc.Id === c.Id) && (c.Labels?.[composeProjectLabel] === stackId || c.Names?.some((name) => name.startsWith(`/${stackId}_`))));
+
+ const stackContainers = [...containers, ...nameFilteredContainers];
+ return stackContainers.some((c) => c.State === 'running');
+ } catch (err) {
+ console.error(`Error checking if stack ${stackId} is running:`, err);
+ return false;
+ }
+}
+
+/**
+ * Update stack auto-update setting
+ */
+export async function updateStackAutoUpdate(stackId: string, autoUpdate: boolean): Promise<void> {
+ try {
+ await updateStackAutoUpdateInDb(stackId, autoUpdate);
+ console.log(`Updated auto-update setting for stack ${stackId} to ${autoUpdate}`);
+ } catch (error) {
+ console.error(`Error updating auto-update setting for stack ${stackId}:`, error);
+ throw error;
}
+}
- // Handle capabilities
- if (serviceConfig.cap_add || serviceConfig.cap_drop) {
- options.HostConfig = options.HostConfig || {};
- if (serviceConfig.cap_add) {
- options.HostConfig.CapAdd = serviceConfig.cap_add;
+/**
+ * Get stacks with auto-update enabled
+ */
+export async function getAutoUpdateStacks(): Promise<Stack[]> {
+ try {
+ return await getAutoUpdateStacksFromDb();
+ } catch (error) {
+ console.error('Error getting auto-update stacks:', error);
+ return [];
+ }
+}
+
+/**
+ * Normalize healthcheck test and substitute variables
+ */
+export function normalizeHealthcheckTest(composeContent: string, envGetter?: (key: string) => string | undefined): string {
+ let doc: any;
+ try {
+ doc = yamlLoad(composeContent);
+ if (!doc || typeof doc !== 'object') {
+ console.warn('Could not parse compose YAML for normalization or content is not an object.');
+ return composeContent;
}
- if (serviceConfig.cap_drop) {
- options.HostConfig.CapDrop = serviceConfig.cap_drop;
+ } catch (e) {
+ console.warn('Could not parse compose YAML for normalization:', e);
+ return composeContent;
+ }
+
+ let modified = false;
+
+ // Perform healthcheck normalization
+ if (doc.services && typeof doc.services === 'object') {
+ for (const serviceName in doc.services) {
+ if (Object.prototype.hasOwnProperty.call(doc.services, serviceName)) {
+ const service = doc.services[serviceName];
+ if (service && service.healthcheck && typeof service.healthcheck === 'object' && service.healthcheck.test && typeof service.healthcheck.test === 'string') {
+ service.healthcheck.test = service.healthcheck.test
+ .trim()
+ .split(/\s+/)
+ .filter((s: string) => s.length > 0);
+ modified = true;
+ }
+ }
+ }
+ }
+
+ // Perform variable substitution if envGetter is provided
+ if (envGetter) {
+ const originalDocSnapshot = JSON.stringify(doc);
+ doc = substituteVariablesInObject(doc, envGetter);
+ if (JSON.stringify(doc) !== originalDocSnapshot) {
+ modified = true;
}
}
- // Handle privileged mode
- if (serviceConfig.privileged) {
- options.HostConfig = options.HostConfig || {};
- options.HostConfig.Privileged = true;
+ if (modified) {
+ // Critical check: ensure container_name does not contain unresolved variables
+ if (doc.services && typeof doc.services === 'object') {
+ for (const serviceName in doc.services) {
+ if (Object.prototype.hasOwnProperty.call(doc.services, serviceName)) {
+ const service = doc.services[serviceName];
+ if (service && typeof service.container_name === 'string' && service.container_name.includes('${')) {
+ console.error(`CRITICAL: Unresolved variable in container_name for service '${serviceName}': ${service.container_name}. ` + `This will likely cause Docker to fail. Ensure the environment variable is defined.`);
+ }
+ }
+ }
+ }
+ return yamlDump(doc, { lineWidth: -1 });
}
+ return composeContent;
+}
+
+/**
+ * Parse YAML content safely
+ */
+export function parseYamlContent(content: string, envGetter?: (key: string) => string | undefined): Record<string, any> | null {
+ try {
+ const parsedYaml = yamlLoad(content);
+
+ if (!parsedYaml || typeof parsedYaml !== 'object') {
+ console.warn('Parsed YAML content is not an object or is null.');
+ return null;
+ }
- // Handle healthcheck
- if (serviceConfig.healthcheck) {
- options.Healthcheck = {
- Test: serviceConfig.healthcheck.test,
- Interval: parseTimeString(serviceConfig.healthcheck.interval, 30000000000), // 30s default
- Timeout: parseTimeString(serviceConfig.healthcheck.timeout, 30000000000), // 30s default
- Retries: serviceConfig.healthcheck.retries || 3,
- StartPeriod: parseTimeString(serviceConfig.healthcheck.start_period, 0) // 0 default
+ if (envGetter) {
+ return substituteVariablesInObject(parsedYaml, envGetter);
+ }
+ return parsedYaml as Record<string, any>;
+ } catch (error) {
+ console.error('Error parsing YAML content:', error);
+ return null;
+ }
+}
+
+// Helper function to recursively substitute variables in an object
+function substituteVariablesInObject(obj: any, envGetter: (key: string) => string | undefined): any {
+ if (Array.isArray(obj)) {
+ return obj.map((item) => substituteVariablesInObject(item, envGetter));
+ } else if (typeof obj === 'object' && obj !== null) {
+ const newObj: Record<string, any> = {};
+ for (const key in obj) {
+ if (Object.prototype.hasOwnProperty.call(obj, key)) {
+ newObj[key] = substituteVariablesInObject(obj[key], envGetter);
+ }
+ }
+ return newObj;
+ } else if (typeof obj === 'string') {
+ let S = obj;
+ for (let i = 0; i < 10 && S.includes('${'); i++) {
+ S = S.replace(/\$\{([^}]+)\}/g, (match, varName) => {
+ const value = envGetter(varName);
+ return value !== undefined ? value : match;
+ });
+ }
+ if (S.includes('${')) {
+ console.warn(`Unresolved variable or recursive substitution limit reached in string: "${obj}". Result: "${S}"`);
+ }
+ return S;
+ }
+ return obj;
+}
+
+/**
+ * Creates all networks defined in the compose file
+ */
+async function createStackNetworks(docker: Dockerode, stackId: string, networks: Record<string, any>): Promise<void> {
+ // If no networks are defined, create a default network for the stack
+ if (!networks || Object.keys(networks).length === 0) {
+ const defaultNetworkName = `${stackId}_default`;
+ try {
+ await docker.createNetwork({
+ Name: defaultNetworkName,
+ Driver: 'bridge',
+ Labels: {
+ 'com.docker.compose.project': stackId,
+ 'com.docker.compose.network': 'default'
+ }
+ });
+ } catch (err: any) {
+ if (err.statusCode === 409) {
+ console.log(`Default network ${defaultNetworkName} already exists, reusing it.`);
+ } else {
+ throw err;
+ }
+ }
+ return;
+ }
+
+ // Process defined networks (only create non-external ones)
+ for (const [networkName, networkConfig] of Object.entries(networks)) {
+ // Skip external networks
+ if (networkConfig.external) {
+ console.log(`Using external network: ${networkConfig.name || networkName}`);
+ continue;
+ }
+
+ // Network creation logic for non-external networks
+ const networkToCreate = {
+ Name: networkConfig.name || `${stackId}_${networkName}`,
+ Driver: networkConfig.driver || 'bridge',
+ Labels: {
+ 'com.docker.compose.project': stackId,
+ 'com.docker.compose.network': networkName
+ },
+ Options: networkConfig.driver_opts || {}
};
- if (serviceConfig.healthcheck.disable === true) {
- options.Healthcheck = { Test: ['NONE'] };
- }
- }
+ try {
+ console.log(`Creating network: ${networkToCreate.Name}`);
+ await docker.createNetwork(networkToCreate);
+ console.log(`Successfully created network: ${networkToCreate.Name}`);
+ } catch (err: any) {
+ if (err.statusCode === 409) {
+ console.log(`Network ${networkToCreate.Name} already exists, reusing it.`);
+ } else {
+ console.error(`Error creating network ${networkToCreate.Name}:`, err);
+ throw err;
+ }
+ }
+ }
+}
+
+/**
+ * Pull image using Docker API
+ */
+async function pullImage(docker: Dockerode, imageTag: string): Promise<void> {
+ return new Promise<void>((resolve, reject) => {
+ docker.pull(imageTag, {}, (pullError: Error | null, stream?: NodeJS.ReadableStream) => {
+ if (pullError) {
+ reject(pullError);
+ return;
+ }
+ if (!stream) {
+ reject(new Error(`Docker pull for ${imageTag} did not return a stream.`));
+ return;
+ }
+ docker.modem.followProgress(
+ stream,
+ (progressError: Error | null, output: any[]) => {
+ if (progressError) {
+ reject(progressError);
+ } else {
+ console.log(`Successfully pulled image: ${imageTag}`);
+ resolve();
+ }
+ },
+ (event: DockerProgressEvent) => {
+ if (event.progress) {
+ console.log(`${imageTag}: ${event.status} ${event.progress}`);
+ } else if (event.status) {
+ console.log(`${imageTag}: ${event.status}`);
+ }
+ }
+ );
+ });
+ });
+}
+
+/**
+ * Create and start all services
+ */
+async function createAndStartServices(docker: Dockerode, stackId: string, composeData: any, stackDir: string): Promise<void> {
+ for (const [serviceName, serviceConfig] of Object.entries(composeData.services || {})) {
+ const service = serviceConfig as any;
+
+ let containerName = service.container_name;
+ if (!containerName || typeof containerName !== 'string') {
+ containerName = `${stackId}_${serviceName}`;
+ }
+ if (containerName.includes('${')) {
+ console.warn(`CRITICAL: Unresolved variable in container_name for service '${serviceName}': ${containerName}. Using default name: ${stackId}_${serviceName}`);
+ containerName = `${stackId}_${serviceName}`;
+ }
+
+ const containerConfig: any = {
+ name: containerName,
+ Image: service.image,
+ User: service.user || '',
+ Labels: {
+ 'com.docker.compose.project': stackId,
+ 'com.docker.compose.service': serviceName,
+ ...service.labels
+ },
+ Env: await prepareEnvironmentVariables(service.environment, stackDir),
+ HostConfig: {
+ RestartPolicy: prepareRestartPolicy(service.restart),
+ Binds: prepareVolumes(service.volumes),
+ PortBindings: preparePorts(service.ports),
+ Dns: service.dns || [],
+ DnsOptions: service.dns_opt || [],
+ DnsSearch: service.dns_search || []
+ }
+ };
+
+ const networkMode = service.network_mode || null;
+ let primaryNetworkName = null;
+ if (networkMode) {
+ containerConfig.HostConfig.NetworkMode = networkMode;
+ } else if (service.networks) {
+ const serviceNetworks = Array.isArray(service.networks) ? service.networks : Object.keys(service.networks);
+
+ if (serviceNetworks.length > 0) {
+ const firstNetName = serviceNetworks[0];
+ const networkDefinition = composeData.networks?.[firstNetName];
+
+ let actualExternalNetIdentifier = firstNetName;
+ if (networkDefinition?.external && networkDefinition.name) {
+ if (typeof networkDefinition.name === 'string' && networkDefinition.name.includes('${') && networkDefinition.name.includes('}')) {
+ console.warn(`External network key '${firstNetName}' has an unresolved variable in name attribute. Using key '${firstNetName}' as identifier.`);
+ } else {
+ actualExternalNetIdentifier = networkDefinition.name;
+ }
+ }
+
+ primaryNetworkName = networkDefinition?.external ? actualExternalNetIdentifier : `${stackId}_${firstNetName}`;
+ containerConfig.HostConfig.NetworkMode = primaryNetworkName;
+ console.log(`Service ${serviceName} will use '${primaryNetworkName}' as primary network.`);
+ }
+ }
+
+ // Handle additional networks
+ const networkingConfig: { EndpointsConfig?: any } = {};
+ if (!networkMode && service.networks) {
+ const serviceNetworks = Array.isArray(service.networks) ? service.networks : Object.keys(service.networks);
+ const additionalNetworks = primaryNetworkName ? serviceNetworks.slice(1) : serviceNetworks;
+
+ if (additionalNetworks.length > 0) {
+ networkingConfig.EndpointsConfig = {};
+
+ for (const netName of additionalNetworks) {
+ const serviceNetConfig = typeof service.networks === 'object' ? service.networks[netName] : {};
+ const networkDefinition = composeData.networks?.[netName];
- // Handle logging
- if (serviceConfig.logging) {
- options.HostConfig = options.HostConfig || {};
- options.HostConfig.LogConfig = {
- Type: serviceConfig.logging.driver || 'json-file',
- Config: serviceConfig.logging.options || {}
- };
- }
+ let actualExternalNetIdentifier = netName;
+ if (networkDefinition?.external && networkDefinition.name) {
+ if (typeof networkDefinition.name === 'string' && networkDefinition.name.includes('${') && networkDefinition.name.includes('}')) {
+ console.warn(`External network key '${netName}' in stack '${stackId}' has a 'name' attribute '${networkDefinition.name}' that appears to be an unresolved variable. Using the network key '${netName}' as the identifier for connection.`);
+ } else {
+ actualExternalNetIdentifier = networkDefinition.name;
+ }
+ }
+ const fullNetworkName = networkDefinition?.external ? actualExternalNetIdentifier : `${stackId}_${netName}`;
- // Handle DNS settings
- if (serviceConfig.dns || serviceConfig.dns_search || serviceConfig.dns_opt) {
- options.HostConfig = options.HostConfig || {};
+ const endpointConfig: any = {};
- // Configure DNS servers
- if (serviceConfig.dns) {
- options.HostConfig.Dns = Array.isArray(serviceConfig.dns) ? serviceConfig.dns : [serviceConfig.dns];
- console.log(`Setting DNS servers for ${serviceName}:`, options.HostConfig.Dns);
- }
+ if (typeof serviceNetConfig === 'object' && serviceNetConfig.aliases) {
+ endpointConfig.Aliases = serviceNetConfig.aliases;
+ }
- // Configure DNS search domains
- if (serviceConfig.dns_search) {
- options.HostConfig.DnsSearch = Array.isArray(serviceConfig.dns_search) ? serviceConfig.dns_search : [serviceConfig.dns_search];
- console.log(`Setting DNS search domains for ${serviceName}:`, options.HostConfig.DnsSearch);
- }
+ if (typeof serviceNetConfig === 'object') {
+ const ipamConfig: any = {};
- // Configure DNS options
- if (serviceConfig.dns_opt) {
- options.HostConfig.DnsOptions = Array.isArray(serviceConfig.dns_opt) ? serviceConfig.dns_opt : [serviceConfig.dns_opt];
- console.log(`Setting DNS options for ${serviceName}:`, options.HostConfig.DnsOptions);
- }
- }
+ if (serviceNetConfig.ipv4_address) {
+ ipamConfig.IPv4Address = serviceNetConfig.ipv4_address;
+ }
- // Handle extra_hosts (add entries to /etc/hosts)
- if (serviceConfig.extra_hosts) {
- options.HostConfig = options.HostConfig || {};
- options.HostConfig.ExtraHosts = Array.isArray(serviceConfig.extra_hosts) ? serviceConfig.extra_hosts : [serviceConfig.extra_hosts];
- console.log(`Setting extra hosts for ${serviceName}:`, options.HostConfig.ExtraHosts);
- }
+ if (serviceNetConfig.ipv6_address) {
+ ipamConfig.IPv6Address = serviceNetConfig.ipv6_address;
+ }
- return options;
-}
+ if (Object.keys(ipamConfig).length > 0) {
+ endpointConfig.IPAMConfig = ipamConfig;
+ }
+ }
-/**
- * Parses a time string to nanoseconds (Docker API format)
- */
-function parseTimeString(timeStr: string | undefined, defaultValue: number): number {
- if (!timeStr) return defaultValue;
+ networkingConfig.EndpointsConfig[fullNetworkName] = endpointConfig;
+ }
+ }
+ }
- const match = timeStr.match(/^(\d+)(ms|s|m|h)$/);
- if (!match) return defaultValue;
+ try {
+ const container = await docker.createContainer(containerConfig);
+ console.log(`Successfully created container: ${containerName} (ID: ${container.id})`);
- const value = parseInt(match[1]);
- const unit = match[2];
+ if (networkingConfig.EndpointsConfig && Object.keys(networkingConfig.EndpointsConfig).length > 0) {
+ for (const netNameKey of Object.keys(networkingConfig.EndpointsConfig)) {
+ try {
+ console.log(`Connecting container ${container.id} to network: ${netNameKey} with config:`, JSON.stringify(networkingConfig.EndpointsConfig[netNameKey]));
- const multipliers: Record<string, number> = {
- ms: 1000000, // milliseconds to nanoseconds
- s: 1000000000, // seconds to nanoseconds
- m: 60000000000, // minutes to nanoseconds
- h: 3600000000000 // hours to nanoseconds
- };
+ const network = docker.getNetwork(netNameKey);
+ await network.connect({
+ Container: container.id,
+ EndpointConfig: networkingConfig.EndpointsConfig[netNameKey] || {}
+ });
+ console.log(`Successfully connected ${container.id} to network: ${netNameKey}`);
+ } catch (netConnectErr) {
+ console.error(`Error connecting container ${container.id} to network ${netNameKey}:`, netConnectErr);
+ throw new Error(`Failed to connect container ${container.id} to network ${netNameKey}: ${netConnectErr instanceof Error ? netConnectErr.message : String(netConnectErr)}`);
+ }
+ }
+ }
- return value * multipliers[unit];
+ console.log(`Starting container: ${containerName} (ID: ${container.id})`);
+ await container.start();
+ console.log(`Successfully started container: ${containerName}`);
+ } catch (createErr) {
+ console.error(`Error creating/starting container for service ${serviceName} (${containerName}):`, createErr);
+ throw createErr;
+ }
+ }
}
/**
- * Parses restart policy string to Docker format
+ * Deploy stack with external networks (custom approach)
*/
-function parseRestartPolicy(policy: string): any {
- if (policy === 'no' || policy === 'none') {
- return { Name: 'no' };
- } else if (policy === 'always') {
- return { Name: 'always' };
- } else if (policy === 'unless-stopped') {
- return { Name: 'unless-stopped' };
- } else if (policy.startsWith('on-failure')) {
- const parts = policy.split(':');
- if (parts.length > 1) {
- return { Name: 'on-failure', MaximumRetryCount: parseInt(parts[1]) };
- } else {
- return { Name: 'on-failure' };
+async function deployStackWithExternalNetworks(stackId: string, composeData: any, stackDir: string): Promise<void> {
+ const docker = await getDockerClient();
+
+ // Pull all images
+ console.log(`Pulling images for stack ${stackId} with external networks...`);
+ for (const [serviceName, serviceConfig] of Object.entries(composeData.services || {})) {
+ const serviceImage = (serviceConfig as any).image;
+ if (serviceImage) {
+ console.log(`Pulling image for service ${serviceName}: ${serviceImage}`);
+ try {
+ await pullImage(docker, serviceImage);
+ } catch (pullErr) {
+ console.warn(`Warning: Failed to pull image ${serviceImage} for service ${serviceName}:`, pullErr);
+ }
}
}
- // Default
- return { Name: 'no' };
+ // Create networks
+ await createStackNetworks(docker, stackId, composeData.networks || {});
+
+ // Create and start services
+ await createAndStartServices(docker, stackId, composeData, stackDir);
+
+ console.log(`Successfully deployed stack ${stackId} with external networks`);
}
/**
- * Cleans up containers from a failed stack deployment
+ * Clean up failed deployment
*/
async function cleanupFailedDeployment(stackId: string): Promise<void> {
console.log(`Cleaning up containers for failed deployment of stack ${stackId}...`);
@@ -865,7 +1718,6 @@ async function cleanupFailedDeployment(stackId: string): Promise<void> {
const composeProjectLabel = 'com.docker.compose.project';
try {
- // Find containers belonging to this stack
const containers = await docker.listContainers({
all: true,
filters: JSON.stringify({
@@ -873,231 +1725,611 @@ async function cleanupFailedDeployment(stackId: string): Promise<void> {
})
});
- // Also find by name convention as fallback
- const containersByName = await docker.listContainers({
- all: true,
- filters: JSON.stringify({
- name: [`${stackId}_`]
- })
- });
+ const allContainers = await docker.listContainers({ all: true });
+ const nameFilteredContainers = allContainers.filter((c) => !containers.some((fc) => fc.Id === c.Id) && c.Names?.some((name) => name.startsWith(`/${stackId}_`)));
- // Combine the results (removing duplicates)
- const allContainerIds = new Set([...containers.map((c) => c.Id), ...containersByName.map((c) => c.Id)]);
+ const stackContainers = [...containers, ...nameFilteredContainers];
- // Stop and remove all containers
- for (const containerId of allContainerIds) {
- const container = docker.getContainer(containerId);
+ if (stackContainers.length === 0) {
+ console.log(`No containers found for stack ${stackId}.`);
+ return;
+ }
+ for (const containerInfo of stackContainers) {
+ const container = docker.getContainer(containerInfo.Id);
try {
- const containerInfo = await container.inspect();
- if (containerInfo.State.Running) {
- console.log(`Stopping container ${containerInfo.Name}...`);
+ const containerDetails = await container.inspect();
+ if (containerDetails.State.Running) {
+ console.log(`Stopping container ${containerInfo.Id}...`);
await container.stop();
}
-
- console.log(`Removing container ${containerInfo.Name}...`);
+ console.log(`Removing container ${containerInfo.Id}...`);
await container.remove();
} catch (err) {
- console.warn(`Error cleaning up container ${containerId}:`, err);
+ console.error(`Error cleaning up container ${containerInfo.Id}:`, err);
}
}
-
- console.log(`Cleanup completed for stack ${stackId}.`);
} catch (err) {
- console.error(`Error during cleanup of stack ${stackId}:`, err);
- throw err;
+ console.error(`Error cleaning up failed deployment for stack ${stackId}:`, err);
}
}
/**
- * Stops a stack by stopping and removing all containers
+ * Prepare environment variables for container creation
*/
-export async function stopStack(stackId: string): Promise<boolean> {
- console.log(`Stopping stack ${stackId}...`);
+async function prepareEnvironmentVariables(environment: any, stackDir: string): Promise<string[]> {
+ const envArray: string[] = [];
+
+ if (Array.isArray(environment)) {
+ // Handle array format: ['KEY=value', 'KEY2=value2']
+ envArray.push(...environment);
+ } else if (typeof environment === 'object' && environment !== null) {
+ // Handle object format: { KEY: 'value', KEY2: 'value2' }
+ for (const [key, value] of Object.entries(environment)) {
+ envArray.push(`${key}=${value}`);
+ }
+ }
+
+ // Load .env file and add variables if they don't already exist
try {
- await cleanupFailedDeployment(stackId);
- stackCache.delete('compose-stacks');
- return true;
- } catch (err) {
- console.error(`Error stopping stack ${stackId}:`, err);
- throw new Error(`Failed to stop stack: ${err instanceof Error ? err.message : String(err)}`);
+ const envFilePath = path.join(stackDir, '.env');
+ const envFileContent = await fs.readFile(envFilePath, 'utf8');
+ const envVars = parseEnvContent(envFileContent);
+
+ for (const [key, value] of Object.entries(envVars)) {
+ // Only add if not already defined in compose environment
+ const keyExists = envArray.some((env) => env.startsWith(`${key}=`));
+ if (!keyExists) {
+ envArray.push(`${key}=${value}`);
+ }
+ }
+ } catch (envError) {
+ // .env file doesn't exist or can't be read, that's okay
}
+
+ return envArray;
}
/**
- * Redeploys a stack by stopping it, pulling images, and redeploying
+ * Prepare restart policy for container
*/
-export async function redeployStack(stackId: string): Promise<boolean> {
- console.log(`Redeploying stack ${stackId}...`);
+function prepareRestartPolicy(restart: string | undefined): any {
+ if (!restart) {
+ return { Name: 'no' };
+ }
+
+ switch (restart) {
+ case 'always':
+ return { Name: 'always' };
+ case 'unless-stopped':
+ return { Name: 'unless-stopped' };
+ case 'on-failure':
+ return { Name: 'on-failure', MaximumRetryCount: 0 };
+ case 'no':
+ return { Name: 'no' };
+ default:
+ // Handle on-failure:5 format
+ if (restart.startsWith('on-failure:')) {
+ const retryCount = parseInt(restart.split(':')[1]) || 0;
+ return { Name: 'on-failure', MaximumRetryCount: retryCount };
+ }
+ return { Name: 'no' };
+ }
+}
+
+/**
+ * Prepare volumes for container
+ */
+function prepareVolumes(volumes: any[]): string[] {
+ if (!Array.isArray(volumes)) {
+ return [];
+ }
+
+ return volumes
+ .map((volume) => {
+ if (typeof volume === 'string') {
+ return volume;
+ } else if (typeof volume === 'object' && volume.type === 'bind') {
+ // Handle long format: { type: 'bind', source: '/host/path', target: '/container/path' }
+ const readonly = volume.read_only ? ':ro' : '';
+ return `${volume.source}:${volume.target}${readonly}`;
+ }
+ return '';
+ })
+ .filter((v) => v);
+}
+
+/**
+ * Prepare port bindings for container
+ */
+function preparePorts(ports: any[]): any {
+ if (!Array.isArray(ports)) {
+ return {};
+ }
+
+ const portBindings: any = {};
+
+ for (const port of ports) {
+ if (typeof port === 'string') {
+ // Handle "8080:80" or "80" format
+ const parts = port.split(':');
+ if (parts.length === 2) {
+ const [hostPort, containerPort] = parts;
+ const containerPortKey = containerPort.includes('/') ? containerPort : `${containerPort}/tcp`;
+ portBindings[containerPortKey] = [{ HostPort: hostPort }];
+ } else if (parts.length === 1) {
+ // Just container port, let Docker assign host port
+ const containerPortKey = parts[0].includes('/') ? parts[0] : `${parts[0]}/tcp`;
+ portBindings[containerPortKey] = [{}];
+ }
+ } else if (typeof port === 'object') {
+ // Handle long format: { target: 80, published: 8080, protocol: 'tcp' }
+ const containerPortKey = `${port.target}/${port.protocol || 'tcp'}`;
+ if (port.published) {
+ portBindings[containerPortKey] = [{ HostPort: port.published.toString() }];
+ } else {
+ portBindings[containerPortKey] = [{}];
+ }
+ }
+ }
+
+ return portBindings;
+}
+/**
+ * Delete stack - removes from both database and files
+ */
+export async function deleteStack(stackId: string, removeFiles = false): Promise<boolean> {
try {
- // First stop the stack
- console.log(`Stopping existing stack ${stackId}...`);
+ // Stop the stack first
await stopStack(stackId);
- // Pull fresh images
- console.log(`Pulling fresh images for stack ${stackId}...`);
- await pullImagesForStack(stackId);
+ // Remove from database
+ try {
+ await deleteStackFromDb(stackId);
+ console.log(`Stack ${stackId} removed from database`);
+ } catch (dbError) {
+ console.error(`Error removing stack ${stackId} from database:`, dbError);
+ }
- // Deploy the stack with fresh images
- console.log(`Deploying stack ${stackId}...`);
- const result = await deployStack(stackId);
+ // Remove files if requested
+ if (removeFiles) {
+ const stackDir = await getStackDir(stackId);
+ try {
+ await fs.rm(stackDir, { recursive: true, force: true });
+ console.log(`Stack ${stackId} files removed from ${stackDir}`);
+ } catch (fileError) {
+ console.error(`Error removing stack ${stackId} files:`, fileError);
+ throw new Error(`Failed to remove stack files: ${fileError}`);
+ }
+ }
- console.log(`Stack ${stackId} has been successfully redeployed`);
- return result;
- } catch (err) {
- console.error(`Error redeploying stack ${stackId}:`, err);
- throw new Error(`Failed to redeploy stack: ${err instanceof Error ? err.message : String(err)}`);
+ stackCache.delete('compose-stacks');
+ return true;
+ } catch (error) {
+ console.error(`Error deleting stack ${stackId}:`, error);
+ throw error;
}
}
/**
- * Restarts all containers in a stack without redeploying
+ * Pull images for a stack
*/
-export async function restartStack(stackId: string): Promise<boolean> {
- console.log(`Restarting stack ${stackId}...`);
- const docker = await getDockerClient();
- const composeProjectLabel = 'com.docker.compose.project';
+export async function pullStackImages(stackId: string): Promise<boolean> {
+ try {
+ const stack = await getStack(stackId);
+ if (!stack.composeContent) {
+ throw new Error(`No compose content found for stack ${stackId}`);
+ }
+
+ const composeData = parseYamlContent(stack.composeContent);
+ if (!composeData || !composeData.services) {
+ throw new Error(`No services found in compose file for stack ${stackId}`);
+ }
+
+ const docker = await getDockerClient();
+ const pullPromises = [];
+
+ for (const [serviceName, serviceConfig] of Object.entries(composeData.services)) {
+ const service = serviceConfig as any;
+ if (service.image) {
+ console.log(`Pulling image for service ${serviceName}: ${service.image}`);
+ pullPromises.push(pullImage(docker, service.image));
+ }
+ }
+ await Promise.all(pullPromises);
+ console.log(`Successfully pulled all images for stack ${stackId}`);
+ return true;
+ } catch (error) {
+ console.error(`Error pulling images for stack ${stackId}:`, error);
+ throw error;
+ }
+}
+
+/**
+ * Get stack logs
+ */
+export async function getStackLogs(stackId: string, options: { tail?: number; follow?: boolean } = {}): Promise<any> {
try {
- // Find containers belonging to this stack
+ const docker = await getDockerClient();
const containers = await docker.listContainers({
all: true,
filters: JSON.stringify({
- label: [`${composeProjectLabel}=${stackId}`]
+ label: [`com.docker.compose.project=${stackId}`]
})
});
- // Also find by name convention as fallback
- const containersByName = await docker.listContainers({
+ const logs: Record<string, string> = {};
+
+ for (const containerInfo of containers) {
+ const container = docker.getContainer(containerInfo.Id);
+ try {
+ let logStream;
+ if (options.follow === true) {
+ logStream = await container.logs({
+ stdout: true,
+ stderr: true,
+ tail: options.tail || 100,
+ follow: true
+ });
+ } else {
+ logStream = await container.logs({
+ stdout: true,
+ stderr: true,
+ tail: options.tail || 100,
+ follow: false
+ });
+ }
+
+ const serviceName = containerInfo.Labels?.['com.docker.compose.service'] || containerInfo.Names?.[0];
+ logs[serviceName || containerInfo.Id] = logStream.toString();
+ } catch (logError) {
+ console.error(`Error getting logs for container ${containerInfo.Id}:`, logError);
+ }
+ }
+
+ return logs;
+ } catch (error) {
+ console.error(`Error getting logs for stack ${stackId}:`, error);
+ throw error;
+ }
+}
+
+/**
+ * Get stack statistics
+ */
+export async function getStackStats(stackId: string): Promise<any> {
+ try {
+ const docker = await getDockerClient();
+ const containers = await docker.listContainers({
all: true,
filters: JSON.stringify({
- name: [`${stackId}_`]
+ label: [`com.docker.compose.project=${stackId}`]
})
});
- // Combine the results (removing duplicates)
- const allContainerIds = new Set([...containers.map((c) => c.Id), ...containersByName.map((c) => c.Id)]);
+ const stats: Record<string, any> = {};
- if (allContainerIds.size === 0) {
- console.log(`No containers found for stack ${stackId}, nothing to restart`);
- return false;
+ for (const containerInfo of containers) {
+ if (containerInfo.State === 'running') {
+ const container = docker.getContainer(containerInfo.Id);
+ try {
+ const statsStream = await container.stats({ stream: false });
+ const serviceName = containerInfo.Labels?.['com.docker.compose.service'] || containerInfo.Names?.[0];
+ stats[serviceName || containerInfo.Id] = statsStream;
+ } catch (statsError) {
+ console.error(`Error getting stats for container ${containerInfo.Id}:`, statsError);
+ }
+ }
}
- // Restart all containers
- for (const containerId of allContainerIds) {
- const container = docker.getContainer(containerId);
+ return stats;
+ } catch (error) {
+ console.error(`Error getting stats for stack ${stackId}:`, error);
+ throw error;
+ }
+}
- try {
- const containerInfo = await container.inspect();
- console.log(`Restarting container ${containerInfo.Name}...`);
- await container.restart({ t: 10 }); // 10 second timeout for restart
- } catch (err) {
- console.warn(`Error restarting container ${containerId}:`, err);
- throw err;
+/**
+ * Validate compose content
+ */
+export function validateComposeContent(content: string): { valid: boolean; errors: string[] } {
+ const errors: string[] = [];
+
+ try {
+ const parsed = yamlLoad(content);
+
+ if (!parsed || typeof parsed !== 'object') {
+ errors.push('Compose content must be a valid YAML object');
+ return { valid: false, errors };
+ }
+
+ const compose = parsed as any;
+
+ // Check for required version (optional but recommended)
+ if (!compose.version && !compose.services) {
+ errors.push('Compose file should have either a version field or services field');
+ }
+
+ // Check services
+ if (!compose.services || typeof compose.services !== 'object') {
+ errors.push('Compose file must have a services section');
+ } else {
+ // Validate each service
+ for (const [serviceName, serviceConfig] of Object.entries(compose.services)) {
+ const service = serviceConfig as any;
+
+ if (!service.image && !service.build) {
+ errors.push(`Service '${serviceName}' must have either an 'image' or 'build' field`);
+ }
+
+ // Check for common misconfigurations
+ if (service.container_name && typeof service.container_name === 'string' && service.container_name.includes('${')) {
+ errors.push(`Service '${serviceName}' has unresolved variables in container_name: ${service.container_name}`);
+ }
}
}
- console.log(`Successfully restarted all containers in stack ${stackId}`);
- return true;
- } catch (err) {
- console.error(`Error restarting stack ${stackId}:`, err);
- throw new Error(`Failed to restart stack: ${err instanceof Error ? err.message : String(err)}`);
+ // Validate networks if present
+ if (compose.networks && typeof compose.networks === 'object') {
+ for (const [networkName, networkConfig] of Object.entries(compose.networks)) {
+ const network = networkConfig as any;
+
+ if (network.external && !network.name && typeof networkName !== 'string') {
+ errors.push(`External network '${networkName}' should have a name field`);
+ }
+ }
+ }
+ } catch (parseError) {
+ errors.push(`YAML parsing error: ${parseError instanceof Error ? parseError.message : String(parseError)}`);
}
+
+ return {
+ valid: errors.length === 0,
+ errors
+ };
}
/**
- * Completely destroys a stack, removing containers, networks, and optionally volumes and files
+ * Export stack configuration
*/
-export async function destroyStack(stackId: string, removeVolumes: boolean = false, removeFiles: boolean = false): Promise<boolean> {
- console.log(`Destroying stack ${stackId}${removeVolumes ? ' including volumes' : ''}...`);
- const docker = await getDockerClient();
- const composeProjectLabel = 'com.docker.compose.project';
-
+export async function exportStack(stackId: string): Promise<{ composeContent: string; envContent: string; name: string }> {
try {
- // First stop and remove all containers
- console.log(`Stopping and removing containers for stack ${stackId}...`);
- await cleanupFailedDeployment(stackId);
+ const stack = await getStack(stackId);
- // Remove networks
- console.log(`Removing networks for stack ${stackId}...`);
- const networks = await docker.listNetworks({
- filters: JSON.stringify({
- label: [`${composeProjectLabel}=${stackId}`]
- })
- });
+ return {
+ name: stack.name,
+ composeContent: stack.composeContent || '',
+ envContent: stack.envContent || ''
+ };
+ } catch (error) {
+ console.error(`Error exporting stack ${stackId}:`, error);
+ throw error;
+ }
+}
- // Also find networks by name convention
- const networksByName = await docker.listNetworks({
+/**
+ * Get stack health status
+ */
+export async function getStackHealth(stackId: string): Promise<{ healthy: boolean; services: Record<string, { healthy: boolean; status: string }> }> {
+ try {
+ const docker = await getDockerClient();
+ const containers = await docker.listContainers({
+ all: true,
filters: JSON.stringify({
- name: [`${stackId}_`]
+ label: [`com.docker.compose.project=${stackId}`]
})
});
- // Combine network results (removing duplicates)
- const allNetworkIds = new Set([...networks.map((n) => n.Id), ...networksByName.map((n) => n.Id)]);
+ const services: Record<string, { healthy: boolean; status: string }> = {};
+ let overallHealthy = true;
- for (const networkId of allNetworkIds) {
- try {
- const network = docker.getNetwork(networkId);
- const networkInfo = await network.inspect();
- console.log(`Removing network ${networkInfo.Name}...`);
- await network.remove();
- } catch (err) {
- console.warn(`Error removing network ${networkId}:`, err);
+ for (const containerInfo of containers) {
+ const serviceName = containerInfo.Labels?.['com.docker.compose.service'] || containerInfo.Names?.[0] || containerInfo.Id;
+
+ let healthy = false;
+ let status = containerInfo.State;
+
+ if (containerInfo.State === 'running') {
+ // Check if container has health check
+ try {
+ const container = docker.getContainer(containerInfo.Id);
+ const details = await container.inspect();
+
+ if (details.State.Health) {
+ healthy = details.State.Health.Status === 'healthy';
+ status = details.State.Health.Status;
+ } else {
+ // No health check, consider running as healthy
+ healthy = true;
+ status = 'running (no healthcheck)';
+ }
+ } catch (inspectError) {
+ console.error(`Error inspecting container ${containerInfo.Id}:`, inspectError);
+ healthy = false;
+ status = 'unknown';
+ }
+ }
+
+ services[serviceName] = { healthy, status };
+
+ if (!healthy) {
+ overallHealthy = false;
}
}
+ return {
+ healthy: overallHealthy,
+ services
+ };
+ } catch (error) {
+ console.error(`Error getting health status for stack ${stackId}:`, error);
+ throw error;
+ }
+}
+
+/**
+ * Destroy stack - alias for deleteStack with additional volume cleanup options
+ * This function matches the API expected by the destroy endpoint
+ */
+export async function destroyStack(stackId: string, removeVolumes = false, removeFiles = false): Promise<boolean> {
+ try {
+ console.log(`Destroying stack ${stackId} (removeVolumes: ${removeVolumes}, removeFiles: ${removeFiles})`);
+
+ // Stop the stack first
+ await stopStack(stackId);
+
// Remove volumes if requested
if (removeVolumes) {
- console.log(`Removing volumes for stack ${stackId}...`);
- const volumes = await docker.listVolumes({
- filters: JSON.stringify({
- label: [`${composeProjectLabel}=${stackId}`]
- })
- });
+ try {
+ const docker = await getDockerClient();
- // Also find volumes by name convention
- const volumesByName =
- (
- await docker.listVolumes({
- filters: JSON.stringify({
- name: [`${stackId}_`]
- })
+ // Find volumes associated with this stack
+ const volumes = await docker.listVolumes({
+ filters: JSON.stringify({
+ label: [`com.docker.compose.project=${stackId}`]
})
- ).Volumes || [];
+ });
- const allVolumeNames = new Set([...(volumes.Volumes || []).map((v) => v.Name), ...volumesByName.map((v) => v.Name)]);
+ if (volumes.Volumes && volumes.Volumes.length > 0) {
+ console.log(`Found ${volumes.Volumes.length} volumes for stack ${stackId}. Removing...`);
- for (const volumeName of allVolumeNames) {
- try {
- console.log(`Removing volume ${volumeName}...`);
- await removeVolume(volumeName);
- } catch (err) {
- console.warn(`Error removing volume ${volumeName}:`, err);
+ for (const volumeInfo of volumes.Volumes) {
+ try {
+ const volume = docker.getVolume(volumeInfo.Name);
+ await volume.remove();
+ console.log(`Removed volume: ${volumeInfo.Name}`);
+ } catch (volumeError) {
+ console.error(`Error removing volume ${volumeInfo.Name}:`, volumeError);
+ }
+ }
+ } else {
+ console.log(`No volumes found for stack ${stackId}`);
}
+ } catch (volumeListError) {
+ console.error(`Error listing volumes for stack ${stackId}:`, volumeListError);
}
}
- // Remove stack files if requested
+ // Remove from database
+ try {
+ await deleteStackFromDb(stackId);
+ console.log(`Stack ${stackId} removed from database`);
+ } catch (dbError) {
+ console.error(`Error removing stack ${stackId} from database:`, dbError);
+ }
+
+ // Remove files if requested
if (removeFiles) {
- console.log(`Removing stack files for ${stackId}...`);
const stackDir = await getStackDir(stackId);
try {
await fs.rm(stackDir, { recursive: true, force: true });
- console.log(`Successfully removed stack directory: ${stackDir}`);
- } catch (err) {
- console.warn(`Error removing stack files at ${stackDir}:`, err);
- // Continue execution - don't fail if files can't be removed
+ console.log(`Stack ${stackId} files removed from ${stackDir}`);
+ } catch (fileError) {
+ console.error(`Error removing stack ${stackId} files:`, fileError);
+ throw new Error(`Failed to remove stack files: ${fileError}`);
}
}
- // Clear cache
stackCache.delete('compose-stacks');
- console.log(`Stack ${stackId} has been successfully destroyed`);
+ console.log(`Successfully destroyed stack ${stackId}`);
return true;
- } catch (err) {
- console.error(`Error destroying stack ${stackId}:`, err);
- throw new Error(`Failed to destroy stack: ${err instanceof Error ? err.message : String(err)}`);
+ } catch (error) {
+ console.error(`Error destroying stack ${stackId}:`, error);
+ throw error;
+ }
+}
+
+/**
+ * Background service to update stack runtime info
+ */
+export class StackRuntimeUpdater {
+ private updateInterval: NodeJS.Timeout | null = null;
+ private isUpdating = false;
+
+ start(intervalMinutes = 2) {
+ if (this.updateInterval) return;
+
+ this.updateInterval = setInterval(
+ async () => {
+ if (this.isUpdating) return;
+
+ this.isUpdating = true;
+ try {
+ await this.updateAllStacksRuntimeInfo();
+ } catch (error) {
+ console.error('Background stack runtime update failed:', error);
+ } finally {
+ this.isUpdating = false;
+ }
+ },
+ intervalMinutes * 60 * 1000
+ );
+
+ console.log(`Stack runtime updater started (${intervalMinutes}m interval)`);
+ }
+
+ stop() {
+ if (this.updateInterval) {
+ clearInterval(this.updateInterval);
+ this.updateInterval = null;
+ }
+ }
+
+ private async updateAllStacksRuntimeInfo() {
+ try {
+ const stacks = await listStacksFromDb();
+ console.log(`Background updating runtime info for ${stacks.length} stacks`);
+
+ for (const stack of stacks) {
+ try {
+ await this.updateSingleStackRuntimeInfo(stack.id);
+ } catch (error) {
+ console.warn(`Failed to update runtime info for stack ${stack.id}:`, error);
+ }
+ }
+
+ // Invalidate cache after updates
+ stackCache.delete('compose-stacks');
+ } catch (error) {
+ console.error('Background runtime update failed:', error);
+ }
+ }
+
+ private async updateSingleStackRuntimeInfo(stackId: string) {
+ const stack = await getStackByIdFromDb(stackId);
+ if (!stack) return;
+
+ let composeContent = stack.composeContent;
+ if (!composeContent) {
+ const composePath = await getComposeFilePath(stackId);
+ if (composePath) {
+ composeContent = await fs.readFile(composePath, 'utf8');
+ await updateStackContentInDb(stackId, { composeContent });
+ }
+ }
+
+ const services = await getStackServices(stackId, composeContent || '');
+ const serviceCount = services.length;
+ const runningCount = services.filter((s) => s.state?.Running).length;
+
+ let status: Stack['status'] = 'stopped';
+ if (serviceCount === 0) {
+ status = 'unknown';
+ } else if (runningCount === serviceCount) {
+ status = 'running';
+ } else if (runningCount > 0) {
+ status = 'partially running';
+ }
+
+ await updateStackRuntimeInfoInDb(stackId, {
+ status,
+ serviceCount,
+ runningCount,
+ lastPolled: new Date()
+ });
}
}
+
+export const stackRuntimeUpdater = new StackRuntimeUpdater();
diff --git a/src/lib/services/docker/stack-migration-service.ts b/src/lib/services/docker/stack-migration-service.ts
index e4428227d..e4212bce6 100644
--- a/src/lib/services/docker/stack-migration-service.ts
+++ b/src/lib/services/docker/stack-migration-service.ts
@@ -1,7 +1,7 @@
import fs from 'node:fs/promises';
import path from 'node:path';
import slugify from 'slugify';
-import { ensureStacksDir, stopStack, startStack, isStackRunning } from './stack-service';
+import { ensureStacksDir, stopStack, startStack, isStackRunning } from './stack-custom-service';
export async function migrateStacksToNameFolders() {
const stacksDir = await ensureStacksDir();
diff --git a/src/lib/services/docker/stack-service.ts b/src/lib/services/docker/stack-service.ts
deleted file mode 100644
index 7ec23b4e2..000000000
--- a/src/lib/services/docker/stack-service.ts
+++ /dev/null
@@ -1,1744 +0,0 @@
-import { promises as fs } from 'node:fs';
-import path, { join } from 'node:path';
-import DockerodeCompose from 'dockerode-compose';
-import { load as yamlLoad, dump as yamlDump } from 'js-yaml';
-import slugify from 'slugify';
-import { directoryExists } from '$lib/utils/fs.utils';
-import { getDockerClient } from '$lib/services/docker/core';
-import { getSettings, ensureStacksDirectory } from '$lib/services/settings-service';
-import type { Stack, StackService, StackUpdate } from '$lib/types/docker/stack.type';
-
-interface DockerProgressEvent {
- status: string;
- progressDetail?: {
- current: number;
- total: number;
- };
- progress?: string;
- id?: string;
-}
-
-/* The above code is declaring a variable `STACKS_DIR` with an empty string as its initial value in
-TypeScript. */
-let STACKS_DIR = '';
-
-const stackCache = new Map();
-const CACHE_TTL = 30000; // 30 seconds
-
-/**
- * The function `initComposeService` initializes the stacks directory and ensures its existence in a
- * TypeScript application.
- */
-export async function initComposeService(): Promise<void> {
- try {
- const settings = await getSettings();
- STACKS_DIR = settings.stacksDirectory;
- console.log(`Stacks directory initialized: ${STACKS_DIR}`);
-
- await ensureStacksDir();
- } catch (err) {
- console.error('Error initializing compose service:', err);
- }
-}
-
-/**
- * The function `updateStacksDirectory` updates the `STACKS_DIR` variable with a new directory path and
- * logs the updated directory path to the console.
- * @param {string} directory - The `directory` parameter is a string that represents the new directory
- * path that you want to set for the `STACKS_DIR` variable. When the `updateStacksDirectory` function
- * is called with a valid `directory` value, it updates the `STACKS_DIR` variable to the new directory
- */
-export function updateStacksDirectory(directory: string): void {
- if (directory) {
- if (!path.isAbsolute(directory)) {
- STACKS_DIR = path.resolve(directory); // Resolve to absolute path
- } else {
- STACKS_DIR = directory;
- }
- console.log(`Stacks directory updated to: ${STACKS_DIR}`);
- }
-}
-
-/**
- * The function `ensureStacksDir` ensures the existence of a directory for storing stacks.
- * @returns The function `ensureStacksDir()` returns the `STACKS_DIR` variable after ensuring that the
- * stacks directory exists.
- */
-export async function ensureStacksDir(): Promise<string> {
- try {
- // Ensure STACKS_DIR is initialized and is an absolute path
- if (!STACKS_DIR || !path.isAbsolute(STACKS_DIR)) {
- let dirPath = STACKS_DIR || (await ensureStacksDirectory()); // ensureStacksDirectory is from settings-service
-
- if (!path.isAbsolute(dirPath)) {
- // Resolve relative to the project's root or initial CWD.
- // path.resolve() called with a single relative path resolves it against process.cwd().
- // This should ideally happen once when the application starts or when settings are loaded.
- dirPath = path.resolve(dirPath);
- }
- STACKS_DIR = dirPath; // Store the absolute path
- }
-
- // Now STACKS_DIR is guaranteed to be absolute.
- await fs.mkdir(STACKS_DIR, { recursive: true });
- return STACKS_DIR; // Return the absolute path
- } catch (err) {
- console.error('Error creating stacks directory:', err);
- throw new Error('Failed to create stacks storage directory');
- }
-}
-
-/**
- * Returns the stack directory for a given stackId (unchanged)
- */
-export async function getStackDir(stackId: string): Promise<string> {
- const stacksDirAbs = await ensureStacksDir(); // This now returns an absolute path
- const safeId = path.basename(stackId); // Use path.basename for safety
- if (safeId !== stackId) {
- // This check might be too strict if stackId can be a path itself.
- // Consider if stackId is just a name or could be more complex.
- // For now, assuming stackId is a simple name.
- console.warn(`Original stackId "${stackId}" was sanitized to "${safeId}". Ensure this is expected.`);
- // throw new Error('Invalid stack id'); // Or handle differently
- }
- return path.join(stacksDirAbs, safeId);
-}
-
-/**
- * Returns the path to the compose file, prioritizing compose.yaml, fallback to docker-compose.yml.
- * Returns null if neither is found.
- */
-export async function getComposeFilePath(stackId: string): Promise<string | null> {
- const stackDirAbs = await getStackDir(stackId); // Will be absolute
- const newPath = path.join(stackDirAbs, 'compose.yaml');
- const oldPath = path.join(stackDirAbs, 'docker-compose.yml');
- try {
- await fs.access(newPath);
- return newPath;
- } catch {
- // If compose.yaml is not found, try docker-compose.yml
- try {
- await fs.access(oldPath);
- return oldPath;
- } catch {
- // Neither compose.yaml nor docker-compose.yml found
- return null;
- }
- }
-}
-
-/**
- * Returns the path to the .env file (no fallback needed, just .env)
- */
-async function getEnvFilePath(stackId: string): Promise<string> {
- const stackDir = await getStackDir(stackId);
- return join(stackDir, '.env');
-}
-
-/**
- * Saves environment variables to a .env file in the stack directory
- * Handles empty content gracefully
- */
-async function saveEnvFile(stackId: string, content?: string): Promise<void> {
- const envPath = await getEnvFilePath(stackId);
-
- // Create a new local variable instead of reassigning the parameter
- const fileContent = content === undefined || content === null ? '' : content;
-
- await fs.writeFile(envPath, fileContent, 'utf8');
- console.log(`Saved .env file for stack ${stackId}`);
-}
-
-/**
- * Loads environment variables from a .env file in the stack directory
- * Returns empty string if the file doesn't exist
- */
-export async function loadEnvFile(stackId: string): Promise<string> {
- const envPath = await getEnvFilePath(stackId);
-
- try {
- return await fs.readFile(envPath, 'utf8');
- } catch (err) {
- const nodeErr = err as NodeJS.ErrnoException;
- // Return empty string if file doesn't exist
- if (nodeErr.code === 'ENOENT') {
- console.log(`No .env file found for stack ${stackId}`);
- return '';
- }
- console.error(`Error reading .env file for stack ${stackId}:`, err);
- throw new Error(`Failed to read .env file: ${nodeErr.message}`, { cause: err });
- }
-}
-
-/**
- * The function `getComposeInstance` returns a new instance of `DockerodeCompose` using a Docker
- * client, compose file path, and stack ID.
- * @param {string} stackId - The `stackId` parameter is a string that represents the identifier of a
- * Docker stack. It is used to retrieve the compose file path and create a new instance of
- * `DockerodeCompose` with the specified stack identifier.
- * @returns The function `getComposeInstance` is returning a Promise that resolves to an instance of
- * `DockerodeCompose`. The `DockerodeCompose` instance is created using the Docker client obtained from
- * `getDockerClient()`, the compose file path obtained asynchronously from
- * `getComposeFilePath(stackId)`, and the `stackId` parameter.
- */
-async function getComposeInstance(stackId: string): Promise<DockerodeCompose> {
- const docker = await getDockerClient();
- const composePath = await getComposeFilePath(stackId);
- if (!composePath) {
- throw new Error(`Compose file not found for stack ${stackId}`);
- }
- return new DockerodeCompose(docker, composePath, stackId);
-}
-
-/**
- * This TypeScript function retrieves information about services in a Docker stack based on the
- * provided stack ID and compose file content.
- * @param {string} stackId - The `stackId` parameter in the `getStackServices` function is a string
- * that represents the identifier of a stack in a Docker environment. This identifier is used to filter
- * and identify containers associated with the specific stack.
- * @param {string} composeContent - The `composeContent` parameter in the `getStackServices` function
- * is expected to be a string containing the content of a Docker Compose file in YAML format. This file
- * defines the services that make up a stack in Docker. The function parses this content to extract
- * service names and then retrieves information about
- * @returns The `getStackServices` function returns a Promise that resolves to an array of
- * `StackService` objects. Each `StackService` object contains information about a service in a
- * specific stack, including the service ID, name, and state (whether it is running, its status, and
- * exit code). If an error occurs during the process, an empty array is returned.
- */
-async function getStackServices(stackId: string, composeContent: string): Promise<StackService[]> {
- const docker = await getDockerClient();
- const composeProjectLabel = 'com.docker.compose.project'; // Standard label
- const composeServiceLabel = 'com.docker.compose.service'; // Standard service label
-
- try {
- // Load the .env file to provide environment variable values
- const envContent = await loadEnvFile(stackId);
-
- // Parse environment variables from .env content
- const envVars: Record<string, string> = {};
- if (envContent) {
- envContent.split('\n').forEach((line) => {
- const trimmedLine = line.trim();
- if (trimmedLine && !trimmedLine.startsWith('#')) {
- const [key, ...valueParts] = trimmedLine.split('=');
- const value = valueParts.join('='); // Handle values that might contain =
- if (key) {
- envVars[key.trim()] = value?.trim() || '';
- }
- }
- });
- }
-
- // Create environment variable getter function
- const getEnvVar = (key: string) => {
- return envVars[key] || process.env[key] || '';
- };
-
- // Use our safe parser utility with the environment variable getter
- const composeData = parseYamlContent(composeContent, getEnvVar);
- if (!composeData || !composeData.services) {
- console.warn(`No services found in compose content for stack ${stackId}`);
- return [];
- }
-
- const serviceNames = Object.keys(composeData.services as Record<string, unknown>);
-
- // List containers, potentially filtering by label for efficiency if needed
- const containers = await docker.listContainers({
- all: true
- });
-
- // Filter containers based on EITHER the project label OR the naming convention
- const stackContainers = containers.filter((container) => {
- const labels = container.Labels || {};
- const names = container.Names || [];
- // Check if any name starts with the conventional prefix (e.g., /stackId_service_1)
- const nameStartsWithPrefix = names.some((name) => name.startsWith(`/${stackId}_`));
- // Check if the standard compose project label matches the stackId
- const hasCorrectLabel = labels[composeProjectLabel] === stackId;
- // Include the container if either condition is true
- return nameStartsWithPrefix || hasCorrectLabel;
- });
-
- const services: StackService[] = [];
-
- for (const containerData of stackContainers) {
- const containerName = containerData.Names?.[0]?.substring(1) || ''; // Remove leading '/'
- const labels = containerData.Labels || {};
- // Prefer the standard compose service label for the service name
- let serviceName = labels[composeServiceLabel];
-
- // Fallback to parsing from container name if the service label is missing
- if (!serviceName) {
- console.warn(`Container ${containerData.Id} in stack ${stackId} is missing the '${composeServiceLabel}' label. Attempting to parse name.`);
- for (const name of serviceNames) {
- // Match patterns like stackId_serviceName_1 or stackId_serviceName
- // Ensure the match is precise to avoid partial overlaps (e.g., 'web' vs 'web-api')
- const servicePrefixWithUnderscore = `${stackId}_${name}_`;
- const servicePrefixExact = `${stackId}_${name}`;
- if (containerName.startsWith(servicePrefixWithUnderscore) || containerName === servicePrefixExact) {
- serviceName = name;
- break;
- }
- }
- }
-
- // Final fallback if still no match (less ideal)
- if (!serviceName) {
- serviceName = containerName; // Use the full container name as a last resort
- console.error(`Could not determine service name for container ${containerName} (ID: ${containerData.Id}) in stack ${stackId}. Using full container name.`);
- }
-
- const service: StackService = {
- id: containerData.Id,
- name: serviceName, // Use the determined service name
- state: {
- Running: containerData.State === 'running',
- Status: containerData.State,
- // Note: listContainers doesn't provide ExitCode reliably, need inspect for that if required later
- ExitCode: containerData.State === 'exited' ? -1 : 0 // Placeholder
- }
- };
-
- // Avoid adding duplicates if multiple containers map to the same service (e.g., scaled services)
- // Check if a service with the same name already exists
- const existingServiceIndex = services.findIndex((s) => s.name === serviceName);
- if (existingServiceIndex !== -1) {
- // If the existing service is just a placeholder ('not created'), replace it
- if (!services[existingServiceIndex].id) {
- services[existingServiceIndex] = service;
- } else {
- // Handle scaled services - maybe add instance number or just log for now
- console.log(`Multiple containers found for service ${serviceName} in stack ${stackId}. Displaying first found.`);
- // Or potentially create a unique name like serviceName + '-' + instanceNumber
- }
- } else {
- services.push(service);
- }
- }
-
- // Add placeholders for services defined in compose but not found among listed containers
- for (const name of serviceNames) {
- if (!services.some((s) => s.name === name)) {
- services.push({
- id: '', // No container ID
- name: name,
- state: {
- Running: false,
- Status: 'not created',
- ExitCode: 0
- }
- });
- }
- }
-
- // Sort services alphabetically by name for consistent order
- services.sort((a, b) => a.name.localeCompare(b.name));
-
- return services;
- } catch (err) {
- console.error(`Error getting services for stack ${stackId}:`, err);
- return []; // Return empty array on error
- }
-}
-
-/**
- * This TypeScript function asynchronously loads information about Docker Compose stacks from a
- * directory and returns an array of Stack objects.
- * @returns The `loadComposeStacks` function returns a Promise that resolves to an array of `Stack`
- * objects. Each `Stack` object represents a stack loaded from the stacks directory. The `Stack` object
- * contains properties such as `id`, `name`, `serviceCount`, `runningCount`, `status`, `createdAt`, and
- * `updatedAt`.
- */
-export async function loadComposeStacks(): Promise<Stack[]> {
- const cacheKey = 'compose-stacks';
- const cachedData = stackCache.get(cacheKey);
-
- if (cachedData && Date.now() - cachedData.timestamp < CACHE_TTL) {
- return cachedData.data;
- }
-
- const stacksDir = await ensureStacksDir();
-
- try {
- const stackDirEntries = await fs.readdir(stacksDir, { withFileTypes: true });
- const stacks: Stack[] = [];
-
- for (const entry of stackDirEntries) {
- if (!entry.isDirectory()) {
- continue;
- }
- const dirName = entry.name; // This is the stack's ID and name
- const stackDir = path.join(stacksDir, dirName);
-
- let composeFilePath: string | null = null;
- let composeContent: string | null = null;
-
- const potentialComposePaths = [path.join(stackDir, 'compose.yaml'), path.join(stackDir, 'docker-compose.yml')];
-
- for (const p of potentialComposePaths) {
- try {
- await fs.access(p);
- composeContent = await fs.readFile(p, 'utf8');
- composeFilePath = p;
- break;
- } catch {
- // File not accessible or doesn't exist, try next
- }
- }
-
- if (!composeContent || !composeFilePath) {
- console.warn(`No compose file found in directory ${dirName}, skipping.`);
- continue;
- }
-
- // All stacks are now treated based on directory and compose file presence
- const services = await getStackServices(dirName, composeContent);
- const serviceCount = services.length;
- const runningCount = services.filter((s) => s.state?.Running).length;
-
- let status: Stack['status'] = 'stopped';
- if (serviceCount === 0) {
- status = 'unknown'; // Or 'stopped' if preferred for empty compose
- } else if (runningCount === serviceCount) {
- status = 'running';
- } else if (runningCount > 0) {
- status = 'partially running';
- }
-
- let dirStat;
- try {
- dirStat = await fs.stat(stackDir);
- } catch (statErr) {
- console.error(`Could not stat directory ${stackDir}:`, statErr);
- const now = new Date().toISOString();
- dirStat = { birthtime: new Date(now), mtime: new Date(now) }; // Fallback
- }
-
- stacks.push({
- id: dirName,
- name: dirName, // Name is the directory name
- serviceCount,
- runningCount,
- status,
- createdAt: dirStat.birthtime.toISOString(),
- updatedAt: dirStat.mtime.toISOString(), // Use directory mtime as a general update timestamp
- isExternal: false
- // composeContent and envContent are loaded on demand by getStack
- });
- }
-
- stackCache.set(cacheKey, {
- data: stacks,
- timestamp: Date.now()
- });
-
- return stacks;
- } catch (err) {
- console.error('Error loading stacks from STACKS_DIR:', err);
- throw new Error('Failed to load compose stacks');
- }
-}
-
-/**
- * Creates a new stack with a compose file and optional .env file
- */
-export async function createStack(name: string, composeContent: string, envContent?: string): Promise<Stack> {
- // Create a safe directory name from the stack name
- const dirName = slugify(name, {
- lower: true, // Convert to lowercase
- strict: true, // Strip special chars
- replacement: '-', // Replace spaces with hyphens
- trim: true // Trim leading/trailing spaces
- });
-
- const stacksDir = await ensureStacksDirectory();
- let counter = 1;
- let uniqueDirName = dirName;
-
- while (await directoryExists(join(stacksDir, uniqueDirName))) {
- uniqueDirName = `${dirName}-${counter}`;
- counter++;
- }
-
- const stackDir = join(stacksDir, uniqueDirName);
- await fs.mkdir(stackDir, { recursive: true });
-
- const normalizedComposeContent = normalizeHealthcheckTest(composeContent);
- await fs.writeFile(join(stackDir, 'compose.yaml'), normalizedComposeContent);
-
- if (envContent) {
- await fs.writeFile(join(stackDir, '.env'), envContent);
- }
-
- // No .stack.json is created.
-
- let serviceCount = 0;
- try {
- // Use our safe parser utility
- const composeData = parseYamlContent(composeContent);
- if (composeData?.services) {
- serviceCount = Object.keys(composeData.services as Record<string, unknown>).length;
- }
- } catch (parseErr) {
- console.warn(`Could not parse compose file during creation for stack ${uniqueDirName}:`, parseErr);
- }
-
- const dirStat = await fs.stat(stackDir);
-
- // Create the stack object
- const newStack: Stack = {
- id: uniqueDirName,
- name: uniqueDirName,
- serviceCount: serviceCount,
- runningCount: 0,
- status: 'stopped',
- createdAt: dirStat.birthtime.toISOString(),
- updatedAt: dirStat.mtime.toISOString(),
- composeContent: composeContent,
- envContent: envContent || '',
- isExternal: false
- };
-
- // Invalidate the cache to ensure the new stack appears immediately
- stackCache.delete('compose-stacks');
-
- return newStack;
-}
-
-/**
- * Gets information about a specific stack including its .env file
- */
-export async function getStack(stackId: string): Promise<Stack> {
- const stackDir = await getStackDir(stackId); // stackId is dirName
-
- try {
- const composePath = await getComposeFilePath(stackId);
- if (!composePath) {
- throw new Error(`Stack '${stackId}' is missing a compose file (compose.yaml or docker-compose.yml).`);
- }
- const composeContent = await fs.readFile(composePath, 'utf8');
- const envContent = await loadEnvFile(stackId);
-
- // Parse environment variables from .env content
- const envVars: Record<string, string> = {};
- if (envContent) {
- envContent.split('\n').forEach((line) => {
- const trimmedLine = line.trim();
- if (trimmedLine && !trimmedLine.startsWith('#')) {
- const [key, ...valueParts] = trimmedLine.split('=');
- const value = valueParts.join('='); // Handle values that might contain =
- if (key) {
- envVars[key.trim()] = value?.trim() || '';
- }
- }
- });
- }
-
- // Create a property getter function for environment variables
- const getEnvProperty = (key: string) => {
- return envVars[key] || process.env[key] || '';
- };
-
- // Parse compose with environment variables - use our parseYamlContent instead
- let composeData;
- try {
- composeData = parseYamlContent(composeContent, getEnvProperty);
- } catch (parseErr) {
- console.error(`Error parsing compose content for stack ${stackId}:`, parseErr);
- // Continue with null composeData - we'll still return the stack object
- composeData = null;
- }
-
- // Get services with the parsed compose content
- const services = await getStackServices(stackId, composeContent);
-
- const serviceCount = services.length;
- const runningCount = services.filter((s) => s.state?.Running).length;
-
- let status: Stack['status'] = 'stopped';
- if (serviceCount === 0) {
- status = 'unknown';
- } else if (runningCount === serviceCount) {
- status = 'running';
- } else if (runningCount > 0) {
- status = 'partially running';
- }
-
- const dirStat = await fs.stat(stackDir);
-
- return {
- id: stackId, // Directory name
- name: stackId, // Directory name
- services,
- serviceCount,
- runningCount,
- status,
- createdAt: dirStat.birthtime.toISOString(),
- updatedAt: dirStat.mtime.toISOString(), // Or could be composePath mtime
- composeContent,
- envContent,
- isExternal: false
- };
- } catch (err) {
- console.error(`Error in getStack for stackId '${stackId}':`, err);
- if (err instanceof Error && err.message.includes('missing a compose file')) {
- throw err;
- }
- throw new Error(`Stack '${stackId}' not found or cannot be accessed.`);
- }
-}
-
-/**
- * Updates a stack with new configuration and/or .env file
- */
-export async function updateStack(currentStackId: string, updates: StackUpdate): Promise<Stack> {
- let effectiveStackId = currentStackId;
- let stackAfterRename: Stack | null = null;
-
- // 1. Handle potential rename first
- if (updates.name) {
- const newSlugifiedName = slugify(updates.name, {
- lower: true,
- strict: true,
- replacement: '-',
- trim: true
- });
-
- if (newSlugifiedName !== currentStackId) {
- console.log(`Rename requested for stack '${currentStackId}' to '${updates.name}' (slug: '${newSlugifiedName}').`);
- // renameStack will throw an error if the stack is running or if other rename conditions are not met.
- stackAfterRename = await renameStack(currentStackId, updates.name);
- effectiveStackId = stackAfterRename.id;
- console.log(`Stack '${currentStackId}' successfully renamed to '${effectiveStackId}'.`);
- } else {
- // Name provided is the same as current, or slugifies to the same. No rename action needed.
- console.log(`Provided name '${updates.name}' is effectively the same as current stack ID '${currentStackId}'. No rename action.`);
- }
- }
-
- // 2. Handle content updates (compose or .env)
- // These updates will apply to the new directory if a rename occurred.
- let contentUpdated = false;
- const stackDirForContent = await getStackDir(effectiveStackId);
-
- const promises = [];
-
- if (updates.composeContent !== undefined) {
- const normalizedComposeContent = normalizeHealthcheckTest(updates.composeContent);
- const currentComposePath = await getComposeFilePath(effectiveStackId); // Check existing, might be null
- const targetComposePath = currentComposePath || path.join(stackDirForContent, 'compose.yaml'); // Default to compose.yaml if not found
-
- promises.push(fs.writeFile(targetComposePath, normalizedComposeContent, 'utf8'));
- contentUpdated = true;
- console.log(`Updating composeContent for stack '${effectiveStackId}'.`);
- }
-
- if (updates.envContent !== undefined) {
- promises.push(saveEnvFile(effectiveStackId, updates.envContent));
- contentUpdated = true;
- console.log(`Updating envContent for stack '${effectiveStackId}'.`);
- }
-
- if (promises.length > 0) {
- await Promise.all(promises);
- }
-
- // Invalidate the cache after any update
- stackCache.delete('compose-stacks');
-
- // 3. Return the final stack state
- if (stackAfterRename && !contentUpdated) {
- // Only a rename occurred, no subsequent content changes in this call.
- // The stackAfterRename object is fresh from renameStack's getStack call.
- return stackAfterRename;
- } else {
- // Content was updated, or no rename occurred but content might have.
- // Or both rename and content update occurred.
- // Fetch the latest stack details.
- return getStack(effectiveStackId);
- }
-}
-
-/**
- * The function `startStack` asynchronously starts a Docker stack identified by `stackId`, pulling the
- * latest images and bringing up the services.
- * @param {string} stackId - The `stackId` parameter is a string that represents the identifier of the
- * stack that you want to start.
- * @returns The `startStack` function returns a Promise<boolean>.
- */
-export async function startStack(stackId: string): Promise<boolean> {
- const stackDir = await getStackDir(stackId);
- const originalCwd = process.cwd();
- let deploymentStarted = false;
-
- try {
- const composePath = await getComposeFilePath(stackId);
- if (!composePath) {
- throw new Error(`Compose file not found for stack ${stackId} during start.`);
- }
- const currentComposeContent = await fs.readFile(composePath, 'utf8');
-
- // Load .env file to provide environment variable values for normalization and parsing
- const envContent = await loadEnvFile(stackId);
- const envVars: Record<string, string> = {};
- if (envContent) {
- envContent.split('\n').forEach((line) => {
- const trimmedLine = line.trim();
- if (trimmedLine && !trimmedLine.startsWith('#')) {
- const [key, ...valueParts] = trimmedLine.split('=');
- const value = valueParts.join('=');
- if (key) {
- envVars[key.trim()] = value?.trim() || '';
- }
- }
- });
- }
-
- const getEnvVar = (key: string): string | undefined => {
- return envVars[key] || process.env[key];
- };
-
- // Normalize and substitute variables in the compose content
- const normalizedComposeContent = normalizeHealthcheckTest(currentComposeContent, getEnvVar);
- if (currentComposeContent !== normalizedComposeContent) {
- console.log(`Normalized and substituted variables in compose file for stack ${stackId}. Writing to disk.`);
- await fs.writeFile(composePath, normalizedComposeContent, 'utf8');
- }
-
- process.chdir(stackDir);
- console.log(`Temporarily changed CWD to: ${stackDir} for stack ${stackId} operations.`);
-
- // Parse the normalized content for composeData
- const composeData = parseYamlContent(normalizedComposeContent, getEnvVar);
-
- if (!composeData) {
- throw new Error(`Failed to parse compose file for stack ${stackId}`);
- }
-
- const hasExternalNetworks = composeData.networks && Object.values(composeData.networks).some((net: any) => net.external);
-
- try {
- deploymentStarted = true;
- if (hasExternalNetworks) {
- console.log(`Stack ${stackId} contains external networks. Using custom deployment approach.`);
- await startStackWithExternalNetworks(stackId, composeData, stackDir);
- } else {
- // Standard approach for stacks without external networks
- const docker = await getDockerClient(); // Ensure docker client is available if needed for image pull
- const imagePullPromises = Object.entries(composeData.services || {})
- .filter(([_, serviceConfig]) => (serviceConfig as any).image)
- .map(async ([serviceName, serviceConfig]) => {
- const serviceImage = (serviceConfig as any).image;
- console.log(`Pulling image for service ${serviceName}: ${serviceImage}`);
- try {
- // Rest of the image pulling logic remains the same
- } catch (pullErr) {
- console.warn(`Warning: Failed to pull image ${serviceImage} for service ${serviceName}:`, pullErr);
- }
- });
-
- await Promise.all(imagePullPromises);
-
- const compose = await getComposeInstance(stackId); // Reads from composePath
- await compose.up();
- }
-
- stackCache.delete('compose-stacks');
- return true;
- } catch (deployErr) {
- // If deployment started but failed, clean up any containers that were created
- if (deploymentStarted) {
- console.log(`Deployment of stack ${stackId} failed. Cleaning up any created containers...`);
- try {
- await cleanupFailedDeployment(stackId);
- } catch (cleanupErr) {
- console.error(`Error cleaning up failed deployment for stack ${stackId}:`, cleanupErr);
- // Continue with the original error even if cleanup fails
- }
- }
-
- // Rethrow the original error
- throw deployErr;
- }
- } catch (err) {
- console.error(`Error starting stack ${stackId} from directory ${stackDir}:`, err);
- const errorMessage = err instanceof Error ? err.message : String(err);
- throw new Error(`Failed to start stack: ${errorMessage}`);
- } finally {
- process.chdir(originalCwd);
- console.log(`Restored CWD to: ${originalCwd}.`);
- }
-}
-
-/**
- * Cleans up containers from a failed stack deployment
- * @param {string} stackId - The ID of the stack that failed to deploy
- */
-async function cleanupFailedDeployment(stackId: string): Promise<void> {
- console.log(`Cleaning up containers for failed deployment of stack ${stackId}...`);
- const docker = await getDockerClient();
- const composeProjectLabel = 'com.docker.compose.project';
-
- try {
- // Find containers belonging to this stack
- const containers = await docker.listContainers({
- all: true, // Include non-running containers
- filters: JSON.stringify({
- label: [`${composeProjectLabel}=${stackId}`]
- })
- });
-
- // Also check by name convention as fallback
- const allContainers = await docker.listContainers({ all: true });
- const nameFilteredContainers = allContainers.filter((c) => !containers.some((fc) => fc.Id === c.Id) && c.Names?.some((name) => name.startsWith(`/${stackId}_`)));
-
- const stackContainers = [...containers, ...nameFilteredContainers];
-
- if (stackContainers.length === 0) {
- console.log(`No containers found for failed deployment of stack ${stackId}.`);
- return;
- }
-
- console.log(`Found ${stackContainers.length} containers to remove for failed deployment of stack ${stackId}.`);
-
- // Remove each container
- for (const containerInfo of stackContainers) {
- try {
- const container = docker.getContainer(containerInfo.Id);
- console.log(`Removing container ${containerInfo.Names?.[0] || containerInfo.Id}...`);
-
- // Force removal to ensure it's gone (equivalent to docker rm -f)
- await container.remove({ force: true });
- console.log(`Successfully removed container ${containerInfo.Names?.[0] || containerInfo.Id}.`);
- } catch (containerErr) {
- console.error(`Error removing container ${containerInfo.Id}:`, containerErr);
- // Continue with others even if one fails
- }
- }
-
- console.log(`Cleanup of failed deployment for stack ${stackId} complete.`);
- } catch (err) {
- console.error(`Error during cleanup of failed deployment for stack ${stackId}:`, err);
- throw err;
- }
-}
-
-/**
- * Custom function to deploy stacks with external networks
- * This uses direct Docker API calls to avoid the disconnect() issue
- */
-async function startStackWithExternalNetworks(stackId: string, composeData: any, stackDir: string): Promise<void> {
- const docker = await getDockerClient();
-
- // Step 1: Pull all images
- console.log(`Pulling images for stack ${stackId} with external networks...`);
- for (const [serviceName, serviceConfig] of Object.entries(composeData.services || {})) {
- const serviceImage = (serviceConfig as any).image;
- if (serviceImage) {
- console.log(`Pulling image for service ${serviceName}: ${serviceImage}`);
- try {
- await new Promise<any[]>((resolve, reject) => {
- docker.pull(serviceImage, {}, (pullError: Error | null, stream?: NodeJS.ReadableStream) => {
- if (pullError) {
- reject(pullError);
- return;
- }
- if (!stream) {
- reject(new Error(`Docker pull for ${serviceImage} did not return a stream.`));
- return;
- }
- docker.modem.followProgress(
- stream,
- (progressError: Error | null, output: any[]) => {
- if (progressError) {
- reject(progressError);
- } else {
- console.log(`Successfully pulled image: ${serviceImage}`);
- resolve(output);
- }
- },
- (event: DockerProgressEvent) => {
- if (event.progress) {
- console.log(`${serviceImage}: ${event.status} ${event.progress}`);
- } else if (event.status) {
- console.log(`${serviceImage}: ${event.status}`);
- }
- }
- );
- });
- });
- } catch (pullErr) {
- console.warn(`Warning: Failed to pull image ${serviceImage} for service ${serviceName}:`, pullErr);
- // Continue with other images - the service might still start with an existing image
- }
- }
- }
-
- // Step 2: Create non-external networks
- console.log(`Creating networks for stack ${stackId}...`);
- for (const [networkName, networkConfig] of Object.entries(composeData.networks || {})) {
- // Skip external networks - they should already exist
- if ((networkConfig as any).external) {
- console.log(`Skipping creation of external network: ${networkName}`);
- continue;
- }
-
- // Create the network if it doesn't exist
- const networkToCreate = {
- Name: (networkConfig as any).name || `${stackId}_${networkName}`,
- Driver: (networkConfig as any).driver || 'bridge',
- Labels: {
- 'com.docker.compose.project': stackId,
- 'com.docker.compose.network': networkName
- },
- Options: (networkConfig as any).driver_opts || {}
- };
-
- try {
- console.log(`Creating network: ${networkToCreate.Name}`);
- await docker.createNetwork(networkToCreate);
- console.log(`Successfully created network: ${networkToCreate.Name}`);
- } catch (netErr: any) {
- // If network already exists (HTTP 409), that's fine
- if (netErr.statusCode === 409) {
- console.log(`Network ${networkToCreate.Name} already exists, reusing it.`);
- } else {
- console.error(`Error creating network ${networkToCreate.Name}:`, netErr);
- throw netErr;
- }
- }
- }
-
- // Step 3: Create and start each service
- console.log(`Creating and starting services for stack ${stackId}...`);
- for (const [serviceName, serviceConfig] of Object.entries(composeData.services || {})) {
- const service = serviceConfig as any;
-
- let containerName = service.container_name;
- if (!containerName || typeof containerName !== 'string') {
- containerName = `${stackId}_${serviceName}`;
- }
- if (containerName.includes('${')) {
- console.warn(`CRITICAL: Unresolved variable in container_name for service '${serviceName}': ${containerName}. Using default name: ${stackId}_${serviceName}`);
- containerName = `${stackId}_${serviceName}`;
- }
-
- const containerConfig: any = {
- name: containerName,
- Image: service.image,
- User: service.user || '', // Add User field
- Labels: {
- 'com.docker.compose.project': stackId,
- 'com.docker.compose.service': serviceName,
- ...service.labels
- },
- Env: await prepareEnvironmentVariables(service.environment, stackDir),
- HostConfig: {
- RestartPolicy: prepareRestartPolicy(service.restart),
- Binds: prepareVolumes(service.volumes),
- PortBindings: preparePorts(service.ports),
- Dns: service.dns || [],
- DnsOptions: service.dns_opt || [],
- DnsSearch: service.dns_search || []
- }
- };
-
- const networkMode = service.network_mode || null;
- let primaryNetworkName = null;
- if (networkMode) {
- containerConfig.HostConfig.NetworkMode = networkMode;
- } else if (service.networks) {
- const serviceNetworks = Array.isArray(service.networks) ? service.networks : Object.keys(service.networks);
-
- if (serviceNetworks.length > 0) {
- const firstNetName = serviceNetworks[0];
- const networkDefinition = composeData.networks?.[firstNetName];
-
- let actualExternalNetIdentifier = firstNetName;
- if (networkDefinition?.external && networkDefinition.name) {
- if (typeof networkDefinition.name === 'string' && networkDefinition.name.includes('${') && networkDefinition.name.includes('}')) {
- console.warn(`External network key '${firstNetName}' has an unresolved variable in name attribute. Using key '${firstNetName}' as identifier.`);
- } else {
- actualExternalNetIdentifier = networkDefinition.name;
- }
- }
-
- // Set the primary network name
- primaryNetworkName = networkDefinition?.external ? actualExternalNetIdentifier : `${stackId}_${firstNetName}`;
- containerConfig.HostConfig.NetworkMode = primaryNetworkName;
- console.log(`Service ${serviceName} will use '${primaryNetworkName}' as primary network.`);
- }
- }
-
- // When preparing the EndpointsConfig, only include additional networks
- const networkingConfig: { EndpointsConfig?: any } = {};
- if (!networkMode && service.networks) {
- const serviceNetworks = Array.isArray(service.networks) ? service.networks : Object.keys(service.networks);
-
- // Skip the first network if we're using it as the primary NetworkMode
- const additionalNetworks = primaryNetworkName ? serviceNetworks.slice(1) : serviceNetworks;
-
- if (additionalNetworks.length > 0) {
- networkingConfig.EndpointsConfig = {};
-
- for (const netName of additionalNetworks) {
- const serviceNetConfig = typeof service.networks === 'object' ? service.networks[netName] : {};
- const networkDefinition = composeData.networks?.[netName];
-
- let actualExternalNetIdentifier = netName;
- if (networkDefinition?.external && networkDefinition.name) {
- if (typeof networkDefinition.name === 'string' && networkDefinition.name.includes('${') && networkDefinition.name.includes('}')) {
- console.warn(`External network key '${netName}' in stack '${stackId}' has a 'name' attribute '${networkDefinition.name}' that appears to be an unresolved variable. Using the network key '${netName}' as the identifier for connection.`);
- } else {
- actualExternalNetIdentifier = networkDefinition.name;
- }
- }
- const fullNetworkName = networkDefinition?.external ? actualExternalNetIdentifier : `${stackId}_${netName}`;
-
- // Create the endpoint config properly with all possible options
- const endpointConfig: any = {};
-
- // Handle network aliases
- if (typeof serviceNetConfig === 'object' && serviceNetConfig.aliases) {
- endpointConfig.Aliases = serviceNetConfig.aliases;
- }
-
- // Handle static IP configuration - CRITICAL for networks like vlan25
- if (typeof serviceNetConfig === 'object') {
- const ipamConfig: any = {};
-
- if (serviceNetConfig.ipv4_address) {
- ipamConfig.IPv4Address = serviceNetConfig.ipv4_address;
- }
-
- if (serviceNetConfig.ipv6_address) {
- ipamConfig.IPv6Address = serviceNetConfig.ipv6_address;
- }
-
- if (Object.keys(ipamConfig).length > 0) {
- endpointConfig.IPAMConfig = ipamConfig;
- }
- }
-
- networkingConfig.EndpointsConfig[fullNetworkName] = endpointConfig;
- }
- }
- }
-
- try {
- const container = await docker.createContainer(containerConfig);
- console.log(`Successfully created container: ${containerName} (ID: ${container.id})`);
-
- if (networkingConfig.EndpointsConfig && Object.keys(networkingConfig.EndpointsConfig).length > 0) {
- for (const netNameKey of Object.keys(networkingConfig.EndpointsConfig)) {
- try {
- // Log the actual config we're using to connect to the network
- console.log(`Connecting container ${container.id} to network: ${netNameKey} with config:`, JSON.stringify(networkingConfig.EndpointsConfig[netNameKey]));
-
- const network = docker.getNetwork(netNameKey);
- await network.connect({
- Container: container.id,
- EndpointConfig: networkingConfig.EndpointsConfig[netNameKey] || {}
- });
- console.log(`Successfully connected ${container.id} to network: ${netNameKey}`);
- } catch (netConnectErr) {
- console.error(`Error connecting container ${container.id} to network ${netNameKey}:`, netConnectErr);
- throw new Error(`Failed to connect container ${container.id} to network ${netNameKey}: ${netConnectErr instanceof Error ? netConnectErr.message : String(netConnectErr)}`);
- }
- }
- }
-
- console.log(`Starting container: ${containerName} (ID: ${container.id})`);
- await container.start();
- console.log(`Successfully started container: ${containerName}`);
- } catch (createErr) {
- console.error(`Error creating/starting container for service ${serviceName} (${containerName}):`, createErr);
- throw createErr;
- }
- }
-
- console.log(`Successfully deployed stack ${stackId} with external networks`);
-}
-
-// Helper functions to prepare container configuration
-async function prepareEnvironmentVariables(env: any, stackDir: string): Promise<string[]> {
- const result: string[] = [];
-
- // Load .env file if it exists
- try {
- const envPath = path.join(stackDir, '.env');
- const envContent = await fs.readFile(envPath, 'utf8');
- const envLines = envContent.split('\n');
-
- for (const line of envLines) {
- const trimmed = line.trim();
- if (trimmed && !trimmed.startsWith('#')) {
- result.push(trimmed);
- }
- }
- } catch (err) {
- // .env file doesn't exist or can't be read, that's okay
- }
-
- // Add environment variables from the service definition
- if (env) {
- if (Array.isArray(env)) {
- result.push(...env);
- } else if (typeof env === 'object') {
- for (const [key, value] of Object.entries(env)) {
- result.push(`${key}=${value}`);
- }
- }
- }
-
- return result;
-}
-
-function prepareRestartPolicy(restart: string | undefined): any {
- if (!restart) return { Name: 'no' };
-
- switch (restart) {
- case 'no':
- return { Name: 'no' };
- case 'always':
- return { Name: 'always' };
- case 'on-failure':
- return { Name: 'on-failure' };
- case 'unless-stopped':
- return { Name: 'unless-stopped' };
- default:
- // For more complex policies like "on-failure:3", parse the max retry count
- if (restart.startsWith('on-failure:')) {
- const maxRetryCount = parseInt(restart.split(':')[1], 10);
- return { Name: 'on-failure', MaximumRetryCount: maxRetryCount };
- }
- return { Name: 'no' };
- }
-}
-
-function prepareVolumes(volumes: any[] | undefined): string[] {
- if (!volumes) return [];
-
- return volumes
- .filter((vol) => typeof vol === 'string' && vol.includes(':'))
- .map((vol) => {
- // Handle volume paths correctly
- const parts = vol.split(':');
- return `${parts[0]}:${parts[1]}${parts[2] ? `:${parts[2]}` : ''}`;
- });
-}
-
-function preparePorts(ports: any[] | undefined): any {
- if (!ports) return {};
-
- const portBindings: any = {};
-
- for (const port of ports) {
- if (typeof port === 'string' && port.includes(':')) {
- const [hostPort, containerPort] = port.split(':');
- const containerPortWithProto = containerPort.includes('/') ? containerPort : `${containerPort}/tcp`;
-
- portBindings[containerPortWithProto] = [{ HostPort: hostPort }];
- }
- }
-
- return portBindings;
-}
-
-/**
- * The function `stopStack` stops a Docker stack identified by its ID and returns a boolean indicating
- * success. It manually stops and removes containers associated with the stack due to potential
- * inconsistencies in `dockerode-compose`.
- * @param {string} stackId - The `stackId` parameter is a string that represents the identifier of the
- * stack that you want to stop.
- * @returns The `stopStack` function returns a Promise that resolves to a boolean value. If the stack
- * is stopped successfully, it returns `true`. If an error occurs during the process of stopping the
- * stack, it will log the error, throw a new Error with a message indicating the failure, and the
- * Promise will be rejected.
- */
-export async function stopStack(stackId: string): Promise<boolean> {
- console.log(`Attempting to stop stack ${stackId} by manually stopping containers...`);
- const docker = await getDockerClient();
- const composeProjectLabel = 'com.docker.compose.project';
- let stoppedCount = 0;
- let removedCount = 0;
-
- try {
- // 1. Find containers belonging to the stack (using label or name convention)
- const containers = await docker.listContainers({
- all: true, // Include stopped containers as well
- filters: JSON.stringify({
- // Primarily filter by the standard compose project label
- label: [`${composeProjectLabel}=${stackId}`]
- })
- });
-
- // Fallback: If label filter missed some, double-check by name (less reliable)
- // This part might be less necessary if the label is consistently applied by `up()`
- const allContainers = await docker.listContainers({ all: true });
- const nameFilteredContainers = allContainers.filter(
- (c) =>
- !containers.some((fc) => fc.Id === c.Id) && // Only check containers not already found by label
- (c.Labels?.[composeProjectLabel] === stackId || c.Names?.some((name) => name.startsWith(`/${stackId}_`)))
- );
- const seen = new Set<string>();
- const stackContainers = [...containers, ...nameFilteredContainers].filter((c) => {
- if (seen.has(c.Id)) return false;
- seen.add(c.Id);
- return true;
- });
-
- if (stackContainers.length === 0) {
- return true; // Nothing to stop
- }
-
- console.log(`Found ${stackContainers.length} containers for stack ${stackId}. Attempting to stop and remove...`);
-
- // 2. Stop and Remove each container
- for (const containerInfo of stackContainers) {
- console.log(`Processing container ${containerInfo.Names?.[0]} (ID: ${containerInfo.Id})...`);
- const container = docker.getContainer(containerInfo.Id);
- try {
- // Stop the container if it's running
- if (containerInfo.State === 'running') {
- console.log(`Stopping container ${containerInfo.Id}...`);
- await container.stop(); // Consider adding a timeout option: { t: 10 }
- console.log(`Container ${containerInfo.Id} stopped.`);
- stoppedCount++;
- } else {
- console.log(`Container ${containerInfo.Id} is already stopped (State: ${containerInfo.State}).`);
- }
-
- // Remove the container
- console.log(`Removing container ${containerInfo.Id}...`);
- await container.remove({ force: true }); // Use force to ensure removal even if stopped uncleanly
- console.log(`Container ${containerInfo.Id} removed.`);
- removedCount++;
- } catch (containerErr) {
- // Log error for specific container but continue with others
- console.error(`Error processing container ${containerInfo.Id} for stack ${stackId}:`, containerErr);
- // Optionally decide if this should cause the whole function to fail
- }
- }
-
- // 3. (Optional) Remove networks associated with the stack
- try {
- const networks = await docker.listNetworks({
- filters: JSON.stringify({
- label: [`${composeProjectLabel}=${stackId}`]
- })
- });
- if (networks.length > 0) {
- console.log(`Found ${networks.length} networks for stack ${stackId}. Attempting to remove...`);
- for (const networkInfo of networks) {
- console.log(`Removing network ${networkInfo.Name} (ID: ${networkInfo.Id})...`);
- const network = docker.getNetwork(networkInfo.Id);
- try {
- await network.remove();
- console.log(`Network ${networkInfo.Name} removed.`);
- } catch (networkErr) {
- console.error(`Error removing network ${networkInfo.Name} (ID: ${networkInfo.Id}):`, networkErr);
- }
- }
- } else {
- console.log(`No networks found specifically for stack ${stackId}.`);
- }
- } catch (networkListErr) {
- console.error(`Error listing networks for stack ${stackId}:`, networkListErr);
- }
-
- console.log(`Stack ${stackId} processing complete. Stopped: ${stoppedCount}, Removed: ${removedCount}.`);
-
- // Invalidate the cache after stopping
- stackCache.delete('compose-stacks');
-
- return true; // Indicate overall success even if some individual steps had errors (adjust if needed)
- } catch (err: unknown) {
- // Log the specific error
- console.error(`Error during manual stop/remove for stack ${stackId}:`, err);
- const errorMessage = err instanceof Error ? err.message : String(err);
- throw new Error(`Failed to stop stack ${stackId}: ${errorMessage}`);
- }
-}
-
-export async function renameStack(currentStackId: string, newName: string): Promise<Stack> {
- if (!currentStackId || !newName) {
- throw new Error('Current stack ID and new name must be provided.');
- }
-
- const currentStackDir = await getStackDir(currentStackId);
- try {
- await fs.access(currentStackDir); // Check if current stack directory exists
- } catch (e) {
- throw new Error(`Stack with ID '${currentStackId}' not found at ${currentStackDir}.`);
- }
-
- // Slugify the new name to create a valid base for the directory name
- const newDirBaseName = slugify(newName, {
- lower: true,
- strict: true,
- replacement: '-',
- trim: true
- });
-
- if (newDirBaseName === currentStackId) {
- throw new Error(`The new name '${newName}' (resolves to '${newDirBaseName}') is effectively the same as the current stack ID '${currentStackId}'. No changes made.`);
- }
-
- // Check if the stack is running
- const running = await isStackRunning(currentStackId);
- if (running) {
- throw new Error(`Stack '${currentStackId}' is currently running. Please stop it before renaming.`);
- }
-
- const stacksDir = await ensureStacksDir();
- let newUniqueDirName = newDirBaseName;
- let counter = 1;
- const MAX_ATTEMPTS = 100; // Safety break for the loop
-
- // Find a unique directory name that is not the currentStackId
- while (counter <= MAX_ATTEMPTS) {
- const pathToCheck = join(stacksDir, newUniqueDirName);
- const exists = await directoryExists(pathToCheck);
-
- if (!exists && newUniqueDirName !== currentStackId) {
- break; // Found a suitable unique name
- }
-
- // If it exists or it's the same as currentStackId, generate a new one
- newUniqueDirName = `${newDirBaseName}-${counter}`;
- counter++;
- }
-
- if (counter > MAX_ATTEMPTS || newUniqueDirName === currentStackId || (await directoryExists(join(stacksDir, newUniqueDirName)))) {
- // This means after MAX_ATTEMPTS, we couldn't find a suitable unique name
- throw new Error(`Could not generate a unique directory name for '${newName}' that is different from '${currentStackId}' and does not already exist. Please try a different name.`);
- }
-
- const newStackDir = join(stacksDir, newUniqueDirName);
-
- try {
- console.log(`Renaming stack directory from '${currentStackDir}' to '${newStackDir}'...`);
- await fs.rename(currentStackDir, newStackDir);
- console.log(`Stack directory for '${currentStackId}' successfully renamed to '${newUniqueDirName}'.`);
-
- // Invalidate the cache after renaming
- stackCache.delete('compose-stacks');
-
- // The stack was stopped. When it's started next using `startStack(newUniqueDirName)`,
- // dockerode-compose will use `newUniqueDirName` as the project name,
- // effectively creating a "new" project from Docker's perspective with the existing files.
- // Old Docker resources (containers, networks, volumes) tied to `currentStackId` will remain
- // until manually pruned or if they conflict and Docker handles it.
-
- return await getStack(newUniqueDirName); // Return the stack info under its new ID/name
- } catch (err) {
- console.error(`Error renaming stack directory for '${currentStackId}' to '${newUniqueDirName}':`, err);
- // If fs.rename fails, the original directory should ideally still be there.
- // No complex rollback needed for fs.rename itself.
- const errorMessage = err instanceof Error ? err.message : String(err);
- throw new Error(`Failed to rename stack: ${errorMessage}`);
- }
-}
-
-/**
- * The function `discoverExternalStacks` asynchronously discovers external Docker stacks and their
- * services, categorizing them based on their running status.
- * @returns The `discoverExternalStacks` function returns a Promise that resolves to an array of
- * `Stack` objects representing external stacks discovered by querying Docker containers. The `Stack`
- * objects contain information about the external stacks, such as the stack name, services, service
- * count, running service count, status (running, stopped, partially running), and timestamps for
- * creation and update. If an error occurs during the discovery
- */
-export async function discoverExternalStacks(): Promise<Stack[]> {
- try {
- const docker = await getDockerClient();
- const containers = await docker.listContainers({ all: true });
-
- const composeProjectLabel = 'com.docker.compose.project';
- const composeServiceLabel = 'com.docker.compose.service';
-
- const projectMap: Record<string, Array<{ id: string; name: string; state: { Running: boolean; Status: string; ExitCode: number } }>> = {};
-
- containers.forEach((container) => {
- const labels = container.Labels || {};
- const projectName = labels[composeProjectLabel];
-
- if (projectName) {
- if (!projectMap[projectName]) {
- projectMap[projectName] = [];
- }
-
- projectMap[projectName].push({
- id: container.Id,
- name: labels[composeServiceLabel] || container.Names[0]?.substring(1),
- state: {
- Running: container.State === 'running',
- Status: container.State,
- ExitCode: 0
- }
- });
- }
- });
-
- const externalStacks: Stack[] = [];
-
- for (const [projectName, services] of Object.entries(projectMap)) {
- const stackDir = await getStackDir(projectName);
- try {
- await fs.access(stackDir);
- // Stack is managed by Arcane, skip it
- continue;
- } catch {
- // Stack is not managed by Arcane, include it
- }
-
- const serviceCount = services.length;
- const runningCount = services.filter((s) => s.state.Running).length;
-
- let status: Stack['status'] = 'stopped';
- if (runningCount === serviceCount && serviceCount > 0) {
- status = 'running';
- } else if (runningCount > 0) {
- status = 'partially running';
- }
-
- externalStacks.push({
- id: projectName,
- name: projectName,
- services,
- serviceCount,
- runningCount,
- status,
- isExternal: true,
- createdAt: new Date().toISOString(),
- updatedAt: new Date().toISOString()
- });
- }
-
- return externalStacks;
- } catch (err) {
- console.error('Error discovering external stacks:', err);
- return [];
- }
-}
-
-/**
- * The function `importExternalStack` imports an external Docker stack by retrieving containers,
- * reading the compose file, and creating a new stack in Arcane's managed stacks.
- * @param {string} stackId - The `stackId` parameter is a string that represents the identifier of the
- * external stack that you want to import. It is used to locate the stack and its associated containers
- * in the Docker environment.
- * @returns The `importExternalStack` function returns a Promise that resolves to a `Stack` object.
- */
-export async function importExternalStack(stackId: string): Promise<Stack> {
- // 1. First, check if we can find the stack by ID
- const docker = await getDockerClient();
- const containers = await docker.listContainers({ all: true });
-
- // Filter containers that belong to this stack
- const stackContainers = containers.filter((container) => {
- const labels = container.Labels || {};
- return labels['com.docker.compose.project'] === stackId;
- });
-
- if (stackContainers.length === 0) {
- throw new Error(`No containers found for stack '${stackId}'`);
- }
-
- // 2. Try to locate the compose file (if available)
- const container = stackContainers[0]; // Use the first container to get labels
- const labels = container.Labels || {};
-
- // 3. Read the compose file if available, or create a new one
- let composeContent = '';
- let envContent: string | undefined = undefined; // Variable to store .env content
- let actualComposeFilePathUsed = ''; // For logging the path that was attempted
-
- const configFilesLabel = labels['com.docker.compose.project.config_files'];
-
- if (configFilesLabel) {
- const potentialComposePaths = configFilesLabel
- .split(',')
- .map((p) => p.trim())
- .filter((p) => p);
-
- let pathToTry = '';
- if (potentialComposePaths.length > 0) {
- // Prioritize common primary compose file names
- const primaryNames = ['compose.yaml', 'docker-compose.yml', 'compose.yml', 'docker-compose.yaml'];
- for (const name of primaryNames) {
- // Check if any of the paths end with a primary name
- const foundPath = potentialComposePaths.find((p) => path.basename(p) === name);
- if (foundPath) {
- pathToTry = foundPath;
- break;
- }
- }
-
- // If no primary name found in the list, try the first one from the list.
- if (!pathToTry) {
- pathToTry = potentialComposePaths[0];
- }
- }
-
- if (pathToTry) {
- actualComposeFilePathUsed = pathToTry;
- try {
- console.log(`Attempting to read compose file for import from: ${actualComposeFilePathUsed}`);
- composeContent = await fs.readFile(actualComposeFilePathUsed, 'utf8');
- console.log(`Successfully read compose file: ${actualComposeFilePathUsed}. Content length: ${composeContent.length}`);
-
- // Attempt to read .env file from the same directory
- const composeFileDir = path.dirname(actualComposeFilePathUsed);
- const envFilePath = path.join(composeFileDir, '.env');
- try {
- envContent = await fs.readFile(envFilePath, 'utf8');
- console.log(`Successfully read .env file from: ${envFilePath}`);
- } catch (envErr) {
- const nodeEnvErr = envErr as NodeJS.ErrnoException;
- if (nodeEnvErr.code === 'ENOENT') {
- console.log(`.env file not found at ${envFilePath}, proceeding without it.`);
- } else {
- console.warn(`Could not read .env file at ${envFilePath} during import:`, envErr);
- }
- }
- } catch (err) {
- console.warn(`Could not read compose file at ${actualComposeFilePathUsed} during import:`, err);
- // composeContent will remain empty, leading to generation logic below
- }
- } else {
- console.warn(`No suitable compose file path found in 'com.docker.compose.project.config_files' label: "${configFilesLabel}"`);
- }
- } else {
- console.warn(`Label 'com.docker.compose.project.config_files' not found for stack '${stackId}'. Will attempt to generate compose file.`);
- }
-
- // 4. If we couldn't read the compose file, generate one based on container inspection
- if (!composeContent) {
- console.log(`Generating compose file for stack '${stackId}' as no existing file could be read or found.`);
- // Create a basic compose file from container inspection
- const services: Record<string, { image: string }> = {};
-
- for (const cont of stackContainers) {
- // Renamed to 'cont' to avoid conflict with outer 'container'
- const containerLabels = cont.Labels || {};
- const serviceName = containerLabels['com.docker.compose.service'] || cont.Names[0]?.replace(`/${stackId}_`, '').replace(/_\d+$/, '') || `service_${cont.Id.substring(0, 8)}`;
-
- // Inspect the container to get more details
- // const containerDetails = await docker.getContainer(cont.Id).inspect(); // Uncomment if more details are needed
-
- services[serviceName] = {
- image: cont.Image
- // Add other properties based on containerDetails if needed
- // e.g., ports, volumes, environment variables.
- // This part can be expanded for a more comprehensive generated file.
- };
- }
-
- // Generate the compose file content
- composeContent = `# Generated compose file for imported stack: ${stackId}
-# This was automatically generated by Arcane from an external stack.
-# The original compose file could not be read from: ${actualComposeFilePathUsed || 'path not specified in labels'}.
-# You may need to adjust this manually for correct operation.
-
-services:
-${yamlDump({ services }, { indent: 2 }).substring('services:'.length).trimStart()}`;
- // Note: If compose file is generated, we don't have a path to look for an associated .env file.
- // envContent will remain undefined in this case.
- }
-
- // Invalidate the cache after importing
- stackCache.delete('compose-stacks');
-
- // 5. Create a new stack in Arcane's managed stacks
- return await createStack(stackId, normalizeHealthcheckTest(composeContent), envContent); // Pass envContent here
-}
-
-/**
- * Lists all managed stacks (from STACKS_DIR) and optionally external Docker Compose projects.
- * Managed stacks from STACKS_DIR will have their metadata loaded if `hasArcaneMeta` is true.
- * Stacks in STACKS_DIR without Arcane metadata are also listed (`hasArcaneMeta: false`).
- * `stack.id` for all returned stacks refers to their primary identifier (directory name for local, project name for external).
- */
-export async function listStacks(includeExternal = false): Promise<Stack[]> {
- // Get managed stacks from STACKS_DIR.
- // Assumes loadComposeStacks returns Stack[] where:
- // - stack.id is the directory name.
- // - stack.meta is populated if stack.hasArcaneMeta is true.
- // - stack.isExternal is set to false.
- // - stack.hasArcaneMeta is correctly set.
- const managedStacks = await loadComposeStacks();
-
- let allStacks: Stack[] = [...managedStacks];
-
- if (includeExternal) {
- const externalStacksList = await discoverExternalStacks();
- // discoverExternalStacks sets isExternal: true.
- // Ensure hasArcaneMeta is consistently false for these, as they are not from STACKS_DIR.
- const processedExternalStacks = externalStacksList.map((stack) => ({
- ...stack,
- hasArcaneMeta: false // External stacks don't have Arcane meta in STACKS_DIR
- // meta property will be undefined for external stacks from discoverExternalStacks
- }));
- allStacks = [...allStacks, ...processedExternalStacks];
- }
-
- return allStacks;
-}
-
-/**
- * Checks if any service in the stack is currently running.
- * @param {string} stackId - The stack/project name or directory.
- * @returns {Promise<boolean>} - True if any container in the stack is running.
- */
-export async function isStackRunning(stackId: string): Promise<boolean> {
- const docker = await getDockerClient();
- const composeProjectLabel = 'com.docker.compose.project';
-
- try {
- // Find containers belonging to the stack (by label or name convention)
- const containers = await docker.listContainers({
- all: true,
- filters: JSON.stringify({
- label: [`${composeProjectLabel}=${stackId}`]
- })
- });
-
- // Fallback: Also check by name prefix if label is missing
- const allContainers = await docker.listContainers({ all: true });
- const nameFilteredContainers = allContainers.filter((c) => !containers.some((fc) => fc.Id === c.Id) && (c.Labels?.[composeProjectLabel] === stackId || c.Names?.some((name) => name.startsWith(`/${stackId}_`))));
-
- const stackContainers = [...containers, ...nameFilteredContainers];
- // If any container is running, the stack is considered running
- return stackContainers.some((c) => c.State === 'running');
- } catch (err) {
- console.error(`Error checking if stack ${stackId} is running:`, err);
- return false;
- }
-}
-
-/**
- * Normalizes compose content:
- * 1. Ensures healthcheck.test is an array.
- * 2. Substitutes environment variables throughout the compose object.
- * @param {string} composeContent - The YAML content of the compose file.
- * @param {function} envGetter - Optional function to get environment variable values.
- * @returns {string} - The normalized and substituted YAML content.
- */
-export function normalizeHealthcheckTest(composeContent: string, envGetter?: (key: string) => string | undefined): string {
- let doc: any; // Use 'any' for easier manipulation, will be validated by yamlLoad
- try {
- doc = yamlLoad(composeContent);
- if (!doc || typeof doc !== 'object') {
- console.warn('Could not parse compose YAML for normalization or content is not an object.');
- return composeContent;
- }
- } catch (e) {
- console.warn('Could not parse compose YAML for normalization:', e);
- return composeContent;
- }
-
- let modified = false;
-
- // Perform healthcheck normalization
- if (doc.services && typeof doc.services === 'object') {
- for (const serviceName in doc.services) {
- if (Object.prototype.hasOwnProperty.call(doc.services, serviceName)) {
- const service = doc.services[serviceName];
- if (service && service.healthcheck && typeof service.healthcheck === 'object' && service.healthcheck.test && typeof service.healthcheck.test === 'string') {
- service.healthcheck.test = service.healthcheck.test
- .trim()
- .split(/\s+/)
- .filter((s: string) => s.length > 0);
- modified = true;
- }
- }
- }
- }
-
- // Perform variable substitution on the entire document object if envGetter is provided
- if (envGetter) {
- const originalDocSnapshot = JSON.stringify(doc);
- doc = substituteVariablesInObject(doc, envGetter);
- if (JSON.stringify(doc) !== originalDocSnapshot) {
- modified = true;
- }
- }
-
- if (modified) {
- // Critical check: After substitution, ensure container_name does not contain unresolved variables
- if (doc.services && typeof doc.services === 'object') {
- for (const serviceName in doc.services) {
- if (Object.prototype.hasOwnProperty.call(doc.services, serviceName)) {
- const service = doc.services[serviceName];
- if (service && typeof service.container_name === 'string' && service.container_name.includes('${')) {
- console.error(`CRITICAL: Unresolved variable in container_name for service '${serviceName}': ${service.container_name}. ` + `This will likely cause Docker to fail. Ensure the environment variable is defined.`);
- // Depending on desired behavior, you might throw an error here
- // throw new Error(`Unresolved variable in container_name for service '${serviceName}': ${service.container_name}`);
- }
- }
- }
- }
- return yamlDump(doc, { lineWidth: -1 });
- }
- return composeContent;
-}
-
-/**
- * Safely parses YAML content as a string (not a file path)
- * Creates a proper wrapper around yaml-env-defaults to ensure it treats input as content
- * @param content YAML content as a string
- * @param envGetter Optional function to get environment variable values
- * @returns Parsed object or null if parsing fails
- */
-export function parseYamlContent(content: string, envGetter?: (key: string) => string | undefined): Record<string, any> | null {
- try {
- // Use js-yaml directly without any potential CommonJS dependencies
- const parsedYaml = yamlLoad(content);
-
- if (!parsedYaml || typeof parsedYaml !== 'object') {
- console.warn('Parsed YAML content is not an object or is null.');
- return null;
- }
-
- if (envGetter) {
- return substituteVariablesInObject(parsedYaml, envGetter);
- }
- return parsedYaml as Record<string, any>;
- } catch (error) {
- console.error('Error parsing YAML content:', error);
- return null;
- }
-}
-
-// Helper function to recursively substitute variables in an object
-function substituteVariablesInObject(obj: any, envGetter: (key: string) => string | undefined): any {
- if (Array.isArray(obj)) {
- return obj.map((item) => substituteVariablesInObject(item, envGetter));
- } else if (typeof obj === 'object' && obj !== null) {
- const newObj: Record<string, any> = {};
- for (const key in obj) {
- if (Object.prototype.hasOwnProperty.call(obj, key)) {
- newObj[key] = substituteVariablesInObject(obj[key], envGetter);
- }
- }
- return newObj;
- } else if (typeof obj === 'string') {
- let S = obj;
- // Loop to handle multiple/nested variables, with a safety break
- for (let i = 0; i < 10 && S.includes('${'); i++) {
- S = S.replace(/\$\{([^}]+)\}/g, (match, varName) => {
- const value = envGetter(varName);
- // If value is found, substitute. Otherwise, keep the placeholder.
- return value !== undefined ? value : match;
- });
- }
- if (S.includes('${')) {
- console.warn(`Unresolved variable or recursive substitution limit reached in string: "${obj}". Result: "${S}"`);
- }
- return S;
- }
- return obj;
-}
diff --git a/src/lib/services/settings-service.ts b/src/lib/services/settings-service.ts
index b12894bdc..be318e8c3 100644
--- a/src/lib/services/settings-service.ts
+++ b/src/lib/services/settings-service.ts
@@ -4,6 +4,7 @@ import proper from 'proper-lockfile';
import type { Settings, OidcConfig } from '$lib/types/settings.type';
import { encrypt, decrypt } from './encryption-service';
import { SETTINGS_DIR, STACKS_DIR, ensureDirectory } from './paths-service';
+import { getSettingsFromDb, saveSettingsToDb } from './database/settings-db-service';
let env: any;
try {
env = await import('$env/static/private');
@@ -102,126 +103,60 @@ async function saveSetting(key: string, value: any): Promise<void> {
}
export async function getSettings(): Promise<Settings> {
- let effectiveSettings: Settings;
-
try {
- await ensureSettingsDir();
- const filePath = path.join(SETTINGS_DIR, 'settings.dat');
+ // Try to get settings from database first
+ const dbSettings = await getSettingsFromDb();
- try {
- await fs.access(filePath);
- const rawData = await fs.readFile(filePath, 'utf8');
- const settingsFromFile = JSON.parse(rawData);
-
- const baseSettings = JSON.parse(JSON.stringify(DEFAULT_SETTINGS)) as Settings;
-
- if (settingsFromFile._encrypted) {
- const { _encrypted, ...nonSensitiveSettings } = settingsFromFile;
- const decryptedData = await decrypt(_encrypted);
-
- effectiveSettings = {
- ...baseSettings,
- ...nonSensitiveSettings,
- auth: {
- ...baseSettings.auth,
- ...(nonSensitiveSettings.auth || {}),
- ...(decryptedData.auth || {})
- },
- registryCredentials: decryptedData.registryCredentials || baseSettings.registryCredentials,
- templateRegistries: nonSensitiveSettings.templateRegistries || baseSettings.templateRegistries,
- onboarding: nonSensitiveSettings.onboarding || baseSettings.onboarding,
- baseServerUrl: nonSensitiveSettings.baseServerUrl || baseSettings.baseServerUrl
- };
- } else {
- effectiveSettings = {
- ...baseSettings,
- ...settingsFromFile,
- auth: {
- ...baseSettings.auth,
- ...(settingsFromFile.auth || {})
- },
- registryCredentials: settingsFromFile.registryCredentials || baseSettings.registryCredentials,
- templateRegistries: settingsFromFile.templateRegistries || baseSettings.templateRegistries,
- onboarding: settingsFromFile.onboarding || baseSettings.onboarding,
- baseServerUrl: settingsFromFile.baseServerUrl || baseSettings.baseServerUrl
- };
- }
- } catch (fileError) {
- console.warn('Settings file not found or unreadable, using default settings.', fileError instanceof Error ? fileError.message : fileError);
+ let effectiveSettings: Settings;
+
+ if (dbSettings) {
+ effectiveSettings = dbSettings;
+ } else {
+ // If no settings in database, use defaults
+ console.log('No settings found in database, using default settings');
effectiveSettings = JSON.parse(JSON.stringify(DEFAULT_SETTINGS)) as Settings;
}
- } catch (dirError) {
- console.error('Critical error ensuring settings directory or reading settings file:', dirError);
- effectiveSettings = JSON.parse(JSON.stringify(DEFAULT_SETTINGS)) as Settings;
- }
- const oidcClientId = env.OIDC_CLIENT_ID;
- const oidcClientSecret = env.OIDC_CLIENT_SECRET;
- const oidcRedirectUri = env.OIDC_REDIRECT_URI;
- const oidcAuthorizationEndpoint = env.OIDC_AUTHORIZATION_ENDPOINT;
- const oidcTokenEndpoint = env.OIDC_TOKEN_ENDPOINT;
- const oidcUserinfoEndpoint = env.OIDC_USERINFO_ENDPOINT;
- const oidcScopesEnv = env.OIDC_SCOPES;
-
- if (oidcClientId && oidcClientSecret && oidcRedirectUri && oidcAuthorizationEndpoint && oidcTokenEndpoint && oidcUserinfoEndpoint) {
- if (!effectiveSettings.auth) {
- effectiveSettings.auth = JSON.parse(JSON.stringify(DEFAULT_SETTINGS.auth));
+ // Override with environment variables if present (for OIDC config)
+ const oidcClientId = env.OIDC_CLIENT_ID;
+ const oidcClientSecret = env.OIDC_CLIENT_SECRET;
+ const oidcRedirectUri = env.OIDC_REDIRECT_URI;
+ const oidcAuthorizationEndpoint = env.OIDC_AUTHORIZATION_ENDPOINT;
+ const oidcTokenEndpoint = env.OIDC_TOKEN_ENDPOINT;
+ const oidcUserinfoEndpoint = env.OIDC_USERINFO_ENDPOINT;
+ const oidcScopesEnv = env.OIDC_SCOPES;
+
+ if (oidcClientId && oidcClientSecret && oidcRedirectUri && oidcAuthorizationEndpoint && oidcTokenEndpoint && oidcUserinfoEndpoint) {
+ if (!effectiveSettings.auth) {
+ effectiveSettings.auth = JSON.parse(JSON.stringify(DEFAULT_SETTINGS.auth));
+ }
+
+ const oidcConfigFromEnv: OidcConfig = {
+ clientId: oidcClientId,
+ clientSecret: oidcClientSecret,
+ redirectUri: oidcRedirectUri,
+ authorizationEndpoint: oidcAuthorizationEndpoint,
+ tokenEndpoint: oidcTokenEndpoint,
+ userinfoEndpoint: oidcUserinfoEndpoint,
+ scopes: oidcScopesEnv || effectiveSettings.auth.oidc?.scopes || DEFAULT_SETTINGS.auth.oidc?.scopes || 'openid email profile'
+ };
+ effectiveSettings.auth.oidc = oidcConfigFromEnv;
}
- const oidcConfigFromEnv: OidcConfig = {
- clientId: oidcClientId,
- clientSecret: oidcClientSecret,
- redirectUri: oidcRedirectUri,
- authorizationEndpoint: oidcAuthorizationEndpoint,
- tokenEndpoint: oidcTokenEndpoint,
- userinfoEndpoint: oidcUserinfoEndpoint,
- scopes: oidcScopesEnv || effectiveSettings.auth.oidc?.scopes || DEFAULT_SETTINGS.auth.oidc?.scopes || 'openid email profile'
- };
- effectiveSettings.auth.oidc = oidcConfigFromEnv;
+ return effectiveSettings;
+ } catch (error) {
+ console.error('Error getting settings from database, falling back to defaults:', error);
+ return JSON.parse(JSON.stringify(DEFAULT_SETTINGS)) as Settings;
}
-
- return effectiveSettings;
}
export async function saveSettings(settings: Settings): Promise<void> {
- await ensureSettingsDir();
- const filePath = path.join(SETTINGS_DIR, 'settings.dat');
-
try {
- await fs.access(filePath);
- } catch {
- await fs.writeFile(filePath, '{}', { mode: 0o600 });
- }
-
- let release;
- try {
- release = await proper.lock(filePath, {
- retries: 5,
- stale: 10000,
- onCompromised: (err) => {
- console.error('Lock was compromised:', err);
- }
- });
-
- const { auth, registryCredentials, ...nonSensitiveSettings } = settings;
-
- const dataToSave = {
- ...nonSensitiveSettings,
- _encrypted: await encrypt({ auth: auth || DEFAULT_SETTINGS.auth, registryCredentials: registryCredentials || [] })
- };
-
- await fs.writeFile(filePath, JSON.stringify(dataToSave, null, 2), { mode: 0o600 });
+ await saveSettingsToDb(settings);
+ console.log('Settings saved to database successfully');
} catch (error) {
- console.error('Error saving settings with lock:', error);
+ console.error('Error saving settings to database:', error);
const errorMessage = error instanceof Error ? error.message : String(error);
throw new Error(`Failed to save settings: ${errorMessage}`);
- } finally {
- if (release) {
- try {
- await release();
- } catch (releaseError) {
- console.error('Error releasing lock:', releaseError);
- }
- }
}
}
diff --git a/src/lib/services/user-service.ts b/src/lib/services/user-service.ts
index 6f4484efa..57d04720e 100644
--- a/src/lib/services/user-service.ts
+++ b/src/lib/services/user-service.ts
@@ -2,16 +2,46 @@ import fs from 'node:fs/promises';
import path from 'node:path';
import bcrypt from 'bcryptjs';
import crypto from 'node:crypto';
-import { nanoid } from 'nanoid';
-import { encrypt, decrypt } from './encryption-service';
+import { decrypt } from './encryption-service';
import { USER_DIR, ensureDirectory } from './paths-service';
import type { User } from '$lib/types/user.type';
+// Import database functions
+import { getUserByIdFromDb, getUserByUsernameFromDb, getUserByOidcSubjectIdFromDb, listUsersFromDb, saveUserToDb } from './database/user-db-service';
+// Export database functions as the default user functions
+export const getUserById = getUserByIdFromDb;
+export const getUserByUsername = getUserByUsernameFromDb;
+export const getUserByOidcSubjectId = getUserByOidcSubjectIdFromDb;
+export const listUsers = listUsersFromDb;
+export const saveUser = saveUserToDb;
+
+// Keep these utility functions that don't need database changes
+export async function verifyPassword(user: User, password: string): Promise<boolean> {
+ if (typeof user.passwordHash !== 'string') {
+ return false;
+ }
+ return await bcrypt.compare(password, user.passwordHash);
+}
+
+export async function hashPassword(password: string): Promise<string> {
+ return await bcrypt.hash(password, 14);
+}
+
+export async function deriveKeyFromPassword(password: string, salt: string): Promise<string> {
+ return new Promise((resolve, reject) => {
+ crypto.pbkdf2(password, salt, 150000, 64, 'sha512', (err, derivedKey) => {
+ if (err) reject(err);
+ else resolve(derivedKey.toString('hex'));
+ });
+ });
+}
+
+// Keep the old file-based functions for migration purposes
async function ensureUserDir() {
await ensureDirectory(USER_DIR, 0o700);
}
-export async function getUserByUsername(username: string): Promise<User | null> {
+export async function getUserByUsernameFromFile(username: string): Promise<User | null> {
try {
await ensureUserDir();
@@ -35,66 +65,7 @@ export async function getUserByUsername(username: string): Promise<User | null>
}
}
-export async function getUserById(id: string): Promise<User | null> {
- try {
- await ensureUserDir();
-
- const filePath = path.join(USER_DIR, `${id}.dat`);
-
- await fs.access(filePath);
-
- const encryptedData = await fs.readFile(filePath, 'utf8');
- const user = (await decrypt(encryptedData)) as User;
-
- return user;
- } catch (error) {
- return null;
- }
-}
-
-export async function saveUser(user: User): Promise<User> {
- await ensureUserDir();
-
- if (!user.id) {
- user.id = nanoid();
- user.createdAt = new Date().toISOString();
- }
-
- const filePath = path.join(USER_DIR, `${user.id}.dat`);
-
- const encryptedData = await encrypt(user);
-
- try {
- await fs.writeFile(filePath, encryptedData, { mode: 0o600 });
- } catch (error) {
- console.error('Error saving encrypted user data:', error);
- throw error;
- }
-
- return user;
-}
-
-export async function verifyPassword(user: User, password: string): Promise<boolean> {
- if (typeof user.passwordHash !== 'string') {
- return false;
- }
- return await bcrypt.compare(password, user.passwordHash);
-}
-
-export async function hashPassword(password: string): Promise<string> {
- return await bcrypt.hash(password, 14);
-}
-
-export async function deriveKeyFromPassword(password: string, salt: string): Promise<string> {
- return new Promise((resolve, reject) => {
- crypto.pbkdf2(password, salt, 150000, 64, 'sha512', (err, derivedKey) => {
- if (err) reject(err);
- else resolve(derivedKey.toString('hex'));
- });
- });
-}
-
-export async function listUsers(): Promise<User[]> {
+export async function listUsersFromFile(): Promise<User[]> {
try {
await ensureUserDir();
@@ -120,9 +91,3 @@ export async function listUsers(): Promise<User[]> {
return [];
}
}
-
-export async function getUserByOidcSubjectId(oidcSubjectId: string): Promise<User | null> {
- const users = await listUsers();
- const user = users.find((u) => u.oidcSubjectId === oidcSubjectId);
- return user || null;
-}
diff --git a/src/lib/types/docker/stack.type.ts b/src/lib/types/docker/stack.type.ts
index 04c426541..8e2f211a9 100644
--- a/src/lib/types/docker/stack.type.ts
+++ b/src/lib/types/docker/stack.type.ts
@@ -54,6 +54,9 @@ export interface Stack {
agentId?: string;
agentHostname?: string;
isRemote?: boolean;
+ dirName?: string;
+ lastPolled?: string;
+ path?: string;
}
export interface StackUpdate {
diff --git a/src/lib/utils/date.utils.ts b/src/lib/utils/date.utils.ts
new file mode 100644
index 000000000..ffc6f2357
--- /dev/null
+++ b/src/lib/utils/date.utils.ts
@@ -0,0 +1,50 @@
+/**
+ * Format date to relative time (e.g., "2 days ago", "3 hours ago")
+ */
+export function formatRelativeTime(dateString: string): string {
+ const date = new Date(dateString);
+ const now = new Date();
+ const diffMs = now.getTime() - date.getTime();
+ const diffSeconds = Math.floor(diffMs / 1000);
+ const diffMinutes = Math.floor(diffSeconds / 60);
+ const diffHours = Math.floor(diffMinutes / 60);
+ const diffDays = Math.floor(diffHours / 24);
+ const diffWeeks = Math.floor(diffDays / 7);
+ const diffMonths = Math.floor(diffDays / 30);
+ const diffYears = Math.floor(diffDays / 365);
+
+ if (diffSeconds < 60) return 'Just now';
+ if (diffMinutes < 60) return `${diffMinutes}m ago`;
+ if (diffHours < 24) return `${diffHours}h ago`;
+ if (diffDays < 7) return `${diffDays}d ago`;
+ if (diffWeeks < 4) return `${diffWeeks}w ago`;
+ if (diffMonths < 12) return `${diffMonths}mo ago`;
+ return `${diffYears}y ago`;
+}
+
+/**
+ * Format date to human-readable format (e.g., "May 4, 2025 at 4:58 AM")
+ */
+export function formatFriendlyDate(dateString: string): string {
+ const date = new Date(dateString);
+ return date.toLocaleDateString('en-US', {
+ month: 'short',
+ day: 'numeric',
+ year: 'numeric',
+ hour: 'numeric',
+ minute: '2-digit',
+ hour12: true
+ });
+}
+
+/**
+ * Format date to short format (e.g., "May 4, 2025")
+ */
+export function formatShortDate(dateString: string): string {
+ const date = new Date(dateString);
+ return date.toLocaleDateString('en-US', {
+ month: 'short',
+ day: 'numeric',
+ year: 'numeric'
+ });
+}
diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte
index 3416743a9..5ae31febd 100644
--- a/src/routes/+page.svelte
+++ b/src/routes/+page.svelte
@@ -383,7 +383,7 @@
selection: false
}}
sort={{
- defaultSort: { id: 'Status', desc: false }
+ defaultSort: { id: 'statusSortValue', desc: false }
}}
pagination={{
pageSize: 5,
diff --git a/src/routes/agents/[agentId]/+page.server.ts b/src/routes/agents/[agentId]/+page.server.ts
index c0515002c..ed1b52d16 100644
--- a/src/routes/agents/[agentId]/+page.server.ts
+++ b/src/routes/agents/[agentId]/+page.server.ts
@@ -1,6 +1,6 @@
import type { PageServerLoad } from './$types';
import { getAgent, getAgentTasks } from '$lib/services/agent/agent-manager';
-import { getDeployments } from '$lib/services/deployment-service';
+import { getDeploymentsFromDb } from '$lib/services/database/deployment-db-service';
import { error } from '@sveltejs/kit';
export const load: PageServerLoad = async ({ params }) => {
@@ -8,7 +8,7 @@ export const load: PageServerLoad = async ({ params }) => {
try {
// Load all data in parallel
- const [agent, tasks, deployments] = await Promise.allSettled([getAgent(agentId), getAgentTasks(agentId), getDeployments(agentId)]);
+ const [agent, tasks, deployments] = await Promise.allSettled([getAgent(agentId), getAgentTasks(agentId), getDeploymentsFromDb(agentId)]);
// Handle agent not found
if (agent.status === 'rejected' || !agent.value) {
@@ -17,7 +17,6 @@ export const load: PageServerLoad = async ({ params }) => {
});
}
- // Calculate actual status (move this logic to server)
const now = new Date();
const timeout = 5 * 60 * 1000; // 5 minutes
const lastSeen = new Date(agent.value.lastSeen);
diff --git a/src/routes/api/settings/+server.ts b/src/routes/api/settings/+server.ts
index b4e3d62d8..a6775a851 100644
--- a/src/routes/api/settings/+server.ts
+++ b/src/routes/api/settings/+server.ts
@@ -2,7 +2,7 @@ import { json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { getSettings as getPersistedSettings, saveSettings as persistSettings } from '$lib/services/settings-service';
import type { Settings } from '$lib/types/settings.type';
-import { initComposeService } from '$lib/services/docker/stack-service';
+import { initComposeService } from '$lib/services/docker/stack-custom-service';
import { initAutoUpdateScheduler, stopAutoUpdateScheduler } from '$lib/services/docker/scheduler-service';
import { ApiErrorCode, type ApiErrorResponse } from '$lib/types/errors.type';
import { tryCatch } from '$lib/utils/try-catch';
diff --git a/src/routes/api/stacks/[stackId]/+server.ts b/src/routes/api/stacks/[stackId]/+server.ts
index 13c50d2b9..b781eae8c 100644
--- a/src/routes/api/stacks/[stackId]/+server.ts
+++ b/src/routes/api/stacks/[stackId]/+server.ts
@@ -1,6 +1,6 @@
import type { RequestHandler } from './$types';
import { json } from '@sveltejs/kit';
-import { updateStack } from '$lib/services/docker/stack-service';
+import { updateStack } from '$lib/services/docker/stack-custom-service';
import { ApiErrorCode, type ApiErrorResponse } from '$lib/types/errors.type';
import { tryCatch } from '$lib/utils/try-catch';
diff --git a/src/routes/api/stacks/[stackId]/pull/+server.ts b/src/routes/api/stacks/[stackId]/pull/+server.ts
index dd53a9a2c..ab82b8874 100644
--- a/src/routes/api/stacks/[stackId]/pull/+server.ts
+++ b/src/routes/api/stacks/[stackId]/pull/+server.ts
@@ -1,5 +1,5 @@
import type { RequestHandler } from './$types';
-import { getStack } from '$lib/services/docker/stack-service';
+import { getStack } from '$lib/services/docker/stack-custom-service';
import { ApiErrorCode, type ApiErrorResponse } from '$lib/types/errors.type';
import { tryCatch } from '$lib/utils/try-catch';
import { getDockerClient } from '$lib/services/docker/core';
diff --git a/src/routes/api/stacks/create/+server.ts b/src/routes/api/stacks/create/+server.ts
index de9784b60..7a0f4059e 100644
--- a/src/routes/api/stacks/create/+server.ts
+++ b/src/routes/api/stacks/create/+server.ts
@@ -1,6 +1,6 @@
import { json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
-import { createStack } from '$lib/services/docker/stack-service';
+import { createStack } from '$lib/services/docker/stack-custom-service';
import { ApiErrorCode, type ApiErrorResponse } from '$lib/types/errors.type';
import { tryCatch } from '$lib/utils/try-catch';
diff --git a/src/routes/api/stacks/import/+server.ts b/src/routes/api/stacks/import/+server.ts
index b92830317..7c2127f8f 100644
--- a/src/routes/api/stacks/import/+server.ts
+++ b/src/routes/api/stacks/import/+server.ts
@@ -1,6 +1,6 @@
import { json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
-import { importExternalStack } from '$lib/services/docker/stack-service';
+import { importExternalStack } from '$lib/services/docker/stack-custom-service';
import { ApiErrorCode, type ApiErrorResponse } from '$lib/types/errors.type';
import { tryCatch } from '$lib/utils/try-catch';
diff --git a/src/routes/api/users/+server.ts b/src/routes/api/users/+server.ts
index dcaf0d557..b22a69c7f 100644
--- a/src/routes/api/users/+server.ts
+++ b/src/routes/api/users/+server.ts
@@ -5,6 +5,7 @@ import type { User } from '$lib/types/user.type';
import { getSettings } from '$lib/services/settings-service';
import { ApiErrorCode, type ApiErrorResponse } from '$lib/types/errors.type';
import { tryCatch } from '$lib/utils/try-catch';
+import { nanoid } from 'nanoid'; // Add this import
export const GET: RequestHandler = async ({ locals }) => {
if (!locals.user || !locals.user.roles.includes('admin')) {
@@ -104,7 +105,7 @@ export const POST: RequestHandler = async ({ request, locals }) => {
}
const newUser: User = {
- id: '',
+ id: nanoid(), // Generate a unique ID
username,
passwordHash: hashResult.data,
displayName: displayName || username,
diff --git a/src/routes/api/users/[id]/+server.ts b/src/routes/api/users/[id]/+server.ts
index a746e13ef..54bef62ab 100644
--- a/src/routes/api/users/[id]/+server.ts
+++ b/src/routes/api/users/[id]/+server.ts
@@ -1,16 +1,12 @@
import { json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { getUserById, saveUser, hashPassword } from '$lib/services/user-service';
+import { deleteUserFromDb } from '$lib/services/database/user-db-service';
import type { User } from '$lib/types/user.type';
import { getSettings } from '$lib/services/settings-service';
-import fs from 'fs/promises';
-import path from 'node:path';
-import { BASE_PATH } from '$lib/services/paths-service';
import { ApiErrorCode, type ApiErrorResponse } from '$lib/types/errors.type';
import { tryCatch } from '$lib/utils/try-catch';
-const USER_DIR = path.join(BASE_PATH, 'users');
-
export const PUT: RequestHandler = async ({ request, params, locals }) => {
try {
const userId = params.id;
@@ -34,10 +30,11 @@ export const PUT: RequestHandler = async ({ request, params, locals }) => {
// Create updated user object
const updatedUser: User = {
...existingUser,
- ...(username !== undefined && { username }), // Make sure this line is included
+ ...(username !== undefined && { username }),
...(displayName !== undefined && { displayName }),
...(email !== undefined && { email }),
- ...(roles !== undefined && { roles })
+ ...(roles !== undefined && { roles }),
+ updatedAt: new Date().toISOString()
};
// Handle password update if provided
@@ -53,7 +50,7 @@ export const PUT: RequestHandler = async ({ request, params, locals }) => {
success: true,
user: {
...updatedUser,
- passwordHash: undefined // Don't return password hash
+ passwordHash: undefined
}
});
} catch (error) {
@@ -83,29 +80,40 @@ export const DELETE: RequestHandler = async ({ params, locals }) => {
return json(response, { status: 400 });
}
- const userFile = path.join(USER_DIR, `${userId}.dat`);
+ try {
+ // Check if user exists in database
+ const existingUser = await getUserById(userId);
+ if (!existingUser) {
+ const response: ApiErrorResponse = {
+ success: false,
+ error: 'User not found',
+ code: ApiErrorCode.NOT_FOUND
+ };
+ return json(response, { status: 404 });
+ }
- const accessResult = await tryCatch(fs.access(userFile));
- if (accessResult.error) {
- const response: ApiErrorResponse = {
- success: false,
- error: 'User not found',
- code: ApiErrorCode.NOT_FOUND
- };
- return json(response, { status: 404 });
- }
+ // Delete user from database
+ const deleteResult = await tryCatch(deleteUserFromDb(userId));
+ if (deleteResult.error) {
+ console.error('Error deleting user from database:', deleteResult.error);
+ const response: ApiErrorResponse = {
+ success: false,
+ error: 'Failed to delete user',
+ code: ApiErrorCode.INTERNAL_SERVER_ERROR,
+ details: deleteResult.error
+ };
+ return json(response, { status: 500 });
+ }
- const unlinkResult = await tryCatch(fs.unlink(userFile));
- if (unlinkResult.error) {
- console.error('Error deleting user:', unlinkResult.error);
+ return json({ success: true });
+ } catch (error) {
+ console.error('Error deleting user:', error);
const response: ApiErrorResponse = {
success: false,
error: 'Failed to delete user',
code: ApiErrorCode.INTERNAL_SERVER_ERROR,
- details: unlinkResult.error
+ details: error
};
return json(response, { status: 500 });
}
-
- return json({ success: true });
};
diff --git a/src/routes/auth/login/+page.server.ts b/src/routes/auth/login/+page.server.ts
index 57636f3aa..65ada6153 100644
--- a/src/routes/auth/login/+page.server.ts
+++ b/src/routes/auth/login/+page.server.ts
@@ -1,6 +1,7 @@
import { fail, redirect } from '@sveltejs/kit';
import type { Actions, PageServerLoad } from './$types';
-import { getUserByUsername, verifyPassword } from '$lib/services/user-service';
+import { getUserByUsernameFromDb } from '$lib/services/database/user-db-service';
+import { verifyPassword } from '$lib/services/user-service';
import { getSettings } from '$lib/services/settings-service';
export const load: PageServerLoad = async ({ locals, url }) => {
@@ -37,7 +38,7 @@ export const actions: Actions = {
const redirectTo = formData.get('redirectTo')?.toString() || '/'; // Ensure redirectTo is retrieved from form
try {
- const user = await getUserByUsername(username);
+ const user = await getUserByUsernameFromDb(username);
if (!user) {
console.log(`User not found: ${username}`);
diff --git a/src/routes/compose/+page.server.ts b/src/routes/compose/+page.server.ts
index 6e7ead86b..f8fe14d29 100644
--- a/src/routes/compose/+page.server.ts
+++ b/src/routes/compose/+page.server.ts
@@ -1,4 +1,4 @@
-import { loadComposeStacks, discoverExternalStacks } from '$lib/services/docker/stack-service';
+import { loadComposeStacks, discoverExternalStacks } from '$lib/services/docker/stack-custom-service';
import { listAgents } from '$lib/services/agent/agent-manager';
import { getAllAgentStacks } from '$lib/services/agent/agent-stack-service';
import type { PageServerLoad } from './$types';
diff --git a/src/routes/compose/+page.svelte b/src/routes/compose/+page.svelte
index 34b525fb7..ab540d873 100644
--- a/src/routes/compose/+page.svelte
+++ b/src/routes/compose/+page.svelte
@@ -19,7 +19,7 @@
import type { StackActions } from '$lib/types/actions.type';
import ArcaneButton from '$lib/components/arcane-button.svelte';
import { tablePersistence } from '$lib/stores/table-store';
- import type { Agent } from '$lib/types/agent.type';
+ import { formatRelativeTime, formatFriendlyDate } from '$lib/utils/date.utils';
let { data }: { data: PageData } = $props();
let stacks = $derived(data.stacks);
@@ -460,6 +460,9 @@
onPageSizeChange={(newSize) => {
tablePersistence.setPageSize('stacks', newSize);
}}
+ sort={{
+ defaultSort: { id: 'name', desc: false }
+ }}
display={{
filterPlaceholder: 'Search compose projects...',
noResultsMessage: 'No stacks found'
@@ -499,7 +502,8 @@
<Table.Cell>
<StatusBadge variant={item.isExternal ? 'amber' : item.isRemote ? 'blue' : 'green'} text={item.isExternal ? 'External' : item.isRemote ? 'Remote' : 'Managed'} />
</Table.Cell>
- <Table.Cell>{item.createdAt}</Table.Cell>
+ <!-- Simple single line format -->
+ <Table.Cell>{formatFriendlyDate(item.createdAt || '')}</Table.Cell>
<Table.Cell>
{#if item.isExternal}
<ArcaneButton action="pull" customLabel="Import" onClick={() => handleImportStack(item.id, item.name)} loading={isLoading.import} disabled={isLoading.import} />
diff --git a/src/routes/compose/[composeId]/+page.server.ts b/src/routes/compose/[composeId]/+page.server.ts
index b840ef468..8b4ddf62c 100644
--- a/src/routes/compose/[composeId]/+page.server.ts
+++ b/src/routes/compose/[composeId]/+page.server.ts
@@ -1,4 +1,4 @@
-import { getStack } from '$lib/services/docker/stack-service';
+import { getStack } from '$lib/services/docker/stack-custom-service';
import { getSettings } from '$lib/services/settings-service';
import { getContainer } from '$lib/services/docker/container-service';
import { listAgents } from '$lib/services/agent/agent-manager';
@@ -37,7 +37,8 @@ export const load: PageServerLoad = async ({ params }) => {
envContent: stack.envContent || '',
originalName: stack.name,
originalComposeContent: stack.composeContent || '',
- originalEnvContent: stack.envContent || ''
+ originalEnvContent: stack.envContent || '',
+ autoUpdate: false // Will be loaded from database if available
};
const settingsResult = await tryCatch(getSettings());
diff --git a/src/routes/compose/[composeId]/+page.svelte b/src/routes/compose/[composeId]/+page.svelte
index ec17d20d0..59b7ff8a6 100644
--- a/src/routes/compose/[composeId]/+page.svelte
+++ b/src/routes/compose/[composeId]/+page.svelte
@@ -434,7 +434,7 @@
<div class="mb-4 rounded-full bg-muted/50 flex items-center justify-center mx-auto size-16">
<Layers class="size-6 text-muted-foreground" />
</div>
- <div class="text-muted-foreground">No services defined in this stack</div>
+ <div class="text-muted-foreground">No services found for this stack</div>
</div>
{/if}
</Card.Content>
diff --git a/src/routes/settings/general/+page.svelte b/src/routes/settings/general/+page.svelte
index 6bd485d48..00550ab70 100644
--- a/src/routes/settings/general/+page.svelte
+++ b/src/routes/settings/general/+page.svelte
@@ -43,15 +43,23 @@
saving: false
});
- // Synchronize values when settings change
+ // Synchronize values when settings change (but only if not touched by user)
$effect(() => {
// Get the latest values from the store
const settings = $settingsStore;
- // Update form inputs with values from the store
- stacksDirectoryInput.value = settings.stacksDirectory || 'data/stacks';
- baseServerUrlInput.value = settings.baseServerUrl || 'localhost';
- maturityThresholdInput.value = settings.maturityThresholdDays || 30;
+ // Only update form inputs if they haven't been touched by the user
+ if (!stacksDirectoryInput.touched) {
+ stacksDirectoryInput.value = settings.stacksDirectory || 'data/stacks';
+ }
+
+ if (!baseServerUrlInput.touched) {
+ baseServerUrlInput.value = settings.baseServerUrl || 'localhost';
+ }
+
+ if (!maturityThresholdInput.touched) {
+ maturityThresholdInput.value = settings.maturityThresholdDays || 30;
+ }
});
// Initialize settings from page data once
@@ -62,6 +70,20 @@
}
});
+ // Initialize form inputs from page data once
+ $effect(() => {
+ if (data.settings && !initialized) {
+ updateSettingsStore(data.settings);
+
+ // Initialize form inputs with values from data
+ stacksDirectoryInput.value = data.settings.stacksDirectory || 'data/stacks';
+ baseServerUrlInput.value = data.settings.baseServerUrl || 'localhost';
+ maturityThresholdInput.value = data.settings.maturityThresholdDays || 30;
+
+ initialized = true;
+ }
+ });
+
// No automatic syncing - just event handlers for changes
function onStacksDirectoryChange() {
stacksDirectoryInput.touched = true;
diff --git a/src/routes/volumes/+page.svelte b/src/routes/volumes/+page.svelte
index a3dd5e154..55af67ebb 100644
--- a/src/routes/volumes/+page.svelte
+++ b/src/routes/volumes/+page.svelte
@@ -242,6 +242,9 @@
onPageSizeChange={(newSize) => {
tablePersistence.setPageSize('volumes', newSize);
}}
+ sort={{
+ defaultSort: { id: 'Name', desc: false }
+ }}
display={{
filterPlaceholder: 'Search volumes...',
noResultsMessage: 'No volumes found matching your filters.'