myt/myt-start.js

60 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2022-12-21 13:17:50 +00:00
const Myt = require('./myt');
const Command = require('./lib/command');
const Container = require('./lib/docker').Container;
const Server = require('./lib/server');
2022-12-21 13:17:50 +00:00
const Run = require('./myt-run');
2020-12-02 07:35:26 +00:00
/**
* Does the minium effort to start the database container, if it doesn't
* exists calls the run command, if it is started does nothing. Keep in
* mind that when you do not rebuild the docker you may be using an outdated
* version of it.
*/
class Start extends Command {
static usage = {
description: 'Start local database server container'
};
static reporter = {
startingContainer: 'Starting container.'
};
2022-12-21 13:17:50 +00:00
async run(myt, opts) {
2020-12-04 09:15:29 +00:00
const ct = new Container(opts.code);
2020-12-02 07:35:26 +00:00
let status;
let exists;
let server;
2020-12-04 09:15:29 +00:00
2020-12-02 07:35:26 +00:00
try {
2020-12-04 09:15:29 +00:00
status = await ct.inspect({
format: '{{json .State.Status}}'
2020-12-02 07:35:26 +00:00
});
exists = true;
2020-12-02 07:35:26 +00:00
} catch (err) {
server = await myt.run(Run, opts);
2020-12-02 07:35:26 +00:00
}
if (exists) {
switch (status) {
case 'running':
break;
case 'exited':
this.emit('startingContainer');
await ct.start();
server = new Server(ct, opts.dbConfig);
await server.wait();
break;
default:
throw new Error(`Unknown docker status: ${status}`);
}
2020-12-02 07:35:26 +00:00
}
2020-12-04 09:15:29 +00:00
return server;
2020-12-02 07:35:26 +00:00
}
}
module.exports = Start;
if (require.main === module)
new Myt().cli(Start);