Python Fabric for remote deployment

Source: Internet
Author: User
Python Fabric for remote deployment

Requirements Description

In a multi-person collaborative development project, almost every day we have to submit code to the GIT server, and then deploy to the test server, every day is knocking over the repeated lines of command, it is boring. What to do? Operation and Maintenance Automation! Let's talk about the fabric, do some repetitive work for us, I believe you will like it as I do!

Background of this project

The project we're doing is using the Django framework, and every day after we commit the code to the GIT server, we manually upload the code to the test server and execute a series of commands from the Django framework. Every day to waste more than 10 minutes of time, doing repetitive work, these jobs are not a programmer to do ...

Solution Solutions

The Python fabric module allows you to solidify the commands for automated deployment or multi-machine operations into a single script and execute them through this script.

Install Fabric

Note: Both the local and the target servers need to be installed
sudo easy_install fabric

or install with PIP:

Pip Install Fabric

Writing scripts

Local is executed on the local machine; Run is performed at the remote machine

From FABRIC.API import hosts, run, env, local, CD, GET, Lcdfrom fabric.tasks import Execute env.hosts = ["fab@192.168.1.10 1:22 "," root@192.168.1.101:22 "]env.passwords = {" fab@192.168.1.101:22 ":" Fab "," root@192.168.1.101:22 ":" Tofabor "} @ Hosts ("Ktv@192.168.1.101:22") def update (): "" "Update Test server Code" "With CD ("/opt/project/project "): # Enter the project directory of the test server run (" Git p ull Origin Master ") # from the GIT server's master branch, pull up the latest code run ("/usr/local/bin/python2.7/opt/project/project/manage.py Makemigrations ") # This is the command run ("/usr/local/bin/python2.7/opt/project/project/manage.py migrate ") of the Django framework to detect database changes # This is the command for the Django framework to perform database changes @hosts ("ktv@192.168.1.101:22") def restart (): "" "Restart Service" "Execute (' Stop ') execute (' Start ') @hosts ("Root@192.168.1.101:22") def start (): "" "Start Service" "" With CD ("/opt/project/project"): Run ("supervisorctl start dev") @hos TS ("ktv@192.168.1.101:22") def Stop (): "" "Stop Service" "" PIDs = Run ("Ps-ef |grep ' 9001 ' | awk ' {print $} ') Pid_list = Pids.split (' \ r \ n ') for I in Pid_list[:-2]: Run (' Kill-9%s '% i) # Kill the running service process 

If the script is saved as fabfile.py (can also be saved as a different name, just run the command is not the same, detailed below)

Execute script

If your script name is fabfile.py, then you can enter your fabfile.py directory at the terminal and enter the following command:

Fab update

Immediately after, you will see the terminal prompts you to enter a git account and password, after you enter the successful, will automatically pull down the GIT server code to the test server.
Then run the following command to restart the service:

Fab restart

If your file name is a different name, such as ab.py, then executing fab update/restart is wrong, how does the cloud run?

Fab-f AB Update
Fab-f AB Restart

Note: Fabric is quite powerful, this article just lists a small feature. For further study, please see the official documentation http://docs.fabfile.org/en/1.6/

The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support topic.alibabacloud.com.

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.