Skip to content

Commit

Permalink
Refactoring: OS Support List moved to Widget
Browse files Browse the repository at this point in the history
  • Loading branch information
Delfer committed May 16, 2018
1 parent 3b013f2 commit a8bd5af
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 105 deletions.
1 change: 1 addition & 0 deletions backend.js
Expand Up @@ -16,6 +16,7 @@ var storage = require('./storage');
var sshell = require('./sshell');
sharedScripts = require('./scripts');
plugins = require('./plugins');
widgets = require('./widgets');

var Log = require('log');
var rfs = require('rotating-file-stream');
Expand Down
33 changes: 17 additions & 16 deletions electron-builder.yml
@@ -1,16 +1,17 @@
files:
- node_modules/**/*
- plugins/*
- scripts/*
- ui/**/*
- "*.js"
- "package.json"
# Standart for electron-builder: https://www.electron.build/file-patterns
- "!**/node_modules/*/{CHANGELOG.md,README.md,README,readme.md,readme,test,__tests__,tests,powered-test,example,examples,*.d.ts}"
- "!**/node_modules/.bin"
- "!**/*.{iml,o,hprof,orig,pyc,pyo,rbc,swp,csproj,sln,xproj}"
- "!**/._*"
- "!.editorconfig"
- "!**/{.DS_Store,.git,.hg,.svn,CVS,RCS,SCCS,__pycache__,thumbs.db,.gitignore,.gitattributes,.flowconfig,.yarn-metadata.json,.idea,.vs,appveyor.yml,.travis.yml,circle.yml,npm-debug.log,.nyc_output,yarn.lock,.yarn-integrity}"

asar: true
files:
- node_modules/**/*
- plugins/*
- widgets/*
- scripts/*
- ui/**/*
- "*.js"
- "package.json"
# Standart for electron-builder: https://www.electron.build/file-patterns
- "!**/node_modules/*/{CHANGELOG.md,README.md,README,readme.md,readme,test,__tests__,tests,powered-test,example,examples,*.d.ts}"
- "!**/node_modules/.bin"
- "!**/*.{iml,o,hprof,orig,pyc,pyo,rbc,swp,csproj,sln,xproj}"
- "!**/._*"
- "!.editorconfig"
- "!**/{.DS_Store,.git,.hg,.svn,CVS,RCS,SCCS,__pycache__,thumbs.db,.gitignore,.gitattributes,.flowconfig,.yarn-metadata.json,.idea,.vs,appveyor.yml,.travis.yml,circle.yml,npm-debug.log,.nyc_output,yarn.lock,.yarn-integrity}"

asar: true
135 changes: 46 additions & 89 deletions plugins/openvpn-plugin.js
Expand Up @@ -6,9 +6,9 @@ var script;

fs.readFile(path.join(__dirname, '../scripts/openvpn-install.sh'), 'utf8', function (err, out) {
if (!err && out) {
script = out.replace('SUDO_USER', 'NOSUDO_USER');
script = out.replace('SUDO_USER', 'NOSUDO_USER');
}
});
});

var plugin = {};
var con;
Expand All @@ -22,28 +22,33 @@ var state = {

exports.name = 'OpenVPN';

var osSupportList = [{
name: "Centos",
version: ["6", "7"]
},
{
name: "Fedora",
version: ["25", "26", "27"]
},
{
name: "Debian",
version: ["7", "8", "9"]
},
{
name: "Ubuntu",
version: ["14.04", "16.04", "17.10"]
},
{
name: "Arch Linux"
}
];


exports.getView = function () {
var osWidget = widgets.os.getView(osSupportList);
return `
<!-- OS -->
<p class="container text-muted p-0">
<label>Supported OS:</label><br>
<label id="os-d" class="badge">Debian</label>
<label id="os-d7" class="badge">7</label>
<label id="os-d8" class="badge">8</label>
<label id="os-d9" class="badge">9</label> |
<label id="os-u" class="badge">Ubuntu</label>
<label id="os-u1404" class="badge">14.04</label>
<label id="os-u1604" class="badge">16.04</label>
<label id="os-u1710" class="badge">17.10</label> <br>
<label id="os-f" class="badge">Fedora</label>
<label id="os-f25" class="badge">25</label>
<label id="os-f26" class="badge">26</label>
<label id="os-f27" class="badge">27</label> |
<label id="os-c" class="badge">CentOS</label>
<label id="os-c6" class="badge">6</label>
<label id="os-c7" class="badge">7</label> |
<label id="os-a" class="badge">Arch Linux</label>
</p>
${osWidget}
<!-- Status -->
<div id='statusDiv' class="alert" role="alert">
Expand Down Expand Up @@ -96,11 +101,6 @@ exports.getView = function () {
<script>
function pluginViewRefreshCallback (state) {
if (state && state.osFamaly) {
$('#os-' + state.osFamaly).addClass("badge-success");
$('#os-' + state.osFamaly + state.osVersion).addClass("badge-success");
}
var statusText;
var statusClass;
if (state.internalError) {
Expand Down Expand Up @@ -233,7 +233,10 @@ exports.setSSHConnection = function (ssh) {
state = {
report: {}
};
detectOS();
widgets.os.init(con)
.then(s => {
state.osName = s;
});
getStatus();
getClients();
};
Expand Down Expand Up @@ -263,60 +266,6 @@ var refreshData = function () {
}
};

var detectOS = () =>
sshell.runCmd(con, 'cat /etc/*-release').then(res => {
var pretty = res.toString().match(/PRETTY_NAME="(.+)"/);
if (pretty) {
state.osName = pretty[1];
} else {
var description = res.toString().match(/DISTRIB_DESCRIPTION="(.+)"/);
if (description) {
state.osName = description[1];
} else {
state.osName = res.toString();
}
}

if (/debian/i.test(state.osName)) {
state.osFamaly = 'd';
if (/7/.test(state.osName)) {
state.osVersion = '7';
} else if (/8/.test(state.osName)) {
state.osVersion = '8';
} else if (/9/.test(state.osName)) {
state.osVersion = '9';
}
} else if (/ubuntu/i.test(state.osName)) {
state.osFamaly = 'u';
if (/14\.04/.test(state.osName)) {
state.osVersion = '1404';
} else if (/16\.04/.test(state.osName)) {
state.osVersion = '1604';
} else if (/17\.10/.test(state.osName)) {
state.osVersion = '1710';
}
} else if (/fedora/i.test(state.osName)) {
state.osFamaly = 'f';
if (/25/.test(state.osName)) {
state.osVersion = '25';
} else if (/26/.test(state.osName)) {
state.osVersion = '26';
} else if (/27/.test(state.osName)) {
state.osVersion = '27';
}
} else if (/centos/i.test(state.osName)) {
state.osFamaly = 'c';
if (/6/.test(state.osName)) {
state.osVersion = '6';
} else if (/7/.test(state.osName)) {
state.osVersion = '7';
}
} else if (/arch linux/i.test(state.osName)) {
state.osFamaly = 'a';
}
uiCallback();
});

var getStatus = () => {
var statusScript = script.replace(/^\s*read .*$/mg, 'exit');

Expand Down Expand Up @@ -356,16 +305,20 @@ var installOpenVPN = () => {
.then(() => sharedScripts.install_package(con, 'openvpn'))
.then(() => sshell.runBashScriptAsRoot(con, installScript))
.then((s) => {
state.report.success = true;
state.report.text = s;
openModal(
'OpenVPN installation successed!',
'<textarea class="form-control" rows="10" style="font-family:monospace;white-space: pre;" disabled>' + s + '</textarea>'
);

state.installationInProgress = false;
getStatus();
getClients();
})
.catch(s => {
state.report.success = false;
state.report.text = s;
openModal(
'OpenVPN installation failed!',
'<textarea class="form-control" rows="10" style="font-family:monospace;white-space: pre;" disabled>' + s + '</textarea>'
);

state.installationInProgress = false;
getStatus();
Expand All @@ -384,14 +337,18 @@ var uninstallOpenVPN = function () {

sshell.runBashScriptAsRoot(con, uninstallScript)
.then(s => {
state.report.success = true;
state.report.text = s;
openModal(
'OpenVPN uninstallation successed!',
'<textarea class="form-control" rows="10" style="font-family:monospace;white-space: pre;" disabled>' + s + '</textarea>'
);

state.uninstallationInProgress = false;
getStatus();
}, s => {
state.report.success = false;
state.report.text = s;
openModal(
'OpenVPN uninstallation failed!',
'<textarea class="form-control" rows="10" style="font-family:monospace;white-space: pre;" disabled>' + s + '</textarea>'
);

state.uninstallationInProgress = false;
getStatus();
Expand Down
4 changes: 4 additions & 0 deletions widgets/index.js
@@ -0,0 +1,4 @@
[
require("./os")
]
.forEach(i => i.load(exports));
104 changes: 104 additions & 0 deletions widgets/os.js
@@ -0,0 +1,104 @@
var sshell = require('../sshell');

var osName;

var widget = {
init: con => {
return new Promise((resolve, reject) => {
sshell.runCmd(con, 'cat /etc/*-release').then(res => {
var pretty = res.toString().match(/PRETTY_NAME="(.+)"/);
if (pretty) {
osName = pretty[1];
} else {
var description = res.toString().match(/DISTRIB_DESCRIPTION="(.+)"/);
if (description) {
osName = description[1];
} else {
osName = res.toString();
}
}
resolve(osName);
}, reject);
});
},

isSupported: false,

getView: list_ => {
var list = JSON.parse(JSON.stringify(list_)); //Deep copy

// OS check
list.forEach(os => {
var re = new RegExp(os.name, 'i');
if (re.test(osName)) {
os.supported = true;
if (os.version && os.version.length > 0) {
os.version.forEach(v => {
re = new RegExp(v, 'i');
if (re.test(osName)) {
os.supportedVersion = v;
}
});
}
}
});

//UI build
widget.isSupported = false;
var ui = `<p class="container text-muted p-0">
<label>Supported OS:</label><br>`;
list.forEach((os, num) => {
var c = '';
if (
(os.supported && os.version && os.version.length > 0 && os.supportedVersion) ||
(os.supported && os.version && os.version.length === 0) ||
(os.supported && !os.version)
) {
widget.isSupported = true;
c = ' badge-success';
} else if (os.supported) {
c = ' badge-danger';
}
ui += '<label class="badge' + c + '">' + os.name + '</label>';

if (os.version && os.version.length > 0) {
os.version.forEach(v => {
if (os.supported && v === os.supportedVersion) {
c = ' badge-success';
} else {
c = '';
}
ui += '<label class="badge' + c + '">' + v + '</label>';
});
}

if (num != (list.length - 1)) {
if (num % 2 === 1) {
ui += '<br>';
} else {
ui += '| ';
}
}
});

if (!widget.isSupported) {
if (list.length % 2 === 0) {
ui += '<br>';
} else {
ui += '| ';
}

if (!osName || osName.length < 1) {
osName = 'Unknown';
}
ui += '<label class="badge badge-danger">' + osName.split(/\s+/).slice(0, 3).join(' ').substr(0, 32) + '</label>';
}

ui += `</p>`;
return ui;
}
};

exports.load = widgets => {
widgets.os = widget;
};

0 comments on commit a8bd5af

Please sign in to comment.