Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 474
Monika Sulik Avatar asked Nov 06 '10 19:11

Monika Sulik


People also ask

What is the fabric command to execute a shell command remotely?

run (fabric.Fabric's run procedure is used for executing a shell command on one or more remote hosts.

What is a fabric file?

Product Overview. The Enterprise File Fabric platform is a data management and protection platform that unifies on-premises and on-cloud company storage assets. Users gain easy, secure, remote access to Amazon S3, Amazon FSx and other object and file storage through file-oriented interfaces and tools.

What is Fab command?

fab uses Python's optparse library, meaning that it honors typical Linux or GNU style short and long options, as well as freely mixing options and arguments. E.g. fab task1 -H hostname task2 -i path/to/keyfile is just as valid as the more straightforward fab -H hostname -i path/to/keyfile task1 task2 .


3 Answers

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")
like image 120
Andrea Francia Avatar answered Oct 19 '22 23:10

Andrea Francia


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

like image 44
jfs Avatar answered Oct 19 '22 23:10

jfs


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")
like image 38
Adam Vandenberg Avatar answered Oct 20 '22 00:10

Adam Vandenberg