Many people may use FTP and rsync to synchronize codes to server, this way is fine but it may be troublesome when you need to deploy many times a day, actually there is a simple way if you can spend time on finding the fast way. We introduce Fabric today for deploying web app to remote server.
Fabric is a deployment tool written with Python, the biggest feature if it is you no need to login to remote server, you can execute remote commands locally.
Here is s simple deployment script written with Python : fabfile.py(Do not change the file name). Put it in your project directory.
#!/usr/bin/env python # -*- coding: utf-8 -*- from datetime import datetime from fabric.api import * # Server username and hostname env.user = 'root' env.hosts = ['www.example.com'] # If there are many hosts, Fabric will deploy one by one # Define a pack() function def pack(): # 打一个tar包: tar_files = ['*.py', 'static/*', 'templates/*', 'favicon.ico'] local('rm -f example.tar.gz') local('tar -czvf example.tar.gz --exclude=\'*.tar.gz\' --exclude=\'fabfile.py\' %s' % ' '.join(tar_files)) # Define deploy function def deploy(): # Temp file in remote server remote_tmp_tar = '/tmp/example.tar.gz' tag = datetime.now().strftime('%y.%m.%d_%H.%M.%S') run('rm -f %s' % remote_tmp_tar) # Upload temp file to remote server put('shici.tar.gz', remote_tmp_tar) # Unpack remote_dist_dir = '/srv/www.example.com@%s' % tag remote_dist_link = '/srv/www.example.com' run('mkdir %s' % remote_dist_dir) with cd(remote_dist_dir): run('tar -xzvf %s' % remote_tmp_tar) # Give permission to www-data: run('chown -R www-data:www-data %s' % remote_dist_dir) # Delete old soft links run('rm -f %s' % remote_dist_link) # Create new soft links run('ln -s %s %s' % (remote_dist_dir, remote_dist_link)) run('chown -R www-data:www-data %s' % remote_dist_link) # Restartb fastcgi: fcgi = '/etc/init.d/py-fastcgi' with settings(warn_only=True): run('%s stop' % fcgi) run('%s start' % fcgi)
There are pack(0 and deploy() two functions, if we use Fabric to deploy, we only need to type following two commands:
$ fab pack $ fab deploy
Fabric provides some simple APIs to complete all deployment tasks, the most frequently used are local() and run(), they will run commands locally and remotely, put() will upload local files to remote server, when we want to upload files to some specified directory, we only need to use with cd('/path/to/dir/'):
By default Fabric will stop executing commands when some command fails to execute, if we want to allow to continue to execute following commands, for example run('rm /tmp/abc') may fail when the file doesn't exist, now we can use with settings(warn_only=True):Fabric may only give warning messages without stopping executing commands.
How does Fabric execute commands remotely? Actually all operations of Fabric are based on SSH, it may prompt to enter token when necessary, so it is very secure. The better way is to create a passwordless SSH connection to the remote server with a certification.
If we have a team project, we can use Fabric and version control system to automatically detect code change, test, pack and deploy codes, because all commands executed by Fabric are some basic Linux commands, if you know Linux, you may be able to use Fabric easily.
It's very simple to use Fabric to deploy Python, Ruby and PHP which are not need to be compiled, but for Java and C#, it's a bit complicated, the compilation itself is very complicated, we may need to use some specified tools or IDEs. It's very hard to be automatic.
For more information about Fabric, please see : http://docs.fabfile.org/en/1.5/
Author :廖雪峰 Source : http://www.liaoxuefeng.com/article/045ed978123946f596213485623b7e68