feat(TALE-7): reimplement integration system

This commit is contained in:
Aarnav Tale
2024-07-09 22:53:00 -04:00
parent 3cc726320a
commit 0aa0406ea6
6 changed files with 356 additions and 375 deletions
+19 -39
View File
@@ -1,28 +1,18 @@
import docker from './docker'
import kubernetes from './kubernetes'
import proc from './proc'
import dockerIntegration from './docker'
import kubernetesIntegration from './kubernetes'
import procIntegration from './proc'
export interface Integration {
name: string
preflight: () => Promise<boolean>
sighup?: () => Promise<void>
restart?: () => Promise<void>
}
export * from './integration'
// Because we previously supported the Docker integration by
// checking for the HEADSCALE_CONTAINER variable, we need to
// check for it here as well.
//
// 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
// an integration.
export async function checkIntegration() {
export function loadIntegration() {
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) {
integration = 'docker'
}
@@ -32,32 +22,22 @@ export async function checkIntegration() {
return
}
let module: Integration | undefined
try {
module = getIntegration(integration)
await module.preflight()
} catch (error) {
console.error('Failed to load integration', error)
return
}
return module
}
function getIntegration(name: string) {
switch (name) {
switch (integration.toLowerCase().trim()) {
case 'docker': {
return docker
return dockerIntegration
}
case 'proc': {
return proc
case 'proc':
case 'native':
case 'linux': {
return procIntegration
}
case 'kubernetes':
case 'k8s': {
return kubernetes
}
default: {
throw new Error(`Unknown integration: ${name}`)
return kubernetesIntegration
}
}
console.error('Unknown integration:', integration)
}