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

前端 未结 4 832
别跟我提以往
别跟我提以往 2021-02-18 19:21

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 s         


        
相关标签:
4条回答
  • 2021-02-18 19:36

    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)
    
    0 讨论(0)
  • 2021-02-18 19:38

    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")
    
    0 讨论(0)
  • 2021-02-18 19:44

    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")
    
    0 讨论(0)
  • 2021-02-18 19:48

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

    0 讨论(0)
提交回复
热议问题