Docker.js 874 B

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