Example #1
0
def index(req):

    delete_form = DeleteClusterForm(prefix='delete')

    items = req.zato.odb.query(Cluster).order_by(Cluster.name).all()
    for item in items:
        client = get_lb_client(item)

        try:
            lb_config = client.get_config()
            item.lb_config = lb_config

            # Assign the flags indicating whether servers are DOWN or in the MAINT mode.
            set_servers_state(item, client)

        except Exception:
            msg = 'Could not invoke agent, client:`{!r}`, e:`{}`'.format(
                client, format_exc())
            logger.error(msg)
            item.lb_config = None

    return_data = {
        'delete_form': delete_form,
        'edit_form': EditClusterForm(prefix='edit'),
        'items': items,
        'lb_use_tls': req.zato.lb_use_tls,
        'lb_tls_verify': req.zato.lb_tls_verify,
    }

    return TemplateResponse(req, 'zato/cluster/index.html', return_data)
Example #2
0
def index(req):

    initial = {}
    initial['odb_type'] = sqlalchemy_django_engine[DATABASE_ENGINE.replace(
        'django.db.backends.', '')]
    initial['odb_host'] = DATABASE_HOST
    initial['odb_port'] = DATABASE_PORT
    initial['odb_user'] = DATABASE_USER
    initial['odb_db_name'] = DATABASE_NAME

    delete_form = DeleteClusterForm(prefix='delete')

    items = req.zato.odb.query(Cluster).order_by('name').all()
    for item in items:
        client = get_lb_client(item)

        try:
            lb_config = client.get_config()
            item.lb_config = lb_config

            # Assign the flags indicating whether servers are DOWN or in the MAINT mode.
            set_servers_state(item, client)

        except Exception, e:
            msg = 'Could not invoke agent, client:[{client!r}], e:[{e}]'.format(
                client=client, e=format_exc(e))
            logger.error(msg)
            item.lb_config = None
Example #3
0
def remote_command(req, cluster_id):
    """ Execute a HAProxy command.
    """
    cluster = req.zato.odb.query(Cluster).filter_by(id=cluster_id).one()
    client = get_lb_client(cluster)

    haproxy_alive = _haproxy_alive(client)
    cluster.stats_uri, cluster.stats_port = _haproxy_stat_config(client=client)

    # We need to know the HAProxy version before we can build up the select box
    # on the form.
    commands = haproxy_stats[("1", "3")]

    version_info = tuple(client.haproxy_version_info())
    if version_info >= ("1", "4"):
        commands.update(haproxy_stats[("1", "4")])

    if req.method == "POST":
        result = client.execute_command(req.POST["command"], req.POST["timeout"], req.POST.get("extra", ""))
        if not result.strip():
            result = "(empty result)"

        initial={"result":result}
        for k, v in req.POST.items():
            if k != "result":
                initial[k] = v
        form = RemoteCommandForm(commands, initial)
    else:
        form = RemoteCommandForm(commands)

    return_data = {"form":form, "cluster":cluster, "haproxy_alive":haproxy_alive}

    return TemplateResponse(req, 'zato/load_balancer/remote_command.html', return_data)
Example #4
0
def servers_edit(req):
    """ Updates a server in both ODB and the load balancer.
    """
    try:
        client = get_lb_client(req.zato.cluster)

        server_id = req.POST['id']
        server = req.zato.odb.query(Server).filter_by(id=server_id).one()

        if client.get_server_data_dict(server.name):
            fetch_lb_data = True
            client.rename_server(req.POST['edit-old_name'],
                                 req.POST['edit-name'])
        else:
            fetch_lb_data = False

        response = req.zato.client.invoke('zato.cluster.server.edit', {
            'id': server_id,
            'name': req.POST['edit-name']
        })

        return _common_edit_message(
            client, 'Server [{}] updated', response.data.id,
            response.data.name, response.data.host, response.data.up_status,
            response.data.up_mod_date, req.zato.cluster_id,
            req.zato.user_profile, fetch_lb_data)

    except Exception, e:
        return HttpResponseServerError(format_exc(e))
Example #5
0
def index(req):

    initial = {}
    initial['odb_type'] = sqlalchemy_django_engine[DATABASE_ENGINE.replace('django.db.backends.', '')]
    initial['odb_host'] = DATABASE_HOST
    initial['odb_port'] = DATABASE_PORT
    initial['odb_user'] = DATABASE_USER
    initial['odb_db_name'] = DATABASE_NAME

    delete_form = DeleteClusterForm(prefix='delete')

    items = req.zato.odb.query(Cluster).order_by('name').all()
    for item in items:
        client = get_lb_client(item)

        try:
            lb_config = client.get_config()
            item.lb_config = lb_config

            # Assign the flags indicating whether servers are DOWN or in the MAINT mode.
            set_servers_state(item, client)

        except Exception, e:
            msg = 'Could not invoke agent, client:[{client!r}], e:[{e}]'.format(client=client,
                                                                e=format_exc(e))
            logger.error(msg)
            item.lb_config = None
Example #6
0
def servers_add_remove_lb(req, action, server_id):
    """ Adds or removes a server from the load balancer's configuration.
    """
    server = req.zato.odb.query(Server).filter_by(id=server_id).one()
    up_mod_date = server.up_mod_date.isoformat() if server.up_mod_date else ""

    client = get_lb_client(req.zato.cluster)
    client.add_remove_server(action, server.name)

    if action == "add":
        success_msg = "added to"
        fetch_lb_data = True
    else:
        success_msg = "removed from"
        fetch_lb_data = False

    return _common_edit_message(
        client,
        "Server [{{}}] {} the load balancer".format(success_msg),
        server.id,
        server.name,
        server.host,
        server.up_status,
        up_mod_date,
        server.cluster_id,
        req.zato.user_profile,
        fetch_lb_data,
    )
Example #7
0
def manage(req, cluster_id):
    """ GUI for managing HAProxy configuration.
    """
    cluster = req.odb.query(Cluster).filter_by(id=cluster_id).one()
    client = get_lb_client(cluster)

    lb_start_time = datetime.fromtimestamp(client.get_uptime_info())
    lb_config = client.get_config()
    lb_work_config = client.get_work_config()
    lb_work_config['verify_fields'] = ', '.join(['%s=%s' % (k,v) for (k, v) in sorted(lb_work_config['verify_fields'].items())])

    form_data = {
        'global_log_host': lb_config['global_']['log']['host'],
        'global_log_port': lb_config['global_']['log']['port'],
        'global_log_level': lb_config['global_']['log']['level'],
        'global_log_facility': lb_config['global_']['log']['facility'],

        'timeout_connect': lb_config['defaults']['timeout_connect'],
        'timeout_client': lb_config['defaults']['timeout_client'],
        'timeout_server': lb_config['defaults']['timeout_server'],

        'http_plain_bind_address':lb_config['frontend']['front_http_plain']['bind']['address'],
        'http_plain_bind_port':lb_config['frontend']['front_http_plain']['bind']['port'],
        'http_plain_log_http_requests':lb_config['frontend']['front_http_plain']['log_http_requests'],
        'http_plain_maxconn':lb_config['frontend']['front_http_plain']['maxconn'],
        'http_plain_monitor_uri':lb_config['frontend']['front_http_plain']['monitor_uri'],
        }

    backends = {}
    for backend_type in lb_config['backend']:
        for name in lb_config['backend'][backend_type]:
            # Is it a server?
            if 'address' in lb_config['backend'][backend_type][name]:
                if not name in backends:
                    backends[name] = {}
                backends[name][backend_type] = {}
                backends[name][backend_type]['address']  = lb_config['backend'][backend_type][name]['address']
                backends[name][backend_type]['port']  = lb_config['backend'][backend_type][name]['port']
                backends[name][backend_type]['extra']  = lb_config['backend'][backend_type][name]['extra']

    backends = OrderedDict(sorted(backends.items(), key=lambda t: t[0]))
    form = ManageLoadBalancerForm(initial=form_data)
    haproxy_alive = _haproxy_alive(client)
    cluster.stats_uri, cluster.stats_port = _haproxy_stat_config(lb_config=lb_config)
    servers_state = client.get_servers_state()

    return_data = {'cluster':cluster, 'lb_start_time':lb_start_time,
                   'lb_config':lb_config, 'lb_work_config':lb_work_config,
                   'form':form, 'backends':backends, 'haproxy_alive':haproxy_alive,
                   'servers_state':servers_state}

    if logger.isEnabledFor(TRACE1):
        logger.log(TRACE1, 'Returning render_to_response [%s]' % return_data)

    return render_to_response('zato/load_balancer/manage.html', return_data,
                              context_instance=RequestContext(req))
Example #8
0
    def __call__(self, req, *args, **kwargs):
        zato_message, _ = invoke_admin_service(req.zato.cluster, "zato:cluster.server.get-by-id", {"id": req.zato.id})

        server = req.zato.odb.query(Server).filter_by(id=req.zato.id).one()

        client = get_lb_client(req.zato.cluster)  # Checks whether the server is known by LB
        if client.get_server_data_dict(server.name):
            client.add_remove_server("remove", zato_message.response.item.name.text)

        return super(ServerDelete, self).__call__(req, *args, **kwargs)
Example #9
0
    def __call__(self, req, *args, **kwargs):
        response = req.zato.client.invoke('zato.server.get-by-id', {'id':req.zato.id})

        server = req.zato.odb.query(Server).filter_by(id=req.zato.id).one()

        client = get_lb_client(req.zato.cluster) # Checks whether the server is known by LB
        if client.get_server_data_dict(server.name):
            client.add_remove_server('remove', response.data.name)
            
        return super(ServerDelete, self).__call__(req, *args, **kwargs)
Example #10
0
    def __call__(self, req, *args, **kwargs):
        response = req.zato.client.invoke('zato.server.get-by-id', {'id':req.zato.id})

        server = req.zato.odb.query(Server).filter_by(id=req.zato.id).one()

        client = get_lb_client(req.zato.cluster) # Checks whether the server is known by LB
        if client.get_server_data_dict(server.name):
            client.add_remove_server('remove', response.data.name)
            
        return super(ServerDelete, self).__call__(req, *args, **kwargs)
Example #11
0
def validate_save_source_code(req, cluster_id):
    """ A common handler for both validating and saving a HAProxy config using
    the raw HAProxy config file's view.
    """
    cluster = req.zato.odb.query(Cluster).filter_by(id=cluster_id).one()
    save = _get_validate_save_flag(cluster_id, req.POST)

    # Invoke the LB agent
    client = get_lb_client(cluster)
    return _client_validate_save(req, client.validate_save_source_code, req.POST["source_code"], save)
Example #12
0
def validate_save(req, cluster_id):
    """ A common handler for both validating and saving a HAProxy config using
    a pretty GUI form.
    """
    save = _get_validate_save_flag(cluster_id, req.POST)

    cluster = req.zato.odb.query(Cluster).filter_by(id=cluster_id).one()
    client = get_lb_client(cluster)

    lb_config = Config()
    lb_config.global_["log"] = {}

    lb_config.frontend["front_http_plain"] = {}
    lb_config.frontend["front_http_plain"]["bind"] = {}

    lb_config.global_["log"]["host"] = req.POST["global_log_host"]
    lb_config.global_["log"]["port"] = req.POST["global_log_port"]
    lb_config.global_["log"]["level"] = req.POST["global_log_level"]
    lb_config.global_["log"]["facility"] = req.POST["global_log_facility"]

    lb_config.defaults["timeout_connect"] = req.POST["timeout_connect"]
    lb_config.defaults["timeout_client"] = req.POST["timeout_client"]
    lb_config.defaults["timeout_server"] = req.POST["timeout_server"]

    lb_config.frontend["front_http_plain"]["bind"]["address"] = req.POST[
        "http_plain_bind_address"]
    lb_config.frontend["front_http_plain"]["bind"]["port"] = req.POST[
        "http_plain_bind_port"]
    lb_config.frontend["front_http_plain"]["log_http_requests"] = req.POST[
        "http_plain_log_http_requests"]
    lb_config.frontend["front_http_plain"]["maxconn"] = req.POST[
        "http_plain_maxconn"]
    lb_config.frontend["front_http_plain"]["monitor_uri"] = req.POST[
        "http_plain_monitor_uri"]

    for key, value in req.POST.items():
        if key.startswith("bck_http"):
            for token in ("address", "port", "extra"):
                splitted = key.split(token)
                if splitted[0] == key:
                    continue  # We don't have the token in that key.

                backend_type, backend_name = splitted

                # Get rid of underscores left over from the .split above.
                backend_type = backend_type[:-1]
                backend_name = backend_name[1:]

                lb_config.backend.setdefault(backend_type, {})
                lb_config.backend[backend_type].setdefault(backend_name, {})
                lb_config.backend[backend_type][backend_name][token] = value

    # Invoke the LB agent
    return _client_validate_save(req, client.validate_save, lb_config, save)
Example #13
0
def get_servers_state(req, cluster_id):
    cluster = req.zato.odb.query(Cluster).filter_by(id=cluster_id).one()
    client = get_lb_client(cluster)

    # Assign the flags indicating whether servers are DOWN or in the MAINT mode.
    try:
        set_servers_state(cluster, client)
    except Exception, e:
        msg = "Failed to invoke the load-balancer's agent and set the state of servers, e:[{e}]".format(e=format_exc(e))
        logger.error(msg)
        return HttpResponseServerError(msg)
Example #14
0
def validate_save_source_code(req, cluster_id):
    """ A common handler for both validating and saving a HAProxy config using
    the raw HAProxy config file's view.
    """
    cluster = req.zato.odb.query(Cluster).filter_by(id=cluster_id).one()
    save = _get_validate_save_flag(cluster_id, req.POST)

    # Invoke the LB agent
    client = get_lb_client(cluster)
    return _client_validate_save(req, client.validate_save_source_code,
                                 req.POST["source_code"], save)
Example #15
0
def get_servers_state(req, cluster_id):
    cluster = req.zato.odb.query(Cluster).filter_by(id=cluster_id).one()
    client = get_lb_client(cluster)

    # Assign the flags indicating whether servers are DOWN or in the MAINT mode.
    try:
        set_servers_state(cluster, client)
    except Exception, e:
        msg = "Failed to invoke the load-balancer's agent and set the state of servers, e:[{e}]".format(e=format_exc(e))
        logger.error(msg)
        return HttpResponseServerError(msg)
Example #16
0
def validate_save(req, cluster_id):
    """ A common handler for both validating and saving a HAProxy config using
    a pretty GUI form.
    """
    save = _get_validate_save_flag(cluster_id, req.POST)

    cluster = req.zato.odb.query(Cluster).filter_by(id=cluster_id).one()
    client = get_lb_client(cluster)

    lb_config = Config()
    lb_config.global_['log'] = {}

    lb_config.frontend['front_http_plain'] = {}
    lb_config.frontend['front_http_plain']['bind'] = {}

    lb_config.global_['log']['host'] = req.POST['global_log_host']
    lb_config.global_['log']['port'] = req.POST['global_log_port']
    lb_config.global_['log']['level'] = req.POST['global_log_level']
    lb_config.global_['log']['facility'] = req.POST['global_log_facility']

    lb_config.defaults['timeout_connect'] = req.POST['timeout_connect']
    lb_config.defaults['timeout_client'] = req.POST['timeout_client']
    lb_config.defaults['timeout_server'] = req.POST['timeout_server']

    lb_config.frontend['front_http_plain']['bind']['address'] = req.POST[
        'http_plain_bind_address']
    lb_config.frontend['front_http_plain']['bind']['port'] = req.POST[
        'http_plain_bind_port']
    lb_config.frontend['front_http_plain']['log_http_requests'] = req.POST[
        'http_plain_log_http_requests']
    lb_config.frontend['front_http_plain']['maxconn'] = req.POST[
        'http_plain_maxconn']
    lb_config.frontend['front_http_plain']['monitor_uri'] = req.POST[
        'http_plain_monitor_uri']

    for key, value in req.POST.items():
        if key.startswith('bck_http'):
            for token in ('address', 'port', 'extra'):
                splitted = key.split(token)
                if splitted[0] == key:
                    continue  # We don't have the token in that key.

                backend_type, backend_name = splitted

                # Get rid of underscores left over from the .split above.
                backend_type = backend_type[:-1]
                backend_name = backend_name[1:]

                lb_config.backend.setdefault(backend_type, {})
                lb_config.backend[backend_type].setdefault(backend_name, {})
                lb_config.backend[backend_type][backend_name][token] = value

    # Invoke the LB agent
    return _client_validate_save(req, client.validate_save, lb_config, save)
Example #17
0
def servers(req):
    """ A view for server management.
    """
    items = req.zato.odb.query(Server).order_by("name").all()

    try:
        client = get_lb_client(req.zato.get("cluster"))
        server_data_dict = client.get_server_data_dict()
        bck_http_plain = client.get_config()["backend"]["bck_http_plain"]
        lb_client_invoked = True
    except Exception, e:
        lb_client_invoked = False
Example #18
0
def servers(req):
    """ A view for server management.
    """
    items = req.zato.odb.query(Server).order_by('name').all()
    
    try:
        client = get_lb_client(req.zato.get('cluster'))
        server_data_dict = client.get_server_data_dict()
        bck_http_plain = client.get_config()['backend']['bck_http_plain']
        lb_client_invoked = True
    except Exception, e:
        logger.error(format_exc(e))
        lb_client_invoked = False
Example #19
0
def servers(req):
    """ A view for server management.
    """
    items = req.zato.odb.query(Server).order_by('name').all()

    try:
        client = get_lb_client(req.zato.get('cluster'))
        server_data_dict = client.get_server_data_dict()
        bck_http_plain = client.get_config()['backend']['bck_http_plain']
        lb_client_invoked = True
    except Exception, e:
        logger.error(format_exc(e))
        lb_client_invoked = False
Example #20
0
def validate_save(req, cluster_id):
    """ A common handler for both validating and saving a HAProxy config using
    a pretty GUI form.
    """
    save = _get_validate_save_flag(cluster_id, req.POST)

    cluster = req.zato.odb.query(Cluster).filter_by(id=cluster_id).one()
    client = get_lb_client(cluster)

    lb_config = Config()
    lb_config.global_["log"] = {}

    lb_config.frontend["front_http_plain"] = {}
    lb_config.frontend["front_http_plain"]["bind"] = {}

    lb_config.global_["log"]["host"] = req.POST["global_log_host"]
    lb_config.global_["log"]["port"] = req.POST["global_log_port"]
    lb_config.global_["log"]["level"] = req.POST["global_log_level"]
    lb_config.global_["log"]["facility"] = req.POST["global_log_facility"]

    lb_config.defaults["timeout_connect"] = req.POST["timeout_connect"]
    lb_config.defaults["timeout_client"] = req.POST["timeout_client"]
    lb_config.defaults["timeout_server"] = req.POST["timeout_server"]

    lb_config.frontend["front_http_plain"]["bind"]["address"] = req.POST["http_plain_bind_address"]
    lb_config.frontend["front_http_plain"]["bind"]["port"] = req.POST["http_plain_bind_port"]
    lb_config.frontend["front_http_plain"]["log_http_requests"] = req.POST["http_plain_log_http_requests"]
    lb_config.frontend["front_http_plain"]["maxconn"] = req.POST["http_plain_maxconn"]
    lb_config.frontend["front_http_plain"]["monitor_uri"] = req.POST["http_plain_monitor_uri"]

    for key, value in req.POST.items():
        if key.startswith("bck_http"):
            for token in("address", "port", "extra"):
                splitted = key.split(token)
                if splitted[0] == key:
                    continue # We don't have the token in that key.

                backend_type, backend_name = splitted

                # Get rid of underscores left over from the .split above.
                backend_type = backend_type[:-1]
                backend_name = backend_name[1:]

                lb_config.backend.setdefault(backend_type, {})
                lb_config.backend[backend_type].setdefault(backend_name, {})
                lb_config.backend[backend_type][backend_name][token] = value

    # Invoke the LB agent
    _client_validate_save(req, client.validate_save, lb_config, save)

    return redirect("lb-manage", cluster_id=cluster_id)
Example #21
0
def manage_source_code(req, cluster_id):
    """ Source code view for managing HAProxy configuration.
    """
    cluster = req.zato.odb.query(Cluster).filter_by(id=cluster_id).one()
    client = get_lb_client(cluster)
    cluster.stats_uri, cluster.stats_port = _haproxy_stat_config(client=client)

    haproxy_alive = _haproxy_alive(client)
    source_code = client.get_config_source_code()
    form = ManageLoadBalancerSourceCodeForm(initial={"source_code":source_code})

    return_data = {"form": form, "haproxy_alive":haproxy_alive, "cluster":cluster}

    return TemplateResponse(req, 'zato/load_balancer/manage_source_code.html', return_data)
Example #22
0
def servers(req):
    """ A view for server management.
    """
    items = req.zato.odb.query(Server).order_by(Server.name).all()

    for item in items:
        if item.up_mod_date:
            item.up_mod_date_user = from_utc_to_user(
                item.up_mod_date.replace(tzinfo=UTC).isoformat(),
                req.zato.user_profile)

    try:
        client = get_lb_client(req.zato.get('cluster'))
        server_data_dict = client.get_server_data_dict()
        bck_http_plain = client.get_config()['backend']['bck_http_plain']
        lb_client_invoked = True
    except Exception:
        logger.error(format_exc())
        lb_client_invoked = False

    if lb_client_invoked:

        def _update_item(server_name, lb_address, lb_state):
            for item in items:
                if item.name == server_name:
                    item.in_lb = True
                    item.lb_address = lb_address
                    item.lb_state = lb_state

                    if item.up_status == SERVER_UP_STATUS.RUNNING:
                        item.may_be_deleted = False
                    else:
                        item.may_be_deleted = True

        for server_name in bck_http_plain:
            lb_address = '{}:{}'.format(bck_http_plain[server_name]['address'],
                                        bck_http_plain[server_name]['port'])
            _update_item(server_name, lb_address,
                         server_data_dict[server_name]['state'])

    return_data = {
        'items': items,
        'search_form': req.zato.search_form,
        'zato_clusters': req.zato.clusters,
        'cluster': req.zato.get('cluster'),
        'edit_form': EditServerForm(prefix='edit')
    }

    return TemplateResponse(req, 'zato/cluster/servers.html', return_data)
Example #23
0
def get_servers_state(req, cluster_id):
    cluster = req.zato.odb.query(Cluster).filter_by(id=cluster_id).one()
    client = get_lb_client(cluster)

    # Assign the flags indicating whether servers are DOWN or in the MAINT mode.
    try:
        set_servers_state(cluster, client)
    except Exception:
        msg = 'Failed to invoke the load-balancer\'s agent and set the state of servers, e:`{}`'.format(
            format_exc())
        logger.error(msg)
        return HttpResponseServerError(msg)

    return TemplateResponse(req, 'zato/cluster/servers_state.html',
                            {'cluster': cluster})
Example #24
0
def get_addresses(req, cluster_id):
    """ Return JSON-formatted addresses known to HAProxy.
    """
    cluster = req.zato.odb.query(Cluster).filter_by(id=cluster_id).one()
    client = get_lb_client(cluster)

    addresses = {}
    addresses["cluster"] = {"lb_host": cluster.lb_host, "lb_agent_port":cluster.lb_agent_port}

    try:
        lb_config = client.get_config()
    except Exception, e:
        msg = "Could not get load balancer's config, client:[{client!r}], e:[{e}]".format(client=client,
                                                            e=format_exc(e))
        logger.error(msg)
        lb_config = None
Example #25
0
def _create_edit(req, verb, item, form_class, prefix=''):

    join = '-' if prefix else ''

    try:
        for s in whitespace:
            if s in req.POST[prefix + join + 'name']:
                return HttpResponseServerError('Cluster name must not contain whitespace.')

        description = req.POST[prefix + join + 'description'].strip()
        description = description if description else None

        item.name = req.POST[prefix + join + 'name'].strip()
        item.description = description
        item.odb_type = req.POST[prefix + join + 'odb_type'].strip()
        item.odb_host = req.POST[prefix + join + 'odb_host'].strip()
        item.odb_port = req.POST[prefix + join + 'odb_port'].strip()
        item.odb_user = req.POST[prefix + join + 'odb_user'].strip()
        item.odb_db_name = req.POST[prefix + join + 'odb_db_name'].strip()
        item.odb_schema = req.POST[prefix + join + 'odb_schema'].strip()
        item.lb_host = req.POST[prefix + join + 'lb_host'].strip()
        item.lb_port = req.POST[prefix + join + 'lb_port'].strip()
        item.lb_agent_port = req.POST[prefix + join + 'lb_agent_port'].strip()
        item.broker_host = req.POST[prefix + join + 'broker_host'].strip()
        item.broker_start_port = req.POST[prefix + join + 'broker_start_port'].strip()
        item.broker_token = req.POST[prefix + join + 'broker_token'].strip()

        try:
            req.odb.add(item)
            req.odb.commit()
            
            try:
                item.lb_config = get_lb_client(item).get_config()
            except Exception, e:
                item.lb_config = None
                msg = "Exception caught while fetching the load balancer's config, e=[{0}]".format(format_exc(e))
                logger.error(msg)                    
            
            return _edit_create_response(item, verb)
        
        except Exception, e:
            msg = 'Exception caught, e=[{0}]'.format(format_exc(e))
            logger.error(msg)

            return HttpResponseServerError(msg)
Example #26
0
def _create_edit(req, verb, item, form_class, prefix=""):

    join = "-" if prefix else ""

    try:
        for s in whitespace:
            if s in req.POST[prefix + join + "name"]:
                return HttpResponseServerError("Cluster name must not contain whitespace.")

        description = req.POST[prefix + join + "description"].strip()
        description = description if description else None

        item.name = req.POST[prefix + join + "name"].strip()
        item.description = description
        item.odb_type = req.POST[prefix + join + "odb_type"].strip()
        item.odb_host = req.POST[prefix + join + "odb_host"].strip()
        item.odb_port = req.POST[prefix + join + "odb_port"].strip()
        item.odb_user = req.POST[prefix + join + "odb_user"].strip()
        item.odb_db_name = req.POST[prefix + join + "odb_db_name"].strip()
        item.odb_schema = req.POST[prefix + join + "odb_schema"].strip()
        item.lb_host = req.POST[prefix + join + "lb_host"].strip()
        item.lb_port = req.POST[prefix + join + "lb_port"].strip()
        item.lb_agent_port = req.POST[prefix + join + "lb_agent_port"].strip()
        item.broker_host = req.POST[prefix + join + "broker_host"].strip()
        item.broker_start_port = req.POST[prefix + join + "broker_start_port"].strip()
        item.broker_token = req.POST[prefix + join + "broker_token"].strip()

        try:
            req.zato.odb.add(item)
            req.zato.odb.commit()

            try:
                item.lb_config = get_lb_client(item).get_config()
            except Exception, e:
                item.lb_config = None
                msg = "Exception caught while fetching the load balancer's config, e:[{0}]".format(format_exc(e))
                logger.error(msg)

            return _edit_create_response(item, verb)

        except Exception, e:
            msg = "Exception caught, e:[{0}]".format(format_exc(e))
            logger.error(msg)

            return HttpResponseServerError(msg)
Example #27
0
def manage_source_code(req, cluster_id):
    """ Source code view for managing HAProxy configuration.
    """
    cluster = req.odb.query(Cluster).filter_by(id=cluster_id).one()
    client = get_lb_client(cluster)
    cluster.stats_uri, cluster.stats_port = _haproxy_stat_config(client=client)

    haproxy_alive = _haproxy_alive(client)
    source_code = client.get_config_source_code()
    form = ManageLoadBalancerSourceCodeForm(initial={"source_code":source_code})

    return_data = {"form": form, "haproxy_alive":haproxy_alive, "cluster":cluster}

    if logger.isEnabledFor(TRACE1):
        logger.log(TRACE1, "Returning render_to_response [%s]" % return_data)

    return render_to_response("zato/load_balancer/manage_source_code.html", return_data,
                              context_instance=RequestContext(req))
Example #28
0
def get_addresses(req, cluster_id):
    """ Return JSON-formatted addresses known to HAProxy.
    """
    cluster = req.zato.odb.query(Cluster).filter_by(id=cluster_id).one()
    client = get_lb_client(cluster)

    addresses = {}
    addresses["cluster"] = {
        "lb_host": cluster.lb_host,
        "lb_agent_port": cluster.lb_agent_port
    }

    try:
        lb_config = client.get_config()
    except Exception, e:
        msg = "Could not get load balancer's config, client:[{client!r}], e:[{e}]".format(
            client=client, e=format_exc(e))
        logger.error(msg)
        lb_config = None
Example #29
0
def _create_edit(req, verb, item, form_class, prefix=''):

    join = '-' if prefix else ''

    try:
        for s in whitespace:
            if s in req.POST[prefix + join + 'name']:
                return HttpResponseServerError(
                    'Cluster name must not contain whitespace.')

        description = req.POST[prefix + join + 'description'].strip()
        description = description if description else None

        item.name = req.POST[prefix + join + 'name'].strip()
        item.description = description

        item.lb_host = req.POST[prefix + join + 'lb_host'].strip()
        item.lb_port = req.POST[prefix + join + 'lb_port'].strip()
        item.lb_agent_port = req.POST[prefix + join + 'lb_agent_port'].strip()

        try:
            req.zato.odb.add(item)
            req.zato.odb.commit()

            try:
                item.lb_config = get_lb_client(item).get_config()
            except Exception:
                item.lb_config = None
                msg = "Exception caught while fetching the load balancer's config, e:`{}`".format(
                    format_exc())
                logger.error(msg)

            return _edit_create_response(item, verb)

        except Exception:
            msg = 'Exception caught, e:`{}`'.format(format_exc())
            logger.error(msg)

            return HttpResponseServerError(msg)

    except Exception:
        req.zato.odb.rollback()
        return HttpResponseServerError(str(format_exc()))
Example #30
0
def servers(req):
    """ A view for server management.
    """
    items = req.zato.odb.query(Server).order_by(Server.name).all()

    for item in items:
        if item.up_mod_date:
            item.up_mod_date_user = from_utc_to_user(
                item.up_mod_date.replace(tzinfo=UTC).isoformat(),
                req.zato.user_profile)

    try:
        client = get_lb_client(req.zato.get('cluster'))
        server_data_dict = client.get_server_data_dict()
        bck_http_plain = client.get_config()['backend']['bck_http_plain']
        lb_client_invoked = True
    except Exception, e:
        logger.error(format_exc(e))
        lb_client_invoked = False
Example #31
0
def servers_add_remove_lb(req, action, server_id):
    """ Adds or removes a server from the load balancer's configuration.
    """
    server = req.zato.odb.query(Server).filter_by(id=server_id).one()
    up_mod_date = server.up_mod_date.isoformat() if server.up_mod_date else ''

    client = get_lb_client(req.zato.cluster)
    client.add_remove_server(action, server.name)

    if action == 'add':
        success_msg = 'added to'
        fetch_lb_data = True
    else:
        success_msg = 'removed from'
        fetch_lb_data = False

    return _common_edit_message(
        client, 'Server [{{}}] {} the load balancer'.format(success_msg),
        server.id, server.name, server.host, server.up_status, up_mod_date,
        server.cluster_id, req.zato.user_profile, fetch_lb_data)
Example #32
0
def manage_source_code(req, cluster_id):
    """ Source code view for managing HAProxy configuration.
    """
    cluster = req.zato.odb.query(Cluster).filter_by(id=cluster_id).one()
    client = get_lb_client(cluster)
    cluster.stats_uri, cluster.stats_port = _haproxy_stat_config(client=client)

    haproxy_alive = _haproxy_alive(client)
    source_code = client.get_config_source_code()
    form = ManageLoadBalancerSourceCodeForm(
        initial={"source_code": source_code})

    return_data = {
        "form": form,
        "haproxy_alive": haproxy_alive,
        "cluster": cluster
    }

    return TemplateResponse(req, 'zato/load_balancer/manage_source_code.html',
                            return_data)
Example #33
0
def index(req):

    delete_form = DeleteClusterForm(prefix='delete')

    items = req.zato.odb.query(Cluster).order_by('name').all()
    for item in items:
        client = get_lb_client(item)

        try:
            lb_config = client.get_config()
            item.lb_config = lb_config

            # Assign the flags indicating whether servers are DOWN or in the MAINT mode.
            set_servers_state(item, client)

        except Exception, e:
            msg = 'Could not invoke agent, client:[{client!r}], e:[{e}]'.format(client=client,
                                                                e=format_exc(e))
            logger.error(msg)
            item.lb_config = None
Example #34
0
def remote_command(req, cluster_id):
    """ Execute a HAProxy command.
    """
    cluster = req.zato.odb.query(Cluster).filter_by(id=cluster_id).one()
    client = get_lb_client(cluster)

    haproxy_alive = _haproxy_alive(client, req.zato.lb_use_tls)
    cluster.stats_uri, cluster.stats_port = _haproxy_stat_config(client=client)

    # We need to know the HAProxy version before we can build up the select box
    # on the form.
    commands = haproxy_stats[('1', '3')]

    version_info = tuple(client.haproxy_version_info())
    if version_info >= ('1', '4'):
        commands.update(haproxy_stats[('1', '4')])

    if req.method == 'POST':
        result = client.execute_command(req.POST['command'],
                                        req.POST['timeout'],
                                        req.POST.get('extra', ''))
        if not result.strip():
            result = '(empty result)'

        initial = {'result': result}
        for k, v in req.POST.items():
            if k != 'result':
                initial[k] = v
        form = RemoteCommandForm(commands, initial)
    else:
        form = RemoteCommandForm(commands)

    return_data = {
        'form': form,
        'cluster': cluster,
        'haproxy_alive': haproxy_alive,
        'lb_use_tls': req.zato.lb_use_tls
    }

    return TemplateResponse(req, 'zato/load_balancer/remote_command.html',
                            return_data)
Example #35
0
def remote_command(req, cluster_id):
    """ Execute a HAProxy command.
    """
    cluster = req.zato.odb.query(Cluster).filter_by(id=cluster_id).one()
    client = get_lb_client(cluster)

    haproxy_alive = _haproxy_alive(client, req.zato.lb_use_tls)
    cluster.stats_uri, cluster.stats_port = _haproxy_stat_config(client=client)

    # We need to know the HAProxy version before we can build up the select box
    # on the form.
    commands = haproxy_stats[("1", "3")]

    version_info = tuple(client.haproxy_version_info())
    if version_info >= ("1", "4"):
        commands.update(haproxy_stats[("1", "4")])

    if req.method == "POST":
        result = client.execute_command(req.POST["command"],
                                        req.POST["timeout"],
                                        req.POST.get("extra", ""))
        if not result.strip():
            result = "(empty result)"

        initial = {"result": result}
        for k, v in req.POST.items():
            if k != "result":
                initial[k] = v
        form = RemoteCommandForm(commands, initial)
    else:
        form = RemoteCommandForm(commands)

    return_data = {
        "form": form,
        "cluster": cluster,
        "haproxy_alive": haproxy_alive,
        "lb_use_tls": req.zato.lb_use_tls
    }

    return TemplateResponse(req, 'zato/load_balancer/remote_command.html',
                            return_data)
Example #36
0
def get_addresses(req, cluster_id):
    """ Return JSON-formatted addresses known to HAProxy.
    """
    cluster = req.zato.odb.query(Cluster).filter_by(id=cluster_id).one()
    client = get_lb_client(cluster)

    addresses = {}
    addresses['cluster'] = {
        'lb_host': cluster.lb_host,
        'lb_agent_port': cluster.lb_agent_port
    }

    try:
        lb_config = client.get_config()
    except Exception:
        msg = 'Could not get load balancer\'s config, client:`{!r}`, e:`{}'.format(
            client, format_exc())
        logger.error(msg)
        lb_config = None

    addresses['cluster']['lb_config'] = lb_config

    return HttpResponse(json.dumps(addresses))
Example #37
0
def servers_edit(req):
    """ Updates a server in both ODB and the load balancer.
    """
    try:
        client = get_lb_client(req.zato.cluster)
        
        server_id = req.POST['id']
        server = req.zato.odb.query(Server).filter_by(id=server_id).one()
        
        if client.get_server_data_dict(server.name):
            fetch_lb_data = True
            client.rename_server(req.POST['edit-old_name'], req.POST['edit-name'])
        else:
            fetch_lb_data = False

        response = req.zato.client.invoke('zato.cluster.server.edit', {'id':server_id, 'name':req.POST['edit-name']})
        
        return _common_edit_message(client, 'Server [{}] updated', 
            response.data.id, response.data.name, response.data.host,
            response.data.up_status, response.data.up_mod_date,
            req.zato.cluster_id, req.zato.user_profile, fetch_lb_data)
    
    except Exception, e:
        return HttpResponseServerError(format_exc(e))
Example #38
0
def servers_edit(req):
    """ Updates a server in both ODB and the load balancer.
    """
    try:
        client = get_lb_client(req.zato.cluster)

        server_id = req.POST["id"]
        server = req.zato.odb.query(Server).filter_by(id=server_id).one()

        if client.get_server_data_dict(server.name):
            fetch_lb_data = True
            client.rename_server(req.POST["edit-old_name"], req.POST["edit-name"])
        else:
            fetch_lb_data = False

        zato_message, _ = invoke_admin_service(
            req.zato.cluster, "zato:cluster.server.edit", {"id": server_id, "name": req.POST["edit-name"]}
        )

        msg_item = zato_message.response.item

        return _common_edit_message(
            client,
            "Server [{}] updated",
            msg_item.id.text,
            msg_item.name.text,
            msg_item.host.text,
            msg_item.up_status.text,
            msg_item.up_mod_date.text,
            req.zato.cluster_id,
            req.zato.user_profile,
            fetch_lb_data,
        )

    except Exception, e:
        return HttpResponseServerError(format_exc(e))
Example #39
0
def manage(req, cluster_id):
    """ GUI for managing HAProxy configuration.
    """
    cluster = req.zato.odb.query(Cluster).filter_by(id=cluster_id).one()
    client = get_lb_client(cluster)

    lb_start_time = from_utc_to_user(client.get_uptime_info(),
                                     req.zato.user_profile)
    lb_config = client.get_config()
    lb_work_config = client.get_work_config()
    lb_work_config['verify_fields'] = ', '.join([
        '%s=%s' % (k, v)
        for (k, v) in sorted(lb_work_config['verify_fields'].items())
    ])

    form_data = {
        'global_log_host':
        lb_config['global_']['log']['host'],
        'global_log_port':
        lb_config['global_']['log']['port'],
        'global_log_level':
        lb_config['global_']['log']['level'],
        'global_log_facility':
        lb_config['global_']['log']['facility'],
        'timeout_connect':
        lb_config['defaults']['timeout_connect'],
        'timeout_client':
        lb_config['defaults']['timeout_client'],
        'timeout_server':
        lb_config['defaults']['timeout_server'],
        'http_plain_bind_address':
        lb_config['frontend']['front_http_plain']['bind']['address'],
        'http_plain_bind_port':
        lb_config['frontend']['front_http_plain']['bind']['port'],
        'http_plain_log_http_requests':
        lb_config['frontend']['front_http_plain']['log_http_requests'],
        'http_plain_maxconn':
        lb_config['frontend']['front_http_plain']['maxconn'],
        'http_plain_monitor_uri':
        lb_config['frontend']['front_http_plain']['monitor_uri'],
    }

    backends = {}
    for backend_type in lb_config['backend']:
        for name in lb_config['backend'][backend_type]:
            # Is it a server?
            if 'address' in lb_config['backend'][backend_type][name]:
                if not name in backends:
                    backends[name] = {}
                backends[name][backend_type] = {}
                backends[name][backend_type]['address'] = lb_config['backend'][
                    backend_type][name]['address']
                backends[name][backend_type]['port'] = lb_config['backend'][
                    backend_type][name]['port']
                backends[name][backend_type]['extra'] = lb_config['backend'][
                    backend_type][name]['extra']

    backends = OrderedDict(sorted(backends.items(), key=lambda t: t[0]))
    form = ManageLoadBalancerForm(initial=form_data)
    haproxy_alive = _haproxy_alive(client, req.zato.lb_use_tls)
    cluster.stats_uri, cluster.stats_port = _haproxy_stat_config(
        lb_config=lb_config)
    servers_state = client.get_servers_state()

    return_data = {
        'cluster': cluster,
        'lb_start_time': lb_start_time,
        'lb_config': lb_config,
        'lb_work_config': lb_work_config,
        'form': form,
        'backends': backends,
        'haproxy_alive': haproxy_alive,
        'servers_state': servers_state,
        'lb_use_tls': req.zato.lb_use_tls
    }

    return TemplateResponse(req, 'zato/load_balancer/manage.html', return_data)