def deploy(network_name, consul_name, service_name, domain, git_url, expected_content, profile_name, count): """Deploy the service in the given network in the given profile with health checks.""" client = load_client(profile_name) network = client.network.get(network_name) if not network: click.echo("Network: \"%s\" not found." % network_name) sys.exit(1) service = client.service.get(network, service_name) if service: print("Service: %s already exists!" % service_name) else: print("Service: %s not found! Creating." % service_name) consul_service = client.service.get(network, consul_name) consul_instances = client.service.get_instances(consul_service) blueprint_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), "..", "blueprint.yml") blueprint_vars = { "jekyll_site_domain": domain, "jekyll_site_github_url": git_url, "consul_ips": [instance.private_ip for instance in consul_instances], "use_sslmate": True, "use_datadog": True } internet = CidrBlock("0.0.0.0/0") service = client.service.create(network, service_name, blueprint=blueprint_path, count=count, template_vars=blueprint_vars) client.paths.add(service, consul_service, 8500) client.paths.add(internet, service, 80) client.paths.add(internet, service, 443) print("Created service: %s!" % service_name) check_health(service, expected_content, use_datadog=True, use_sslmate=True) print("") print("Deploy Successful!") print("") print("Public IPs: %s" % [i.public_ip for i in client.service.get_instances(service)]) print("Use 'cldls service get %s %s' for more info." % (network_name, service_name))
def hc_produce_forever( producer: Producer, delay_seconds: float = PRODUCER_DELAY_SECS, url: str = "https://www.example.com/", topic=KAFKA_TOPIC, ): """Loop forever, checking health and sending to kafka.""" while True: health = check_health(url) # Trigger any available delivery report callbacks from previous produce() calls producer.poll(0) send_to_kafka(health, producer, topic=topic) sleep(delay_seconds)
def test_flow(self): p = producer.make_producer() health = check_health("https://example.com/") producer.send_to_kafka(health, p, topic=settings.TEST_KAFKA_TOPIC) p.flush() start = monotonic() # find our message for ten seconds. returned_health = None c = consumer.make_consumer(topic=settings.TEST_KAFKA_TOPIC) try: while (monotonic() - start) < 30: returned_health = consumer.hc_consume(c) if returned_health is not None: break finally: c.close() self.assertEqual(health, returned_health) db.ensure_table(connection_uri=settings.TEST_POSTGRES_URI) consumer.store_health(health, settings.TEST_POSTGRES_URI) # This is basically a sanity check. The calculation is done inside postgres, so if we record the right thing # we should get the right answers. As long as this doesn't error out, we're likely doing alright. avg, num_bad, ninetieth_pc = get_metrics_for_last_5_mins("https://example.com/", settings.TEST_POSTGRES_URI)
def test_check_health_timeout(self): health = check_health("https://example.com/xyz", timeout_secs=0.0001) self.assertEqual(health.status, "timeout") self.assertIsNone(health.code) self.assertIsNone(health.time_taken)
def test_check_health_negative(self): health = check_health("https://example.com/xyz", timeout_secs=1) self.assertEqual(health.status, "bad") self.assertEqual(health.code, 404) self.assertGreater(health.time_taken, 0)
def test_check_health(self): health = check_health("https://example.com/", timeout_secs=1) self.assertEqual(health.status, "ok") self.assertEqual(health.code, 200) self.assertGreater(health.time_taken, 0)