Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
fix: importing stacks if files are in the data/stacks directory (#161)
Kyle Mendell9bfb4795ca57
diff --git a/docker-compose.yml b/docker-compose.ymlindex 8c4f9fc20..6d0f08e58 100644--- a/docker-compose.yml+++ b/docker-compose.yml@@ -10,6 +10,9 @@ services:# Mount a volume for Arcane's persistent data (stacks, settings, etc.).# Replace './arcane-data' with your preferred host path.- ./arcane-data:/app/data+ # This is where you existing stack files are located if you want to import them, when arcane tries+ # to import it it will need to read from the path the compose file is at, hence why onl read only is needed.+ - /opt/my-docker-stacks:/opt/my-docker-stacks:ro # Host path : Container pathenvironment:- APP_ENV=production # Sets the application environment to production.diff --git a/docs/docs/getting-started/configuration.md b/docs/docs/getting-started/configuration.mdindex 5c9aa8ae6..699bcffdf 100644--- a/docs/docs/getting-started/configuration.md+++ b/docs/docs/getting-started/configuration.md@@ -71,6 +71,34 @@ services:When you do this, any settings you change in the Arcane web UI will be saved in that `arcane-data` folder and will still be there even if you restart Arcane.+#### Accessing Other Host Directories (e.g., for Importing Stacks)++Arcane, running inside its container, can't see your computer's files directly unless you tell Docker to share specific folders. If you want to use features like "Import Stacks" to bring in `docker-compose.yml` files from a directory on your computer (that's not already part of the main `arcane-data` volume), you need to:++1. **Mount the Host Directory as a Volume:**+ Add another line to the `volumes` section in your Arcane `docker-compose.yml`. This maps the directory on your computer (where your stacks are) to a path _inside_ the Arcane container.++ For example, if your existing stacks are in `/home/user/my-docker-projects` on your host server, you need to map this to the same folder inside the Arcane container like this:++ ```yaml+ # In your docker-compose.yml file+ services:+ arcane:+ # ... other settings ...+ volumes:+ - /var/run/docker.sock:/var/run/docker.sock+ - arcane-data:/app/data+ - /home/user/my-docker-projects:/home/user/my-docker-projects:ro # Host path : Container path+ # ... other settings ...+ ```++ - **Tip:** Using `:ro` (read-only) for the volume is a good idea if Arcane only needs to read these files (like for importing) and not change them. This helps prevent accidental modifications to your original files.++2. **Use the Container Path in Arcane:**+ When Arcane's "Import Stacks" feature asks for a directory path to scan, you must provide the path _as seen from inside the container_ (e.g., `/home/user/my-docker-projects` from the example above). Arcane will then look for your `docker-compose.yml` files there.++ Once imported, Arcane typically copies or manages these stack configurations within its own data directory (e.g., in a subfolder of `/app/data/stacks`), so the original files in your mounted import directory are usually just read during the import process.+### 2. Special Instructions for Arcane (Environment Variables)Think of these like sticky notes you give to Arcane when it starts up. Some are really important for it to work correctly in Docker.diff --git a/docs/docs/getting-started/quickstart.md b/docs/docs/getting-started/quickstart.mdindex 20a50363a..ad9dfd19b 100644--- a/docs/docs/getting-started/quickstart.md+++ b/docs/docs/getting-started/quickstart.md@@ -46,7 +46,26 @@ This guide provides the fastest way to get Arcane up and running using Docker CoBefore starting, review the `docker-compose.yml` file:- **Docker Socket:** Mounts `/var/run/docker.sock` to allow Arcane to manage Docker.- - **Data Persistence:** Uses a volume named `arcane-data` (or a host path like `./arcane-data`) mapped to `/app/data` inside the container. This stores Arcane's settings, stacks, users, sessions, and encryption keys.+ - **Data Persistence:** Uses a volume named `arcane-data` (or a host path like `./arcane-data`) mapped to `/app/data` inside the container. This stores Arcane's settings, stacks, users, sessions, and encryption keys. By default, Arcane-managed stack files are stored within this volume (e.g., at `/app/data/stacks` inside the container).++ - **Importing Existing Stacks from a Host Directory:**+ If you have existing `docker-compose.yml` files on your host machine that you want to import into Arcane, you need to make them accessible to the Arcane container:++ 1. **Mount the Host Directory:** Add an additional volume mount to your Arcane `docker-compose.yml` file. This maps the directory on your host (where your stacks are) to a path inside the Arcane container.+ For example, if your stacks are in `/opt/my-docker-stacks` on your host, you need to map them to `/opt/my-docker-stacks` inside the container:+ ```yaml+ services:+ arcane:+ # ... other settings ...+ volumes:+ - /var/run/docker.sock:/var/run/docker.sock+ - arcane-data:/app/data+ - /opt/my-docker-stacks:/opt/my-docker-stacks:ro # Host path : Container path+ # ...+ ```+ _Note: Using `:ro` (read-only) for the import volume is a good practice if Arcane only needs to read these files for import._+ 2. **Provide Container Path to Arcane:** When using Arcane's "Import Stacks" feature, you will specify the path _inside the container_ where Arcane can find these files (e.g., `/opt/my-docker-stacks` from the example above). Arcane will then scan this directory for `docker-compose.yml` files.+ 3. Once imported, Arcane will typically copy or manage these stack configurations within its own data directory (e.g., `/app/data/stacks`), depending on its import logic. The original files in your mounted import directory are usually just read during the import process.- **Permissions & Environment Variables (Important):**diff --git a/docs/docs/guides/auto-update.md b/docs/docs/guides/auto-update.mdnew file mode 100644index 000000000..a952ade57--- /dev/null+++ b/docs/docs/guides/auto-update.md@@ -0,0 +1,57 @@+---+sidebar_position: 2+title: Auto Updates+---++Arcane can automatically monitor your running stacks and redeploy them when new versions of their Docker images become available. This feature helps keep your services up-to-date with minimal manual intervention.++## Global Configuration++First, ensure that the global auto-update feature is enabled in Arcane's settings. You can typically find this in the application's main settings or configuration panel. If this global setting is disabled, no stacks will be auto-updated, regardless of their individual configurations.++## Stack-Specific Configuration via Docker Compose Labels++To enable auto-updates for a specific stack, you need to add a special label to at least one of its services within its `docker-compose.yml` or `compose.yaml` file.++The label to use is: `arcane.stack.auto-update: "true"`++**How it works:**++- If Arcane's global auto-update setting is enabled, Arcane will periodically check all running or partially running stacks.+- For each stack, it will inspect its `compose.yaml` (or `docker-compose.yml`) file.+- If **any service** within that compose file contains the label `arcane.stack.auto-update: "true"`, the entire stack is considered eligible for auto-updates.+- Arcane will then proceed to check the images defined in that stack for newer versions (respecting any version tags you've specified).+- If an update is found for one or more images that are configured for auto-updates (e.g., using Watchtower labels or if the image itself is eligible), the entire stack will be pulled and redeployed.++**Example:**++Here's how you would enable auto-updates for a stack named `my-web-app` that has a `web` service:++```yaml+# In your STACKS_DIR/my-web-app/compose.yaml++version: '3.8'++services:+ web:+ image: nginx:latest # This image will be monitored for updates+ ports:+ - '8080:80'+ labels:+ - 'arcane.stack.auto-update=true' # Enables auto-update for the 'my-web-app' stack+ # You can also add Watchtower-specific labels here if needed for fine-grained control+ # - "com.centurylinklabs.watchtower.enable=true" # Example if using Watchtower conventions++ # other_service:+ # image: redis:alpine+ # # This service does not need the label if 'web' already has it for the stack to be eligible.+ # # However, its image will also be checked for updates if the stack is redeployed.+```++**Important Notes:**++- **Only one service needs the label:** You only need to add the `arcane.stack.auto-update: "true"` label to one service in the stack's compose file to make the entire stack eligible.+- **Global setting priority:** The global auto-update setting in Arcane must be enabled for this label to have any effect.+- **Image versioning:** Be mindful of the image tags you use (e.g., `latest`, specific versions like `1.2.3`, or rolling tags like `stable`). Auto-updates will pull based on the tag specified. Using `latest` means you'll always get the newest image marked as `latest`.+- **Redeployment:** When an update is triggered, Arcane will typically perform a full stack redeployment (similar to `docker-compose down` followed by `docker-compose up -d --pull`). Ensure your services are designed to handle graceful shutdowns and startups.+- **Checking Interval:** The frequency of checks is determined by Arcane's auto-update service configuration (e.g., a cron job or timed interval).diff --git a/src/lib/services/api/stack-api-service.ts b/src/lib/services/api/stack-api-service.tsindex c28b654a1..684894c87 100644--- a/src/lib/services/api/stack-api-service.ts+++ b/src/lib/services/api/stack-api-service.ts@@ -39,11 +39,10 @@ export default class StackAPIService extends BaseAPIService {return res.data;}- async save(id: string, name: string, content: string, autoUpdate: boolean, envContent?: string) {+ async save(id: string, name: string, content: string, envContent?: string) {const res = await this.api.put(`/stacks/${id}`, {name,composeContent: content,- autoUpdate,envContent});return res.data;diff --git a/src/lib/services/docker/auto-update-service.ts b/src/lib/services/docker/auto-update-service.tsindex 4fdbcc3d9..d005cbb14 100644--- a/src/lib/services/docker/auto-update-service.ts+++ b/src/lib/services/docker/auto-update-service.ts@@ -2,6 +2,7 @@ import { listContainers, getContainer, recreateContainer } from './container-serimport { listStacks, getStack, fullyRedeployStack } from './stack-service';import { pullImage, getImage, listImages } from './image-service';import { getSettings } from '../settings-service';+import yaml from 'js-yaml'; // Add js-yaml importimport type { ServiceContainer } from '$lib/types/docker';import type { Stack } from '$lib/types/docker/stack.type';@@ -111,45 +112,98 @@ export async function checkAndUpdateStacks(): Promise<{return { checked: 0, updated: 0, errors: [] };}- const stacks = await listStacks();- const eligibleStacks = stacks.filter(- (s) =>- // Only consider running stacks with auto-update enabled- (s.status === 'running' || s.status === 'partially running') && s.meta && s.meta.autoUpdate === true- );+ const allListedStacks = await listStacks();+ const eligibleStacksForProcessing: Stack[] = [];++ for (const listedStack of allListedStacks) {+ // Only consider running or partially running stacks+ if (listedStack.status !== 'running' && listedStack.status !== 'partially running') {+ continue;+ }++ try {+ // Fetch full stack details to get composeContent for label checking+ const fullStack = await getStack(listedStack.id);+ if (!fullStack.composeContent) {+ console.warn(`Auto-update: Stack ${listedStack.id} has no compose content, skipping eligibility check.`);+ continue;+ }++ const composeData = yaml.load(fullStack.composeContent) as any;+ let stackIsEligibleByLabel = false;++ if (composeData && composeData.services) {+ for (const serviceName in composeData.services) {+ const service = composeData.services[serviceName];+ if (service.labels) {+ let labelValue: string | undefined = undefined;+ // Docker Compose labels can be an array of "key=value" strings or a map/object+ if (Array.isArray(service.labels)) {+ const foundLabel = service.labels.find((l: string) => l.startsWith('arcane.stack.auto-update='));+ if (foundLabel) {+ labelValue = foundLabel.split('=')[1];+ }+ } else if (typeof service.labels === 'object' && service.labels !== null) {+ labelValue = service.labels['arcane.stack.auto-update'];+ }++ if (labelValue === 'true') {+ stackIsEligibleByLabel = true;+ break; // Found one service with the label, stack is eligible+ }+ }+ }+ }++ if (stackIsEligibleByLabel) {+ // Use the listedStack object as it's from the initial list.+ // fullStack could also be used if preferred, ensure types are compatible.+ eligibleStacksForProcessing.push(listedStack);+ }+ } catch (error) {+ console.error(`Auto-update: Error checking eligibility for stack ${listedStack.id}:`, error);+ }+ }const results = {- checked: eligibleStacks.length,+ checked: eligibleStacksForProcessing.length, // Number of stacks found eligible and processedupdated: 0,errors: [] as Array<{ id: string; error: string }>};// Process eligible stacks- for (const stack of eligibleStacks) {+ for (const stackToUpdate of eligibleStacksForProcessing) {try {// Skip if already being updated- if (updatingStacks.has(stack.id)) continue;+ if (updatingStacks.has(stackToUpdate.id)) {+ console.log(`Auto-update: Skipping stack ${stackToUpdate.name} (${stackToUpdate.id}), already in progress.`);+ continue;+ }- const updateAvailable = await checkStackImagesUpdate(stack);+ // checkStackImagesUpdate will call getStack internally again to ensure it has the latest+ // composeContent before checking images. This is acceptable.+ const updateAvailable = await checkStackImagesUpdate(stackToUpdate);if (updateAvailable) {- updatingStacks.add(stack.id);+ updatingStacks.add(stackToUpdate.id);- console.log(`Auto-update: Redeploying stack ${stack.name} (${stack.id})`);- await fullyRedeployStack(stack.id);+ console.log(`Auto-update: Redeploying stack ${stackToUpdate.name} (${stackToUpdate.id})`);+ await fullyRedeployStack(stackToUpdate.id);- console.log(`Auto-update: Stack ${stack.name} redeployed successfully`);+ console.log(`Auto-update: Stack ${stackToUpdate.name} redeployed successfully`);results.updated++;- updatingStacks.delete(stack.id);+ updatingStacks.delete(stackToUpdate.id);+ } else {+ console.log(`Auto-update: Stack ${stackToUpdate.name} (${stackToUpdate.id}) is up-to-date or no images triggered an update.`);}} catch (error: unknown) {- console.error(`Auto-update error for stack ${stack.id}:`, error);+ console.error(`Auto-update error for stack ${stackToUpdate.id}:`, error);const msg = error instanceof Error ? error.message : String(error);results.errors.push({- id: stack.id,+ id: stackToUpdate.id,error: msg});- updatingStacks.delete(stack.id);+ updatingStacks.delete(stackToUpdate.id); // Ensure lock is released on error}}diff --git a/src/lib/services/docker/stack-service.ts b/src/lib/services/docker/stack-service.tsindex babc8ef08..0e5f05375 100644--- a/src/lib/services/docker/stack-service.ts+++ b/src/lib/services/docker/stack-service.ts@@ -94,9 +94,10 @@ async function getStackDir(stackId: string): Promise<string> {}/**- * Returns the path to the compose file, prioritizing compose.yaml, fallback to docker-compose.yml+ * Returns the path to the compose file, prioritizing compose.yaml, fallback to docker-compose.yml.+ * Returns null if neither is found.*/-async function getComposeFilePath(stackId: string): Promise<string> {+async function getComposeFilePath(stackId: string): Promise<string | null> {const stackDirAbs = await getStackDir(stackId); // Will be absoluteconst newPath = path.join(stackDirAbs, 'compose.yaml');const oldPath = path.join(stackDirAbs, 'docker-compose.yml');@@ -108,18 +109,18 @@ async function getComposeFilePath(stackId: string): Promise<string> {try {await fs.access(oldPath);return oldPath;- } catch (fallbackError) {- // Neither compose.yaml nor docker-compose.yml found at the absolute path- console.error(`Neither compose.yaml nor docker-compose.yml found in ${stackDirAbs}`);- throw fallbackError; // Re-throw the error from accessing oldPath+ } catch {+ // Neither compose.yaml nor docker-compose.yml found+ return null;}}}/**- * Returns the path to the meta file, prioritizing .stack.json, fallback to meta.json+ * Returns the path to the meta file, prioritizing .stack.json, fallback to meta.json.+ * Returns null if neither is found.*/-async function getStackMetaPath(stackId: string): Promise<string> {+async function getStackMetaPath(stackId: string): Promise<string | null> {const stackDir = await getStackDir(stackId);const newPath = join(stackDir, '.stack.json');const oldPath = join(stackDir, 'meta.json');@@ -127,8 +128,14 @@ async function getStackMetaPath(stackId: string): Promise<string> {await fs.access(newPath);return newPath;} catch {- await fs.access(oldPath);- return oldPath;+ // .stack.json not accessible, try meta.json+ try {+ await fs.access(oldPath);+ return oldPath;+ } catch {+ // meta.json also not accessible+ return null;+ }}}@@ -189,6 +196,9 @@ async function loadEnvFile(stackId: string): Promise<string> {async function getComposeInstance(stackId: string): Promise<DockerodeCompose> {const docker = getDockerClient();const composePath = await getComposeFilePath(stackId);+ if (!composePath) {+ throw new Error(`Compose file not found for stack ${stackId}`);+ }return new DockerodeCompose(docker, composePath, stackId);}@@ -240,10 +250,6 @@ async function getStackServices(stackId: string, composeContent: string): Promisreturn nameStartsWithPrefix || hasCorrectLabel;});- if (stackContainers.length === 0) {- console.log(`No running or stopped containers found for stack ${stackId} based on name prefix or label.`);- }-const services: StackService[] = [];for (const containerData of stackContainers) {@@ -338,76 +344,76 @@ export async function loadComposeStacks(): Promise<Stack[]> {const stacksDir = await ensureStacksDir();try {- const stackDirs = await fs.readdir(stacksDir);+ const stackDirEntries = await fs.readdir(stacksDir, { withFileTypes: true });const stacks: Stack[] = [];- for (const dir of stackDirs) {- const stackDir = path.join(stacksDir, dir);-- // Skip if not a directory (e.g., .DS_Store or other files)- let stat;- try {- stat = await fs.stat(stackDir);- } catch {+ for (const entry of stackDirEntries) {+ if (!entry.isDirectory()) {continue;}- if (!stat.isDirectory()) continue;+ const dirName = entry.name; // This is the stack's ID and name+ const stackDir = path.join(stacksDir, dirName);- try {- const newMetaPath = path.join(stackDir, '.stack.json');- const newComposePath = path.join(stackDir, 'compose.yaml');+ let composeFilePath: string | null = null;+ let composeContent: string | null = null;- let metaContent: string;- let composeContent: string;- let meta: StackMeta;+ const potentialComposePaths = [path.join(stackDir, 'compose.yaml'), path.join(stackDir, 'docker-compose.yml')];+ for (const p of potentialComposePaths) {try {- [metaContent, composeContent] = await Promise.all([fs.readFile(newMetaPath, 'utf8'), fs.readFile(newComposePath, 'utf8')]);- meta = JSON.parse(metaContent) as StackMeta;- } catch (newWayErr) {- // Fallback to old way- const oldMetaPath = await getStackMetaPath(dir);- const oldComposePath = await getComposeFilePath(dir);-- [metaContent, composeContent] = await Promise.all([fs.readFile(oldMetaPath, 'utf8'), fs.readFile(oldComposePath, 'utf8')]);- meta = JSON.parse(metaContent) as StackMeta;+ await fs.access(p);+ composeContent = await fs.readFile(p, 'utf8');+ composeFilePath = p;+ break;+ } catch {+ // File not accessible or doesn't exist, try next}+ }- const services = await getStackServices(dir, composeContent);+ if (!composeContent || !composeFilePath) {+ console.warn(`No compose file found in directory ${dirName}, skipping.`);+ continue;+ }- const serviceCount = services.length;- const runningCount = services.filter((s) => s.state?.Running).length;+ // 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 (runningCount === serviceCount && serviceCount > 0) {- status = 'running';- } else if (runningCount > 0) {- status = 'partially running';- }+ 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';+ }- const isLegacy = !(await fs.access(path.join(stackDir, '.stack.json')).then(- () => true,- () => false- ));-- stacks.push({- id: dir,- name: meta.name,- serviceCount,- runningCount,- status,- createdAt: meta.createdAt,- updatedAt: meta.updatedAt,- isLegacy- });- } catch (err) {- console.warn(`Error loading stack ${dir}:`, err);+ 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+ });}return stacks;} catch (err) {- console.error('Error loading stacks:', err);+ console.error('Error loading stacks from STACKS_DIR:', err);throw new Error('Failed to load compose stacks');}}@@ -416,9 +422,6 @@ export async function loadComposeStacks(): Promise<Stack[]> {* Creates a new stack with a compose file and optional .env file*/export async function createStack(name: string, composeContent: string, envContent?: string): Promise<Stack> {- // Generate a unique ID for references (still needed for APIs)- const id = nanoid();-// Create a safe directory name from the stack nameconst dirName = slugify(name, {lower: true, // Convert to lowercase@@ -427,7 +430,6 @@ export async function createStack(name: string, composeContent: string, envContetrim: true // Trim leading/trailing spaces});- // Ensure directory name is unique - add suffix if neededconst stacksDir = await ensureStacksDirectory();let counter = 1;let uniqueDirName = dirName;@@ -437,29 +439,16 @@ export async function createStack(name: string, composeContent: string, envContecounter++;}- // Create stack directory with the name-based folderconst stackDir = join(stacksDir, uniqueDirName);await fs.mkdir(stackDir, { recursive: true });- // Save compose fileawait fs.writeFile(join(stackDir, 'compose.yaml'), composeContent);- // Save env file if providedif (envContent) {await fs.writeFile(join(stackDir, '.env'), envContent);}- // Create stack metadata file with the ID reference- const meta = {- id,- name,- dirName: uniqueDirName,- path: stackDir,- createdAt: new Date().toISOString(),- updatedAt: new Date().toISOString()- };-- await fs.writeFile(join(stackDir, '.stack.json'), JSON.stringify(meta, null, 2));+ // No .stack.json is created.let serviceCount = 0;try {@@ -468,20 +457,22 @@ export async function createStack(name: string, composeContent: string, envConteserviceCount = Object.keys(composeData.services).length;}} catch (parseErr) {- console.warn(`Could not parse compose file during creation for stack ${meta.name}:`, parseErr);+ console.warn(`Could not parse compose file during creation for stack ${uniqueDirName}:`, parseErr);}+ const dirStat = await fs.stat(stackDir);+return {- id: meta.id,- name: meta.name,+ id: uniqueDirName, // ID is the directory name+ name: uniqueDirName, // Name is also the directory nameserviceCount: serviceCount,- runningCount: 0,+ runningCount: 0, // New stacks are initially stoppedstatus: 'stopped',- createdAt: meta.createdAt,- updatedAt: meta.updatedAt,+ createdAt: dirStat.birthtime.toISOString(),+ updatedAt: dirStat.mtime.toISOString(),composeContent: composeContent,envContent: envContent || '',- meta+ isExternal: false};}@@ -489,118 +480,117 @@ export async function createStack(name: string, composeContent: string, envConte* Gets information about a specific stack including its .env file*/export async function getStack(stackId: string): Promise<Stack> {- try {- // Try the new way first (.stack.json and compose.yaml)- const stackDir = await getStackDir(stackId);- const newMetaPath = path.join(stackDir, '.stack.json');- const newComposePath = path.join(stackDir, 'compose.yaml');+ const stackDir = await getStackDir(stackId); // stackId is dirName- let metaContent: string;- let composeContent: string;- let envContent: string;- let meta: StackMeta;-- try {- [metaContent, composeContent, envContent] = await Promise.all([fs.readFile(newMetaPath, 'utf8'), fs.readFile(newComposePath, 'utf8'), loadEnvFile(stackId)]);- meta = JSON.parse(metaContent) as StackMeta;- } catch (newWayErr) {- // If new way fails, fall back to old way (meta.json and docker-compose.yml)- const oldMetaPath = await getStackMetaPath(stackId);- const oldComposePath = await getComposeFilePath(stackId);-- [metaContent, composeContent, envContent] = await Promise.all([fs.readFile(oldMetaPath, 'utf8'), fs.readFile(oldComposePath, 'utf8'), loadEnvFile(stackId)]);- meta = JSON.parse(metaContent) as StackMeta;+ 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);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 (runningCount === serviceCount && serviceCount > 0) {+ 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,- name: meta.name,+ id: stackId, // Directory name+ name: stackId, // Directory nameservices,serviceCount,runningCount,status,- createdAt: meta.createdAt,- updatedAt: meta.updatedAt,+ createdAt: dirStat.birthtime.toISOString(),+ updatedAt: dirStat.mtime.toISOString(), // Or could be composePath mtimecomposeContent,envContent,- meta+ isExternal: false};} catch (err) {- console.error(`Error getting stack ${stackId}:`, err);- throw new Error(`Stack not found or cannot be accessed`);+ 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(stackId: string, updates: StackUpdate): Promise<Stack> {- const metaPath = await getStackMetaPath(stackId);- const composePath = await getComposeFilePath(stackId);-- try {- const metaContent = await fs.readFile(metaPath, 'utf8');- const meta = JSON.parse(metaContent) as StackMeta;-- const updatedMeta: StackMeta = {- ...meta,- name: updates.name || meta.name,- updatedAt: new Date().toISOString(),- ...(updates.autoUpdate !== undefined ? { autoUpdate: updates.autoUpdate } : {})- };-- const promises = [fs.writeFile(metaPath, JSON.stringify(updatedMeta, null, 2), 'utf8')];+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 (updates.composeContent) {- promises.push(fs.writeFile(composePath, updates.composeContent, 'utf8'));+ 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.`);}+ }- if (updates.envContent !== undefined) {- promises.push(saveEnvFile(stackId, updates.envContent));- }+ // 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);- await Promise.all(promises);+ const promises = [];- const composeContent = updates.composeContent || (await fs.readFile(composePath, 'utf8'));- const envContent = updates.envContent !== undefined ? updates.envContent : await loadEnvFile(stackId);+ if (updates.composeContent !== undefined) {+ let currentComposePath = await getComposeFilePath(effectiveStackId); // Check existing, might be null+ const targetComposePath = currentComposePath || path.join(stackDirForContent, 'compose.yaml'); // Default to compose.yaml if not found- const services = await getStackServices(stackId, composeContent);+ promises.push(fs.writeFile(targetComposePath, updates.composeContent, 'utf8'));+ contentUpdated = true;+ console.log(`Updating composeContent for stack '${effectiveStackId}'.`);+ }- const serviceCount = services.length;- const runningCount = services.filter((s) => s.state?.Running).length;+ if (updates.envContent !== undefined) {+ promises.push(saveEnvFile(effectiveStackId, updates.envContent));+ contentUpdated = true;+ console.log(`Updating envContent for stack '${effectiveStackId}'.`);+ }- let status: Stack['status'] = 'stopped';- if (runningCount === serviceCount && serviceCount > 0) {- status = 'running';- } else if (runningCount > 0) {- status = 'partially running';- }+ if (promises.length > 0) {+ await Promise.all(promises);+ }- return {- id: stackId,- name: updatedMeta.name,- serviceCount,- runningCount,- status,- createdAt: updatedMeta.createdAt,- updatedAt: updatedMeta.updatedAt,- composeContent,- envContent- };- } catch (err) {- console.error(`Error updating stack ${stackId}:`, err);- throw new Error('Failed to update stack');+ // 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);}}@@ -881,6 +871,83 @@ export async function removeStack(stackId: string): Promise<boolean> {}}+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}'.`);++ // 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.@@ -987,94 +1054,141 @@ export async function importExternalStack(stackId: string): Promise<Stack> {}// 2. Try to locate the compose file (if available)- let composeFilePath = '';- const container = stackContainers[0];+ const container = stackContainers[0]; // Use the first container to get labelsconst labels = container.Labels || {};- if (labels['com.docker.compose.project.config_files']) {- composeFilePath = labels['com.docker.compose.project.config_files'];- }-// 3. Read the compose file if available, or create a new onelet 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 (composeFilePath) {- try {- composeContent = await fs.readFile(composeFilePath, 'utf8');- } catch (err) {- console.warn(`Couldn't read compose file at ${composeFilePath}:`, err);- // Will generate a new one below+ // 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 inspectionif (!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 inspectionconst services: Record<string, any> = {};- for (const container of stackContainers) {- const containerLabels = container.Labels || {};- const serviceName = containerLabels['com.docker.compose.service'] || container.Names[0]?.replace(`/${stackId}_`, '').replace('_1', '') || `service_${container.Id.substring(0, 8)}`;+ 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(container.Id).inspect();+ // const containerDetails = await docker.getContainer(cont.Id).inspect(); // Uncomment if more details are neededservices[serviceName] = {- image: container.Image- // Add other properties based on containerDetails+ 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 contentcomposeContent = `# Generated compose file for imported stack: ${stackId}-# This was automatically generated by Arcane from an external stack-# You may need to adjust it manually for correct operation+# 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.-version: '3'services:-${yaml.dump({ services }).substring(10)}`; // Remove the services: line+${yaml.dump({ 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.}// 5. Create a new stack in Arcane's managed stacks- return await createStack(stackId, composeContent);+ return await createStack(stackId, composeContent, envContent); // Pass envContent here}/**- * Lists all managed and optionally external stacks+ * 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+ // 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();- // Add meta information to managed stacks- const enrichedManagedStacks = await Promise.all(- managedStacks.map(async (stack) => {- try {- // Read the meta file to get autoUpdate property- const metaPath = await getStackMetaPath(stack.id);- const metaContent = await fs.readFile(metaPath, 'utf8');- const meta = JSON.parse(metaContent) as StackMeta;-- // Return stack with meta included- return {- ...stack,- meta- };- } catch (err) {- console.warn(`Failed to read meta for stack ${stack.id}:`, err);- return stack; // Return stack without meta if there was an error- }- })- );+ let allStacks: Stack[] = [...managedStacks];- // Get external stacks if requested- let externalStacks: Stack[] = [];if (includeExternal) {- externalStacks = await discoverExternalStacks();+ 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];}- // Combine managed and external stacks- return [...enrichedManagedStacks, ...externalStacks];+ return allStacks;}/**diff --git a/src/lib/types/docker/stack.type.ts b/src/lib/types/docker/stack.type.tsindex 2022c4377..ff0afb4b8 100644--- a/src/lib/types/docker/stack.type.ts+++ b/src/lib/types/docker/stack.type.ts@@ -25,13 +25,12 @@ export interface Stack {services?: StackService[];serviceCount?: number;runningCount?: number;- status: 'running' | 'stopped' | 'partially running';+ status: 'running' | 'stopped' | 'partially running' | 'unknown';isExternal?: boolean;createdAt?: string;updatedAt?: string;composeContent?: string;envContent?: string;- meta?: StackMeta;isLegacy?: boolean;}diff --git a/src/lib/utils/api.util.ts b/src/lib/utils/api.util.tsindex 0f1d44326..aeb074378 100644--- a/src/lib/utils/api.util.ts+++ b/src/lib/utils/api.util.ts@@ -1,18 +1,18 @@-import type { Result } from './try-catch';+import type { Result } from './try-catch'; // Assuming Result<T, Error> is { data?: T, error?: Error }import { toast } from 'svelte-sonner';import { extractDockerErrorMessage } from '$lib/utils/errors.util';-export function handleApiResultWithCallbacks<T>({ result, message, setLoadingState = () => {}, onSuccess, onError = () => {} }: { result: Result<T, Error>; message: string; setLoadingState?: (value: boolean) => void; onSuccess: (result: Result<T, Error>) => void; onError?: (result: Result<T, Error>) => void }) {+export function handleApiResultWithCallbacks<T>({ result, message, setLoadingState = () => {}, onSuccess, onError = () => {} }: { result: Result<T, Error>; message: string; setLoadingState?: (value: boolean) => void; onSuccess: (data: T) => void; onError?: (error: Error) => void }) {setLoadingState(true);+if (result.error) {const dockerMsg = extractDockerErrorMessage(result.error);- console.error(`onErrorCallback: ${message}:`, result.error);+ console.error(`API Error: ${message}:`, result.error);toast.error(`${message}: ${dockerMsg}`);+ onError(result.error);setLoadingState(false);- onError(result);- return;- } else if (result.data) {- onSuccess(result);+ } else {+ onSuccess(result.data as T);setLoadingState(false);}}diff --git a/src/routes/api/stacks/[stackId]/+server.ts b/src/routes/api/stacks/[stackId]/+server.tsindex 155199ad6..13c50d2b9 100644--- a/src/routes/api/stacks/[stackId]/+server.ts+++ b/src/routes/api/stacks/[stackId]/+server.ts@@ -6,9 +6,9 @@ import { tryCatch } from '$lib/utils/try-catch';export const PUT: RequestHandler = async ({ params, request }) => {const { stackId } = params;- const { name, composeContent, autoUpdate, envContent } = await request.json();+ const { name, composeContent, envContent } = await request.json();- const result = await tryCatch(updateStack(stackId, { name, composeContent, autoUpdate, envContent }));+ const result = await tryCatch(updateStack(stackId, { name, composeContent, envContent }));if (result.error) {console.error('Error updating stack:', result.error);diff --git a/src/routes/stacks/[stackId]/+page.server.ts b/src/routes/stacks/[stackId]/+page.server.tsindex 649a328f7..41a65f328 100644--- a/src/routes/stacks/[stackId]/+page.server.ts+++ b/src/routes/stacks/[stackId]/+page.server.ts@@ -31,7 +31,6 @@ export const load: PageServerLoad = async ({ params }) => {name: stack.name,composeContent: stack.composeContent || '',envContent: stack.envContent || '',- autoUpdate: stack.meta?.autoUpdate || false,originalName: stack.name,originalComposeContent: stack.composeContent || '',originalEnvContent: stack.envContent || ''diff --git a/src/routes/stacks/[stackId]/+page.svelte b/src/routes/stacks/[stackId]/+page.svelteindex f9f868a84..fc603a4c6 100644--- a/src/routes/stacks/[stackId]/+page.svelte+++ b/src/routes/stacks/[stackId]/+page.svelte@@ -11,11 +11,10 @@import StatusBadge from '$lib/components/badges/status-badge.svelte';import { statusVariantMap } from '$lib/types/statuses';import { capitalizeFirstLetter } from '$lib/utils/string.utils';- import { invalidateAll } from '$app/navigation';+ import { invalidateAll, goto } from '$app/navigation';import { toast } from 'svelte-sonner';import YamlEditor from '$lib/components/yaml-editor.svelte';import EnvEditor from '$lib/components/env-editor.svelte';- import { Switch } from '$lib/components/ui/switch/index.js';import { tryCatch } from '$lib/utils/try-catch';import StackAPIService from '$lib/services/api/stack-api-service';import { handleApiResultWithCallbacks } from '$lib/utils/api.util';@@ -40,13 +39,11 @@let name = $derived(editorState.name);let composeContent = $derived(editorState.composeContent);let envContent = $derived(editorState.envContent || '');- let autoUpdate = $derived(editorState.autoUpdate);let originalName = $derived(editorState.originalName);let originalComposeContent = $derived(editorState.originalComposeContent);let originalEnvContent = $derived(editorState.originalEnvContent || '');- let originalAutoUpdate = $derived(editorState.autoUpdate);- let hasChanges = $derived(name !== originalName || composeContent !== originalComposeContent || envContent !== originalEnvContent || autoUpdate !== originalAutoUpdate);+ let hasChanges = $derived(name !== originalName || composeContent !== originalComposeContent || envContent !== originalEnvContent);const baseServerUrl = $derived(settings?.baseServerUrl || 'localhost');@@ -61,20 +58,30 @@async function handleSaveChanges() {if (!stack || !hasChanges) return;+ // Store the original stack ID before saving, in case it changes+ const currentStackId = stack.id;+handleApiResultWithCallbacks({- result: await tryCatch(stackApi.save(stack.id, name, composeContent, autoUpdate, envContent)),+ result: await tryCatch(stackApi.save(currentStackId, name, composeContent, envContent)),message: 'Failed to Save Stack',setLoadingState: (value) => (isLoading.saving = value),- onSuccess: async () => {- originalName = name;+ onSuccess: async (updatedStack) => {+ console.log('Stack save successful', updatedStack);+ toast.success('Stack updated successfully!');++ // Update local state for "original" values to reset hasChanges+ originalName = updatedStack.name;originalComposeContent = composeContent;originalEnvContent = envContent;- originalAutoUpdate = autoUpdate;- console.log('Stack save successful');- toast.success('Stack updated successfully!');await new Promise((resolve) => setTimeout(resolve, 200));- await invalidateAll();++ if (updatedStack && updatedStack.id !== currentStackId) {+ console.log(`Stack ID changed from ${currentStackId} to ${updatedStack.id}. Navigating...`);+ await goto(`/stacks/${name}`, { invalidateAll: true });+ } else {+ await invalidateAll();+ }}});}@@ -243,7 +250,10 @@<div class="space-y-4"><div class="grid w-full max-w-sm items-center gap-1.5"><Label for="name">Stack Name</Label>- <Input type="text" id="name" name="name" bind:value={name} required disabled={isLoading.saving} />+ <Input type="text" id="name" name="name" bind:value={name} required disabled={isLoading.saving || stack?.status === 'running' || stack?.status === 'partially running'} />+ {#if stack?.status === 'running' || stack?.status === 'partially running'}+ <p class="text-xs text-muted-foreground">Stack name cannot be changed while the stack is running. Please stop the stack first.</p>+ {/if}</div><div class="grid grid-cols-1 md:grid-cols-3 gap-4">@@ -266,14 +276,6 @@<p class="text-xs text-muted-foreground">Define environment variables in KEY=value format. These will be saved as a .env file in the stack directory.</p></div></div>-- <div class="flex items-center space-x-2 mt-4">- <Switch id="auto-update" name="autoUpdate" bind:checked={autoUpdate} />- <Label for="auto-update" class="font-medium">Enable auto-update</Label>- <div class="inline-block">- <p class="text-xs text-muted-foreground">When enabled, Arcane will periodically check for newer versions of all images in this stack and automatically redeploy it.</p>- </div>- </div></div></Card.Content><Card.Footer class="flex justify-between">