Simple Deployment of Fabric in Python

Source: Internet
Author: User
This article mainly introduces the simple deployment method of Fabric in Python. Fabric is the next popular automation tool in Python. if you need it, refer to Fabric as a deployment tool developed using Python, the biggest feature is that you can easily deploy several lines of Python scripts by running remote commands locally without logging on to the remote server.
Document Portal
Simple Installation

sudo easy_install fabric

Deployment script

#! /Usr/bin/env python #-*-coding: UTF-8-*-from datetime import datetimefrom fabric. api import * # login user and host name: env. user = 'root' env. hosts = ['www .example.com '] # if multiple hosts exist, fabric will automatically deploy def pack () in sequence: 'define a pack task' # Create a tar package: 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) def deploy (): 'define a deployment task' # temporary remote server File: 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 the tar file to the remote server: put('shici.tar.gz ', remote_tmp_tar) # decompress: tags = '/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) # set the www-data permission for the new directory: run ('chown-R www-data: www-data % s' % remote_dist_dir) # delete the old soft link: run ('rm-f % s' % remote_dist_link) # Create a new soft link and direct it to the newly deployed Directory: run ('ln-s % s' % (remote_dist_dir, remote_dist_link) run ('chown-R www-data: www-data % s' % remote_dist_link) # restart fastcgi: fcgi = '/etc/init. d/py-fastcgi 'with settings (warn_only = True): run (' % s stop' % fcgi) run ('% s start' % fcgi)

The preceding two tasks, pack and deploy, are defined. If you deploy the task with Fabric, simply enter the following two commands:

$ fab pack$ fab deploy

Fabric provides several simple APIs to complete all deployment. The most common APIs are local () and run (), which execute commands locally and remotely, put () you can upload local files to a remote directory. to remotely specify the current directory, you only need to use with cd ('/path/to/dir.

By default, Fabric stops executing subsequent commands when the command fails. Sometimes, we can ignore failed commands for execution. for example, run ('rm/tmp/ABC') may fail if the file does not exist, at this time, you can use with settings (warn_only = True): to execute the command, so that Fabric will only generate a warning message without interrupting the execution.

How does Fabric remotely execute commands? In fact, all Fabric operations are performed based on SSH. if necessary, it will prompt you to enter a password, so it is very secure. A better way is to configure a password-less ssh connection with a certificate on the specified deployment server.

If it is developed based on a team, Fabric can use the version library to automatically detect code and perform testing, packaging, and deployment tasks. Fabric runs basic Linux commands. Therefore, Fabric does not need to be extended. Fabric can be deployed with Linux commands.

Using Fabric to deploy non-compiled website applications such as Python, Ruby, and PHP is very convenient, and it is troublesome for compiled Java and C, compilation itself is an extremely complex big project that relies on specific tools or IDE and is difficult to automate.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.