Beispiel #1
0
def symlink(src, dst):
    if dst.endswith('/'):
        raise NotImplementedError('Creating link inside directory not '
                                  'implemented')
    lst = remote.lstat(dst)

    if lst:
        if not S_ISLNK(lst.st_mode):
            raise RemotePathIsNotALinkError('Already exists and not a link: '
                                            '{}'.format(dst))

        # remote is a link
        rsrc = remote.readlink(dst)
        if rsrc == src:
            return Unchanged(msg='Unchanged link: {} -> {}'.format(dst, src))

        # we need to update the link, unfortunately, this is often not possible
        # atomically
        remote.unlink(dst)
        remote.symlink(src, dst)
        return Changed(msg='Changed link: {} -> {} (previously -> {})'.format(
            dst, src, rsrc))

    remote.symlink(src, dst)
    return Changed(msg='Created link: {} -> {}'.format(dst, src))
Beispiel #2
0
def symlink(src, dst):
    if dst.endswith('/'):
        raise NotImplementedError('Creating link inside directory not '
                                  'implemented')
    lst = remote.lstat(dst)

    if lst:
        if not S_ISLNK(lst.st_mode):
            raise RemotePathIsNotALinkError('Already exists and not a link: '
                                            '{}'.format(dst))

        # remote is a link
        rsrc = remote.readlink(dst)
        if rsrc == src:
            return Unchanged(msg='Unchanged link: {} -> {}'.format(dst, src))

        # we need to update the link, unfortunately, this is often not possible
        # atomically
        remote.unlink(dst)
        remote.symlink(src, dst)
        return Changed(msg='Changed link: {} -> {} (previously -> {})'
                       .format(dst, src, rsrc))

    remote.symlink(src, dst)
    return Changed(msg='Created link: {} -> {}'.format(dst, src))
Beispiel #3
0
def run():
    log.warning('Running testing module. Do not run this on a real machine!')

    log.debug('Testing popen')
    proc = remote.popen(['uname'])
    stdout, stderr = proc.communicate()
    assert 'Linux' == stdout.strip()

    log.debug('Testing getcwd()')
    assert '/home/vagrant' == remote.getcwd()

    log.debug('Testing chdir()')
    remote.chdir('/')
    assert '/' == remote.getcwd()
    remote.chdir('/home/vagrant')

    # create a sample file
    TESTFN = 'testfile'
    TESTDN = 'TESTDIR'
    log.debug('Testing file')
    with remote.file(TESTFN, mode='w') as out:
        out.write('test')

    log.debug('Testing chmod')
    remote.chmod(TESTFN, 0732)

    log.debug('Testing mkdir')
    # FIXME: umask?
    # FIXME: on exists/conflict?
    remote.mkdir(TESTDN, 0700)

    log.debug('Testing listdir')
    assert TESTFN in remote.listdir('.')
    assert TESTDN in remote.listdir('.')

    log.debug('Testing rmdir')
    remote.rmdir(TESTDN)

    # FIXME: can't test chown without root access

    log.debug('Testing normalize')
    assert '/home' == remote.normalize('./..')

    log.debug('Testing symlink')
    remote.symlink('to', 'from')

    log.debug('Testing lstat')
    remote.lstat('from')

    log.debug('Testing readlink')
    assert remote.readlink('/home/vagrant/from') == 'to'

    log.debug('Testing rename')
    remote.rename('from', 'from2')
    assert remote.readlink('/home/vagrant/from2') == 'to'

    log.debug('Testing unlink')
    remote.unlink('/home/vagrant/from2')

    log.debug('Testing stat')
    s = remote.stat(TESTFN)
    assert s.st_uid == 1000
    assert s.st_gid == 1000
    remote.unlink(TESTFN)
Beispiel #4
0
def upload_file(local_path,
                remote_path=None,
                follow_symlink=True,
                create_parent=False):
    """Uploads a local file to a remote and if does not exist or differs
    from the local version, uploads it.

    To avoid having to transfer the file one or more times if unchanged,
    different methods for verification are available. These can be configured
    using the ``fs_remote_file_verify`` configuration variable.

    :param local_path: Local file to upload. If it is a symbolic link, it will
                       be resolved first.
    :param remote_path: Remote name for the file. If ``None``, same as
                        ``local_path``. If it points to a directory, the file
                        will be uploaded to the directory. Symbolic links not
                        pointing to a directory are an error.

    :param return: ``False`` if no upload was necessary, ``True`` otherwise.
    """
    st, remote_path = _expand_remote_dest(local_path, remote_path)
    lst = os.stat(local_path) if follow_symlink else os.lstat(local_path)

    verifier = Verifier._by_short_name(config['fs_remote_file_verify'])()
    uploader = Uploader._by_short_name(config['fs_remote_file_upload'])()

    if lst is None:
        raise ConfigurationError(
            'Local file {!r} does not exist'.format(local_path))

    if S_ISLNK(lst.st_mode):
        # local file is a link
        rst = remote.lstat(remote_path)

        if rst:
            if not S_ISLNK(rst.st_mode):
                # remote file is not a link, unlink it
                remote.unlink(remote_path)
            elif remote.readlink(remote_path) != os.readlink(local_path):
                # non matching links
                remote.unlink(remote_path)
            else:
                # links pointing to the same target
                return Unchanged(
                    msg='Symbolink link up-to-date: {}'.format(remote_path))

        remote.symlink(os.readlink(local_path), remote_path)
        return Changed(msg='Created remote link: {}'.format(remote_path))

    if not st or not verifier.verify_file(st, local_path, remote_path):
        if create_parent:
            create_dir(remote.path.dirname(remote_path))

        uploader.upload_file(local_path, remote_path)

        if config.get_bool('fs_update_mtime'):
            times = (lst.st_mtime, lst.st_mtime)
            remote.utime(remote_path, times)
            log.debug('Updated atime/mtime: {}'.format(times))
        return Changed(msg='Upload {} -> {}'.format(local_path, remote_path))

    return Unchanged(msg='File up-to-date: {}'.format(remote_path))
Beispiel #5
0
def upload_file(local_path,
                remote_path=None,
                follow_symlink=True,
                create_parent=False):
    """Uploads a local file to a remote and if does not exist or differs
    from the local version, uploads it.

    To avoid having to transfer the file one or more times if unchanged,
    different methods for verification are available. These can be configured
    using the ``fs_remote_file_verify`` configuration variable.

    :param local_path: Local file to upload. If it is a symbolic link, it will
                       be resolved first.
    :param remote_path: Remote name for the file. If ``None``, same as
                        ``local_path``. If it points to a directory, the file
                        will be uploaded to the directory. Symbolic links not
                        pointing to a directory are an error.

    :param return: ``False`` if no upload was necessary, ``True`` otherwise.
    """
    st, remote_path = _expand_remote_dest(local_path, remote_path)
    lst = os.stat(local_path) if follow_symlink else os.lstat(local_path)

    verifier = Verifier._by_short_name(config['fs_remote_file_verify'])()
    uploader = Uploader._by_short_name(config['fs_remote_file_upload'])()

    if lst is None:
        raise ConfigurationError('Local file {!r} does not exist'.format(
            local_path))

    if S_ISLNK(lst.st_mode):
        # local file is a link
        rst = remote.lstat(remote_path)

        if rst:
            if not S_ISLNK(rst.st_mode):
                # remote file is not a link, unlink it
                remote.unlink(remote_path)
            elif remote.readlink(remote_path) != os.readlink(local_path):
                # non matching links
                remote.unlink(remote_path)
            else:
                # links pointing to the same target
                return Unchanged(
                    msg='Symbolink link up-to-date: {}'.format(remote_path))

        remote.symlink(os.readlink(local_path), remote_path)
        return Changed(msg='Created remote link: {}'.format(remote_path))

    if not st or not verifier.verify_file(st, local_path, remote_path):
        if create_parent:
            create_dir(remote.path.dirname(remote_path))

        uploader.upload_file(local_path, remote_path)

        if config.get_bool('fs_update_mtime'):
            times = (lst.st_mtime, lst.st_mtime)
            remote.utime(remote_path, times)
            log.debug('Updated atime/mtime: {}'.format(times))
        return Changed(msg='Upload {} -> {}'.format(local_path, remote_path))

    return Unchanged(msg='File up-to-date: {}'.format(remote_path))