gns3-web-ui/installed-software.js

105 lines
2.7 KiB
JavaScript
Raw Permalink Normal View History

2019-01-09 12:45:37 +00:00
var commandExistsSync = require('command-exists').sync;
var app = require('electron').app;
var fs = require('fs');
var util = require('util');
var fetch = require('node-fetch')
var stream = require('stream');
var path = require('path');
const { spawn } = require('child_process');
2019-02-01 08:58:31 +00:00
const { ipcMain } = require('electron');
var pipeline = util.promisify(stream.pipeline);
2019-01-09 12:45:37 +00:00
exports.getInstalledSoftware = (softwareList) => {
const installed = {};
for(var software of softwareList) {
var name = software.name;
var locations = software.locations;
2019-01-09 12:45:37 +00:00
installed[name] = [];
for(var location of locations) {
// var exists = commandExistsSync(command);
var exists = fs.existsSync(location);
2019-01-09 12:45:37 +00:00
if(exists) {
installed[name].push(location);
2019-01-09 12:45:37 +00:00
}
}
}
return installed;
}
async function downloadFile(resource, softwarePath) {
var response = await fetch(resource);
if (response.status != 200) {
throw new Error(`Cannot download file ${resource}, response status = ${response.status}`);
}
await pipeline(
response.body,
fs.createWriteStream(softwarePath)
);
}
2019-02-04 10:29:39 +00:00
async function getSoftwareInstallationPath(software) {
if (software.installer) {
return path.join(app.getPath('temp'), software.binary);
}
else {
const externalPath = path.join(app.getAppPath(), 'external');
const exists = fs.existsSync(externalPath);
if (!exists) {
fs.mkdirSync(externalPath);
}
return path.join(externalPath, software.binary);
}
}
ipcMain.on('installed-software-install', async function (event, software) {
2019-02-04 10:29:39 +00:00
const softwarePath = await getSoftwareInstallationPath(software);
2019-01-14 12:52:31 +00:00
const responseChannel = `installed-software-installed-${software.name}`;
if (software.type == 'web') {
const exists = fs.existsSync(softwarePath);
if (exists) {
console.log(`Skipping downloading file due to '${softwarePath}' path exists`);
}
else {
console.log(`File '${softwarePath}' doesn't exist. Downloading file.`);
try {
await downloadFile(software.resource, softwarePath);
} catch(error) {
2019-01-14 12:52:31 +00:00
event.sender.send(responseChannel, {
success: false,
message: error.message
});
}
}
}
let child;
if (software.sudo) {
child = spawn('powershell.exe', ['Start-Process', '-FilePath', `"${softwarePath}"`]);
}
else {
child = spawn(softwarePath, software.installation_arguments);
}
child.on('exit', () => {
2019-01-14 13:07:42 +00:00
event.sender.send(responseChannel, {
success: true
});
});
child.on('error', (err) => {
2019-01-14 13:07:42 +00:00
event.sender.send(responseChannel, {
success: false,
message: err.message
});
});
child.stdin.end();
});