c3dbf9012c3943c2d3bbb0dc9d280955b9caf903
[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 _0x01ff=['update','stop','destroy','delete','dump','../../config/pm2','../../config/logger','child_process','exec','json','error','stack','name','status','send','npm\x20install','index','list','then','reject','express','motion2','catch','show','describe','params','create','body','script','join','../../files/plugins','cwd','chdir','dirname','Missing\x20name\x20and/or\x20path'];(function(_0x54fea4,_0x18ed21){var _0x43b8d8=function(_0x1352e5){while(--_0x1352e5){_0x54fea4['push'](_0x54fea4['shift']());}};_0x43b8d8(++_0x18ed21);}(_0x01ff,0x1cc));var _0xf01f=function(_0x2f1043,_0x52cb0e){_0x2f1043=_0x2f1043-0x0;var _0x38110c=_0x01ff[_0x2f1043];return _0x38110c;};'use strict';var _=require('lodash');var pm2=require(_0xf01f('0x0'));var logger=require(_0xf01f('0x1'))('api');var path=require('path');var exec=require(_0xf01f('0x2'))[_0xf01f('0x3')];var BPromise=require('bluebird');function handleEntityNotFound(_0x49e7d6,_0x30203e){return function(_0x4af83c){if(!_0x4af83c){_0x49e7d6['sendStatus'](0x194);;}return _0x4af83c;};}function respondWithResult(_0x311d35,_0x1498f2){_0x1498f2=_0x1498f2||0xc8;return function(_0x2d43f4){if(_0x2d43f4){return _0x311d35['status'](_0x1498f2)[_0xf01f('0x4')](_0x2d43f4);}return null;};}function handleError(_0x1327f8,_0x385b8e){_0x385b8e=_0x385b8e||0x1f4;return function(_0x3d58e3){logger[_0xf01f('0x5')](_0x3d58e3[_0xf01f('0x6')]);if(_0x3d58e3[_0xf01f('0x7')]){delete _0x3d58e3[_0xf01f('0x7')];}_0x1327f8[_0xf01f('0x8')](_0x385b8e)[_0xf01f('0x9')](_0x3d58e3);};}function execChildProcess(){return new BPromise(function(_0x4ebdaa,_0x1879b6){exec(_0xf01f('0xa'),function(_0x45a78d,_0x414db5,_0x856f98){logger['info'](_0x414db5);logger['info'](_0x856f98);if(_0x45a78d){logger['error'](_0x45a78d);return _0x1879b6(_0x45a78d);}return _0x4ebdaa(_0x414db5);});});}exports[_0xf01f('0xb')]=function(_0x9c33db,_0x9c9b55){return pm2[_0xf01f('0xc')]()[_0xf01f('0xd')](function(_0x1fc1c5){if(_0x1fc1c5){var _0xbed0d4=_[_0xf01f('0xe')](_0x1fc1c5,function(_0x2fa1bb){return _0x2fa1bb[_0xf01f('0x7')]===_0xf01f('0xf')||_0x2fa1bb[_0xf01f('0x7')]===_0xf01f('0x10');});return{'rows':_0xbed0d4,'count':_0xbed0d4['length']};}return null;})[_0xf01f('0xd')](respondWithResult(_0x9c9b55,null))[_0xf01f('0x11')](handleError(_0x9c9b55,null));};exports[_0xf01f('0x12')]=function(_0x1bc62b,_0x39d40c){return pm2[_0xf01f('0x13')](_0x1bc62b[_0xf01f('0x14')]['id'])[_0xf01f('0xd')](respondWithResult(_0x39d40c,null))[_0xf01f('0xd')](handleEntityNotFound(_0x39d40c,null))[_0xf01f('0x11')](handleError(_0x39d40c,null));};exports[_0xf01f('0x15')]=function(_0x424cd1,_0x3e3c5a){if(_0x424cd1[_0xf01f('0x16')][_0xf01f('0x7')]&&_0x424cd1[_0xf01f('0x16')][_0xf01f('0x17')]){var _0x380c31=path[_0xf01f('0x18')](__dirname,_0xf01f('0x19'),_0x424cd1[_0xf01f('0x16')][_0xf01f('0x17')]);var _0x20cbfb=process[_0xf01f('0x1a')]();process[_0xf01f('0x1b')](path[_0xf01f('0x1c')](_0x380c31));return execChildProcess()[_0xf01f('0xd')](function(_0x40b193){process[_0xf01f('0x1b')](_0x20cbfb);return pm2['start']({'name':_0x424cd1[_0xf01f('0x16')][_0xf01f('0x7')],'script':_0x380c31});})[_0xf01f('0xd')](function(_0x541142){pm2['dump']();return _0x541142[0x0];})[_0xf01f('0xd')](respondWithResult(_0x3e3c5a,0xc9))[_0xf01f('0x11')](handleError(_0x3e3c5a,null));}else{return _0x3e3c5a[_0xf01f('0x8')](0x1f4)['send']({'message':_0xf01f('0x1d')});}};exports[_0xf01f('0x1e')]=function(_0x3f52a8,_0x224411){if(_0x3f52a8['body']['id']){delete _0x3f52a8[_0xf01f('0x16')]['id'];}var _0x4e806d={'online':'restart','stopped':_0xf01f('0x1f')};if(_0x4e806d['hasOwnProperty'](_0x3f52a8[_0xf01f('0x16')]['status'])){return pm2[_0x4e806d[_0x3f52a8[_0xf01f('0x16')][_0xf01f('0x8')]]](_0x3f52a8[_0xf01f('0x14')]['id'])[_0xf01f('0xd')](respondWithResult(_0x224411,0xc8))[_0xf01f('0x11')](handleError(_0x224411,null));}else{return _0x224411[_0xf01f('0x8')](0x1f4)['send']({'message':'Unknown\x20status'});}};exports[_0xf01f('0x20')]=function(_0x3f5b47,_0x54b8c3){if(_0x3f5b47[_0xf01f('0x16')]['id']){delete _0x3f5b47[_0xf01f('0x16')]['id'];}if(_0x3f5b47[_0xf01f('0x14')]['id']){return pm2[_0xf01f('0x21')](_0x3f5b47[_0xf01f('0x14')]['id'])[_0xf01f('0xd')](function(_0x5e9e8a){pm2[_0xf01f('0x22')]();return _0x5e9e8a[0x0];})['then'](respondWithResult(_0x54b8c3,null))[_0xf01f('0x11')](handleError(_0x54b8c3,null));}else{return _0x54b8c3[_0xf01f('0x8')](0x1f4)[_0xf01f('0x9')]({'message':'Unknown\x20id'});}};