最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Fabric 2中如何实现Fabric 1中的rsync_project去实现同步代码以实现项目代码部署

代码 crifan 1306浏览 0评论
折腾:
【未解决】使用Fabric把代码从Mac本地部署到CentOS服务器
已经基本上尝试了Fabric 2的一些命令和效果了。
接下来需要去实现项目的代码同步,
最好是参考之前Fabric1的代码:
def rsync():
    rsync_project(
            remote_dir="/root/xxx_20180101/",
            local_dir="../processData",
            exclude=(".*", "*.pyc", 'logs', '*.log', 'tmp', 'cach', 'model', 'models'),
            delete=('processData/')
去实现类似效果:同步整个项目的目录中的代码
且可以排除一些文件夹,特定后缀的文件等
且同步后还可以删除某些文件夹
Fabric 2 rsync_project
Project Tools — Fabric documentation
结果发现是1.14版本,不是最新的2.x的版本
项目工具 — Fabric 文档
Project Tools — Fabric 0.9 documentation
还是0.9版本的呢
how rsync_project to run on the remote · Issue #1503 · fabric/fabric
算了,竟然没有找到fabric2中如何实现fabric1的rsync_project
fabric 2 rsync project
`transfers.rsync` ignores ssh agent and always asks for key password · Issue #16 · fabric/patchwork
好像是利用:
from patchwork.transfers import rsync
去调用rsync?
Separation of local vs remote lowercase-C contexts (for cd, prefix, run/local, etc) · Issue #1752 · fabric/fabric
fabric.contrib.project.rsync_project Example
不过也从:
https://fabric-chs.readthedocs.io/zh_CN/chs/api/contrib/project.html
看到解释:“rsync_project() 只是一个简单的 rsync 封装,关于 rsync 是如何工作的,请阅读它自身的使用手册。为了保证工作正常,你需要保证本地和远程系统中都已安装 rsync。
这个函数会调用 Fabric local() 操作,并将其操作的输出返回回来;也就是说会返回 stdout,如果有的话,还会包含因而产生的 rsync 调用结果。”
所以感觉fabric2中应该就是直接去调用rsync,理论上就可以实现之前的fabric1中的rsync_project
继续看:
https://fabric-chs.readthedocs.io/zh_CN/chs/api/contrib/project.html
前面的代码中的内容,好像分别都只是调用rsync的参数的感觉:
  • remote_dir:是唯一必选的参数,指的需要同步的远程服务器目录。根据 rsync实现方式其具体行为取决于 local_dir 的值。
  • local_dir:默认情况下, rsync_project 使用当前工作目录作为源目录,你可以使用字符串参数 local_dir 覆盖该设置。该参数会原封不动地传递给 rsync,因此它的值可以是单个目录(”my_directory”),或者多个目录(”dir1 dir2″)。详细用法请参阅 rsync 的文档。
  • exclude:可选,可以是一个字符串,也可以是一个字符串的迭代器,用于向 rsync 传递一个或多个 –exclude 参数。
  • delete:用于设置 rsync 的 –delete 选项的参数。其值为 True 将会在远程删除本地已不存在的文件。默认值为 False。
另外,看起来是:
和rsync_project类似的功能的还有个:upload_project
两者区别是:
upload_project() 使用 scp 来复制整个项目,rsync_project() 会使用 rsync 命令,只会将本地比远程更新的文件同步过去。
-》换言之:
rsync_project:也是把 本地的(更新的,改动后)的代码同步到远程服务器中
此处先去本地看看rsync的help:
➜  Downloads rsync --help
rsync  version 2.6.9  protocol version 29
Copyright (C) 1996-2006 by Andrew Tridgell, Wayne Davison, and others.
<
http://rsync.samba.org/
>
Capabilities: 64-bit files, socketpairs, hard links, symlinks, batchfiles,
              inplace, IPv6, 64-bit system inums, 64-bit internal inums

rsync comes with ABSOLUTELY NO WARRANTY.  This is free software, and you
are welcome to redistribute it under certain conditions.  See the GNU
General Public Licence for details.

rsync is a file transfer program capable of efficient remote update
via a fast differencing algorithm.

Usage: rsync [OPTION]... SRC [SRC]... DEST
  or   rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST
  or   rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST
  or   rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST
  or   rsync [OPTION]... [USER@]HOST:SRC [DEST]
  or   rsync [OPTION]... [USER@]HOST::SRC [DEST]
  or   rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]
The ':' usages connect via remote shell, while '::' & 'rsync://' usages connect
to an rsync daemon, and require SRC or DEST to start with a module name.

Options
 -v, --verbose               increase verbosity
 -q, --quiet                 suppress non-error messages
     --no-motd               suppress daemon-mode MOTD (see manpage caveat)
 -c, --checksum              skip based on checksum, not mod-time & size
 -a, --archive               archive mode; same as -rlptgoD (no -H)
     --no-OPTION             turn off an implied OPTION (e.g. --no-D)
 -r, --recursive             recurse into directories
 -R, --relative              use relative path names
     --no-implied-dirs       don't send implied dirs with --relative
 -b, --backup                make backups (see --suffix & --backup-dir)
     --backup-dir=DIR        make backups into hierarchy based in DIR
     --suffix=SUFFIX         set backup suffix (default ~ w/o --backup-dir)
 -u, --update                skip files that are newer on the receiver
     --inplace               update destination files in-place (SEE MAN PAGE)
     --append                append data onto shorter files
 -d, --dirs                  transfer directories without recursing
 -l, --links                 copy symlinks as symlinks
 -L, --copy-links            transform symlink into referent file/dir
     --copy-unsafe-links     only "unsafe" symlinks are transformed
     --safe-links            ignore symlinks that point outside the source tree
 -k, --copy-dirlinks         transform symlink to a dir into referent dir
 -K, --keep-dirlinks         treat symlinked dir on receiver as dir
 -H, --hard-links            preserve hard links
 -p, --perms                 preserve permissions
     --executability         preserve the file's executability
     --chmod=CHMOD           affect file and/or directory permissions
 -o, --owner                 preserve owner (super-user only)
 -g, --group                 preserve group
     --devices               preserve device files (super-user only)
     --specials              preserve special files
 -D                          same as --devices --specials
 -t, --times                 preserve times
 -O, --omit-dir-times        omit directories when preserving times
     --super                 receiver attempts super-user activities
 -S, --sparse                handle sparse files efficiently
 -n, --dry-run               show what would have been transferred
 -W, --whole-file            copy files whole (without rsync algorithm)
 -x, --one-file-system       don't cross filesystem boundaries
 -B, --block-size=SIZE       force a fixed checksum block-size
 -e, --rsh=COMMAND           specify the remote shell to use
     --rsync-path=PROGRAM    specify the rsync to run on the remote machine
     --existing              skip creating new files on receiver
     --ignore-existing       skip updating files that already exist on receiver
     --remove-source-files   sender removes synchronized files (non-dirs)
     --del                   an alias for --delete-during
     --delete                delete extraneous files from destination dirs
     --delete-before         receiver deletes before transfer (default)
     --delete-during         receiver deletes during transfer, not before
     --delete-after          receiver deletes after transfer, not before
     --delete-excluded       also delete excluded files from destination dirs
     --ignore-errors         delete even if there are I/O errors
     --force                 force deletion of directories even if not empty
     --max-delete=NUM        don't delete more than NUM files
     --max-size=SIZE         don't transfer any file larger than SIZE
     --min-size=SIZE         don't transfer any file smaller than SIZE
     --partial               keep partially transferred files
     --partial-dir=DIR       put a partially transferred file into DIR
     --delay-updates         put all updated files into place at transfer's end
 -m, --prune-empty-dirs      prune empty directory chains from the file-list
     --numeric-ids           don't map uid/gid values by user/group name
     --timeout=TIME          set I/O timeout in seconds
 -I, --ignore-times          don't skip files that match in size and mod-time
     --size-only             skip files that match in size
     --modify-window=NUM     compare mod-times with reduced accuracy
 -T, --temp-dir=DIR          create temporary files in directory DIR
 -y, --fuzzy                 find similar file for basis if no dest file
     --compare-dest=DIR      also compare destination files relative to DIR
     --copy-dest=DIR         ... and include copies of unchanged files
     --link-dest=DIR         hardlink to files in DIR when unchanged
 -z, --compress              compress file data during the transfer
     --compress-level=NUM    explicitly set compression level
 -C, --cvs-exclude           auto-ignore files the same way CVS does
 -f, --filter=RULE           add a file-filtering RULE
 -F                          same as --filter='dir-merge /.rsync-filter'
                             repeated: --filter='- .rsync-filter'
     --exclude=PATTERN       exclude files matching PATTERN
     --exclude-from=FILE     read exclude patterns from FILE
     --include=PATTERN       don't exclude files matching PATTERN
     --include-from=FILE     read include patterns from FILE
     --files-from=FILE       read list of source-file names from FILE
 -0, --from0                 all *-from/filter files are delimited by 0s
     --address=ADDRESS       bind address for outgoing socket to daemon
     --port=PORT             specify double-colon alternate port number
     --sockopts=OPTIONS      specify custom TCP options
     --blocking-io           use blocking I/O for the remote shell
     --stats                 give some file-transfer stats
 -8, --8-bit-output          leave high-bit chars unescaped in output
 -h, --human-readable        output numbers in a human-readable format
     --progress              show progress during transfer
 -P                          same as --partial --progress
 -i, --itemize-changes       output a change-summary for all updates
     --out-format=FORMAT     output updates using the specified FORMAT
     --log-file=FILE         log what we're doing to the specified FILE
     --log-file-format=FMT   log updates using the specified FMT
     --password-file=FILE    read password from FILE
     --list-only             list the files instead of copying them
     --bwlimit=KBPS          limit I/O bandwidth; KBytes per second
     --write-batch=FILE      write a batched update to FILE
     --only-write-batch=FILE like --write-batch but w/o updating destination
     --read-batch=FILE       read a batched update from FILE
     --protocol=NUM          force an older protocol version to be used
 -E, --extended-attributes   copy extended attributes
     --cache                 disable fcntl(F_NOCACHE)
 -4, --ipv4                  prefer IPv4
 -6, --ipv6                  prefer IPv6
     --version               print version number
(-h) --help                  show this help (-h works with no other options)

Use "rsync --daemon --help" to see the daemon-mode command-line options.
Please see the rsync(1) and rsyncd.conf(5) man pages for full documentation.
See 
http://rsync.samba.org/
 for updates, bug reports, and answers
【已解决】rsync中delete参数的含义和调用方式
看到:
http://docs.fabfile.org/en/2.3/api/connection.html#fabric.connection.Connection.put
put(*args, **kwargs)
Put a remote file (or file-like object) to the remote filesystem.
Simply a wrapper for Transfer.put. Please see its documentation for all details.
New in version 2.0.
感觉说的不太对啊,自己的代码:
putFileResult = remoteConn.put('fabfile.py', remote=RemotePathRoot)
是把 local的file,上传到remote中的
而不是把remote的file,上传到remote中的
对于echo,用:
remoteConn.run('pwd', echo=True)
输出pwd:
现在感觉还有几个事情要搞清楚:
  • 之前的lcd等,现在如何调用本地命令
  • cd后,后续的ls等命令,没有基于新目录操作
  • 如何在fabric2中用rsync
【基本解决】Fabric 2中如何执行和调用本地和远端的命令
【已解决】fabric 2中如何在远程服务器中基于cd切换路径后再执行命令
然后再去搞清楚,如何去Fabric 2中用rsync
fabric 2 using rsync
python – Write a fabric file for rsync with a ssh key – Stack Overflow
how rsync_project to run on the remote · Issue #1503 · fabric/fabric
`transfers.rsync` ignores ssh agent and always asks for key password · Issue #16 · fabric/patchwork
去看看本地和在线的SSH_AUTH_SOCK
➜  Downloads echo $SSH_AUTH_SOCK
/private/tmp/com.apple.launchd.ZKOljEgscB/Listeners
➜  Downloads rsync --version
rsync  version 2.6.9  protocol version 29
Copyright (C) 1996-2006 by Andrew Tridgell, Wayne Davison, and others.
<
http://rsync.samba.org/
>
Capabilities: 64-bit files, socketpairs, hard links, symlinks, batchfiles,
              inplace, IPv6, 64-bit system inums, 64-bit internal inums

rsync comes with ABSOLUTELY NO WARRANTY.  This is free software, and you
are welcome to redistribute it under certain conditions.  See the GNU
General Public Licence for details.
在线:
[root@xxx-general-01 ~]# echo $SSH_AUTH_SOCK

[root@xxx-general-01 ~]# rsync --version
rsync  version 3.0.9  protocol version 30
Copyright (C) 1996-2011 by Andrew Tridgell, Wayne Davison, and others.
Web site: 
http://rsync.samba.org/
Capabilities:
    64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints,
    socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace,
    append, ACLs, xattrs, iconv, symtimes

rsync comes with ABSOLUTELY NO WARRANTY.  This is free software, and you
are welcome to redistribute it under certain conditions.  See the GNU
General Public Licence for details.
transfers — Patchwork documentation
fabric call rsync
python – How do I copy a directory to a remote machine using Fabric? – Stack Overflow
都找不到 最新版Fabric 2中如何用rsync
http://www.fabfile.org/upgrading.html#file-transfer
好像有关于文件传输的去看看put和rsync
fabric.connection.Connection.get
fabric.connection.Connection.put
最后是通过patchwork实现的调用rsync去同步文件的。
详见:
【已解决】用patchwork的rsync实现同步本地代码部署到远端服务器
话说,Fabric 2中,由于rsync功能改为patchwork后,却少了之前很好用的:
用 Fabric 来发布代码 – 李林克斯
    with lcd(local_app_dir), cd(remote_app_dir):
        # 1. backup
        run("rm -rf SomeProj.bak")          # run 是在远程服务器上执行命令
        run("mv SomeProj SomeProj.bak")
中的with功能:
在切换到(本地或远程的)对应的目录后,进行后续操作。
而此处patchwork中的rsync就没法支持这么方便的操作。

转载请注明:在路上 » 【已解决】Fabric 2中如何实现Fabric 1中的rsync_project去实现同步代码以实现项目代码部署

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
81 queries in 0.198 seconds, using 22.20MB memory