Beispiel #1
0
    def get_file_details(self, file_path):
        """
        Retrieves the contents of a file and its permissions.

        @param file_path: Path to the file
        @type file_path: string
        @return: File details including permissions and content
        @rtype: FileDetails
        """

        command = ('[ -f {file_path} ] && echo "File exists" || '
                   'echo "File does not exist"'.format(file_path=file_path))
        output = self.ssh_client.execute_command(command)
        if output is None:
            return None
        output = output.stdout

        if not output.rstrip('\n') == 'File exists':
            raise FileNotFoundException(
                "File {file_path} not found on instance.".format(
                    file_path=file_path))

        file_permissions = self.ssh_client.execute_command(
            'stat -c %a {file_path}'.format(
                file_path=file_path)).stdout.rstrip("\n")
        file_contents = self.ssh_client.execute_command(
            'cat {file_path}'.format(file_path=file_path)).stdout
        return FileDetails(file_permissions, file_contents, file_path)
 def create_file(self, file_name, file_content, file_path=None):
     '''
     @summary: Create a new file
     @param file_name: File Name
     @type file_name: String
     @param file_content: File Content
     @type file_content: String
     @return filedetails: File details such as content, name and path
     @rtype filedetails; FileDetails
     '''
     if file_path is None:
         file_path = "/root/" + file_name
     self.ssh_client.execute_command('echo -n ' + file_content + '>>' +
                                     file_path)
     return FileDetails("644", file_content, file_path)
Beispiel #3
0
    def get_file_details(self, file_path):
        """
        Retrieves the contents of a file and its permissions.

        @param file_path: Path to the file
        @type file_path: string
        @return: File details including permissions and content
        @rtype: FileDetails
        """

        file_permissions = self.get_filesystem_permissions(path=file_path)

        file_contents = self.client.execute_command(
            'type {file_path}'.format(file_path=file_path)).std_out
        return FileDetails(file_permissions, file_contents.rstrip("\n"),
                           file_path)
Beispiel #4
0
    def create_file(self, file_name, file_content, file_path):
        """
        Creates a new file with the provided content.

        @param file_name: File name
        @type file_name: string
        @param file_content: File content
        @type file_content: String
        @rtype: FileDetails
        """
        self.client.execute_command(
            'echo {file_content} >> {file_path}\\{file_name}'.format(
                file_content=file_content,
                file_path=file_path,
                file_name=file_name))
        return FileDetails(None, file_content, file_path)
Beispiel #5
0
    def create_file(self, file_name, file_content, file_path=None):
        """
        Creates a new file with the provided content.

        @param file_name: File name
        @type file_name: string
        @param file_content: File content
        @type file_content: String
        @rtype: FileDetails
        """

        if file_path is None:
            file_path = "/root/{file_name}".format(file_name=file_name)
        self.ssh_client.execute_command(
            'echo -n {file_content} >> {file_path}'.format(
                file_content=file_content, file_name=file_name))
        return FileDetails("644", file_content, file_path)
    def get_file_details(self, filepath):
        """
        @summary: Get the file details
        @param filepath: Path to the file
        @type filepath: string
        @return: File details including permissions and content
        @rtype: FileDetails
        """
        command = ('[ -f ' + filepath + ' ] && echo "File exists" || '
                   'echo "File does not exist"')
        output = self.ssh_client.exec_command(command)
        if not output.rstrip('\n') == 'File exists':
            raise FileNotFoundException("File:" + filepath +
                                        " not found on instance.")

        file_permissions = self.ssh_client.exec_command('stat -c %a ' +
                                                        filepath).rstrip("\n")
        file_contents = self.ssh_client.exec_command('cat ' + filepath)
        return FileDetails(file_permissions, file_contents, filepath)
Beispiel #7
0
    def create_file(self, file_name, file_content, file_path=None):
        """
        Creates a new file with the provided content.

        @param file_name: File name
        @type file_name: string
        @param file_content: File content
        @type file_content: String
        @rtype: FileDetails
        """

        file_path = file_path or "/root"

        if not file_path.endswith("/"):
            file_path = "{0}/".format(file_path)

        file_path = "{0}{1}".format(file_path, file_name)

        self.ssh_client.execute_command(
            'echo -n {file_content} >> {file_path}'.format(
                file_content=file_content, file_path=file_path))

        return FileDetails("644", file_content, file_path)