70eff740489473b170c7a8d1faa5dd7b1239ba63
[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 _0xfb80=['destroy','delete','dump','../../config/pm2','../../config/logger','api','path','exec','sendStatus','json','error','name','status','send','npm\x20install','info','index','list','then','reject','express','length','catch','show','describe','body','script','chdir','Missing\x20name\x20and/or\x20path','restart','stop','params'];(function(_0x36d203,_0x3653f3){var _0x2f9e62=function(_0x461a77){while(--_0x461a77){_0x36d203['push'](_0x36d203['shift']());}};_0x2f9e62(++_0x3653f3);}(_0xfb80,0x1e3));var _0x0fb8=function(_0x30d061,_0x54ed28){_0x30d061=_0x30d061-0x0;var _0x16c8fc=_0xfb80[_0x30d061];return _0x16c8fc;};'use strict';var _=require('lodash');var pm2=require(_0x0fb8('0x0'));var logger=require(_0x0fb8('0x1'))(_0x0fb8('0x2'));var path=require(_0x0fb8('0x3'));var exec=require('child_process')[_0x0fb8('0x4')];var BPromise=require('bluebird');function handleEntityNotFound(_0x37df34,_0x442c93){return function(_0xdd4b45){if(!_0xdd4b45){_0x37df34[_0x0fb8('0x5')](0x194);;}return _0xdd4b45;};}function respondWithResult(_0xb4ba40,_0x25d761){_0x25d761=_0x25d761||0xc8;return function(_0x1229fe){if(_0x1229fe){return _0xb4ba40['status'](_0x25d761)[_0x0fb8('0x6')](_0x1229fe);}return null;};}function handleError(_0x1b7945,_0x5d0d31){_0x5d0d31=_0x5d0d31||0x1f4;return function(_0x623ebe){logger[_0x0fb8('0x7')](_0x623ebe['stack']);if(_0x623ebe[_0x0fb8('0x8')]){delete _0x623ebe[_0x0fb8('0x8')];}_0x1b7945[_0x0fb8('0x9')](_0x5d0d31)[_0x0fb8('0xa')](_0x623ebe);};}function execChildProcess(){return new BPromise(function(_0x522f0c,_0x17f713){exec(_0x0fb8('0xb'),function(_0x309af5,_0x53745e,_0x2d4e1c){logger[_0x0fb8('0xc')](_0x53745e);logger[_0x0fb8('0xc')](_0x2d4e1c);if(_0x309af5){logger['error'](_0x309af5);return _0x17f713(_0x309af5);}return _0x522f0c(_0x53745e);});});}exports[_0x0fb8('0xd')]=function(_0x4b6637,_0x6a5484){return pm2[_0x0fb8('0xe')]()[_0x0fb8('0xf')](function(_0xf5b461){if(_0xf5b461){var _0x2094b8=_[_0x0fb8('0x10')](_0xf5b461,function(_0x454890){return _0x454890[_0x0fb8('0x8')]===_0x0fb8('0x11')||_0x454890[_0x0fb8('0x8')]==='motion2';});return{'rows':_0x2094b8,'count':_0x2094b8[_0x0fb8('0x12')]};}return null;})['then'](respondWithResult(_0x6a5484,null))[_0x0fb8('0x13')](handleError(_0x6a5484,null));};exports[_0x0fb8('0x14')]=function(_0x35f3d3,_0x3be5df){return pm2[_0x0fb8('0x15')](_0x35f3d3['params']['id'])[_0x0fb8('0xf')](respondWithResult(_0x3be5df,null))['then'](handleEntityNotFound(_0x3be5df,null))['catch'](handleError(_0x3be5df,null));};exports['create']=function(_0x628829,_0x16319b){if(_0x628829[_0x0fb8('0x16')][_0x0fb8('0x8')]&&_0x628829['body'][_0x0fb8('0x17')]){var _0x231934=path['join'](__dirname,'../../files/plugins',_0x628829[_0x0fb8('0x16')]['script']);var _0x58eedf=process['cwd']();process[_0x0fb8('0x18')](path['dirname'](_0x231934));return execChildProcess()[_0x0fb8('0xf')](function(_0x3c6e58){process[_0x0fb8('0x18')](_0x58eedf);return pm2['start']({'name':_0x628829[_0x0fb8('0x16')][_0x0fb8('0x8')],'script':_0x231934});})['then'](function(_0x5c8553){pm2['dump']();return _0x5c8553[0x0];})[_0x0fb8('0xf')](respondWithResult(_0x16319b,0xc9))[_0x0fb8('0x13')](handleError(_0x16319b,null));}else{return _0x16319b[_0x0fb8('0x9')](0x1f4)[_0x0fb8('0xa')]({'message':_0x0fb8('0x19')});}};exports['update']=function(_0x25a7f0,_0x4368fe){if(_0x25a7f0['body']['id']){delete _0x25a7f0['body']['id'];}var _0x4e87c8={'online':_0x0fb8('0x1a'),'stopped':_0x0fb8('0x1b')};if(_0x4e87c8['hasOwnProperty'](_0x25a7f0[_0x0fb8('0x16')][_0x0fb8('0x9')])){return pm2[_0x4e87c8[_0x25a7f0[_0x0fb8('0x16')]['status']]](_0x25a7f0[_0x0fb8('0x1c')]['id'])[_0x0fb8('0xf')](respondWithResult(_0x4368fe,0xc8))[_0x0fb8('0x13')](handleError(_0x4368fe,null));}else{return _0x4368fe[_0x0fb8('0x9')](0x1f4)[_0x0fb8('0xa')]({'message':'Unknown\x20status'});}};exports[_0x0fb8('0x1d')]=function(_0x1342b9,_0x129819){if(_0x1342b9[_0x0fb8('0x16')]['id']){delete _0x1342b9[_0x0fb8('0x16')]['id'];}if(_0x1342b9['params']['id']){return pm2[_0x0fb8('0x1e')](_0x1342b9[_0x0fb8('0x1c')]['id'])[_0x0fb8('0xf')](function(_0x1e91f3){pm2[_0x0fb8('0x1f')]();return _0x1e91f3[0x0];})[_0x0fb8('0xf')](respondWithResult(_0x129819,null))['catch'](handleError(_0x129819,null));}else{return _0x129819[_0x0fb8('0x9')](0x1f4)[_0x0fb8('0xa')]({'message':'Unknown\x20id'});}};