diff --git a/.eslintrc.json b/.eslintrc.json index 1c0dbe6..b5c6517 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -17,6 +17,7 @@ "semi": [ "error", "always" - ] + ], + "prefer-const": "error" } } \ No newline at end of file diff --git a/README.md b/README.md index 960a593..2f24478 100644 --- a/README.md +++ b/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 [optional]` - `up static [outbound port]` - Create new static server at current folder. - `up proxy [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 ` - Kill the server for this domain. ## Examples diff --git a/actions/createProxyServer.js b/actions/createProxyServer.js index d38f984..6a3285a 100644 --- a/actions/createProxyServer.js +++ b/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); diff --git a/actions/createStaticServer.js b/actions/createStaticServer.js index 65e33e4..45619e9 100644 --- a/actions/createStaticServer.js +++ b/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 diff --git a/actions/killALL.js b/actions/killALL.js new file mode 100644 index 0000000..5726dc9 --- /dev/null +++ b/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; \ No newline at end of file diff --git a/build/defaultNginx.conf b/build/defaultNginx.conf new file mode 100644 index 0000000..1e6c68f --- /dev/null +++ b/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; + # } + #} + +} \ No newline at end of file diff --git a/index.js b/index.js index f34d099..7650e66 100755 --- a/index.js +++ b/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"); diff --git a/package.json b/package.json index 1064781..3aee64a 100644 --- a/package.json +++ b/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": { diff --git a/utils/listFile.js b/utils/listFile.js index afd896d..7e5b688 100644 --- a/utils/listFile.js +++ b/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; } diff --git a/utils/nginxPath.js b/utils/nginxPath.js index 73666c0..7d1b698 100644 --- a/utils/nginxPath.js +++ b/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; \ No newline at end of file +module.exports.webRootDomain = webRootDomain; +module.exports.serversUp = serversUp; +module.exports.serversBakUp = serversBakUp; \ No newline at end of file