myt/myvc-start.js

47 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-12-02 07:35:26 +00:00
2020-12-04 16:30:26 +00:00
const MyVC = require('./myvc');
2020-12-04 09:15:29 +00:00
const Container = require('./docker').Container;
2020-12-02 07:35:26 +00:00
const Server = require('./server/server');
2020-12-04 09:15:29 +00:00
const Run = require('./myvc-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 {
async run(myvc, 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;
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
});
} catch (err) {
2020-12-04 09:15:29 +00:00
const run = new Run()
return await run.run(myvc, opts);
2020-12-02 07:35:26 +00:00
}
switch (status) {
case 'running':
2020-12-04 09:15:29 +00:00
break;
2020-12-02 07:35:26 +00:00
case 'exited':
2020-12-04 09:15:29 +00:00
await ct.start();
break;
2020-12-02 07:35:26 +00:00
default:
throw new Error(`Unknown docker status: ${status}`);
}
2020-12-04 09:15:29 +00:00
const server = new Server(ct, opts.dbConfig);
await server.wait();
return server;
2020-12-02 07:35:26 +00:00
}
}
module.exports = Start;
if (require.main === module)
new MyVC().run(Start);