Last active
August 29, 2015 13:57
-
-
Save oskarhane/9483970 to your computer and use it in GitHub Desktop.
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/python3 | |
# Writter by Oskar Hane | |
import subprocess | |
import sys | |
import re | |
conf_dir = '/etc/nginx/sites-enabled/' | |
command = ["/usr/bin/docker ps | grep " + sys.argv[1]] | |
p = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True) | |
output, err = p.communicate() | |
d_str = output.decode("utf-8") | |
ports = re.findall(':([0-9]+)->([0-9]+)', d_str) | |
domain = re.findall('\s([\S]+)\s*$', d_str) | |
if not (re.search('(\.[a-z]{2,4})$', domain[0])): | |
print('Not a domain') | |
exit() | |
proxy_port = 0 | |
for i in range(len(ports)): | |
if(''.join(ports[i][1]) != "80"): | |
continue | |
proxy_port = ''.join(ports[i][0]) | |
break | |
if(proxy_port == 0): | |
print('Port 80 not open') | |
exit() | |
f = open(conf_dir + domain[0], 'w+') | |
f.write('upstream ' + sys.argv[1] + " {\n") | |
f.write("\tserver 127.0.0.1:" + proxy_port + ";\n}\n") | |
listen_str = 'server {\n' | |
listen_str += "\tlisten 80;\n" | |
listen_str += "\tserver_name " + domain[0] + ";\n" | |
listen_str += "\tlocation / {\n" | |
listen_str += "\t\tproxy_pass http://" + sys.argv[1] + ";\n" | |
listen_str += "\t\tproxy_set_header X-Real-IP $remote_addr;\n" | |
listen_str += "\t\tproxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n" | |
listen_str += "\t\tproxy_set_header X-NginX-Proxy true;\n" | |
listen_str += "\t\tproxy_set_header Host $host;\n" | |
listen_str += "\t\tproxy_redirect off;\n" | |
listen_str += "\t}\n" | |
listen_str += "}" | |
f.write(listen_str) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment