def main(cls, args=None, use_files=True): # type: (Sequence[str], bool) -> Any """Entry point to autopush's main command line scripts. aka autopush/autoendpoint. """ ns = cls.parse_args(cls.config_files if use_files else [], args) if not ns.no_aws: logging.HOSTNAME = utils.get_ec2_instance_id() PushLogger.setup_logging( cls.logger_name, log_level=ns.log_level or ("debug" if ns.debug else "info"), log_format="text" if ns.human_logs else "json", log_output=ns.log_output, sentry_dsn=bool(os.environ.get("SENTRY_DSN")), firehose_delivery_stream=ns.firehose_stream_name) try: app = cls.from_argparse(ns) except InvalidSettings as e: log.critical(str(e)) return 1 app.setup() app.run()
def test_get_ec2_instance_id_unknown(self, request_mock): import requests from autopush.utils import get_ec2_instance_id request_mock.side_effect = requests.HTTPError result = get_ec2_instance_id() assert result == "Unknown"
def test_get_ec2_instance_id(self, request_mock): from autopush.utils import get_ec2_instance_id mock_reply = Mock() mock_reply.content = "i-123242" request_mock.return_value = mock_reply result = get_ec2_instance_id() eq_(result, "i-123242")
def from_config(conf): # type: (AutopushConfig) -> IMetrics """Create an IMetrics from the given config""" if conf.datadog_api_key: return DatadogMetrics( hostname=get_ec2_instance_id() if conf.ami_id else conf.hostname, api_key=conf.datadog_api_key, app_key=conf.datadog_app_key, flush_interval=conf.datadog_flush_interval, ) elif conf.statsd_host: return TwistedMetrics(conf.statsd_host, conf.statsd_port) else: return SinkMetrics()
def from_settings(settings): # type: (AutopushSettings) -> IMetrics """Create an IMetrics from the given settings""" if settings.datadog_api_key: return DatadogMetrics( hostname=get_ec2_instance_id() if settings.ami_id else settings.hostname, api_key=settings.datadog_api_key, app_key=settings.datadog_app_key, flush_interval=settings.datadog_flush_interval, ) elif settings.statsd_host: return TwistedMetrics(settings.statsd_host, settings.statsd_port) else: return SinkMetrics()
def setup_logging(cls, logger_name, log_level="info", log_format="json", log_output="stdout", sentry_dsn=None, firehose_delivery_stream=None, no_aws=False): global instance_id_or_hostname if not instance_id_or_hostname: instance_id = None if no_aws else get_ec2_instance_id() instance_id_or_hostname = instance_id or socket.getfqdn() pl = cls(logger_name, log_level=log_level, log_format=log_format, log_output=log_output, sentry_dsn=sentry_dsn, firehose_delivery_stream=firehose_delivery_stream) pl.start() reactor.addSystemEventTrigger('before', 'shutdown', pl.stop) return pl