c3dd2a1a22a63979811989d7b01a1756a3e131f4
[motion2.git] / server / api / pm2 / pm2.controller.js
1 // *************************************************************************
2 // *                                                                       *
3 // * xCALLY Motion -  The Omnichannel Contact Center                       *
4 // * Copyright (c) Xenialab s.r.l. All Rights Reserved                     *
5 // *                                                                       *
6 // *************************************************************************
7 // *                                                                       *
8 // * Email: info@xcally.com                                                *
9 // * Website: https://www.xcally.com                                       *
10 // *                                                                       *
11 // *************************************************************************
12 // *                                                                       *
13 // * The SOFTWARE PRODUCT is protected by copyright laws and international *
14 // * copyright treaties, as well as other intellectual property laws and   *
15 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                         *
16 // *                                                                       *
17 // *************************************************************************
18 var _0x8d69=['delete','Unknown\x20id','lodash','../../config/logger','api','path','child_process','exec','bluebird','sendStatus','status','json','stack','name','npm\x20install','info','index','then','reject','express','motion2','length','catch','show','describe','params','create','body','script','join','../../files/plugins','chdir','dump','send','Missing\x20name\x20and/or\x20path','stop','hasOwnProperty','destroy'];(function(_0x2561e3,_0x54bfae){var _0x6da30=function(_0x3de592){while(--_0x3de592){_0x2561e3['push'](_0x2561e3['shift']());}};_0x6da30(++_0x54bfae);}(_0x8d69,0x74));var _0x98d6=function(_0x4bbdff,_0x380dab){_0x4bbdff=_0x4bbdff-0x0;var _0x4cf619=_0x8d69[_0x4bbdff];return _0x4cf619;};'use strict';var _=require(_0x98d6('0x0'));var pm2=require('../../config/pm2');var logger=require(_0x98d6('0x1'))(_0x98d6('0x2'));var path=require(_0x98d6('0x3'));var exec=require(_0x98d6('0x4'))[_0x98d6('0x5')];var BPromise=require(_0x98d6('0x6'));function handleEntityNotFound(_0x546e9b,_0x3bd283){return function(_0x17e859){if(!_0x17e859){_0x546e9b[_0x98d6('0x7')](0x194);;}return _0x17e859;};}function respondWithResult(_0x25ec8a,_0x4c14dd){_0x4c14dd=_0x4c14dd||0xc8;return function(_0x41aeb1){if(_0x41aeb1){return _0x25ec8a[_0x98d6('0x8')](_0x4c14dd)[_0x98d6('0x9')](_0x41aeb1);}return null;};}function handleError(_0x2ad86d,_0x101311){_0x101311=_0x101311||0x1f4;return function(_0x38f082){logger['error'](_0x38f082[_0x98d6('0xa')]);if(_0x38f082[_0x98d6('0xb')]){delete _0x38f082[_0x98d6('0xb')];}_0x2ad86d[_0x98d6('0x8')](_0x101311)['send'](_0x38f082);};}function execChildProcess(){return new BPromise(function(_0x486fe1,_0x1bd853){exec(_0x98d6('0xc'),function(_0x1be6b5,_0x28179d,_0x21953b){logger['info'](_0x28179d);logger[_0x98d6('0xd')](_0x21953b);if(_0x1be6b5){logger['error'](_0x1be6b5);return _0x1bd853(_0x1be6b5);}return _0x486fe1(_0x28179d);});});}exports[_0x98d6('0xe')]=function(_0x2609f6,_0x57558f){return pm2['list']()[_0x98d6('0xf')](function(_0x1c853d){if(_0x1c853d){var _0x3fdcc4=_[_0x98d6('0x10')](_0x1c853d,function(_0x55897f){return _0x55897f[_0x98d6('0xb')]===_0x98d6('0x11')||_0x55897f[_0x98d6('0xb')]===_0x98d6('0x12');});return{'rows':_0x3fdcc4,'count':_0x3fdcc4[_0x98d6('0x13')]};}return null;})['then'](respondWithResult(_0x57558f,null))[_0x98d6('0x14')](handleError(_0x57558f,null));};exports[_0x98d6('0x15')]=function(_0x559fb8,_0x14b6c3){return pm2[_0x98d6('0x16')](_0x559fb8[_0x98d6('0x17')]['id'])[_0x98d6('0xf')](respondWithResult(_0x14b6c3,null))[_0x98d6('0xf')](handleEntityNotFound(_0x14b6c3,null))[_0x98d6('0x14')](handleError(_0x14b6c3,null));};exports[_0x98d6('0x18')]=function(_0x905384,_0x5798e6){if(_0x905384[_0x98d6('0x19')][_0x98d6('0xb')]&&_0x905384['body'][_0x98d6('0x1a')]){var _0x2bf1de=path[_0x98d6('0x1b')](__dirname,_0x98d6('0x1c'),_0x905384[_0x98d6('0x19')][_0x98d6('0x1a')]);var _0x4feb3b=process['cwd']();process[_0x98d6('0x1d')](path['dirname'](_0x2bf1de));return execChildProcess()['then'](function(_0x5d607b){process['chdir'](_0x4feb3b);return pm2['start']({'name':_0x905384[_0x98d6('0x19')][_0x98d6('0xb')],'script':_0x2bf1de});})[_0x98d6('0xf')](function(_0x5d472a){pm2[_0x98d6('0x1e')]();return _0x5d472a[0x0];})[_0x98d6('0xf')](respondWithResult(_0x5798e6,0xc9))[_0x98d6('0x14')](handleError(_0x5798e6,null));}else{return _0x5798e6[_0x98d6('0x8')](0x1f4)[_0x98d6('0x1f')]({'message':_0x98d6('0x20')});}};exports['update']=function(_0x595088,_0x41a408){if(_0x595088[_0x98d6('0x19')]['id']){delete _0x595088[_0x98d6('0x19')]['id'];}var _0x2f357a={'online':'restart','stopped':_0x98d6('0x21')};if(_0x2f357a[_0x98d6('0x22')](_0x595088[_0x98d6('0x19')][_0x98d6('0x8')])){return pm2[_0x2f357a[_0x595088[_0x98d6('0x19')][_0x98d6('0x8')]]](_0x595088['params']['id'])[_0x98d6('0xf')](respondWithResult(_0x41a408,0xc8))[_0x98d6('0x14')](handleError(_0x41a408,null));}else{return _0x41a408[_0x98d6('0x8')](0x1f4)[_0x98d6('0x1f')]({'message':'Unknown\x20status'});}};exports[_0x98d6('0x23')]=function(_0x371617,_0xf0a48f){if(_0x371617[_0x98d6('0x19')]['id']){delete _0x371617[_0x98d6('0x19')]['id'];}if(_0x371617[_0x98d6('0x17')]['id']){return pm2[_0x98d6('0x24')](_0x371617['params']['id'])[_0x98d6('0xf')](function(_0x17e7d8){pm2[_0x98d6('0x1e')]();return _0x17e7d8[0x0];})[_0x98d6('0xf')](respondWithResult(_0xf0a48f,null))['catch'](handleError(_0xf0a48f,null));}else{return _0xf0a48f[_0x98d6('0x8')](0x1f4)[_0x98d6('0x1f')]({'message':_0x98d6('0x25')});}};