UNPKG

874 BJavaScriptView Raw
1
2const util = require('util')
3const spawn = require('child_process').spawn
4const DockerMgmt = {}
5
6module.exports = DockerMgmt
7
8function execDocker(cmd, cb) {
9 var i = spawn('docker', cmd, {
10 stdio : 'inherit',
11 env: process.env,
12 shell : true
13 })
14
15 i.on('close', cb)
16}
17
18DockerMgmt.processCommand = function(PM2, start_id, select_id, action, cb) {
19 PM2.Client.executeRemote('getSystemData', {}, (err, sys_infos) => {
20 if (sys_infos.containers && sys_infos.containers.length == 0)
21 return cb(new Error(`Process ${select_id} not found`))
22 var container = sys_infos.containers[select_id - start_id - 1]
23 if (action == 'stopProcessId')
24 execDocker(['stop', container.id], cb)
25 if (action == 'deleteProcessId')
26 execDocker(['rm', container.id], cb)
27 if (action == 'restartProcessId')
28 execDocker(['restart', container.id], cb)
29 })
30}