Skip to content

Commit 3a2fa8d

Browse files
author
underpostnet
committed
ci(package-pwa-microservices-template-ghpkg): ⚙️ Update github repo package
1 parent 761ad53 commit 3a2fa8d

File tree

18 files changed

+218
-80
lines changed

18 files changed

+218
-80
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,8 @@
119119
"docwriter.hotkey.mac": "⌘ + .",
120120
"[dotenv]": {
121121
"editor.defaultFormatter": "foxundermoon.shell-format"
122+
},
123+
"[shellscript]": {
124+
"editor.defaultFormatter": "foxundermoon.shell-format"
122125
}
123126
}

bin/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ program
103103
.option('--info-router', 'Display router structure')
104104
.option('--expose', 'Expose service match deploy-list')
105105
.option('--info-util', 'Display kubectl util management commands')
106+
.option('--cert', 'Reset tls/ssl certificate secrets')
106107
.option('--build-manifest', 'Build kind yaml manifests: deployments, services, proxy and secrets')
108+
.option('--version', 'Set custom version')
107109
.description('Manage deployment, for default deploy development pods')
108110
.action(Underpost.deploy.callback);
109111

@@ -137,6 +139,8 @@ program
137139
.argument('[path]', 'Absolute or relative directory, for default is current')
138140
.option('--image-archive', 'Only load tar image from ./images')
139141
.option('--podman-save', 'Save image from podman to ./images')
142+
.option('--image-name <image-name>', 'Set custom image name')
143+
.option('--image-version <image-version>', 'Set custom image version')
140144
.description('Build image from Dockerfile')
141145
.action(Underpost.image.dockerfile.build);
142146

@@ -158,6 +162,11 @@ program
158162
.option('--import', 'Import container backups from repositories')
159163
.option('--export', 'Export container backups to repositories')
160164
.option('--pod-name <pod-name>', 'Optional pod context')
165+
.option('--collection <collection>', 'Collection')
166+
.option('--out-path <out-path>', 'Custom out path backup')
167+
.option('--drop', 'Drop databases')
168+
.option('--preserveUUID', 'Preserve Ids')
169+
.option('--git', 'Upload to github')
161170
.option('--ns <ns-name>', 'Optional name space context')
162171
.description('Manage databases')
163172
.action(Underpost.db.callback);
@@ -182,6 +191,7 @@ program
182191
.argument('[job-list]', `Deploy id list, e.g. ${Object.keys(Underpost.cron)}, for default all available jobs`)
183192
.option('--itc', 'Inside container execution context')
184193
.option('--init', 'Init cron jobs for cron job default deploy id')
194+
.option('--git', 'Upload to github')
185195
.description('Cron jobs management')
186196
.action(Underpost.cron.callback);
187197

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ services:
5858
cpus: '0.25'
5959
memory: 20M
6060
labels: # labels in Compose file instead of Dockerfile
61-
engine.version: '2.8.56'
61+
engine.version: '2.8.6'
6262
networks:
6363
- load-balancer
6464

manifests/mongodb/kustomization.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ resources:
77
- headless-service.yaml
88
- statefulset.yaml
99
- backup-pv-pvc.yaml
10-
- backup-cronjob.yaml
11-
- backup-access.yaml
10+
# - backup-cronjob.yaml
11+
# - backup-access.yaml

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"type": "module",
33
"main": "src/index.js",
44
"name": "@underpostnet/underpost",
5-
"version": "2.8.56",
5+
"version": "2.8.6",
66
"description": "pwa api rest template",
77
"scripts": {
88
"start": "env-cmd -f .env.production node --max-old-space-size=8192 src/server",

src/cli/cron.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class UnderpostCron {
4646
callback: async function (
4747
deployList = 'default',
4848
jobList = Object.keys(UnderpostCron.JOB),
49-
options = { itc: false, init: false },
49+
options = { itc: false, init: false, git: false },
5050
) {
5151
if (options.init === true) {
5252
await Underpost.test.setUpInfo();

src/cli/db.js

Lines changed: 101 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,26 @@ import { mergeFile, splitFileFactory } from '../server/conf.js';
22
import { loggerFactory } from '../server/logger.js';
33
import { shellExec } from '../server/process.js';
44
import fs from 'fs-extra';
5+
import UnderpostDeploy from './deploy.js';
56

67
const logger = loggerFactory(import.meta);
78

89
class UnderpostDB {
910
static API = {
10-
async callback(deployList = 'default', options = { import: false, export: false, podName: false, ns: false }) {
11+
async callback(
12+
deployList = 'default',
13+
options = {
14+
import: false,
15+
export: false,
16+
podName: false,
17+
ns: false,
18+
collection: '',
19+
outPath: '',
20+
drop: false,
21+
preserveUUID: false,
22+
git: false,
23+
},
24+
) {
1125
const newBackupTimestamp = new Date().getTime();
1226
const nameSpace = options.ns && typeof options.ns === 'string' ? options.ns : 'default';
1327
for (const _deployId of deployList.split(',')) {
@@ -43,10 +57,12 @@ class UnderpostDB {
4357
logger.info('', { hostFolder, provider, dbName });
4458

4559
const backUpPath = `../${repoName}/${hostFolder}`;
60+
if (!fs.existsSync(backUpPath)) fs.mkdirSync(backUpPath, { recursive: true });
61+
shellExec(`cd ${backUpPath} && find . -type d -empty -delete`); // delete empty folders
4662
const times = await fs.readdir(backUpPath);
47-
const currentBackupTimestamp = Math.max(...times.map((t) => parseInt(t)));
63+
const currentBackupTimestamp = Math.max(...times.map((t) => parseInt(t)).filter((t) => !isNaN(t)));
4864
dbs[provider][dbName].currentBackupTimestamp = currentBackupTimestamp;
49-
const removeBackupTimestamp = Math.min(...times.map((t) => parseInt(t)));
65+
const removeBackupTimestamp = Math.min(...times.map((t) => parseInt(t)).filter((t) => !isNaN(t)));
5066

5167
const sqlContainerPath = `/home/${dbName}.sql`;
5268
const _fromPartsParts = `../${repoName}/${hostFolder}/${currentBackupTimestamp}/${dbName}-parths.json`;
@@ -68,59 +84,105 @@ class UnderpostDB {
6884
}
6985

7086
if (options.export === true && times.length >= 5) {
87+
logger.info('remove', `../${repoName}/${hostFolder}/${removeBackupTimestamp}`);
7188
fs.removeSync(`../${repoName}/${hostFolder}/${removeBackupTimestamp}`);
89+
logger.info('create', `../${repoName}/${hostFolder}/${newBackupTimestamp}`);
7290
fs.mkdirSync(`../${repoName}/${hostFolder}/${newBackupTimestamp}`, { recursive: true });
7391
}
7492

7593
switch (provider) {
7694
case 'mariadb': {
77-
const podName =
78-
options.podName && typeof options.podName === 'string' ? options.podName : `mariadb-statefulset-0`;
95+
const podNames =
96+
options.podName && typeof options.podName === 'string'
97+
? options.podName.split(',')
98+
: UnderpostDeploy.API.get('mariadb'); // `mariadb-statefulset-0`;
7999
const serviceName = 'mariadb';
80-
if (options.import === true) {
81-
shellExec(`sudo kubectl cp ${_toSqlPath} ${nameSpace}/${podName}:/${dbName}.sql`);
82-
const cmd = `mariadb -u ${user} -p${password} ${dbName} < /${dbName}.sql`;
83-
shellExec(
84-
`kubectl exec -i ${podName} -- ${serviceName} -p${password} -e 'CREATE DATABASE ${dbName};'`,
85-
);
86-
shellExec(`sudo kubectl exec -i ${podName} -- sh -c "${cmd}"`);
87-
}
88-
if (options.export === true) {
89-
const cmd = `mariadb-dump --user=${user} --password=${password} --lock-tables ${dbName} > ${sqlContainerPath}`;
90-
shellExec(`sudo kubectl exec -i ${podName} -- sh -c "${cmd}"`);
91-
shellExec(`sudo kubectl cp ${nameSpace}/${podName}:${sqlContainerPath} ${_toNewSqlPath}`);
92-
await splitFileFactory(dbName, _toNewSqlPath);
100+
for (const podNameData of [podNames[0]]) {
101+
const podName = podNameData.NAME;
102+
if (options.import === true) {
103+
shellExec(`sudo kubectl exec -i ${podName} -- sh -c "rm -rf /${dbName}.sql"`);
104+
shellExec(`sudo kubectl cp ${_toSqlPath} ${nameSpace}/${podName}:/${dbName}.sql`);
105+
const cmd = `mariadb -u ${user} -p${password} ${dbName} < /${dbName}.sql`;
106+
shellExec(
107+
`kubectl exec -i ${podName} -- ${serviceName} -p${password} -e 'CREATE DATABASE ${dbName};'`,
108+
);
109+
shellExec(`sudo kubectl exec -i ${podName} -- sh -c "${cmd}"`);
110+
}
111+
if (options.export === true) {
112+
shellExec(`sudo kubectl exec -i ${podName} -- sh -c "rm -rf ${sqlContainerPath}"`);
113+
const cmd = `mariadb-dump --user=${user} --password=${password} --lock-tables ${dbName} > ${sqlContainerPath}`;
114+
shellExec(`sudo kubectl exec -i ${podName} -- sh -c "${cmd}"`);
115+
shellExec(
116+
`sudo kubectl cp ${nameSpace}/${podName}:${sqlContainerPath} ${
117+
options.outPath ? options.outPath : _toNewSqlPath
118+
}`,
119+
);
120+
await splitFileFactory(dbName, options.outPath ? options.outPath : _toNewSqlPath);
121+
}
93122
}
94123
break;
95124
}
96125

97126
case 'mongoose': {
98127
if (options.import === true) {
99-
const podName =
100-
options.podName && typeof options.podName === 'string' ? options.podName : `mongodb-0`;
101-
shellExec(`sudo kubectl cp ${_toBsonPath} ${nameSpace}/${podName}:/${dbName}`);
102-
const cmd = `mongorestore -d ${dbName} /${dbName}`;
103-
shellExec(`sudo kubectl exec -i ${podName} -- sh -c "${cmd}"`);
128+
const podNames =
129+
options.podName && typeof options.podName === 'string'
130+
? options.podName.split(',')
131+
: UnderpostDeploy.API.get('mongo');
132+
// `mongodb-0`;
133+
for (const podNameData of [podNames[0]]) {
134+
const podName = podNameData.NAME;
135+
shellExec(`sudo kubectl exec -i ${podName} -- sh -c "rm -rf /${dbName}"`);
136+
shellExec(
137+
`sudo kubectl cp ${
138+
options.outPath ? options.outPath : _toBsonPath
139+
} ${nameSpace}/${podName}:/${dbName}`,
140+
);
141+
const cmd = `mongorestore -d ${dbName} /${dbName}${options.drop ? ' --drop' : ''}${
142+
options.preserveUUID ? ' --preserveUUID' : ''
143+
}`;
144+
shellExec(`sudo kubectl exec -i ${podName} -- sh -c "${cmd}"`);
145+
}
104146
}
105147
if (options.export === true) {
106-
const podName = `backup-access`;
107-
const containerBaseBackupPath = '/backup';
108-
let timeFolder = shellExec(
109-
`sudo kubectl exec -i ${podName} -- sh -c "cd ${containerBaseBackupPath} && ls -a"`,
110-
{
111-
stdout: true,
112-
disableLog: false,
113-
silent: true,
114-
},
115-
).split(`\n`);
116-
timeFolder = timeFolder[timeFolder.length - 2];
117-
if (timeFolder === '..') {
118-
logger.warn(`Cannot backup available`, { timeFolder });
119-
} else {
148+
const podNames =
149+
options.podName && typeof options.podName === 'string'
150+
? options.podName.split(',')
151+
: UnderpostDeploy.API.get('mongo'); // `backup-access`;
152+
for (const podNameData of [podNames[0]]) {
153+
const podName = podNameData.NAME;
154+
shellExec(`sudo kubectl exec -i ${podName} -- sh -c "rm -rf /${dbName}"`);
155+
if (options.collections)
156+
for (const collection of options.collections)
157+
shellExec(
158+
`sudo kubectl exec -i ${podName} -- sh -c "mongodump -d ${dbName} --collection ${collection} -o /${dbName}"`,
159+
);
160+
else shellExec(`sudo kubectl exec -i ${podName} -- sh -c "mongodump -d ${dbName} -o /${dbName}"`);
120161
shellExec(
121-
`sudo kubectl cp ${nameSpace}/${podName}:${containerBaseBackupPath}/${timeFolder}/${dbName} ${_toNewBsonPath}`,
162+
`sudo kubectl cp ${nameSpace}/${podName}:/${dbName} ${
163+
options.outPath ? options.outPath : _toNewBsonPath
164+
}`,
122165
);
123166
}
167+
if (false) {
168+
const containerBaseBackupPath = '/backup';
169+
let timeFolder = shellExec(
170+
`sudo kubectl exec -i ${podName} -- sh -c "cd ${containerBaseBackupPath} && ls -a"`,
171+
{
172+
stdout: true,
173+
disableLog: false,
174+
silent: true,
175+
},
176+
).split(`\n`);
177+
timeFolder = timeFolder[timeFolder.length - 2];
178+
if (timeFolder === '..') {
179+
logger.warn(`Cannot backup available`, { timeFolder });
180+
} else {
181+
shellExec(
182+
`sudo kubectl cp ${nameSpace}/${podName}:${containerBaseBackupPath}/${timeFolder}/${dbName} ${_toNewBsonPath}`,
183+
);
184+
}
185+
}
124186
}
125187
break;
126188
}
@@ -131,7 +193,7 @@ class UnderpostDB {
131193
}
132194
}
133195
}
134-
if (options.export === true) {
196+
if (options.export === true && options.git === true) {
135197
shellExec(`cd ../${repoName} && git add .`);
136198
shellExec(
137199
`underpost cmt ../${repoName} backup '' '${new Date(newBackupTimestamp).toLocaleDateString()} ${new Date(

src/cli/deploy.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class UnderpostDeploy {
3232
await Config.build(undefined, 'proxy', deployList);
3333
return buildPortProxyRouter(env === 'development' ? 80 : 443, buildProxyRouter());
3434
},
35-
async buildManifest(deployList, env) {
35+
async buildManifest(deployList, env, version) {
3636
for (const _deployId of deployList.split(',')) {
3737
const deployId = _deployId.trim();
3838
if (!deployId) continue;
@@ -69,7 +69,18 @@ spec:
6969
spec:
7070
containers:
7171
- name: ${deployId}-${env}
72-
image: localhost/${deployId}-${env}:${Underpost.version}
72+
image: localhost/underpost-engine:${version && typeof version === 'string' ? version : Underpost.version}
73+
lifecycle:
74+
postStart:
75+
exec:
76+
command:
77+
- /bin/sh
78+
- -c
79+
- >
80+
sleep 60 &&
81+
underpost config set deploy-id ${deployId} &&
82+
underpost config set deploy-env ${env}
83+
# image: localhost/${deployId}-${env}:${version && typeof version === 'string' ? version : Underpost.version}
7384
---
7485
apiVersion: v1
7586
kind: Service
@@ -180,7 +191,16 @@ spec:
180191
async callback(
181192
deployList = 'default',
182193
env = 'development',
183-
options = { remove: false, infoRouter: false, sync: false, buildManifest: false, infoUtil: false, expose: false },
194+
options = {
195+
remove: false,
196+
infoRouter: false,
197+
sync: false,
198+
buildManifest: false,
199+
infoUtil: false,
200+
expose: false,
201+
cert: false,
202+
version: '',
203+
},
184204
) {
185205
if (options.infoUtil === true)
186206
return logger.info(`
@@ -191,7 +211,7 @@ kubectl scale statefulsets <stateful-set-name> --replicas=<new-replicas>
191211
if (deployList === 'dd' && fs.existsSync(`./engine-private/deploy/dd.router`))
192212
deployList = fs.readFileSync(`./engine-private/deploy/dd.router`, 'utf8');
193213
if (options.sync) UnderpostDeploy.API.sync(deployList);
194-
if (options.buildManifest === true) await UnderpostDeploy.API.buildManifest(deployList, env);
214+
if (options.buildManifest === true) await UnderpostDeploy.API.buildManifest(deployList, env, options.version);
195215
if (options.infoRouter === true)
196216
return logger.info('router', await UnderpostDeploy.API.routerFactory(deployList, env));
197217
const etcHost = (
@@ -219,7 +239,7 @@ kubectl scale statefulsets <stateful-set-name> --replicas=<new-replicas>
219239
const confServer = JSON.parse(fs.readFileSync(`./engine-private/conf/${deployId}/conf.server.json`, 'utf8'));
220240
for (const host of Object.keys(confServer)) {
221241
shellExec(`sudo kubectl delete HTTPProxy ${host}`);
222-
if (env === 'production') shellExec(`sudo kubectl delete Certificate ${host}`);
242+
if (env === 'production' && options.cert === true) shellExec(`sudo kubectl delete Certificate ${host}`);
223243
if (!options.remove === true && env === 'development') concatHots += ` ${host}`;
224244
}
225245

@@ -231,7 +251,8 @@ kubectl scale statefulsets <stateful-set-name> --replicas=<new-replicas>
231251
if (!options.remove === true) {
232252
shellExec(`sudo kubectl apply -f ./${manifestsPath}/deployment.yaml`);
233253
shellExec(`sudo kubectl apply -f ./${manifestsPath}/proxy.yaml`);
234-
if (env === 'production') shellExec(`sudo kubectl apply -f ./${manifestsPath}/secret.yaml`);
254+
if (env === 'production' && options.cert === true)
255+
shellExec(`sudo kubectl apply -f ./${manifestsPath}/secret.yaml`);
235256
}
236257
}
237258
let renderHosts;

0 commit comments

Comments
 (0)