diff --git a/lib/Interval4.js b/lib/Interval4.js index 84abb51..308e8d2 100755 --- a/lib/Interval4.js +++ b/lib/Interval4.js @@ -183,73 +183,114 @@ class Interval4 extends Interval { } } - /* - * fetches the list of updates available for the machine - * format: [package-info|package-info|...] - */ - getListOfUpdates(adapter) { - - if(machine.pkgMgr === undefined || machine.pkgMgr === null) { - adapter.log.debug('Package-Manager not initialized!'); - return; - } - + debianUpdates(adapter) { // we want to spawn commands const cmd = require('child_process'); - const list = []; - switch(machine.platform) { - case 'linux': - // TODO implement for different package managers - if(machine.pkgMgr === 'apt') { - // debian packages with apt - cmd.execSync('sudo apt update 2> /dev/null'); - const lines = cmd.execSync('sudo apt list --upgradeable 2> /dev/null').toString().split('\n'); - let k = 0; - let pkg; - for(let i = 0; i < lines.length; i++) { - if(lines[i].length > 16) { - pkg = lines[i].toString().split('/'); - if(pkg != undefined && pkg[0] != undefined) { - list[k++] = pkg[0].trim(); + + cmd.exec('sudo apt update', (err, stdout, stderr) => { + if(err) { + adapter.log.error(err); + } else { + if(stderr) { + adapter.log.info(stderr.trim()); + } + cmd.exec('sudo apt list --upgradeable', (err, stdout, stderr) => { + if(err) { + adapter.log.error(err); + } else { + if(stderr) { + adapter.log.info(stderr.trim()); + } + const lines = stdout.toString().split('\n'); + let k = 0; + let pkg; + for(let i = 0; i < lines.length; i++) { + if(lines[i].length > 16) { + pkg = lines[i].toString().split('/'); + if(pkg != undefined && pkg[0] != undefined) { + list[k++] = pkg[0].trim(); + } } } + // maintain machine state + this.maintainStates(list, adapter); } - } else if(machine.pkgMgr === 'yum') { - const lines = cmd.execSync('sudo yum check-update 2> /dev/null').toString().split('\n'); - let k = 0; - // TODO: wie arbeitet yum - let pkg; - for(let i = 0; i < lines.length; i++) { - pkg = lines[i].toString().split('|'); - if(pkg != undefined && pkg[1] != undefined) { - list[k++] = pkg[1].trim(); - } - } - } else if(machine.pkgMgr === 'zypper') { - // redhat packages with zypper - cmd.execSync('sudo zypper refresh'); - const lines = cmd.execSync('sudo zypper list-updates 2> /dev/null').toString().split('\n'); - let k = 0; - let pkg; - for(let i = 5; i < lines.length; i++) { - pkg = lines[i].toString().split('|'); - if(pkg != undefined && pkg[2] != undefined) { - list[k++] = pkg[2].trim(); - } - } - } - break; - case 'darwin': - case 'win32': - default: - adapter.log.info('Update-Manager not implemented for ' + machine.platform); - break; + }); + } + }); + } + + redhatUpdates(adapter) { + // we want to spawn commands + const cmd = require('child_process'); + const list = []; + + const lines = cmd.execSync('sudo yum check-update 2> /dev/null').toString().split('\n'); + let k = 0; + // TODO: wie arbeitet yum + let pkg; + for(let i = 0; i < lines.length; i++) { + pkg = lines[i].toString().split('|'); + if(pkg != undefined && pkg[1] != undefined) { + list[k++] = pkg[1].trim(); + } + } + // maintain machine state + this.maintainStates(list, adapter); + } + + suseUpdates(adapter) { + // we want to spawn commands + const cmd = require('child_process'); + const list = []; + + cmd.execSync('sudo zypper refresh'); + const lines = cmd.execSync('sudo zypper list-updates 2> /dev/null').toString().split('\n'); + let k = 0; + let pkg; + for(let i = 5; i < lines.length; i++) { + pkg = lines[i].toString().split('|'); + if(pkg != undefined && pkg[2] != undefined) { + list[k++] = pkg[2].trim(); + } } // maintain machine state this.maintainStates(list, adapter); } + /* + * fetches the list of updates available for the machine + * format: [package-info|package-info|...] + */ + getListOfUpdates(adapter) { + // check package manager + if(machine.pkgMgr === undefined || machine.pkgMgr === null) { + adapter.log.debug('Package-Manager not initialized!'); + } else { + switch(machine.platform) { + case 'linux': + // TODO implement for different package managers + if(machine.pkgMgr === 'apt') { + // debian packages with apt + this.debianUpdates(adapter); + } else if(machine.pkgMgr === 'yum') { + // redhat updates with yum + this.redhatUpdates(adapter); + } else if(machine.pkgMgr === 'zypper') { + // redhat packages with zypper + this.suseUpdates(adapter); + } + break; + case 'darwin': + case 'win32': + default: + adapter.log.info('Update-Manager not implemented for ' + machine.platform); + break; + } + } + } + /* * Function checks battery state for all devices in every adapter * Currently implemented: @@ -310,36 +351,47 @@ class Interval4 extends Interval { const cmd = require('child_process'); let newController = ''; let newAdapter = ''; - let lines = []; + const options = {}; switch(machine.platform) { case 'linux': - lines = cmd.execSync('iobroker update 2> /dev/null',[],{cwd:'/opt/iobroker'}).toString().split('\n'); + options.cwd = '/opt/iobroker'; break; case 'win32': - lines = cmd.execSync('iobroker update 2> /dev/null',[],{cwd:'C:\\iobroker'}).toString().split('\n'); + options.cwd ='C:\\iobroker'; break; case 'darwin': default: - adapter.log.info('Update-Manager not implemented for ' + machine.platform); - break; + adapter.log.info('IoBroker Update-Manager not implemented for ' + machine.platform); + // bail out + return; } - for(let i = 0; i < lines.length; i++) { - // adapter.log.debug(lines[i]); - if(lines[i].search('Updateable') > 0) { - const content = lines[i].split(':'); - const adapterName = content[0].split('"')[1]; - const newVersion = content[1].split(' ')[1].trim(); - // adapter.log.debug(adapterName); - if(adapterName == 'js-controller') { - newController = adapterName + '@' + newVersion; - } else { // Adapter - newAdapter = newAdapter + (adapterName + '@' + newVersion + ','); + cmd.execFile('iobroker', ['update'], options, (err, stdout, stderr) => { + if(err) { + adapter.log.error(err); + } else { + if(stderr) { + adapter.log.error(stderr.trim()); + } + const lines = stdout.toString().split('\n'); + for(let i = 0; i < lines.length; i++) { + // adapter.log.debug(lines[i]); + if(lines[i].search('Updateable') > 0) { + const content = lines[i].split(':'); + const adapterName = content[0].split('"')[1]; + const newVersion = content[1].split(' ')[1].trim(); + // adapter.log.debug(adapterName); + if(adapterName == 'js-controller') { + newController = adapterName + '@' + newVersion; + } else { // Adapter + newAdapter = newAdapter + (adapterName + '@' + newVersion + ','); + } + } } + // adapter.log.debug(newController + '/' + newAdapter); + adapter.setForeignState(defs.hostEntryListOfAdapterUpdates, {val: newAdapter, ack: true}); + adapter.setForeignState(defs.hostEntryControllerUpdate, {val: newController, ack: true}); } - } - // adapter.log.debug(newController + '/' + newAdapter); - adapter.setForeignState(defs.hostEntryListOfAdapterUpdates, {val: newAdapter, ack: true}); - adapter.setForeignState(defs.hostEntryControllerUpdate, {val: newController, ack: true}); + }); } // end of class