Ejemplo n.º 1
0
    def __init__(self, url):

        exp = re.compile(sftpUrlRegex)
        match = exp.search(url)
        if match is None:
            raise FuseFAMException(
                _("Error matching the schema 'sftp://*****:*****@server/anything' with '%s' (The '/' after server is mandatory)"
                  ) % url)

        self.user = match.group(2)
        self.password = match.group(3)
        self.server = match.group(4)
        self.port = match.group(6)
        self.pathinside = match.group(7)
Ejemplo n.º 2
0
 def _defineMountDirName(self, remote):
     """
     """
     exp = re.compile(ssh_url_re)
     match = exp.search(remote)
     if not match:
         raise FuseFAMException(
             _("Error matching the schema 'ssh://*****:*****@example.com:21/home/' with '%s' (The '/' after server is mandatory)"
               ) % remote)
     else:
         user = match.group(1)
         dirname = "ssh_" + user + "@" + match.group(4)
         if match.group(6):
             dirname += "_" + match.group(6)
         return dirname
Ejemplo n.º 3
0
    def mount(self, source, mountbase):
        """
        Mount the source intor the mountbase dir . This method should create a mount point to mount the source.
        The name of the mount point should be very expressive so that we avoid collision with other mount points
        @param source: The remote path
        @param mountbase: The mount points base dir
        @return: The mount point complete path
        """
        exp = re.compile(ssh_url_re)
        match = exp.search(source)
        if not match:
            raise FuseFAMException(
                _("Error matching the schema 'ssh://*****:*****@example.com/home/' with '%s' (The '/' after server is mandatory)"
                  ) % source)
        else:
            remoteSource = "ssh://" + match.group(1)
            if match.group(3):
                remoteSource += ":" + match.group(3)
            remoteSource += "@" + match.group(4)
            if match.group(6):
                remoteSource += ":" + match.group(6)
            remoteSource += "/"

            user = match.group(1)
            mountpoint = local_file_utils.joinpath(
                mountbase, self._defineMountDirName(source))
            if match.group(7):
                pathinside = match.group(7)
            else:
                pathinside = ""

        #If the path is already mounted No need to retry
        if self.checkifmounted(source, mountbase):
            return (remoteSource, mountpoint, pathinside)

        cmd = "sshfs " + user + "@" + match.group(4) + ":/"
        cmd = cmd + " " + mountpoint

        port = match.group(6)
        if port:
            cmd += " -p " + port
        if not local_file_utils.path_exists(mountpoint):
            local_file_utils.makedir(mountpoint)

        if system.is_superuser():
            cmd += " -o allow_root"

        self.logger.debug("Spawning: " + cmd)
        password = match.group(3)
        sshfsp = pexpect.spawn(cmd)
        i = sshfsp.expect(['(yes/no)', 'password:'******'Password:'******'yes')
            i = sshfsp.expect(
                ['(yes/no)', 'password:'******'Password:'******'(yes/no)', 'password:'******'Password:'******'%(command)s' didn't perform normally. Output => %(erroroutput)s "
                  ) % {
                      "command": cmd,
                      "erroroutput": result
                  })

        return (remoteSource, mountpoint, pathinside)
Ejemplo n.º 4
0
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#

from gettext import gettext as _
import re

from sbackup.fuse_plugins import pluginFAM
from sbackup.util.exceptions import FuseFAMException, SBException
from sbackup.util import local_file_utils
from sbackup.util import system

try:
    import pexpect
except ImportError:
    raise FuseFAMException("Unable to import required module `pexpect`")

ssh_re = "^ssh://"
ssh_url_re = ssh_re + "([^:]+?)(:([^@]+?))?@([^/^:^@]+?)(:([0-9]+?))?/(.*)"


class sshFuseFAM(pluginFAM):
    """
    The fuseFAM plugin for ssh
    @requires: sshfs, python-pexpect
    @author: Oumar Aziz Ouattara
    """
    def __init__(self):
        pluginFAM.__init__(self)

    def match_scheme(self, remoteSource):
Ejemplo n.º 5
0
    def mount(self, source, mountbase):
        """Mount the source into the mountbase dir . This method should
        create a mount point to mount the source. The name of the mount
        point should be very expressive so that we avoid collision with
        other mount points
        
        @param source: The remote path
        @param mountbase: The mount points base dir
        @return: The mount point complete path
        """
        #make the mount point
        spliturl = SplittedURL(source)
        mountpoint = self.__get_mount_dir(mountbase, spliturl)

        if not os.path.exists(mountpoint):
            os.mkdir(mountpoint)

        #If the path is already mounted No need to retry
        if not self.checkifmounted(source, mountbase):
            # Create output log file
            outptr, outFile = mkstemp(prefix="sftpFuseFAMmount_output_")
            # Create error log file
            errptr, errFile = mkstemp(prefix="sftpFuseFAMmount_error_")

            # the option 'allow_root' is necessary to grant access
            # if the script is invoked as superuser
            curl_cmd = ["curlftpfs", "-o", "direct_io"]

            if spliturl.user and spliturl.password:
                curl_cmd.append("-o")
                opts = "user=%s:%s" % (spliturl.user, spliturl.password)
                curl_cmd.append(opts)
            if os.getuid() == 0:
                curl_cmd.append("-o")
                curl_cmd.append("allow_root")

            server = spliturl.server
            if spliturl.port:
                server += ":" + spliturl.port

            curl_cmd.append(server)

            curl_cmd.append(mountpoint)

            # Call the subprocess using convenience method
            try:
                retval = subprocess.call(curl_cmd, 0, None, None, outptr,
                                         errptr)
            except OSError, _exc:
                os.rmdir(mountpoint)
                raise FuseFAMException(
                    _("Couldn't found external application 'curlftpfs' needed for handling of sftp sites: %s"
                      ) % _exc)

            # Close log handles
            os.close(errptr)
            os.close(outptr)
            outStr, errStr = local_file_utils.readfile(
                outFile), local_file_utils.readfile(errFile)
            local_file_utils.delete(outFile)
            local_file_utils.delete(errFile)
            if retval != 0:
                os.rmdir(mountpoint)
                raise FuseFAMException(
                    _("Couldn't mount '%(server)s' into '%(mountpoint)s' : %(error)s"
                      ) % {
                          'server': spliturl.server,
                          'mountpoint': mountpoint,
                          'error': errStr
                      })