Browse Source

Added feature:

All servers have been killed.
A backup of your old servers.up is saved in /etc/up-serve/servers.bak.up.
Check this if you need to.
pull/6/head
Muthu Kumar 7 years ago
parent
commit
1af16daf09
  1. 3
      .eslintrc.json
  2. 10
      README.md
  3. 6
      actions/createProxyServer.js
  4. 8
      actions/createStaticServer.js
  5. 20
      actions/killALL.js
  6. 136
      build/defaultNginx.conf
  7. 13
      index.js
  8. 2
      package.json
  9. 31
      utils/listFile.js
  10. 36
      utils/nginxPath.js

3
.eslintrc.json

@ -17,6 +17,7 @@
"semi": [
"error",
"always"
]
],
"prefer-const": "error"
}
}

10
README.md

@ -8,9 +8,9 @@
# up
> Current version: `up v.0.1.8 (Pre-Alpha)`
> Current version: `up v.0.1.9 (Pre-Alpha)`
> Notes: `up` is coming soon to `Alpha`! 🎉 Changelog will be added from `up v.0.2.0` [(Alpha/MVP)](Roadmap.md)\
> Notes: `up` has reached MVP! 🎉 Changelog will be added from alpha version: `up v.0.2.0` [(Roadmap)](Roadmap.md)\
> ⚠️ ❌ `up` is still not ready for use yet! Do not attempt to use this in development or production until alpha!
**`up`** is a command line application that creates nginx server blocks quickly with a single command.
@ -18,7 +18,7 @@
## Installation
As of now, `up` only supports Debian and Ubuntu based distros. Support for more distros will come soon. Add an issue to bump this process.
`up` currently supports nginx mainline and nginx stable on Linux based distros. Support for more distros will come soon. Add an issue to bump this process.
You will need to have [_node JS_](https://nodejs.org) and [_nginx_](https://nginx.org) installed.
@ -28,13 +28,13 @@ Install `up` from npm:
> `up` is now available as a command.
## Commands
## Basic Commands
Format: `up command <required> [optional]`
- `up static <domain> [outbound port]` - Create new static server at current folder.
- `up proxy <domain> <inbound port> [outbound port]` - Create new proxy server listening at said port.
- `up list` - List currently available servers. (Doesn't work yet)
- `up list` - List currently available servers.
- `up kill <domain>` - Kill the server for this domain.
## Examples

6
actions/createProxyServer.js

@ -9,6 +9,9 @@ var appendToList = require('../utils/listFile').appendToList;
var EOL = require('os').EOL; // \n if used on Linux, \r\n if used on Windows.
function createProxyServer(domain, inPort, outPort) {
outPort = outPort || 80;
shell.mkdir('-p', npath.confD());
fs.outputFileSync((conf(npath.confD(), domain, outPort)),
"server {" + EOL +
" listen " + outPort + ";" + EOL +
@ -26,7 +29,8 @@ function createProxyServer(domain, inPort, outPort) {
" }" + EOL +
"}"
);
shell.mkdir('-p', npath.enabledSites()); // Creates directory if doesn't exist
shell.mkdir('-p', npath.confD());
shell.mkdir('-p', npath.enabledSites()); // Creates directories if doesn't exist
shell.ln('-sf', conf(npath.confD(), domain, outPort), conf(npath.enabledSites(), domain, outPort)); // Symlink the conf file from sites-available to sites-enabled
appendToList(domain, outPort, inPort);

8
actions/createStaticServer.js

@ -12,11 +12,13 @@ var EOL = require('os').EOL; // \n if used on Linux, \r\n if used on Windows.
function createStaticServer(domain, outPort) {
outPort = outPort || 80;
shell.mkdir('-p', npath.confD());
fs.outputFileSync((conf(npath.confD(), domain, outPort)), // Gets nginx's paths from nginxPath.js
"server {" + EOL +
" listen " + outPort + ";" + EOL +
" listen [::]:" + outPort + ";" + EOL +
" root " + npath.webRoot() + domain + ";" + EOL +
" root " + npath.webRoot() + domain + "." + outPort + ";" + EOL +
" index index.html index.htm;" + EOL +
"" + EOL +
" server_name " + domain + ";" + EOL +
@ -25,9 +27,9 @@ function createStaticServer(domain, outPort) {
" }" + EOL +
"}"
);
shell.mkdir('-p', npath.enabledSites()); // Creates directory if doesn't exist
shell.mkdir('-p', npath.enabledSites()); // Creates directories if doesn't exist
shell.rm('-rf', conf(npath.enabledSites(), domain, outPort)); // Removes domain from sites-enabled if exists
shell.ln('-sf', conf(npath.confD(), domain, outPort), conf(npath.enabledSites(), domain, outPort)); // Symlink the conf file from sites-available to sites-enabled
shell.ln('-sf', conf(npath.confD(), domain, outPort), conf(npath.enabledSites(), domain, outPort)); // Symlink the conf file from confD to sites-enabled
shell.rm('-rf', npath.webRootDomain(domain, outPort)); // Removes domain from webroot if exists
shell.mkdir('-p', npath.webRoot()); // Creating the nginx www path if it doesn't exist so symlink doesn't fail
shell.ln('-sf', currentPath, npath.webRootDomain(domain, outPort)); // Symlink current directory to nginx's web root

20
actions/killALL.js

@ -0,0 +1,20 @@
var shell = require('shelljs');
var npath = require('../utils/nginxPath');
var conf = require('../utils/nginxConf');
function killALL () {
shell.rm('-Rf', npath.serversBakUp);
shell.mv(npath.serversUp(), npath.serversBakUp());
shell.rm('-Rf', npath() + "sites-available");
shell.rm('-Rf', npath.confD());
shell.rm('-Rf', npath.enabledSites());
shell.rm('-Rf', npath.webRoot());
shell.mkdir('-p', npath.confD());
shell.mkdir('-p', npath.enabledSites());
shell.mkdir('-p', npath.webRoot());
shell.cp('./build/defaultNginx.conf', conf(npath.confD())); // Create the default.conf file
shell.ln('-sf', npath.confD() + "default.conf", npath.enabledSites() + "default.conf"); // Symlink the default.conf file from confD to sites-enabled
}
module.exports = killALL;

136
build/defaultNginx.conf

@ -0,0 +1,136 @@
#user nobody;
#Defines which Linux system user will own and run the Nginx server
worker_processes 1;
#Referes to single threaded process. Generally set to be equal to the number of CPUs or cores.
#error_log logs/error.log; #error_log logs/error.log notice;
#Specifies the file where server logs.
#pid logs/nginx.pid;
#nginx will write its master process ID(PID).
events {
worker_connections 1024;
# worker_processes and worker_connections allows you to calculate maxclients value:
# max_clients = worker_processes * worker_connections
}
http {
include mime.types;
# anything written in /opt/nginx/conf/mime.types is interpreted as if written inside the http { } block
default_type application/octet-stream;
#
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
# If serving locally stored static files, sendfile is essential to speed up the server,
# But if using as reverse proxy one can deactivate it
#tcp_nopush on;
# works opposite to tcp_nodelay. Instead of optimizing delays, it optimizes the amount of data sent at once.
#keepalive_timeout 0;
keepalive_timeout 65;
# timeout during which a keep-alive client connection will stay open.
#gzip on;
# tells the server to use on-the-fly gzip compression.
server {
# You would want to make a separate file with its own server block for each virtual domain
# on your server and then include them.
listen 80;
#tells Nginx the hostname and the TCP port where it should listen for HTTP connections.
# listen 80; is equivalent to listen *:80;
server_name localhost;
# lets you doname-based virtual hosting
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#The location setting lets you configure how nginx responds to requests for resources within the server.
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}

13
index.js

@ -1,5 +1,7 @@
#!/usr/bin/env node
'use strict';
// Requiring npm modules
var program = require('commander');
var chalk = require('chalk');
@ -10,6 +12,7 @@ var createProxyServer = require('./actions/createProxyServer');
var createStaticServer = require('./actions/createStaticServer');
var killServer = require('./actions/killServer');
var listServers = require('./actions/listServers');
var killALL = require('./actions/killALL');
// Requiring utils
var validate = require('./utils/validate');
@ -62,6 +65,16 @@ program
});
program
.command('kill-all')
.description('Warning! Will completely kill all servers and reset nginx')
.action(function() {
killALL();
console.log("\nAll servers have been killed.\n" +
"A backup of your old servers.up is saved in /etc/up-serve/servers.bak.up.\n" +
"Check this if you need to.\n");
});
program
.command('*') // This should pick invalid commands, but it doesn't, yet.
.action(function () {
console.log("\nInvalid command. Type " + chalk.cyan('up --help') + " for help.\n");

2
package.json

@ -1,6 +1,6 @@
{
"name": "up-serve",
"version": "0.1.8",
"version": "0.1.9",
"description": "A cli tool to quickly create and manage nginx server blocks.",
"main": "index.js",
"scripts": {

31
utils/listFile.js

@ -1,8 +1,7 @@
var fs = require('fs-extra');
var removeFromArray = require('./removeFromArray');
var listFilePath = "/etc/up-serve/servers.up";
var listFilePath = require('./nginxPath').serversUp;
function appendToList(domain, outPort, inPort) {
@ -20,36 +19,30 @@ function appendToList(domain, outPort, inPort) {
domBlock.inPort = inPort;
}
if (fs.existsSync(listFilePath)) {
var jsonBuffer = JSON.parse(fs.readFileSync(listFilePath));
if (fs.existsSync(listFilePath())) {
var jsonBuffer = JSON.parse(fs.readFileSync(listFilePath()));
jsonFile.domains = removeFromArray(jsonBuffer.domains, domain, outPort);
console.log(jsonFile);
}
console.log(jsonFile);
console.log(jsonFile.domains);
jsonFile.domains.push(domBlock);
console.log(jsonFile);
jsonFile = JSON.stringify(jsonFile, null, '\t');
console.log(jsonFile);
fs.writeFileSync(listFilePath, jsonFile);
fs.writeFileSync(listFilePath(), jsonFile);
}
function removeFromList (domain, outPort) {
var jsonFile = { "domains": [] };
if (fs.existsSync(listFilePath)) {
jsonFile = fs.readFileSync(listFilePath);
jsonFile = JSON.parse(jsonFile);
jsonFile = removeFromArray(jsonFile.domains, domain, outPort);
if (fs.existsSync(listFilePath())) {
var jsonFile = { "domains": [] };
var jsonBuffer = JSON.parse(fs.readFileSync(listFilePath()));
jsonFile.domains = removeFromArray(jsonBuffer.domains, domain, outPort);
jsonFile = JSON.stringify(jsonFile, null, '\t');
fs.writeFileSync(listFilePath, jsonFile);
jsonFile = JSON.stringify(jsonBuffer, null, '\t');
fs.writeFileSync(listFilePath(), jsonFile);
}
else console.log("\nNo servers were created using `up` yet.\n");
}
function readServers () {
var serversList = JSON.parse(fs.readFileSync(listFilePath));
var serversList = JSON.parse(fs.readFileSync(listFilePath()));
if(!serversList.domains[0]) return undefined;
return serversList;
}

36
utils/nginxPath.js

@ -1,15 +1,24 @@
'use strict';
// These functions just return paths. Later, these should be modified to poll from nginx's config.
var enabled = "/etc/nginx/sites-enabled/";
var conf = "/etc/nginx/conf.d/";
var wwwRoot = "/etc/up-serve/static/";
var npath = "/etc/nginx/";
var enabled = npath + "sites-enabled/";
var confDpath = npath + "conf.d/";
var upPath = "/etc/up-serve/";
var wwwRoot = upPath + "static/";
var serverListPath = upPath + "servers";
function nginxPath() {
return npath;
}
function enabledSites() {
return enabled;
}
function confD() {
return conf;
return confDpath;
}
function webRoot() {
@ -17,11 +26,24 @@ function webRoot() {
}
function webRootDomain(domain, outPort) {
var rootWithDomain = wwwRoot + domain + "." + outPort;
return rootWithDomain;
const path = wwwRoot + domain + "." + outPort;
return path;
}
function serversUp() {
const path = serverListPath + ".up";
return path;
}
function serversBakUp() {
const path = serverListPath + ".bak.up";
return path;
}
module.exports = nginxPath;
module.exports.confD = confD;
module.exports.enabledSites = enabledSites;
module.exports.webRoot = webRoot;
module.exports.webRootDomain = webRootDomain;
module.exports.webRootDomain = webRootDomain;
module.exports.serversUp = serversUp;
module.exports.serversBakUp = serversBakUp;
Loading…
Cancel
Save