Last active
August 29, 2015 14:02
-
-
Save stef-k/5d202fa3a05bd320488b to your computer and use it in GitHub Desktop.
Command line tool to enable - disable and list all sites for the Nginx server.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env rdmd | |
| /* | |
| The MIT License (MIT) | |
| Copyright (c) 2014 Stef Kariotidis | |
| Permission is hereby granted, free of charge, to any person obtaining a copy | |
| of this software and associated documentation files (the "Software"), to deal | |
| in the Software without restriction, including without limitation the rights | |
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| copies of the Software, and to permit persons to whom the Software is | |
| furnished to do so, subject to the following conditions: | |
| The above copyright notice and this permission notice shall be included in | |
| all copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| THE SOFTWARE. | |
| */ | |
| /** Command line tool to enable - disable and list all sites for the Nginx server. | |
| * Version: 0.2 | |
| * | |
| * Examples: | |
| * sudo xnable list // lists all available and enabled configurations | |
| * | |
| * sudo xnable en site_name // enables the given configuration | |
| * | |
| * sudo xnable dis site_name // disables the given configuration | |
| * | |
| * Authors: Stef Kariotidis stef[dot]kariotidis[at]gmail[dot]com | |
| * License: MIT License | |
| */ | |
| module xnable; | |
| import std.stdio; | |
| import std.process; | |
| import std.file; | |
| // the root directory where Nginx has it's configurations files | |
| private string rootDir = "/etc/nginx"; | |
| // where are the configuration files | |
| private string sourceDir = "/sites-available/"; | |
| // where are the enabled (soft links) configurations | |
| private string targetDir = "/sites-enabled/"; | |
| void main(string[] args) { | |
| if (!check(args)) | |
| { | |
| writeln("usage: xnable en|dis site_name"); | |
| } | |
| else | |
| { | |
| if(std.file.exists(rootDir ~ sourceDir) && | |
| std.file.exists(rootDir ~ targetDir)) | |
| { | |
| if(args.length == 2) | |
| { | |
| list(); | |
| } | |
| else if (args[1] == "en") | |
| { | |
| enable(args[2]); | |
| } | |
| else if (args[1] == "dis") | |
| { | |
| disable(args[2]); | |
| } | |
| } | |
| else | |
| { | |
| writefln("Directory does not exist, "); | |
| writeln("make sure that /etc/ngix/sites-available and"); | |
| writeln("/etc/ngix/sites-enabled exist."); | |
| } | |
| } | |
| } | |
| // Dumb check arguments | |
| private int check(string[] args) | |
| { | |
| bool result = false; | |
| foreach (arg; args) | |
| { | |
| if (args.length == 3 && (args[1] == "en" || args[1] == "dis")) | |
| { | |
| result = true; | |
| } | |
| else if (args.length == 2 && args[1] == "list") | |
| { | |
| result = true; | |
| } | |
| } | |
| return result; | |
| } | |
| /** | |
| * Enable a site configuration | |
| * Params: | |
| * siteName = The name of the configuration to be enabled | |
| */ | |
| void enable(string siteName) | |
| { | |
| auto source = rootDir ~ sourceDir ~ siteName; | |
| auto target = rootDir ~ targetDir ~ siteName; | |
| auto cmd = "ln -s " ~ source ~ " " ~ target; | |
| auto proc = executeShell(cmd); | |
| if (proc.status != 0) | |
| { | |
| writeln("Failed to enable " ~ siteName); | |
| writeln(proc); | |
| } | |
| else | |
| { | |
| writeln(siteName ~ " enabled."); | |
| } | |
| } | |
| /** | |
| * Disable a site configuration | |
| * Params: | |
| * siteName = The name of the configuration to be disabled. | |
| */ | |
| void disable(string siteName) | |
| { | |
| auto target = rootDir ~ targetDir; | |
| auto cmd = "cd " ~ target ~ "&& " ~ "rm " ~ siteName; | |
| auto entries = dirEntries(target, "*.*", SpanMode.shallow, false); | |
| auto proc = executeShell(cmd); | |
| if (proc.status != 0) | |
| { | |
| writeln("Failed to disable " ~ siteName); | |
| writeln(proc); | |
| } | |
| else | |
| { | |
| writeln(siteName ~ " removed."); | |
| } | |
| } | |
| /** | |
| * List both all available and enabled configurations | |
| */ | |
| void list() | |
| { | |
| auto source = rootDir ~ sourceDir; | |
| auto target = rootDir ~ targetDir; | |
| auto entries = dirEntries(source, "*", SpanMode.shallow, false); | |
| writeln("\nsites-available:"); | |
| foreach(entry; entries) | |
| { | |
| writeln(entry); | |
| } | |
| auto ent = dirEntries(target, "*", SpanMode.shallow, false); | |
| writeln("\nsites-enabled:"); | |
| foreach (e; ent) | |
| { | |
| writeln(e); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment