def get_container_status(labbook_name: str, owner: str, username: str) -> bool: labbook_key = infer_docker_image_name(labbook_name=labbook_name, owner=owner, username=username) try: client = get_docker_client() container = client.containers.get(labbook_key) if container.status == "running": return True else: return False except: pass return False
def prior_mutate_and_get_payload(cls, root, info, owner, original_labbook_name, new_labbook_name, client_mutation_id=None): # NOTE!!! This is the code that was originally to rename. # Temporarily, rename functionality is disabled. # Load LabBook username = get_logged_in_username() working_directory = Configuration().config['git']['working_directory'] inferred_lb_directory = os.path.join(working_directory, username, owner, 'labbooks', original_labbook_name) lb = LabBook(author=get_logged_in_author()) lb.from_directory(inferred_lb_directory) # Image names old_tag = '{}-{}-{}'.format(username, owner, original_labbook_name) new_tag = '{}-{}-{}'.format(username, owner, new_labbook_name) # Rename LabBook lb.rename(new_labbook_name) logger.info( f"Renamed LabBook from `{original_labbook_name}` to `{new_labbook_name}`" ) # Build image with new name...should be fast and use the Docker cache client = get_docker_client() image_builder = ImageBuilder(lb.root_dir) image_builder.build_image(docker_client=client, image_tag=new_tag, username=username, background=True) # Delete old image if it had previously been built successfully try: client.images.get(old_tag) client.images.remove(old_tag) except ImageNotFound: logger.warning( f"During renaming, original image {old_tag} not found, removal skipped." ) return RenameLabbook(success=True)
def resolve_container_status(self, info): """Resolve the image_status field""" # Check if the container is running by looking up the container labbook_key = infer_docker_image_name(labbook_name=self.name, owner=self.owner, username=get_logged_in_username()) try: client = get_docker_client() container = client.containers.get(labbook_key) if container.status == "running": container_status = ContainerStatus.RUNNING else: container_status = ContainerStatus.NOT_RUNNING except (NotFound, requests.exceptions.ConnectionError): container_status = ContainerStatus.NOT_RUNNING return container_status.value
def docker_socket_fixture(): """Helper method to get the docker client version""" client = get_docker_client() version = client.version()['ApiVersion'] if "CIRCLECI" in os.environ: docker_host = os.environ['DOCKER_HOST'] docker_host = docker_host.replace("tcp", "https") responses.add_passthru( f"{docker_host}/v{version}/images/default-default-labbook1/json") responses.add_passthru( f"{docker_host}/v{version}/containers/default-default-labbook1/json" ) responses.add_passthru( f"{docker_host}/v{version}/images/default-default-labbook1/json") responses.add_passthru( f"{docker_host}/v{version}/containers/default-default-labbook1/json" ) responses.add_passthru( f"{docker_host}/v{version}/images/default-test-sample-repo-lb/json" ) responses.add_passthru( f"{docker_host}/v{version}/containers/default-test-sample-repo-lb/json" ) responses.add_passthru( #'http+docker://35.196.196.144:2376/v1.30/containers/default-test-sample-repo-lb/json') '{docker_host}/v{version}/containers/default-test-sample-repo-lb/json' ) else: responses.add_passthru( f"http+docker://localunixsocket/v{version}/images/default-default-labbook1/json" ) responses.add_passthru( f"http+docker://localunixsocket/v{version}/containers/default-default-labbook1/json" ) responses.add_passthru( f"http+docker://localunixsocket/v{version}/images/default-test-sample-repo-lb/json" ) responses.add_passthru( f"http+docker://localunixsocket/v{version}/containers/default-test-sample-repo-lb/json" ) yield
def helper_resolve_image_status(self, labbook): """Helper to resolve the image status of a labbook""" labbook_image_key = infer_docker_image_name(labbook_name=self.name, owner=self.owner, username=get_logged_in_username()) dispatcher = Dispatcher() lb_jobs = [dispatcher.query_task(j.job_key) for j in dispatcher.get_jobs_for_labbook(labbook.key)] for j in lb_jobs: logger.debug("Current job for labbook: status {}, meta {}".format(j.status, j.meta)) # First, check if image exists or not -- The first step of building an image untags any existing ones. # Therefore, we know that if one exists, there most likely is not one being built. try: client = get_docker_client() client.images.get(labbook_image_key) image_status = ImageStatus.EXISTS except (ImageNotFound, requests.exceptions.ConnectionError): image_status = ImageStatus.DOES_NOT_EXIST if any([j.status == 'failed' and j.meta.get('method') == 'build_image' for j in lb_jobs]): logger.debug("Image status for {} is BUILD_FAILED".format(labbook.key)) if image_status == ImageStatus.EXISTS: # The indication that there's a failed job is probably lingering from a while back, so don't # change the status to FAILED. Only do that if there is no Docker image. logger.debug(f'Got failed build_image for labbook {labbook.key}, but image exists.') else: image_status = ImageStatus.BUILD_FAILED if any([j.status in ['started'] and j.meta.get('method') == 'build_image' for j in lb_jobs]): logger.debug(f"Image status for {labbook.key} is BUILD_IN_PROGRESS") # build_image being in progress takes precedence over if image already exists (unlikely event). if image_status == ImageStatus.EXISTS: logger.warning(f'Got build_image for labbook {labbook.key}, but image exists.') image_status = ImageStatus.BUILD_IN_PROGRESS if any([j.status in ['queued'] and j.meta.get('method') == 'build_image' for j in lb_jobs]): logger.warning(f"build_image for {labbook.key} stuck in queued state") image_status = ImageStatus.BUILD_QUEUED return image_status.value
def reset_images(request): """A pytest fixture that checks if the test images exist and deletes them""" # Clean up images client = get_docker_client() # image should never exist before the test starts image_name = "gmlb-{}-{}".format(get_logged_in_username(), f'default-{request.param}') try: client.images.get(image_name) client.images.remove(image_name) raise ValueError( "Test image exists before test started. Attempting to automatically removing image. Run again" ) except ImageNotFound: pass yield None try: client.images.get(image_name) client.images.remove(image_name) except ImageNotFound: pass
def build_image_for_jupyterlab(): # Create temp dir config_file, temp_dir = _create_temp_work_dir() # Create user identity user_dir = os.path.join(temp_dir, '.labmanager', 'identity') os.makedirs(user_dir) with open(os.path.join(user_dir, 'user.json'), 'wt') as user_file: json.dump( { "username": "******", "email": "*****@*****.**", "given_name": "unittester", "family_name": "tester" }, user_file) # Create test client schema = graphene.Schema(query=LabbookQuery, mutation=LabbookMutations) # get environment data and index erm = RepositoryManager(config_file) erm.update_repositories() erm.index_repositories() with patch.object(Configuration, 'find_default_config', lambda self: config_file): # Load User identity into app context app = Flask("lmsrvlabbook") app.config["LABMGR_CONFIG"] = Configuration() app.config["LABMGR_ID_MGR"] = get_identity_manager(Configuration()) with app.app_context(): # within this block, current_app points to app. Set current user explicitly (this is done in the middleware) flask.g.user_obj = app.config["LABMGR_ID_MGR"].get_user_profile() # Create a test client client = Client( schema, middleware=[LabBookLoaderMiddleware(), error_middleware], context_value=ContextMock()) # Create a labook lb = LabBook(config_file) lb.new(name="containerunittestbook", description="Testing docker building.", owner={"username": "******"}) # Create Component Manager cm = ComponentManager(lb) # Add a component cm.add_component("base", ENV_UNIT_TEST_REPO, ENV_UNIT_TEST_BASE, ENV_UNIT_TEST_REV) cm.add_packages("pip3", [{ "manager": "pip3", "package": "requests", "version": "2.18.4" }]) ib = ImageBuilder(lb) ib.assemble_dockerfile(write=True) docker_client = get_docker_client() try: lb, docker_image_id = ContainerOperations.build_image( labbook=lb, username="******") yield lb, ib, docker_client, docker_image_id, client, "unittester" finally: shutil.rmtree(lb.root_dir) try: docker_client.containers.get(docker_image_id).stop() docker_client.containers.get(docker_image_id).remove() except: pass try: docker_client.images.remove(docker_image_id, force=True, noprune=False) except: pass