By Tianyi
Fabric is a Python library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks
from fabric.api import env, local, task, settings
env.hosts = ['123.123.1.1'] # Some remote hosts ip
@task
def install_postgres_locally():
with settings(user='root'):
local('apt-get install postgresql postgresql-client')
@task
def install_postgres_remotely():
with settings(user='root'):
run('apt-get install postgresql postgresql-client')
fab install_postgres_locally
fab install_postgres_remotely
fabtools includes useful functions to help you write your Fabric files.
fabtools makes it easier to manage system users, packages, databases, etc.
from fabric.api import local, task, settings
from fabtools import deb, arch
@task
install_postgres_deb():
with settings(user='root'):
deb.install('postgresql postgresql-client')
@task
def install_postgres_arch():
with settings(user='root')
arch.install('postgresql postgresql-client')
Talk is cheap. Show me the code-- Torvalds, Linus (2000-08-25)
Normal we can just have uWSGI, Nginx, Postgresql configuration files ready, and use Fabric command to copy the files over.
put('/path/to/local/uwsgi.ini, '/path/to/remote/uwsgi.ini')
We can go further than this :)
JJinja2 is a full featured template engine for Python. It has full unicode support, an optional integrated sandboxed execution environment, widely used and BSD licensed.
# the base directory (full path)
{% if chdir %}
chdir = {{ chdir }}
{% endif %}
# Django's wsgi file
{% if project_name %}
module = {{ project_name }}.wsgi
{% endif %}
{% if virtualenv_path %}
virtualenv = {{ virtualenv_path }}
{% endif %}
# process-related settings
master = true
{% if processes %}
processes = {{ processes }}
{% else %}
processes = 4
{% endif %}
...
from fabric.api import env, task, settings, run, cd
from jinja2 import Environment, FileSystemLoader
@task
def create_uwsgi_conf():
templateLoader = FileSystemLoader(searchpath="templates")
template_env = Environment(loader=templateLoader)
template = template_env.get_template('uwsgi.ini')
uwsgi_params = {}
uwsgi_params['chdir'] = env.project_path
uwsgi_params['project_name'] = env.project_name
uwsgi_params['virtualenv_path'] = env.virtualenv_path
uwsgi_params['processes'] = 10
uwsgi_params['socket_path'] = env.project_path
with open('./uwsgi.ini', 'w') as output_file:
output_file.write(template.render(uwsgi_params))
Hope this will work......
@3quarterstack