headplane/app/integration/index.ts

69 lines
1.7 KiB
TypeScript
Raw Normal View History

2024-12-31 10:30:14 +05:30
import log from '~/utils/log';
2024-12-31 10:30:14 +05:30
import dockerIntegration from './docker';
2024-12-31 10:31:50 +05:30
import type { IntegrationFactory } from './integration';
2024-12-31 10:30:14 +05:30
import kubernetesIntegration from './kubernetes';
import procIntegration from './proc';
2024-12-31 10:30:14 +05:30
export * from './integration';
export async function loadIntegration() {
2024-12-31 10:30:14 +05:30
let integration = process.env.HEADSCALE_INTEGRATION?.trim().toLowerCase();
// Old HEADSCALE_CONTAINER variable upgrade path
// This ensures that when people upgrade from older versions of Headplane
// they don't explicitly need to define the new HEADSCALE_INTEGRATION
// variable that is needed to configure docker
if (!integration && process.env.HEADSCALE_CONTAINER) {
2024-12-31 10:30:14 +05:30
integration = 'docker';
}
if (!integration) {
2024-12-31 10:30:14 +05:30
log.info('INTG', 'No integration set with HEADSCALE_INTEGRATION');
return;
}
2024-12-31 10:30:14 +05:30
let integrationFactory: IntegrationFactory | undefined;
switch (integration.toLowerCase().trim()) {
case 'docker': {
2024-12-31 10:30:14 +05:30
integrationFactory = dockerIntegration;
break;
}
case 'proc':
case 'native':
case 'linux': {
2024-12-31 10:30:14 +05:30
integrationFactory = procIntegration;
break;
}
case 'kubernetes':
case 'k8s': {
2024-12-31 10:30:14 +05:30
integrationFactory = kubernetesIntegration;
break;
}
default: {
2024-12-31 10:30:14 +05:30
log.error('INTG', 'Unknown integration: %s', integration);
throw new Error(`Unknown integration: ${integration}`);
}
}
2024-12-31 10:30:14 +05:30
log.info('INTG', 'Loading integration: %s', integration);
try {
const res = await integrationFactory.isAvailable(
integrationFactory.context,
2024-12-31 10:30:14 +05:30
);
if (!res) {
2024-12-31 10:30:14 +05:30
log.error('INTG', 'Integration %s is not available', integration);
return;
}
} catch (error) {
2024-12-31 10:30:14 +05:30
log.error('INTG', 'Failed to load integration %s: %s', integration, error);
return;
}
2024-12-31 10:30:14 +05:30
log.info('INTG', 'Loaded integration: %s', integration);
return integrationFactory;
}