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

发布于 2024-10-01 07:39:41 字数 628 浏览 5 评论 0原文

我想在远程主机上创建一个名为 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)

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

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

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:

'''
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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

兮颜 2024-10-08 07:39:41

将 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")

Use StringIO with 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")
留一抹残留的笑 2024-10-08 07:39:41

您可以使用 fabric.contrib.files

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

丿*梦醉红颜 2024-10-08 07:39:41

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

然后,我在文件中使用标记,例如:

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()

我使用 Fabric 将文件“放置”到远程主机,然后使用“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")

What I do is have the file locally as something like "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.

run("sed -i backup -e 's/$USER$/%s' -e 's/$HOST$/%s' app.wsgi.template" % (user, host))
run("mv app.wsgi.template app.wsgi")
捎一片雪花 2024-10-08 07:39:41

带 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")

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

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)

StringIO with put works with a little bit of editing. 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")

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文