我是 Python 和 Fabric 的新手。我们目前使用 capistrano 并有类似的设置:
/api-b2b
- Capfile (with generic deployment/setup info)
/int - target host config (like ip, access etc.)
/prod - target host config (like ip, access etc.)
/dev - target host config (like ip, access etc.)
/api-b2c
/int
/prod
/dev
/application1
/int
/prod
/dev
/application2
/int
/prod
/dev
Run Code Online (Sandbox Code Playgroud)
我们对 capistrano 处理我们的 Java 应用程序不满意——fabric 看起来是一个更好(更简单)的替代方案。
到目前为止,我看到的所有示例 fabfile 都“相对简单”,因为它们仅处理不同主机的一个应用程序。我想看到一些代码,其中不同的应用程序/主机由相同的结构文件/基础设施(如继承等)处理,以便为 git 处理、目录创建、符号链接等常见任务共享相同的逻辑。我希望你明白我的意思。我希望整个逻辑相同,只是应用程序配置不同(git repo,目标目录)。所有其余的在应用程序中都是相同的(相同的服务器布局......)
我希望能够输入这样的东西
$ cd api-b2b
$ fab env_prod deploy
$ cd api-b2c
$ fab env_prod deploy …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用结构编写一个简单的 python 代码,以使用 get() 函数将文件从一台主机传输到另一台主机,尽管我不断收到错误消息:
MacBook-Pro-3:PythonsScripts$ fab get:'/tmp/test','/tmp/test'
[hostname] Executing task 'get'
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/fabric/main.py", line 743, in main
*args, **kwargs
File "/Library/Python/2.7/site-packages/fabric/tasks.py", line 387, in execute
multiprocessing
File "/Library/Python/2.7/site-packages/fabric/tasks.py", line 277, in _execute
return task.run(*args, **kwargs)
File "/Library/Python/2.7/site-packages/fabric/tasks.py", line 174, in run
return self.wrapped(*args, **kwargs)
File "/Users/e0126914/Desktop/PYTHON/PythonsScripts/fabfile.py", line 128, in get
get('/tmp/test','/tmp/test') ***This line repeats many times then last error below***
RuntimeError: maximum recursion depth exceeded
Run Code Online (Sandbox Code Playgroud)
我目前的代码是:
from fabric.api import *
from getpass import getpass …
Run Code Online (Sandbox Code Playgroud) 我正在使用 python Fabric 中的 run 命令远程执行脚本。
C = fabric.Connection('ip', user='user', connect_kwargs={"password": "password"})
try:
r = C.run('python3 ~/script.py')
if r:
print('{} SUCCESS'.format(C.host))
break
else:
print('{} ERROR'.format(C.host))
break
except:
print('{} ERROR'.format(C.host))
Run Code Online (Sandbox Code Playgroud)
我的 script.py 是:
def download_file(url, filename):
try:
response = requests.get(url)
# Open file and write the content
with open(filename, 'wb') as file:
# A chunk of 128 bytes
for chunk in response:
file.write(chunk)
return 1
except requests.exceptions.RequestException as e:
return 0
download_file(url,filename)
Run Code Online (Sandbox Code Playgroud)
当我执行 run 命令时,有没有办法查看我的函数中返回了哪个值,1 还是 0?
谢谢你!
我希望我的Fabric任务有连字符( - )而不是下划线(_).例如,database-reset而不是database_reset.但是,不允许使用连字符作为Python函数名称.
Fabric中是否可以创建名称与相应Python函数不完全匹配的任务?
尝试使用python fabric脚本添加用户.
uid = prompt("Enter username:")
eid = prompt("Enter user's email address:")
password = generate_passwd()
run('adduser -g vpn -c %s %s') % (eid, uid)
run("echo -e '%s\n' | passwd %s") % (password, uid)
print (password)
Run Code Online (Sandbox Code Playgroud)
错误:
File "/tmp/hardway/fabfile.py", line 56, in addvpnuser
run('adduser -g vpn -c %s %s') % (eid, uid)
TypeError: not all arguments converted during string formatting
Run Code Online (Sandbox Code Playgroud)
知道我做错了什么.提前致谢.
我安装了Fabric via pip
.
得到pip
:
sudo apt-get install python-pip
Run Code Online (Sandbox Code Playgroud)
得到面料:
sudo pip install fabric
Run Code Online (Sandbox Code Playgroud)
(当我尝试w/out sudo时失败,并警告我文档或测试中缺少文件).
现在我得到以下内容:
$ which fab
/usr/local/bin/fab
$
$ fab
bash: /usr/bin/fab: No such file or directory
Run Code Online (Sandbox Code Playgroud)
咦!?我想which
搜索了PATH
bash将使用的解决方案.我误会了什么吗?Fabric,Pip或Bash应该归咎于此吗?我如何解决它?
我知道现在我可以跑/usr/local/bin/fab
或:
`which fab` deploy
Run Code Online (Sandbox Code Playgroud)
但我希望它能正确运行!
Fabric 包括prompt()操作来收集用户的输入:
fabric.operations.prompt(text, key=None, default='', validate=None)
Run Code Online (Sandbox Code Playgroud)
有一次,我要求用户输入密码:
pw = prompt('Password:')
Run Code Online (Sandbox Code Playgroud)
当用户键入他的密码时,它会以纯文本形式显示在 shell 中。是否可以隐藏用户的输入?
我正在将织物用于使用gunicorn的站点部署。我有以下由deploy()
函数调用的代码。
def restart_gunicorn():
sudo('ps ax|grep gunicorn')
sudo('pkill -HUP -f [g]unicorn.*master')
sudo('gunicorn -b 0.0.0.0:8080 %(path)s/application/wsgi &' % env)
Run Code Online (Sandbox Code Playgroud)
当我运行fab文件时,它显示以下内容:
out: 7694 pts/18 S+ 0:00 grep gunicorn
[127.0.0.1:2222] out:
[127.0.0.1:2222] sudo: pkill -HUP -f [g]unicorn.*master
[127.0.0.1:2222] out: sudo password:
[127.0.0.1:2222] out:
Fatal error: sudo() received nonzero return code 1 while executing!
Requested: pkill -HUP -f [g]unicorn.*master
Run Code Online (Sandbox Code Playgroud)
我认为它应该杀死gunicorn并重新启动它。我在这里缺少什么?
fabric ×8
python ×7
linux ×2
bash ×1
capistrano ×1
deployment ×1
django ×1
get ×1
gunicorn ×1
pip ×1
python-2.7 ×1
shell ×1