Пример #1
0
    def GetHistory(self, show_empty_layers=False):
        """Returns a dict containing the modification history of the container.

    Args:
      show_empty_layers (bool): whether to display empty layers.
    Returns:
      dict: object describing history of the container.
    """
        result_dict = {}
        for layer in self.GetOrderedLayers():
            layer_info = self.GetLayerInfo(layer)
            layer_dict = {}

            if layer is None:
                raise ValueError('Layer {0:s} does not exist'.format(layer))
            layer_size = self.GetLayerSize(layer)
            layer_dict['size'] = layer_size
            if layer_size > 0 or show_empty_layers or self.docker_version == 2:
                layer_dict['created_at'] = utils.FormatDatetime(
                    layer_info['created'])
                container_cmd = layer_info['container_config'].get('Cmd', None)
                if container_cmd:
                    layer_dict['container_cmd'] = ' '.join(container_cmd)
                comment = layer_info.get('comment', None)
                if comment:
                    layer_dict['comment'] = comment
            result_dict[layer] = layer_dict

        return result_dict
Пример #2
0
  def GetHistory(self, container_object, show_empty_layers=False):
    """Returns a string representing the modification history of a container.

    Args:
      container_object (Container): the container object.
      show_empty_layers (bool): whether to display empty layers.
    Returns:
      str: the human readable history.
    """
    history_str = ''
    for layer in self.GetOrderedLayers(container_object):
      layer_info = self.GetLayerInfo(layer)
      if layer is None:
        raise ValueError('Layer {0:s} does not exist'.format(layer))
      history_str += '-------------------------------------------------------\n'
      history_str += layer+'\n'
      if layer_info is None:
        history_str += 'no info =('
      else:
        layer_size = self.GetLayerSize(layer)
        if layer_size > 0 or show_empty_layers or self.docker_version == 2:
          history_str += '\tsize : {0:d}'.format(layer_size)
          history_str += '\tcreated at : {0:s}'.format(
              utils.FormatDatetime(layer_info['created']))
          container_cmd = layer_info['container_config'].get('Cmd', None)
          if container_cmd:
            history_str += '\twith command : {0:s}'.format(
                ' '.join(container_cmd))
          comment = layer_info.get('comment', None)
          if comment:
            history_str += '\tcomment : {0:s}'.format(comment)
        else:
          history_str += 'Empty layer'
    return history_str
Пример #3
0
  def GetContainersString(self, only_running=False):
    """Returns a string describing the running containers.

    Args:
      only_running (bool): Whether we display only running Containers.

    Returns:
      str: the string displaying information about running containers.
    """
    result_string = ''
    for container_object in self.GetContainersList(only_running=only_running):
      image_id = container_object.image_id
      if self.docker_version == 2:
        image_id = image_id.split(':')[1]

      if container_object.config_labels:
        labels_list = ['{0:s}: {1:s}'.format(k, v) for (k, v) in
                       container_object.config_labels.items()]
        labels_str = ', '.join(labels_list)
        result_string += 'Container id: {0:s} / Labels : {1:s}\n'.format(
            container_object.container_id, labels_str)
      else:
        result_string += 'Container id: {0:s} / No Label\n'.format(
            container_object.container_id)
      result_string += '\tStart date: {0:s}\n'.format(
          utils.FormatDatetime(container_object.start_timestamp))
      result_string += '\tImage ID: {0:s}\n'.format(image_id)
      result_string += '\tImage Name: {0:s}\n'.format(
          container_object.config_image_name)

    return result_string
Пример #4
0
  def GetContainersJson(self, only_running=False):
    """Returns a dict describing the running containers.

    Args:
      only_running (bool): Whether we display only running Containers.

    Returns:
      dict: A dict object representing the containers.
    """
    result = []
    for container_object in self.GetContainersList(only_running=only_running):
      image_id = container_object.image_id
      if self.docker_version == 2:
        image_id = image_id.split(':')[1]
      container_json = {
          'container_id': container_object.container_id,
          'image_id': image_id
      }

      if container_object.config_labels:
        container_json['labels'] = container_object.config_labels
      container_json['start_date'] = utils.FormatDatetime(
          container_object.start_timestamp)
      container_json['image_name'] = container_object.config_image_name

      if container_object.mount_id:
        container_json['mount_id'] = container_object.mount_id

      result.append(container_json)

    return result
Пример #5
0
 def testFormatDatetime(self):
   """Tests the utils.FormatDatetime function."""
   test_date = '2017-12-25T15:59:59.102938 msqedigrb msg'
   expected_time_str = '2017-12-25T15:59:59.102938'
   self.assertEqual(expected_time_str, utils.FormatDatetime(test_date))