Created
November 16, 2015 21:00
-
-
Save ArmedGuy/f9a3367c4bf2b6b767eb to your computer and use it in GitHub Desktop.
Script for running a Nginx edge server that reverse proxies to internal servers.
This file contains 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 python | |
import sys | |
import os | |
import os.path | |
import subprocess | |
nginx_conf = """ | |
server { | |
server_name %s; | |
root /var/www/html; | |
index index.html; | |
location / { | |
proxy_pass http://%s; | |
try_files $uri $uri/ =404; | |
} | |
} | |
""" | |
config_path = "/etc/nginx/sites/%s" | |
def exists(conf): | |
global config_path | |
return os.path.isfile(config_path % ("%s.conf" % conf)) or os.path.isfile(config_path % ("%s.conf.disabled" % conf)) | |
def isenabled(conf): | |
global config_path | |
return exists(conf) and not os.path.isfile(config_path % ("%s.conf.disabled" % conf)) | |
def isroot(): | |
process = subprocess.Popen(["whoami"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
stdout, stderr = process.communicate() | |
return stdout.strip() == "root" | |
def reload(): | |
subprocess.call("service nginx reload", shell=True) | |
if __name__ == "__main__": | |
if not isroot(): | |
print "Please run as root" | |
exit() | |
args = sys.argv[1:] | |
argc = len(args) | |
if argc == 0: | |
print "Too few arguments" | |
exit() | |
if args[0] == "add": | |
if argc != 3: | |
print "Too few arguments" | |
exit() | |
conf = args[1] | |
if exists(conf): | |
print "Config already exists" | |
if not isenabled(conf): | |
print " - but not enabled, please use webedge enable" | |
exit() | |
f = open(config_path % ("%s.conf.disabled" % conf), "w") | |
f.write(nginx_conf % (args[1], args[2])) | |
f.close() | |
print "%s configured to %s, please use webedge enable to enable the configuration" % (args[1], args[2]) | |
elif args[0] == "remove": | |
if len(args) != 2: | |
print "Too few arguments" | |
exit() | |
conf = args[1] | |
if not exists(conf): | |
print "Config does not exist" | |
exit() | |
if isenabled(conf): | |
print "Config is enabled, please disable before removing" | |
exit() | |
os.remove(config_path % ("%s.conf.disabled" % conf)) | |
print "Config %s has been removed" % conf | |
elif args[0] == "enable": | |
if len(args) != 2: | |
print "Too few arguments" | |
exit() | |
conf = args[1] | |
if not exists(conf): | |
print "Config does not exist" | |
exit() | |
if not isenabled(conf): | |
os.rename(config_path % ("%s.conf.disabled" % conf), config_path % ("%s.conf" % conf)) | |
print "%s has been enabled, reloading nginx" % conf | |
reload() | |
else: | |
print "Config already enabled!" | |
elif args[0] == "disable": | |
if len(args) != 2: | |
print "Too few arguments" | |
exit() | |
conf = args[1] | |
if not exists(conf): | |
print "Config does not exist" | |
exit() | |
if not isenabled(conf): | |
print "Config is already disabled!" | |
else: | |
os.rename(config_path % ("%s.conf" % conf), config_path % ("%s.conf.disabled" % conf)) | |
print "%s has been disabled, reloading nginx" % conf | |
reload() | |
elif args[0] == "list": | |
files = os.listdir(config_path.replace("%s", "")) | |
for f in files: | |
print f | |
else: | |
print "Unknown command" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment