Exemple #1
0
    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(
                    f'Expected image directory {image_path} does not exist.')
            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, encoding='utf-8') as rf:
                repo_obj = json.loads(rf.read())
                repo_obj['path'] = repositories_file_path
                result.append(repo_obj)

        return utils.PrettyPrintJSON(result)
Exemple #2
0
 def testPrettyPrintJSON(self):
   """Tests the utils.PrettyPrintJSON function."""
   test_dict = {'test': [{'dict1': {'key1': 'val1'}, 'dict2': None}]}
   expected_string = ('{\n    "test": [\n        {\n            "dict1": {\n'
                      '                "key1": "val1"\n            }, \n'
                      '            "dict2": null\n        }\n    ]\n}\n')
   self.assertEqual(expected_string, utils.PrettyPrintJSON(test_dict))
Exemple #3
0
  def ShowContainers(self, only_running=False):
    """Displays the running containers.

    Args:
      only_running (bool): Whether we display only running Containers.
    """
    print(utils.PrettyPrintJSON(
        self._explorer.GetContainersJson(only_running=only_running),
        sort_keys=False))
Exemple #4
0
  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._explorer.GetContainer(container_id)
    print(utils.PrettyPrintJSON(container_object.GetHistory(show_empty_layers),
                                sort_keys=False))