折腾:
【未解决】使用Fabric把代码从Mac本地部署到CentOS服务器
期间,需要先去Mac本地安装Fabric去试试。
mac 安装 fabric
都太复杂
在服务器上安装fabric远程部署项目
pip去安装
➜ ~ which pip pip: aliased to noglob pip ➜ ~ pip --version pip 10.0.1 from /Users/crifan/Library/Python/2.7/lib/python/site-packages/pip (python 2.7) ➜ ~ pip install fabric Collecting fabric Downloading https://files.pythonhosted.org/packages/83/a1/87f8cdaf1827fa8bdcbdc34ff9b8c3a094e582ead729e687a4b49977917e/fabric-2.3.1-py2.py3-none-any.whl (48kB) 100% |████████████████████████████████| 51kB 109kB/s Collecting cryptography>=1.1 (from fabric) Downloading https://files.pythonhosted.org/packages/5d/b1/9863611b121ee524135bc0068533e6d238cc837337170e722224fe940e2d/cryptography-2.3.1-cp27-cp27m-macosx_10_6_intel.whl (1.5MB) 100% |████████████████████████████████| 1.5MB 14kB/s Collecting paramiko>=2.4 (from fabric) Downloading https://files.pythonhosted.org/packages/3e/db/cb7b6656e0e7387637ce850689084dc0b94b44df31cc52e5fc5c2c4fd2c1/paramiko-2.4.1-py2.py3-none-any.whl (194kB) 100% |████████████████████████████████| 194kB 27kB/s Collecting invoke<2.0,>=1.1 (from fabric) Downloading https://files.pythonhosted.org/packages/d8/ae/a9fbb8544bfe2617d175a2fc37400da849240fdb5691bcbd36d7171389da/invoke-1.1.1-py2-none-any.whl (159kB) 100% |████████████████████████████████| 163kB 11kB/s Requirement already satisfied: six>=1.4.1 in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from cryptography>=1.1->fabric) (1.4.1) Collecting cffi!=1.11.3,>=1.7 (from cryptography>=1.1->fabric) Downloading https://files.pythonhosted.org/packages/7e/4a/b647e46faaa2dcfb16069b6aad2d8509982fd63710a325b8ad7db80f18be/cffi-1.11.5-cp27-cp27m-macosx_10_6_intel.whl (238kB) 100% |████████████████████████████████| 245kB 20kB/s Requirement already satisfied: enum34; python_version < "3" in ./Library/Python/2.7/lib/python/site-packages (from cryptography>=1.1->fabric) (1.1.6) Collecting idna>=2.1 (from cryptography>=1.1->fabric) Using cached https://files.pythonhosted.org/packages/4b/2a/0276479a4b3caeb8a8c1af2f8e4355746a97fab05a372e4a2c6a6b876165/idna-2.7-py2.py3-none-any.whl Collecting asn1crypto>=0.21.0 (from cryptography>=1.1->fabric) Using cached https://files.pythonhosted.org/packages/ea/cd/35485615f45f30a510576f1a56d1e0a7ad7bd8ab5ed7cdc600ef7cd06222/asn1crypto-0.24.0-py2.py3-none-any.whl Collecting ipaddress; python_version < "3" (from cryptography>=1.1->fabric) Downloading https://files.pythonhosted.org/packages/fc/d0/7fc3a811e011d4b388be48a0e381db8d990042df54aa4ef4599a31d39853/ipaddress-1.0.22-py2.py3-none-any.whl Collecting pyasn1>=0.1.7 (from paramiko>=2.4->fabric) Downloading https://files.pythonhosted.org/packages/d1/a1/7790cc85db38daa874f6a2e6308131b9953feb1367f2ae2d1123bb93a9f5/pyasn1-0.4.4-py2.py3-none-any.whl (72kB) 100% |████████████████████████████████| 81kB 13kB/s Collecting bcrypt>=3.1.3 (from paramiko>=2.4->fabric) Downloading https://files.pythonhosted.org/packages/a1/9c/c89411a505dca5ae822a28c6de6946583ff8a1d5d9190292f301d28dcf85/bcrypt-3.1.4-cp27-cp27m-macosx_10_6_intel.whl (51kB) 100% |████████████████████████████████| 61kB 19kB/s Collecting pynacl>=1.0.1 (from paramiko>=2.4->fabric) Downloading https://files.pythonhosted.org/packages/7b/b2/8588fb7665c8b4f6b46921ead952dcd28ec15ed66d686b4ae69d6f0aed3c/PyNaCl-1.2.1-cp27-cp27m-macosx_10_6_intel.whl (243kB) 100% |████████████████████████████████| 245kB 40kB/s Collecting pycparser (from cffi!=1.11.3,>=1.7->cryptography>=1.1->fabric) pipenv 11.10.0 requires certifi, which is not installed. pipenv 11.10.0 requires requests[security], which is not installed. pipenv 11.10.0 requires virtualenv, which is not installed. matplotlib 1.3.1 requires nose, which is not installed. matplotlib 1.3.1 requires tornado, which is not installed. Installing collected packages: pycparser, cffi, idna, asn1crypto, ipaddress, cryptography, pyasn1, bcrypt, pynacl, paramiko, invoke, fabric Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/pycparser-2.18.dist-info' Consider using the `--user` option or check the permissions. 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. ➜ ~ pip install --upgrade pip Collecting pip Using cached https://files.pythonhosted.org/packages/5f/25/e52d3f31441505a5f3af41213346e5b6c221c9e086a166f3703d2ddaf940/pip-18.0-py2.py3-none-any.whl pipenv 11.10.0 requires certifi, which is not installed. pipenv 11.10.0 requires requests[security], which is not installed. pipenv 11.10.0 requires virtualenv, which is not installed. matplotlib 1.3.1 requires nose, which is not installed. matplotlib 1.3.1 requires tornado, which is not installed. Installing collected packages: pip Found existing installation: pip 10.0.1 Uninstalling pip-10.0.1: Successfully uninstalled pip-10.0.1 Rolling back uninstall of pip Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/pip' Consider using the `--user` option or check the permissions. 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. ➜ ~ pip install fabric --user Collecting fabric Using cached https://files.pythonhosted.org/packages/83/a1/87f8cdaf1827fa8bdcbdc34ff9b8c3a094e582ead729e687a4b49977917e/fabric-2.3.1-py2.py3-none-any.whl Collecting cryptography>=1.1 (from fabric) Using cached https://files.pythonhosted.org/packages/5d/b1/9863611b121ee524135bc0068533e6d238cc837337170e722224fe940e2d/cryptography-2.3.1-cp27-cp27m-macosx_10_6_intel.whl Collecting paramiko>=2.4 (from fabric) Using cached https://files.pythonhosted.org/packages/3e/db/cb7b6656e0e7387637ce850689084dc0b94b44df31cc52e5fc5c2c4fd2c1/paramiko-2.4.1-py2.py3-none-any.whl Collecting invoke<2.0,>=1.1 (from fabric) Using cached https://files.pythonhosted.org/packages/d8/ae/a9fbb8544bfe2617d175a2fc37400da849240fdb5691bcbd36d7171389da/invoke-1.1.1-py2-none-any.whl Requirement already satisfied: six>=1.4.1 in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from cryptography>=1.1->fabric) (1.4.1) Collecting cffi!=1.11.3,>=1.7 (from cryptography>=1.1->fabric) Using cached https://files.pythonhosted.org/packages/7e/4a/b647e46faaa2dcfb16069b6aad2d8509982fd63710a325b8ad7db80f18be/cffi-1.11.5-cp27-cp27m-macosx_10_6_intel.whl Requirement already satisfied: enum34; python_version < "3" in ./Library/Python/2.7/lib/python/site-packages (from cryptography>=1.1->fabric) (1.1.6) Collecting idna>=2.1 (from cryptography>=1.1->fabric) Using cached https://files.pythonhosted.org/packages/4b/2a/0276479a4b3caeb8a8c1af2f8e4355746a97fab05a372e4a2c6a6b876165/idna-2.7-py2.py3-none-any.whl Collecting asn1crypto>=0.21.0 (from cryptography>=1.1->fabric) Using cached https://files.pythonhosted.org/packages/ea/cd/35485615f45f30a510576f1a56d1e0a7ad7bd8ab5ed7cdc600ef7cd06222/asn1crypto-0.24.0-py2.py3-none-any.whl Collecting ipaddress; python_version < "3" (from cryptography>=1.1->fabric) Using cached https://files.pythonhosted.org/packages/fc/d0/7fc3a811e011d4b388be48a0e381db8d990042df54aa4ef4599a31d39853/ipaddress-1.0.22-py2.py3-none-any.whl Collecting pyasn1>=0.1.7 (from paramiko>=2.4->fabric) Using cached https://files.pythonhosted.org/packages/d1/a1/7790cc85db38daa874f6a2e6308131b9953feb1367f2ae2d1123bb93a9f5/pyasn1-0.4.4-py2.py3-none-any.whl Collecting bcrypt>=3.1.3 (from paramiko>=2.4->fabric) Using cached https://files.pythonhosted.org/packages/a1/9c/c89411a505dca5ae822a28c6de6946583ff8a1d5d9190292f301d28dcf85/bcrypt-3.1.4-cp27-cp27m-macosx_10_6_intel.whl Collecting pynacl>=1.0.1 (from paramiko>=2.4->fabric) Using cached https://files.pythonhosted.org/packages/7b/b2/8588fb7665c8b4f6b46921ead952dcd28ec15ed66d686b4ae69d6f0aed3c/PyNaCl-1.2.1-cp27-cp27m-macosx_10_6_intel.whl Collecting pycparser (from cffi!=1.11.3,>=1.7->cryptography>=1.1->fabric) pipenv 11.10.0 requires certifi, which is not installed. pipenv 11.10.0 requires requests[security], which is not installed. pipenv 11.10.0 requires virtualenv, which is not installed. matplotlib 1.3.1 requires nose, which is not installed. matplotlib 1.3.1 requires tornado, which is not installed. Installing collected packages: pycparser, cffi, idna, asn1crypto, ipaddress, cryptography, pyasn1, bcrypt, pynacl, paramiko, invoke, fabric Successfully installed asn1crypto-0.24.0 bcrypt-3.1.4 cffi-1.11.5 cryptography-2.3.1 fabric-2.3.1 idna-2.7 invoke-1.1.1 ipaddress-1.0.22 paramiko-2.4.1 pyasn1-0.4.4 pycparser-2.18 pynacl-1.2.1 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. ➜ ~ which fab /Users/crifan/Library/Python/2.7/bin/fab ➜ ~ fab --version Fabric 2.3.1 Paramiko 2.4.1 Invoke 1.1.1 ➜ ~
然后去试试效果,结果出错:
【已解决】Mac中运行fab hello出错:No idea what ‘hello’ is!
然后继续试用和研究:
from invoke import task from fabric import Connection @task def hello(c): print("Hello fabric!") c.run("echo '--- pip show fabric ---'") # c.run("pip show fabric") c.run("pwd") c.run("ls -lha") # c.run("echo '--- fab --verion ---'") # c.run("fab --verion") # c.run("fab -V") # c.run("/Users/crifan/Library/Python/2.7/bin/fab --verion") @task def remote(curContext): remoteConn = Connection(host='47.x.x.x', user=' username ') remoteConn.run("pwd") result = remoteConn.run('uname -s') print(result) print("result.exited=%s" % (result.exited)) print(" result.command=%s " % ( result.command )) print("result.connection=%s" % (result.connection)) print("result.connection.host=%s" % (result.connection.host))
输出:
➜ ~ fab remote /root Linux Command exited with status 0. === stdout === Linux (no stderr) result.exited=0 result.command=uname -s result.connection=<Connection host=47.x.x.x user= username > result.connection.host=47.x.x.x
试试capture,结果无法识别
capturedPwdResult = c.run("pwd", capture=True) print("capturedPwdResult=%s", capturedPwdResult)
输出:
Traceback (most recent call last): File "/Users/crifan/Library/Python/2.7/bin/fab", line 11, in <module> sys.exit(program.run()) File "/Users/crifan/Library/Python/2.7/lib/python/site-packages/invoke/program.py", line 335, in run self.execute() File "/Users/crifan/Library/Python/2.7/lib/python/site-packages/invoke/program.py", line 491, in execute executor.execute(*self.tasks) File "/Users/crifan/Library/Python/2.7/lib/python/site-packages/invoke/executor.py", line 129, in execute result = call.task(*args, **call.kwargs) File "/Users/crifan/Library/Python/2.7/lib/python/site-packages/invoke/tasks.py", line 128, in __call__ result = self.body(*args, **kwargs) File "/Users/crifan/fabfile.py", line 18, in hello capturedPwdResult = c.run("pwd", capture=True) File "/Users/crifan/Library/Python/2.7/lib/python/site-packages/invoke/context.py", line 94, in run return self._run(runner, command, **kwargs) File "/Users/crifan/Library/Python/2.7/lib/python/site-packages/invoke/context.py", line 101, in _run return runner.run(command, **kwargs) File "/Users/crifan/Library/Python/2.7/lib/python/site-packages/invoke/runners.py", line 271, in run return self._run_body(command, **kwargs) File "/Users/crifan/Library/Python/2.7/lib/python/site-packages/invoke/runners.py", line 277, in _run_body opts, out_stream, err_stream, in_stream = self._run_opts(kwargs) File "/Users/crifan/Library/Python/2.7/lib/python/site-packages/invoke/runners.py", line 423, in _run_opts raise TypeError(err.format(list(kwargs.keys())[0])) TypeError: run() got an unexpected keyword argument 'capture'
其他测试结果:
from invoke import task from fabric import Connection def seperatorLine(seperatorChar='-', count=80): print(seperatorChar * count) @task def hello(c): print("Hello fabric!") seperatorLine() c.run("echo 'Now will ouput: pip show fabric'") c.run("pip show fabric") seperatorLine() c.run("echo $PATH") seperatorLine() c.run("uname -a") seperatorLine() capturedPwdResult = c.run("pwd", capture=True) print("capturedPwdResult=%s", capturedPwdResult) seperatorLine() # c.run("which fab") # c.run("ls -lha") # c.run("echo '--- fab --verion ---'") # c.run("fab --verion") # c.run("fab -V") # c.run("/Users/crifan/Library/Python/2.7/bin/fab --verion") @task def remote(curContext): print("curContext=%s", curContext) seperatorLine() # remoteConn = Connection(host='47.x.x.x', user='username') remoteConn = Connection(' username @47.x.x.x:22') # remoteConn.run("pwd") capturedPwdResult = remoteConn.run("pwd") print("capturedPwdResult=%s" % capturedPwdResult) seperatorLine() result = remoteConn.run('uname -s') print(result) seperatorLine() print("result.connection=%s" % (result.connection)) print("result.connection.host=%s" % (result.connection.host)) seperatorLine() print("result.stdout=%s" % (result.stdout)) print("result.stderr=%s" % (result.stderr)) print("result.encoding=%s" % (result.encoding)) print(" result.command=%s " % ( result.command )) print("result.shell=%s" % (result.shell)) print("result.env=%s" % (result.env)) print("result.exited=%s" % (result.exited)) print("result.return_code=%s" % (result.return_code)) print("result.pty=%s" % (result.pty)) # print("result.hide=%s" % (result.hide)) print("result.ok=%s" % (result.ok)) print("result.__weakref__=%s" % (result.__weakref__)) print("result.failed=%s" % (result.failed))
输出:
➜ ~ fab remote ('curContext=%s', <Context: <Config: {'tasks': {'search_root': None, 'collection_name': 'fabfile', 'dedupe': True, 'auto_dash_names': True}, 'run': {'shell': '/bin/bash', 'hide': None, 'pty': False, 'encoding': None, 'in_stream': None, 'replace_env': True, 'echo': False, 'warn': False, 'echo_stdin': None, 'watchers': [], 'env': {}, 'out_stream': None, 'err_stream': None, 'fallback': True}, 'timeouts': {'connect': None}, 'sudo': {'password': None, 'prompt': '[sudo] password: ', 'user': None}, 'inline_ssh_env': False, 'port': 22, 'load_ssh_configs': True, 'user': 'crifan', 'ssh_config_path': None, 'connect_kwargs': {}, 'forward_agent': False, 'gateway': None, 'runners': {'remote': <class 'fabric.runners.Remote'>, 'local': <class 'invoke.runners.Local'>}}>>) -------------------------------------------------------------------------------- /root capturedPwdResult=Command exited with status 0. === stdout === /root (no stderr) -------------------------------------------------------------------------------- Linux Command exited with status 0. === stdout === Linux (no stderr) -------------------------------------------------------------------------------- result.connection=<Connection host=47.x.x.x user= username > result.connection.host=47.x.x.x -------------------------------------------------------------------------------- result.stdout=Linux result.stderr= result.encoding=UTF-8 result.command=uname -s result.shell=/bin/bash result.env={} result.exited=0 result.return_code=0 result.pty=False result.ok=True result.__weakref__=None result.failed=False
【已解决】Fabric的三个版本fabric1、fabric2和fabric3的区别
【总结】
详见:
【整理Book】项目代码部署工具:Fabric
转载请注明:在路上 » 【已解决】Mac中安装并试用Fabric