Skip to content

Commit

Permalink
Merge pull request #73 from pkrll/dev/0.2.1
Browse files Browse the repository at this point in the history
0.2.1
  • Loading branch information
pkrll authored Jun 23, 2018
2 parents d96e7c3 + dc5f404 commit 62c8ed8
Show file tree
Hide file tree
Showing 73 changed files with 2,381 additions and 786 deletions.
2 changes: 2 additions & 0 deletions bootstrapper/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
dist/
11 changes: 11 additions & 0 deletions bootstrapper/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.PHONY: build

build:
npm run build

run:
npm run start:server

clean:
rm -rf dist/*
rm -f tmp/info.log
13 changes: 13 additions & 0 deletions bootstrapper/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env node
'use strict';

const express = require('express');
const app = express();
const server = require('http').Server(app);
const winston = require('winston');

require('./helpers/config.js')(app);
require('./helpers/socket.js')(server);
require('./app/router')(app);

server.listen(app.get('port'), () => winston.loggers.get('logger').info('Server started. Listening on port ' + app.get('port')));
Empty file.
22 changes: 22 additions & 0 deletions bootstrapper/app/controllers/RaspyController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict'
const Raspy = require('../models/Raspy.js');

exports.update = (callback) => {
Raspy.update(callback);
}

exports.install = (callback) => {
Raspy.install(callback);
}

exports.restart = (callback) => {
Raspy.restart(callback);
}

exports.stop = (callback) => {
Raspy.stop(callback);
}

exports.shutdown = (callback) => {
Raspy.shutdown(callback);
}
18 changes: 18 additions & 0 deletions bootstrapper/app/controllers/SystemController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict'
const System = require('../models/System.js');

exports.isNodeAppRunning = (appName, callback) => {
System.isNodeAppRunning(appName).then(response => {
callback(response);
}).catch(error => {
callback(error);
});
}

exports.getLogHistory = (options, callback) => {
System.getLogHistory(options).then(response => {
callback(response);
}).catch(error => {
callback(error);
});
}
Empty file.
37 changes: 37 additions & 0 deletions bootstrapper/app/models/Raspy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict'

exports.update = callback => execute('cd ../ && make update', callback);
exports.install = callback => execute('cd ../ && make install', callback);
exports.restart = callback => execute('cd ../ && make restart', callback);
exports.stop = callback => execute('cd ../ && make stop', callback);
exports.shutdown = callback => execute('cd ../ && make shutdown', callback);

function execute(process, callback) {
const spawn = require('child_process').spawn;
const command = spawn(process, { shell: true });

let history = '';

command.stdout.on('data', function (data) {
history += data.toString();
callback({
status: 1,
result: data.toString()
});
});

command.stderr.on('data', function (data) {
history += data.toString();
callback({
status: 0,
error: { message: data.toString() }
});
});

command.on('exit', function (code) {
const winston = require('winston').loggers.get('command-logger');
winston.log('info', history);
callback({ status: -1 });
});

}
34 changes: 34 additions & 0 deletions bootstrapper/app/models/System.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict'

exports.isNodeAppRunning = appName => {
const psNode = require('ps-node');

return new Promise( (resolve, reject) => {
psNode.lookup({
command: 'node',
arguments: appName
}, function(error, result) {
if (error) {
reject({status: 0, error: error});
} else {
let isRunning = (result.length > 0);
resolve({status: 1, running: isRunning, arguments: process.arguments, pid: process.pid});
}
})
});
};

exports.getLogHistory = options => {
const winston = require('winston').loggers.get('command-logger');

return new Promise( (resolve, reject) => {
winston.query(options, (error, results) => {
if (error) {
reject({status: 0, error: error});
} else {
let result = (results.file) ? results.file.reverse() : [];
resolve({status: 1, result: result});
}
});
});
}
15 changes: 15 additions & 0 deletions bootstrapper/app/router/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict'
const express = require('express');
const router = express.Router();
const path = require('path');

module.exports = function (app) {

app.use(express.static(app.get('dist')));

app.get('*', function(req, res) {
let file = path.join(app.get('dist'), '/index.html');
res.sendFile(file);
});

}
55 changes: 55 additions & 0 deletions bootstrapper/app/router/socket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict'
const winston = require('winston').loggers.get('command-logger');
const System = require('../controllers/SystemController.js');
const Raspy = require('../controllers/RaspyController.js');

module.exports = socket => {

socket.on('client:status', () => {
System.isNodeAppRunning('server.js', response => {
socket.emit('status', response);
});
});

socket.on('client:perform', request => {
winston.log('info', '$ ' + request.command);

switch (request.command) {
case 'update':
Raspy.update(message => socket.emit('command', message));
break;
case 'install':
Raspy.install(message => socket.emit('command', message));
break;
case 'restart':
Raspy.restart(message => socket.emit('command', message));
break;
case 'stop':
Raspy.stop(message => socket.emit('command', message));
break;
case 'shutdown':
Raspy.shutdown(message => socket.emit('command', message));
break;
default:
socket.emit('command', {
status: 0,
error: { message: 'Unrecognized command ' + request.command }
});
break;
}
});

socket.on('client:history', () => {
const options = {
from: new Date - 24 * 60 * 60 * 1000 * 7,
until: new Date,
limit: 20,
start: 0,
order: 'desc',
fields: ['message', 'level']
};

System.getLogHistory(options, response => socket.emit('history', response));
});

};
12 changes: 12 additions & 0 deletions bootstrapper/config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict'

module.exports = {
dev: {
port: 5001,
databasePath: 'db.json'
},
prod: {
port: 5001,
databasePath: 'db.json'
}
}
33 changes: 33 additions & 0 deletions bootstrapper/helpers/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';
const express = require('express');
const path = require('path');
const config = require('../config/')
const winston = require('winston');

module.exports = function(app) {
const isDebugMode = (process.env.NODE_ENV != 'production');
const defaultPort = (isDebugMode) ? config.dev.port : config.port.port;
const databasePath = (isDebugMode) ? config.dev.databasePath : config.port.databasePath;

app.set('databasePath', databasePath);
app.set('port', process.env.PORT || defaultPort);
app.set('dist', path.join(__dirname, '../dist'));

winston.loggers.add('command-logger', {
console: {
level: 'info',
colorize: true,
label: 'Command history'
},
file: { filename: 'tmp/commands.log' }
});

winston.loggers.add('logger', {
console: {
level: 'info',
colorize: true,
label: 'Logger'
},
file: { filename: 'tmp/info.log' }
});
};
27 changes: 27 additions & 0 deletions bootstrapper/helpers/socket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict'
const socketioAuth = require("socketio-auth");
const socketEvents = require('../app/router/socket.js');

const authenticate = async (socket, data, callback) => {
var username = data.username;
var password = data.password;
var check = (username == 'admin' && password == 'secret');

console.log("Authentication attempt: " + ((check) ? 'success' : 'failure'));

return callback(null, check);
}

const disconnected = socket => {
console.log("Socket with id " + socket.id + " disconnected.");
}

module.exports = function (server) {
const socketio = require('socket.io')(server);
socketioAuth(socketio, {
authenticate: authenticate,
disconnect: disconnected,
postAuthenticate: socketEvents,
timeout: "none"
});
}
9 changes: 9 additions & 0 deletions bootstrapper/nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"ignore": [
".git", "node_modules/**/node_modules", "*.json", "config/*", "web/*", "nodemon.json", "Makefile", "tmp"
],
"events": {
"restart": "",
"start": ""
}
}
20 changes: 20 additions & 0 deletions bootstrapper/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "raspy-bootstrapper",
"version": "1.0.0",
"description": "Bootstrapper for Raspy",
"private": true,
"scripts": {
"dev": "nodemon app.js",
"start:client": "cd web && npm start",
"start:server": "npm run dev",
"build": "node web/build/build.js"
},
"dependencies": {
"express": "^4.16.3",
"express-basic-auth": "^1.1.5",
"ps-node": "^0.1.6",
"socket.io": "^2.1.1",
"socketio-auth": "^0.1.1",
"winston": "^2.4.3"
}
}
12 changes: 12 additions & 0 deletions bootstrapper/web/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"presets": [
["env", {
"modules": false,
"targets": {
"browsers": ["> 1%", "last 2 versions", "not ie <= 8"]
}
}],
"stage-2"
],
"plugins": ["transform-vue-jsx", "transform-runtime"]
}
9 changes: 9 additions & 0 deletions bootstrapper/web/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
13 changes: 13 additions & 0 deletions bootstrapper/web/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.DS_Store
node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Editor directories and files
.idea
.vscode
*.suo
*.ntvs*
*.njsproj
*.sln
10 changes: 10 additions & 0 deletions bootstrapper/web/.postcssrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// https://github.com/michael-ciniawsky/postcss-load-config

module.exports = {
"plugins": {
"postcss-import": {},
"postcss-url": {},
// to edit target browsers: use "browserslist" field in package.json
"autoprefixer": {}
}
}
Loading

0 comments on commit 62c8ed8

Please sign in to comment.