Skip to content

Modern Docker Management, Designed for Everyone

fix: rework auto update service

Kyle Mendelld88178a2c2c9
diff --git a/docs/docs/guides/auto-update.md b/docs/docs/guides/auto-update.md
index a952ade57..7bf4506c7 100644
--- a/docs/docs/guides/auto-update.md
+++ b/docs/docs/guides/auto-update.md
@@ -38,7 +38,7 @@ services:
ports:
- '8080:80'
labels:
- - 'arcane.stack.auto-update=true' # Enables auto-update for the 'my-web-app' stack
+ 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
diff --git a/src/lib/services/docker/auto-update-service.ts b/src/lib/services/docker/auto-update-service.ts
index 8a220330e..c7f4d76d4 100644
--- a/src/lib/services/docker/auto-update-service.ts
+++ b/src/lib/services/docker/auto-update-service.ts
@@ -1,6 +1,5 @@
import { listContainers, getContainer, recreateContainer } from './container-service';
-import { listStacks, getStack } from './stack-custom-service';
-import { redeployStack } from './stack-custom-service';
+import { listStacks, getStack, redeployStack } from './stack-custom-service';
import { pullImage, getImage } from './image-service';
import { getSettings } from '../settings-service';
import yaml from 'js-yaml';
@@ -18,18 +17,23 @@ export async function checkAndUpdateContainers(): Promise<{
const settings = await getSettings();
if (!settings.autoUpdate) {
+ console.log('Auto-update is disabled globally');
return { checked: 0, updated: 0, errors: [] };
}
+ console.log('Starting container auto-update check...');
const containers = await listContainers();
const eligibleContainers: ContainerInfo[] = [];
+ // Find containers with auto-update label
for (const container of containers) {
if (container.State !== 'running') continue;
+
try {
const containerDetails = await getContainer(container.Id);
- if (containerDetails?.Config.Labels?.['arcane.auto-update'] === 'true') {
+ if (containerDetails?.Config?.Labels?.['arcane.auto-update'] === 'true') {
eligibleContainers.push(container);
+ console.log(`Container ${getContainerName(container)} is eligible for auto-update`);
}
} catch (error) {
console.error(`Error fetching container details for ${container.Id}:`, error);
@@ -42,39 +46,41 @@ export async function checkAndUpdateContainers(): Promise<{
errors: [] as Array<{ id: string; error: string }>
};
+ console.log(`Found ${eligibleContainers.length} containers eligible for auto-update`);
+
for (const container of eligibleContainers) {
const containerId = container.Id;
- const containerName = container.Names && container.Names.length > 0 ? container.Names[0].substring(1) : containerId;
+ const containerName = getContainerName(container);
+
try {
if (updatingContainers.has(containerId)) {
- console.log(`Auto-update: Skipping ${containerName} (${containerId}), already in progress.`);
+ console.log(`Skipping ${containerName}, already updating`);
continue;
}
+ updatingContainers.add(containerId);
+ console.log(`Checking for updates: ${containerName}`);
+
const updateAvailable = await checkContainerImageUpdate(container);
if (updateAvailable) {
- updatingContainers.add(containerId);
- console.log(`Auto-update: Update found for container ${containerName} (${containerId}). Recreating...`);
- console.log(`Auto-update: Pulling latest image ${container.Image} for ${containerName}...`);
+ console.log(`Update available for ${containerName}, recreating...`);
await pullImage(container.Image);
await recreateContainer(containerId);
- console.log(`Auto-update: Container ${containerName} recreated successfully`);
+ console.log(`Successfully updated ${containerName}`);
results.updated++;
- updatingContainers.delete(containerId);
} else {
- console.log(`Auto-update: Container ${containerName} (${containerId}) is up-to-date.`);
+ console.log(`${containerName} is up-to-date`);
}
} catch (error: unknown) {
- console.error(`Auto-update error for container ${containerId}:`, error);
+ console.error(`Error updating container ${containerName}:`, error);
const msg = error instanceof Error ? error.message : String(error);
- results.errors.push({
- id: containerId,
- error: msg
- });
+ results.errors.push({ id: containerId, error: msg });
+ } finally {
updatingContainers.delete(containerId);
}
}
+ console.log(`Container auto-update completed: ${results.updated}/${results.checked} updated`);
return results;
}
@@ -86,189 +92,292 @@ export async function checkAndUpdateStacks(): Promise<{
const settings = await getSettings();
if (!settings.autoUpdate) {
+ console.log('Auto-update is disabled globally');
return { checked: 0, updated: 0, errors: [] };
}
- const allListedStacks = await listStacks();
- const eligibleStacksForProcessing: Stack[] = [];
+ console.log('Starting stack auto-update check...');
+ const allStacks = await listStacks();
+ const eligibleStacks: Stack[] = [];
- for (const listedStack of allListedStacks) {
- if (listedStack.status !== 'running' && listedStack.status !== 'partially running') {
+ // Find stacks with auto-update label and that are running
+ for (const stack of allStacks) {
+ if (stack.status !== 'running' && stack.status !== 'partially running') {
+ console.log(`Skipping stack ${stack.name} - not running (status: ${stack.status})`);
continue;
}
+
try {
- 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 Record<string, unknown>;
- let stackIsEligibleByLabel = false;
- if (composeData && typeof composeData === 'object' && 'services' in composeData) {
- const services = (composeData as { services: Record<string, any> }).services;
- for (const serviceName in services) {
- const service = services[serviceName];
- if (service.labels) {
- let labelValue: string | undefined = undefined;
- 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;
- }
- }
- }
- }
- if (stackIsEligibleByLabel) {
- eligibleStacksForProcessing.push(listedStack);
+ const isEligible = await isStackEligibleForAutoUpdate(stack);
+ if (isEligible) {
+ eligibleStacks.push(stack);
+ console.log(`Stack ${stack.name} is eligible for auto-update`);
+ } else {
+ console.log(`Stack ${stack.name} is not eligible for auto-update (no label found)`);
}
} catch (error) {
- console.error(`Auto-update: Error checking eligibility for stack ${listedStack.id}:`, error);
+ console.error(`Error checking eligibility for stack ${stack.name}:`, error);
}
}
const results = {
- checked: eligibleStacksForProcessing.length,
+ checked: eligibleStacks.length,
updated: 0,
errors: [] as Array<{ id: string; error: string }>
};
- for (const stackToUpdate of eligibleStacksForProcessing) {
+ console.log(`Found ${eligibleStacks.length} stacks eligible for auto-update`);
+
+ for (const stack of eligibleStacks) {
try {
- if (updatingStacks.has(stackToUpdate.id)) {
- console.log(`Auto-update: Skipping stack ${stackToUpdate.name} (${stackToUpdate.id}), already in progress.`);
+ if (updatingStacks.has(stack.id)) {
+ console.log(`Skipping stack ${stack.name}, already updating`);
continue;
}
- const updateAvailable = await checkStackImagesUpdate(stackToUpdate);
+
+ updatingStacks.add(stack.id);
+ console.log(`Checking for updates: ${stack.name}`);
+
+ const updateAvailable = await checkStackImagesUpdate(stack);
if (updateAvailable) {
- updatingStacks.add(stackToUpdate.id);
- console.log(`Auto-update: Redeploying stack ${stackToUpdate.name} (${stackToUpdate.id})`);
- await redeployStack(stackToUpdate.id);
- console.log(`Auto-update: Stack ${stackToUpdate.name} redeployed successfully`);
+ console.log(`Updates available for stack ${stack.name}, redeploying...`);
+ await redeployStack(stack.id);
+ console.log(`Successfully redeployed stack ${stack.name}`);
results.updated++;
- updatingStacks.delete(stackToUpdate.id);
} else {
- console.log(`Auto-update: Stack ${stackToUpdate.name} (${stackToUpdate.id}) is up-to-date or no images triggered an update.`);
+ console.log(`Stack ${stack.name} is up-to-date`);
}
} catch (error: unknown) {
- console.error(`Auto-update error for stack ${stackToUpdate.id}:`, error);
+ console.error(`Error updating stack ${stack.name}:`, error);
const msg = error instanceof Error ? error.message : String(error);
- results.errors.push({
- id: stackToUpdate.id,
- error: msg
- });
- updatingStacks.delete(stackToUpdate.id);
+ results.errors.push({ id: stack.id, error: msg });
+ } finally {
+ updatingStacks.delete(stack.id);
}
}
+ console.log(`Stack auto-update completed: ${results.updated}/${results.checked} updated`);
return results;
}
+/**
+ * Check if a stack is eligible for auto-update by looking for the label
+ */
+async function isStackEligibleForAutoUpdate(stack: Stack): Promise<boolean> {
+ try {
+ const fullStack = await getStack(stack.id);
+ if (!fullStack?.composeContent) {
+ console.warn(`Stack ${stack.name} has no compose content`);
+ return false;
+ }
+
+ const composeData = yaml.load(fullStack.composeContent) as any;
+ if (!composeData?.services) {
+ console.warn(`Stack ${stack.name} has no services in compose file`);
+ return false;
+ }
+
+ // Check each service for the auto-update label
+ for (const [serviceName, service] of Object.entries(composeData.services)) {
+ if (hasAutoUpdateLabel(service as any)) {
+ console.log(`Found auto-update label in service ${serviceName} of stack ${stack.name}`);
+ return true;
+ }
+ }
+
+ return false;
+ } catch (error) {
+ console.error(`Error parsing compose file for stack ${stack.name}:`, error);
+ return false;
+ }
+}
+
+/**
+ * Check if a service has the auto-update label
+ */
+function hasAutoUpdateLabel(service: any): boolean {
+ if (!service?.labels) return false;
+
+ // Handle array format: ["arcane.stack.auto-update=true"]
+ if (Array.isArray(service.labels)) {
+ return service.labels.some((label: string) => typeof label === 'string' && (label === 'arcane.stack.auto-update=true' || label.startsWith('arcane.stack.auto-update=true')));
+ }
+
+ // Handle object format: {"arcane.stack.auto-update": "true"}
+ if (typeof service.labels === 'object' && service.labels !== null) {
+ return service.labels['arcane.stack.auto-update'] === 'true' || service.labels['arcane.stack.auto-update'] === true;
+ }
+
+ return false;
+}
+
+/**
+ * Check if container image has updates available
+ */
async function checkContainerImageUpdate(container: ContainerInfo): Promise<boolean> {
- const containerName = container.Names && container.Names.length > 0 ? container.Names[0].substring(1) : container.Id;
+ const containerName = getContainerName(container);
+
try {
const imageRef = container.Image;
+
+ // Skip digest-based images
if (/^sha256:[A-Fa-f0-9]{64}$/.test(imageRef)) {
- console.log(`Auto-update: Skipping image check for ${containerName}, image is by digest: ${imageRef}`);
+ console.log(`Skipping ${containerName} - using digest-based image`);
return false;
}
+
+ // Get current image details
const currentImage = await getImage(container.ImageID);
if (!currentImage) {
- console.warn(`Auto-update: Current image details not found for ${containerName} (ImageID: ${container.ImageID})`);
+ console.warn(`Current image not found for ${containerName}`);
return false;
}
- console.log(`Auto-update: Pulling image ${imageRef} for ${containerName} to check for updates...`);
+ console.log(`Pulling latest ${imageRef} for ${containerName}...`);
await pullImage(imageRef);
- const freshPulledImageDetails = await getImage(imageRef);
- if (!freshPulledImageDetails) {
- console.warn(`Auto-update: Image details for ${imageRef} not found after pull for ${containerName}.`);
+ // Get updated image details
+ const updatedImage = await getImage(imageRef);
+ if (!updatedImage) {
+ console.warn(`Updated image not found after pull for ${containerName}`);
return false;
}
- if (freshPulledImageDetails.Id !== container.ImageID) {
- console.log(`Auto-update: New image version found for ${containerName}. Current: ${container.ImageID}, New: ${freshPulledImageDetails.Id}`);
- return true;
- } else {
- console.log(`Auto-update: Image ${imageRef} for ${containerName} is already up-to-date (ID: ${container.ImageID}).`);
- return false;
+ // Compare image IDs
+ const hasUpdate = updatedImage.Id !== container.ImageID;
+ if (hasUpdate) {
+ console.log(`Update found for ${containerName}: ${container.ImageID} -> ${updatedImage.Id}`);
}
- } catch (error: unknown) {
- console.error(`Error checking for image update for ${containerName}:`, error);
+
+ return hasUpdate;
+ } catch (error) {
+ console.error(`Error checking image update for ${containerName}:`, error);
return false;
}
}
+/**
+ * Check if stack images have updates available
+ */
async function checkStackImagesUpdate(stack: Stack): Promise<boolean> {
- let updateFound = false;
try {
const fullStack = await getStack(stack.id);
- if (!fullStack || !fullStack.composeContent) {
- console.warn(`Stack ${stack.name} (${stack.id}) compose content not found.`);
+ if (!fullStack?.composeContent) {
+ console.warn(`No compose content for stack ${stack.name}`);
return false;
}
- const composeLines = fullStack.composeContent.split('\n');
- const imageLines = composeLines.filter((line) => line.trim().startsWith('image:') || line.includes(' image:'));
- if (imageLines.length === 0) {
- console.log(`No image references found in stack ${stack.name}.`);
+
+ const imageRefs = extractImageReferences(fullStack.composeContent);
+ if (imageRefs.length === 0) {
+ console.log(`No images found in stack ${stack.name}`);
return false;
}
- const imageRefs = imageLines
- .map((line) => {
- const imagePart = line.split('image:')[1].trim();
- return imagePart.replace(/['"]/g, '').split(/[\s#]/)[0];
- })
- .filter((ref) => ref && (ref.includes(':') || ref.includes('/')));
- const uniqueImageRefs = [...new Set(imageRefs)];
- console.log(`Checking images for stack ${stack.name}: ${uniqueImageRefs.join(', ')}`);
- for (const imageRef of uniqueImageRefs) {
+
+ console.log(`Checking ${imageRefs.length} images for stack ${stack.name}: ${imageRefs.join(', ')}`);
+
+ let updateFound = false;
+ for (const imageRef of imageRefs) {
try {
- let currentImageId: string | null = null;
- try {
- const currentImage = await getImage(imageRef);
- if (currentImage) {
- currentImageId = currentImage.Id;
- }
- } catch (e: unknown) {
- if (e instanceof Error && 'statusCode' in e && (e as { statusCode?: number }).statusCode !== 404) {
- console.warn(`Could not get current details for image ${imageRef}: ${e.message}`);
- }
- }
- console.log(`Pulling ${imageRef} to check for updates...`);
- await pullImage(imageRef);
- let newImageId: string | null = null;
- try {
- const newImage = await getImage(imageRef);
- if (newImage) {
- newImageId = newImage.Id;
- } else {
- console.warn(`Image ${imageRef} not found after pull.`);
- continue;
- }
- } catch (e: unknown) {
- console.error(`Could not get details for image ${imageRef} after pull: ${e instanceof Error ? e.message : String(e)}`);
+ // Skip digest-based images
+ if (/^sha256:[A-Fa-f0-9]{64}$/.test(imageRef)) {
+ console.log(`Skipping digest-based image: ${imageRef}`);
continue;
}
- if (newImageId && newImageId !== currentImageId) {
- console.log(`Update found for image ${imageRef} in stack ${stack.name}. New ID: ${newImageId}, Old ID: ${currentImageId}`);
+
+ const hasUpdate = await checkImageUpdate(imageRef);
+ if (hasUpdate) {
+ console.log(`Update found for image ${imageRef} in stack ${stack.name}`);
updateFound = true;
- } else {
- console.log(`Image ${imageRef} is up-to-date.`);
+ // Continue checking other images for logging purposes
}
- } catch (error: unknown) {
- console.error(`Error checking/pulling image update for ${imageRef} in stack ${stack.name}:`, error);
+ } catch (error) {
+ console.error(`Error checking image ${imageRef} in stack ${stack.name}:`, error);
}
}
+
return updateFound;
- } catch (error: unknown) {
- console.error(`Error processing stack updates for ${stack.name}:`, error);
+ } catch (error) {
+ console.error(`Error checking stack images for ${stack.name}:`, error);
return false;
}
}
+
+/**
+ * Extract image references from compose content
+ */
+function extractImageReferences(composeContent: string): string[] {
+ try {
+ const composeData = yaml.load(composeContent) as any;
+ const images: string[] = [];
+
+ if (composeData?.services) {
+ for (const [serviceName, service] of Object.entries(composeData.services)) {
+ const serviceObj = service as any;
+ if (serviceObj?.image && typeof serviceObj.image === 'string') {
+ images.push(serviceObj.image.trim());
+ }
+ }
+ }
+
+ return [...new Set(images)]; // Remove duplicates
+ } catch (error) {
+ console.error('Error parsing compose content for images:', error);
+ return [];
+ }
+}
+
+/**
+ * Check if a single image has updates
+ */
+async function checkImageUpdate(imageRef: string): Promise<boolean> {
+ try {
+ // Get current image ID
+ let currentImageId: string | null = null;
+ try {
+ const currentImage = await getImage(imageRef);
+ currentImageId = currentImage?.Id || null;
+ } catch (error) {
+ // Image might not exist locally yet
+ console.log(`Image ${imageRef} not found locally, will pull fresh`);
+ }
+
+ // Pull latest version
+ console.log(`Pulling ${imageRef}...`);
+ await pullImage(imageRef);
+
+ // Get new image ID
+ const newImage = await getImage(imageRef);
+ if (!newImage) {
+ console.warn(`Failed to get image details after pull: ${imageRef}`);
+ return false;
+ }
+
+ // Compare IDs
+ if (!currentImageId) {
+ console.log(`New image pulled: ${imageRef}`);
+ return true; // New image, consider it an update
+ }
+
+ const hasUpdate = newImage.Id !== currentImageId;
+ if (hasUpdate) {
+ console.log(`Image updated: ${imageRef} (${currentImageId} -> ${newImage.Id})`);
+ } else {
+ console.log(`Image up-to-date: ${imageRef}`);
+ }
+
+ return hasUpdate;
+ } catch (error) {
+ console.error(`Error checking image update for ${imageRef}:`, error);
+ return false;
+ }
+}
+
+/**
+ * Get container name from container info
+ */
+function getContainerName(container: ContainerInfo): string {
+ if (container.Names && container.Names.length > 0) {
+ return container.Names[0].startsWith('/') ? container.Names[0].substring(1) : container.Names[0];
+ }
+ return container.Id.substring(0, 12);
+}