|
| 1 | +#/usr/bin/env python |
| 2 | + |
| 3 | +# This script sits on an Nginx server, and is run by remote Apache/Nginx |
| 4 | +# or app servers to register themselves in a cluster with Nginx. We are using |
| 5 | +# this on EC2, to let web/php servers register with the Nginx load balancer. |
| 6 | + |
| 7 | +# Assumes nginx.conf has an upstream default in the config like so: |
| 8 | +# upstream default { include /etc/nginx/default.upstream; } |
| 9 | +# This file is simply a list of hostnames. |
| 10 | +# You may include other upstream files, in the form: |
| 11 | +# upstream cluster_name { include /etc/nginx/cluster_name.upstream; } |
| 12 | + |
| 13 | +# Usage: python register-nginx.py [-d/--deregister] client_hostname [cluster_name] |
| 14 | + |
| 15 | +# By the way, I know this is UGLY. But it was quick and dirty. Some day, I might |
| 16 | +# fix it. |
| 17 | + |
| 18 | +import sys |
| 19 | + |
| 20 | +DEBUG = False |
| 21 | + |
| 22 | +if DEBUG: |
| 23 | + file_template = "%s.upstream" |
| 24 | +else: |
| 25 | + file_template = "/etc/nginx/%s.upstream" |
| 26 | + |
| 27 | +def register(hostname, cluster="default"): |
| 28 | + # See if the hostname exists already |
| 29 | + with open(file_template % cluster) as cluster_file: |
| 30 | + for line in cluster_file.readlines(): |
| 31 | + if hostname + '\n' == line: |
| 32 | + print "Host %s already registered with cluster %s" % (hostname, cluster) |
| 33 | + sys.exit(1) |
| 34 | + # Append the new hostname to the file |
| 35 | + with open(file_template % cluster, 'a') as cluster_file: |
| 36 | + cluster_file.write(hostname + '\n') |
| 37 | +def deregister(hostname, cluster="default"): |
| 38 | + new_file = [] |
| 39 | + # Read through the file, add all lines that aren't the hostname to a new array |
| 40 | + with open(file_template % cluster, 'r') as cluster_file: |
| 41 | + lines = cluster_file.readlines() |
| 42 | + old_file_len = len(lines) |
| 43 | + for line in lines: |
| 44 | + print hostname, line |
| 45 | + if hostname + '\n' != line: |
| 46 | + new_file.append(line) |
| 47 | + if len(new_file) == old_file_len: |
| 48 | + print "Host %s not registered with cluster %s" % (hostname, cluster) |
| 49 | + sys.exit(2) |
| 50 | + # Write the array, minus the deregistered hostname, to the file |
| 51 | + with open(file_template % cluster, 'w') as cluster_file: |
| 52 | + for line in new_file: |
| 53 | + cluster_file.write(line) |
| 54 | + |
| 55 | +if __name__ == "__main__": |
| 56 | + # arv[0] == appname, argv[1] == first arg, etc If len == 2: default register, 3: deregister OR other cluster. 4: deregister other cluster |
| 57 | + if len(sys.argv) == 2: |
| 58 | + register(sys.argv[1]) |
| 59 | + elif len(sys.argv) == 3: |
| 60 | + if sys.argv[1] == '-d' or sys.argv[1] == '--deregister': |
| 61 | + deregister(sys.argv[2]) |
| 62 | + else: |
| 63 | + register(sys.argv[1], sys.argv[2]) |
| 64 | + elif len(sys.argv) == 4: |
| 65 | + deregister(sys.argv[2], sys.argv[3]) |
| 66 | + else: |
| 67 | + print "usage:" |
| 68 | + print "python register-nginx.py [-d/--deregister] hostname [cluster_name]" |
0 commit comments