8a4f0972e602268e83d4122c16dfae4381570da7
[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 _0xe10f=['../../files/plugins','cwd','chdir','dirname','dump','Missing\x20name\x20and/or\x20path','restart','hasOwnProperty','Unknown\x20status','Unknown\x20id','lodash','../../config/pm2','../../config/logger','api','child_process','bluebird','sendStatus','status','error','stack','name','send','npm\x20install','info','index','reject','express','motion2','catch','show','describe','params','then','body','script'];(function(_0x6be6cf,_0x1f1ed1){var _0x28600f=function(_0x362f20){while(--_0x362f20){_0x6be6cf['push'](_0x6be6cf['shift']());}};_0x28600f(++_0x1f1ed1);}(_0xe10f,0xff));var _0xfe10=function(_0x18e34c,_0x1f87c5){_0x18e34c=_0x18e34c-0x0;var _0x5ad33e=_0xe10f[_0x18e34c];return _0x5ad33e;};'use strict';var _=require(_0xfe10('0x0'));var pm2=require(_0xfe10('0x1'));var logger=require(_0xfe10('0x2'))(_0xfe10('0x3'));var path=require('path');var exec=require(_0xfe10('0x4'))['exec'];var BPromise=require(_0xfe10('0x5'));function handleEntityNotFound(_0x147d53,_0xc02f7c){return function(_0x15b79a){if(!_0x15b79a){_0x147d53[_0xfe10('0x6')](0x194);;}return _0x15b79a;};}function respondWithResult(_0x2614d8,_0x44b41f){_0x44b41f=_0x44b41f||0xc8;return function(_0x2c789d){if(_0x2c789d){return _0x2614d8[_0xfe10('0x7')](_0x44b41f)['json'](_0x2c789d);}return null;};}function handleError(_0x62bb74,_0x330c23){_0x330c23=_0x330c23||0x1f4;return function(_0x469e73){logger[_0xfe10('0x8')](_0x469e73[_0xfe10('0x9')]);if(_0x469e73[_0xfe10('0xa')]){delete _0x469e73['name'];}_0x62bb74[_0xfe10('0x7')](_0x330c23)[_0xfe10('0xb')](_0x469e73);};}function execChildProcess(){return new BPromise(function(_0x17240a,_0x358845){exec(_0xfe10('0xc'),function(_0x45c448,_0x580cd0,_0xa08fe0){logger['info'](_0x580cd0);logger[_0xfe10('0xd')](_0xa08fe0);if(_0x45c448){logger[_0xfe10('0x8')](_0x45c448);return _0x358845(_0x45c448);}return _0x17240a(_0x580cd0);});});}exports[_0xfe10('0xe')]=function(_0x36828c,_0x4c841c){return pm2['list']()['then'](function(_0x16fde0){if(_0x16fde0){var _0x3e6c9f=_[_0xfe10('0xf')](_0x16fde0,function(_0x30b01b){return _0x30b01b[_0xfe10('0xa')]===_0xfe10('0x10')||_0x30b01b[_0xfe10('0xa')]===_0xfe10('0x11');});return{'rows':_0x3e6c9f,'count':_0x3e6c9f['length']};}return null;})['then'](respondWithResult(_0x4c841c,null))[_0xfe10('0x12')](handleError(_0x4c841c,null));};exports[_0xfe10('0x13')]=function(_0x43fd8b,_0x15c52f){return pm2[_0xfe10('0x14')](_0x43fd8b[_0xfe10('0x15')]['id'])['then'](respondWithResult(_0x15c52f,null))[_0xfe10('0x16')](handleEntityNotFound(_0x15c52f,null))[_0xfe10('0x12')](handleError(_0x15c52f,null));};exports['create']=function(_0x413d57,_0x44c09e){if(_0x413d57[_0xfe10('0x17')]['name']&&_0x413d57[_0xfe10('0x17')][_0xfe10('0x18')]){var _0x408754=path['join'](__dirname,_0xfe10('0x19'),_0x413d57['body'][_0xfe10('0x18')]);var _0x409e9b=process[_0xfe10('0x1a')]();process[_0xfe10('0x1b')](path[_0xfe10('0x1c')](_0x408754));return execChildProcess()[_0xfe10('0x16')](function(_0x49c1d6){process[_0xfe10('0x1b')](_0x409e9b);return pm2['start']({'name':_0x413d57[_0xfe10('0x17')][_0xfe10('0xa')],'script':_0x408754});})[_0xfe10('0x16')](function(_0x116b5d){pm2[_0xfe10('0x1d')]();return _0x116b5d[0x0];})[_0xfe10('0x16')](respondWithResult(_0x44c09e,0xc9))[_0xfe10('0x12')](handleError(_0x44c09e,null));}else{return _0x44c09e[_0xfe10('0x7')](0x1f4)[_0xfe10('0xb')]({'message':_0xfe10('0x1e')});}};exports['update']=function(_0x34aa36,_0x1e6d9f){if(_0x34aa36[_0xfe10('0x17')]['id']){delete _0x34aa36[_0xfe10('0x17')]['id'];}var _0x380b05={'online':_0xfe10('0x1f'),'stopped':'stop'};if(_0x380b05[_0xfe10('0x20')](_0x34aa36[_0xfe10('0x17')]['status'])){return pm2[_0x380b05[_0x34aa36[_0xfe10('0x17')][_0xfe10('0x7')]]](_0x34aa36['params']['id'])['then'](respondWithResult(_0x1e6d9f,0xc8))[_0xfe10('0x12')](handleError(_0x1e6d9f,null));}else{return _0x1e6d9f['status'](0x1f4)[_0xfe10('0xb')]({'message':_0xfe10('0x21')});}};exports['destroy']=function(_0x2a4bd8,_0x1fac33){if(_0x2a4bd8[_0xfe10('0x17')]['id']){delete _0x2a4bd8[_0xfe10('0x17')]['id'];}if(_0x2a4bd8['params']['id']){return pm2['delete'](_0x2a4bd8[_0xfe10('0x15')]['id'])['then'](function(_0x3da9ac){pm2[_0xfe10('0x1d')]();return _0x3da9ac[0x0];})['then'](respondWithResult(_0x1fac33,null))[_0xfe10('0x12')](handleError(_0x1fac33,null));}else{return _0x1fac33['status'](0x1f4)[_0xfe10('0xb')]({'message':_0xfe10('0x22')});}};