refactor: persist docker

This commit is contained in:
cha0s 2021-01-24 10:41:04 -06:00
parent d3d4458845
commit 97be0919b7
2 changed files with 30 additions and 16 deletions

View File

@ -1,21 +1,24 @@
import {spawn} from 'child_process'; import {spawn, spawnSync} from 'child_process';
import mkdirp from 'mkdirp'; import mkdirp from 'mkdirp';
export default function createDockerContainer(latus) { export default function createDockerContainer(latus) {
const {id} = latus.get('@latus/core');
const { const {
config: { database,
'@latus/db/server': { docker,
database, password,
docker, port,
password, } = latus.get('@latus/db/server');
port, const name = `${id}_sequelize`;
}, const {output} = spawnSync(`docker container inspect -f '{{.State.Running}}' ${name}`);
}, if ('true' === output) {
} = latus; return;
}
const args = [ const args = [
'run', 'run',
'--rm', '--rm',
'--name', name,
'-p', `${port}:3306`, '-p', `${port}:3306`,
'-e', `MYSQL_DATABASE=${database}`, '-e', `MYSQL_DATABASE=${database}`,
'-e', `MYSQL_ROOT_PASSWORD=${password}`, '-e', `MYSQL_ROOT_PASSWORD=${password}`,
@ -32,6 +35,8 @@ export default function createDockerContainer(latus) {
'mysql', 'mysql',
'--default-authentication-plugin=mysql_native_password', '--default-authentication-plugin=mysql_native_password',
); );
const {pid} = spawn('docker', args, {stdio: 'ignore'}); spawn('docker', args, {
process.on('exit', () => process.kill(pid)); detached: true,
stdio: 'ignore',
}).unref();
} }

View File

@ -1,6 +1,6 @@
import D from 'debug'; import D from 'debug';
import {spawn} from 'child_process'; import {spawn, spawnSync} from 'child_process';
import mkdirp from 'mkdirp'; import mkdirp from 'mkdirp';
@ -9,10 +9,17 @@ import createClient from './create-client';
const debug = D('@latus/redis/docker'); const debug = D('@latus/redis/docker');
export default async function createDockerContainer(latus) { export default async function createDockerContainer(latus) {
const {config: {'@latus/redis/server': {docker, port}}} = latus; const {id} = latus.get('@latus/core');
const {docker, port} = latus.get('@latus/redis/server');
const name = `${id}_redis`;
const {output} = spawnSync(`docker container inspect -f '{{.State.Running}}' ${name}`);
if ('true' === output) {
return;
}
const args = [ const args = [
'run', 'run',
'--rm', '--rm',
'--name', name,
'-p', `${port}:6379`, '-p', `${port}:6379`,
]; ];
if ('cached' === docker) { if ('cached' === docker) {
@ -26,8 +33,10 @@ export default async function createDockerContainer(latus) {
args.push( args.push(
'redis', 'redis',
); );
const {pid} = spawn('docker', args, {stdio: 'ignore'}); spawn('docker', args, {
process.on('exit', () => process.kill(pid)); detached: true,
stdio: 'ignore',
}).unref();
// eslint-disable-next-line no-constant-condition // eslint-disable-next-line no-constant-condition
while (true) { while (true) {
const client = createClient(latus); const client = createClient(latus);