2021-03-08 15:42:49 +00:00
|
|
|
from contextlib import contextmanager
|
2021-03-05 22:50:11 +00:00
|
|
|
import logging
|
2021-03-08 15:42:49 +00:00
|
|
|
import requests
|
|
|
|
from retrying import retry
|
2021-03-05 22:50:11 +00:00
|
|
|
import subprocess
|
|
|
|
import yaml
|
|
|
|
|
2021-03-08 15:42:49 +00:00
|
|
|
from time import sleep
|
|
|
|
|
|
|
|
from constants import METRICS_PORT, MAX_RETRIES, BACKOFF_SECS
|
|
|
|
|
2021-03-05 22:50:11 +00:00
|
|
|
LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2021-03-08 15:42:49 +00:00
|
|
|
|
2021-03-05 22:50:11 +00:00
|
|
|
def write_config(path, config):
|
|
|
|
config_path = path / "config.yaml"
|
|
|
|
with open(config_path, 'w') as outfile:
|
|
|
|
yaml.dump(config, outfile)
|
|
|
|
return config_path
|
|
|
|
|
|
|
|
|
2021-03-11 13:49:09 +00:00
|
|
|
def start_cloudflared(path, config, cfd_args=["run"], cfd_pre_args=["tunnel"], new_process=False, allow_input=False, capture_output=True):
|
|
|
|
config_path = write_config(path, config.full_config)
|
|
|
|
cmd = [config.cloudflared_binary]
|
2021-03-08 14:09:10 +00:00
|
|
|
cmd += cfd_pre_args
|
2021-03-05 22:50:11 +00:00
|
|
|
cmd += ["--config", config_path]
|
2021-03-08 14:09:10 +00:00
|
|
|
cmd += cfd_args
|
2021-03-05 22:50:11 +00:00
|
|
|
LOGGER.info(f"Run cmd {cmd} with config {config}")
|
2021-03-08 15:42:49 +00:00
|
|
|
if new_process:
|
2021-03-11 13:49:09 +00:00
|
|
|
return run_cloudflared_background(cmd, allow_input, capture_output)
|
2021-03-08 15:42:49 +00:00
|
|
|
# By setting check=True, it will raise an exception if the process exits with non-zero exit code
|
|
|
|
return subprocess.run(cmd, check=True, capture_output=capture_output)
|
|
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
2021-03-11 13:49:09 +00:00
|
|
|
def run_cloudflared_background(cmd, allow_input, capture_output):
|
2021-03-08 15:42:49 +00:00
|
|
|
output = subprocess.PIPE if capture_output else subprocess.DEVNULL
|
2021-03-11 13:49:09 +00:00
|
|
|
stdin = subprocess.PIPE if allow_input else None
|
2021-03-08 15:42:49 +00:00
|
|
|
try:
|
2021-03-11 13:49:09 +00:00
|
|
|
cfd = subprocess.Popen(cmd, stdin=stdin, stdout=output, stderr=output)
|
2021-03-08 15:42:49 +00:00
|
|
|
yield cfd
|
|
|
|
finally:
|
|
|
|
cfd.terminate()
|
|
|
|
|
|
|
|
|
|
|
|
@retry(stop_max_attempt_number=MAX_RETRIES, wait_fixed=BACKOFF_SECS * 1000)
|
2021-03-11 20:43:31 +00:00
|
|
|
def wait_tunnel_ready(tunnel_url=None, expect_connections=4):
|
|
|
|
metrics_url = f'http://localhost:{METRICS_PORT}/ready'
|
2021-03-11 13:49:09 +00:00
|
|
|
|
|
|
|
with requests.Session() as s:
|
2021-03-11 20:43:31 +00:00
|
|
|
resp = send_request(s, metrics_url, True)
|
2021-03-11 13:49:09 +00:00
|
|
|
assert resp.json()[
|
2021-03-11 20:43:31 +00:00
|
|
|
"readyConnections"] >= expect_connections, f"Ready endpoint returned {resp.json()} but we expect at least {expect_connections} connections"
|
|
|
|
if tunnel_url is not None:
|
|
|
|
send_request(s, tunnel_url, True)
|
2021-03-11 13:49:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
@retry(stop_max_attempt_number=MAX_RETRIES, wait_fixed=BACKOFF_SECS * 1000)
|
2021-03-11 20:43:31 +00:00
|
|
|
def check_tunnel_not_connected():
|
2021-03-11 13:49:09 +00:00
|
|
|
url = f'http://localhost:{METRICS_PORT}/ready'
|
|
|
|
|
|
|
|
resp = requests.get(url, timeout=1)
|
|
|
|
assert resp.status_code == 503, f"Expect {url} returns 503, got {resp.status_code}"
|
2021-03-08 15:42:49 +00:00
|
|
|
|
|
|
|
|
2021-03-11 20:43:31 +00:00
|
|
|
# In some cases we don't need to check response status, such as when sending batch requests to generate logs
|
2021-03-08 15:42:49 +00:00
|
|
|
def send_requests(url, count, require_ok=True):
|
|
|
|
errors = 0
|
|
|
|
with requests.Session() as s:
|
|
|
|
for _ in range(count):
|
2021-03-11 13:49:09 +00:00
|
|
|
resp = send_request(s, url, require_ok)
|
|
|
|
if resp is None:
|
2021-03-08 15:42:49 +00:00
|
|
|
errors += 1
|
|
|
|
sleep(0.01)
|
|
|
|
if errors > 0:
|
|
|
|
LOGGER.warning(
|
|
|
|
f"{errors} out of {count} requests to {url} return non-200 status")
|
|
|
|
|
|
|
|
|
|
|
|
@retry(stop_max_attempt_number=MAX_RETRIES, wait_fixed=BACKOFF_SECS * 1000)
|
|
|
|
def send_request(session, url, require_ok):
|
|
|
|
resp = session.get(url, timeout=BACKOFF_SECS)
|
|
|
|
if require_ok:
|
|
|
|
assert resp.status_code == 200, f"{url} returned {resp}"
|
2021-03-11 13:49:09 +00:00
|
|
|
return resp if resp.status_code == 200 else None
|