コード例 #1
0
    def test_config_with_defaults(self):
        """
        Ensures that application config is loaded by consul using the default
        namespace and host
        """

        with MockConsulKV():
            app = self.create_app()
            consul = Consul(app)
            consul.apply_remote_config()

        self.assertEqual(app.config['cfg_1'], 'consul_1')
        self.assertEqual(app.config['cfg_3'], 'consul_3')
        self.assertEqual(app.config['cfg_4'], {'inner': 'value'})
コード例 #2
0
    def test_config_with_defaults(self):
        """
        Ensures that application config is loaded by consul using the default
        namespace and host
        """

        with MockConsulKV():
            app = self.create_app()
            consul = Consul(app)
            consul.apply_remote_config()

        self.assertEqual(app.config['cfg_1'], 'consul_1')
        self.assertEqual(app.config['cfg_3'], 'consul_3')
        self.assertEqual(app.config['cfg_4'], {'inner': 'value'})
コード例 #3
0
def initialize_consul_client(app):
    server_port = app.config['SERVER_PORT']

    app_name = app.config['APP_NAME']

    profile = app.config['SPRING_PROFILES_ACTIVE']

    hostname = app.config['HOSTNAME']

    # Consul
    # This extension should be the first one if enabled:
    consul = Consul(app=app)
    # Fetch the configuration:
    consul.apply_remote_config(namespace=f'config/application,{profile}/data')
    # Register Consul service:
    consul.register_service(name=app_name,
                            interval='30s',
                            tags=[app_name],
                            port=server_port,
                            httpcheck=hostname + ":" + str(server_port) +
                            "/actuator/health")

    if profile != 'prod':
        jwt_secret = ""
        for data in yaml.load_all(app.config[''], Loader=yaml.BaseLoader):
            try:
                jwt_secret = data['com']['microservice']['authentication'][
                    'jwt']['keyValue']
                break
            except Exception:
                log.warning("Not found jwt_secret")

        if jwt_secret == "":
            raise Exception("jwt_secret not found")
        log.debug('Jwt Secret: %s', jwt_secret)
        app.config['JWT_SECRET_KEY'] = jwt_secret

    else:
        app.config['JWT_PUBLIC_KEY'] = open(app.config['JWT_PUBLIC_KEY'],
                                            "r").read()

    log.debug('Config environment: %s', app.config)

    # provide app's version and deploy environment/config name to set a gauge metric
    register_metrics(app, app_version="v0.1.2", app_config="staging")
コード例 #4
0
def register_consul(app):
    if 'CONSUL_SERVER_HOST' not in app.config:
        return app

    host_ip = app.config['SERVER_HOST'] or get_ip_address(
        app.config['HOST_ADAPTER'])
    consul = Consul(app=app,
                    consul_host=app.config['CONSUL_SERVER_HOST'],
                    consul_port=app.config['CONSUL_SERVER_PORT'])
    consul.apply_remote_config(namespace=app.config['CONSUL_NAMESPACE'])
    consul.register_service(name=app.config['CONSUL_SERVICE_NAME'],
                            interval=app.config['CONSUL_REGISTER_INTERVAL'],
                            tags=app.config['CONSUL_REGISTER_TAGS'],
                            port=app.config['SERVER_PORT'],
                            address=host_ip,
                            httpcheck='http://{host}:{port}/health'.format(
                                host=host_ip, port=app.config['SERVER_PORT']))

    register_health_check_api(app)
    return app
コード例 #5
0
    def test_config_with_environ(self):
        """
        Ensures that application config is loaded by consul using host and
        namespace defined by environmental variables
        """

        os.environ['CONSUL_HOST'] = "consul.adsabs"
        os.environ['SERVICE'] = "sample_application"
        os.environ['ENVIRONMENT'] = "testing"

        with MockConsulKV():
            app = self.create_app()
            consul = Consul(app)
            consul.apply_remote_config()

        del os.environ['CONSUL_HOST']
        del os.environ['SERVICE']
        del os.environ['ENVIRONMENT']

        self.assertEqual(app.config['cfg_1'], 'consul_1')
        self.assertEqual(app.config['cfg_3'], 'consul_3')
コード例 #6
0
    def test_config_with_environ(self):
        """
        Ensures that application config is loaded by consul using host and
        namespace defined by environmental variables
        """

        os.environ['CONSUL_HOST'] = "consul.adsabs"
        os.environ['SERVICE'] = "sample_application"
        os.environ['ENVIRONMENT'] = "testing"

        with MockConsulKV():
            app = self.create_app()
            consul = Consul(app)
            consul.apply_remote_config()

        del os.environ['CONSUL_HOST']
        del os.environ['SERVICE']
        del os.environ['ENVIRONMENT']

        self.assertEqual(app.config['cfg_1'], 'consul_1')
        self.assertEqual(app.config['cfg_3'], 'consul_3')
コード例 #7
0
app = Flask(__name__)


@app.route('/healthcheck')
def health_check():
    """
    This function is used to say current status to the Consul.
    Format: https://www.consul.io/docs/agent/checks.html

    :return: Empty response with status 200, 429 or 500
    """
    # TODO: implement any other checking logic.
    return '', 200


# Consul
# This extension should be the first one if enabled:
consul = Consul(app=app)
# Fetch the conviguration:
consul.apply_remote_config(namespace='mynamespace/')
# Register Consul service:
consul.register_service(name='my-web-app',
                        interval='10s',
                        tags=[
                            'webserver',
                        ],
                        port=5000,
                        httpcheck='http://localhost:5000/healthcheck')

if __name__ == '__main__':
    app.run()