/* Copyright 2019 Intel Corporation Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ var promise = require('promise'); var servicemanager = require('service-manager'); var mgr = new servicemanager(); //attachDebugger({ webport: 9995, wait: 1 }).then(console.log); function task() { this._ObjectID = 'task-scheduler'; this.create = function create(options) { var ret = new promise(function (res, rej) { this._res = res; this._rej = rej; }); if(options.name && options.service) { switch(process.platform) { case 'win32': var parms = ['schtasks', '/Create', '/RU SYSTEM']; for (var ftype in options) { switch(ftype.toUpperCase()) { case 'MINUTE': case 'HOURLY': case 'DAILY': case 'WEEKLY': case 'MONTHLY': parms.push('/SC ' + ftype.toUpperCase()); parms.push('/MO ' + options[ftype]); break; case 'DAY': parms.push('/D ' + options[ftype]); break; case 'MONTH': parms.push('/M ' + options[ftype]); break; case 'TIME': parms.push('/ST ' + options[ftype]); break; case 'NAME': parms.push('/TN "' + options[ftype].split('/').join('\\') + '"'); break; case 'SERVICE': parms.push('/TR "net start ' + options[ftype] + '"'); break; } } console.log(parms.join(' ')); ret.child = require('child_process').execFile(process.env['windir'] + '\\system32\\schtasks.exe', parms); ret.child.stdout.str = ''; ret.child.stdout.on('data', function (chunk) { this.str += chunk.toString(); }); ret.child.stderr.on('data', function (chunk) { }); ret.child.promise = ret; ret.child.on('exit', function (code) { if (code == 0) { this.promise._res(); } else { this.promise._rej(code); }}); break; case 'linux': if (require('fs').existsSync('/etc/cron.d/' + options.name.split('/').join('_').split('.').join(''))) { ret._rej('Task [' + options.name + '] Already exists'); return (ret); } var minute = '*'; var hour = '*'; var day = '*'; var month = '*'; var weekday = '*'; for (var ftype in options) { switch(ftype.toUpperCase()) { case 'MINUTE': if (!options.TIME && !options.time) { minute = '*/' + options[ftype]; } break; case 'HOURLY': if (!options.TIME && !options.time) { hour = '*/' + options[ftype]; } break; case 'DAILY': day = '*/' + options[ftype]; break; case 'WEEKLY': if (options[ftype] == 1) { if(!options.DAY && !options.day) { weekday = 0; } } else { ret._rej('Only Once/Weekly supported on Linux'); return (ret); } break; case 'DAY': if (options.weekly || options.WEEKLY) { weekday = options[ftype]; } else { day = options[ftype]; } break; case 'TIME': hour = options[ftype].split(':')[0]; minute = options[ftype].split(':')[1]; break; case 'MONTHLY': month = '*/' + options[ftype]; break; } } var action = 'SHELL=/bin/sh\nPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin\n\n'; action += (minute + ' ' + hour + ' ' + day + ' ' + month + ' ' + weekday + ' root '); switch(require('service-manager').manager.getServiceType()) { case 'init': var child = require('child_process').execFile('/bin/sh', ['sh']); child.stdout.str = ''; child.stdout.on('data', function (chunk) { this.str += chunk.toString(); }); child.stderr.on('data', function (chunk) { }); child.stdin.write("whereis service | awk '{print $2}'\n\exit\n"); child.waitExit(); child.stdout.str = child.stdout.str.trim(); action += (child.stdout.str + ' ' + options.service + ' restart >/dev/null 2>&1 \n'); break; case 'upstart': var child = require('child_process').execFile('/bin/sh', ['sh']); child.stdout.str = ''; child.stdout.on('data', function (chunk) { this.str += chunk.toString(); }); child.stderr.on('data', function (chunk) { }); child.stdin.write("whereis initctl | awk '{print $2}'\n\exit\n"); child.waitExit(); child.stdout.str = child.stdout.str.trim(); action += (child.stdout.str + ' ' + options.service + ' restart >/dev/null 2>&1 \n'); break; case 'systemd': var child = require('child_process').execFile('/bin/sh', ['sh']); child.stdout.str = ''; child.stdout.on('data', function (chunk) { this.str += chunk.toString(); }); child.stderr.on('data', function (chunk) { }); child.stdin.write("whereis systemctl | awk '{print $2}'\n\exit\n"); child.waitExit(); child.stdout.str = child.stdout.str.trim(); action += (child.stdout.str + ' restart ' + options.service + ' >/dev/null 2>&1 \n'); break; default: ret._rej('Unknown Service Platform: ' + require('service-manager').manager.getServiceType()); return (ret); } try { var m = require('fs').CHMOD_MODES.S_IRUSR | require('fs').CHMOD_MODES.S_IWUSR | require('fs').CHMOD_MODES.S_IROTH; require('fs').writeFileSync('/etc/cron.d/' + options.name.split('/').join('_').split('.').join(''), action, { flags: 'wb', mode: m }); } catch(e) { ret._rej(e); return (ret); } ret._res(); break; case 'darwin': var taskname = options.name.split('/').join('_').split('.').join(''); var plist = '\n'; plist += '\n'; plist += '\n'; plist += ' \n'; plist += ' Label\n'; plist += (' ' + taskname + '\n'); plist += ' ProgramArguments\n'; plist += ' \n'; plist += ' /bin/launchctl\n'; plist += ' start\n'; plist += (' ' + options.service + '\n'); plist += ' \n'; plist += ' RunAtLoad\n'; plist += ' \n'; plist += '{{{INTERVAL}}}'; plist += ' \n'; plist += ''; var interval = null; var periodic = ''; for (var ftype in options) { switch (ftype.toUpperCase()) { case 'MINUTE': if (interval != null || periodic != '') { ret._rej('Invalid Options'); return (ret); } interval = ' ' + (parseInt(options[ftype]) * 60) + '\n'; break; case 'HOURLY': if (interval != null || periodic != '') { ret._rej('Invalid Options'); return (ret); } interval = ' ' + (parseInt(options[ftype]) * 60 * 60) + '\n'; break; case 'DAILY': if (interval != null || periodic != '') { ret._rej('Invalid Options'); return (ret);} interval = ' ' + (parseInt(options[ftype]) * 24 * 60 * 60) + '\n'; break; case 'WEEKLY': if (interval != null) { ret._rej('Invalid Options'); return (ret); } if (!options.DAY && !options.day) { interval = ' ' + (parseInt(options[ftype]) * 60) + '\n'; } else if(parseInt(options[ftype]) != 1) { ret._rej('Invalid Options, Only Once Weekly Supported when DAY specified'); return (ret); } break; case 'MONTHLY': if (interval != null || periodic != '') { ret._rej('Invalid Options'); return (ret);} interval = ' ' + (parseInt(options[ftype]) * 30 * 24 * 60 * 60) + '\n'; break; case 'DAY': if (interval != null) { ret._rej('Invalid Options'); return (ret);} if (parseInt(options.weekly) == 1 || parseInt(options.WEEKLY) == 1) { periodic += ' Weekday\n'; periodic += (' ' + options[ftype] + '\n'); } else { periodic += ' Day\n'; periodic += (' ' + options[ftype] + '\n'); } break; case 'MONTH': if (interval != null) { ret._rej('Invalid Options'); return (ret);} periodic += ' Month\n'; periodic += (' ' + options[ftype] + '\n'); break; case 'TIME': if (interval != null) { ret._rej('Invalid Options'); return (ret);} periodic += ' Hour\n'; periodic += (' ' + options[ftype].split(':')[0] + '\n'); periodic += ' Minute\n'; periodic += (' ' + options[ftype].split(':')[1] + '\n'); break; } } if (interval) { plist = plist.replace('{{{INTERVAL}}}', ' StartInterval\n' + interval); } if (periodic) { plist = plist.replace('{{{INTERVAL}}}', ' StartCalendarInterval\n \n' + periodic + ' \n'); } require('fs').writeFileSync('/Library/LaunchDaemons/' + taskname + '.plist', plist); var child = require('child_process').execFile('/bin/sh', ['sh']); child.stdout.on('data', function (chunk) { }); child.stdin.write('launchctl load /Library/LaunchDaemons/' + taskname + '.plist\nexit\n'); child.waitExit(); ret._res(); break; default: ret._rej('Not implemented on ' + process.platform); break; } } else { ret._rej('Invalid Parameters, must at least specify name and service'); } return (ret); }; this.info = function info(name) { var ret = new promise(function (res, rej) { this._res = res; this._rej = rej; }); switch (process.platform) { default: ret._rej('Not implemented on ' + process.platform); break; } return (ret); }; this.delete = function _delete(name) { var ret = new promise(function (res, rej) { this._res = res; this._rej = rej; }); switch (process.platform) { case 'win32': ret.child = require('child_process').execFile(process.env['windir'] + '\\system32\\schtasks.exe', ['schtasks', '/Delete', '/TN "' + name.split('/').join('\\') + '"', '/F']); ret.child.stdout.str = ''; ret.child.stdout.on('data', function (chunk) { this.str += chunk.toString(); }); ret.child.stderr.on('data', function (chunk) { }); ret.child.promise = ret; ret.child.on('exit', function (code) { if (code == 0) { this.promise._res(); } else { this.promise._rej(code); } }); break; case 'linux': if (require('fs').existsSync('/etc/cron.d/' + name.split('/').join('_').split('.').join(''))) { try { require('fs').unlinkSync('/etc/cron.d/' + name.split('/').join('_').split('.').join('')); } catch(e) { ret._rej(e); return (ret); } ret._res(); } else { ret._rej('Task [' + name + '] does not exist'); } break; case 'darwin': var taskname = name.split('/').join('_').split('.').join(''); if (require('fs').existsSync('/Library/LaunchDaemons/' + taskname + '.plist')) { var child = require('child_process').execFile('/bin/sh', ['sh']); child.stdout.on('data', function (chunk) { }); child.stdin.write('launchctl unload /Library/LaunchDaemons/' + taskname + '.plist\nexit\n'); child.waitExit(); try { require('fs').unlinkSync('/Library/LaunchDaemons/' + taskname + '.plist'); } catch (e) { ret._rej(e); return (ret); } ret._res(); } else { ret._rej('Task [' + name + '] does not exist'); } break; default: ret._rej('Not implemented on ' + process.platform); break; } return (ret); }; } module.exports = new task();