-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
06a2d3a
commit 57e6088
Showing
8 changed files
with
193 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { exec } from 'child_process'; | ||
|
||
export const shellExec = async (cmd, options = {}) => { | ||
return new Promise(function (resolve, reject) { | ||
exec(cmd, { shell: '/bin/bash' }, (err, stdout, stderr) => { | ||
if (err) { | ||
resolve({ code: err.code, stdout, stderr }); | ||
} else { | ||
resolve({ code: 0, stdout, stderr }); | ||
} | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import http from 'http'; | ||
import { shellExec } from './shellExec.mjs'; | ||
|
||
const timeStart = new Date().getTime(); | ||
|
||
const Status = { | ||
GREEN: 'green', | ||
YELLOW: 'yellow', | ||
RED: 'red', | ||
}; | ||
|
||
const supervisorStatusToStatus = { | ||
STOPPED: Status.GREY, | ||
STARTING: Status.YELLOW, | ||
RUNNING: Status.GREEN, | ||
BACKOFF: Status.RED, | ||
STOPPING: Status.YELLOW, | ||
EXITED: Status.RED, | ||
FATAL: Status.RED, | ||
UNKNOWN: Status.RED, | ||
}; | ||
|
||
const processStatusToMessage = (name, status) => { | ||
if (status === Status.GREEN) return `${name} process is running`; | ||
if (status === Status.YELLOW) return `${name} process is having issues`; | ||
if (status === Status.RED) return `${name} process has failed to start`; | ||
return `${name} process is stopped`; | ||
}; | ||
|
||
const getSupervisorStatus = async (name, process) => { | ||
let status = Status.RED; | ||
let isPending = true; | ||
|
||
const processStatus = (await shellExec(`supervisorctl status ${process}`)).stdout || ''; | ||
if (!processStatus.includes('ERROR')) { | ||
const supervisorStatus = processStatus.split(' ').filter((item) => !!item.trim())[1]; | ||
status = supervisorStatusToStatus[supervisorStatus] || Status.RED; | ||
isPending = status === Status.RED; | ||
} | ||
|
||
return { | ||
status, | ||
isPending, | ||
message: processStatusToMessage(name, status), | ||
}; | ||
}; | ||
|
||
const getWWWStatus = async () => { | ||
try { | ||
const nextjsResponse = (await shellExec('curl http://localhost')).stdout; | ||
if (nextjsResponse && nextjsResponse.includes('__NEXT_DATA__')) return { status: Status.GREEN, message: '' }; | ||
return { status: Status.RED, message: 'Next.js is not responding' }; | ||
} catch { | ||
return { status: Status.RED, message: 'Failed to query Next.js status' }; | ||
} | ||
}; | ||
|
||
const getPostgresStatus = async () => { | ||
const commonStatus = { | ||
identifier: 'postgres', | ||
name: 'PostgreSQL', | ||
description: 'PostgreSQL database', | ||
}; | ||
|
||
try { | ||
const postgresResponse = (await shellExec('pg_isready -h $POSTGRES_HOST -p $POSTGRES_PORT')).stdout; | ||
if (postgresResponse && postgresResponse.includes('accepting connections')) { | ||
return { | ||
...commonStatus, | ||
status: Status.GREEN, | ||
message: '', | ||
}; | ||
} | ||
|
||
return { | ||
...commonStatus, | ||
status: Status.RED, | ||
message: 'PostgreSQL is not responding', | ||
}; | ||
} catch { | ||
return { | ||
...commonStatus, | ||
status: Status.RED, | ||
message: 'PostgreSQL is not responding', | ||
}; | ||
} | ||
}; | ||
|
||
const getStatus = async () => { | ||
// First see if supervisor has started www | ||
const wwwProcessStatus = await getSupervisorStatus('Odoo', 'odoo'); | ||
let wwwStatus = { | ||
identifier: 'odoo', | ||
name: 'Odoo', | ||
description: 'Odoo website', | ||
...wwwProcessStatus, | ||
}; | ||
|
||
// Then check if site is running | ||
if (wwwProcessStatus.status === Status.GREEN) { | ||
wwwStatus = { ...wwwStatus, ...(await getWWWStatus()) }; | ||
} | ||
|
||
// Exception... Don't show red until some time has passed | ||
const belowYellowThreshold = (new Date().getTime() - timeStart) / 1000 < 30; | ||
if (wwwStatus.status === Status.RED && belowYellowThreshold) { | ||
wwwStatus.status = Status.YELLOW; | ||
wwwStatus.message = 'Waiting for Odoo...'; | ||
} | ||
|
||
const status = { | ||
diploiStatusVersion: 1, | ||
items: [wwwStatus], | ||
}; | ||
const hasPostgres = !!process.env.parameter_group_postgres_enabled; | ||
if (hasPostgres) { | ||
status.items.push(await getPostgresStatus()); | ||
} | ||
|
||
return status; | ||
}; | ||
|
||
const requestListener = async (req, res) => { | ||
res.writeHead(200); | ||
res.end(JSON.stringify(await getStatus())); | ||
}; | ||
|
||
const server = http.createServer(requestListener); | ||
server.listen(3000, '0.0.0.0'); | ||
|
||
console.log('🌎 Status Server Started ' + new Date().toISOString()); | ||
|
||
const podReadinessLoop = async () => { | ||
const status = await getStatus(); | ||
let allOK = !status.items.find((s) => s.status !== Status.GREEN); | ||
if (allOK) { | ||
await shellExec('touch /tmp/pod-ready'); | ||
} else { | ||
await shellExec('rm -f /tmp/pod-ready'); | ||
} | ||
setTimeout(() => { | ||
podReadinessLoop(); | ||
}, 5000 + (allOK ? 1 : 0) * 5000); | ||
}; | ||
podReadinessLoop(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: odoo | ||
name: app | ||
spec: | ||
ports: | ||
- port: 8069 | ||
name: odoo | ||
name: app | ||
- port: 3000 | ||
name: status | ||
selector: | ||
app: odoo | ||
app: app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters