예제 #1
0
def handle_app(appname):
    with open(path) as json_data:
        data = json.load(json_data)

    verbose = False
    chosen_region = None
    ret = {}
    query_string = request.query_string

    if not query_string == "":
        for query in query_string.split("&"):
            if "verbose" in query.lower():
                if query.endswith("1"):
                    verbose = True
            elif "region" in query.lower():
                chosen_region = query[7:]

    if verbose:
        print request.url
    redir = None
    if nohttps == None:
        proto = request.headers.get("X-Forwarded-Proto")
        if not proto == "https":
            redir = _check_ssl(request.url, verbose)
    if not redir == None:
        return redir

    for app in data["apps"]:
        if appname.lower() == app["name"].lower():
            app_config = app["config"]

            for config in app_config:

                if config.get("s3filepath"):
                    datapath = config.get("localpath")
                    awslib._get_file(bucket_name, config["s3filepath"], datapath)
                    with open(datapath) as filedata:
                        output = json.load(filedata)
                    return jsonify(**output)
                elif config.get("R53"):
                    ret = {}
                    for item in config["R53"]:
                        ret[item["Name"]] = {}
                        ret[item["Name"]]["all_ips"] = []
                        ret[item["Name"]]["all_ips"] = awslib._get_records_from_zone(
                            item["HostedZoneId"], item["Pattern"], item["Domain"]
                        )
                    return jsonify(**ret)

                dnsname = config["dnsname"]
                bs_app = config["beanstalk_app_name"]
                region = config["region"]

                if not chosen_region == None:
                    if not region == chosen_region:
                        continue

                exclusions = config["exclusions"]
                eip_check = config.get("show_eip")
                lb_check = config.get("show_lb_ip")
                inst_check = config.get("show_inst_ip")
                if ret.get(region) == None:
                    ret[region] = {}
                lb_name = awslib._active_balancer(dnsname, region)

                if ret[region].get("all_ips") == None:
                    ret[region]["all_ips"] = []

                if not eip_check == None:
                    eips = awslib._list_eips(region, filter=exclusions)
                    if verbose:
                        if ret[region].get("eips") == None:
                            ret[region]["eips"] = eips
                        else:
                            ret[region]["eips"].extend(eips)

                    if eip_check:
                        ret[region]["all_ips"].extend(eips)

                if not lb_check == None:
                    lb_url = awslib._environment_descr(bs_app, lb_name, region)
                    elb = awslib._balancer_ip(lb_url)

                    if verbose:
                        if ret[region].get("elb") == None:
                            ret[region]["elb"] = elb
                        else:
                            ret[region]["elb"].extend(elb)

                    if lb_check:
                        ret[region]["all_ips"].extend(elb)

                if not inst_check == None:
                    inst_ips = awslib._instance_ip(lb_name, region)
                    if verbose:
                        if ret[region].get("instance_ips") == None:
                            ret[region]["instance_ips"] = inst_ips
                        else:
                            ret[region]["instance_ips"].extend(inst_ips)

                    if inst_check:
                        ret[region]["all_ips"].extend(inst_ips)

    if not ret:
        return redirect(url_for("handle_index"), code=302)
    else:
        return jsonify(**ret)
예제 #2
0
import awslib
import os


bucket_name = os.environ.get("IPLIST_CONFIG_BUCKET")
s3path = os.environ.get("IPLIST_CONFIG_PATH")
nohttps = os.environ.get("NOHTTPS")

path = join("iplist_config", "config.json")

if s3path == None:
    print "No Env Labeled IPLIST_CONFIG_PATH"
elif bucket_name == None:
    print "No bucket name specified"
else:
    awslib._get_file(bucket_name, s3path, path)


@app.route("/")
def handle_index():
    redir = None
    if nohttps == None:
        proto = request.headers.get("X-Forwarded-Proto")
        if not proto == "https":
            redir = _check_ssl(request.url)

    if not redir == None:
        return redir

    with open(path) as json_data:
        data = json.load(json_data)
def handle_app(appname):
    with open(path) as json_data:
        data = json.load(json_data)

    verbose = False
    chosen_region = None
    ret = {}
    query_string = request.query_string

    if not query_string == "":
        for query in query_string.split('&'):
            if "verbose" in query.lower():
                if query.endswith("1"):
                    verbose = True
            elif "region" in query.lower():
                chosen_region = query[7:]

    if verbose:
        print request.url
    redir = None
    if nohttps == None:
        proto = request.headers.get("X-Forwarded-Proto")
        if not proto == "https":
            redir = _check_ssl(request.url, verbose)
    if not redir == None:
        return redir

    for app in data['apps']:
        if appname.lower() == app['name'].lower():
            app_config = app['config']

            for config in app_config:

                if config.get('s3filepath'):
                    datapath = config.get('localpath')
                    awslib._get_file(bucket_name, config['s3filepath'],
                                     datapath)
                    with open(datapath) as filedata:
                        output = json.load(filedata)
                    return jsonify(**output)
                elif config.get('R53'):
                    ret = {}
                    for item in config['R53']:
                        ret[item['Name']] = {}
                        ret[item['Name']]['all_ips'] = []
                        ret[item['Name']][
                            'all_ips'] = awslib._get_records_from_zone(
                                item['HostedZoneId'], item['Pattern'],
                                item['Domain'])
                    return jsonify(**ret)

                dnsname = config['dnsname']
                bs_app = config['beanstalk_app_name']
                region = config['region']

                if not chosen_region == None:
                    if not region == chosen_region:
                        continue

                exclusions = config['exclusions']
                eip_check = config.get('show_eip')
                lb_check = config.get('show_lb_ip')
                inst_check = config.get('show_inst_ip')
                if ret.get(region) == None:
                    ret[region] = {}
                lb_name = awslib._active_balancer(dnsname, region)

                if ret[region].get('all_ips') == None:
                    ret[region]['all_ips'] = []

                if not eip_check == None:
                    eips = awslib._list_eips(region, filter=exclusions)
                    if verbose:
                        if ret[region].get('eips') == None:
                            ret[region]['eips'] = eips
                        else:
                            ret[region]['eips'].extend(eips)

                    if eip_check:
                        ret[region]['all_ips'].extend(eips)

                if not lb_check == None:
                    lb_url = awslib._environment_descr(bs_app, lb_name, region)
                    elb = awslib._balancer_ip(lb_url)

                    if verbose:
                        if ret[region].get('elb') == None:
                            ret[region]['elb'] = elb
                        else:
                            ret[region]['elb'].extend(elb)

                    if lb_check:
                        ret[region]['all_ips'].extend(elb)

                if not inst_check == None:
                    inst_ips = awslib._instance_ip(lb_name, region)
                    if verbose:
                        if ret[region].get('instance_ips') == None:
                            ret[region]['instance_ips'] = inst_ips
                        else:
                            ret[region]['instance_ips'].extend(inst_ips)

                    if inst_check:
                        ret[region]['all_ips'].extend(inst_ips)

    if not ret:
        return redirect(url_for('handle_index'), code=302)
    else:
        return jsonify(**ret)
예제 #4
0
def handle_app(appname):
    verbose = False
    chosen_region = None
    query_string = request.query_string

    if not query_string == "":
        for query in query_string.split('&'):
            if "verbose" in query.lower():
                if query.endswith("1"):
                    verbose = True
            elif "region" in query.lower():
                chosen_region = query[7:]
    suffix = ".json"

    if verbose:
        suffix = ".verbose" + suffix

    if chosen_region:
        suffix = "." + chosen_region + suffix

    app_cache_file = os.path.join(cache_root_directory,appname.lower() + suffix)

    read_from_cache = True
    try:
        print(app_cache_file)
        with open(app_cache_file, "r") as cache:
            cache_time = float(cache.readline().strip())
            current_time = time.time()
            if (current_time - cache_time) > cache_timeout_period_in_seconds:
                read_from_cache = False
    except IOError:
        read_from_cache = False

    if read_from_cache:
        print("Reading cached data for this request. here")
    else:
        print("Cache is out of date. Refreshing for this request.")

        try:
            with open(path) as json_data:
                data = json.load(json_data)

            ret = {}

            if verbose:
                print (request.url)
            redir = None
            if nohttps == None:
                proto = request.headers.get("X-Forwarded-Proto")
                if not proto == "https":
                    redir = _check_ssl(request.url, verbose)
            if not redir == None:
                return redir

            for app in data['apps']:
                # create url link for both name and alternative name for ip-range apps
                if appname.lower() == app['name'].lower() or appname.lower() == str(app.get('altname')).lower():
                    app_config = app['config']

                    for config in app_config:

                        if config.get('s3filepath'):
                            datapath = config.get('localpath')
                            awslib._get_file(bucket_name, config['s3filepath'], datapath)
                            with open(datapath) as filedata:
                                output = json.load(filedata)
                            break
                        elif config.get('R53'):
                            ret = {}
                            for item in config['R53']:
                                ret[item['Name']] = {}
                                ret[item['Name']]['all_ips'] = []
                                ret[item['Name']]['all_ips'] = awslib._get_records_from_zone(item['HostedZoneId'], item['Pattern'], item['Domain'])
                            break

                        dnsname = config['dnsname']
                        bs_app = config['beanstalk_app_name']
                        region = config['region']

                        # only run next section if region equal chosen_region
                        if chosen_region:
                            if chosen_region != region:
                                continue

                        exclusions = config['exclusions']
                        eip_check = config.get('show_eip')
                        lb_check = config.get('show_lb_ip')
                        inst_check = config.get('show_inst_ip')
                        if ret.get(region) == None:
                            ret[region] = {}
                        lb_name = awslib._active_balancer(dnsname, region)

                        if ret[region].get('all_ips') == None:
                            ret[region]['all_ips'] = []

                        if not eip_check == None:
                            eips = awslib._list_eips(region, filter=exclusions)
                            if verbose:
                                if ret[region].get('eips') == None:
                                    ret[region]['eips'] = eips
                                else:
                                    ret[region]['eips'].extend(eips)

                            if eip_check:
                                ret[region]['all_ips'].extend(eips)

                        if not lb_check == None:
                            lb_url = awslib._environment_descr(bs_app, lb_name, region)
                            elb = awslib._balancer_ip(lb_url)

                            if verbose:
                                if ret[region].get('elb') == None:
                                    ret[region]['elb'] = elb
                                else:
                                    ret[region]['elb'].extend(elb)

                            if lb_check:
                                ret[region]['all_ips'].extend(elb)

                        if not inst_check == None:
                            inst_ips = awslib._instance_ip(lb_name, region)
                            if verbose:
                                if ret[region].get('instance_ips') == None:
                                    ret[region]['instance_ips'] = inst_ips
                                else:
                                    ret[region]['instance_ips'].extend(inst_ips)

                            if inst_check:
                                ret[region]['all_ips'].extend(inst_ips)

            if not ret:
                return redirect(url_for('handle_index'), code=302)
            else:
                #sort ip list in ret when it can
                ret = ip_list_sort(ret)
                _write_cache(app_cache_file,ret)
        except:
            print ("Error: Unable to load new information for app: " + str(appname))
            traceback.print_exc()

    with open(app_cache_file, "r") as cache:
        # read the first line as cache time
        cache_time = cache.readline()
        line = cache.readline()
        return jsonify(**eval(line))
from flask import make_response, request, redirect, url_for
import awslib
import os

bucket_name = os.environ.get('IPLIST_CONFIG_BUCKET')
s3path = os.environ.get('IPLIST_CONFIG_PATH')
nohttps = os.environ.get('NOHTTPS')

path = join('iplist_config', 'config.json')

if s3path == None:
    print "No Env Labeled IPLIST_CONFIG_PATH"
elif bucket_name == None:
    print "No bucket name specified"
else:
    awslib._get_file(bucket_name, s3path, path)


@app.route('/')
def handle_index():
    redir = None
    if nohttps == None:
        proto = request.headers.get("X-Forwarded-Proto")
        if not proto == "https":
            redir = _check_ssl(request.url)

    if not redir == None:
        return redir

    with open(path) as json_data:
        data = json.load(json_data)