折腾:
【已解决】Mac中安装并试用Fabric
期间,去运行fabric试试,结果出错:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | ➜ ~ vi fabfile.py ➜ ~ ll total 736 drwxr-xr-x 2 crifan staff 64B 1 26 2018 AnacondaProjects drwx------ 10 crifan staff 320B 5 25 14:24 Applications drwx------@ 4 crifan staff 128B 4 26 17:36 Applications (Parallels) drwx------@ 10 crifan staff 320B 5 25 14:24 Desktop drwx------@ 33 crifan staff 1.0K 8 16 10:10 Documents drwx------+ 88 crifan staff 2.8K 8 16 10:05 Downloads drwxr-xr-x 3 crifan staff 96B 11 12 2017 GitBook drwxr-xr-x 5 crifan staff 160B 6 8 2017 HBuilder drwxr-xr-x 4 crifan staff 128B 8 13 2016 HBuilderProjects drwx------@ 100 crifan staff 3.1K 6 19 14:07 Library drwx------+ 12 crifan staff 384B 3 28 10:24 Movies drwx------+ 10 crifan staff 320B 12 23 2017 Music drwx------+ 287 crifan staff 9.0K 4 6 21:51 Pictures drwxr-xr-x+ 5 crifan staff 160B 9 18 2015 Public drwxr-xr-x+ 3 crifan staff 96B 8 23 2017 Sites drwxr-xr-x 23 crifan staff 736B 1 27 2018 anaconda2 drwxr-xr-x 4 crifan staff 128B 7 26 2016 bin drwxr-xr-x 9 crifan staff 288B 9 11 2017 dev -rw-r--r-- 1 crifan staff 1.7K 5 18 17:39 dump.rdb -rw-r--r-- 1 crifan staff 39B 8 16 10:57 fabfile.py -rw-r--r-- 1 crifan staff 334K 9 18 2015 ishot-1.png -rw-r--r-- 1 crifan staff 19K 1 16 2018 jmeter.log -rw-r--r--@ 1 crifan staff 1.7K 2 7 2017 runningfast_aws_production.pem drwxr-xr-x 2 crifan staff 64B 9 26 2017 tmp drwxr-xr-x 3 crifan staff 96B 11 14 2015 微云同步盘 ➜ ~ fab hello No idea what 'hello' is! ➜ ~ cat fabfile.py def hello(): print( "Hello world!" ) ➜ ~ fab hello No idea what 'hello' is! |
所以搜:
fabric No idea what ‘hello’ is!
1 2 | ➜ ~ fab --list No tasks found in collection 'fabfile' ! |
通过之前:
【已解决】Mac中安装并试用Fabric
的:
1 2 3 4 5 6 | ➜ ~ which fab /Users/crifan/Library/Python/2 .7 /bin/fab ➜ ~ fab --version Fabric 2.3.1 Paramiko 2.4.1 Invoke 1.1.1 |
知道了此处的fabric版本是2.3.1,
而上面这些教程看起来都是1.x的版本的老教程?
估计和2.x的版本不兼容
去找新版教程
回到命令行试试制定fabfile的文件名:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | ➜ ~ fab - - help Usage: fab [ - - core - opts] task1 [ - - task1 - opts] ... taskN [ - - taskN - opts] Core options: - - complete Print tab - completion candidates for given parse remainder. - - hide = STRING Set default value of run() 's ' hide' kwarg. - - no - dedupe Disable task deduplication. - - prompt - for - login - password Request an upfront SSH - auth password prompt. - - prompt - for - passphrase Request an upfront SSH key passphrase prompt. - - prompt - for - sudo - password Prompt user at start of session for the sudo.password config value. - - write - pyc Enable creation of .pyc files. - c STRING, - - collection = STRING Specify collection name to load. - d, - - debug Enable debug output. - D INT , - - list - depth = INT When listing tasks, only show the first INT levels. - e, - - echo Echo executed commands before running. - f STRING, - - config = STRING Runtime configuration file to use. - F STRING, - - list - format = STRING Change the display format used when listing tasks. Should be one of: flat (default), nested, json. - h [STRING], - - help [ = STRING] Show core or per - task help and exit. - H STRING, - - hosts = STRING Comma - separated host name(s) to execute tasks against. - i, - - identity Path to runtime SSH identity (key) file . May be given multiple times. - l [STRING], - - list [ = STRING] List available tasks, optionally limited to a namespace. - p, - - pty Use a pty when executing shell commands. - r STRING, - - search - root = STRING Change root directory used for finding task modules. - S STRING, - - ssh - config = STRING Path to runtime SSH config file . - V, - - version Show version and exit. - w, - - warn - only Warn, instead of failing, when shell commands fail. ➜ ~ fab - f fabfile.py hello No idea what 'hello' is ! ➜ ~ fab - - config = fabfile.py hello No idea what 'hello' is ! |
问题依旧。
参考最新官网去运行,也出错:
【已解决】fabric的Connection出错:paramiko.ssh_exception.AuthenticationException: Authentication failed
然后就可以继续操作了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | ➜ ~ python Python 2.7 . 10 (default, Oct 6 2017 , 22 : 29 : 07 ) [GCC 4.2 . 1 Compatible Apple LLVM 9.0 . 0 (clang - 900.0 . 31 )] on darwin Type "help" , "copyright" , "credits" or "license" for more information. >>> from fabric import Connection >>> c = Connection(host = '47.x.x.x' , user = 'username' ) >>> result = c.run( 'uname -s' ) Linux >>> result.stdout.strip() = = 'Linux' True >>> result.exited 0 >>> result.ok True >>> result.command 'uname -s' >>> result.connection <Connection host = 47.x .x.x user = username > >>> result.connection.host '47.x.x.x' >>> |
另外也看到了:
->
中提到了:
Fabric 2+版本和Fabric 1相比:
相当于完全重新实现了
很多(之前组织的不够好的)功能,被废弃或者重新组织了
“* Command-line parser overhauled to allow for regular GNU/POSIX style flags and options on a per-task basis (no more fab mytask:weird=custom,arg=format);”
-》导致了此处的:
Fabric 1中的
fab hello
现在也不支持了。
而想要在Fabrci 2+的新版本中实现类似于Fabric 1的hello world的例子,则需要再去研究看看
参考:
而
中也提到了:
对于(此处hello world这种)和远程remote服务器没关系的,往往都是调用invoke实现的
而Invoke本身主要就是处理:命令行中的参数导入,任务管理,shell命令执行等事情的
-》所以此处的Fabric 1的hello world,参考:
可以改为:
1 2 3 4 5 6 7 8 9 10 11 | from invoke import task @task def hello(c): print ( "Hello fabric!" ) c.run( "echo '--- pip show fabric ---'" ) c.run( "pip show fabric" ) # c.run("echo '--- fab --verion ---'") # c.run("fab --verion") # c.run("fab -V") # c.run("/Users/crifan/Library/Python/2.7/bin/fab --verion") |
结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | ➜ ~ cat fabfile.py from invoke import task @task def hello(c): print ( "Hello fabric!" ) c.run( "echo '--- pip show fabric ---'" ) c.run( "pip show fabric" ) # c.run("echo '--- fab --verion ---'") # c.run("fab --verion") # c.run("fab -V") # c.run("/Users/crifan/Library/Python/2.7/bin/fab --verion")% ➜ ~ fab hello Hello fabric! - - - pip show fabric - - - Name: fabric Version: 2.3 . 1 Summary: High level SSH command execution Home - page: http: / / fabfile.org Author: Jeff Forcier Author - email: jeff@bitprophet.org License: BSD Location: / Users / crifan / Library / Python / 2.7 / lib / python / site - packages Requires: cryptography, paramiko, invoke Required - by: You are using pip version 10.0 . 1 , however version 18.0 is available. You should consider upgrading via the 'pip install --upgrade pip' command. |
【总结】
Fabric 1是老版本
新的Fabric 2+的版本,属于彻底重写了,语法和功能有很大变化
-》所以之前的Fabric 1的hello world例子失效了,要改为:
1 2 3 4 5 6 7 | from invoke import task @task def hello(c): print ( "Hello fabric!" ) c.run( "echo '--- pip show fabric ---'" ) c.run( "pip show fabric" ) |
即可。