From b170e11dd6802ab3c2ece446165982672149ae56 Mon Sep 17 00:00:00 2001 From: Aarnav Tale Date: Fri, 2 Aug 2024 16:14:05 -0400 Subject: [PATCH] fix(TALE-7): don't destructure context otherwise it won't set properly --- app/integration/docker.ts | 54 ++++++++++++++++++++--------------- app/integration/kubernetes.ts | 18 ++++++------ app/integration/proc.ts | 12 ++++---- 3 files changed, 46 insertions(+), 38 deletions(-) diff --git a/app/integration/docker.ts b/app/integration/docker.ts index 473373a..e31966c 100644 --- a/app/integration/docker.ts +++ b/app/integration/docker.ts @@ -21,19 +21,19 @@ export default createIntegration({ container: undefined, maxAttempts: 10, }, - isAvailable: async ({ client, container }) => { + isAvailable: async (context) => { // Check for the HEADSCALE_CONTAINER environment variable first // to avoid unnecessary fetching of the Docker socket - container = process.env.HEADSCALE_CONTAINER + context.container = process.env.HEADSCALE_CONTAINER ?.trim() .toLowerCase() - if (!container || container.length === 0) { + if (!context.container || context.container.length === 0) { log.error('INTG', 'Missing HEADSCALE_CONTAINER variable') return false } - log.info('INTG', 'Using container: %s', container) + log.info('INTG', 'Using container: %s', context.container) const path = process.env.DOCKER_SOCK ?? 'unix:///var/run/docker.sock' let url: URL | undefined @@ -63,7 +63,7 @@ export default createIntegration({ return false } - client = new Client(url.href) + context.client = new Client(url.href) } // Check if the socket is accessible @@ -80,30 +80,30 @@ export default createIntegration({ return false } - client = new Client('http://localhost', { - socketPath: path, + context.client = new Client('http://localhost', { + socketPath: url.pathname, }) } - return client !== undefined + return context.client !== undefined }, - onAclChange: async ({ client, container, maxAttempts }) => { - if (!client || !container) { + onAclChange: async (context) => { + if (!context.client || !context.container) { return } log.info('INTG', 'Sending SIGHUP to Headscale via Docker') let attempts = 0 - while (attempts <= maxAttempts) { - const response = await client.request({ + while (attempts <= context.maxAttempts) { + const response = await context.client.request({ method: 'POST', - path: `/v1.30/containers/${container}/kill?signal=SIGHUP`, + path: `/v1.30/containers/${context.container}/kill?signal=SIGHUP`, }) if (response.statusCode !== 204) { - if (attempts < maxAttempts) { + if (attempts < context.maxAttempts) { attempts++ await setTimeout(1000) continue @@ -113,25 +113,27 @@ export default createIntegration({ const body = await response.body.text() throw new Error(`API request failed: ${stringCode} ${body}`) } + + break } }, - onConfigChange: async ({ client, container, maxAttempts }) => { - if (!client || !container) { + onConfigChange: async (context) => { + if (!context.client || !context.container) { return } log.info('INTG', 'Restarting Headscale via Docker') let attempts = 0 - while (attempts <= maxAttempts) { - const response = await client.request({ + while (attempts <= context.maxAttempts) { + const response = await context.client.request({ method: 'POST', - path: `/v1.30/containers/${container}/restart`, + path: `/v1.30/containers/${context.container}/restart`, }) if (response.statusCode !== 204) { - if (attempts < maxAttempts) { + if (attempts < context.maxAttempts) { attempts++ await setTimeout(1000) continue @@ -141,10 +143,12 @@ export default createIntegration({ const body = await response.body.text() throw new Error(`API request failed: ${stringCode} ${body}`) } + + break } attempts = 0 - while (attempts <= maxAttempts) { + while (attempts <= context.maxAttempts) { try { await pull('v1', '') return @@ -153,13 +157,17 @@ export default createIntegration({ break } - if (attempts < maxAttempts) { + if (error instanceof HeadscaleError && error.status === 404) { + break + } + + if (attempts < context.maxAttempts) { attempts++ await setTimeout(1000) continue } - throw new Error(`Missed restart deadline for ${container}`) + throw new Error(`Missed restart deadline for ${context.container}`) } } }, diff --git a/app/integration/kubernetes.ts b/app/integration/kubernetes.ts index b2cc881..345ecde 100644 --- a/app/integration/kubernetes.ts +++ b/app/integration/kubernetes.ts @@ -18,7 +18,7 @@ export default createIntegration({ context: { pid: undefined, }, - isAvailable: async ({ pid }) => { + isAvailable: async (context) => { if (platform() !== 'linux') { log.error('INTG', 'Kubernetes is only available on Linux') return false @@ -168,8 +168,8 @@ export default createIntegration({ return false } - pid = pids[0] - log.info('INTG', 'Found Headscale process with PID: %d', pid) + context.pid = pids[0] + log.info('INTG', 'Found Headscale process with PID: %d', context.pid) return true } catch { log.error('INTG', 'Failed to read /proc') @@ -177,21 +177,21 @@ export default createIntegration({ } }, - onAclChange: ({ pid }) => { - if (!pid) { + onAclChange: (context) => { + if (!context.pid) { return } log.info('INTG', 'Sending SIGHUP to Headscale') - kill(pid, 'SIGHUP') + kill(context.pid, 'SIGHUP') }, - onConfigChange: ({ pid }) => { - if (!pid) { + onConfigChange: (context) => { + if (!context.pid) { return } log.info('INTG', 'Sending SIGTERM to Headscale') - kill(pid, 'SIGTERM') + kill(context.pid, 'SIGTERM') }, }) diff --git a/app/integration/proc.ts b/app/integration/proc.ts index 02c556e..d50d6ea 100644 --- a/app/integration/proc.ts +++ b/app/integration/proc.ts @@ -16,7 +16,7 @@ export default createIntegration({ context: { pid: undefined, }, - isAvailable: async ({ pid }) => { + isAvailable: async (context) => { if (platform() !== 'linux') { log.error('INTG', '/proc is only available on Linux') return false @@ -63,8 +63,8 @@ export default createIntegration({ return false } - pid = pids[0] - log.info('INTG', 'Found Headscale process with PID: %d', pid) + context.pid = pids[0] + log.info('INTG', 'Found Headscale process with PID: %d', context.pid) return true } catch { log.error('INTG', 'Failed to read /proc') @@ -72,12 +72,12 @@ export default createIntegration({ } }, - onAclChange: ({ pid }) => { - if (!pid) { + onAclChange: (context) => { + if (!context.pid) { return } log.info('INTG', 'Sending SIGHUP to Headscale') - kill(pid, 'SIGHUP') + kill(context.pid, 'SIGHUP') }, })