Exemple #1
0
    def script(cls, service, params):
        """
        Consul Script Check

        This feature is tricky because it lets you specify a script check to run from Consul.
        If running Consul in a container, you're limited to what you can run from that container.
        For example, curl must be installed for this to work:

        SERVICE_CHECK_SCRIPT=curl --silent --fail example.com

        The default interval for any non-TTL check is 10s, but you can set it with _CHECK_INTERVAL.
        The check command will be interpolated with the $SERVICE_IP and $SERVICE_PORT placeholders:

        SERVICE_CHECK_SCRIPT=nc $SERVICE_IP $SERVICE_PORT | grep OK
        """
        # https://github.com/cablehead/python-consul/blob/53eb41c4760b983aec878ef73e72c11e0af501bb/consul/base.py#L53
        # https://github.com/gliderlabs/registrator/blob/4322fe00304d6de661865721b073dc5c7e750bd2/consul/consul.go#L115
        # https://github.com/gliderlabs/registrator/blob/master/docs/user/backends.md#consul-script-check
        # https://www.consul.io/docs/agent/options#_enable_script_checks
        # https://www.hashicorp.com/blog/protecting-consul-from-rce-risk-in-specific-configurations
        args = cls._value(params, 'script')
        if args:
            # Run the script *args* every *interval* (e.g. "10s") to perfom health check
            args = args.replace('$SERVICE_IP',
                                service.ip).replace('$SERVICE_PORT',
                                                    str(service.port))
            interval = cls._value(params, 'interval')
            if cls.consul_version >= (1, 1, 0):
                args = args.split(' ')
                ret = Check.script(args, interval)
                return cls._post_process(ret, params)
            else:
                # compat
                # https://github.com/cablehead/python-consul/commit/f405dee1beb6019986307c121702d2e9ad40bcda
                # https://github.com/cablehead/python-consul/commit/e3493a0e6089d01ae37347f452cf7510813e2eb4
                ret = Check.script(args, interval)
                ret['script'] = args
                del ret['args']
                return cls._post_process(ret, params)
        return None