Example #1
0
def delete_listener(listener_id):
    _check_listener_exists(listener_id)

    # check if that haproxy is still running and if stop it
    if os.path.exists(util.pid_path(listener_id)) and os.path.exists(
            os.path.join('/proc', util.get_haproxy_pid(listener_id))):
        cmd = "/usr/sbin/service haproxy-{0} stop".format(listener_id)
        try:
            subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as e:
            LOG.debug("Failed to stop HAProxy service: %s", e)
            return flask.make_response(flask.jsonify(dict(
                message="Error stopping haproxy",
                details=e.output)), 500)

    # parse config and delete stats socket
    try:
        cfg = _parse_haproxy_file(listener_id)
        os.remove(cfg['stats_socket'])
    except Exception:
        pass

    # delete the ssl files
    try:
        shutil.rmtree(_cert_dir(listener_id))
    except Exception:
        pass

    # delete the directory + upstart script for that listener
    shutil.rmtree(util.haproxy_dir(listener_id))
    if os.path.exists(util.upstart_path(listener_id)):
        os.remove(util.upstart_path(listener_id))

    return flask.jsonify({'message': 'OK'})
Example #2
0
def upload_haproxy_config(listener_id):
    stream = Wrapped(flask.request.stream)

    if not os.path.exists(util.haproxy_dir(listener_id)):
        os.makedirs(util.haproxy_dir(listener_id))

    name = os.path.join(util.haproxy_dir(listener_id), 'haproxy.cfg.new')
    with open(name, 'w') as file:
        b = stream.read(BUFFER)
        while (b):
            file.write(b)
            b = stream.read(BUFFER)

    # use haproxy to check the config
    cmd = "haproxy -c -f {config_file}".format(config_file=name)

    try:
        subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT)
    except subprocess.CalledProcessError as e:
        LOG.debug("Failed to verify haproxy file: %s", e)
        os.remove(name)  # delete file
        return flask.make_response(flask.jsonify(dict(
            message="Invalid request",
            details=e.output)), 400)

    # file ok - move it
    os.rename(name, util.config_path(listener_id))

    if not os.path.exists(util.upstart_path(listener_id)):
        with open(util.upstart_path(listener_id), 'w') as text_file:
            text = template.render(
                haproxy_pid=util.pid_path(listener_id),
                haproxy_cmd=util.CONF.haproxy_amphora.haproxy_cmd,
                haproxy_cfg=util.config_path(listener_id),
                respawn_count=util.CONF.haproxy_amphora.respawn_count,
                respawn_interval=util.CONF.haproxy_amphora.respawn_interval
            )
            text_file.write(text)

    res = flask.make_response(flask.jsonify({
        'message': 'OK'}), 202)
    res.headers['ETag'] = stream.get_md5()
    return res