Skip to content

Commit

Permalink
Make status work?
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcusAhlfors committed Nov 20, 2023
1 parent 06a2d3a commit 57e6088
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 35 deletions.
2 changes: 1 addition & 1 deletion diploi-credential-helper
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ echo `date -u +"%Y-%m-%dT%H:%M:%SZ"` Starting $1 >> /var/log/git-credential-help
test "$1" = get || exit 2;

# Read core token from file
repository=odoo
repository=app
ct=`cat /etc/diploi-git/credential-token-$repository` || exit 3;

echo `date -u +"%Y-%m-%dT%H:%M:%SZ"` CredentialToken $ct >> /var/log/git-credential-helper.log
Expand Down
32 changes: 16 additions & 16 deletions diploi-template.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
diploiTemplateVersion: V1.0
name: diploi-odoo
description: Odoo 16 ERP template
description: Odoo 16 ERP & CRM template
defaultName: Odoo

stages:
Expand All @@ -12,8 +12,8 @@ stages:
identifier: production

contexts:
- name: odoo
identifier: odoo
- name: app
identifier: app
- name: postgres
identifier: postgres

Expand Down Expand Up @@ -60,37 +60,37 @@ environmentVariables:
- identifier: TEST
defaultValue: This text comes from an environment variable
type: string
contexts: label=odoo
contexts: label=app

repositories:
- name: odoo
identifier: odoo
- name: app
identifier: app

hosts:
- name: Odoo
identifier: odoo
identifier: app
urlFormat: '[label].[default-domain]'

ssh:
- usernameFormat: '[label]'
contexts: label=odoo
contexts: label=app
containerUser: odoo
editorPath: /mnt/extra-addons
stages:
- development
- usernameFormat: '[label]-root'
contexts: label=odoo
contexts: label=app
containerUser: root
editorPath: /root
stages:
- development
- usernameFormat: '[label]-[index]'
contexts: label=odoo
contexts: label=app
containerUser: odoo
stages:
- production
- usernameFormat: '[label]-[index]-root'
contexts: label=odoo
contexts: label=app
containerUser: root
stages:
- production
Expand All @@ -100,18 +100,18 @@ ssh:
logs:
- identifier: odoo-log
name: Odoo Log
labelSelector: app=odoo
labelSelector: app=app
command: tail -n 2000 -F /var/log/odoo.log

actions:
- identifier: odoo-restart
name: Restart Odoo
labelSelector: app=odoo
labelSelector: app=app
command: supervisord restart odoo

images:
- identifier: odoo
repository: odoo
- identifier: app
repository: app
dockerfile: Dockerfile
image: ghcr.io/diploi/odoo-template
stages:
Expand Down Expand Up @@ -150,4 +150,4 @@ storage:
sizeMiB: 4096

status:
url: http://odoo:3000/status
url: http://app:3000/status
13 changes: 13 additions & 0 deletions initialProject/status/shellExec.mjs
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 });
}
});
});
};
145 changes: 145 additions & 0 deletions initialProject/status/status.mjs
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();
2 changes: 1 addition & 1 deletion supervisord.conf
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ stdout_logfile=/var/log/odoo.log
stderr_logfile=/var/log/odoo.log

[program:status]
directory=/app/status
directory=/mnt/extra-addons/status
command=node status.mjs
autostart=false
autorestart=true
Expand Down
6 changes: 3 additions & 3 deletions templates/odoo-ingress.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ metadata:
spec:
tls:
- hosts:
- {{ .Values.hosts.odoo }}
- {{ .Values.hosts.app }}
secretName: tls-secret
rules:
- host: {{ .Values.hosts.odoo }}
- host: {{ .Values.hosts.app }}
http:
paths:
- path: '/'
pathType: Prefix
backend:
service:
name: odoo
name: app
port:
number: 8069
6 changes: 3 additions & 3 deletions templates/odoo-service.yaml
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
22 changes: 11 additions & 11 deletions templates/odoo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ kind: StatefulSet
kind: Deployment
{{- end }}
metadata:
name: odoo
name: app
labels:
app: odoo
app: app
spec:
selector:
matchLabels:
app: odoo
app: app
{{- if eq .Values.stage "development"}}
serviceName: odoo
serviceName: app
{{- else }}
strategy:
type: RollingUpdate
Expand All @@ -25,14 +25,14 @@ spec:
template:
metadata:
labels:
app: odoo
app: app
spec:
terminationGracePeriodSeconds: 10
imagePullSecrets:
- name: diploi-pull-secret
containers:
- name: odoo
image: {{ .Values.images.odoo }}
- name: app
image: {{ .Values.images.app }}
imagePullPolicy: Always
#command: ['sh', '-c', 'echo The app is running! && sleep 10000']
ports:
Expand All @@ -47,7 +47,7 @@ spec:
periodSeconds: 5
env:
{{- range .Values.env }}
{{- if contains "odoo" .contexts }}
{{- if contains "app" .contexts }}
- name: {{ .identifier }}
value: {{ .value | quote }}
{{- end }}
Expand All @@ -57,11 +57,11 @@ spec:
value: {{ .value | quote }}
{{- end }}
- name: REPOSITORY_URL
value: {{ .Values.repositories.odoo.url }}
value: {{ .Values.repositories.app.url }}
- name: REPOSITORY_BRANCH
value: {{ .Values.repositories.odoo.branch }}
value: {{ .Values.repositories.app.branch }}
- name: APP_PUBLIC_URL
value: {{ .Values.hosts.odoo }}
value: {{ .Values.hosts.app }}
- name: STAGE
value: {{ .Values.stage }}
- name: HOST
Expand Down

0 comments on commit 57e6088

Please sign in to comment.