Ejemplo n.º 1
0
def host_add_post():
    group_id = request.form['group_id']
    if not group_id:
        return jsonify(msg='no group_id given')

    group_id = int(group_id)
    group = HostGroup.read('id = %s', [group_id])
    if not group:
        return jsonify(msg='no such group')

    hosts = request.form['hosts'].strip()
    if not hosts:
        return jsonify(msg='hosts is blank')

    host_arr = hosts.splitlines()
    safe_host_arr = [h for h in host_arr if h]
    if not safe_host_arr:
        return jsonify(msg='hosts is blank')

    success = []
    failure = []

    for h in safe_host_arr:
        h = h.strip()
        msg = GroupHost.bind(group_id, h)
        if not msg:
            success.append('%s<br>' % h)
        else:
            failure.append('%s %s<br>' % (h, msg))

    data = '<div class="alert alert-danger" role="alert">failure:<hr>' + ''.join(
        failure) + '</div><div class="alert alert-success" role="alert">success:<hr>' + ''.join(success) + '</div>'

    return jsonify(msg='', data=data)
Ejemplo n.º 2
0
def host_add_post():
    group_id = request.form['group_id']
    if not group_id:
        return jsonify(msg='no group_id given')

    group_id = int(group_id)
    group = HostGroup.read('id = %s', [group_id])
    if not group:
        return jsonify(msg='no such group')

    hosts = request.form['hosts'].strip()
    if not hosts:
        return jsonify(msg='hosts is blank')

    host_arr = hosts.splitlines()
    safe_host_arr = [h for h in host_arr if h]
    if not safe_host_arr:
        return jsonify(msg='hosts is blank')

    success = []
    failure = []

    for h in safe_host_arr:
        msg = GroupHost.bind(group_id, h)
        if not msg:
            success.append('%s<br>' % h)
        else:
            failure.append('%s %s<br>' % (h, msg))

    data = '<div class="alert alert-danger" role="alert">failure:<hr>' + ''.join(
        failure
    ) + '</div><div class="alert alert-success" role="alert">success:<hr>' + ''.join(
        success) + '</div>'

    return jsonify(msg='', data=data)
Ejemplo n.º 3
0
def api_marathon_health():
    marathon_stats = request.get_json()

    if marathon_stats["eventType"] == "deployment_success":
        action_type = marathon_stats["plan"]["steps"][-1]["actions"][-1]["type"]
        app = marathon_stats["plan"]["steps"][-1]["actions"][-1]["app"].lstrip("/")
        if action_type == "StopApplication":
            grp_id = HostGroup.query_grp_id(app)
            if grp_id > 0:
                group_service.delete_group(grp_id)
        else:
            grp_id = HostGroup.create(app,"root",1)
            if grp_id < 0:
                grp_id = HostGroup.query_grp_id(app)
                if grp_id < 0:
                    return 'grp_name no exist'
            import requests,json
            r = requests.get(MARATHON_ADDRESS+"/v2/apps/"+app)
            app_info = json.loads(r.text)
            marathon_hosts = []
            for i in app_info["app"]["tasks"]:
                marathon_hosts.append(i["host"])
            vs,_ = Host.query(1, 10000000, '', '0', grp_id)
            of_names = [v.hostname for v in vs]

            of_names_set = set(of_names)
            marathon_hosts_set = set(marathon_hosts)
            delete_hosts = list(of_names_set - marathon_hosts_set)
            add_hosts = list(marathon_hosts_set - of_names_set)

            for h in add_hosts:
                msg = GroupHost.bind(grp_id,h)
            of_ids = [int(v.id) for v in vs if v.hostname in delete_hosts]
            if len(delete_hosts)>0:
                ids = ",".join('%s' % id for id in of_ids)
                msg = GroupHost.unbind(int(grp_id[0]),ids)
    return 'ok'