Created
July 28, 2017 06:34
-
-
Save freeart/486c49939ca16434988607c58853461f to your computer and use it in GitHub Desktop.
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
from nginxparser import load, dumps | |
import re | |
import firebase_admin | |
from firebase_admin import credentials | |
from firebase_admin import db | |
conf = load(open("./xxx.conf")) | |
configuration = { | |
'credential': credentials.Certificate("xxx"), | |
'options': { 'databaseURL': "https://xxx.firebaseio.com" } | |
} | |
firebase_admin.initialize_app(**configuration) | |
env = db.reference("environment") | |
new = env.get() | |
def find_ap(src): | |
for path in src: | |
upstream = re.match(r"(ap-.+)-([0-9_]+)", path) | |
if upstream: | |
section = upstream.group(1) | |
ip = upstream.group(2).replace("_", ".") | |
yield {'cfg': src[path], 'path': path, 'section': section, 'ip': ip} | |
def find_upstream(conf): | |
for block in conf: | |
if block[0][0] == "upstream": | |
upstream = re.match(r"(ap-.+)", block[0][1]) | |
if upstream: | |
yield block | |
upstreams = find_upstream(conf) | |
aps = find_ap(new) | |
for upstream in upstreams: | |
exists = False | |
for ap in aps: | |
if ap['section'] == upstream[0][1]: | |
if exists == False: | |
upstream[1] = [['keepalive', '64']] | |
exists = True | |
upstream[1].append(['server', ap['ip'] + ":" + `ap['cfg']['port']`]) | |
print dumps(conf) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment