headplane/app/utils/integration/docker.ts

143 lines
3.6 KiB
TypeScript
Raw Normal View History

2025-02-27 13:42:36 -05:00
import { constants, access } from 'node:fs/promises';
2024-12-31 10:30:14 +05:30
import { setTimeout } from 'node:timers/promises';
import { Client } from 'undici';
2025-02-27 13:42:36 -05:00
import { HeadscaleError, healthcheck, pull } from '~/utils/headscale';
import { HeadplaneConfig } from '~server/context/parser';
import log from '~server/utils/log';
2025-02-27 13:42:36 -05:00
import { Integration } from './abstract';
2025-02-27 13:42:36 -05:00
type T = NonNullable<HeadplaneConfig['integration']>['docker'];
export default class DockerIntegration extends Integration<T> {
private maxAttempts = 10;
private client: Client | undefined;
2025-02-27 13:42:36 -05:00
get name() {
return 'Docker';
}
2025-02-27 13:42:36 -05:00
async isAvailable() {
if (this.context.container_name.length === 0) {
log.error('INTG', 'Docker container name is empty');
2024-12-31 10:30:14 +05:30
return false;
}
2025-02-27 13:42:36 -05:00
log.info('INTG', 'Using container: %s', this.context.container_name);
2024-12-31 10:30:14 +05:30
let url: URL | undefined;
try {
2025-02-27 13:42:36 -05:00
url = new URL(this.context.socket);
} catch {
2025-02-27 13:42:36 -05:00
log.error('INTG', 'Invalid Docker socket path: %s', this.context.socket);
2024-12-31 10:30:14 +05:30
return false;
}
if (url.protocol !== 'tcp:' && url.protocol !== 'unix:') {
2024-12-31 10:30:14 +05:30
log.error('INTG', 'Invalid Docker socket protocol: %s', url.protocol);
return false;
}
// The API is available as an HTTP endpoint and this
// will simplify the fetching logic in undici
if (url.protocol === 'tcp:') {
// Apparently setting url.protocol doesn't work anymore?
2024-12-31 10:30:14 +05:30
const fetchU = url.href.replace(url.protocol, 'http:');
try {
2024-12-31 10:30:14 +05:30
log.info('INTG', 'Checking API: %s', fetchU);
await fetch(new URL('/v1.30/version', fetchU).href);
} catch (error) {
2025-02-27 13:42:36 -05:00
log.error('INTG', 'Failed to connect to Docker API: %s', error);
log.debug('INTG', 'Connection error: %o', error);
2024-12-31 10:30:14 +05:30
return false;
}
2025-02-27 13:42:36 -05:00
this.client = new Client(fetchU);
}
// Check if the socket is accessible
if (url.protocol === 'unix:') {
try {
2024-12-31 10:30:14 +05:30
log.info('INTG', 'Checking socket: %s', url.pathname);
await access(url.pathname, constants.R_OK);
} catch (error) {
2025-02-27 13:42:36 -05:00
log.error('INTG', 'Failed to access Docker socket: %s', url.pathname);
log.debug('INTG', 'Access error: %o', error);
2024-12-31 10:30:14 +05:30
return false;
}
2025-02-27 13:42:36 -05:00
this.client = new Client('http://localhost', {
socketPath: url.pathname,
2024-12-31 10:30:14 +05:30
});
}
2025-02-27 13:42:36 -05:00
return this.client !== undefined;
}
2025-02-27 13:42:36 -05:00
async onConfigChange() {
if (!this.client) {
2024-12-31 10:30:14 +05:30
return;
}
2024-12-31 10:30:14 +05:30
log.info('INTG', 'Restarting Headscale via Docker');
2024-12-31 10:30:14 +05:30
let attempts = 0;
2025-02-27 13:42:36 -05:00
while (attempts <= this.maxAttempts) {
log.debug(
2024-12-31 10:30:14 +05:30
'INTG',
'Restarting container: %s (attempt %d)',
2025-02-27 13:42:36 -05:00
this.context.container_name,
attempts,
2024-12-31 10:30:14 +05:30
);
2025-02-27 13:42:36 -05:00
const response = await this.client.request({
method: 'POST',
2025-02-27 13:42:36 -05:00
path: `/v1.30/containers/${this.context.container_name}/restart`,
2024-12-31 10:30:14 +05:30
});
if (response.statusCode !== 204) {
2025-02-27 13:42:36 -05:00
if (attempts < this.maxAttempts) {
2024-12-31 10:30:14 +05:30
attempts++;
await setTimeout(1000);
continue;
}
2024-12-31 10:30:14 +05:30
const stringCode = response.statusCode.toString();
const body = await response.body.text();
throw new Error(`API request failed: ${stringCode} ${body}`);
}
2024-12-31 10:30:14 +05:30
break;
}
2024-12-31 10:30:14 +05:30
attempts = 0;
2025-02-27 13:42:36 -05:00
while (attempts <= this.maxAttempts) {
try {
2024-12-31 10:30:14 +05:30
log.debug('INTG', 'Checking Headscale status (attempt %d)', attempts);
2025-02-27 13:42:36 -05:00
await healthcheck();
log.info('INTG', 'Headscale is up and running');
2024-12-31 10:30:14 +05:30
return;
} catch (error) {
if (error instanceof HeadscaleError && error.status === 401) {
2024-12-31 10:30:14 +05:30
break;
}
if (error instanceof HeadscaleError && error.status === 404) {
2024-12-31 10:30:14 +05:30
break;
}
2025-02-27 13:42:36 -05:00
if (attempts < this.maxAttempts) {
2024-12-31 10:30:14 +05:30
attempts++;
await setTimeout(1000);
continue;
}
2025-02-27 13:42:36 -05:00
log.error(
'INTG',
'Missed restart deadline for %s',
this.context.container_name,
);
return;
}
}
2025-02-27 13:42:36 -05:00
}
}