Example #1
0
  def __init__(self, docker_directory, container_id, docker_version=2):
    """Initializes the Container class.

    Args:
      docker_directory (str): the absolute path to the Docker directory.
      container_id (str): the container ID.
      docker_version (int): (Optional) the version of the Docker storage module.

    Raises:
      errors.BadContainerException: if there was an error when parsing
        container_info_json_path
    """
    self.container_config_filename = 'config.v2.json'
    if docker_version == 1:
      self.container_config_filename = 'config.json'

    self.docker_directory = docker_directory

    container_info_json_path = os.path.join(
        self.docker_directory, 'containers', container_id,
        self.container_config_filename)
    with open(container_info_json_path) as container_info_json_file:
      container_info_dict = json.load(container_info_json_file)

    if container_info_dict is None:
      raise errors.BadContainerException(
          'Could not load container configuration file {0}'.format(
              container_info_json_path)
      )

    self.container_id = container_info_dict.get('ID', None)
    json_config = container_info_dict.get('Config', None)
    if json_config:
      self.config_image_name = json_config.get('Image', None)
      self.config_labels = json_config.get('Labels', None)
    self.creation_timestamp = container_info_dict.get('Created', None)
    self.image_id = container_info_dict.get('Image', None)
    self.mount_id = None
    self.mount_points = container_info_dict.get('MountPoints', None)
    self.name = container_info_dict.get('Name', '')
    json_state = container_info_dict.get('State', None)
    if json_state:
      self.running = json_state.get('Running', False)
      self.start_timestamp = json_state.get('StartedAt', False)
    self.storage_driver = container_info_dict.get('Driver', None)
    if self.storage_driver is None:
      raise errors.BadContainerException(
          '{0} container config file lacks Driver key'.format(
              container_info_json_path))
    self.volumes = container_info_dict.get('Volumes', None)

    if docker_version == 2:
      c_path = os.path.join(
          self.docker_directory, 'image', self.storage_driver, 'layerdb',
          'mounts', container_id)
      with open(os.path.join(c_path, 'mount-id')) as mount_id_file:
        self.mount_id = mount_id_file.read()
Example #2
0
    def _SetStorage(self, storage_name):
        """Sets the storage_object attribute.

    Args:
      storage_name (str): the name of the storage.
    Returns:
      BaseStorage: a storage object.
    Raises:
      BadContainerException: if no storage Driver is defined, or if it is not
        implemented
    """
        storage_class = self.STORAGES_MAP.get(storage_name, None)

        if storage_class is None:
            raise errors.BadContainerException(
                'Storage driver {0} is not implemented'.format(storage_name))

        self.storage_object = storage_class(self.docker_directory,
                                            self.docker_version)
Example #3
0
    def __init__(self, docker_directory, container_id, docker_version=2):
        """Initializes the Container class.

    Args:
      docker_directory (str): the absolute path to the Docker directory.
      container_id (str): the container ID.
      docker_version (int): (Optional) the version of the Docker storage module.

    Raises:
      errors.BadContainerException: if there was an error when parsing
        container_info_json_path
    """
        self.docker_version = docker_version
        self.container_config_filename = 'config.v2.json'
        if self.docker_version == 1:
            self.container_config_filename = 'config.json'

        self.docker_directory = docker_directory

        container_info_json_path = os.path.join(self.docker_directory,
                                                'containers', container_id,
                                                self.container_config_filename)

        if not os.path.isfile(container_info_json_path):
            raise errors.BadContainerException(
                'Unable to find container configuration file: '
                f'{container_info_json_path}')
        with open(container_info_json_path,
                  encoding='utf-8') as container_info_json_file:
            container_info_dict = json.load(container_info_json_file)

        if container_info_dict is None:
            raise errors.BadContainerException(
                'Could not load container configuration file: '
                f'{container_info_json_path}')

        self.container_id = container_info_dict.get('ID', None)

        # Parse the 'Config' key, which relates to the Image configuration
        self.config_image_name = self._GetConfigValue(container_info_dict,
                                                      'Image')
        self.config_labels = self._GetConfigValue(container_info_dict,
                                                  'Labels')
        self.creation_timestamp = container_info_dict.get('Created', None)
        self.image_id = container_info_dict.get('Image', None)
        self.mount_id = None
        self.mount_points = container_info_dict.get('MountPoints', None)
        self.name = container_info_dict.get('Name', '')
        json_state = container_info_dict.get('State', None)
        if json_state:
            self.running = json_state.get('Running', False)
            self.start_timestamp = json_state.get('StartedAt', False)
        self.storage_name = container_info_dict.get('Driver', None)
        if self.storage_name is None:
            raise errors.BadContainerException(
                f'{container_info_json_path} container config file lacks Driver key'
            )
        self.upper_dir = None
        self.volumes = container_info_dict.get('Volumes', None)

        self.exposed_ports = self._GetConfigValue(container_info_dict,
                                                  'ExposedPorts')

        self._SetStorage(self.storage_name)

        if self.docker_version == 2:
            c_path = os.path.join(self.docker_directory, 'image',
                                  self.storage_name, 'layerdb', 'mounts',
                                  container_id)
            mount_id_path = os.path.join(c_path, 'mount-id')
            with open(mount_id_path, encoding='utf-8') as mount_id_file:
                self.mount_id = mount_id_file.read()

        if self.storage_name in ['overlay', 'overlay2']:
            self.upper_dir = os.path.join(self.storage_object.docker_directory,
                                          self.storage_object.STORAGE_METHOD,
                                          self.mount_id,
                                          self.storage_object.UPPERDIR_NAME)

        self.log_path = container_info_dict.get('LogPath', None)