33c1cb9196213ab93163dbfad22823c7e94634b7
[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 _0xbb53=['send','Missing\x20name\x20and/or\x20path','update','restart','stop','hasOwnProperty','destroy','delete','Unknown\x20id','lodash','../../config/pm2','../../config/logger','api','path','child_process','exec','bluebird','status','json','error','stack','name','npm\x20install','info','index','then','reject','motion2','length','catch','show','params','body','script','join','../../files/plugins','cwd','chdir','dirname','dump'];(function(_0x374d10,_0x3299a3){var _0x43d792=function(_0xfa7380){while(--_0xfa7380){_0x374d10['push'](_0x374d10['shift']());}};_0x43d792(++_0x3299a3);}(_0xbb53,0x121));var _0x3bb5=function(_0x2fa681,_0x5074bc){_0x2fa681=_0x2fa681-0x0;var _0x36f2ab=_0xbb53[_0x2fa681];return _0x36f2ab;};'use strict';var _=require(_0x3bb5('0x0'));var pm2=require(_0x3bb5('0x1'));var logger=require(_0x3bb5('0x2'))(_0x3bb5('0x3'));var path=require(_0x3bb5('0x4'));var exec=require(_0x3bb5('0x5'))[_0x3bb5('0x6')];var BPromise=require(_0x3bb5('0x7'));function handleEntityNotFound(_0x83e44b,_0x450d9e){return function(_0x55fdaf){if(!_0x55fdaf){_0x83e44b['sendStatus'](0x194);;}return _0x55fdaf;};}function respondWithResult(_0x23f11e,_0x2460b0){_0x2460b0=_0x2460b0||0xc8;return function(_0x28aa7d){if(_0x28aa7d){return _0x23f11e[_0x3bb5('0x8')](_0x2460b0)[_0x3bb5('0x9')](_0x28aa7d);}return null;};}function handleError(_0x480f9c,_0x5a4ae3){_0x5a4ae3=_0x5a4ae3||0x1f4;return function(_0x4cfdd7){logger[_0x3bb5('0xa')](_0x4cfdd7[_0x3bb5('0xb')]);if(_0x4cfdd7[_0x3bb5('0xc')]){delete _0x4cfdd7['name'];}_0x480f9c[_0x3bb5('0x8')](_0x5a4ae3)['send'](_0x4cfdd7);};}function execChildProcess(){return new BPromise(function(_0x53af18,_0xd8d9bf){exec(_0x3bb5('0xd'),function(_0x5cfc6b,_0xe00ace,_0x511e41){logger[_0x3bb5('0xe')](_0xe00ace);logger['info'](_0x511e41);if(_0x5cfc6b){logger['error'](_0x5cfc6b);return _0xd8d9bf(_0x5cfc6b);}return _0x53af18(_0xe00ace);});});}exports[_0x3bb5('0xf')]=function(_0x35a99b,_0x1e46a5){return pm2['list']()[_0x3bb5('0x10')](function(_0x12e409){if(_0x12e409){var _0x519123=_[_0x3bb5('0x11')](_0x12e409,function(_0x15c369){return _0x15c369['name']==='express'||_0x15c369['name']===_0x3bb5('0x12');});return{'rows':_0x519123,'count':_0x519123[_0x3bb5('0x13')]};}return null;})['then'](respondWithResult(_0x1e46a5,null))[_0x3bb5('0x14')](handleError(_0x1e46a5,null));};exports[_0x3bb5('0x15')]=function(_0x4656c4,_0x262059){return pm2['describe'](_0x4656c4[_0x3bb5('0x16')]['id'])[_0x3bb5('0x10')](respondWithResult(_0x262059,null))[_0x3bb5('0x10')](handleEntityNotFound(_0x262059,null))['catch'](handleError(_0x262059,null));};exports['create']=function(_0x19b177,_0x478021){if(_0x19b177[_0x3bb5('0x17')]['name']&&_0x19b177[_0x3bb5('0x17')][_0x3bb5('0x18')]){var _0x3f94ad=path[_0x3bb5('0x19')](__dirname,_0x3bb5('0x1a'),_0x19b177[_0x3bb5('0x17')]['script']);var _0x37444e=process[_0x3bb5('0x1b')]();process[_0x3bb5('0x1c')](path[_0x3bb5('0x1d')](_0x3f94ad));return execChildProcess()[_0x3bb5('0x10')](function(_0xfb9e8){process[_0x3bb5('0x1c')](_0x37444e);return pm2['start']({'name':_0x19b177[_0x3bb5('0x17')][_0x3bb5('0xc')],'script':_0x3f94ad});})['then'](function(_0x4fc81b){pm2[_0x3bb5('0x1e')]();return _0x4fc81b[0x0];})[_0x3bb5('0x10')](respondWithResult(_0x478021,0xc9))['catch'](handleError(_0x478021,null));}else{return _0x478021['status'](0x1f4)[_0x3bb5('0x1f')]({'message':_0x3bb5('0x20')});}};exports[_0x3bb5('0x21')]=function(_0xc1c149,_0x57abf4){if(_0xc1c149['body']['id']){delete _0xc1c149[_0x3bb5('0x17')]['id'];}var _0x4e8c65={'online':_0x3bb5('0x22'),'stopped':_0x3bb5('0x23')};if(_0x4e8c65[_0x3bb5('0x24')](_0xc1c149['body']['status'])){return pm2[_0x4e8c65[_0xc1c149[_0x3bb5('0x17')][_0x3bb5('0x8')]]](_0xc1c149[_0x3bb5('0x16')]['id'])[_0x3bb5('0x10')](respondWithResult(_0x57abf4,0xc8))[_0x3bb5('0x14')](handleError(_0x57abf4,null));}else{return _0x57abf4[_0x3bb5('0x8')](0x1f4)[_0x3bb5('0x1f')]({'message':'Unknown\x20status'});}};exports[_0x3bb5('0x25')]=function(_0xa643c9,_0x5ebd0a){if(_0xa643c9[_0x3bb5('0x17')]['id']){delete _0xa643c9[_0x3bb5('0x17')]['id'];}if(_0xa643c9[_0x3bb5('0x16')]['id']){return pm2[_0x3bb5('0x26')](_0xa643c9['params']['id'])[_0x3bb5('0x10')](function(_0x25ba4c){pm2['dump']();return _0x25ba4c[0x0];})[_0x3bb5('0x10')](respondWithResult(_0x5ebd0a,null))[_0x3bb5('0x14')](handleError(_0x5ebd0a,null));}else{return _0x5ebd0a[_0x3bb5('0x8')](0x1f4)[_0x3bb5('0x1f')]({'message':_0x3bb5('0x27')});}};