fix: fix integrations not loading

This commit is contained in:
Aarnav Tale 2025-04-02 21:29:00 -04:00
parent d5fb8a2966
commit 1fb084451d
12 changed files with 184 additions and 202 deletions

View File

@ -1,7 +0,0 @@
ROOT_API_KEY=abcdefghijklmnopqrstuvwxyz
COOKIE_SECRET=abcdefghijklmnopqrstuvwxyz
DISABLE_API_KEY_LOGIN=true
HEADSCALE_CONTAINER=headscale
HOST=0.0.0.0
PORT=3000
CONFIG_FILE=/etc/headscale/config.yaml

View File

@ -1,8 +1,4 @@
import type { import type { LinksFunction, MetaFunction } from 'react-router';
LinksFunction,
LoaderFunctionArgs,
MetaFunction,
} from 'react-router';
import { import {
Links, Links,
Meta, Meta,
@ -18,8 +14,6 @@ import ToastProvider from '~/components/ToastProvider';
import stylesheet from '~/tailwind.css?url'; import stylesheet from '~/tailwind.css?url';
import { LiveDataProvider } from '~/utils/live-data'; import { LiveDataProvider } from '~/utils/live-data';
import { useToastQueue } from '~/utils/toast'; import { useToastQueue } from '~/utils/toast';
import { LoadContext } from './server';
import log from './utils/log';
export const meta: MetaFunction = () => [ export const meta: MetaFunction = () => [
{ title: 'Headplane' }, { title: 'Headplane' },

View File

@ -1,6 +1,5 @@
import { ActionFunctionArgs, data } from 'react-router'; import { ActionFunctionArgs, data } from 'react-router';
import { LoadContext } from '~/server'; import { LoadContext } from '~/server';
import { hp_getIntegration } from '~/utils/integration/loader';
export async function dnsAction({ export async function dnsAction({
request, request,
@ -51,7 +50,7 @@ async function renameTailnet(formData: FormData, context: LoadContext) {
}, },
]); ]);
await hp_getIntegration()?.onConfigChange(); await context.integration?.onConfigChange(context.client);
} }
async function toggleMagic(formData: FormData, context: LoadContext) { async function toggleMagic(formData: FormData, context: LoadContext) {
@ -67,7 +66,7 @@ async function toggleMagic(formData: FormData, context: LoadContext) {
}, },
]); ]);
await hp_getIntegration()?.onConfigChange(); await context.integration?.onConfigChange(context.client);
} }
async function removeNs(formData: FormData, context: LoadContext) { async function removeNs(formData: FormData, context: LoadContext) {
@ -100,7 +99,7 @@ async function removeNs(formData: FormData, context: LoadContext) {
]); ]);
} }
await hp_getIntegration()?.onConfigChange(); await context.integration?.onConfigChange(context.client);
} }
async function addNs(formData: FormData, context: LoadContext) { async function addNs(formData: FormData, context: LoadContext) {
@ -135,7 +134,7 @@ async function addNs(formData: FormData, context: LoadContext) {
]); ]);
} }
await hp_getIntegration()?.onConfigChange(); await context.integration?.onConfigChange(context.client);
} }
async function removeDomain(formData: FormData, context: LoadContext) { async function removeDomain(formData: FormData, context: LoadContext) {
@ -153,7 +152,7 @@ async function removeDomain(formData: FormData, context: LoadContext) {
}, },
]); ]);
await hp_getIntegration()?.onConfigChange(); await context.integration?.onConfigChange(context.client);
} }
async function addDomain(formData: FormData, context: LoadContext) { async function addDomain(formData: FormData, context: LoadContext) {
@ -173,7 +172,7 @@ async function addDomain(formData: FormData, context: LoadContext) {
}, },
]); ]);
await hp_getIntegration()?.onConfigChange(); await context.integration?.onConfigChange(context.client);
} }
async function removeRecord(formData: FormData, context: LoadContext) { async function removeRecord(formData: FormData, context: LoadContext) {
@ -196,7 +195,7 @@ async function removeRecord(formData: FormData, context: LoadContext) {
}, },
]); ]);
await hp_getIntegration()?.onConfigChange(); await context.integration?.onConfigChange(context.client);
} }
async function addRecord(formData: FormData, context: LoadContext) { async function addRecord(formData: FormData, context: LoadContext) {
@ -219,5 +218,5 @@ async function addRecord(formData: FormData, context: LoadContext) {
}, },
]); ]);
await hp_getIntegration()?.onConfigChange(); await context.integration?.onConfigChange(context.client);
} }

View File

@ -8,6 +8,12 @@ many side-effects (in this case, importing a module may run code).
server server
├── index.ts: Loads everything and starts the web server. ├── index.ts: Loads everything and starts the web server.
├── config/ ├── config/
│ ├── integration/
│ │ ├── abstract.ts: Defines the abstract class for integrations.
│ │ ├── docker.ts: Contains the Docker integration.
│ │ ├── index.ts: Determines the correct integration to use (if any).
│ │ ├── kubernetes.ts: Contains the Kubernetes integration.
│ │ ├── proc.ts: Contains the Proc integration.
│ ├── env.ts: Checks the environment variables for custom overrides. │ ├── env.ts: Checks the environment variables for custom overrides.
│ ├── loader.ts: Checks the configuration file and coalesces with ENV. │ ├── loader.ts: Checks the configuration file and coalesces with ENV.
│ ├── schema.ts: Defines the schema for the Headplane configuration. │ ├── schema.ts: Defines the schema for the Headplane configuration.

View File

@ -1,3 +1,5 @@
import type { ApiClient } from '~/server/headscale/api-client';
export abstract class Integration<T> { export abstract class Integration<T> {
protected context: NonNullable<T>; protected context: NonNullable<T>;
constructor(context: T) { constructor(context: T) {
@ -9,6 +11,6 @@ export abstract class Integration<T> {
} }
abstract isAvailable(): Promise<boolean> | boolean; abstract isAvailable(): Promise<boolean> | boolean;
abstract onConfigChange(): Promise<void> | void; abstract onConfigChange(client: ApiClient): Promise<void> | void;
abstract get name(): string; abstract get name(): string;
} }

View File

@ -1,9 +1,9 @@
import { constants, access } from 'node:fs/promises'; import { constants, access } from 'node:fs/promises';
import { setTimeout } from 'node:timers/promises'; import { setTimeout } from 'node:timers/promises';
import { Client } from 'undici'; import { Client } from 'undici';
import { HeadscaleError, healthcheck, pull } from '~/utils/headscale'; import { ApiClient } from '~/server/headscale/api-client';
import { HeadplaneConfig } from '~server/context/parser'; import log from '~/utils/log';
import log from '~server/utils/log'; import type { HeadplaneConfig } from '../schema';
import { Integration } from './abstract'; import { Integration } from './abstract';
type T = NonNullable<HeadplaneConfig['integration']>['docker']; type T = NonNullable<HeadplaneConfig['integration']>['docker'];
@ -17,21 +17,25 @@ export default class DockerIntegration extends Integration<T> {
async isAvailable() { async isAvailable() {
if (this.context.container_name.length === 0) { if (this.context.container_name.length === 0) {
log.error('INTG', 'Docker container name is empty'); log.error('config', 'Docker container name is empty');
return false; return false;
} }
log.info('INTG', 'Using container: %s', this.context.container_name); log.info('config', 'Using container: %s', this.context.container_name);
let url: URL | undefined; let url: URL | undefined;
try { try {
url = new URL(this.context.socket); url = new URL(this.context.socket);
} catch { } catch {
log.error('INTG', 'Invalid Docker socket path: %s', this.context.socket); log.error(
'config',
'Invalid Docker socket path: %s',
this.context.socket,
);
return false; return false;
} }
if (url.protocol !== 'tcp:' && url.protocol !== 'unix:') { if (url.protocol !== 'tcp:' && url.protocol !== 'unix:') {
log.error('INTG', 'Invalid Docker socket protocol: %s', url.protocol); log.error('config', 'Invalid Docker socket protocol: %s', url.protocol);
return false; return false;
} }
@ -42,11 +46,11 @@ export default class DockerIntegration extends Integration<T> {
const fetchU = url.href.replace(url.protocol, 'http:'); const fetchU = url.href.replace(url.protocol, 'http:');
try { try {
log.info('INTG', 'Checking API: %s', fetchU); log.info('config', 'Checking API: %s', fetchU);
await fetch(new URL('/v1.30/version', fetchU).href); await fetch(new URL('/v1.30/version', fetchU).href);
} catch (error) { } catch (error) {
log.error('INTG', 'Failed to connect to Docker API: %s', error); log.error('config', 'Failed to connect to Docker API: %s', error);
log.debug('INTG', 'Connection error: %o', error); log.debug('config', 'Connection error: %o', error);
return false; return false;
} }
@ -56,11 +60,11 @@ export default class DockerIntegration extends Integration<T> {
// Check if the socket is accessible // Check if the socket is accessible
if (url.protocol === 'unix:') { if (url.protocol === 'unix:') {
try { try {
log.info('INTG', 'Checking socket: %s', url.pathname); log.info('config', 'Checking socket: %s', url.pathname);
await access(url.pathname, constants.R_OK); await access(url.pathname, constants.R_OK);
} catch (error) { } catch (error) {
log.error('INTG', 'Failed to access Docker socket: %s', url.pathname); log.error('config', 'Failed to access Docker socket: %s', url.pathname);
log.debug('INTG', 'Access error: %o', error); log.debug('config', 'Access error: %o', error);
return false; return false;
} }
@ -72,17 +76,17 @@ export default class DockerIntegration extends Integration<T> {
return this.client !== undefined; return this.client !== undefined;
} }
async onConfigChange() { async onConfigChange(client: ApiClient) {
if (!this.client) { if (!this.client) {
return; return;
} }
log.info('INTG', 'Restarting Headscale via Docker'); log.info('config', 'Restarting Headscale via Docker');
let attempts = 0; let attempts = 0;
while (attempts <= this.maxAttempts) { while (attempts <= this.maxAttempts) {
log.debug( log.debug(
'INTG', 'config',
'Restarting container: %s (attempt %d)', 'Restarting container: %s (attempt %d)',
this.context.container_name, this.context.container_name,
attempts, attempts,
@ -111,19 +115,15 @@ export default class DockerIntegration extends Integration<T> {
attempts = 0; attempts = 0;
while (attempts <= this.maxAttempts) { while (attempts <= this.maxAttempts) {
try { try {
log.debug('INTG', 'Checking Headscale status (attempt %d)', attempts); log.debug('config', 'Checking Headscale status (attempt %d)', attempts);
await healthcheck(); const status = await client.healthcheck();
log.info('INTG', 'Headscale is up and running'); if (status === false) {
throw new Error('Headscale is not running');
}
log.info('config', 'Headscale is up and running');
return; return;
} catch (error) { } catch (error) {
if (error instanceof HeadscaleError && error.status === 401) {
break;
}
if (error instanceof HeadscaleError && error.status === 404) {
break;
}
if (attempts < this.maxAttempts) { if (attempts < this.maxAttempts) {
attempts++; attempts++;
await setTimeout(1000); await setTimeout(1000);
@ -131,7 +131,7 @@ export default class DockerIntegration extends Integration<T> {
} }
log.error( log.error(
'INTG', 'config',
'Missed restart deadline for %s', 'Missed restart deadline for %s',
this.context.container_name, this.context.container_name,
); );

View File

@ -0,0 +1,62 @@
import { HeadplaneConfig } from '~/server/config/schema';
import log from '~/utils/log';
import dockerIntegration from './docker';
import kubernetesIntegration from './kubernetes';
import procIntegration from './proc';
export async function loadIntegration(context: HeadplaneConfig['integration']) {
const integration = getIntegration(context);
if (!integration) {
return;
}
try {
const res = await integration.isAvailable();
if (!res) {
log.error('config', 'Integration %s is not available', integration);
return;
}
} catch (error) {
log.error(
'config',
'Failed to load integration %s: %s',
integration,
error,
);
log.debug('config', 'Loading error: %o', error);
return;
}
return integration;
}
function getIntegration(integration: HeadplaneConfig['integration']) {
const docker = integration?.docker;
const k8s = integration?.kubernetes;
const proc = integration?.proc;
if (!docker?.enabled && !k8s?.enabled && !proc?.enabled) {
log.debug('config', 'No integrations enabled');
return;
}
if (docker?.enabled && k8s?.enabled && proc?.enabled) {
log.error('config', 'Multiple integrations enabled, please pick one only');
return;
}
if (docker?.enabled) {
log.info('config', 'Using Docker integration');
return new dockerIntegration(integration?.docker);
}
if (k8s?.enabled) {
log.info('config', 'Using Kubernetes integration');
return new kubernetesIntegration(integration?.kubernetes);
}
if (proc?.enabled) {
log.info('config', 'Using Proc integration');
return new procIntegration(integration?.proc);
}
}

View File

@ -4,9 +4,9 @@ import { join, resolve } from 'node:path';
import { kill } from 'node:process'; import { kill } from 'node:process';
import { setTimeout } from 'node:timers/promises'; import { setTimeout } from 'node:timers/promises';
import { Config, CoreV1Api, KubeConfig } from '@kubernetes/client-node'; import { Config, CoreV1Api, KubeConfig } from '@kubernetes/client-node';
import { HeadscaleError, healthcheck } from '~/utils/headscale'; import { ApiClient } from '~/server/headscale/api-client';
import { HeadplaneConfig } from '~server/context/parser'; import log from '~/utils/log';
import log from '~server/utils/log'; import { HeadplaneConfig } from '../schema';
import { Integration } from './abstract'; import { Integration } from './abstract';
// TODO: Upgrade to the new CoreV1Api from @kubernetes/client-node // TODO: Upgrade to the new CoreV1Api from @kubernetes/client-node
@ -21,16 +21,16 @@ export default class KubernetesIntegration extends Integration<T> {
async isAvailable() { async isAvailable() {
if (platform() !== 'linux') { if (platform() !== 'linux') {
log.error('INTG', 'Kubernetes is only available on Linux'); log.error('config', 'Kubernetes is only available on Linux');
return false; return false;
} }
const svcRoot = Config.SERVICEACCOUNT_ROOT; const svcRoot = Config.SERVICEACCOUNT_ROOT;
try { try {
log.debug('INTG', 'Checking Kubernetes service account at %s', svcRoot); log.debug('config', 'Checking Kubernetes service account at %s', svcRoot);
const files = await readdir(svcRoot); const files = await readdir(svcRoot);
if (files.length === 0) { if (files.length === 0) {
log.error('INTG', 'Kubernetes service account not found'); log.error('config', 'Kubernetes service account not found');
return false; return false;
} }
@ -41,17 +41,17 @@ export default class KubernetesIntegration extends Integration<T> {
Config.SERVICEACCOUNT_NAMESPACE_PATH, Config.SERVICEACCOUNT_NAMESPACE_PATH,
]; ];
log.debug('INTG', 'Looking for %s', expectedFiles.join(', ')); log.debug('config', 'Looking for %s', expectedFiles.join(', '));
if (!expectedFiles.every((file) => mappedFiles.has(file))) { if (!expectedFiles.every((file) => mappedFiles.has(file))) {
log.error('INTG', 'Malformed Kubernetes service account'); log.error('config', 'Malformed Kubernetes service account');
return false; return false;
} }
} catch (error) { } catch (error) {
log.error('INTG', 'Failed to access %s: %s', svcRoot, error); log.error('config', 'Failed to access %s: %s', svcRoot, error);
return false; return false;
} }
log.debug('INTG', 'Reading Kubernetes service account at %s', svcRoot); log.debug('config', 'Reading Kubernetes service account at %s', svcRoot);
const namespace = await readFile( const namespace = await readFile(
Config.SERVICEACCOUNT_NAMESPACE_PATH, Config.SERVICEACCOUNT_NAMESPACE_PATH,
'utf8', 'utf8',
@ -59,39 +59,39 @@ export default class KubernetesIntegration extends Integration<T> {
// Some very ugly nesting but it's necessary // Some very ugly nesting but it's necessary
if (this.context.validate_manifest === false) { if (this.context.validate_manifest === false) {
log.warn('INTG', 'Skipping strict Pod status check'); log.warn('config', 'Skipping strict Pod status check');
} else { } else {
const pod = this.context.pod_name; const pod = this.context.pod_name;
if (!pod) { if (!pod) {
log.error('INTG', 'Missing POD_NAME variable'); log.error('config', 'Missing POD_NAME variable');
return false; return false;
} }
if (pod.trim().length === 0) { if (pod.trim().length === 0) {
log.error('INTG', 'Pod name is empty'); log.error('config', 'Pod name is empty');
return false; return false;
} }
log.debug( log.debug(
'INTG', 'config',
'Checking Kubernetes pod %s in namespace %s', 'Checking Kubernetes pod %s in namespace %s',
pod, pod,
namespace, namespace,
); );
try { try {
log.debug('INTG', 'Attempgin to get cluster KubeConfig'); log.debug('config', 'Attempgin to get cluster KubeConfig');
const kc = new KubeConfig(); const kc = new KubeConfig();
kc.loadFromCluster(); kc.loadFromCluster();
const cluster = kc.getCurrentCluster(); const cluster = kc.getCurrentCluster();
if (!cluster) { if (!cluster) {
log.error('INTG', 'Malformed kubeconfig'); log.error('config', 'Malformed kubeconfig');
return false; return false;
} }
log.info( log.info(
'INTG', 'config',
'Service account connected to %s (%s)', 'Service account connected to %s (%s)',
cluster.name, cluster.name,
cluster.server, cluster.server,
@ -100,14 +100,14 @@ export default class KubernetesIntegration extends Integration<T> {
const kCoreV1Api = kc.makeApiClient(CoreV1Api); const kCoreV1Api = kc.makeApiClient(CoreV1Api);
log.info( log.info(
'INTG', 'config',
'Checking pod %s in namespace %s (%s)', 'Checking pod %s in namespace %s (%s)',
pod, pod,
namespace, namespace,
kCoreV1Api.basePath, kCoreV1Api.basePath,
); );
log.debug('INTG', 'Reading pod info for %s', pod); log.debug('config', 'Reading pod info for %s', pod);
const { response, body } = await kCoreV1Api.readNamespacedPod( const { response, body } = await kCoreV1Api.readNamespacedPod(
pod, pod,
namespace, namespace,
@ -115,36 +115,39 @@ export default class KubernetesIntegration extends Integration<T> {
if (response.statusCode !== 200) { if (response.statusCode !== 200) {
log.error( log.error(
'INTG', 'config',
'Failed to read pod info: http %d', 'Failed to read pod info: http %d',
response.statusCode, response.statusCode,
); );
return false; return false;
} }
log.debug('INTG', 'Got pod info: %o', body.spec); log.debug('config', 'Got pod info: %o', body.spec);
const shared = body.spec?.shareProcessNamespace; const shared = body.spec?.shareProcessNamespace;
if (shared === undefined) { if (shared === undefined) {
log.error('INTG', 'Pod does not have spec.shareProcessNamespace set'); log.error(
'config',
'Pod does not have spec.shareProcessNamespace set',
);
return false; return false;
} }
if (!shared) { if (!shared) {
log.error( log.error(
'INTG', 'config',
'Pod has set but disabled spec.shareProcessNamespace', 'Pod has set but disabled spec.shareProcessNamespace',
); );
return false; return false;
} }
log.info('INTG', 'Pod %s enabled shared processes', pod); log.info('config', 'Pod %s enabled shared processes', pod);
} catch (error) { } catch (error) {
log.error('INTG', 'Failed to read pod info: %s', error); log.error('config', 'Failed to read pod info: %s', error);
return false; return false;
} }
} }
log.debug('INTG', 'Looking for namespaced process in /proc'); log.debug('config', 'Looking for namespaced process in /proc');
const dir = resolve('/proc'); const dir = resolve('/proc');
try { try {
const subdirs = await readdir(dir); const subdirs = await readdir(dir);
@ -157,13 +160,13 @@ export default class KubernetesIntegration extends Integration<T> {
const path = join('/proc', dir, 'cmdline'); const path = join('/proc', dir, 'cmdline');
try { try {
log.debug('INTG', 'Reading %s', path); log.debug('config', 'Reading %s', path);
const data = await readFile(path, 'utf8'); const data = await readFile(path, 'utf8');
if (data.includes('headscale')) { if (data.includes('headscale')) {
return pid; return pid;
} }
} catch (error) { } catch (error) {
log.debug('INTG', 'Failed to read %s: %s', path, error); log.debug('config', 'Failed to read %s: %s', path, error);
} }
}); });
@ -176,10 +179,10 @@ export default class KubernetesIntegration extends Integration<T> {
} }
} }
log.debug('INTG', 'Found Headscale processes: %o', pids); log.debug('config', 'Found Headscale processes: %o', pids);
if (pids.length > 1) { if (pids.length > 1) {
log.error( log.error(
'INTG', 'config',
'Found %d Headscale processes: %s', 'Found %d Headscale processes: %s',
pids.length, pids.length,
pids.join(', '), pids.join(', '),
@ -188,49 +191,45 @@ export default class KubernetesIntegration extends Integration<T> {
} }
if (pids.length === 0) { if (pids.length === 0) {
log.error('INTG', 'Could not find Headscale process'); log.error('config', 'Could not find Headscale process');
return false; return false;
} }
this.pid = pids[0]; this.pid = pids[0];
log.info('INTG', 'Found Headscale process with PID: %d', this.pid); log.info('config', 'Found Headscale process with PID: %d', this.pid);
return true; return true;
} catch { } catch {
log.error('INTG', 'Failed to read /proc'); log.error('config', 'Failed to read /proc');
return false; return false;
} }
} }
async onConfigChange() { async onConfigChange(client: ApiClient) {
if (!this.pid) { if (!this.pid) {
return; return;
} }
try { try {
log.info('INTG', 'Sending SIGTERM to Headscale'); log.info('config', 'Sending SIGTERM to Headscale');
kill(this.pid, 'SIGTERM'); kill(this.pid, 'SIGTERM');
} catch (error) { } catch (error) {
log.error('INTG', 'Failed to send SIGTERM to Headscale: %s', error); log.error('config', 'Failed to send SIGTERM to Headscale: %s', error);
log.debug('INTG', 'kill(1) error: %o', error); log.debug('config', 'kill(1) error: %o', error);
} }
await setTimeout(1000); await setTimeout(1000);
let attempts = 0; let attempts = 0;
while (attempts <= this.maxAttempts) { while (attempts <= this.maxAttempts) {
try { try {
log.debug('INTG', 'Checking Headscale status (attempt %d)', attempts); log.debug('config', 'Checking Headscale status (attempt %d)', attempts);
await healthcheck(); const status = await client.healthcheck();
log.info('INTG', 'Headscale is up and running'); if (status === false) {
throw new Error('Headscale is not running');
}
log.info('config', 'Headscale is up and running');
return; return;
} catch (error) { } catch (error) {
if (error instanceof HeadscaleError && error.status === 401) {
break;
}
if (error instanceof HeadscaleError && error.status === 404) {
break;
}
if (attempts < this.maxAttempts) { if (attempts < this.maxAttempts) {
attempts++; attempts++;
await setTimeout(1000); await setTimeout(1000);
@ -238,7 +237,7 @@ export default class KubernetesIntegration extends Integration<T> {
} }
log.error( log.error(
'INTG', 'config',
'Missed restart deadline for Headscale (pid %d)', 'Missed restart deadline for Headscale (pid %d)',
this.pid, this.pid,
); );

View File

@ -3,9 +3,9 @@ import { platform } from 'node:os';
import { join, resolve } from 'node:path'; import { join, resolve } from 'node:path';
import { kill } from 'node:process'; import { kill } from 'node:process';
import { setTimeout } from 'node:timers/promises'; import { setTimeout } from 'node:timers/promises';
import { HeadscaleError, healthcheck } from '~/utils/headscale'; import { ApiClient } from '~/server/headscale/api-client';
import { HeadplaneConfig } from '~server/context/parser'; import log from '~/utils/log';
import log from '~server/utils/log'; import { HeadplaneConfig } from '../schema';
import { Integration } from './abstract'; import { Integration } from './abstract';
type T = NonNullable<HeadplaneConfig['integration']>['proc']; type T = NonNullable<HeadplaneConfig['integration']>['proc'];
@ -19,11 +19,11 @@ export default class ProcIntegration extends Integration<T> {
async isAvailable() { async isAvailable() {
if (platform() !== 'linux') { if (platform() !== 'linux') {
log.error('INTG', '/proc is only available on Linux'); log.error('config', '/proc is only available on Linux');
return false; return false;
} }
log.debug('INTG', 'Checking /proc for Headscale process'); log.debug('config', 'Checking /proc for Headscale process');
const dir = resolve('/proc'); const dir = resolve('/proc');
try { try {
const subdirs = await readdir(dir); const subdirs = await readdir(dir);
@ -36,13 +36,13 @@ export default class ProcIntegration extends Integration<T> {
const path = join('/proc', dir, 'cmdline'); const path = join('/proc', dir, 'cmdline');
try { try {
log.debug('INTG', 'Reading %s', path); log.debug('config', 'Reading %s', path);
const data = await readFile(path, 'utf8'); const data = await readFile(path, 'utf8');
if (data.includes('headscale')) { if (data.includes('headscale')) {
return pid; return pid;
} }
} catch (error) { } catch (error) {
log.error('INTG', 'Failed to read %s: %s', path, error); log.error('config', 'Failed to read %s: %s', path, error);
} }
}); });
@ -55,10 +55,10 @@ export default class ProcIntegration extends Integration<T> {
} }
} }
log.debug('INTG', 'Found Headscale processes: %o', pids); log.debug('config', 'Found Headscale processes: %o', pids);
if (pids.length > 1) { if (pids.length > 1) {
log.error( log.error(
'INTG', 'config',
'Found %d Headscale processes: %s', 'Found %d Headscale processes: %s',
pids.length, pids.length,
pids.join(', '), pids.join(', '),
@ -67,49 +67,46 @@ export default class ProcIntegration extends Integration<T> {
} }
if (pids.length === 0) { if (pids.length === 0) {
log.error('INTG', 'Could not find Headscale process'); log.error('config', 'Could not find Headscale process');
return false; return false;
} }
this.pid = pids[0]; this.pid = pids[0];
log.info('INTG', 'Found Headscale process with PID: %d', this.pid); log.info('config', 'Found Headscale process with PID: %d', this.pid);
return true; return true;
} catch { } catch {
log.error('INTG', 'Failed to read /proc'); log.error('config', 'Failed to read /proc');
return false; return false;
} }
} }
async onConfigChange() { async onConfigChange(client: ApiClient) {
if (!this.pid) { if (!this.pid) {
return; return;
} }
try { try {
log.info('INTG', 'Sending SIGTERM to Headscale'); log.info('config', 'Sending SIGTERM to Headscale');
kill(this.pid, 'SIGTERM'); kill(this.pid, 'SIGTERM');
} catch (error) { } catch (error) {
log.error('INTG', 'Failed to send SIGTERM to Headscale: %s', error); log.error('config', 'Failed to send SIGTERM to Headscale: %s', error);
log.debug('INTG', 'kill(1) error: %o', error); log.debug('config', 'kill(1) error: %o', error);
} }
await setTimeout(1000); await setTimeout(1000);
let attempts = 0; let attempts = 0;
while (attempts <= this.maxAttempts) { while (attempts <= this.maxAttempts) {
try { try {
log.debug('INTG', 'Checking Headscale status (attempt %d)', attempts); log.debug('config', 'Checking Headscale status (attempt %d)', attempts);
await healthcheck(); const status = await client.healthcheck();
log.info('INTG', 'Headscale is up and running'); if (status === false) {
log.error('config', 'Headscale is not running');
return;
}
log.info('config', 'Headscale is up and running');
return; return;
} catch (error) { } catch (error) {
if (error instanceof HeadscaleError && error.status === 401) {
break;
}
if (error instanceof HeadscaleError && error.status === 404) {
break;
}
if (attempts < this.maxAttempts) { if (attempts < this.maxAttempts) {
attempts++; attempts++;
await setTimeout(1000); await setTimeout(1000);
@ -117,7 +114,7 @@ export default class ProcIntegration extends Integration<T> {
} }
log.error( log.error(
'INTG', 'config',
'Missed restart deadline for Headscale (pid %d)', 'Missed restart deadline for Headscale (pid %d)',
this.pid, this.pid,
); );

View File

@ -34,10 +34,7 @@ export class ResponseError extends Error {
} }
} }
// Represents an error that occurred during a request export class ApiClient {
// class RequestError extends Error {
class ApiClient {
private agent: Agent; private agent: Agent;
private base: string; private base: string;

View File

@ -4,6 +4,7 @@ import { createHonoServer } from 'react-router-hono-server/node';
import type { WebSocket } from 'ws'; import type { WebSocket } from 'ws';
import log from '~/utils/log'; import log from '~/utils/log';
import { configureConfig, configureLogger, envVariables } from './config/env'; import { configureConfig, configureLogger, envVariables } from './config/env';
import { loadIntegration } from './config/integration';
import { loadConfig } from './config/loader'; import { loadConfig } from './config/loader';
import { createApiClient } from './headscale/api-client'; import { createApiClient } from './headscale/api-client';
import { loadHeadscaleConfig } from './headscale/config-loader'; import { loadHeadscaleConfig } from './headscale/config-loader';
@ -61,6 +62,7 @@ const appLoadContext = {
config.server.agent.ttl, config.server.agent.ttl,
), ),
integration: await loadIntegration(config.integration),
oidc: config.oidc ? await createOidcClient(config.oidc) : undefined, oidc: config.oidc ? await createOidcClient(config.oidc) : undefined,
}; };

View File

@ -1,69 +0,0 @@
import { HeadplaneConfig } from '~/server/config/schema';
import log from '~/utils/log';
import { Integration } from './abstract';
// import dockerIntegration from './docker';
// import kubernetesIntegration from './kubernetes';
// import procIntegration from './proc';
const runtimeIntegration: Integration<unknown> | undefined = undefined;
export function hp_getIntegration() {
return runtimeIntegration;
}
export async function hp_loadIntegration(
context: HeadplaneConfig['integration'],
) {
// const integration = getIntegration(context);
// if (!integration) {
// return;
// }
// try {
// const res = await integration.isAvailable();
// if (!res) {
// log.error('INTG', 'Integration %s is not available', integration);
// return;
// }
// } catch (error) {
// log.error('INTG', 'Failed to load integration %s: %s', integration, error);
// log.debug('INTG', 'Loading error: %o', error);
// return;
// }
// runtimeIntegration = integration;
}
function getIntegration(integration: HeadplaneConfig['integration']) {
const docker = integration?.docker;
const k8s = integration?.kubernetes;
const proc = integration?.proc;
if (!docker?.enabled && !k8s?.enabled && !proc?.enabled) {
log.debug('INTG', 'No integrations enabled');
return;
}
if (docker?.enabled && k8s?.enabled && proc?.enabled) {
log.error('INTG', 'Multiple integrations enabled, please pick one only');
return;
}
// if (docker?.enabled) {
// log.info('INTG', 'Using Docker integration');
// return new dockerIntegration(integration?.docker);
// }
// if (k8s?.enabled) {
// log.info('INTG', 'Using Kubernetes integration');
// return new kubernetesIntegration(integration?.kubernetes);
// }
// if (proc?.enabled) {
// log.info('INTG', 'Using Proc integration');
// return new procIntegration(integration?.proc);
// }
}
// IMPORTANT THIS IS A SIDE EFFECT ON INIT
// TODO: Switch this to the new singleton system
// const context = hp_getConfig();
// hp_loadIntegration(context.integration);