bash 如何在fabric(python部署工具)中的远程主机上创建一个新文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4114712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:52:27  来源:igfitidea点击:

How do I create a new file on a remote host in fabric (python deployment tool)?

pythonbashfabric

提问by Monika Sulik

I'd like to create a file with the name passenger_wsgi.py on a remote host. I'd like to use the following string to create the file's content:

我想在远程主机上创建一个名为passenger_wsgi.py 的文件。我想使用以下字符串来创建文件的内容:

'''
import sys, os

sys.path.insert(0, "/ruby/%s/www/%s/django-projects")
sys.path.insert(0, "/ruby/%s/www/%s/django-projects/project")

import django.core.handlers.wsgi
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'
application = django.core.handlers.wsgi.WSGIHandler()
''' % (user,host,user,host)

The user and host variables would be parameters of the fabric function.

用户和主机变量将是结构函数的参数。

I'm a total newbie to any sort of file manipulation in python, but also I'm not really sure what the procedure should be in fabric. Should I be creating the file locally and then uploading it with fabric's put command (and removing the local version afterwards)? Should I be creating the file on the remote host with an appropriate bash command (using fabric's run)? If so, then how is it best to deal with all the " and ' in the string - will fabric escape it? Or should I be tackling this in some different manner?

我是 python 中任何类型的文件操作的新手,但我也不确定在结构中应该使用什么程序。我应该在本地创建文件,然后使用fabric 的 put 命令上传它(然后删除本地版本)?我应该使用适当的 bash 命令(使用结构的运行)在远程主机上创建文件吗?如果是这样,那么最好如何处理字符串中的所有 " 和 ' - 织物会逃脱它吗?或者我应该以某种不同的方式解决这个问题?

采纳答案by jfs

You could use append()or upload_template()functions from fabric.contrib.files

您可以使用fabric.contrib.files 中的append()upload_template()函数

回答by Andrea Francia

Use StringIO with put:

将 StringIO 与 put 一起使用:

text = '''
import sys, os

sys.path.insert(0, "/ruby/%s/www/%s/django-projects")
sys.path.insert(0, "/ruby/%s/www/%s/django-projects/project")

import django.core.handlers.wsgi
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'
application = django.core.handlers.wsgi.WSGIHandler()
''' % (user, host, user, host)

import StringIO
put(StringIO.StringIO(text), "remote-path")

回答by Adam Vandenberg

What I do is have the file locally as something like "app.wsgi.template".

我所做的是在本地将文件作为“app.wsgi.template”之类的文件。

I then use tokens in the file, like:

然后我在文件中使用令牌,例如:

import sys, os

sys.path.insert(0, "/ruby/$HOST$/www/$HOST$/django-projects")
sys.path.insert(0, "/ruby/$HOST$/www/$HOST$/django-projects/project")

import django.core.handlers.wsgi
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'
application = django.core.handlers.wsgi.WSGIHandler()

I use fabric to "put" the file over to the remote host, then use "sed" (or equivalent functions in Python) to replace the "$HOST$" and "$USER$" tokens with the values I want.

我使用结构将文件“放置”到远程主机,然后使用“sed”(或 Python 中的等效函数)将“$HOST$”和“$USER$”标记替换为我想要的值。

run("sed -i backup -e 's/$USER$/%s' -e 's/$HOST$/%s' app.wsgi.template" % (user, host))
run("mv app.wsgi.template app.wsgi")

回答by Moses Liao GZ

StringIO with put works with a little bit of editing. Try this:

带有 put 的 StringIO 需要一些编辑。尝试这个:

put(StringIO.StringIO(
'''
import sys, os

sys.path.insert(0, "/ruby/%s/www/%s/django-projects")
sys.path.insert(0, "/ruby/%s/www/%s/django-projects/project")

import django.core.handlers.wsgi
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'
application = django.core.handlers.wsgi.WSGIHandler()
''' % (user,host,user,host)), "remote-path")

if you have an issue with permissions, try this:

如果您有权限问题,请尝试以下操作:

put(StringIO.StringIO(
'''
import sys, os

sys.path.insert(0, "/ruby/%s/www/%s/django-projects")
sys.path.insert(0, "/ruby/%s/www/%s/django-projects/project")

import django.core.handlers.wsgi
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'
application = django.core.handlers.wsgi.WSGIHandler()
''' % (user,host,user,host)), "remote-path", use_sudo=True)