def GetRepositoriesString(self): """Returns information about images in the local Docker repositories. Returns: str: human readable list of images in local Docker repositories. Raises: errors.BadStorageException: If required files or directories are not found in the provided Docker directory. """ repositories = [] if self.docker_version == 1: repositories = [os.path.join(self.docker_directory, 'repositories-aufs')] else: image_path = os.path.join(self.docker_directory, 'image') if not os.path.isdir(image_path): raise errors.BadStorageException( 'Expected image directory {0} does not exist.'.format(image_path)) for storage_method in os.listdir(image_path): repositories_file_path = os.path.join( image_path, storage_method, 'repositories.json') if os.path.isfile(repositories_file_path): repositories.append(repositories_file_path) result = [] for repositories_file_path in sorted(repositories): with open(repositories_file_path) as rf: repo_obj = json.loads(rf.read()) repo_obj['path'] = repositories_file_path result.append(repo_obj) return utils.PrettyPrintJSON(result)
def testPrettyPrintJSON(self): """Tests the utils.PrettyPrintJSON function.""" test_dict = {'test': [{'dict1': {'key1': 'val1'}, 'dict2': None}]} test_json = json.dumps(test_dict) expected_string = ('{\n "test": [\n {\n "dict1": {\n' ' "key1": "val1"\n }, \n' ' "dict2": null\n }\n ]\n}') self.assertEqual(expected_string, utils.PrettyPrintJSON(test_json))
def ShowContainers(self, only_running=False): """Displays the running containers. Args: only_running (bool): Whether we display only running Containers. """ print(utils.PrettyPrintJSON( self.GetContainersJson(only_running=only_running)))
def ShowHistory(self, container_id, show_empty_layers=False): """Prints the modification history of a container. Args: container_id (str): the ID of the container. show_empty_layers (bool): whether to display empty layers. """ container_object = self.GetContainer(container_id) print(utils.PrettyPrintJSON(container_object.GetHistory(show_empty_layers)))
def ShowRepositories(self): """Returns information about the images in the Docker repository. Returns: str: human readable information about image repositories. """ repositories_file_path = os.path.join( self.docker_directory, 'image', self.STORAGE_METHOD, 'repositories.json') if self.docker_version == 1: repositories_file_path = os.path.join( self.docker_directory, 'repositories-aufs') result_string = ( 'Listing repositories from file {0:s}').format(repositories_file_path) with open(repositories_file_path) as rf: repositories_string = rf.read() return result_string + utils.PrettyPrintJSON(repositories_string)