def check_all_boxes(): nomad_client = nomad.Nomad(discover_service("nomad").ip) deployments = nomad_client.job.get_deployments("ssh-client") for deployment in deployments: tunnel_exist = Tunnel.query.filter_by(job_id=deployment).first() if not tunnel_exist: cleanup_old_nomad_box(deployment)
def cleanup_old_nomad_box(job_id): nomad_client = nomad.Nomad(discover_service("nomad").ip) try: del_box_nomad(nomad_client, job_id) except nomad.api.exceptions.BaseNomadException: cleanup_old_nomad_box.schedule(timedelta(hours=2), job_id, timeout=60000) raise nomad.api.exceptions.BaseNomadException
def __init__(self, current_user: User, tunnel: Optional[Tunnel], job_id=None): self.current_user = current_user self.tunnel = tunnel if job_id: self.job_id = job_id if tunnel: self.subdomain = tunnel.subdomain self.job_id = tunnel.job_id self.nomad_client = nomad.Nomad(discover_service("nomad").ip)
def __init__(self, current_user: User, box: Optional[Box], job_id=None): self.current_user = current_user self.box = box if job_id: self.job_id = job_id if box: self.config = box.config self.job_id = box.job_id self.nomad_client = nomad.Nomad(discover_service("nomad").ip)
def test_third_invocation_of_named_tunnel_works(self, current_user, session): asub = ReservedSubdomainFactory(user=current_user, name="bobjoeboe") session.add(asub) session.flush() first_time = TunnelCreationService( current_user=current_user, subdomain_id=asub.id, port_types=["http"], ssh_key="", ).create() nomad_client = nomad.Nomad(discover_service("nomad").ip) del_tunnel_nomad(nomad_client, first_time.job_id) asub.in_use = False session.add(asub) session.flush() second_time = TunnelCreationService( current_user=current_user, subdomain_id=asub.id, port_types=["http"], ssh_key="", ).create() nomad_client = nomad.Nomad(discover_service("nomad").ip) del_tunnel_nomad(nomad_client, first_time.job_id) asub.in_use = False session.add(asub) session.flush() third_time = TunnelCreationService( current_user=current_user, subdomain_id=asub.id, port_types=["http"], ssh_key="", ).create() assert first_time.ssh_port != second_time.ssh_port != third_time.ssh_port
def check_all_boxes(): nomad_client = nomad.Nomad(discover_service("nomad").ip) deployments = nomad_client.job.get_deployments("ssh-client") for deployment in deployments: box_exist = Box.query.filter_by(job_id=deployment).first() if datetime.utcnow >= box_exist.session_end_time: cleanup_old_nomad_box(deployment) continue if not box_exist: cleanup_old_nomad_box(deployment)
def test_find_unused_boxes(self, current_user, session): """ Kills unused boxes """ asub = ReservedSubdomainFactory(user=current_user, name="bobjoebob") session.add(asub) session.flush() TunnelCreationService( current_user=current_user, subdomain_id=asub.id, port_types=["http"], ssh_key="", ).create() find_unused_boxes() find_unused_boxes() nomad_client = nomad.Nomad(discover_service("nomad").ip) deploys = nomad_client.job.get_deployments("ssh-client-bobjoebob") assert len(deploys) == 0
def __init__( self, current_user: User, config_id: Optional[int], ssh_key: str, image: str, ): self.ssh_key = ssh_key self.image = image self.current_user = current_user self.session_end_time = datetime.utcnow() + timedelta( seconds=current_user.limits().duration) if config_id: self.config = Config.query.get(config_id) else: self.config = ConfigCreationService(self.current_user).create() # We need to do this each time so each if a nomad service goes down # it doesnt affect web api self.nomad_client = nomad.Nomad(discover_service("nomad").ip)
def __init__( self, current_user: User, subdomain_id: Optional[int], port_types: list, ssh_key: str, ): self.port_types = port_types self.ssh_key = ssh_key self.current_user = current_user if subdomain_id: self.subdomain = Subdomain.query.get(subdomain_id) else: tcp_url = len(self.port_types) == 1 and self.port_types[0] == "tcp" self.subdomain = SubdomainCreationService( self.current_user ).get_unused_subdomain(tcp_url) # We need to do this each time so each if a nomad service goes down # it doesnt affect web api self.nomad_client = nomad.Nomad(discover_service("nomad").ip)
def find_unused_boxes(): consul_client = consul.Consul(host=discover_service("consul").ip) services = consul_client.catalog.services() user_services = {k: v for (k, v) in services[1].items() if "ssh-" in k} for service in user_services: health_checks = consul_client.health.service(service)[1][0]["Checks"] for health_check in health_checks: if health_check["Name"] == "Serf Health Status": continue if health_check["Status"] == "passing": continue subdomain = health_check["ServiceName"].split("-")[1] if subdomain == "TCP": subdomain = subdomain + "-" + health_check[ "ServiceName"].split("-")[2] exists = redis_client.sismember("unhealthy_tunnels", subdomain) if exists: redis_client.srem("unhealthy_tunnels", subdomain) cleanup_old_nomad_box("ssh-client-" + subdomain) else: redis_client.sadd("unhealthy_tunnels", subdomain)
def test_service_discovery_with_a_record(self): ip = discover_service("nomad", "A").ip assert re.match(r"172\.1[78]\.\d{1,3}\.\d{1,3}", ip)
def test_service_discovery_with_a_record(self): ip = discover_service("nomad", "A").ip # NOTE - This test depends on the local ip address of your docker bridge # which changes from time to time. expand this regex as necessary assert re.match(r"172\.1[6789]\.\d{1,3}\.\d{1,3}", ip)