Exemplo n.º 1
0
 def mk_dir(self, path, mode=None, uid=None, gid=None):
     """Creates an empty directory on the container.
     This pushes an empty directory to the containers file system
     named by the `filepath`.
     :param path: The path in the container to to store the data in.
     :type path: str
     :param mode: The unit mode to store the file with.  The default of
         None stores the file with the current mask of 0700, which is
         the lxd default.
     :type mode: Union[oct, int, str]
     :param uid: The uid to use inside the container. Default of None
         results in 0 (root).
     :type uid: int
     :param gid: The gid to use inside the container.  Default of None
         results in 0 (root).
     :type gid: int
     :raises: LXDAPIException if something goes wrong
     """
     headers = self._resolve_headers(mode=mode, uid=uid, gid=gid)
     headers["X-LXD-type"] = "directory"
     response = self._endpoint.post(params={"path": path},
                                    headers=headers)
     if response.status_code == 200:
         return
     raise LXDAPIException(response)
Exemplo n.º 2
0
        def put(self, filepath, data, mode=None, uid=None, gid=None):
            """Push a file to the container.

            This pushes a single file to the containers file system named by
            the `filepath`.

            :param filepath: The path in the container to to store the data in.
            :type filepath: str
            :param data: The data to store in the file.
            :type data: bytes or str
            :param mode: The unit mode to store the file with.  The default of
                None stores the file with the current mask of 0700, which is
                the lxd default.
            :type mode: Union[oct, int, str]
            :param uid: The uid to use inside the container. Default of None
                results in 0 (root).
            :type uid: int
            :param gid: The gid to use inside the container.  Default of None
                results in 0 (root).
            :type gid: int
            :raises: LXDAPIException if something goes wrong
            """
            headers = self._resolve_headers(mode=mode, uid=uid, gid=gid)
            response = (
                self._client.api.containers[self._container.name].files.post(
                    params={'path': filepath},
                    data=data,
                    headers=headers or None))
            if response.status_code == 200:
                return
            raise LXDAPIException(response)
Exemplo n.º 3
0
    def put(cls, client, cert, key):
        response = client.api.cluster.certificate.put(
            json={"cluster_certificate": cert, "cluster_certificate_key": key}
        )

        if response.status_code == 200:
            return
        raise LXDAPIException(response)
Exemplo n.º 4
0
 def delete(self, filepath):
     if not self.delete_available():
         raise ValueError(
             'File Deletion is not available for this host')
     response = self._client.api.containers[
         self._container.name].files.delete(params={'path': filepath})
     if response.status_code != 200:
         raise LXDAPIException(response)
Exemplo n.º 5
0
        def recursive_put(self, src, dst, mode=None, uid=None, gid=None):
            """Recursively push directory to the container.

            Recursively pushes directory to the containers
            named by the `dst`

            :param src: The source path of directory to copy.
            :type src: str
            :param dst: The destination path in the container
                    of directory to copy
            :type dst: str
            :param mode: The unit mode to store the file with.  The default of
                None stores the file with the current mask of 0700, which is
                the lxd default.
            :type mode: Union[oct, int, str]
            :param uid: The uid to use inside the container. Default of None
                results in 0 (root).
            :type uid: int
            :param gid: The gid to use inside the container.  Default of None
                results in 0 (root).
            :type gid: int
            :raises: NotADirectoryError if src is not a directory
            :raises: LXDAPIException if an error occurs
            """
            norm_src = os.path.normpath(src)
            if not os.path.isdir(norm_src):
                raise NotADirectoryError(
                    "'src' parameter must be a directory ")

            idx = len(norm_src)
            dst_items = set()

            for path, dirname, files in os.walk(norm_src):
                dst_path = os.path.normpath(
                    os.path.join(dst, path[idx:].lstrip(os.path.sep)))
                # create directory or symlink (depending on what's there)
                if path not in dst_items:
                    dst_items.add(path)
                    headers = self._resolve_headers(mode=mode,
                                                    uid=uid,
                                                    gid=gid)
                    # determine what the file is: a directory or a symlink
                    fmode = os.stat(path).st_mode
                    if stat.S_ISLNK(fmode):
                        headers['X-LXD-type'] = 'symlink'
                    else:
                        headers['X-LXD-type'] = 'directory'
                    (self._client.api.containers[
                        self._container.name].files.post(
                            params={'path': dst_path}, headers=headers))

                # copy files
                for f in files:
                    src_file = os.path.join(path, f)
                    with open(src_file, 'rb') as fp:
                        filepath = os.path.join(dst_path, f)
                        headers = self._resolve_headers(mode=mode,
                                                        uid=uid,
                                                        gid=gid)
                        response = (self._client.api.containers[
                            self._container.name].files.post(
                                params={'path': filepath},
                                data=fp.read(),
                                headers=headers or None))
                        if response.status_code != 200:
                            raise LXDAPIException(response)
Exemplo n.º 6
0
 def delete(self, filepath):
     self._client.assert_has_api_extension('file_delete')
     response = self._client.api.containers[
         self._container.name].files.delete(params={'path': filepath})
     if response.status_code != 200:
         raise LXDAPIException(response)
Exemplo n.º 7
0
 def generate_exception(*args, **kwargs):
     response = mock.Mock()
     response.status_code = 103
     raise LXDAPIException(response)
Exemplo n.º 8
0
 def delete(self, filepath):
     self._instance.client.assert_has_api_extension("file_delete")
     response = self._endpoint.delete(params={"path": filepath})
     if response.status_code != 200:
         raise LXDAPIException(response)
Exemplo n.º 9
0
 def generate_exception():
     response = mock.Mock()
     response.status_code = 400
     raise LXDAPIException(response)